blob: 0cc7313944f5e6daebe4c906f207163cb31e921a [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,
Fugang Duanba593e02014-09-13 05:00:53 +0800160 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800161};
162
163static const struct of_device_id fec_dt_ids[] = {
164 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
165 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
166 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000167 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000168 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800169 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800170 { /* sentinel */ }
171};
172MODULE_DEVICE_TABLE(of, fec_dt_ids);
173
Shawn Guo49da97d2011-01-05 21:13:11 +0000174static unsigned char macaddr[ETH_ALEN];
175module_param_array(macaddr, byte, NULL, 0);
176MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
177
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000178#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * Some hardware gets it MAC address out of local flash memory.
181 * if this is non-zero then assume it is the address to get MAC from.
182 */
183#if defined(CONFIG_NETtel)
184#define FEC_FLASHMAC 0xf0006006
185#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
186#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#elif defined(CONFIG_CANCam)
188#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000189#elif defined (CONFIG_M5272C3)
190#define FEC_FLASHMAC (0xffe04000 + 4)
191#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000192#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#else
194#define FEC_FLASHMAC 0
195#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800196#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000197
Jim Baxtercdffcf12013-07-02 22:52:56 +0100198/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100200#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100202#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jim Baxter4c09eed2013-04-19 08:10:49 +0000204/* FEC receive acceleration */
205#define FEC_RACC_IPDIS (1 << 1)
206#define FEC_RACC_PRODIS (1 << 2)
207#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Matt Waddel6b265292006-06-27 13:10:56 +1000210 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * size bits. Other FEC hardware does not, so we need to take that into
212 * account when setting it.
213 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000214#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100215 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
217#else
218#define OPT_FRAME_SIZE 0
219#endif
220
Bryan Wue6b043d2010-03-31 02:10:44 +0000221/* FEC MII MMFR bits definition */
222#define FEC_MMFR_ST (1 << 30)
223#define FEC_MMFR_OP_READ (2 << 28)
224#define FEC_MMFR_OP_WRITE (1 << 28)
225#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
226#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
227#define FEC_MMFR_TA (2 << 16)
228#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500230#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Sascha Hauer22f6b862009-04-15 01:32:18 +0000232/* Transmitter timeout */
233#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Frank Libaa70a52013-01-16 16:55:58 +0000235#define FEC_PAUSE_FLAG_AUTONEG 0x1
236#define FEC_PAUSE_FLAG_ENABLE 0x2
237
Nimrod Andy79f33912014-06-12 08:16:23 +0800238#define TSO_HEADER_SIZE 128
239/* Max number of allowed TCP segments for software TSO */
240#define FEC_MAX_TSO_SEGS 100
241#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
242
243#define IS_TSO_HEADER(txq, addr) \
244 ((addr >= txq->tso_hdrs_dma) && \
245 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
246
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000247static int mii_cnt;
248
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800249static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800250struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
251 struct fec_enet_private *fep,
252 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000253{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800254 struct bufdesc *new_bd = bdp + 1;
255 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800256 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
257 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800258 struct bufdesc_ex *ex_base;
259 struct bufdesc *base;
260 int ring_size;
261
Fugang Duan4d494cd2014-09-13 05:00:48 +0800262 if (bdp >= txq->tx_bd_base) {
263 base = txq->tx_bd_base;
264 ring_size = txq->tx_ring_size;
265 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800266 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800267 base = rxq->rx_bd_base;
268 ring_size = rxq->rx_ring_size;
269 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800270 }
271
272 if (fep->bufdesc_ex)
273 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
274 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000275 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800276 return (new_bd >= (base + ring_size)) ?
277 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000278}
279
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800280static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800281struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
282 struct fec_enet_private *fep,
283 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000284{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800285 struct bufdesc *new_bd = bdp - 1;
286 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800287 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
288 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800289 struct bufdesc_ex *ex_base;
290 struct bufdesc *base;
291 int ring_size;
292
Fugang Duan4d494cd2014-09-13 05:00:48 +0800293 if (bdp >= txq->tx_bd_base) {
294 base = txq->tx_bd_base;
295 ring_size = txq->tx_ring_size;
296 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800297 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800298 base = rxq->rx_bd_base;
299 ring_size = rxq->rx_ring_size;
300 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800301 }
302
303 if (fep->bufdesc_ex)
304 return (struct bufdesc *)((ex_new_bd < ex_base) ?
305 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000306 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800307 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000308}
309
Nimrod Andy61a44272014-06-12 08:16:18 +0800310static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
311 struct fec_enet_private *fep)
312{
313 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
314}
315
Fugang Duan4d494cd2014-09-13 05:00:48 +0800316static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
317 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800318{
319 int entries;
320
Fugang Duan4d494cd2014-09-13 05:00:48 +0800321 entries = ((const char *)txq->dirty_tx -
322 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800323
Fugang Duan4d494cd2014-09-13 05:00:48 +0800324 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800325}
326
Shawn Guob5680e02011-01-05 21:13:13 +0000327static void *swap_buffer(void *bufaddr, int len)
328{
329 int i;
330 unsigned int *buf = bufaddr;
331
Fabio Estevamffed61e2013-05-21 05:44:26 +0000332 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000333 *buf = cpu_to_be32(*buf);
334
335 return bufaddr;
336}
337
Russell King344756f2014-07-08 13:01:59 +0100338static void fec_dump(struct net_device *ndev)
339{
340 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800341 struct bufdesc *bdp;
342 struct fec_enet_priv_tx_q *txq;
343 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100344
345 netdev_info(ndev, "TX ring dump\n");
346 pr_info("Nr SC addr len SKB\n");
347
Fugang Duan4d494cd2014-09-13 05:00:48 +0800348 txq = fep->tx_queue[0];
349 bdp = txq->tx_bd_base;
350
Russell King344756f2014-07-08 13:01:59 +0100351 do {
352 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
353 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800354 bdp == txq->cur_tx ? 'S' : ' ',
355 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100356 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800357 txq->tx_skbuff[index]);
358 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100359 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800360 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100361}
362
Fugang Duan62a02c92014-06-18 08:33:52 +0800363static inline bool is_ipv4_pkt(struct sk_buff *skb)
364{
365 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
366}
367
Jim Baxter4c09eed2013-04-19 08:10:49 +0000368static int
369fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
370{
371 /* Only run for packets requiring a checksum. */
372 if (skb->ip_summed != CHECKSUM_PARTIAL)
373 return 0;
374
375 if (unlikely(skb_cow_head(skb, 0)))
376 return -1;
377
Fugang Duan62a02c92014-06-18 08:33:52 +0800378 if (is_ipv4_pkt(skb))
379 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000380 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
381
382 return 0;
383}
384
Nimrod Andy6e909282014-06-12 08:16:22 +0800385static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800386fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
387 struct sk_buff *skb,
388 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800389{
390 struct fec_enet_private *fep = netdev_priv(ndev);
391 const struct platform_device_id *id_entry =
392 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800393 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800394 struct bufdesc_ex *ebdp;
395 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800396 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800397 int frag, frag_len;
398 unsigned short status;
399 unsigned int estatus = 0;
400 skb_frag_t *this_frag;
401 unsigned int index;
402 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100403 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800404 int i;
405
406 for (frag = 0; frag < nr_frags; frag++) {
407 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800408 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800409 ebdp = (struct bufdesc_ex *)bdp;
410
411 status = bdp->cbd_sc;
412 status &= ~BD_ENET_TX_STATS;
413 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
414 frag_len = skb_shinfo(skb)->frags[frag].size;
415
416 /* Handle the last BD specially */
417 if (frag == nr_frags - 1) {
418 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
419 if (fep->bufdesc_ex) {
420 estatus |= BD_ENET_TX_INT;
421 if (unlikely(skb_shinfo(skb)->tx_flags &
422 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
423 estatus |= BD_ENET_TX_TS;
424 }
425 }
426
427 if (fep->bufdesc_ex) {
428 if (skb->ip_summed == CHECKSUM_PARTIAL)
429 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
430 ebdp->cbd_bdu = 0;
431 ebdp->cbd_esc = estatus;
432 }
433
434 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
435
Fugang Duan4d494cd2014-09-13 05:00:48 +0800436 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800437 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
438 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800439 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
440 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800441
442 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
443 swap_buffer(bufaddr, frag_len);
444 }
445
Russell Kingd6bf3142014-07-08 00:23:19 +0100446 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
447 DMA_TO_DEVICE);
448 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800449 dev_kfree_skb_any(skb);
450 if (net_ratelimit())
451 netdev_err(ndev, "Tx DMA memory map failed\n");
452 goto dma_mapping_error;
453 }
454
Russell Kingd6bf3142014-07-08 00:23:19 +0100455 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800456 bdp->cbd_datlen = frag_len;
457 bdp->cbd_sc = status;
458 }
459
Fugang Duan4d494cd2014-09-13 05:00:48 +0800460 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800461
462 return 0;
463
464dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800465 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800466 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800467 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800468 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
469 bdp->cbd_datlen, DMA_TO_DEVICE);
470 }
471 return NETDEV_TX_OK;
472}
473
Fugang Duan4d494cd2014-09-13 05:00:48 +0800474static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
475 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800476{
477 struct fec_enet_private *fep = netdev_priv(ndev);
478 const struct platform_device_id *id_entry =
479 platform_get_device_id(fep->pdev);
480 int nr_frags = skb_shinfo(skb)->nr_frags;
481 struct bufdesc *bdp, *last_bdp;
482 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100483 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800484 unsigned short status;
485 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800486 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800487 unsigned int estatus = 0;
488 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800489 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800490 int ret;
491
Fugang Duan4d494cd2014-09-13 05:00:48 +0800492 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800493 if (entries_free < MAX_SKB_FRAGS + 1) {
494 dev_kfree_skb_any(skb);
495 if (net_ratelimit())
496 netdev_err(ndev, "NOT enough BD for SG!\n");
497 return NETDEV_TX_OK;
498 }
499
Nimrod Andy6e909282014-06-12 08:16:22 +0800500 /* Protocol checksum off-load for TCP and UDP. */
501 if (fec_enet_clear_csum(skb, ndev)) {
502 dev_kfree_skb_any(skb);
503 return NETDEV_TX_OK;
504 }
505
506 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800507 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800508 status = bdp->cbd_sc;
509 status &= ~BD_ENET_TX_STATS;
510
511 /* Set buffer length and buffer pointer */
512 bufaddr = skb->data;
513 buflen = skb_headlen(skb);
514
Fugang Duan4d494cd2014-09-13 05:00:48 +0800515 queue = skb_get_queue_mapping(skb);
516 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800517 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
518 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800519 memcpy(txq->tx_bounce[index], skb->data, buflen);
520 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800521
522 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
523 swap_buffer(bufaddr, buflen);
524 }
525
Russell Kingd6bf3142014-07-08 00:23:19 +0100526 /* Push the data cache so the CPM does not get stale memory data. */
527 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
528 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800529 dev_kfree_skb_any(skb);
530 if (net_ratelimit())
531 netdev_err(ndev, "Tx DMA memory map failed\n");
532 return NETDEV_TX_OK;
533 }
534
535 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800536 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800537 if (ret)
538 return ret;
539 } else {
540 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
541 if (fep->bufdesc_ex) {
542 estatus = BD_ENET_TX_INT;
543 if (unlikely(skb_shinfo(skb)->tx_flags &
544 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
545 estatus |= BD_ENET_TX_TS;
546 }
547 }
548
549 if (fep->bufdesc_ex) {
550
551 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
552
553 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
554 fep->hwts_tx_en))
555 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
556
557 if (skb->ip_summed == CHECKSUM_PARTIAL)
558 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
559
560 ebdp->cbd_bdu = 0;
561 ebdp->cbd_esc = estatus;
562 }
563
Fugang Duan4d494cd2014-09-13 05:00:48 +0800564 last_bdp = txq->cur_tx;
565 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800566 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800567 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800568
569 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100570 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800571
572 /* Send it on its way. Tell FEC it's ready, interrupt when done,
573 * it's the last BD of the frame, and to put the CRC on the end.
574 */
575 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
576 bdp->cbd_sc = status;
577
Sascha Hauer22f6b862009-04-15 01:32:18 +0000578 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800579 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800581 skb_tx_timestamp(skb);
582
Fugang Duan4d494cd2014-09-13 05:00:48 +0800583 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Frank Lide5fb0a2013-03-03 17:34:25 +0000585 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800586 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000587
Nimrod Andy6e909282014-06-12 08:16:22 +0800588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Nimrod Andy79f33912014-06-12 08:16:23 +0800591static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800592fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
593 struct net_device *ndev,
594 struct bufdesc *bdp, int index, char *data,
595 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800596{
597 struct fec_enet_private *fep = netdev_priv(ndev);
598 const struct platform_device_id *id_entry =
599 platform_get_device_id(fep->pdev);
600 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
601 unsigned short status;
602 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100603 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800604
605 status = bdp->cbd_sc;
606 status &= ~BD_ENET_TX_STATS;
607
608 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800609
610 if (((unsigned long) data) & FEC_ALIGNMENT ||
611 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800612 memcpy(txq->tx_bounce[index], data, size);
613 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800614
615 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
616 swap_buffer(data, size);
617 }
618
Russell Kingd6bf3142014-07-08 00:23:19 +0100619 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
620 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800621 dev_kfree_skb_any(skb);
622 if (net_ratelimit())
623 netdev_err(ndev, "Tx DMA memory map failed\n");
624 return NETDEV_TX_BUSY;
625 }
626
Russell Kingd6bf3142014-07-08 00:23:19 +0100627 bdp->cbd_datlen = size;
628 bdp->cbd_bufaddr = addr;
629
Nimrod Andy79f33912014-06-12 08:16:23 +0800630 if (fep->bufdesc_ex) {
631 if (skb->ip_summed == CHECKSUM_PARTIAL)
632 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
633 ebdp->cbd_bdu = 0;
634 ebdp->cbd_esc = estatus;
635 }
636
637 /* Handle the last BD specially */
638 if (last_tcp)
639 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
640 if (is_last) {
641 status |= BD_ENET_TX_INTR;
642 if (fep->bufdesc_ex)
643 ebdp->cbd_esc |= BD_ENET_TX_INT;
644 }
645
646 bdp->cbd_sc = status;
647
648 return 0;
649}
650
651static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800652fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
653 struct sk_buff *skb, struct net_device *ndev,
654 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800655{
656 struct fec_enet_private *fep = netdev_priv(ndev);
657 const struct platform_device_id *id_entry =
658 platform_get_device_id(fep->pdev);
659 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
660 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
661 void *bufaddr;
662 unsigned long dmabuf;
663 unsigned short status;
664 unsigned int estatus = 0;
665
666 status = bdp->cbd_sc;
667 status &= ~BD_ENET_TX_STATS;
668 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
669
Fugang Duan4d494cd2014-09-13 05:00:48 +0800670 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
671 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800672 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
673 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800674 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
675 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800676
677 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
678 swap_buffer(bufaddr, hdr_len);
679
680 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
681 hdr_len, DMA_TO_DEVICE);
682 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
683 dev_kfree_skb_any(skb);
684 if (net_ratelimit())
685 netdev_err(ndev, "Tx DMA memory map failed\n");
686 return NETDEV_TX_BUSY;
687 }
688 }
689
690 bdp->cbd_bufaddr = dmabuf;
691 bdp->cbd_datlen = hdr_len;
692
693 if (fep->bufdesc_ex) {
694 if (skb->ip_summed == CHECKSUM_PARTIAL)
695 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
696 ebdp->cbd_bdu = 0;
697 ebdp->cbd_esc = estatus;
698 }
699
700 bdp->cbd_sc = status;
701
702 return 0;
703}
704
Fugang Duan4d494cd2014-09-13 05:00:48 +0800705static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
706 struct sk_buff *skb,
707 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800708{
709 struct fec_enet_private *fep = netdev_priv(ndev);
710 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
711 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800712 struct bufdesc *bdp = txq->cur_tx;
713 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800714 struct tso_t tso;
715 unsigned int index = 0;
716 int ret;
717
Fugang Duan4d494cd2014-09-13 05:00:48 +0800718 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800719 dev_kfree_skb_any(skb);
720 if (net_ratelimit())
721 netdev_err(ndev, "NOT enough BD for TSO!\n");
722 return NETDEV_TX_OK;
723 }
724
725 /* Protocol checksum off-load for TCP and UDP. */
726 if (fec_enet_clear_csum(skb, ndev)) {
727 dev_kfree_skb_any(skb);
728 return NETDEV_TX_OK;
729 }
730
731 /* Initialize the TSO handler, and prepare the first payload */
732 tso_start(skb, &tso);
733
734 total_len = skb->len - hdr_len;
735 while (total_len > 0) {
736 char *hdr;
737
Fugang Duan4d494cd2014-09-13 05:00:48 +0800738 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800739 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
740 total_len -= data_left;
741
742 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800743 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800744 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800745 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800746 if (ret)
747 goto err_release;
748
749 while (data_left > 0) {
750 int size;
751
752 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800753 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
754 index = fec_enet_get_bd_index(txq->tx_bd_base,
755 bdp, fep);
756 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
757 bdp, index,
758 tso.data, size,
759 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800760 total_len == 0);
761 if (ret)
762 goto err_release;
763
764 data_left -= size;
765 tso_build_data(skb, &tso, size);
766 }
767
Fugang Duan4d494cd2014-09-13 05:00:48 +0800768 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800769 }
770
771 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800772 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800773
Nimrod Andy79f33912014-06-12 08:16:23 +0800774 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800775 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800776
777 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800778 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800779
780 return 0;
781
782err_release:
783 /* TODO: Release all used data descriptors for TSO */
784 return ret;
785}
786
Nimrod Andy61a44272014-06-12 08:16:18 +0800787static netdev_tx_t
788fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
789{
790 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800791 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800792 unsigned short queue;
793 struct fec_enet_priv_tx_q *txq;
794 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800795 int ret;
796
Fugang Duan4d494cd2014-09-13 05:00:48 +0800797 queue = skb_get_queue_mapping(skb);
798 txq = fep->tx_queue[queue];
799 nq = netdev_get_tx_queue(ndev, queue);
800
Nimrod Andy79f33912014-06-12 08:16:23 +0800801 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800802 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800803 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800804 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800805 if (ret)
806 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800807
Fugang Duan4d494cd2014-09-13 05:00:48 +0800808 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
809 if (entries_free <= txq->tx_stop_threshold)
810 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800811
812 return NETDEV_TX_OK;
813}
814
Frank Li14109a52013-03-26 16:12:03 +0000815/* Init RX & TX buffer descriptors
816 */
817static void fec_enet_bd_init(struct net_device *dev)
818{
819 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800820 struct fec_enet_priv_tx_q *txq;
821 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000822 struct bufdesc *bdp;
823 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800824 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000825
Frank Li59d0f7462014-09-13 05:00:50 +0800826 for (q = 0; q < fep->num_rx_queues; q++) {
827 /* Initialize the receive buffer descriptors. */
828 rxq = fep->rx_queue[q];
829 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800830
Frank Li59d0f7462014-09-13 05:00:50 +0800831 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000832
Frank Li59d0f7462014-09-13 05:00:50 +0800833 /* Initialize the BD for every fragment in the page. */
834 if (bdp->cbd_bufaddr)
835 bdp->cbd_sc = BD_ENET_RX_EMPTY;
836 else
837 bdp->cbd_sc = 0;
838 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000839 }
Frank Li59d0f7462014-09-13 05:00:50 +0800840
841 /* Set the last buffer to wrap */
842 bdp = fec_enet_get_prevdesc(bdp, fep, q);
843 bdp->cbd_sc |= BD_SC_WRAP;
844
845 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000846 }
847
Frank Li59d0f7462014-09-13 05:00:50 +0800848 for (q = 0; q < fep->num_tx_queues; q++) {
849 /* ...and the same for transmit */
850 txq = fep->tx_queue[q];
851 bdp = txq->tx_bd_base;
852 txq->cur_tx = bdp;
853
854 for (i = 0; i < txq->tx_ring_size; i++) {
855 /* Initialize the BD for every fragment in the page. */
856 bdp->cbd_sc = 0;
857 if (txq->tx_skbuff[i]) {
858 dev_kfree_skb_any(txq->tx_skbuff[i]);
859 txq->tx_skbuff[i] = NULL;
860 }
861 bdp->cbd_bufaddr = 0;
862 bdp = fec_enet_get_nextdesc(bdp, fep, q);
863 }
864
865 /* Set the last buffer to wrap */
866 bdp = fec_enet_get_prevdesc(bdp, fep, q);
867 bdp->cbd_sc |= BD_SC_WRAP;
868 txq->dirty_tx = bdp;
869 }
870}
871
Frank Lice99d0d2014-09-13 05:00:52 +0800872static void fec_enet_active_rxring(struct net_device *ndev)
873{
874 struct fec_enet_private *fep = netdev_priv(ndev);
875 int i;
876
877 for (i = 0; i < fep->num_rx_queues; i++)
878 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
879}
880
Frank Li59d0f7462014-09-13 05:00:50 +0800881static void fec_enet_enable_ring(struct net_device *ndev)
882{
883 struct fec_enet_private *fep = netdev_priv(ndev);
884 struct fec_enet_priv_tx_q *txq;
885 struct fec_enet_priv_rx_q *rxq;
886 int i;
887
888 for (i = 0; i < fep->num_rx_queues; i++) {
889 rxq = fep->rx_queue[i];
890 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
891
892 /* enable DMA1/2 */
893 if (i)
894 writel(RCMR_MATCHEN | RCMR_CMP(i),
895 fep->hwp + FEC_RCMR(i));
896 }
897
898 for (i = 0; i < fep->num_tx_queues; i++) {
899 txq = fep->tx_queue[i];
900 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
901
902 /* enable DMA1/2 */
903 if (i)
904 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
905 fep->hwp + FEC_DMA_CFG(i));
906 }
907}
908
909static void fec_enet_reset_skb(struct net_device *ndev)
910{
911 struct fec_enet_private *fep = netdev_priv(ndev);
912 struct fec_enet_priv_tx_q *txq;
913 int i, j;
914
915 for (i = 0; i < fep->num_tx_queues; i++) {
916 txq = fep->tx_queue[i];
917
918 for (j = 0; j < txq->tx_ring_size; j++) {
919 if (txq->tx_skbuff[j]) {
920 dev_kfree_skb_any(txq->tx_skbuff[j]);
921 txq->tx_skbuff[j] = NULL;
922 }
923 }
924 }
Frank Li14109a52013-03-26 16:12:03 +0000925}
926
Russell Kingdbc64a82014-07-08 12:40:12 +0100927/*
928 * This function is called to start or restart the FEC during a link
929 * change, transmit timeout, or to reconfigure the FEC. The network
930 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100931 */
932static void
Russell Kingef833372014-07-08 12:40:43 +0100933fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100934{
935 struct fec_enet_private *fep = netdev_priv(ndev);
936 const struct platform_device_id *id_entry =
937 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000938 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100939 u32 temp_mac[2];
940 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000941 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100942
Fugang Duan106c3142014-09-13 05:00:51 +0800943 /* Whack a reset. We should wait for this.
944 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
945 * instead of reset MAC itself.
946 */
947 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
948 writel(0, fep->hwp + FEC_ECNTRL);
949 } else {
950 writel(1, fep->hwp + FEC_ECNTRL);
951 udelay(10);
952 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100953
954 /*
955 * enet-mac reset will reset mac address registers too,
956 * so need to reconfigure it.
957 */
958 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
959 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
960 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
961 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
962 }
963
964 /* Clear any outstanding interrupt. */
965 writel(0xffc00000, fep->hwp + FEC_IEVENT);
966
Uwe Kleine-König45993652011-01-19 20:47:04 +0100967 /* Set maximum receive buffer size. */
968 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
969
Frank Li14109a52013-03-26 16:12:03 +0000970 fec_enet_bd_init(ndev);
971
Frank Li59d0f7462014-09-13 05:00:50 +0800972 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100973
Frank Li59d0f7462014-09-13 05:00:50 +0800974 /* Reset tx SKB buffers. */
975 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100976
977 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100978 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100979 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100980 writel(0x04, fep->hwp + FEC_X_CNTRL);
981 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100982 /* No Rcv on Xmit */
983 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100984 writel(0x0, fep->hwp + FEC_X_CNTRL);
985 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100986
Uwe Kleine-König45993652011-01-19 20:47:04 +0100987 /* Set MII speed */
988 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
989
Guenter Roeckd1391932013-06-18 10:04:59 -0700990#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000991 /* set RX checksum */
992 val = readl(fep->hwp + FEC_RACC);
993 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
994 val |= FEC_RACC_OPTIONS;
995 else
996 val &= ~FEC_RACC_OPTIONS;
997 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700998#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000999
Uwe Kleine-König45993652011-01-19 20:47:04 +01001000 /*
1001 * The phy interface and speed need to get configured
1002 * differently on enet-mac.
1003 */
1004 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001005 /* Enable flow control and length check */
1006 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001007
Shawn Guo230dec62011-09-23 02:12:48 +00001008 /* RGMII, RMII or MII */
1009 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1010 rcntl |= (1 << 6);
1011 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001012 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001013 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001014 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001015
Shawn Guo230dec62011-09-23 02:12:48 +00001016 /* 1G, 100M or 10M */
1017 if (fep->phy_dev) {
1018 if (fep->phy_dev->speed == SPEED_1000)
1019 ecntl |= (1 << 5);
1020 else if (fep->phy_dev->speed == SPEED_100)
1021 rcntl &= ~(1 << 9);
1022 else
1023 rcntl |= (1 << 9);
1024 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001025 } else {
1026#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001027 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001028 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001029 /* disable the gasket and wait */
1030 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1031 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1032 udelay(1);
1033
1034 /*
1035 * configure the gasket:
1036 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001037 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001038 */
Eric Benard8d82f212012-01-12 06:10:28 +00001039 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1040 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1041 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1042 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1043 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001044
1045 /* re-enable the gasket */
1046 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1047 }
1048#endif
1049 }
Frank Libaa70a52013-01-16 16:55:58 +00001050
Guenter Roeckd1391932013-06-18 10:04:59 -07001051#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001052 /* enable pause frame*/
1053 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1054 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1055 fep->phy_dev && fep->phy_dev->pause)) {
1056 rcntl |= FEC_ENET_FCE;
1057
Jim Baxter4c09eed2013-04-19 08:10:49 +00001058 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001059 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1060 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1061 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1062 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1063
1064 /* OPD */
1065 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1066 } else {
1067 rcntl &= ~FEC_ENET_FCE;
1068 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001069#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001070
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001071 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001072
Stefan Wahren84fe6182014-03-12 11:28:19 +01001073 /* Setup multicast filter. */
1074 set_multicast_list(ndev);
1075#ifndef CONFIG_M5272
1076 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1077 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1078#endif
1079
Shawn Guo230dec62011-09-23 02:12:48 +00001080 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1081 /* enable ENET endian swap */
1082 ecntl |= (1 << 8);
1083 /* enable ENET store and forward mode */
1084 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1085 }
1086
Frank Liff43da82013-01-03 16:04:23 +00001087 if (fep->bufdesc_ex)
1088 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001089
Chris Healy38ae92d2013-06-25 23:18:52 -07001090#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001091 /* Enable the MIB statistic event counters */
1092 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001093#endif
1094
Uwe Kleine-König45993652011-01-19 20:47:04 +01001095 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001096 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001097 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001098
Frank Liff43da82013-01-03 16:04:23 +00001099 if (fep->bufdesc_ex)
1100 fec_ptp_start_cyclecounter(ndev);
1101
Uwe Kleine-König45993652011-01-19 20:47:04 +01001102 /* Enable interrupts we wish to service */
1103 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1104}
1105
1106static void
1107fec_stop(struct net_device *ndev)
1108{
1109 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001110 const struct platform_device_id *id_entry =
1111 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001112 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001113
1114 /* We cannot expect a graceful transmit stop without link !!! */
1115 if (fep->link) {
1116 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1117 udelay(10);
1118 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001119 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001120 }
1121
Fugang Duan106c3142014-09-13 05:00:51 +08001122 /* Whack a reset. We should wait for this.
1123 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1124 * instead of reset MAC itself.
1125 */
1126 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1127 writel(0, fep->hwp + FEC_ECNTRL);
1128 } else {
1129 writel(1, fep->hwp + FEC_ECNTRL);
1130 udelay(10);
1131 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001132 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1133 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001134
1135 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001136 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001137 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001138 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1139 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001140}
1141
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001144fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001146 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Russell King344756f2014-07-08 13:01:59 +01001148 fec_dump(ndev);
1149
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001150 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Russell King36cdc742014-07-08 13:01:44 +01001152 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001153}
1154
Russell King36cdc742014-07-08 13:01:44 +01001155static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001156{
1157 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001158 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001159 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001160
Russell King36cdc742014-07-08 13:01:44 +01001161 rtnl_lock();
1162 if (netif_device_present(ndev) || netif_running(ndev)) {
1163 napi_disable(&fep->napi);
1164 netif_tx_lock_bh(ndev);
1165 fec_restart(ndev);
1166 netif_wake_queue(ndev);
1167 netif_tx_unlock_bh(ndev);
1168 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001169 }
Russell King36cdc742014-07-08 13:01:44 +01001170 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001174fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1175 struct skb_shared_hwtstamps *hwtstamps)
1176{
1177 unsigned long flags;
1178 u64 ns;
1179
1180 spin_lock_irqsave(&fep->tmreg_lock, flags);
1181 ns = timecounter_cyc2time(&fep->tc, ts);
1182 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1183
1184 memset(hwtstamps, 0, sizeof(*hwtstamps));
1185 hwtstamps->hwtstamp = ns_to_ktime(ns);
1186}
1187
1188static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001189fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001192 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001193 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001195 struct fec_enet_priv_tx_q *txq;
1196 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001197 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001198 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001200 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001201
1202 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1203
1204 txq = fep->tx_queue[queue_id];
1205 /* get next bdp of dirty_tx */
1206 nq = netdev_get_tx_queue(ndev, queue_id);
1207 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Frank Lide5fb0a2013-03-03 17:34:25 +00001209 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001210 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001211
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001212 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001213
1214 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001215 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001216 break;
1217
Fugang Duan4d494cd2014-09-13 05:00:48 +08001218 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001219
Fugang Duan4d494cd2014-09-13 05:00:48 +08001220 skb = txq->tx_skbuff[index];
1221 txq->tx_skbuff[index] = NULL;
1222 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001223 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1224 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001225 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001226 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001227 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001228 continue;
1229 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001232 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 BD_ENET_TX_RL | BD_ENET_TX_UN |
1234 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001235 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001236 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001237 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001238 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001239 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001240 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001242 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001243 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001244 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001245 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001248 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
Frank Liff43da82013-01-03 16:04:23 +00001251 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1252 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001253 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001254 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001255
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001256 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001257 skb_tstamp_tx(skb, &shhwtstamps);
1258 }
Frank Liff43da82013-01-03 16:04:23 +00001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* Deferred means some collisions occurred during transmit,
1261 * but we eventually sent the packet OK.
1262 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001263 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001264 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001265
Sascha Hauer22f6b862009-04-15 01:32:18 +00001266 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001268
Fugang Duan4d494cd2014-09-13 05:00:48 +08001269 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001270
Sascha Hauer22f6b862009-04-15 01:32:18 +00001271 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001272 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001273
Sascha Hauer22f6b862009-04-15 01:32:18 +00001274 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001276 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001277 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1278 if (entries_free >= txq->tx_wake_threshold)
1279 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Russell Kingccea2962014-07-08 13:01:38 +01001282
1283 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001284 if (bdp != txq->cur_tx &&
1285 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1286 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1287}
1288
1289static void
1290fec_enet_tx(struct net_device *ndev)
1291{
1292 struct fec_enet_private *fep = netdev_priv(ndev);
1293 u16 queue_id;
1294 /* First process class A queue, then Class B and Best Effort queue */
1295 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1296 clear_bit(queue_id, &fep->work_tx);
1297 fec_enet_tx_queue(ndev, queue_id);
1298 }
1299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/* During a receive, the cur_rx points to the current incoming buffer.
1303 * When we update through the ring, if the next incoming buffer has
1304 * not been given to the system, we just set the empty indicator,
1305 * effectively tossing the packet.
1306 */
Frank Lidc975382013-01-28 18:31:42 +00001307static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001308fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001310 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001311 const struct platform_device_id *id_entry =
1312 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001313 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001314 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001315 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct sk_buff *skb;
1317 ushort pkt_len;
1318 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001319 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001320 struct bufdesc_ex *ebdp = NULL;
1321 bool vlan_packet_rcvd = false;
1322 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001323 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001324
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001325#ifdef CONFIG_M532x
1326 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001327#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001328 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1329 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* First, grab all of the stats for the incoming packet.
1332 * These get messed up if we get called due to a busy condition.
1333 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001334 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Sascha Hauer22f6b862009-04-15 01:32:18 +00001336 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Frank Lidc975382013-01-28 18:31:42 +00001338 if (pkt_received >= budget)
1339 break;
1340 pkt_received++;
1341
Sascha Hauer22f6b862009-04-15 01:32:18 +00001342 /* Since we have allocated space to hold a complete frame,
1343 * the last indicator should be set.
1344 */
1345 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001346 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Russell Kingdb3421c2014-07-08 13:01:49 +01001348
Sascha Hauer22f6b862009-04-15 01:32:18 +00001349 /* Check for errors. */
1350 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001352 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001353 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1354 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001355 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001356 }
1357 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001358 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001359 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001360 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001361 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001362 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001364
1365 /* Report late collisions as a frame error.
1366 * On this error, the BD is closed, but we don't know what we
1367 * have in the buffer. So, just drop this frame on the floor.
1368 */
1369 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001370 ndev->stats.rx_errors++;
1371 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001372 goto rx_processing_done;
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Sascha Hauer22f6b862009-04-15 01:32:18 +00001375 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001376 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001377 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001378 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Fugang Duan4d494cd2014-09-13 05:00:48 +08001380 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1381 data = rxq->rx_skbuff[index]->data;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001382 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1383 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001384
Shawn Guob5680e02011-01-05 21:13:13 +00001385 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1386 swap_buffer(data, pkt_len);
1387
Jim Baxtercdffcf12013-07-02 22:52:56 +01001388 /* Extract the enhanced buffer descriptor */
1389 ebdp = NULL;
1390 if (fep->bufdesc_ex)
1391 ebdp = (struct bufdesc_ex *)bdp;
1392
1393 /* If this is a VLAN packet remove the VLAN Tag */
1394 vlan_packet_rcvd = false;
1395 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001396 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001397 /* Push and remove the vlan tag */
1398 struct vlan_hdr *vlan_header =
1399 (struct vlan_hdr *) (data + ETH_HLEN);
1400 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1401 pkt_len -= VLAN_HLEN;
1402
1403 vlan_packet_rcvd = true;
1404 }
1405
Sascha Hauer22f6b862009-04-15 01:32:18 +00001406 /* This does 16 byte alignment, exactly what we need.
1407 * The packet length includes FCS, but we don't want to
1408 * include that when passing upstream as it messes up
1409 * bridging applications.
1410 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001411 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Sascha Hauer85498892009-04-15 01:32:21 +00001413 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001414 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001415 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001416 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001417 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001418 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001419
1420 /* Extract the frame data without the VLAN header. */
1421 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1422 if (vlan_packet_rcvd)
1423 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001424 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
Jim Baxtercdffcf12013-07-02 22:52:56 +01001425 data + payload_offset,
1426 pkt_len - 4 - (2 * ETH_ALEN));
1427
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001428 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001429
Frank Li6605b732012-10-30 18:25:31 +00001430 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001431 if (fep->hwts_rx_en && fep->bufdesc_ex)
1432 fec_enet_hwtstamp(fep, ebdp->ts,
1433 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001434
Jim Baxter4c09eed2013-04-19 08:10:49 +00001435 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001436 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001437 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1438 /* don't check it */
1439 skb->ip_summed = CHECKSUM_UNNECESSARY;
1440 } else {
1441 skb_checksum_none_assert(skb);
1442 }
1443 }
1444
Jim Baxtercdffcf12013-07-02 22:52:56 +01001445 /* Handle received VLAN packets */
1446 if (vlan_packet_rcvd)
1447 __vlan_hwaccel_put_tag(skb,
1448 htons(ETH_P_8021Q),
1449 vlan_tag);
1450
Richard Cochran0affdf32013-08-30 20:28:10 +02001451 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001452 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001453
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001454 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1455 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001456rx_processing_done:
1457 /* Clear the status flags for this buffer */
1458 status &= ~BD_ENET_RX_STATS;
1459
1460 /* Mark the buffer empty */
1461 status |= BD_ENET_RX_EMPTY;
1462 bdp->cbd_sc = status;
1463
Frank Liff43da82013-01-03 16:04:23 +00001464 if (fep->bufdesc_ex) {
1465 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1466
1467 ebdp->cbd_esc = BD_ENET_RX_INT;
1468 ebdp->cbd_prot = 0;
1469 ebdp->cbd_bdu = 0;
1470 }
Frank Li6605b732012-10-30 18:25:31 +00001471
Sascha Hauer22f6b862009-04-15 01:32:18 +00001472 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001473 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001474
Sascha Hauer22f6b862009-04-15 01:32:18 +00001475 /* Doing this here will keep the FEC running while we process
1476 * incoming frames. On a heavily loaded network, we should be
1477 * able to keep up at the expense of system resources.
1478 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001479 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001481 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001482 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Fugang Duan4d494cd2014-09-13 05:00:48 +08001485static int
1486fec_enet_rx(struct net_device *ndev, int budget)
1487{
1488 int pkt_received = 0;
1489 u16 queue_id;
1490 struct fec_enet_private *fep = netdev_priv(ndev);
1491
1492 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1493 clear_bit(queue_id, &fep->work_rx);
1494 pkt_received += fec_enet_rx_queue(ndev,
1495 budget - pkt_received, queue_id);
1496 }
1497 return pkt_received;
1498}
1499
1500static bool
1501fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1502{
1503 if (int_events == 0)
1504 return false;
1505
1506 if (int_events & FEC_ENET_RXF)
1507 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001508 if (int_events & FEC_ENET_RXF_1)
1509 fep->work_rx |= (1 << 0);
1510 if (int_events & FEC_ENET_RXF_2)
1511 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001512
1513 if (int_events & FEC_ENET_TXF)
1514 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001515 if (int_events & FEC_ENET_TXF_1)
1516 fep->work_tx |= (1 << 0);
1517 if (int_events & FEC_ENET_TXF_2)
1518 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001519
1520 return true;
1521}
1522
Uwe Kleine-König45993652011-01-19 20:47:04 +01001523static irqreturn_t
1524fec_enet_interrupt(int irq, void *dev_id)
1525{
1526 struct net_device *ndev = dev_id;
1527 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001528 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001529 uint int_events;
1530 irqreturn_t ret = IRQ_NONE;
1531
Russell King7a168072014-07-08 00:22:44 +01001532 int_events = readl(fep->hwp + FEC_IEVENT);
1533 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001534 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001535
Russell King7a168072014-07-08 00:22:44 +01001536 if (int_events & napi_mask) {
1537 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001538
Russell King7a168072014-07-08 00:22:44 +01001539 /* Disable the NAPI interrupts */
1540 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1541 napi_schedule(&fep->napi);
1542 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001543
Russell King7a168072014-07-08 00:22:44 +01001544 if (int_events & FEC_ENET_MII) {
1545 ret = IRQ_HANDLED;
1546 complete(&fep->mdio_done);
1547 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001548
1549 return ret;
1550}
1551
Frank Lidc975382013-01-28 18:31:42 +00001552static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1553{
1554 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001555 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001556 int pkts;
1557
1558 /*
1559 * Clear any pending transmit or receive interrupts before
1560 * processing the rings to avoid racing with the hardware.
1561 */
1562 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1563
1564 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001565
Frank Lide5fb0a2013-03-03 17:34:25 +00001566 fec_enet_tx(ndev);
1567
Frank Lidc975382013-01-28 18:31:42 +00001568 if (pkts < budget) {
1569 napi_complete(napi);
1570 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1571 }
1572 return pkts;
1573}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001576static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001578 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001579 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001580 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Shawn Guo49da97d2011-01-05 21:13:11 +00001582 /*
1583 * try to get mac address in following order:
1584 *
1585 * 1) module parameter via kernel command line in form
1586 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1587 */
1588 iap = macaddr;
1589
1590 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001591 * 2) from device tree data
1592 */
1593 if (!is_valid_ether_addr(iap)) {
1594 struct device_node *np = fep->pdev->dev.of_node;
1595 if (np) {
1596 const char *mac = of_get_mac_address(np);
1597 if (mac)
1598 iap = (unsigned char *) mac;
1599 }
1600 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001601
1602 /*
1603 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001604 */
1605 if (!is_valid_ether_addr(iap)) {
1606#ifdef CONFIG_M5272
1607 if (FEC_FLASHMAC)
1608 iap = (unsigned char *)FEC_FLASHMAC;
1609#else
1610 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001611 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001612#endif
1613 }
1614
1615 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001616 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001617 */
1618 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001619 *((__be32 *) &tmpaddr[0]) =
1620 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1621 *((__be16 *) &tmpaddr[4]) =
1622 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 iap = &tmpaddr[0];
1624 }
1625
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001626 /*
1627 * 5) random mac address
1628 */
1629 if (!is_valid_ether_addr(iap)) {
1630 /* Report it and use a random ethernet address instead */
1631 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1632 eth_hw_addr_random(ndev);
1633 netdev_info(ndev, "Using random MAC address: %pM\n",
1634 ndev->dev_addr);
1635 return;
1636 }
1637
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001638 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Shawn Guo49da97d2011-01-05 21:13:11 +00001640 /* Adjust MAC if using macaddr */
1641 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001642 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645/* ------------------------------------------------------------------------- */
1646
Bryan Wue6b043d2010-03-31 02:10:44 +00001647/*
1648 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001650static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001652 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001653 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001654 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Bryan Wue6b043d2010-03-31 02:10:44 +00001656 /* Prevent a state halted on mii error */
1657 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1658 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001659 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001661
Russell King8ce56242014-07-08 12:40:07 +01001662 /*
1663 * If the netdev is down, or is going down, we're not interested
1664 * in link state events, so just mark our idea of the link as down
1665 * and ignore the event.
1666 */
1667 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1668 fep->link = 0;
1669 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001670 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001671 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001672 status_change = 1;
1673 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001674
Russell Kingef833372014-07-08 12:40:43 +01001675 if (fep->full_duplex != phy_dev->duplex) {
1676 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001677 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001678 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001679
1680 if (phy_dev->speed != fep->speed) {
1681 fep->speed = phy_dev->speed;
1682 status_change = 1;
1683 }
1684
1685 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001686 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001687 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001688 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001689 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001690 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001691 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001692 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001693 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001694 } else {
1695 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001696 napi_disable(&fep->napi);
1697 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001698 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001699 netif_tx_unlock_bh(ndev);
1700 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001701 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001702 status_change = 1;
1703 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001704 }
1705
Bryan Wue6b043d2010-03-31 02:10:44 +00001706 if (status_change)
1707 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Bryan Wue6b043d2010-03-31 02:10:44 +00001710static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Bryan Wue6b043d2010-03-31 02:10:44 +00001712 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001713 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001714
1715 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001716 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001717
1718 /* start a read op */
1719 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1720 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1721 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1722
1723 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001724 time_left = wait_for_completion_timeout(&fep->mdio_done,
1725 usecs_to_jiffies(FEC_MII_TIMEOUT));
1726 if (time_left == 0) {
1727 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001728 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001729 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001730 }
1731
1732 /* return value */
1733 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1734}
1735
1736static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1737 u16 value)
1738{
1739 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001740 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001741
1742 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001743 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001744
Shawn Guo862f0982011-01-05 21:13:09 +00001745 /* start a write op */
1746 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001747 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1748 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1749 fep->hwp + FEC_MII_DATA);
1750
1751 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001752 time_left = wait_for_completion_timeout(&fep->mdio_done,
1753 usecs_to_jiffies(FEC_MII_TIMEOUT));
1754 if (time_left == 0) {
1755 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001756 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001757 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001758 }
1759
1760 return 0;
1761}
1762
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001763static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1764{
1765 struct fec_enet_private *fep = netdev_priv(ndev);
1766 int ret;
1767
1768 if (enable) {
1769 ret = clk_prepare_enable(fep->clk_ahb);
1770 if (ret)
1771 return ret;
1772 ret = clk_prepare_enable(fep->clk_ipg);
1773 if (ret)
1774 goto failed_clk_ipg;
1775 if (fep->clk_enet_out) {
1776 ret = clk_prepare_enable(fep->clk_enet_out);
1777 if (ret)
1778 goto failed_clk_enet_out;
1779 }
1780 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001781 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001782 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001783 if (ret) {
1784 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001785 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001786 } else {
1787 fep->ptp_clk_on = true;
1788 }
1789 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001790 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001791 if (fep->clk_ref) {
1792 ret = clk_prepare_enable(fep->clk_ref);
1793 if (ret)
1794 goto failed_clk_ref;
1795 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001796 } else {
1797 clk_disable_unprepare(fep->clk_ahb);
1798 clk_disable_unprepare(fep->clk_ipg);
1799 if (fep->clk_enet_out)
1800 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001801 if (fep->clk_ptp) {
1802 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001803 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001804 fep->ptp_clk_on = false;
1805 mutex_unlock(&fep->ptp_clk_mutex);
1806 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001807 if (fep->clk_ref)
1808 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001809 }
1810
1811 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001812
1813failed_clk_ref:
1814 if (fep->clk_ref)
1815 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001816failed_clk_ptp:
1817 if (fep->clk_enet_out)
1818 clk_disable_unprepare(fep->clk_enet_out);
1819failed_clk_enet_out:
1820 clk_disable_unprepare(fep->clk_ipg);
1821failed_clk_ipg:
1822 clk_disable_unprepare(fep->clk_ahb);
1823
1824 return ret;
1825}
1826
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001827static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001828{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001829 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001830 const struct platform_device_id *id_entry =
1831 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001832 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001833 char mdio_bus_id[MII_BUS_ID_SIZE];
1834 char phy_name[MII_BUS_ID_SIZE + 3];
1835 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001836 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001837
Bryan Wu418bd0d2010-05-28 03:40:39 -07001838 fep->phy_dev = NULL;
1839
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001840 if (fep->phy_node) {
1841 phy_dev = of_phy_connect(ndev, fep->phy_node,
1842 &fec_enet_adjust_link, 0,
1843 fep->phy_interface);
1844 } else {
1845 /* check for attached phy */
1846 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1847 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1848 continue;
1849 if (fep->mii_bus->phy_map[phy_id] == NULL)
1850 continue;
1851 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1852 continue;
1853 if (dev_id--)
1854 continue;
1855 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1856 break;
1857 }
1858
1859 if (phy_id >= PHY_MAX_ADDR) {
1860 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1861 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1862 phy_id = 0;
1863 }
1864
1865 snprintf(phy_name, sizeof(phy_name),
1866 PHY_ID_FMT, mdio_bus_id, phy_id);
1867 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1868 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001869 }
1870
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001871 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001872 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001873 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001874 }
1875
1876 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001877 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001878 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001879 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001880#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001881 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001882#endif
Frank Libaa70a52013-01-16 16:55:58 +00001883 }
Shawn Guo230dec62011-09-23 02:12:48 +00001884 else
1885 phy_dev->supported &= PHY_BASIC_FEATURES;
1886
Bryan Wue6b043d2010-03-31 02:10:44 +00001887 phy_dev->advertising = phy_dev->supported;
1888
1889 fep->phy_dev = phy_dev;
1890 fep->link = 0;
1891 fep->full_duplex = 0;
1892
Joe Perches31b77202013-04-13 19:03:17 +00001893 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1894 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1895 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001896
Bryan Wue6b043d2010-03-31 02:10:44 +00001897 return 0;
1898}
1899
1900static int fec_enet_mii_init(struct platform_device *pdev)
1901{
Shawn Guob5680e02011-01-05 21:13:13 +00001902 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001903 struct net_device *ndev = platform_get_drvdata(pdev);
1904 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001905 const struct platform_device_id *id_entry =
1906 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001907 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001908 int err = -ENXIO, i;
1909
Shawn Guob5680e02011-01-05 21:13:13 +00001910 /*
1911 * The dual fec interfaces are not equivalent with enet-mac.
1912 * Here are the differences:
1913 *
1914 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1915 * - fec0 acts as the 1588 time master while fec1 is slave
1916 * - external phys can only be configured by fec0
1917 *
1918 * That is to say fec1 can not work independently. It only works
1919 * when fec0 is working. The reason behind this design is that the
1920 * second interface is added primarily for Switch mode.
1921 *
1922 * Because of the last point above, both phys are attached on fec0
1923 * mdio interface in board design, and need to be configured by
1924 * fec0 mii_bus.
1925 */
Shawn Guo43af9402011-12-05 05:01:15 +00001926 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001927 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001928 if (mii_cnt && fec0_mii_bus) {
1929 fep->mii_bus = fec0_mii_bus;
1930 mii_cnt++;
1931 return 0;
1932 }
1933 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001934 }
1935
Bryan Wue6b043d2010-03-31 02:10:44 +00001936 fep->mii_timeout = 0;
1937
1938 /*
1939 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001940 *
1941 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1942 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1943 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1944 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001945 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001946 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001947 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1948 fep->phy_speed--;
1949 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001950 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1951
1952 fep->mii_bus = mdiobus_alloc();
1953 if (fep->mii_bus == NULL) {
1954 err = -ENOMEM;
1955 goto err_out;
1956 }
1957
1958 fep->mii_bus->name = "fec_enet_mii_bus";
1959 fep->mii_bus->read = fec_enet_mdio_read;
1960 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001961 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1962 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001963 fep->mii_bus->priv = fep;
1964 fep->mii_bus->parent = &pdev->dev;
1965
1966 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1967 if (!fep->mii_bus->irq) {
1968 err = -ENOMEM;
1969 goto err_out_free_mdiobus;
1970 }
1971
1972 for (i = 0; i < PHY_MAX_ADDR; i++)
1973 fep->mii_bus->irq[i] = PHY_POLL;
1974
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001975 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
1976 if (node) {
1977 err = of_mdiobus_register(fep->mii_bus, node);
1978 of_node_put(node);
1979 } else {
1980 err = mdiobus_register(fep->mii_bus);
1981 }
1982
1983 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00001984 goto err_out_free_mdio_irq;
1985
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001986 mii_cnt++;
1987
Shawn Guob5680e02011-01-05 21:13:13 +00001988 /* save fec0 mii_bus */
1989 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1990 fec0_mii_bus = fep->mii_bus;
1991
Bryan Wue6b043d2010-03-31 02:10:44 +00001992 return 0;
1993
Bryan Wue6b043d2010-03-31 02:10:44 +00001994err_out_free_mdio_irq:
1995 kfree(fep->mii_bus->irq);
1996err_out_free_mdiobus:
1997 mdiobus_free(fep->mii_bus);
1998err_out:
1999 return err;
2000}
2001
2002static void fec_enet_mii_remove(struct fec_enet_private *fep)
2003{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002004 if (--mii_cnt == 0) {
2005 mdiobus_unregister(fep->mii_bus);
2006 kfree(fep->mii_bus->irq);
2007 mdiobus_free(fep->mii_bus);
2008 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002009}
2010
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002011static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002012 struct ethtool_cmd *cmd)
2013{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002014 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002015 struct phy_device *phydev = fep->phy_dev;
2016
2017 if (!phydev)
2018 return -ENODEV;
2019
2020 return phy_ethtool_gset(phydev, cmd);
2021}
2022
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002023static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002024 struct ethtool_cmd *cmd)
2025{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002026 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002027 struct phy_device *phydev = fep->phy_dev;
2028
2029 if (!phydev)
2030 return -ENODEV;
2031
2032 return phy_ethtool_sset(phydev, cmd);
2033}
2034
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002035static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002036 struct ethtool_drvinfo *info)
2037{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002038 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Jiri Pirko7826d432013-01-06 00:44:26 +00002040 strlcpy(info->driver, fep->pdev->dev.driver->name,
2041 sizeof(info->driver));
2042 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2043 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
Bryan Wue6b043d2010-03-31 02:10:44 +00002045
Frank Li5ebae4892013-01-06 16:25:07 +00002046static int fec_enet_get_ts_info(struct net_device *ndev,
2047 struct ethtool_ts_info *info)
2048{
2049 struct fec_enet_private *fep = netdev_priv(ndev);
2050
2051 if (fep->bufdesc_ex) {
2052
2053 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2054 SOF_TIMESTAMPING_RX_SOFTWARE |
2055 SOF_TIMESTAMPING_SOFTWARE |
2056 SOF_TIMESTAMPING_TX_HARDWARE |
2057 SOF_TIMESTAMPING_RX_HARDWARE |
2058 SOF_TIMESTAMPING_RAW_HARDWARE;
2059 if (fep->ptp_clock)
2060 info->phc_index = ptp_clock_index(fep->ptp_clock);
2061 else
2062 info->phc_index = -1;
2063
2064 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2065 (1 << HWTSTAMP_TX_ON);
2066
2067 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2068 (1 << HWTSTAMP_FILTER_ALL);
2069 return 0;
2070 } else {
2071 return ethtool_op_get_ts_info(ndev, info);
2072 }
2073}
2074
Guenter Roeckd1391932013-06-18 10:04:59 -07002075#if !defined(CONFIG_M5272)
2076
Frank Libaa70a52013-01-16 16:55:58 +00002077static void fec_enet_get_pauseparam(struct net_device *ndev,
2078 struct ethtool_pauseparam *pause)
2079{
2080 struct fec_enet_private *fep = netdev_priv(ndev);
2081
2082 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2083 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2084 pause->rx_pause = pause->tx_pause;
2085}
2086
2087static int fec_enet_set_pauseparam(struct net_device *ndev,
2088 struct ethtool_pauseparam *pause)
2089{
2090 struct fec_enet_private *fep = netdev_priv(ndev);
2091
Russell King0b146ca2014-07-08 00:22:59 +01002092 if (!fep->phy_dev)
2093 return -ENODEV;
2094
Frank Libaa70a52013-01-16 16:55:58 +00002095 if (pause->tx_pause != pause->rx_pause) {
2096 netdev_info(ndev,
2097 "hardware only support enable/disable both tx and rx");
2098 return -EINVAL;
2099 }
2100
2101 fep->pause_flag = 0;
2102
2103 /* tx pause must be same as rx pause */
2104 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2105 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2106
2107 if (pause->rx_pause || pause->autoneg) {
2108 fep->phy_dev->supported |= ADVERTISED_Pause;
2109 fep->phy_dev->advertising |= ADVERTISED_Pause;
2110 } else {
2111 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2112 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2113 }
2114
2115 if (pause->autoneg) {
2116 if (netif_running(ndev))
2117 fec_stop(ndev);
2118 phy_start_aneg(fep->phy_dev);
2119 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002120 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002121 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002122 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002123 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002124 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002125 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002126 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002127 }
Frank Libaa70a52013-01-16 16:55:58 +00002128
2129 return 0;
2130}
2131
Chris Healy38ae92d2013-06-25 23:18:52 -07002132static const struct fec_stat {
2133 char name[ETH_GSTRING_LEN];
2134 u16 offset;
2135} fec_stats[] = {
2136 /* RMON TX */
2137 { "tx_dropped", RMON_T_DROP },
2138 { "tx_packets", RMON_T_PACKETS },
2139 { "tx_broadcast", RMON_T_BC_PKT },
2140 { "tx_multicast", RMON_T_MC_PKT },
2141 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2142 { "tx_undersize", RMON_T_UNDERSIZE },
2143 { "tx_oversize", RMON_T_OVERSIZE },
2144 { "tx_fragment", RMON_T_FRAG },
2145 { "tx_jabber", RMON_T_JAB },
2146 { "tx_collision", RMON_T_COL },
2147 { "tx_64byte", RMON_T_P64 },
2148 { "tx_65to127byte", RMON_T_P65TO127 },
2149 { "tx_128to255byte", RMON_T_P128TO255 },
2150 { "tx_256to511byte", RMON_T_P256TO511 },
2151 { "tx_512to1023byte", RMON_T_P512TO1023 },
2152 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2153 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2154 { "tx_octets", RMON_T_OCTETS },
2155
2156 /* IEEE TX */
2157 { "IEEE_tx_drop", IEEE_T_DROP },
2158 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2159 { "IEEE_tx_1col", IEEE_T_1COL },
2160 { "IEEE_tx_mcol", IEEE_T_MCOL },
2161 { "IEEE_tx_def", IEEE_T_DEF },
2162 { "IEEE_tx_lcol", IEEE_T_LCOL },
2163 { "IEEE_tx_excol", IEEE_T_EXCOL },
2164 { "IEEE_tx_macerr", IEEE_T_MACERR },
2165 { "IEEE_tx_cserr", IEEE_T_CSERR },
2166 { "IEEE_tx_sqe", IEEE_T_SQE },
2167 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2168 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2169
2170 /* RMON RX */
2171 { "rx_packets", RMON_R_PACKETS },
2172 { "rx_broadcast", RMON_R_BC_PKT },
2173 { "rx_multicast", RMON_R_MC_PKT },
2174 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2175 { "rx_undersize", RMON_R_UNDERSIZE },
2176 { "rx_oversize", RMON_R_OVERSIZE },
2177 { "rx_fragment", RMON_R_FRAG },
2178 { "rx_jabber", RMON_R_JAB },
2179 { "rx_64byte", RMON_R_P64 },
2180 { "rx_65to127byte", RMON_R_P65TO127 },
2181 { "rx_128to255byte", RMON_R_P128TO255 },
2182 { "rx_256to511byte", RMON_R_P256TO511 },
2183 { "rx_512to1023byte", RMON_R_P512TO1023 },
2184 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2185 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2186 { "rx_octets", RMON_R_OCTETS },
2187
2188 /* IEEE RX */
2189 { "IEEE_rx_drop", IEEE_R_DROP },
2190 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2191 { "IEEE_rx_crc", IEEE_R_CRC },
2192 { "IEEE_rx_align", IEEE_R_ALIGN },
2193 { "IEEE_rx_macerr", IEEE_R_MACERR },
2194 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2195 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2196};
2197
2198static void fec_enet_get_ethtool_stats(struct net_device *dev,
2199 struct ethtool_stats *stats, u64 *data)
2200{
2201 struct fec_enet_private *fep = netdev_priv(dev);
2202 int i;
2203
2204 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2205 data[i] = readl(fep->hwp + fec_stats[i].offset);
2206}
2207
2208static void fec_enet_get_strings(struct net_device *netdev,
2209 u32 stringset, u8 *data)
2210{
2211 int i;
2212 switch (stringset) {
2213 case ETH_SS_STATS:
2214 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2215 memcpy(data + i * ETH_GSTRING_LEN,
2216 fec_stats[i].name, ETH_GSTRING_LEN);
2217 break;
2218 }
2219}
2220
2221static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2222{
2223 switch (sset) {
2224 case ETH_SS_STATS:
2225 return ARRAY_SIZE(fec_stats);
2226 default:
2227 return -EOPNOTSUPP;
2228 }
2229}
Guenter Roeckd1391932013-06-18 10:04:59 -07002230#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002231
Chris Healy32bc9b42013-06-17 07:25:06 -07002232static int fec_enet_nway_reset(struct net_device *dev)
2233{
2234 struct fec_enet_private *fep = netdev_priv(dev);
2235 struct phy_device *phydev = fep->phy_dev;
2236
2237 if (!phydev)
2238 return -ENODEV;
2239
2240 return genphy_restart_aneg(phydev);
2241}
2242
stephen hemminger9b07be42012-01-04 12:59:49 +00002243static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002244 .get_settings = fec_enet_get_settings,
2245 .set_settings = fec_enet_set_settings,
2246 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002247 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002248 .get_link = ethtool_op_get_link,
Chris Healy38ae92d2013-06-25 23:18:52 -07002249#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002250 .get_pauseparam = fec_enet_get_pauseparam,
2251 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002252 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002253 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002254 .get_sset_count = fec_enet_get_sset_count,
2255#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002256 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002257};
2258
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002259static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002260{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002261 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002262 struct phy_device *phydev = fep->phy_dev;
2263
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002264 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002265 return -EINVAL;
2266
2267 if (!phydev)
2268 return -ENODEV;
2269
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002270 if (fep->bufdesc_ex) {
2271 if (cmd == SIOCSHWTSTAMP)
2272 return fec_ptp_set(ndev, rq);
2273 if (cmd == SIOCGHWTSTAMP)
2274 return fec_ptp_get(ndev, rq);
2275 }
Frank Liff43da82013-01-03 16:04:23 +00002276
Richard Cochran28b04112010-07-17 08:48:55 +00002277 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002280static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002281{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002282 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002283 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002284 struct sk_buff *skb;
2285 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002286 struct fec_enet_priv_tx_q *txq;
2287 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002288 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002289
Frank Li59d0f7462014-09-13 05:00:50 +08002290 for (q = 0; q < fep->num_rx_queues; q++) {
2291 rxq = fep->rx_queue[q];
2292 bdp = rxq->rx_bd_base;
2293 for (i = 0; i < rxq->rx_ring_size; i++) {
2294 skb = rxq->rx_skbuff[i];
2295 rxq->rx_skbuff[i] = NULL;
2296 if (skb) {
2297 dma_unmap_single(&fep->pdev->dev,
2298 bdp->cbd_bufaddr,
2299 FEC_ENET_RX_FRSIZE,
2300 DMA_FROM_DEVICE);
2301 dev_kfree_skb(skb);
2302 }
2303 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002304 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002305 }
2306
Frank Li59d0f7462014-09-13 05:00:50 +08002307 for (q = 0; q < fep->num_tx_queues; q++) {
2308 txq = fep->tx_queue[q];
2309 bdp = txq->tx_bd_base;
2310 for (i = 0; i < txq->tx_ring_size; i++) {
2311 kfree(txq->tx_bounce[i]);
2312 txq->tx_bounce[i] = NULL;
2313 skb = txq->tx_skbuff[i];
2314 txq->tx_skbuff[i] = NULL;
2315 dev_kfree_skb(skb);
2316 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002317 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002318}
2319
Frank Li59d0f7462014-09-13 05:00:50 +08002320static void fec_enet_free_queue(struct net_device *ndev)
2321{
2322 struct fec_enet_private *fep = netdev_priv(ndev);
2323 int i;
2324 struct fec_enet_priv_tx_q *txq;
2325
2326 for (i = 0; i < fep->num_tx_queues; i++)
2327 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2328 txq = fep->tx_queue[i];
2329 dma_free_coherent(NULL,
2330 txq->tx_ring_size * TSO_HEADER_SIZE,
2331 txq->tso_hdrs,
2332 txq->tso_hdrs_dma);
2333 }
2334
2335 for (i = 0; i < fep->num_rx_queues; i++)
2336 if (fep->rx_queue[i])
2337 kfree(fep->rx_queue[i]);
2338
2339 for (i = 0; i < fep->num_tx_queues; i++)
2340 if (fep->tx_queue[i])
2341 kfree(fep->tx_queue[i]);
2342}
2343
2344static int fec_enet_alloc_queue(struct net_device *ndev)
2345{
2346 struct fec_enet_private *fep = netdev_priv(ndev);
2347 int i;
2348 int ret = 0;
2349 struct fec_enet_priv_tx_q *txq;
2350
2351 for (i = 0; i < fep->num_tx_queues; i++) {
2352 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2353 if (!txq) {
2354 ret = -ENOMEM;
2355 goto alloc_failed;
2356 }
2357
2358 fep->tx_queue[i] = txq;
2359 txq->tx_ring_size = TX_RING_SIZE;
2360 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2361
2362 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2363 txq->tx_wake_threshold =
2364 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2365
2366 txq->tso_hdrs = dma_alloc_coherent(NULL,
2367 txq->tx_ring_size * TSO_HEADER_SIZE,
2368 &txq->tso_hdrs_dma,
2369 GFP_KERNEL);
2370 if (!txq->tso_hdrs) {
2371 ret = -ENOMEM;
2372 goto alloc_failed;
2373 }
2374 }
2375
2376 for (i = 0; i < fep->num_rx_queues; i++) {
2377 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2378 GFP_KERNEL);
2379 if (!fep->rx_queue[i]) {
2380 ret = -ENOMEM;
2381 goto alloc_failed;
2382 }
2383
2384 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2385 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2386 }
2387 return ret;
2388
2389alloc_failed:
2390 fec_enet_free_queue(ndev);
2391 return ret;
2392}
2393
2394static int
2395fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002396{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002397 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002398 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002399 struct sk_buff *skb;
2400 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002401 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002402
Frank Li59d0f7462014-09-13 05:00:50 +08002403 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002404 bdp = rxq->rx_bd_base;
2405 for (i = 0; i < rxq->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002406 dma_addr_t addr;
2407
Fabio Estevamb72061a2012-02-07 08:27:31 +00002408 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002409 if (!skb)
2410 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002411
Russell King730ee362014-07-08 00:23:14 +01002412 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002413 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002414 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2415 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002416 if (net_ratelimit())
2417 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002418 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002419 }
Russell King730ee362014-07-08 00:23:14 +01002420
Fugang Duan4d494cd2014-09-13 05:00:48 +08002421 rxq->rx_skbuff[i] = skb;
Russell King730ee362014-07-08 00:23:14 +01002422 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002423 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002424
2425 if (fep->bufdesc_ex) {
2426 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2427 ebdp->cbd_esc = BD_ENET_RX_INT;
2428 }
2429
Frank Li59d0f7462014-09-13 05:00:50 +08002430 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002431 }
2432
2433 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002434 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002435 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002436 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002437
Frank Li59d0f7462014-09-13 05:00:50 +08002438 err_alloc:
2439 fec_enet_free_buffers(ndev);
2440 return -ENOMEM;
2441}
2442
2443static int
2444fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2445{
2446 struct fec_enet_private *fep = netdev_priv(ndev);
2447 unsigned int i;
2448 struct bufdesc *bdp;
2449 struct fec_enet_priv_tx_q *txq;
2450
2451 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002452 bdp = txq->tx_bd_base;
2453 for (i = 0; i < txq->tx_ring_size; i++) {
2454 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2455 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002456 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002457
2458 bdp->cbd_sc = 0;
2459 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002460
Frank Liff43da82013-01-03 16:04:23 +00002461 if (fep->bufdesc_ex) {
2462 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002463 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002464 }
2465
Frank Li59d0f7462014-09-13 05:00:50 +08002466 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002467 }
2468
2469 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002470 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002471 bdp->cbd_sc |= BD_SC_WRAP;
2472
2473 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002474
2475 err_alloc:
2476 fec_enet_free_buffers(ndev);
2477 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002478}
2479
Frank Li59d0f7462014-09-13 05:00:50 +08002480static int fec_enet_alloc_buffers(struct net_device *ndev)
2481{
2482 struct fec_enet_private *fep = netdev_priv(ndev);
2483 unsigned int i;
2484
2485 for (i = 0; i < fep->num_rx_queues; i++)
2486 if (fec_enet_alloc_rxq_buffers(ndev, i))
2487 return -ENOMEM;
2488
2489 for (i = 0; i < fep->num_tx_queues; i++)
2490 if (fec_enet_alloc_txq_buffers(ndev, i))
2491 return -ENOMEM;
2492 return 0;
2493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002496fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002498 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002499 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002501 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002502 ret = fec_enet_clk_enable(ndev, true);
2503 if (ret)
2504 return ret;
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 /* I should reset the ring buffers here, but I don't yet know
2507 * a simple way to do that.
2508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002510 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002511 if (ret)
2512 return ret;
2513
Bryan Wu418bd0d2010-05-28 03:40:39 -07002514 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002515 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002516 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002517 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002518 return ret;
2519 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002520
Russell Kingef833372014-07-08 12:40:43 +01002521 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002522 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002523 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002524 netif_tx_start_all_queues(ndev);
2525
Sascha Hauer22f6b862009-04-15 01:32:18 +00002526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002530fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002532 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Russell Kingd76cfae2014-07-08 00:23:04 +01002534 phy_stop(fep->phy_dev);
2535
Russell King31a6de32014-07-08 12:40:23 +01002536 if (netif_device_present(ndev)) {
2537 napi_disable(&fep->napi);
2538 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002539 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Russell King635cf172014-07-08 00:22:54 +01002542 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002543 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002544
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002545 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002546 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002547 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return 0;
2550}
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552/* Set or clear the multicast filter for this adaptor.
2553 * Skeleton taken from sunlance driver.
2554 * The CPM Ethernet implementation allows Multicast as well as individual
2555 * MAC address filtering. Some of the drivers check to make sure it is
2556 * a group multicast address, and discard those that are not. I guess I
2557 * will do the same for now, but just remove the test if you want
2558 * individual filtering as well (do the upper net layers want or support
2559 * this kind of feature?).
2560 */
2561
2562#define HASH_BITS 6 /* #bits in hash */
2563#define CRC32_POLY 0xEDB88320
2564
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002565static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002567 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002568 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002569 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned char hash;
2571
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002572 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002573 tmp = readl(fep->hwp + FEC_R_CNTRL);
2574 tmp |= 0x8;
2575 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002576 return;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Sascha Hauer4e831832009-04-15 01:32:19 +00002579 tmp = readl(fep->hwp + FEC_R_CNTRL);
2580 tmp &= ~0x8;
2581 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002582
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002583 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002584 /* Catch all multicast addresses, so set the
2585 * filter to all 1's
2586 */
2587 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2588 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Sascha Hauer4e831832009-04-15 01:32:19 +00002590 return;
2591 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002592
Sascha Hauer4e831832009-04-15 01:32:19 +00002593 /* Clear filter and add the addresses in hash register
2594 */
2595 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2596 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002598 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002599 /* calculate crc32 value of mac address */
2600 crc = 0xffffffff;
2601
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002602 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002603 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002604 for (bit = 0; bit < 8; bit++, data >>= 1) {
2605 crc = (crc >> 1) ^
2606 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002609
2610 /* only upper 6 bits (HASH_BITS) are used
2611 * which point to specific bit in he hash registers
2612 */
2613 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2614
2615 if (hash > 31) {
2616 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2617 tmp |= 1 << (hash - 32);
2618 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2619 } else {
2620 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2621 tmp |= 1 << hash;
2622 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
2625}
2626
Sascha Hauer22f6b862009-04-15 01:32:18 +00002627/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002628static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002629fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002631 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002632 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Lucas Stach44934fa2014-03-30 21:32:08 +02002634 if (addr) {
2635 if (!is_valid_ether_addr(addr->sa_data))
2636 return -EADDRNOTAVAIL;
2637 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2638 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002639
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002640 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2641 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002642 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002643 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002644 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002648#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002649/**
2650 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002651 * @dev: The FEC network adapter
2652 *
2653 * Polled functionality used by netconsole and others in non interrupt mode
2654 *
2655 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002656static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002657{
2658 int i;
2659 struct fec_enet_private *fep = netdev_priv(dev);
2660
2661 for (i = 0; i < FEC_IRQ_NUM; i++) {
2662 if (fep->irq[i] > 0) {
2663 disable_irq(fep->irq[i]);
2664 fec_enet_interrupt(fep->irq[i], dev);
2665 enable_irq(fep->irq[i]);
2666 }
2667 }
2668}
2669#endif
2670
Russell King8506fa12014-07-08 12:40:33 +01002671#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2672
Jim Baxter4c09eed2013-04-19 08:10:49 +00002673static int fec_set_features(struct net_device *netdev,
2674 netdev_features_t features)
2675{
2676 struct fec_enet_private *fep = netdev_priv(netdev);
2677 netdev_features_t changed = features ^ netdev->features;
2678
Russell King8506fa12014-07-08 12:40:33 +01002679 /* Quiesce the device if necessary */
2680 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2681 napi_disable(&fep->napi);
2682 netif_tx_lock_bh(netdev);
2683 fec_stop(netdev);
2684 }
2685
Jim Baxter4c09eed2013-04-19 08:10:49 +00002686 netdev->features = features;
2687
2688 /* Receive checksum has been changed */
2689 if (changed & NETIF_F_RXCSUM) {
2690 if (features & NETIF_F_RXCSUM)
2691 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2692 else
2693 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002694 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002695
Russell King8506fa12014-07-08 12:40:33 +01002696 /* Resume the device after updates */
2697 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002698 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002699 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002700 netif_tx_unlock_bh(netdev);
2701 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002702 }
2703
2704 return 0;
2705}
2706
Fugang Duan4d494cd2014-09-13 05:00:48 +08002707u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
2708 void *accel_priv, select_queue_fallback_t fallback)
2709{
2710 return skb_tx_hash(ndev, skb);
2711}
2712
Sascha Hauer009fda82009-04-15 01:32:23 +00002713static const struct net_device_ops fec_netdev_ops = {
2714 .ndo_open = fec_enet_open,
2715 .ndo_stop = fec_enet_close,
2716 .ndo_start_xmit = fec_enet_start_xmit,
Fugang Duan4d494cd2014-09-13 05:00:48 +08002717 .ndo_select_queue = fec_enet_select_queue,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002718 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002719 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002720 .ndo_validate_addr = eth_validate_addr,
2721 .ndo_tx_timeout = fec_timeout,
2722 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002723 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002724#ifdef CONFIG_NET_POLL_CONTROLLER
2725 .ndo_poll_controller = fec_poll_controller,
2726#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002727 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002728};
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /*
2731 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002734static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002736 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002737 const struct platform_device_id *id_entry =
2738 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002739 struct fec_enet_priv_tx_q *txq;
2740 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002741 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002742 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002743 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002744 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002745
Frank Li59d0f7462014-09-13 05:00:50 +08002746 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002747
Nimrod Andy55d02182014-06-12 08:16:21 +08002748 if (fep->bufdesc_ex)
2749 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2750 else
2751 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002752 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002753 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002755 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002756 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002757 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002758 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002759 return -ENOMEM;
2760 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002761
Fugang Duan4d494cd2014-09-13 05:00:48 +08002762 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Shawn Guo49da97d2011-01-05 21:13:11 +00002764 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002765 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002766 /* make sure MAC we just acquired is programmed into the hw */
2767 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002769 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08002770 for (i = 0; i < fep->num_rx_queues; i++) {
2771 rxq = fep->rx_queue[i];
2772 rxq->index = i;
2773 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2774 rxq->bd_dma = bd_dma;
2775 if (fep->bufdesc_ex) {
2776 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2777 cbd_base = (struct bufdesc *)
2778 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2779 } else {
2780 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2781 cbd_base += rxq->rx_ring_size;
2782 }
2783 }
2784
2785 for (i = 0; i < fep->num_tx_queues; i++) {
2786 txq = fep->tx_queue[i];
2787 txq->index = i;
2788 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2789 txq->bd_dma = bd_dma;
2790 if (fep->bufdesc_ex) {
2791 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
2792 cbd_base = (struct bufdesc *)
2793 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
2794 } else {
2795 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
2796 cbd_base += txq->tx_ring_size;
2797 }
2798 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Sascha Hauer22f6b862009-04-15 01:32:18 +00002801 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002802 ndev->watchdog_timeo = TX_TIMEOUT;
2803 ndev->netdev_ops = &fec_netdev_ops;
2804 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002805
Frank Lidc975382013-01-28 18:31:42 +00002806 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002807 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002808
Nimrod Andy09d1e542014-06-12 08:16:20 +08002809 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002810 /* enable hw VLAN support */
2811 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002812
Shawn Guo48496252013-05-08 21:08:22 +00002813 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002814 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2815
Shawn Guo48496252013-05-08 21:08:22 +00002816 /* enable hw accelerator */
2817 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002818 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002819 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2820 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002821
Nimrod Andy09d1e542014-06-12 08:16:20 +08002822 ndev->hw_features = ndev->features;
2823
Russell Kingef833372014-07-08 12:40:43 +01002824 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return 0;
2827}
2828
Shawn Guoca2cc332011-06-25 02:04:35 +08002829#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002830static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002831{
2832 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002833 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002834 struct device_node *np = pdev->dev.of_node;
2835
2836 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002837 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002838
Shawn Guoa3caad02012-06-27 03:45:24 +00002839 of_property_read_u32(np, "phy-reset-duration", &msec);
2840 /* A sane reset duration should not be longer than 1s */
2841 if (msec > 1000)
2842 msec = 1;
2843
Shawn Guoca2cc332011-06-25 02:04:35 +08002844 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002845 if (!gpio_is_valid(phy_reset))
2846 return;
2847
Shawn Guo119fc002012-06-27 03:45:22 +00002848 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2849 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002850 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002851 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002852 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002853 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002854 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002855 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002856}
2857#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002858static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002859{
2860 /*
2861 * In case of platform probe, the reset has been done
2862 * by machine code.
2863 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002864}
2865#endif /* CONFIG_OF */
2866
Fugang Duan9fc095f2014-09-13 05:00:49 +08002867static void
2868fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
2869{
2870 struct device_node *np = pdev->dev.of_node;
2871 int err;
2872
2873 *num_tx = *num_rx = 1;
2874
2875 if (!np || !of_device_is_available(np))
2876 return;
2877
2878 /* parse the num of tx and rx queues */
2879 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
2880 err |= of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
2881 if (err) {
2882 *num_tx = 1;
2883 *num_rx = 1;
2884 return;
2885 }
2886
2887 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
2888 dev_err(&pdev->dev, "Invalidate num_tx(=%d), fail back to 1\n",
2889 *num_tx);
2890 *num_tx = 1;
2891 return;
2892 }
2893
2894 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
2895 dev_err(&pdev->dev, "Invalidate num_rx(=%d), fail back to 1\n",
2896 *num_rx);
2897 *num_rx = 1;
2898 return;
2899 }
2900
2901}
2902
Bill Pemberton33897cc2012-12-03 09:23:58 -05002903static int
Sascha Haueread73182009-01-28 23:03:11 +00002904fec_probe(struct platform_device *pdev)
2905{
2906 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002907 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002908 struct net_device *ndev;
2909 int i, irq, ret = 0;
2910 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002911 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002912 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002913 struct device_node *np = pdev->dev.of_node, *phy_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08002914 int num_tx_qs = 1;
2915 int num_rx_qs = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002916
2917 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2918 if (of_id)
2919 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002920
Fugang Duan9fc095f2014-09-13 05:00:49 +08002921 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
2922
Sascha Haueread73182009-01-28 23:03:11 +00002923 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08002924 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
2925 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00002926 if (!ndev)
2927 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002928
2929 SET_NETDEV_DEV(ndev, &pdev->dev);
2930
2931 /* setup board info structure */
2932 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002933
Fugang Duan9fc095f2014-09-13 05:00:49 +08002934 fep->num_rx_queues = num_rx_qs;
2935 fep->num_tx_queues = num_tx_qs;
2936
Guenter Roeckd1391932013-06-18 10:04:59 -07002937#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002938 /* default enable pause frame auto negotiation */
2939 if (pdev->id_entry &&
2940 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2941 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002942#endif
Frank Libaa70a52013-01-16 16:55:58 +00002943
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002944 /* Select default pin state */
2945 pinctrl_pm_select_default_state(&pdev->dev);
2946
Fabio Estevam399db752013-07-21 13:25:03 -03002947 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302948 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2949 if (IS_ERR(fep->hwp)) {
2950 ret = PTR_ERR(fep->hwp);
2951 goto failed_ioremap;
2952 }
2953
Bryan Wue6b043d2010-03-31 02:10:44 +00002954 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002955 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002956
Frank Liff43da82013-01-03 16:04:23 +00002957 fep->bufdesc_ex = 0;
2958
Sascha Haueread73182009-01-28 23:03:11 +00002959 platform_set_drvdata(pdev, ndev);
2960
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002961 phy_node = of_parse_phandle(np, "phy-handle", 0);
2962 if (!phy_node && of_phy_is_fixed_link(np)) {
2963 ret = of_phy_register_fixed_link(np);
2964 if (ret < 0) {
2965 dev_err(&pdev->dev,
2966 "broken fixed-link specification\n");
2967 goto failed_phy;
2968 }
2969 phy_node = of_node_get(np);
2970 }
2971 fep->phy_node = phy_node;
2972
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002973 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002974 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002975 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002976 if (pdata)
2977 fep->phy_interface = pdata->phy;
2978 else
2979 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2980 } else {
2981 fep->phy_interface = ret;
2982 }
2983
Fabio Estevame2f8d552013-02-22 06:40:45 +00002984 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2985 if (IS_ERR(fep->clk_ipg)) {
2986 ret = PTR_ERR(fep->clk_ipg);
2987 goto failed_clk;
2988 }
2989
2990 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2991 if (IS_ERR(fep->clk_ahb)) {
2992 ret = PTR_ERR(fep->clk_ahb);
2993 goto failed_clk;
2994 }
2995
Linus Torvalds38f56f32013-05-07 11:06:17 -07002996 /* enet_out is optional, depends on board */
2997 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2998 if (IS_ERR(fep->clk_enet_out))
2999 fep->clk_enet_out = NULL;
3000
Nimrod Andy91c0d982014-08-21 17:09:38 +08003001 fep->ptp_clk_on = false;
3002 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003003
3004 /* clk_ref is optional, depends on board */
3005 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3006 if (IS_ERR(fep->clk_ref))
3007 fep->clk_ref = NULL;
3008
Fabio Estevame2f8d552013-02-22 06:40:45 +00003009 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003010 fep->bufdesc_ex =
3011 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003012 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003013 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003014 fep->bufdesc_ex = 0;
3015 }
3016
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003017 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003018 if (ret)
3019 goto failed_clk;
3020
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003021 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3022 if (!IS_ERR(fep->reg_phy)) {
3023 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003024 if (ret) {
3025 dev_err(&pdev->dev,
3026 "Failed to enable phy regulator: %d\n", ret);
3027 goto failed_regulator;
3028 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003029 } else {
3030 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003031 }
3032
3033 fec_reset_phy(pdev);
3034
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003035 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003036 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003037
3038 ret = fec_enet_init(ndev);
3039 if (ret)
3040 goto failed_init;
3041
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003042 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003043 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003044 if (irq < 0) {
3045 if (i)
3046 break;
3047 ret = irq;
3048 goto failed_irq;
3049 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003050 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003051 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003052 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003053 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003054 }
3055
Bryan Wue6b043d2010-03-31 02:10:44 +00003056 ret = fec_enet_mii_init(pdev);
3057 if (ret)
3058 goto failed_mii_init;
3059
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003060 /* Carrier starts down, phylib will bring it up */
3061 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003062 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003063 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003064
Sascha Haueread73182009-01-28 23:03:11 +00003065 ret = register_netdev(ndev);
3066 if (ret)
3067 goto failed_register;
3068
Fabio Estevameb1d0642013-04-13 07:25:36 +00003069 if (fep->bufdesc_ex && fep->ptp_clock)
3070 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3071
Russell King36cdc742014-07-08 13:01:44 +01003072 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003073 return 0;
3074
3075failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003076 fec_enet_mii_remove(fep);
3077failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003078failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003079failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003080 if (fep->reg_phy)
3081 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003082failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003083 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003084failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003085failed_phy:
3086 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003087failed_ioremap:
3088 free_netdev(ndev);
3089
3090 return ret;
3091}
3092
Bill Pemberton33897cc2012-12-03 09:23:58 -05003093static int
Sascha Haueread73182009-01-28 23:03:11 +00003094fec_drv_remove(struct platform_device *pdev)
3095{
3096 struct net_device *ndev = platform_get_drvdata(pdev);
3097 struct fec_enet_private *fep = netdev_priv(ndev);
3098
Nimrod Andy91c0d982014-08-21 17:09:38 +08003099 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003100 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003101 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003102 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003103 if (fep->reg_phy)
3104 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003105 if (fep->ptp_clock)
3106 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003107 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003108 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003109 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003110
Sascha Haueread73182009-01-28 23:03:11 +00003111 return 0;
3112}
3113
Fabio Estevamdd66d382014-07-24 18:24:01 -03003114static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003115{
Eric Benard87cad5c2010-06-18 04:19:54 +00003116 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003117 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003118
Russell Kingda1774e2014-07-08 12:39:57 +01003119 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003120 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003121 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003122 napi_disable(&fep->napi);
3123 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003124 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003125 netif_tx_unlock_bh(ndev);
3126 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003127 }
Russell Kingda1774e2014-07-08 12:39:57 +01003128 rtnl_unlock();
3129
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003130 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003131 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003132
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003133 if (fep->reg_phy)
3134 regulator_disable(fep->reg_phy);
3135
Sascha Haueread73182009-01-28 23:03:11 +00003136 return 0;
3137}
3138
Fabio Estevamdd66d382014-07-24 18:24:01 -03003139static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003140{
Eric Benard87cad5c2010-06-18 04:19:54 +00003141 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003142 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003143 int ret;
3144
3145 if (fep->reg_phy) {
3146 ret = regulator_enable(fep->reg_phy);
3147 if (ret)
3148 return ret;
3149 }
Sascha Haueread73182009-01-28 23:03:11 +00003150
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003151 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003152 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003153 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003154 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003155
Russell Kingda1774e2014-07-08 12:39:57 +01003156 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003157 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003158 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003159 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003160 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003161 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003162 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003163 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003164 }
Russell Kingda1774e2014-07-08 12:39:57 +01003165 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003166
Sascha Haueread73182009-01-28 23:03:11 +00003167 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003168
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003169failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003170 if (fep->reg_phy)
3171 regulator_disable(fep->reg_phy);
3172 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003173}
3174
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003175static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003176
Sascha Haueread73182009-01-28 23:03:11 +00003177static struct platform_driver fec_driver = {
3178 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003179 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003180 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003181 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003182 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003183 },
Shawn Guob5680e02011-01-05 21:13:13 +00003184 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003185 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003186 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003187};
3188
Fabio Estevamaaca2372012-01-23 16:44:50 +00003189module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003191MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192MODULE_LICENSE("GPL");