blob: 9d3eed46830d73041b53c12411bf68d8939041e2 [file] [log] [blame]
Fabio Estevam1f508122018-05-20 13:55:19 -03001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10006 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * pages and then divide them into 2K frame buffers. This way I know I
8 * have buffers large enough to hold one frame within one buffer descriptor.
9 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
10 * will be much more memory efficient and will easily handle lots of
11 * small packets.
12 *
13 * Much better multiple PHY support by Magnus Damm.
14 * Copyright (c) 2000 Ericsson Radio Systems AB.
15 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100016 * Support for FEC controller of ColdFire processors.
17 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100018 *
19 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100020 * Copyright (c) 2004-2006 Macq Electronique SA.
Shawn Guob5680e02011-01-05 21:13:13 +000021 *
Shawn Guo230dec62011-09-23 02:12:48 +000022 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/string.h>
Andrew Lunn8fff7552015-07-25 22:38:02 +020028#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/ptrace.h>
30#include <linux/errno.h>
31#include <linux/ioport.h>
32#include <linux/slab.h>
33#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000038#include <linux/in.h>
39#include <linux/ip.h>
40#include <net/ip.h>
Nimrod Andy79f33912014-06-12 08:16:23 +080041#include <net/tso.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000042#include <linux/tcp.h>
43#include <linux/udp.h>
44#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/spinlock.h>
46#include <linux/workqueue.h>
47#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000048#include <linux/io.h>
49#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000050#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000051#include <linux/platform_device.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010052#include <linux/mdio.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000053#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070054#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080055#include <linux/of.h>
56#include <linux/of_device.h>
57#include <linux/of_gpio.h>
Uwe Kleine-König407066f2014-08-11 17:35:33 +020058#include <linux/of_mdio.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080059#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000060#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010061#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030062#include <linux/pinctrl/consumer.h>
Frank Lic259c132014-10-03 14:29:14 -070063#include <linux/prefetch.h>
Lucas Stach29380902016-06-03 18:31:19 +020064#include <soc/imx/cpuidle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Greg Ungerer080853a2007-07-30 16:28:46 +100066#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Christoph Müllner772e42b2013-06-27 21:18:23 +020070static void set_multicast_list(struct net_device *ndev);
Fugang Duand851b472014-09-17 05:18:52 +080071static void fec_enet_itr_coal_init(struct net_device *ndev);
Christoph Müllner772e42b2013-06-27 21:18:23 +020072
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
Andrew Lunn8fff7552015-07-25 22:38:02 +020084#define FEC_MDIO_PM_TIMEOUT 100 /* ms */
Frank Libaa70a52013-01-16 16:55:58 +000085
Shawn Guob5680e02011-01-05 21:13:13 +000086static struct platform_device_id fec_devtype[] = {
87 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080088 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000089 .name = DRIVER_NAME,
90 .driver_data = 0,
91 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080092 .name = "imx25-fec",
Andrew Lunn2b308422017-06-07 03:57:09 +020093 .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR,
Shawn Guo0ca1e292011-07-01 18:11:22 +080094 }, {
95 .name = "imx27-fec",
Andrew Lunn2b308422017-06-07 03:57:09 +020096 .driver_data = FEC_QUIRK_MIB_CLEAR,
Shawn Guo0ca1e292011-07-01 18:11:22 +080097 }, {
Shawn Guob5680e02011-01-05 21:13:13 +000098 .name = "imx28-fec",
Stefan Agner3d125f92015-01-14 00:20:21 +010099 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
Greg Ungerer18803492015-06-20 15:51:57 +1000100 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800101 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000102 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000103 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100104 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Greg Ungerer18803492015-06-20 15:51:57 +1000105 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
106 FEC_QUIRK_HAS_RACC,
Shawn Guo230dec62011-09-23 02:12:48 +0000107 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000108 .name = "mvf600-fec",
Greg Ungerer18803492015-06-20 15:51:57 +1000109 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
Jingchang Luca7c4a42013-04-11 21:12:45 +0000110 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800111 .name = "imx6sx-fec",
112 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
113 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800114 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
Greg Ungerer18803492015-06-20 15:51:57 +1000115 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
Fugang Duanff7566b2016-06-22 18:52:35 +0800116 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE,
Fugang Duan95a77472014-09-13 05:00:47 +0800117 }, {
Fugang Duana51d3ab2016-06-22 18:52:36 +0800118 .name = "imx6ul-fec",
119 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
120 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Fugang Duan99492ad2017-04-11 19:13:08 +0800121 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
122 FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
123 FEC_QUIRK_HAS_COALESCE,
Fugang Duana51d3ab2016-06-22 18:52:36 +0800124 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800125 /* sentinel */
126 }
Shawn Guob5680e02011-01-05 21:13:13 +0000127};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800128MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000129
Shawn Guoca2cc332011-06-25 02:04:35 +0800130enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000131 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800132 IMX27_FEC, /* runs on i.mx27/35/51 */
133 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000134 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000135 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800136 IMX6SX_FEC,
Fugang Duana51d3ab2016-06-22 18:52:36 +0800137 IMX6UL_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800138};
139
140static const struct of_device_id fec_dt_ids[] = {
141 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
142 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
143 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000144 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000145 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800146 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Fugang Duana51d3ab2016-06-22 18:52:36 +0800147 { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800148 { /* sentinel */ }
149};
150MODULE_DEVICE_TABLE(of, fec_dt_ids);
151
Shawn Guo49da97d2011-01-05 21:13:11 +0000152static unsigned char macaddr[ETH_ALEN];
153module_param_array(macaddr, byte, NULL, 0);
154MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
155
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000156#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * Some hardware gets it MAC address out of local flash memory.
159 * if this is non-zero then assume it is the address to get MAC from.
160 */
161#if defined(CONFIG_NETtel)
162#define FEC_FLASHMAC 0xf0006006
163#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
164#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#elif defined(CONFIG_CANCam)
166#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000167#elif defined (CONFIG_M5272C3)
168#define FEC_FLASHMAC (0xffe04000 + 4)
169#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000170#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#else
172#define FEC_FLASHMAC 0
173#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800174#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000175
Jim Baxtercdffcf12013-07-02 22:52:56 +0100176/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Andrew Lunnfbbeefd2017-07-30 19:36:05 +0200177 *
178 * 2048 byte skbufs are allocated. However, alignment requirements
179 * varies between FEC variants. Worst case is 64, so round down by 64.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Andrew Lunnfbbeefd2017-07-30 19:36:05 +0200181#define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define PKT_MINBUF_SIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Jim Baxter4c09eed2013-04-19 08:10:49 +0000184/* FEC receive acceleration */
185#define FEC_RACC_IPDIS (1 << 1)
186#define FEC_RACC_PRODIS (1 << 2)
Eric Nelson3ac72b72016-09-24 07:42:19 -0700187#define FEC_RACC_SHIFT16 BIT(7)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000188#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
189
Andrew Lunn2b308422017-06-07 03:57:09 +0200190/* MIB Control Register */
191#define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
Matt Waddel6b265292006-06-27 13:10:56 +1000194 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * size bits. Other FEC hardware does not, so we need to take that into
196 * account when setting it.
197 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000198#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Lucas Stach3f1dcc62018-01-17 19:30:55 +0100199 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
200 defined(CONFIG_ARM64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
202#else
203#define OPT_FRAME_SIZE 0
204#endif
205
Bryan Wue6b043d2010-03-31 02:10:44 +0000206/* FEC MII MMFR bits definition */
207#define FEC_MMFR_ST (1 << 30)
208#define FEC_MMFR_OP_READ (2 << 28)
209#define FEC_MMFR_OP_WRITE (1 << 28)
210#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
211#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
212#define FEC_MMFR_TA (2 << 16)
213#define FEC_MMFR_DATA(v) (v & 0xffff)
Nimrod Andyde40ed32014-12-24 17:30:39 +0800214/* FEC ECR bits definition */
215#define FEC_ECR_MAGICEN (1 << 2)
216#define FEC_ECR_SLEEP (1 << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500218#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Sascha Hauer22f6b862009-04-15 01:32:18 +0000220/* Transmitter timeout */
221#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Frank Libaa70a52013-01-16 16:55:58 +0000223#define FEC_PAUSE_FLAG_AUTONEG 0x1
224#define FEC_PAUSE_FLAG_ENABLE 0x2
Nimrod Andyde40ed32014-12-24 17:30:39 +0800225#define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
226#define FEC_WOL_FLAG_ENABLE (0x1 << 1)
227#define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
Frank Libaa70a52013-01-16 16:55:58 +0000228
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800229#define COPYBREAK_DEFAULT 256
230
Nimrod Andy79f33912014-06-12 08:16:23 +0800231/* Max number of allowed TCP segments for software TSO */
232#define FEC_MAX_TSO_SEGS 100
233#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
234
235#define IS_TSO_HEADER(txq, addr) \
236 ((addr >= txq->tso_hdrs_dma) && \
Troy Kisky7355f272016-02-05 14:52:46 -0700237 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
Nimrod Andy79f33912014-06-12 08:16:23 +0800238
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000239static int mii_cnt;
240
Troy Kisky7355f272016-02-05 14:52:46 -0700241static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
242 struct bufdesc_prop *bd)
Frank Liff43da82013-01-03 16:04:23 +0000243{
Troy Kisky7355f272016-02-05 14:52:46 -0700244 return (bdp >= bd->last) ? bd->base
Fugang Duan145d6e22017-04-11 19:13:04 +0800245 : (struct bufdesc *)(((void *)bdp) + bd->dsize);
Frank Liff43da82013-01-03 16:04:23 +0000246}
247
Troy Kisky7355f272016-02-05 14:52:46 -0700248static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
249 struct bufdesc_prop *bd)
Frank Liff43da82013-01-03 16:04:23 +0000250{
Troy Kisky7355f272016-02-05 14:52:46 -0700251 return (bdp <= bd->base) ? bd->last
Fugang Duan145d6e22017-04-11 19:13:04 +0800252 : (struct bufdesc *)(((void *)bdp) - bd->dsize);
Frank Liff43da82013-01-03 16:04:23 +0000253}
254
Troy Kisky7355f272016-02-05 14:52:46 -0700255static int fec_enet_get_bd_index(struct bufdesc *bdp,
256 struct bufdesc_prop *bd)
Nimrod Andy61a44272014-06-12 08:16:18 +0800257{
Troy Kisky7355f272016-02-05 14:52:46 -0700258 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
Nimrod Andy61a44272014-06-12 08:16:18 +0800259}
260
Troy Kisky7355f272016-02-05 14:52:46 -0700261static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800262{
263 int entries;
264
Troy Kisky7355f272016-02-05 14:52:46 -0700265 entries = (((const char *)txq->dirty_tx -
266 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800267
Troy Kisky7355f272016-02-05 14:52:46 -0700268 return entries >= 0 ? entries : entries + txq->bd.ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800269}
270
Lothar Waßmannc20e5992014-11-17 10:51:24 +0100271static void swap_buffer(void *bufaddr, int len)
Shawn Guob5680e02011-01-05 21:13:13 +0000272{
273 int i;
274 unsigned int *buf = bufaddr;
275
Lothar Waßmann7b487d02014-11-17 10:51:23 +0100276 for (i = 0; i < len; i += 4, buf++)
Lothar Waßmanne4537892014-11-17 10:51:22 +0100277 swab32s(buf);
Shawn Guob5680e02011-01-05 21:13:13 +0000278}
279
Lothar Waßmann1310b542014-11-07 10:02:47 +0100280static void swap_buffer2(void *dst_buf, void *src_buf, int len)
281{
282 int i;
283 unsigned int *src = src_buf;
284 unsigned int *dst = dst_buf;
285
286 for (i = 0; i < len; i += 4, src++, dst++)
287 *dst = swab32p(src);
288}
289
Russell King344756f2014-07-08 13:01:59 +0100290static void fec_dump(struct net_device *ndev)
291{
292 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800293 struct bufdesc *bdp;
294 struct fec_enet_priv_tx_q *txq;
295 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100296
297 netdev_info(ndev, "TX ring dump\n");
298 pr_info("Nr SC addr len SKB\n");
299
Fugang Duan4d494cd2014-09-13 05:00:48 +0800300 txq = fep->tx_queue[0];
Troy Kisky7355f272016-02-05 14:52:46 -0700301 bdp = txq->bd.base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800302
Russell King344756f2014-07-08 13:01:59 +0100303 do {
Johannes Berg5cfa3032016-01-24 16:52:37 +0100304 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
Russell King344756f2014-07-08 13:01:59 +0100305 index,
Troy Kisky7355f272016-02-05 14:52:46 -0700306 bdp == txq->bd.cur ? 'S' : ' ',
Fugang Duan4d494cd2014-09-13 05:00:48 +0800307 bdp == txq->dirty_tx ? 'H' : ' ',
Johannes Berg5cfa3032016-01-24 16:52:37 +0100308 fec16_to_cpu(bdp->cbd_sc),
309 fec32_to_cpu(bdp->cbd_bufaddr),
310 fec16_to_cpu(bdp->cbd_datlen),
Fugang Duan4d494cd2014-09-13 05:00:48 +0800311 txq->tx_skbuff[index]);
Troy Kisky7355f272016-02-05 14:52:46 -0700312 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Russell King344756f2014-07-08 13:01:59 +0100313 index++;
Troy Kisky7355f272016-02-05 14:52:46 -0700314 } while (bdp != txq->bd.base);
Russell King344756f2014-07-08 13:01:59 +0100315}
316
Fugang Duan62a02c92014-06-18 08:33:52 +0800317static inline bool is_ipv4_pkt(struct sk_buff *skb)
318{
319 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
320}
321
Jim Baxter4c09eed2013-04-19 08:10:49 +0000322static int
323fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
324{
325 /* Only run for packets requiring a checksum. */
326 if (skb->ip_summed != CHECKSUM_PARTIAL)
327 return 0;
328
329 if (unlikely(skb_cow_head(skb, 0)))
330 return -1;
331
Fugang Duan62a02c92014-06-18 08:33:52 +0800332 if (is_ipv4_pkt(skb))
333 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000334 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
335
336 return 0;
337}
338
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800339static struct bufdesc *
Fugang Duan4d494cd2014-09-13 05:00:48 +0800340fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
341 struct sk_buff *skb,
342 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800343{
344 struct fec_enet_private *fep = netdev_priv(ndev);
Troy Kisky7355f272016-02-05 14:52:46 -0700345 struct bufdesc *bdp = txq->bd.cur;
Nimrod Andy6e909282014-06-12 08:16:22 +0800346 struct bufdesc_ex *ebdp;
347 int nr_frags = skb_shinfo(skb)->nr_frags;
348 int frag, frag_len;
349 unsigned short status;
350 unsigned int estatus = 0;
351 skb_frag_t *this_frag;
352 unsigned int index;
353 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100354 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800355 int i;
356
357 for (frag = 0; frag < nr_frags; frag++) {
358 this_frag = &skb_shinfo(skb)->frags[frag];
Troy Kisky7355f272016-02-05 14:52:46 -0700359 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800360 ebdp = (struct bufdesc_ex *)bdp;
361
Johannes Berg5cfa3032016-01-24 16:52:37 +0100362 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800363 status &= ~BD_ENET_TX_STATS;
364 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
365 frag_len = skb_shinfo(skb)->frags[frag].size;
366
367 /* Handle the last BD specially */
368 if (frag == nr_frags - 1) {
369 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
370 if (fep->bufdesc_ex) {
371 estatus |= BD_ENET_TX_INT;
372 if (unlikely(skb_shinfo(skb)->tx_flags &
373 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
374 estatus |= BD_ENET_TX_TS;
375 }
376 }
377
378 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100379 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700380 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy6e909282014-06-12 08:16:22 +0800381 if (skb->ip_summed == CHECKSUM_PARTIAL)
382 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
383 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100384 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800385 }
386
387 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
388
Troy Kisky7355f272016-02-05 14:52:46 -0700389 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800390 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100391 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800392 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
393 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800394
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100395 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800396 swap_buffer(bufaddr, frag_len);
397 }
398
Russell Kingd6bf3142014-07-08 00:23:19 +0100399 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
400 DMA_TO_DEVICE);
401 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800402 if (net_ratelimit())
403 netdev_err(ndev, "Tx DMA memory map failed\n");
404 goto dma_mapping_error;
405 }
406
Johannes Berg5cfa3032016-01-24 16:52:37 +0100407 bdp->cbd_bufaddr = cpu_to_fec32(addr);
408 bdp->cbd_datlen = cpu_to_fec16(frag_len);
Troy Kiskybe293462016-02-05 14:52:49 -0700409 /* Make sure the updates to rest of the descriptor are
410 * performed before transferring ownership.
411 */
412 wmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +0100413 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800414 }
415
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800416 return bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800417dma_mapping_error:
Troy Kisky7355f272016-02-05 14:52:46 -0700418 bdp = txq->bd.cur;
Nimrod Andy6e909282014-06-12 08:16:22 +0800419 for (i = 0; i < frag; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -0700420 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100421 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
422 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
Nimrod Andy6e909282014-06-12 08:16:22 +0800423 }
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800424 return ERR_PTR(-ENOMEM);
Nimrod Andy6e909282014-06-12 08:16:22 +0800425}
426
Fugang Duan4d494cd2014-09-13 05:00:48 +0800427static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
428 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800429{
430 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800431 int nr_frags = skb_shinfo(skb)->nr_frags;
432 struct bufdesc *bdp, *last_bdp;
433 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100434 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800435 unsigned short status;
436 unsigned short buflen;
437 unsigned int estatus = 0;
438 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800439 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800440
Troy Kisky7355f272016-02-05 14:52:46 -0700441 entries_free = fec_enet_get_free_txdesc_num(txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800442 if (entries_free < MAX_SKB_FRAGS + 1) {
443 dev_kfree_skb_any(skb);
444 if (net_ratelimit())
445 netdev_err(ndev, "NOT enough BD for SG!\n");
446 return NETDEV_TX_OK;
447 }
448
Nimrod Andy6e909282014-06-12 08:16:22 +0800449 /* Protocol checksum off-load for TCP and UDP. */
450 if (fec_enet_clear_csum(skb, ndev)) {
451 dev_kfree_skb_any(skb);
452 return NETDEV_TX_OK;
453 }
454
455 /* Fill in a Tx ring entry */
Troy Kisky7355f272016-02-05 14:52:46 -0700456 bdp = txq->bd.cur;
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800457 last_bdp = bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100458 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800459 status &= ~BD_ENET_TX_STATS;
460
461 /* Set buffer length and buffer pointer */
462 bufaddr = skb->data;
463 buflen = skb_headlen(skb);
464
Troy Kisky7355f272016-02-05 14:52:46 -0700465 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800466 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100467 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800468 memcpy(txq->tx_bounce[index], skb->data, buflen);
469 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800470
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100471 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800472 swap_buffer(bufaddr, buflen);
473 }
474
Russell Kingd6bf3142014-07-08 00:23:19 +0100475 /* Push the data cache so the CPM does not get stale memory data. */
476 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
477 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800478 dev_kfree_skb_any(skb);
479 if (net_ratelimit())
480 netdev_err(ndev, "Tx DMA memory map failed\n");
481 return NETDEV_TX_OK;
482 }
483
484 if (nr_frags) {
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800485 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Troy Kiskyfc75ba52016-02-05 14:52:50 -0700486 if (IS_ERR(last_bdp)) {
487 dma_unmap_single(&fep->pdev->dev, addr,
488 buflen, DMA_TO_DEVICE);
489 dev_kfree_skb_any(skb);
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800490 return NETDEV_TX_OK;
Troy Kiskyfc75ba52016-02-05 14:52:50 -0700491 }
Nimrod Andy6e909282014-06-12 08:16:22 +0800492 } else {
493 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
494 if (fep->bufdesc_ex) {
495 estatus = BD_ENET_TX_INT;
496 if (unlikely(skb_shinfo(skb)->tx_flags &
497 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
498 estatus |= BD_ENET_TX_TS;
499 }
500 }
Troy Kiskyfc75ba52016-02-05 14:52:50 -0700501 bdp->cbd_bufaddr = cpu_to_fec32(addr);
502 bdp->cbd_datlen = cpu_to_fec16(buflen);
Nimrod Andy6e909282014-06-12 08:16:22 +0800503
504 if (fep->bufdesc_ex) {
505
506 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
507
508 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
509 fep->hwts_tx_en))
510 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
511
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100512 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700513 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andybefe8212014-09-23 15:40:55 +0800514
Nimrod Andy6e909282014-06-12 08:16:22 +0800515 if (skb->ip_summed == CHECKSUM_PARTIAL)
516 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
517
518 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100519 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800520 }
521
Troy Kisky7355f272016-02-05 14:52:46 -0700522 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800523 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800524 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800525
Troy Kiskybe293462016-02-05 14:52:49 -0700526 /* Make sure the updates to rest of the descriptor are performed before
527 * transferring ownership.
528 */
529 wmb();
Nimrod Andy6e909282014-06-12 08:16:22 +0800530
531 /* Send it on its way. Tell FEC it's ready, interrupt when done,
532 * it's the last BD of the frame, and to put the CRC on the end.
533 */
534 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100535 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800536
Sascha Hauer22f6b862009-04-15 01:32:18 +0000537 /* If this was the last BD in the ring, start at the beginning again. */
Troy Kisky7355f272016-02-05 14:52:46 -0700538 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800540 skb_tx_timestamp(skb);
541
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800542 /* Make sure the update to bdp and tx_skbuff are performed before
Troy Kisky7355f272016-02-05 14:52:46 -0700543 * txq->bd.cur.
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800544 */
545 wmb();
Troy Kisky7355f272016-02-05 14:52:46 -0700546 txq->bd.cur = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Frank Lide5fb0a2013-03-03 17:34:25 +0000548 /* Trigger transmission start */
Troy Kisky53bb20d2016-02-05 14:52:47 -0700549 writel(0, txq->bd.reg_desc_active);
Frank Lide5fb0a2013-03-03 17:34:25 +0000550
Nimrod Andy6e909282014-06-12 08:16:22 +0800551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Nimrod Andy79f33912014-06-12 08:16:23 +0800554static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800555fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
556 struct net_device *ndev,
557 struct bufdesc *bdp, int index, char *data,
558 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800559{
560 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200561 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800562 unsigned short status;
563 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100564 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800565
Johannes Berg5cfa3032016-01-24 16:52:37 +0100566 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800567 status &= ~BD_ENET_TX_STATS;
568
569 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800570
Fugang Duan41ef84c2014-09-13 05:00:54 +0800571 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100572 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800573 memcpy(txq->tx_bounce[index], data, size);
574 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800575
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100576 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800577 swap_buffer(data, size);
578 }
579
Russell Kingd6bf3142014-07-08 00:23:19 +0100580 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
581 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800582 dev_kfree_skb_any(skb);
583 if (net_ratelimit())
584 netdev_err(ndev, "Tx DMA memory map failed\n");
585 return NETDEV_TX_BUSY;
586 }
587
Johannes Berg5cfa3032016-01-24 16:52:37 +0100588 bdp->cbd_datlen = cpu_to_fec16(size);
589 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Russell Kingd6bf3142014-07-08 00:23:19 +0100590
Nimrod Andy79f33912014-06-12 08:16:23 +0800591 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100592 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700593 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy79f33912014-06-12 08:16:23 +0800594 if (skb->ip_summed == CHECKSUM_PARTIAL)
595 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
596 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100597 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800598 }
599
600 /* Handle the last BD specially */
601 if (last_tcp)
602 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
603 if (is_last) {
604 status |= BD_ENET_TX_INTR;
605 if (fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100606 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
Nimrod Andy79f33912014-06-12 08:16:23 +0800607 }
608
Johannes Berg5cfa3032016-01-24 16:52:37 +0100609 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800610
611 return 0;
612}
613
614static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800615fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
616 struct sk_buff *skb, struct net_device *ndev,
617 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800618{
619 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800620 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200621 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800622 void *bufaddr;
623 unsigned long dmabuf;
624 unsigned short status;
625 unsigned int estatus = 0;
626
Johannes Berg5cfa3032016-01-24 16:52:37 +0100627 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800628 status &= ~BD_ENET_TX_STATS;
629 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
630
Fugang Duan4d494cd2014-09-13 05:00:48 +0800631 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
632 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800633 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100634 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800635 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
636 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800637
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100638 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800639 swap_buffer(bufaddr, hdr_len);
640
641 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
642 hdr_len, DMA_TO_DEVICE);
643 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
644 dev_kfree_skb_any(skb);
645 if (net_ratelimit())
646 netdev_err(ndev, "Tx DMA memory map failed\n");
647 return NETDEV_TX_BUSY;
648 }
649 }
650
Johannes Berg5cfa3032016-01-24 16:52:37 +0100651 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
652 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
Nimrod Andy79f33912014-06-12 08:16:23 +0800653
654 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100655 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700656 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy79f33912014-06-12 08:16:23 +0800657 if (skb->ip_summed == CHECKSUM_PARTIAL)
658 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
659 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100660 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800661 }
662
Johannes Berg5cfa3032016-01-24 16:52:37 +0100663 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800664
665 return 0;
666}
667
Fugang Duan4d494cd2014-09-13 05:00:48 +0800668static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
669 struct sk_buff *skb,
670 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800671{
672 struct fec_enet_private *fep = netdev_priv(ndev);
673 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
674 int total_len, data_left;
Troy Kisky7355f272016-02-05 14:52:46 -0700675 struct bufdesc *bdp = txq->bd.cur;
Nimrod Andy79f33912014-06-12 08:16:23 +0800676 struct tso_t tso;
677 unsigned int index = 0;
678 int ret;
679
Troy Kisky7355f272016-02-05 14:52:46 -0700680 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800681 dev_kfree_skb_any(skb);
682 if (net_ratelimit())
683 netdev_err(ndev, "NOT enough BD for TSO!\n");
684 return NETDEV_TX_OK;
685 }
686
687 /* Protocol checksum off-load for TCP and UDP. */
688 if (fec_enet_clear_csum(skb, ndev)) {
689 dev_kfree_skb_any(skb);
690 return NETDEV_TX_OK;
691 }
692
693 /* Initialize the TSO handler, and prepare the first payload */
694 tso_start(skb, &tso);
695
696 total_len = skb->len - hdr_len;
697 while (total_len > 0) {
698 char *hdr;
699
Troy Kisky7355f272016-02-05 14:52:46 -0700700 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800701 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
702 total_len -= data_left;
703
704 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800705 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800706 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800707 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800708 if (ret)
709 goto err_release;
710
711 while (data_left > 0) {
712 int size;
713
714 size = min_t(int, tso.size, data_left);
Troy Kisky7355f272016-02-05 14:52:46 -0700715 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
716 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800717 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
718 bdp, index,
719 tso.data, size,
720 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800721 total_len == 0);
722 if (ret)
723 goto err_release;
724
725 data_left -= size;
726 tso_build_data(skb, &tso, size);
727 }
728
Troy Kisky7355f272016-02-05 14:52:46 -0700729 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800730 }
731
732 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800733 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800734
Nimrod Andy79f33912014-06-12 08:16:23 +0800735 skb_tx_timestamp(skb);
Troy Kisky7355f272016-02-05 14:52:46 -0700736 txq->bd.cur = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800737
738 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100739 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Troy Kisky53bb20d2016-02-05 14:52:47 -0700740 !readl(txq->bd.reg_desc_active) ||
741 !readl(txq->bd.reg_desc_active) ||
742 !readl(txq->bd.reg_desc_active) ||
743 !readl(txq->bd.reg_desc_active))
744 writel(0, txq->bd.reg_desc_active);
Nimrod Andy79f33912014-06-12 08:16:23 +0800745
746 return 0;
747
748err_release:
749 /* TODO: Release all used data descriptors for TSO */
750 return ret;
751}
752
Nimrod Andy61a44272014-06-12 08:16:18 +0800753static netdev_tx_t
754fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
755{
756 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800757 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800758 unsigned short queue;
759 struct fec_enet_priv_tx_q *txq;
760 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800761 int ret;
762
Fugang Duan4d494cd2014-09-13 05:00:48 +0800763 queue = skb_get_queue_mapping(skb);
764 txq = fep->tx_queue[queue];
765 nq = netdev_get_tx_queue(ndev, queue);
766
Nimrod Andy79f33912014-06-12 08:16:23 +0800767 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800768 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800769 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800770 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800771 if (ret)
772 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800773
Troy Kisky7355f272016-02-05 14:52:46 -0700774 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800775 if (entries_free <= txq->tx_stop_threshold)
776 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800777
778 return NETDEV_TX_OK;
779}
780
Frank Li14109a52013-03-26 16:12:03 +0000781/* Init RX & TX buffer descriptors
782 */
783static void fec_enet_bd_init(struct net_device *dev)
784{
785 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800786 struct fec_enet_priv_tx_q *txq;
787 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000788 struct bufdesc *bdp;
789 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800790 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000791
Frank Li59d0f7462014-09-13 05:00:50 +0800792 for (q = 0; q < fep->num_rx_queues; q++) {
793 /* Initialize the receive buffer descriptors. */
794 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700795 bdp = rxq->bd.base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800796
Troy Kisky7355f272016-02-05 14:52:46 -0700797 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000798
Frank Li59d0f7462014-09-13 05:00:50 +0800799 /* Initialize the BD for every fragment in the page. */
800 if (bdp->cbd_bufaddr)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100801 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Li59d0f7462014-09-13 05:00:50 +0800802 else
Johannes Berg5cfa3032016-01-24 16:52:37 +0100803 bdp->cbd_sc = cpu_to_fec16(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700804 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Frank Li14109a52013-03-26 16:12:03 +0000805 }
Frank Li59d0f7462014-09-13 05:00:50 +0800806
807 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700808 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100809 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800810
Troy Kisky7355f272016-02-05 14:52:46 -0700811 rxq->bd.cur = rxq->bd.base;
Frank Li14109a52013-03-26 16:12:03 +0000812 }
813
Frank Li59d0f7462014-09-13 05:00:50 +0800814 for (q = 0; q < fep->num_tx_queues; q++) {
815 /* ...and the same for transmit */
816 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700817 bdp = txq->bd.base;
818 txq->bd.cur = bdp;
Frank Li59d0f7462014-09-13 05:00:50 +0800819
Troy Kisky7355f272016-02-05 14:52:46 -0700820 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800821 /* Initialize the BD for every fragment in the page. */
Johannes Berg5cfa3032016-01-24 16:52:37 +0100822 bdp->cbd_sc = cpu_to_fec16(0);
Fugang Duan178e5f52017-12-22 17:12:09 +0800823 if (bdp->cbd_bufaddr &&
824 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
825 dma_unmap_single(&fep->pdev->dev,
826 fec32_to_cpu(bdp->cbd_bufaddr),
827 fec16_to_cpu(bdp->cbd_datlen),
828 DMA_TO_DEVICE);
Frank Li59d0f7462014-09-13 05:00:50 +0800829 if (txq->tx_skbuff[i]) {
830 dev_kfree_skb_any(txq->tx_skbuff[i]);
831 txq->tx_skbuff[i] = NULL;
832 }
Johannes Berg5cfa3032016-01-24 16:52:37 +0100833 bdp->cbd_bufaddr = cpu_to_fec32(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700834 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Li59d0f7462014-09-13 05:00:50 +0800835 }
836
837 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700838 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100839 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800840 txq->dirty_tx = bdp;
841 }
842}
843
Frank Lice99d0d2014-09-13 05:00:52 +0800844static void fec_enet_active_rxring(struct net_device *ndev)
845{
846 struct fec_enet_private *fep = netdev_priv(ndev);
847 int i;
848
849 for (i = 0; i < fep->num_rx_queues; i++)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700850 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
Frank Lice99d0d2014-09-13 05:00:52 +0800851}
852
Frank Li59d0f7462014-09-13 05:00:50 +0800853static void fec_enet_enable_ring(struct net_device *ndev)
854{
855 struct fec_enet_private *fep = netdev_priv(ndev);
856 struct fec_enet_priv_tx_q *txq;
857 struct fec_enet_priv_rx_q *rxq;
858 int i;
859
860 for (i = 0; i < fep->num_rx_queues; i++) {
861 rxq = fep->rx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700862 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
Andrew Lunnfbbeefd2017-07-30 19:36:05 +0200863 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800864
865 /* enable DMA1/2 */
866 if (i)
867 writel(RCMR_MATCHEN | RCMR_CMP(i),
868 fep->hwp + FEC_RCMR(i));
869 }
870
871 for (i = 0; i < fep->num_tx_queues; i++) {
872 txq = fep->tx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700873 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800874
875 /* enable DMA1/2 */
876 if (i)
877 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
878 fep->hwp + FEC_DMA_CFG(i));
879 }
880}
881
882static void fec_enet_reset_skb(struct net_device *ndev)
883{
884 struct fec_enet_private *fep = netdev_priv(ndev);
885 struct fec_enet_priv_tx_q *txq;
886 int i, j;
887
888 for (i = 0; i < fep->num_tx_queues; i++) {
889 txq = fep->tx_queue[i];
890
Troy Kisky7355f272016-02-05 14:52:46 -0700891 for (j = 0; j < txq->bd.ring_size; j++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800892 if (txq->tx_skbuff[j]) {
893 dev_kfree_skb_any(txq->tx_skbuff[j]);
894 txq->tx_skbuff[j] = NULL;
895 }
896 }
897 }
Frank Li14109a52013-03-26 16:12:03 +0000898}
899
Russell Kingdbc64a82014-07-08 12:40:12 +0100900/*
901 * This function is called to start or restart the FEC during a link
902 * change, transmit timeout, or to reconfigure the FEC. The network
903 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100904 */
905static void
Russell Kingef833372014-07-08 12:40:43 +0100906fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100907{
908 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000909 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100910 u32 temp_mac[2];
911 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000912 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100913
Fugang Duan106c3142014-09-13 05:00:51 +0800914 /* Whack a reset. We should wait for this.
915 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
916 * instead of reset MAC itself.
917 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100918 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800919 writel(0, fep->hwp + FEC_ECNTRL);
920 } else {
921 writel(1, fep->hwp + FEC_ECNTRL);
922 udelay(10);
923 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100924
925 /*
926 * enet-mac reset will reset mac address registers too,
927 * so need to reconfigure it.
928 */
Gavin Schenkb82d44d2016-09-30 11:46:10 +0200929 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
930 writel((__force u32)cpu_to_be32(temp_mac[0]),
931 fep->hwp + FEC_ADDR_LOW);
932 writel((__force u32)cpu_to_be32(temp_mac[1]),
933 fep->hwp + FEC_ADDR_HIGH);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100934
935 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800936 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100937
Frank Li14109a52013-03-26 16:12:03 +0000938 fec_enet_bd_init(ndev);
939
Frank Li59d0f7462014-09-13 05:00:50 +0800940 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100941
Frank Li59d0f7462014-09-13 05:00:50 +0800942 /* Reset tx SKB buffers. */
943 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100944
945 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100946 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100947 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100948 writel(0x04, fep->hwp + FEC_X_CNTRL);
949 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100950 /* No Rcv on Xmit */
951 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100952 writel(0x0, fep->hwp + FEC_X_CNTRL);
953 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100954
Uwe Kleine-König45993652011-01-19 20:47:04 +0100955 /* Set MII speed */
956 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
957
Guenter Roeckd1391932013-06-18 10:04:59 -0700958#if !defined(CONFIG_M5272)
Greg Ungerer18803492015-06-20 15:51:57 +1000959 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
Greg Ungerer18803492015-06-20 15:51:57 +1000960 val = readl(fep->hwp + FEC_RACC);
Eric Nelson3ac72b72016-09-24 07:42:19 -0700961 /* align IP header */
962 val |= FEC_RACC_SHIFT16;
Greg Ungerer18803492015-06-20 15:51:57 +1000963 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
Eric Nelson3ac72b72016-09-24 07:42:19 -0700964 /* set RX checksum */
Greg Ungerer18803492015-06-20 15:51:57 +1000965 val |= FEC_RACC_OPTIONS;
966 else
967 val &= ~FEC_RACC_OPTIONS;
968 writel(val, fep->hwp + FEC_RACC);
Fabio Estevam32867fc2016-03-31 12:05:17 -0300969 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
Greg Ungerer18803492015-06-20 15:51:57 +1000970 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700971#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000972
Uwe Kleine-König45993652011-01-19 20:47:04 +0100973 /*
974 * The phy interface and speed need to get configured
975 * differently on enet-mac.
976 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100977 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100978 /* Enable flow control and length check */
979 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100980
Shawn Guo230dec62011-09-23 02:12:48 +0000981 /* RGMII, RMII or MII */
Markus Pargmanne813bb22015-04-30 17:07:50 +0200982 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
983 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
984 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
985 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shawn Guo230dec62011-09-23 02:12:48 +0000986 rcntl |= (1 << 6);
987 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100988 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100989 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100990 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100991
Shawn Guo230dec62011-09-23 02:12:48 +0000992 /* 1G, 100M or 10M */
Philippe Reynes45f5c322016-05-10 00:19:42 +0200993 if (ndev->phydev) {
994 if (ndev->phydev->speed == SPEED_1000)
Shawn Guo230dec62011-09-23 02:12:48 +0000995 ecntl |= (1 << 5);
Philippe Reynes45f5c322016-05-10 00:19:42 +0200996 else if (ndev->phydev->speed == SPEED_100)
Shawn Guo230dec62011-09-23 02:12:48 +0000997 rcntl &= ~(1 << 9);
998 else
999 rcntl |= (1 << 9);
1000 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001001 } else {
1002#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001003 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001004 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001005 /* disable the gasket and wait */
1006 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1007 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1008 udelay(1);
1009
1010 /*
1011 * configure the gasket:
1012 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001013 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001014 */
Eric Benard8d82f212012-01-12 06:10:28 +00001015 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1016 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
Philippe Reynes45f5c322016-05-10 00:19:42 +02001017 if (ndev->phydev && ndev->phydev->speed == SPEED_10)
Eric Benard8d82f212012-01-12 06:10:28 +00001018 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1019 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020
1021 /* re-enable the gasket */
1022 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1023 }
1024#endif
1025 }
Frank Libaa70a52013-01-16 16:55:58 +00001026
Guenter Roeckd1391932013-06-18 10:04:59 -07001027#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001028 /* enable pause frame*/
1029 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1030 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
Philippe Reynes45f5c322016-05-10 00:19:42 +02001031 ndev->phydev && ndev->phydev->pause)) {
Frank Libaa70a52013-01-16 16:55:58 +00001032 rcntl |= FEC_ENET_FCE;
1033
Jim Baxter4c09eed2013-04-19 08:10:49 +00001034 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001035 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1036 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1037 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1038 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1039
1040 /* OPD */
1041 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1042 } else {
1043 rcntl &= ~FEC_ENET_FCE;
1044 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001045#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001046
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001047 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001048
Stefan Wahren84fe6182014-03-12 11:28:19 +01001049 /* Setup multicast filter. */
1050 set_multicast_list(ndev);
1051#ifndef CONFIG_M5272
1052 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1053 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1054#endif
1055
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001056 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001057 /* enable ENET endian swap */
1058 ecntl |= (1 << 8);
1059 /* enable ENET store and forward mode */
1060 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1061 }
1062
Frank Liff43da82013-01-03 16:04:23 +00001063 if (fep->bufdesc_ex)
1064 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001065
Chris Healy38ae92d2013-06-25 23:18:52 -07001066#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001067 /* Enable the MIB statistic event counters */
1068 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001069#endif
1070
Uwe Kleine-König45993652011-01-19 20:47:04 +01001071 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001072 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001073 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001074
Frank Liff43da82013-01-03 16:04:23 +00001075 if (fep->bufdesc_ex)
1076 fec_ptp_start_cyclecounter(ndev);
1077
Uwe Kleine-König45993652011-01-19 20:47:04 +01001078 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001079 if (fep->link)
1080 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1081 else
1082 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001083
1084 /* Init the interrupt coalescing */
1085 fec_enet_itr_coal_init(ndev);
1086
Uwe Kleine-König45993652011-01-19 20:47:04 +01001087}
1088
1089static void
1090fec_stop(struct net_device *ndev)
1091{
1092 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001093 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001094 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001095 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001096
1097 /* We cannot expect a graceful transmit stop without link !!! */
1098 if (fep->link) {
1099 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1100 udelay(10);
1101 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001102 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001103 }
1104
Fugang Duan106c3142014-09-13 05:00:51 +08001105 /* Whack a reset. We should wait for this.
1106 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1107 * instead of reset MAC itself.
1108 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001109 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1110 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1111 writel(0, fep->hwp + FEC_ECNTRL);
1112 } else {
1113 writel(1, fep->hwp + FEC_ECNTRL);
1114 udelay(10);
1115 }
1116 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001117 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001118 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1119 val = readl(fep->hwp + FEC_ECNTRL);
1120 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1121 writel(val, fep->hwp + FEC_ECNTRL);
1122
1123 if (pdata && pdata->sleep_mode_enable)
1124 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001125 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001126 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001127
1128 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001129 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1130 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001131 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001132 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1133 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001134}
1135
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001138fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001140 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Russell King344756f2014-07-08 13:01:59 +01001142 fec_dump(ndev);
1143
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001144 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Russell King36cdc742014-07-08 13:01:44 +01001146 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001147}
1148
Russell King36cdc742014-07-08 13:01:44 +01001149static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001150{
1151 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001152 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001153 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001154
Russell King36cdc742014-07-08 13:01:44 +01001155 rtnl_lock();
1156 if (netif_device_present(ndev) || netif_running(ndev)) {
1157 napi_disable(&fep->napi);
1158 netif_tx_lock_bh(ndev);
1159 fec_restart(ndev);
1160 netif_wake_queue(ndev);
1161 netif_tx_unlock_bh(ndev);
1162 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001163 }
Russell King36cdc742014-07-08 13:01:44 +01001164 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001168fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1169 struct skb_shared_hwtstamps *hwtstamps)
1170{
1171 unsigned long flags;
1172 u64 ns;
1173
1174 spin_lock_irqsave(&fep->tmreg_lock, flags);
1175 ns = timecounter_cyc2time(&fep->tc, ts);
1176 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1177
1178 memset(hwtstamps, 0, sizeof(*hwtstamps));
1179 hwtstamps->hwtstamp = ns_to_ktime(ns);
1180}
1181
1182static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001183fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct fec_enet_private *fep;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001186 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001187 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001189 struct fec_enet_priv_tx_q *txq;
1190 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001191 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001192 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001194 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001195
1196 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1197
1198 txq = fep->tx_queue[queue_id];
1199 /* get next bdp of dirty_tx */
1200 nq = netdev_get_tx_queue(ndev, queue_id);
1201 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Frank Lide5fb0a2013-03-03 17:34:25 +00001203 /* get next bdp of dirty_tx */
Troy Kisky7355f272016-02-05 14:52:46 -07001204 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Lide5fb0a2013-03-03 17:34:25 +00001205
Troy Kisky7355f272016-02-05 14:52:46 -07001206 while (bdp != READ_ONCE(txq->bd.cur)) {
1207 /* Order the load of bd.cur and cbd_sc */
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001208 rmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +01001209 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001210 if (status & BD_ENET_TX_READY)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001211 break;
1212
Troy Kisky7355f272016-02-05 14:52:46 -07001213 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy2b995f62015-01-20 14:10:35 +08001214
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001215 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001216 txq->tx_skbuff[index] = NULL;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001217 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1218 dma_unmap_single(&fep->pdev->dev,
1219 fec32_to_cpu(bdp->cbd_bufaddr),
1220 fec16_to_cpu(bdp->cbd_datlen),
1221 DMA_TO_DEVICE);
1222 bdp->cbd_bufaddr = cpu_to_fec32(0);
Troy Kisky7fafe802016-05-27 13:30:40 -07001223 if (!skb)
1224 goto skb_done;
Frank Lide5fb0a2013-03-03 17:34:25 +00001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001227 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 BD_ENET_TX_RL | BD_ENET_TX_UN |
1229 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001230 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001231 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001232 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001233 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001234 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001235 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001236 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001237 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001238 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001239 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001240 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001242 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001243 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
Frank Liff43da82013-01-03 16:04:23 +00001246 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1247 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001248 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001249 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001250
Johannes Berg5cfa3032016-01-24 16:52:37 +01001251 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001252 skb_tstamp_tx(skb, &shhwtstamps);
1253 }
Frank Liff43da82013-01-03 16:04:23 +00001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* Deferred means some collisions occurred during transmit,
1256 * but we eventually sent the packet OK.
1257 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001258 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001259 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001260
Sascha Hauer22f6b862009-04-15 01:32:18 +00001261 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dev_kfree_skb_any(skb);
Troy Kisky7fafe802016-05-27 13:30:40 -07001263skb_done:
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001264 /* Make sure the update to bdp and tx_skbuff are performed
1265 * before dirty_tx
1266 */
1267 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +08001268 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001269
Sascha Hauer22f6b862009-04-15 01:32:18 +00001270 /* Update pointer to next buffer descriptor to be transmitted */
Troy Kisky7355f272016-02-05 14:52:46 -07001271 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001272
Sascha Hauer22f6b862009-04-15 01:32:18 +00001273 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001275 if (netif_queue_stopped(ndev)) {
Troy Kisky7355f272016-02-05 14:52:46 -07001276 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001277 if (entries_free >= txq->tx_wake_threshold)
1278 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Russell Kingccea2962014-07-08 13:01:38 +01001281
Fugang Duanc10bc0e2017-04-11 19:13:07 +08001282 /* ERR006358: Keep the transmitter going */
Troy Kisky7355f272016-02-05 14:52:46 -07001283 if (bdp != txq->bd.cur &&
Troy Kisky53bb20d2016-02-05 14:52:47 -07001284 readl(txq->bd.reg_desc_active) == 0)
1285 writel(0, txq->bd.reg_desc_active);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001286}
1287
1288static void
1289fec_enet_tx(struct net_device *ndev)
1290{
1291 struct fec_enet_private *fep = netdev_priv(ndev);
1292 u16 queue_id;
1293 /* First process class A queue, then Class B and Best Effort queue */
1294 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1295 clear_bit(queue_id, &fep->work_tx);
1296 fec_enet_tx_queue(ndev, queue_id);
1297 }
1298 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001301static int
1302fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1303{
1304 struct fec_enet_private *fep = netdev_priv(ndev);
1305 int off;
1306
1307 off = ((unsigned long)skb->data) & fep->rx_align;
1308 if (off)
1309 skb_reserve(skb, fep->rx_align + 1 - off);
1310
Johannes Berg5cfa3032016-01-24 16:52:37 +01001311 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1312 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001313 if (net_ratelimit())
1314 netdev_err(ndev, "Rx DMA memory map failed\n");
1315 return -ENOMEM;
1316 }
1317
1318 return 0;
1319}
1320
1321static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001322 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001323{
1324 struct fec_enet_private *fep = netdev_priv(ndev);
1325 struct sk_buff *new_skb;
1326
1327 if (length > fep->rx_copybreak)
1328 return false;
1329
1330 new_skb = netdev_alloc_skb(ndev, length);
1331 if (!new_skb)
1332 return false;
1333
Johannes Berg5cfa3032016-01-24 16:52:37 +01001334 dma_sync_single_for_cpu(&fep->pdev->dev,
1335 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001336 FEC_ENET_RX_FRSIZE - fep->rx_align,
1337 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001338 if (!swap)
1339 memcpy(new_skb->data, (*skb)->data, length);
1340 else
1341 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001342 *skb = new_skb;
1343
1344 return true;
1345}
1346
Troy Kisky7355f272016-02-05 14:52:46 -07001347/* During a receive, the bd_rx.cur points to the current incoming buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * When we update through the ring, if the next incoming buffer has
1349 * not been given to the system, we just set the empty indicator,
1350 * effectively tossing the packet.
1351 */
Frank Lidc975382013-01-28 18:31:42 +00001352static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001353fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001355 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001356 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001357 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001358 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001359 struct sk_buff *skb_new = NULL;
1360 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 ushort pkt_len;
1362 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001363 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001364 struct bufdesc_ex *ebdp = NULL;
1365 bool vlan_packet_rcvd = false;
1366 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001367 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001368 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001369 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001370
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001371#ifdef CONFIG_M532x
1372 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001373#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001374 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1375 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* First, grab all of the stats for the incoming packet.
1378 * These get messed up if we get called due to a busy condition.
1379 */
Troy Kisky7355f272016-02-05 14:52:46 -07001380 bdp = rxq->bd.cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Johannes Berg5cfa3032016-01-24 16:52:37 +01001382 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Frank Lidc975382013-01-28 18:31:42 +00001384 if (pkt_received >= budget)
1385 break;
1386 pkt_received++;
1387
Russell Kinged63f1d2015-09-02 17:24:14 +08001388 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
Russell Kingdb3421c2014-07-08 13:01:49 +01001389
Sascha Hauer22f6b862009-04-15 01:32:18 +00001390 /* Check for errors. */
Troy Kisky095098e2016-02-05 14:52:44 -07001391 status ^= BD_ENET_RX_LAST;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001392 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Troy Kisky095098e2016-02-05 14:52:44 -07001393 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1394 BD_ENET_RX_CL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001395 ndev->stats.rx_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001396 if (status & BD_ENET_RX_OV) {
1397 /* FIFO overrun */
1398 ndev->stats.rx_fifo_errors++;
1399 goto rx_processing_done;
1400 }
1401 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1402 | BD_ENET_RX_LAST)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +00001403 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001404 ndev->stats.rx_length_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001405 if (status & BD_ENET_RX_LAST)
1406 netdev_err(ndev, "rcv is not +last\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001407 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001408 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001409 ndev->stats.rx_crc_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001410 /* Report late collisions as a frame error. */
1411 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1412 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001413 goto rx_processing_done;
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Sascha Hauer22f6b862009-04-15 01:32:18 +00001416 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001417 ndev->stats.rx_packets++;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001418 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001419 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Troy Kisky7355f272016-02-05 14:52:46 -07001421 index = fec_enet_get_bd_index(bdp, &rxq->bd);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001422 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001423
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001424 /* The packet length includes FCS, but we don't want to
1425 * include that when passing upstream as it messes up
1426 * bridging applications.
1427 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001428 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1429 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001430 if (!is_copybreak) {
1431 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1432 if (unlikely(!skb_new)) {
1433 ndev->stats.rx_dropped++;
1434 goto rx_processing_done;
1435 }
Johannes Berg5cfa3032016-01-24 16:52:37 +01001436 dma_unmap_single(&fep->pdev->dev,
1437 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001438 FEC_ENET_RX_FRSIZE - fep->rx_align,
1439 DMA_FROM_DEVICE);
1440 }
1441
1442 prefetch(skb->data - NET_IP_ALIGN);
1443 skb_put(skb, pkt_len - 4);
1444 data = skb->data;
Eric Nelson3ac72b72016-09-24 07:42:19 -07001445
Fabio Estevam235bde12016-10-21 09:34:29 -02001446 if (!is_copybreak && need_swap)
1447 swap_buffer(data, pkt_len);
1448
Eric Nelson3ac72b72016-09-24 07:42:19 -07001449#if !defined(CONFIG_M5272)
1450 if (fep->quirks & FEC_QUIRK_HAS_RACC)
1451 data = skb_pull_inline(skb, 2);
1452#endif
1453
Jim Baxtercdffcf12013-07-02 22:52:56 +01001454 /* Extract the enhanced buffer descriptor */
1455 ebdp = NULL;
1456 if (fep->bufdesc_ex)
1457 ebdp = (struct bufdesc_ex *)bdp;
1458
1459 /* If this is a VLAN packet remove the VLAN Tag */
1460 vlan_packet_rcvd = false;
1461 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Johannes Berg5cfa3032016-01-24 16:52:37 +01001462 fep->bufdesc_ex &&
1463 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001464 /* Push and remove the vlan tag */
1465 struct vlan_hdr *vlan_header =
1466 (struct vlan_hdr *) (data + ETH_HLEN);
1467 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001468
1469 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001470
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001471 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001472 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001473 }
1474
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001475 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001477 /* Get receive timestamp from the skb */
1478 if (fep->hwts_rx_en && fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +01001479 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001480 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001481
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001482 if (fep->bufdesc_ex &&
1483 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001484 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001485 /* don't check it */
1486 skb->ip_summed = CHECKSUM_UNNECESSARY;
1487 } else {
1488 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001489 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001490 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001491
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001492 /* Handle received VLAN packets */
1493 if (vlan_packet_rcvd)
1494 __vlan_hwaccel_put_tag(skb,
1495 htons(ETH_P_8021Q),
1496 vlan_tag);
1497
1498 napi_gro_receive(&fep->napi, skb);
1499
1500 if (is_copybreak) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001501 dma_sync_single_for_device(&fep->pdev->dev,
1502 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001503 FEC_ENET_RX_FRSIZE - fep->rx_align,
1504 DMA_FROM_DEVICE);
1505 } else {
1506 rxq->rx_skbuff[index] = skb_new;
1507 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1508 }
1509
Sascha Hauer22f6b862009-04-15 01:32:18 +00001510rx_processing_done:
1511 /* Clear the status flags for this buffer */
1512 status &= ~BD_ENET_RX_STATS;
1513
1514 /* Mark the buffer empty */
1515 status |= BD_ENET_RX_EMPTY;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001516
Frank Liff43da82013-01-03 16:04:23 +00001517 if (fep->bufdesc_ex) {
1518 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1519
Johannes Berg5cfa3032016-01-24 16:52:37 +01001520 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00001521 ebdp->cbd_prot = 0;
1522 ebdp->cbd_bdu = 0;
1523 }
Troy Kiskybe293462016-02-05 14:52:49 -07001524 /* Make sure the updates to rest of the descriptor are
1525 * performed before transferring ownership.
1526 */
1527 wmb();
1528 bdp->cbd_sc = cpu_to_fec16(status);
Frank Li6605b732012-10-30 18:25:31 +00001529
Sascha Hauer22f6b862009-04-15 01:32:18 +00001530 /* Update BD pointer to next entry */
Troy Kisky7355f272016-02-05 14:52:46 -07001531 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001532
Sascha Hauer22f6b862009-04-15 01:32:18 +00001533 /* Doing this here will keep the FEC running while we process
1534 * incoming frames. On a heavily loaded network, we should be
1535 * able to keep up at the expense of system resources.
1536 */
Troy Kisky53bb20d2016-02-05 14:52:47 -07001537 writel(0, rxq->bd.reg_desc_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Troy Kisky7355f272016-02-05 14:52:46 -07001539 rxq->bd.cur = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001540 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Fugang Duan4d494cd2014-09-13 05:00:48 +08001543static int
1544fec_enet_rx(struct net_device *ndev, int budget)
1545{
1546 int pkt_received = 0;
1547 u16 queue_id;
1548 struct fec_enet_private *fep = netdev_priv(ndev);
1549
1550 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
Uwe Kleine-König1c021bb2016-05-03 16:38:53 +02001551 int ret;
1552
1553 ret = fec_enet_rx_queue(ndev,
Fugang Duan4d494cd2014-09-13 05:00:48 +08001554 budget - pkt_received, queue_id);
Uwe Kleine-König1c021bb2016-05-03 16:38:53 +02001555
1556 if (ret < budget - pkt_received)
1557 clear_bit(queue_id, &fep->work_rx);
1558
1559 pkt_received += ret;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001560 }
1561 return pkt_received;
1562}
1563
1564static bool
1565fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1566{
1567 if (int_events == 0)
1568 return false;
1569
Troy Kisky5e62d982017-09-19 17:33:07 -07001570 if (int_events & FEC_ENET_RXF_0)
Fugang Duan4d494cd2014-09-13 05:00:48 +08001571 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001572 if (int_events & FEC_ENET_RXF_1)
1573 fep->work_rx |= (1 << 0);
1574 if (int_events & FEC_ENET_RXF_2)
1575 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001576
Troy Kisky5e62d982017-09-19 17:33:07 -07001577 if (int_events & FEC_ENET_TXF_0)
Fugang Duan4d494cd2014-09-13 05:00:48 +08001578 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001579 if (int_events & FEC_ENET_TXF_1)
1580 fep->work_tx |= (1 << 0);
1581 if (int_events & FEC_ENET_TXF_2)
1582 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001583
1584 return true;
1585}
1586
Uwe Kleine-König45993652011-01-19 20:47:04 +01001587static irqreturn_t
1588fec_enet_interrupt(int irq, void *dev_id)
1589{
1590 struct net_device *ndev = dev_id;
1591 struct fec_enet_private *fep = netdev_priv(ndev);
1592 uint int_events;
1593 irqreturn_t ret = IRQ_NONE;
1594
Russell King7a168072014-07-08 00:22:44 +01001595 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001596 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001597 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001598
Fugang Duan61615cd2015-03-04 07:52:11 +08001599 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001600 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001601
Nimrod Andy94191fd2014-12-16 18:25:58 +08001602 if (napi_schedule_prep(&fep->napi)) {
1603 /* Disable the NAPI interrupts */
Troy Kisky80dc6a92016-02-05 14:52:48 -07001604 writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001605 __napi_schedule(&fep->napi);
1606 }
Russell King7a168072014-07-08 00:22:44 +01001607 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001608
Russell King7a168072014-07-08 00:22:44 +01001609 if (int_events & FEC_ENET_MII) {
1610 ret = IRQ_HANDLED;
1611 complete(&fep->mdio_done);
1612 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001613 return ret;
1614}
1615
Frank Lidc975382013-01-28 18:31:42 +00001616static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1617{
1618 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001619 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001620 int pkts;
1621
Russell King7a168072014-07-08 00:22:44 +01001622 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001623
Frank Lide5fb0a2013-03-03 17:34:25 +00001624 fec_enet_tx(ndev);
1625
Frank Lidc975382013-01-28 18:31:42 +00001626 if (pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001627 napi_complete_done(napi, pkts);
Frank Lidc975382013-01-28 18:31:42 +00001628 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1629 }
1630 return pkts;
1631}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001634static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001636 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001637 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001638 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Shawn Guo49da97d2011-01-05 21:13:11 +00001640 /*
1641 * try to get mac address in following order:
1642 *
1643 * 1) module parameter via kernel command line in form
1644 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1645 */
1646 iap = macaddr;
1647
1648 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001649 * 2) from device tree data
1650 */
1651 if (!is_valid_ether_addr(iap)) {
1652 struct device_node *np = fep->pdev->dev.of_node;
1653 if (np) {
1654 const char *mac = of_get_mac_address(np);
1655 if (mac)
1656 iap = (unsigned char *) mac;
1657 }
1658 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001659
1660 /*
1661 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001662 */
1663 if (!is_valid_ether_addr(iap)) {
1664#ifdef CONFIG_M5272
1665 if (FEC_FLASHMAC)
1666 iap = (unsigned char *)FEC_FLASHMAC;
1667#else
1668 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001669 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001670#endif
1671 }
1672
1673 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001674 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001675 */
1676 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001677 *((__be32 *) &tmpaddr[0]) =
1678 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1679 *((__be16 *) &tmpaddr[4]) =
1680 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 iap = &tmpaddr[0];
1682 }
1683
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001684 /*
1685 * 5) random mac address
1686 */
1687 if (!is_valid_ether_addr(iap)) {
1688 /* Report it and use a random ethernet address instead */
1689 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1690 eth_hw_addr_random(ndev);
1691 netdev_info(ndev, "Using random MAC address: %pM\n",
1692 ndev->dev_addr);
1693 return;
1694 }
1695
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001696 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Shawn Guo49da97d2011-01-05 21:13:11 +00001698 /* Adjust MAC if using macaddr */
1699 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001700 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703/* ------------------------------------------------------------------------- */
1704
Bryan Wue6b043d2010-03-31 02:10:44 +00001705/*
1706 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001708static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001710 struct fec_enet_private *fep = netdev_priv(ndev);
Philippe Reynes45f5c322016-05-10 00:19:42 +02001711 struct phy_device *phy_dev = ndev->phydev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001712 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Bryan Wue6b043d2010-03-31 02:10:44 +00001714 /* Prevent a state halted on mii error */
1715 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1716 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001719
Russell King8ce56242014-07-08 12:40:07 +01001720 /*
1721 * If the netdev is down, or is going down, we're not interested
1722 * in link state events, so just mark our idea of the link as down
1723 * and ignore the event.
1724 */
1725 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1726 fep->link = 0;
1727 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001728 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001729 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001730 status_change = 1;
1731 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001732
Russell Kingef833372014-07-08 12:40:43 +01001733 if (fep->full_duplex != phy_dev->duplex) {
1734 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001735 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001736 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001737
1738 if (phy_dev->speed != fep->speed) {
1739 fep->speed = phy_dev->speed;
1740 status_change = 1;
1741 }
1742
1743 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001744 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001745 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001746 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001747 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001748 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001749 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001750 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001751 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001752 } else {
1753 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001754 napi_disable(&fep->napi);
1755 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001756 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001757 netif_tx_unlock_bh(ndev);
1758 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001759 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001760 status_change = 1;
1761 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001762 }
1763
Bryan Wue6b043d2010-03-31 02:10:44 +00001764 if (status_change)
1765 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Bryan Wue6b043d2010-03-31 02:10:44 +00001768static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Bryan Wue6b043d2010-03-31 02:10:44 +00001770 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001771 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001772 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001773 int ret = 0;
1774
1775 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001776 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001777 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001778
1779 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001780 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001781
1782 /* start a read op */
1783 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1784 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1785 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1786
1787 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001788 time_left = wait_for_completion_timeout(&fep->mdio_done,
1789 usecs_to_jiffies(FEC_MII_TIMEOUT));
1790 if (time_left == 0) {
1791 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001792 netdev_err(fep->netdev, "MDIO read timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001793 ret = -ETIMEDOUT;
1794 goto out;
Bryan Wue6b043d2010-03-31 02:10:44 +00001795 }
1796
Andrew Lunn8fff7552015-07-25 22:38:02 +02001797 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1798
1799out:
1800 pm_runtime_mark_last_busy(dev);
1801 pm_runtime_put_autosuspend(dev);
1802
1803 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001804}
1805
1806static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1807 u16 value)
1808{
1809 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001810 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001811 unsigned long time_left;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001812 int ret;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001813
1814 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001815 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001816 return ret;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001817 else
1818 ret = 0;
Bryan Wue6b043d2010-03-31 02:10:44 +00001819
1820 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001821 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001822
Shawn Guo862f0982011-01-05 21:13:09 +00001823 /* start a write op */
1824 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001825 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1826 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1827 fep->hwp + FEC_MII_DATA);
1828
1829 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001830 time_left = wait_for_completion_timeout(&fep->mdio_done,
1831 usecs_to_jiffies(FEC_MII_TIMEOUT));
1832 if (time_left == 0) {
1833 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001834 netdev_err(fep->netdev, "MDIO write timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001835 ret = -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001836 }
1837
Andrew Lunn8fff7552015-07-25 22:38:02 +02001838 pm_runtime_mark_last_busy(dev);
1839 pm_runtime_put_autosuspend(dev);
1840
1841 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001842}
1843
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001844static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1845{
1846 struct fec_enet_private *fep = netdev_priv(ndev);
1847 int ret;
1848
1849 if (enable) {
1850 ret = clk_prepare_enable(fep->clk_ahb);
1851 if (ret)
1852 return ret;
Uwe Kleine-König01e59432016-10-20 10:28:27 +02001853
1854 ret = clk_prepare_enable(fep->clk_enet_out);
1855 if (ret)
1856 goto failed_clk_enet_out;
1857
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001858 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001859 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001860 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001861 if (ret) {
1862 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001863 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001864 } else {
1865 fep->ptp_clk_on = true;
1866 }
1867 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001868 }
Uwe Kleine-König01e59432016-10-20 10:28:27 +02001869
1870 ret = clk_prepare_enable(fep->clk_ref);
1871 if (ret)
1872 goto failed_clk_ref;
Richard Leitner1b0a83a2017-12-11 13:17:00 +01001873
1874 phy_reset_after_clk_enable(ndev->phydev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001875 } else {
1876 clk_disable_unprepare(fep->clk_ahb);
Uwe Kleine-König01e59432016-10-20 10:28:27 +02001877 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001878 if (fep->clk_ptp) {
1879 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001880 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001881 fep->ptp_clk_on = false;
1882 mutex_unlock(&fep->ptp_clk_mutex);
1883 }
Uwe Kleine-König01e59432016-10-20 10:28:27 +02001884 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001885 }
1886
1887 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001888
1889failed_clk_ref:
1890 if (fep->clk_ref)
1891 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001892failed_clk_ptp:
1893 if (fep->clk_enet_out)
1894 clk_disable_unprepare(fep->clk_enet_out);
1895failed_clk_enet_out:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001896 clk_disable_unprepare(fep->clk_ahb);
1897
1898 return ret;
1899}
1900
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001901static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001902{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001903 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001904 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001905 char mdio_bus_id[MII_BUS_ID_SIZE];
1906 char phy_name[MII_BUS_ID_SIZE + 3];
1907 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001908 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001909
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001910 if (fep->phy_node) {
1911 phy_dev = of_phy_connect(ndev, fep->phy_node,
1912 &fec_enet_adjust_link, 0,
1913 fep->phy_interface);
Andrew Lunn9558df32017-07-30 22:11:06 +02001914 if (!phy_dev) {
1915 netdev_err(ndev, "Unable to connect to phy\n");
Nimrod Andy213a9922014-12-09 18:46:56 +08001916 return -ENODEV;
Andrew Lunn9558df32017-07-30 22:11:06 +02001917 }
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001918 } else {
1919 /* check for attached phy */
1920 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
Andrew Lunn7f854422016-01-06 20:11:18 +01001921 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001922 continue;
1923 if (dev_id--)
1924 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001925 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001926 break;
1927 }
1928
1929 if (phy_id >= PHY_MAX_ADDR) {
1930 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001931 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001932 phy_id = 0;
1933 }
1934
1935 snprintf(phy_name, sizeof(phy_name),
1936 PHY_ID_FMT, mdio_bus_id, phy_id);
1937 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1938 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001939 }
1940
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001941 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001942 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001943 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001944 }
1945
1946 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001947 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001948 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001949 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001950#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001951 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001952#endif
Frank Libaa70a52013-01-16 16:55:58 +00001953 }
Shawn Guo230dec62011-09-23 02:12:48 +00001954 else
1955 phy_dev->supported &= PHY_BASIC_FEATURES;
1956
Bryan Wue6b043d2010-03-31 02:10:44 +00001957 phy_dev->advertising = phy_dev->supported;
1958
Bryan Wue6b043d2010-03-31 02:10:44 +00001959 fep->link = 0;
1960 fep->full_duplex = 0;
1961
Andrew Lunn22209432016-01-06 20:11:13 +01001962 phy_attached_info(phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001963
Bryan Wue6b043d2010-03-31 02:10:44 +00001964 return 0;
1965}
1966
1967static int fec_enet_mii_init(struct platform_device *pdev)
1968{
Shawn Guob5680e02011-01-05 21:13:13 +00001969 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001970 struct net_device *ndev = platform_get_drvdata(pdev);
1971 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001972 struct device_node *node;
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001973 int err = -ENXIO;
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001974 u32 mii_speed, holdtime;
Bryan Wue6b043d2010-03-31 02:10:44 +00001975
Shawn Guob5680e02011-01-05 21:13:13 +00001976 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001977 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001978 * Here are the differences:
1979 *
1980 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1981 * - fec0 acts as the 1588 time master while fec1 is slave
1982 * - external phys can only be configured by fec0
1983 *
1984 * That is to say fec1 can not work independently. It only works
1985 * when fec0 is working. The reason behind this design is that the
1986 * second interface is added primarily for Switch mode.
1987 *
1988 * Because of the last point above, both phys are attached on fec0
1989 * mdio interface in board design, and need to be configured by
1990 * fec0 mii_bus.
1991 */
Stefan Agner3d125f92015-01-14 00:20:21 +01001992 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001993 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001994 if (mii_cnt && fec0_mii_bus) {
1995 fep->mii_bus = fec0_mii_bus;
1996 mii_cnt++;
1997 return 0;
1998 }
1999 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00002000 }
2001
Bryan Wue6b043d2010-03-31 02:10:44 +00002002 fep->mii_timeout = 0;
2003
2004 /*
2005 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002006 *
2007 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2008 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2009 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2010 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002011 */
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002012 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002013 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002014 mii_speed--;
2015 if (mii_speed > 63) {
2016 dev_err(&pdev->dev,
Peter Meerwald-Stadler981a0542016-12-16 14:23:36 +01002017 "fec clock (%lu) too fast to get right mii speed\n",
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002018 clk_get_rate(fep->clk_ipg));
2019 err = -EINVAL;
2020 goto err_out;
2021 }
2022
2023 /*
2024 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2025 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2026 * versions are RAZ there, so just ignore the difference and write the
2027 * register always.
2028 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2029 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2030 * output.
2031 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2032 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2033 * holdtime cannot result in a value greater than 3.
2034 */
2035 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2036
2037 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2038
Bryan Wue6b043d2010-03-31 02:10:44 +00002039 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2040
2041 fep->mii_bus = mdiobus_alloc();
2042 if (fep->mii_bus == NULL) {
2043 err = -ENOMEM;
2044 goto err_out;
2045 }
2046
2047 fep->mii_bus->name = "fec_enet_mii_bus";
2048 fep->mii_bus->read = fec_enet_mdio_read;
2049 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002050 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2051 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002052 fep->mii_bus->priv = fep;
2053 fep->mii_bus->parent = &pdev->dev;
2054
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002055 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2056 if (node) {
2057 err = of_mdiobus_register(fep->mii_bus, node);
2058 of_node_put(node);
2059 } else {
2060 err = mdiobus_register(fep->mii_bus);
2061 }
2062
2063 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002064 goto err_out_free_mdiobus;
Bryan Wue6b043d2010-03-31 02:10:44 +00002065
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002066 mii_cnt++;
2067
Shawn Guob5680e02011-01-05 21:13:13 +00002068 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002069 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002070 fec0_mii_bus = fep->mii_bus;
2071
Bryan Wue6b043d2010-03-31 02:10:44 +00002072 return 0;
2073
Bryan Wue6b043d2010-03-31 02:10:44 +00002074err_out_free_mdiobus:
2075 mdiobus_free(fep->mii_bus);
2076err_out:
2077 return err;
2078}
2079
2080static void fec_enet_mii_remove(struct fec_enet_private *fep)
2081{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002082 if (--mii_cnt == 0) {
2083 mdiobus_unregister(fep->mii_bus);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002084 mdiobus_free(fep->mii_bus);
2085 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002086}
2087
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002088static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002089 struct ethtool_drvinfo *info)
2090{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002091 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Jiri Pirko7826d432013-01-06 00:44:26 +00002093 strlcpy(info->driver, fep->pdev->dev.driver->name,
2094 sizeof(info->driver));
2095 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2096 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
Bryan Wue6b043d2010-03-31 02:10:44 +00002098
Philippe Reynesdb65f352015-05-11 00:01:42 +02002099static int fec_enet_get_regs_len(struct net_device *ndev)
2100{
2101 struct fec_enet_private *fep = netdev_priv(ndev);
2102 struct resource *r;
2103 int s = 0;
2104
2105 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2106 if (r)
2107 s = resource_size(r);
2108
2109 return s;
2110}
2111
2112/* List of registers that can be safety be read to dump them with ethtool */
2113#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Lucas Stach3f1dcc62018-01-17 19:30:55 +01002114 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2115 defined(CONFIG_ARM64)
Philippe Reynesdb65f352015-05-11 00:01:42 +02002116static u32 fec_enet_register_offset[] = {
2117 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2118 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2119 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2120 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2121 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2122 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2123 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2124 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2125 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2126 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2127 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2128 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2129 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2130 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2131 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2132 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2133 RMON_T_P_GTE2048, RMON_T_OCTETS,
2134 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2135 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2136 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2137 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2138 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2139 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2140 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2141 RMON_R_P_GTE2048, RMON_R_OCTETS,
2142 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2143 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2144};
2145#else
2146static u32 fec_enet_register_offset[] = {
2147 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2148 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2149 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2150 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2151 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2152 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2153 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2154 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2155 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2156};
2157#endif
2158
2159static void fec_enet_get_regs(struct net_device *ndev,
2160 struct ethtool_regs *regs, void *regbuf)
2161{
2162 struct fec_enet_private *fep = netdev_priv(ndev);
2163 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2164 u32 *buf = (u32 *)regbuf;
2165 u32 i, off;
2166
2167 memset(buf, 0, regs->len);
2168
2169 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2170 off = fec_enet_register_offset[i] / 4;
2171 buf[off] = readl(&theregs[off]);
2172 }
2173}
2174
Frank Li5ebae4892013-01-06 16:25:07 +00002175static int fec_enet_get_ts_info(struct net_device *ndev,
2176 struct ethtool_ts_info *info)
2177{
2178 struct fec_enet_private *fep = netdev_priv(ndev);
2179
2180 if (fep->bufdesc_ex) {
2181
2182 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2183 SOF_TIMESTAMPING_RX_SOFTWARE |
2184 SOF_TIMESTAMPING_SOFTWARE |
2185 SOF_TIMESTAMPING_TX_HARDWARE |
2186 SOF_TIMESTAMPING_RX_HARDWARE |
2187 SOF_TIMESTAMPING_RAW_HARDWARE;
2188 if (fep->ptp_clock)
2189 info->phc_index = ptp_clock_index(fep->ptp_clock);
2190 else
2191 info->phc_index = -1;
2192
2193 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2194 (1 << HWTSTAMP_TX_ON);
2195
2196 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2197 (1 << HWTSTAMP_FILTER_ALL);
2198 return 0;
2199 } else {
2200 return ethtool_op_get_ts_info(ndev, info);
2201 }
2202}
2203
Guenter Roeckd1391932013-06-18 10:04:59 -07002204#if !defined(CONFIG_M5272)
2205
Frank Libaa70a52013-01-16 16:55:58 +00002206static void fec_enet_get_pauseparam(struct net_device *ndev,
2207 struct ethtool_pauseparam *pause)
2208{
2209 struct fec_enet_private *fep = netdev_priv(ndev);
2210
2211 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2212 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2213 pause->rx_pause = pause->tx_pause;
2214}
2215
2216static int fec_enet_set_pauseparam(struct net_device *ndev,
2217 struct ethtool_pauseparam *pause)
2218{
2219 struct fec_enet_private *fep = netdev_priv(ndev);
2220
Philippe Reynes45f5c322016-05-10 00:19:42 +02002221 if (!ndev->phydev)
Russell King0b146ca2014-07-08 00:22:59 +01002222 return -ENODEV;
2223
Frank Libaa70a52013-01-16 16:55:58 +00002224 if (pause->tx_pause != pause->rx_pause) {
2225 netdev_info(ndev,
2226 "hardware only support enable/disable both tx and rx");
2227 return -EINVAL;
2228 }
2229
2230 fep->pause_flag = 0;
2231
2232 /* tx pause must be same as rx pause */
2233 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2234 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2235
2236 if (pause->rx_pause || pause->autoneg) {
Philippe Reynes45f5c322016-05-10 00:19:42 +02002237 ndev->phydev->supported |= ADVERTISED_Pause;
2238 ndev->phydev->advertising |= ADVERTISED_Pause;
Frank Libaa70a52013-01-16 16:55:58 +00002239 } else {
Philippe Reynes45f5c322016-05-10 00:19:42 +02002240 ndev->phydev->supported &= ~ADVERTISED_Pause;
2241 ndev->phydev->advertising &= ~ADVERTISED_Pause;
Frank Libaa70a52013-01-16 16:55:58 +00002242 }
2243
2244 if (pause->autoneg) {
2245 if (netif_running(ndev))
2246 fec_stop(ndev);
Philippe Reynes45f5c322016-05-10 00:19:42 +02002247 phy_start_aneg(ndev->phydev);
Frank Libaa70a52013-01-16 16:55:58 +00002248 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002249 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002250 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002251 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002252 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002253 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002254 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002255 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002256 }
Frank Libaa70a52013-01-16 16:55:58 +00002257
2258 return 0;
2259}
2260
Chris Healy38ae92d2013-06-25 23:18:52 -07002261static const struct fec_stat {
2262 char name[ETH_GSTRING_LEN];
2263 u16 offset;
2264} fec_stats[] = {
2265 /* RMON TX */
2266 { "tx_dropped", RMON_T_DROP },
2267 { "tx_packets", RMON_T_PACKETS },
2268 { "tx_broadcast", RMON_T_BC_PKT },
2269 { "tx_multicast", RMON_T_MC_PKT },
2270 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2271 { "tx_undersize", RMON_T_UNDERSIZE },
2272 { "tx_oversize", RMON_T_OVERSIZE },
2273 { "tx_fragment", RMON_T_FRAG },
2274 { "tx_jabber", RMON_T_JAB },
2275 { "tx_collision", RMON_T_COL },
2276 { "tx_64byte", RMON_T_P64 },
2277 { "tx_65to127byte", RMON_T_P65TO127 },
2278 { "tx_128to255byte", RMON_T_P128TO255 },
2279 { "tx_256to511byte", RMON_T_P256TO511 },
2280 { "tx_512to1023byte", RMON_T_P512TO1023 },
2281 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2282 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2283 { "tx_octets", RMON_T_OCTETS },
2284
2285 /* IEEE TX */
2286 { "IEEE_tx_drop", IEEE_T_DROP },
2287 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2288 { "IEEE_tx_1col", IEEE_T_1COL },
2289 { "IEEE_tx_mcol", IEEE_T_MCOL },
2290 { "IEEE_tx_def", IEEE_T_DEF },
2291 { "IEEE_tx_lcol", IEEE_T_LCOL },
2292 { "IEEE_tx_excol", IEEE_T_EXCOL },
2293 { "IEEE_tx_macerr", IEEE_T_MACERR },
2294 { "IEEE_tx_cserr", IEEE_T_CSERR },
2295 { "IEEE_tx_sqe", IEEE_T_SQE },
2296 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2297 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2298
2299 /* RMON RX */
2300 { "rx_packets", RMON_R_PACKETS },
2301 { "rx_broadcast", RMON_R_BC_PKT },
2302 { "rx_multicast", RMON_R_MC_PKT },
2303 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2304 { "rx_undersize", RMON_R_UNDERSIZE },
2305 { "rx_oversize", RMON_R_OVERSIZE },
2306 { "rx_fragment", RMON_R_FRAG },
2307 { "rx_jabber", RMON_R_JAB },
2308 { "rx_64byte", RMON_R_P64 },
2309 { "rx_65to127byte", RMON_R_P65TO127 },
2310 { "rx_128to255byte", RMON_R_P128TO255 },
2311 { "rx_256to511byte", RMON_R_P256TO511 },
2312 { "rx_512to1023byte", RMON_R_P512TO1023 },
2313 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2314 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2315 { "rx_octets", RMON_R_OCTETS },
2316
2317 /* IEEE RX */
2318 { "IEEE_rx_drop", IEEE_R_DROP },
2319 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2320 { "IEEE_rx_crc", IEEE_R_CRC },
2321 { "IEEE_rx_align", IEEE_R_ALIGN },
2322 { "IEEE_rx_macerr", IEEE_R_MACERR },
2323 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2324 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2325};
2326
Nikita Yushchenkof85de662016-12-06 09:26:53 +03002327#define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
2328
Nikita Yushchenko80cca772016-11-29 09:44:51 +03002329static void fec_enet_update_ethtool_stats(struct net_device *dev)
Chris Healy38ae92d2013-06-25 23:18:52 -07002330{
2331 struct fec_enet_private *fep = netdev_priv(dev);
2332 int i;
2333
2334 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
Nikita Yushchenko80cca772016-11-29 09:44:51 +03002335 fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
2336}
2337
2338static void fec_enet_get_ethtool_stats(struct net_device *dev,
2339 struct ethtool_stats *stats, u64 *data)
2340{
2341 struct fec_enet_private *fep = netdev_priv(dev);
2342
2343 if (netif_running(dev))
2344 fec_enet_update_ethtool_stats(dev);
2345
Nikita Yushchenkof85de662016-12-06 09:26:53 +03002346 memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
Chris Healy38ae92d2013-06-25 23:18:52 -07002347}
2348
2349static void fec_enet_get_strings(struct net_device *netdev,
2350 u32 stringset, u8 *data)
2351{
2352 int i;
2353 switch (stringset) {
2354 case ETH_SS_STATS:
2355 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2356 memcpy(data + i * ETH_GSTRING_LEN,
2357 fec_stats[i].name, ETH_GSTRING_LEN);
2358 break;
2359 }
2360}
2361
2362static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2363{
2364 switch (sset) {
2365 case ETH_SS_STATS:
2366 return ARRAY_SIZE(fec_stats);
2367 default:
2368 return -EOPNOTSUPP;
2369 }
2370}
Nikita Yushchenkof85de662016-12-06 09:26:53 +03002371
Andrew Lunn2b308422017-06-07 03:57:09 +02002372static void fec_enet_clear_ethtool_stats(struct net_device *dev)
2373{
2374 struct fec_enet_private *fep = netdev_priv(dev);
2375 int i;
2376
2377 /* Disable MIB statistics counters */
2378 writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
2379
2380 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2381 writel(0, fep->hwp + fec_stats[i].offset);
2382
2383 /* Don't disable MIB statistics counters */
2384 writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
2385}
2386
Nikita Yushchenkof85de662016-12-06 09:26:53 +03002387#else /* !defined(CONFIG_M5272) */
2388#define FEC_STATS_SIZE 0
2389static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2390{
2391}
Fabio Estevam41e8e402017-06-09 22:37:22 -03002392
2393static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
2394{
2395}
Guenter Roeckd1391932013-06-18 10:04:59 -07002396#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002397
Fugang Duand851b472014-09-17 05:18:52 +08002398/* ITR clock source is enet system clock (clk_ahb).
2399 * TCTT unit is cycle_ns * 64 cycle
2400 * So, the ICTT value = X us / (cycle_ns * 64)
2401 */
2402static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2403{
2404 struct fec_enet_private *fep = netdev_priv(ndev);
2405
2406 return us * (fep->itr_clk_rate / 64000) / 1000;
2407}
2408
2409/* Set threshold for interrupt coalescing */
2410static void fec_enet_itr_coal_set(struct net_device *ndev)
2411{
2412 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002413 int rx_itr, tx_itr;
2414
Fugang Duand851b472014-09-17 05:18:52 +08002415 /* Must be greater than zero to avoid unpredictable behavior */
2416 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2417 !fep->tx_time_itr || !fep->tx_pkts_itr)
2418 return;
2419
2420 /* Select enet system clock as Interrupt Coalescing
2421 * timer Clock Source
2422 */
2423 rx_itr = FEC_ITR_CLK_SEL;
2424 tx_itr = FEC_ITR_CLK_SEL;
2425
2426 /* set ICFT and ICTT */
2427 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2428 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2429 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2430 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2431
2432 rx_itr |= FEC_ITR_EN;
2433 tx_itr |= FEC_ITR_EN;
2434
2435 writel(tx_itr, fep->hwp + FEC_TXIC0);
2436 writel(rx_itr, fep->hwp + FEC_RXIC0);
Fugang Duanff7566b2016-06-22 18:52:35 +08002437 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
2438 writel(tx_itr, fep->hwp + FEC_TXIC1);
2439 writel(rx_itr, fep->hwp + FEC_RXIC1);
2440 writel(tx_itr, fep->hwp + FEC_TXIC2);
2441 writel(rx_itr, fep->hwp + FEC_RXIC2);
2442 }
Fugang Duand851b472014-09-17 05:18:52 +08002443}
2444
2445static int
2446fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2447{
2448 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002449
Fugang Duanff7566b2016-06-22 18:52:35 +08002450 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
Fugang Duand851b472014-09-17 05:18:52 +08002451 return -EOPNOTSUPP;
2452
2453 ec->rx_coalesce_usecs = fep->rx_time_itr;
2454 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2455
2456 ec->tx_coalesce_usecs = fep->tx_time_itr;
2457 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2458
2459 return 0;
2460}
2461
2462static int
2463fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2464{
2465 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002466 unsigned int cycle;
2467
Fugang Duanff7566b2016-06-22 18:52:35 +08002468 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
Fugang Duand851b472014-09-17 05:18:52 +08002469 return -EOPNOTSUPP;
2470
2471 if (ec->rx_max_coalesced_frames > 255) {
Colin Ian King9f647a62016-06-06 09:21:44 +01002472 pr_err("Rx coalesced frames exceed hardware limitation\n");
Fugang Duand851b472014-09-17 05:18:52 +08002473 return -EINVAL;
2474 }
2475
2476 if (ec->tx_max_coalesced_frames > 255) {
Colin Ian King9f647a62016-06-06 09:21:44 +01002477 pr_err("Tx coalesced frame exceed hardware limitation\n");
Fugang Duand851b472014-09-17 05:18:52 +08002478 return -EINVAL;
2479 }
2480
2481 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2482 if (cycle > 0xFFFF) {
Colin Ian King9f647a62016-06-06 09:21:44 +01002483 pr_err("Rx coalesced usec exceed hardware limitation\n");
Fugang Duand851b472014-09-17 05:18:52 +08002484 return -EINVAL;
2485 }
2486
2487 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2488 if (cycle > 0xFFFF) {
Colin Ian King9f647a62016-06-06 09:21:44 +01002489 pr_err("Rx coalesced usec exceed hardware limitation\n");
Fugang Duand851b472014-09-17 05:18:52 +08002490 return -EINVAL;
2491 }
2492
2493 fep->rx_time_itr = ec->rx_coalesce_usecs;
2494 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2495
2496 fep->tx_time_itr = ec->tx_coalesce_usecs;
2497 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2498
2499 fec_enet_itr_coal_set(ndev);
2500
2501 return 0;
2502}
2503
2504static void fec_enet_itr_coal_init(struct net_device *ndev)
2505{
2506 struct ethtool_coalesce ec;
2507
2508 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2509 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2510
2511 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2512 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2513
2514 fec_enet_set_coalesce(ndev, &ec);
2515}
2516
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002517static int fec_enet_get_tunable(struct net_device *netdev,
2518 const struct ethtool_tunable *tuna,
2519 void *data)
2520{
2521 struct fec_enet_private *fep = netdev_priv(netdev);
2522 int ret = 0;
2523
2524 switch (tuna->id) {
2525 case ETHTOOL_RX_COPYBREAK:
2526 *(u32 *)data = fep->rx_copybreak;
2527 break;
2528 default:
2529 ret = -EINVAL;
2530 break;
2531 }
2532
2533 return ret;
2534}
2535
2536static int fec_enet_set_tunable(struct net_device *netdev,
2537 const struct ethtool_tunable *tuna,
2538 const void *data)
2539{
2540 struct fec_enet_private *fep = netdev_priv(netdev);
2541 int ret = 0;
2542
2543 switch (tuna->id) {
2544 case ETHTOOL_RX_COPYBREAK:
2545 fep->rx_copybreak = *(u32 *)data;
2546 break;
2547 default:
2548 ret = -EINVAL;
2549 break;
2550 }
2551
2552 return ret;
2553}
2554
Nimrod Andyde40ed32014-12-24 17:30:39 +08002555static void
2556fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2557{
2558 struct fec_enet_private *fep = netdev_priv(ndev);
2559
2560 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2561 wol->supported = WAKE_MAGIC;
2562 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2563 } else {
2564 wol->supported = wol->wolopts = 0;
2565 }
2566}
2567
2568static int
2569fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2570{
2571 struct fec_enet_private *fep = netdev_priv(ndev);
2572
2573 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2574 return -EINVAL;
2575
2576 if (wol->wolopts & ~WAKE_MAGIC)
2577 return -EINVAL;
2578
2579 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2580 if (device_may_wakeup(&ndev->dev)) {
2581 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2582 if (fep->irq[0] > 0)
2583 enable_irq_wake(fep->irq[0]);
2584 } else {
2585 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2586 if (fep->irq[0] > 0)
2587 disable_irq_wake(fep->irq[0]);
2588 }
2589
2590 return 0;
2591}
2592
stephen hemminger9b07be42012-01-04 12:59:49 +00002593static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002594 .get_drvinfo = fec_enet_get_drvinfo,
Philippe Reynesdb65f352015-05-11 00:01:42 +02002595 .get_regs_len = fec_enet_get_regs_len,
2596 .get_regs = fec_enet_get_regs,
Florian Fainelli11d59282016-11-15 10:06:40 -08002597 .nway_reset = phy_ethtool_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002598 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002599 .get_coalesce = fec_enet_get_coalesce,
2600 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002601#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002602 .get_pauseparam = fec_enet_get_pauseparam,
2603 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002604 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002605 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002606 .get_sset_count = fec_enet_get_sset_count,
2607#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002608 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002609 .get_tunable = fec_enet_get_tunable,
2610 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002611 .get_wol = fec_enet_get_wol,
2612 .set_wol = fec_enet_set_wol,
Philippe Reynes9365fbf2016-05-10 00:19:43 +02002613 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2614 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Bryan Wue6b043d2010-03-31 02:10:44 +00002615};
2616
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002617static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002618{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002619 struct fec_enet_private *fep = netdev_priv(ndev);
Philippe Reynes45f5c322016-05-10 00:19:42 +02002620 struct phy_device *phydev = ndev->phydev;
Bryan Wue6b043d2010-03-31 02:10:44 +00002621
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002622 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002623 return -EINVAL;
2624
2625 if (!phydev)
2626 return -ENODEV;
2627
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002628 if (fep->bufdesc_ex) {
2629 if (cmd == SIOCSHWTSTAMP)
2630 return fec_ptp_set(ndev, rq);
2631 if (cmd == SIOCGHWTSTAMP)
2632 return fec_ptp_get(ndev, rq);
2633 }
Frank Liff43da82013-01-03 16:04:23 +00002634
Richard Cochran28b04112010-07-17 08:48:55 +00002635 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002638static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002639{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002640 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002641 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002642 struct sk_buff *skb;
2643 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002644 struct fec_enet_priv_tx_q *txq;
2645 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002646 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002647
Frank Li59d0f7462014-09-13 05:00:50 +08002648 for (q = 0; q < fep->num_rx_queues; q++) {
2649 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002650 bdp = rxq->bd.base;
2651 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002652 skb = rxq->rx_skbuff[i];
2653 rxq->rx_skbuff[i] = NULL;
2654 if (skb) {
2655 dma_unmap_single(&fep->pdev->dev,
Johannes Berg5cfa3032016-01-24 16:52:37 +01002656 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002657 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002658 DMA_FROM_DEVICE);
2659 dev_kfree_skb(skb);
2660 }
Troy Kisky7355f272016-02-05 14:52:46 -07002661 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Russell King730ee362014-07-08 00:23:14 +01002662 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002663 }
2664
Frank Li59d0f7462014-09-13 05:00:50 +08002665 for (q = 0; q < fep->num_tx_queues; q++) {
2666 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002667 bdp = txq->bd.base;
2668 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002669 kfree(txq->tx_bounce[i]);
2670 txq->tx_bounce[i] = NULL;
2671 skb = txq->tx_skbuff[i];
2672 txq->tx_skbuff[i] = NULL;
2673 dev_kfree_skb(skb);
2674 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002675 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002676}
2677
Frank Li59d0f7462014-09-13 05:00:50 +08002678static void fec_enet_free_queue(struct net_device *ndev)
2679{
2680 struct fec_enet_private *fep = netdev_priv(ndev);
2681 int i;
2682 struct fec_enet_priv_tx_q *txq;
2683
2684 for (i = 0; i < fep->num_tx_queues; i++)
2685 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2686 txq = fep->tx_queue[i];
Fugang Duan94920122017-04-11 19:13:05 +08002687 dma_free_coherent(&fep->pdev->dev,
Troy Kisky7355f272016-02-05 14:52:46 -07002688 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002689 txq->tso_hdrs,
2690 txq->tso_hdrs_dma);
2691 }
2692
2693 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002694 kfree(fep->rx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002695 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002696 kfree(fep->tx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002697}
2698
2699static int fec_enet_alloc_queue(struct net_device *ndev)
2700{
2701 struct fec_enet_private *fep = netdev_priv(ndev);
2702 int i;
2703 int ret = 0;
2704 struct fec_enet_priv_tx_q *txq;
2705
2706 for (i = 0; i < fep->num_tx_queues; i++) {
2707 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2708 if (!txq) {
2709 ret = -ENOMEM;
2710 goto alloc_failed;
2711 }
2712
2713 fep->tx_queue[i] = txq;
Troy Kisky7355f272016-02-05 14:52:46 -07002714 txq->bd.ring_size = TX_RING_SIZE;
2715 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002716
2717 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2718 txq->tx_wake_threshold =
Troy Kisky7355f272016-02-05 14:52:46 -07002719 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
Frank Li59d0f7462014-09-13 05:00:50 +08002720
Fugang Duan94920122017-04-11 19:13:05 +08002721 txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
Troy Kisky7355f272016-02-05 14:52:46 -07002722 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002723 &txq->tso_hdrs_dma,
2724 GFP_KERNEL);
2725 if (!txq->tso_hdrs) {
2726 ret = -ENOMEM;
2727 goto alloc_failed;
2728 }
2729 }
2730
2731 for (i = 0; i < fep->num_rx_queues; i++) {
2732 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2733 GFP_KERNEL);
2734 if (!fep->rx_queue[i]) {
2735 ret = -ENOMEM;
2736 goto alloc_failed;
2737 }
2738
Troy Kisky7355f272016-02-05 14:52:46 -07002739 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
2740 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002741 }
2742 return ret;
2743
2744alloc_failed:
2745 fec_enet_free_queue(ndev);
2746 return ret;
2747}
2748
2749static int
2750fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002751{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002752 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002753 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002754 struct sk_buff *skb;
2755 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002756 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002757
Frank Li59d0f7462014-09-13 05:00:50 +08002758 rxq = fep->rx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002759 bdp = rxq->bd.base;
2760 for (i = 0; i < rxq->bd.ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002761 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002762 if (!skb)
2763 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002764
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002765 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002766 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002767 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002768 }
Russell King730ee362014-07-08 00:23:14 +01002769
Fugang Duan4d494cd2014-09-13 05:00:48 +08002770 rxq->rx_skbuff[i] = skb;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002771 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Liff43da82013-01-03 16:04:23 +00002772
2773 if (fep->bufdesc_ex) {
2774 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002775 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002776 }
2777
Troy Kisky7355f272016-02-05 14:52:46 -07002778 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002779 }
2780
2781 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002782 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002783 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +08002784 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002785
Frank Li59d0f7462014-09-13 05:00:50 +08002786 err_alloc:
2787 fec_enet_free_buffers(ndev);
2788 return -ENOMEM;
2789}
2790
2791static int
2792fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2793{
2794 struct fec_enet_private *fep = netdev_priv(ndev);
2795 unsigned int i;
2796 struct bufdesc *bdp;
2797 struct fec_enet_priv_tx_q *txq;
2798
2799 txq = fep->tx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002800 bdp = txq->bd.base;
2801 for (i = 0; i < txq->bd.ring_size; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002802 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2803 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002804 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002805
Johannes Berg5cfa3032016-01-24 16:52:37 +01002806 bdp->cbd_sc = cpu_to_fec16(0);
2807 bdp->cbd_bufaddr = cpu_to_fec32(0);
Frank Li6605b732012-10-30 18:25:31 +00002808
Frank Liff43da82013-01-03 16:04:23 +00002809 if (fep->bufdesc_ex) {
2810 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002811 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002812 }
2813
Troy Kisky7355f272016-02-05 14:52:46 -07002814 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002815 }
2816
2817 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002818 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002819 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002820
2821 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002822
2823 err_alloc:
2824 fec_enet_free_buffers(ndev);
2825 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002826}
2827
Frank Li59d0f7462014-09-13 05:00:50 +08002828static int fec_enet_alloc_buffers(struct net_device *ndev)
2829{
2830 struct fec_enet_private *fep = netdev_priv(ndev);
2831 unsigned int i;
2832
2833 for (i = 0; i < fep->num_rx_queues; i++)
2834 if (fec_enet_alloc_rxq_buffers(ndev, i))
2835 return -ENOMEM;
2836
2837 for (i = 0; i < fep->num_tx_queues; i++)
2838 if (fec_enet_alloc_txq_buffers(ndev, i))
2839 return -ENOMEM;
2840 return 0;
2841}
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002844fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002846 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002847 int ret;
Richard Leitner1b0a83a2017-12-11 13:17:00 +01002848 bool reset_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Andrew Lunn8fff7552015-07-25 22:38:02 +02002850 ret = pm_runtime_get_sync(&fep->pdev->dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03002851 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002852 return ret;
2853
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002854 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002855 ret = fec_enet_clk_enable(ndev, true);
2856 if (ret)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002857 goto clk_enable;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002858
Richard Leitner1b0a83a2017-12-11 13:17:00 +01002859 /* During the first fec_enet_open call the PHY isn't probed at this
2860 * point. Therefore the phy_reset_after_clk_enable() call within
2861 * fec_enet_clk_enable() fails. As we need this reset in order to be
2862 * sure the PHY is working correctly we check if we need to reset again
2863 * later when the PHY is probed
2864 */
2865 if (ndev->phydev && ndev->phydev->drv)
2866 reset_again = false;
2867 else
2868 reset_again = true;
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /* I should reset the ring buffers here, but I don't yet know
2871 * a simple way to do that.
2872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002874 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002875 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002876 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002877
Nimrod Andy55dd2752015-06-23 15:32:51 +08002878 /* Init MAC prior to mii bus probe */
2879 fec_restart(ndev);
2880
Bryan Wu418bd0d2010-05-28 03:40:39 -07002881 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002882 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002883 if (ret)
2884 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002885
Richard Leitner1b0a83a2017-12-11 13:17:00 +01002886 /* Call phy_reset_after_clk_enable() again if it failed during
2887 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
2888 */
2889 if (reset_again)
2890 phy_reset_after_clk_enable(ndev->phydev);
2891
Lucas Stach29380902016-06-03 18:31:19 +02002892 if (fep->quirks & FEC_QUIRK_ERR006687)
2893 imx6q_cpuidle_fec_irqs_used();
2894
Russell Kingce5eaf02014-02-18 12:55:42 +00002895 napi_enable(&fep->napi);
Philippe Reynes45f5c322016-05-10 00:19:42 +02002896 phy_start(ndev->phydev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002897 netif_tx_start_all_queues(ndev);
2898
Nimrod Andyde40ed32014-12-24 17:30:39 +08002899 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2900 FEC_WOL_FLAG_ENABLE);
2901
Sascha Hauer22f6b862009-04-15 01:32:18 +00002902 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002903
2904err_enet_mii_probe:
2905 fec_enet_free_buffers(ndev);
2906err_enet_alloc:
2907 fec_enet_clk_enable(ndev, false);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002908clk_enable:
2909 pm_runtime_mark_last_busy(&fep->pdev->dev);
2910 pm_runtime_put_autosuspend(&fep->pdev->dev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002911 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
2915static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002916fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002918 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Philippe Reynes45f5c322016-05-10 00:19:42 +02002920 phy_stop(ndev->phydev);
Russell Kingd76cfae2014-07-08 00:23:04 +01002921
Russell King31a6de32014-07-08 12:40:23 +01002922 if (netif_device_present(ndev)) {
2923 napi_disable(&fep->napi);
2924 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002925 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Philippe Reynes45f5c322016-05-10 00:19:42 +02002928 phy_disconnect(ndev->phydev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002929
Lucas Stach29380902016-06-03 18:31:19 +02002930 if (fep->quirks & FEC_QUIRK_ERR006687)
2931 imx6q_cpuidle_fec_irqs_unused();
2932
Nikita Yushchenko80cca772016-11-29 09:44:51 +03002933 fec_enet_update_ethtool_stats(ndev);
2934
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002935 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002936 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002937 pm_runtime_mark_last_busy(&fep->pdev->dev);
2938 pm_runtime_put_autosuspend(&fep->pdev->dev);
2939
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002940 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return 0;
2943}
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945/* Set or clear the multicast filter for this adaptor.
2946 * Skeleton taken from sunlance driver.
2947 * The CPM Ethernet implementation allows Multicast as well as individual
2948 * MAC address filtering. Some of the drivers check to make sure it is
2949 * a group multicast address, and discard those that are not. I guess I
2950 * will do the same for now, but just remove the test if you want
2951 * individual filtering as well (do the upper net layers want or support
2952 * this kind of feature?).
2953 */
2954
Jiri Kosina6176e892016-08-12 16:34:49 +02002955#define FEC_HASH_BITS 6 /* #bits in hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956#define CRC32_POLY 0xEDB88320
2957
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002958static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002960 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002961 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002962 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 unsigned char hash;
Rui Sousa01f8902b2017-02-13 10:01:25 +08002964 unsigned int hash_high = 0, hash_low = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002966 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002967 tmp = readl(fep->hwp + FEC_R_CNTRL);
2968 tmp |= 0x8;
2969 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002970 return;
2971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Sascha Hauer4e831832009-04-15 01:32:19 +00002973 tmp = readl(fep->hwp + FEC_R_CNTRL);
2974 tmp &= ~0x8;
2975 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002976
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002977 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002978 /* Catch all multicast addresses, so set the
2979 * filter to all 1's
2980 */
2981 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2982 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Sascha Hauer4e831832009-04-15 01:32:19 +00002984 return;
2985 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002986
Rui Sousa01f8902b2017-02-13 10:01:25 +08002987 /* Add the addresses in hash register */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002988 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002989 /* calculate crc32 value of mac address */
2990 crc = 0xffffffff;
2991
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002992 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002993 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002994 for (bit = 0; bit < 8; bit++, data >>= 1) {
2995 crc = (crc >> 1) ^
2996 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002999
Jiri Kosina6176e892016-08-12 16:34:49 +02003000 /* only upper 6 bits (FEC_HASH_BITS) are used
Peter Meerwald-Stadler981a0542016-12-16 14:23:36 +01003001 * which point to specific bit in the hash registers
Sascha Hauer4e831832009-04-15 01:32:19 +00003002 */
Jiri Kosina6176e892016-08-12 16:34:49 +02003003 hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
Sascha Hauer4e831832009-04-15 01:32:19 +00003004
Rui Sousa01f8902b2017-02-13 10:01:25 +08003005 if (hash > 31)
3006 hash_high |= 1 << (hash - 32);
3007 else
3008 hash_low |= 1 << hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
Rui Sousa01f8902b2017-02-13 10:01:25 +08003010
3011 writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3012 writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
Sascha Hauer22f6b862009-04-15 01:32:18 +00003015/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00003016static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003017fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003019 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00003020 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Lucas Stach44934fa2014-03-30 21:32:08 +02003022 if (addr) {
3023 if (!is_valid_ether_addr(addr->sa_data))
3024 return -EADDRNOTAVAIL;
3025 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3026 }
Sascha Hauer009fda82009-04-15 01:32:23 +00003027
Nimrod Andy9638d192015-09-10 09:35:39 +08003028 /* Add netif status check here to avoid system hang in below case:
3029 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3030 * After ethx down, fec all clocks are gated off and then register
3031 * access causes system hang.
3032 */
3033 if (!netif_running(ndev))
3034 return 0;
3035
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003036 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3037 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00003038 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003039 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07003040 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00003041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003044#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00003045/**
3046 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003047 * @dev: The FEC network adapter
3048 *
3049 * Polled functionality used by netconsole and others in non interrupt mode
3050 *
3051 */
Wei Yongjun47a52472013-03-20 05:06:11 +00003052static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003053{
3054 int i;
3055 struct fec_enet_private *fep = netdev_priv(dev);
3056
3057 for (i = 0; i < FEC_IRQ_NUM; i++) {
3058 if (fep->irq[i] > 0) {
3059 disable_irq(fep->irq[i]);
3060 fec_enet_interrupt(fep->irq[i], dev);
3061 enable_irq(fep->irq[i]);
3062 }
3063 }
3064}
3065#endif
3066
Nimrod Andy5bc26722014-10-13 10:53:48 +08003067static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00003068 netdev_features_t features)
3069{
3070 struct fec_enet_private *fep = netdev_priv(netdev);
3071 netdev_features_t changed = features ^ netdev->features;
3072
3073 netdev->features = features;
3074
3075 /* Receive checksum has been changed */
3076 if (changed & NETIF_F_RXCSUM) {
3077 if (features & NETIF_F_RXCSUM)
3078 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3079 else
3080 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01003081 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08003082}
Jim Baxter4c09eed2013-04-19 08:10:49 +00003083
Nimrod Andy5bc26722014-10-13 10:53:48 +08003084static int fec_set_features(struct net_device *netdev,
3085 netdev_features_t features)
3086{
3087 struct fec_enet_private *fep = netdev_priv(netdev);
3088 netdev_features_t changed = features ^ netdev->features;
3089
Fabio Estevam5b40f702015-09-25 18:31:32 -03003090 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08003091 napi_disable(&fep->napi);
3092 netif_tx_lock_bh(netdev);
3093 fec_stop(netdev);
3094 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01003095 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003096 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01003097 netif_tx_unlock_bh(netdev);
3098 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08003099 } else {
3100 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00003101 }
3102
3103 return 0;
3104}
3105
Sascha Hauer009fda82009-04-15 01:32:23 +00003106static const struct net_device_ops fec_netdev_ops = {
3107 .ndo_open = fec_enet_open,
3108 .ndo_stop = fec_enet_close,
3109 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003110 .ndo_set_rx_mode = set_multicast_list,
Sascha Hauer009fda82009-04-15 01:32:23 +00003111 .ndo_validate_addr = eth_validate_addr,
3112 .ndo_tx_timeout = fec_timeout,
3113 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01003114 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003115#ifdef CONFIG_NET_POLL_CONTROLLER
3116 .ndo_poll_controller = fec_poll_controller,
3117#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00003118 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00003119};
3120
Troy Kisky53bb20d2016-02-05 14:52:47 -07003121static const unsigned short offset_des_active_rxq[] = {
3122 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3123};
3124
3125static const unsigned short offset_des_active_txq[] = {
3126 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3127};
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /*
3130 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00003131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003133static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003135 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00003136 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08003137 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08003138 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08003139 unsigned int i;
Troy Kisky7355f272016-02-05 14:52:46 -07003140 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3141 sizeof(struct bufdesc);
3142 unsigned dsize_log2 = __fls(dsize);
Nimrod Andy55d02182014-06-12 08:16:21 +08003143
Troy Kisky7355f272016-02-05 14:52:46 -07003144 WARN_ON(dsize != (1 << dsize_log2));
Lucas Stach3f1dcc62018-01-17 19:30:55 +01003145#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
Fugang Duan41ef84c2014-09-13 05:00:54 +08003146 fep->rx_align = 0xf;
3147 fep->tx_align = 0xf;
3148#else
3149 fep->rx_align = 0x3;
3150 fep->tx_align = 0x3;
3151#endif
3152
Frank Li59d0f7462014-09-13 05:00:50 +08003153 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003154
Troy Kisky7355f272016-02-05 14:52:46 -07003155 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003157 /* Allocate memory for buffer descriptors. */
Lucas Stachc0a1a0a2015-07-23 16:06:19 +02003158 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3159 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003160 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003161 return -ENOMEM;
3162 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003163
Fugang Duan4d494cd2014-09-13 05:00:48 +08003164 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Shawn Guo49da97d2011-01-05 21:13:11 +00003166 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003167 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003168 /* make sure MAC we just acquired is programmed into the hw */
3169 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003171 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003172 for (i = 0; i < fep->num_rx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003173 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3174 unsigned size = dsize * rxq->bd.ring_size;
3175
3176 rxq->bd.qid = i;
3177 rxq->bd.base = cbd_base;
3178 rxq->bd.cur = cbd_base;
3179 rxq->bd.dma = bd_dma;
3180 rxq->bd.dsize = dsize;
3181 rxq->bd.dsize_log2 = dsize_log2;
Troy Kisky53bb20d2016-02-05 14:52:47 -07003182 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
Troy Kisky7355f272016-02-05 14:52:46 -07003183 bd_dma += size;
3184 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3185 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003186 }
3187
3188 for (i = 0; i < fep->num_tx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003189 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3190 unsigned size = dsize * txq->bd.ring_size;
3191
3192 txq->bd.qid = i;
3193 txq->bd.base = cbd_base;
3194 txq->bd.cur = cbd_base;
3195 txq->bd.dma = bd_dma;
3196 txq->bd.dsize = dsize;
3197 txq->bd.dsize_log2 = dsize_log2;
Troy Kisky53bb20d2016-02-05 14:52:47 -07003198 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
Troy Kisky7355f272016-02-05 14:52:46 -07003199 bd_dma += size;
3200 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3201 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003202 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Sascha Hauer22f6b862009-04-15 01:32:18 +00003205 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003206 ndev->watchdog_timeo = TX_TIMEOUT;
3207 ndev->netdev_ops = &fec_netdev_ops;
3208 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003209
Frank Lidc975382013-01-28 18:31:42 +00003210 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003211 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003212
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003213 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003214 /* enable hw VLAN support */
3215 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003216
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003217 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003218 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3219
Shawn Guo48496252013-05-08 21:08:22 +00003220 /* enable hw accelerator */
3221 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003222 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003223 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3224 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003225
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003226 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003227 fep->tx_align = 0;
3228 fep->rx_align = 0x3f;
3229 }
3230
Nimrod Andy09d1e542014-06-12 08:16:20 +08003231 ndev->hw_features = ndev->features;
3232
Russell Kingef833372014-07-08 12:40:43 +01003233 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Andrew Lunn2b308422017-06-07 03:57:09 +02003235 if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
3236 fec_enet_clear_ethtool_stats(ndev);
3237 else
3238 fec_enet_update_ethtool_stats(ndev);
Nikita Yushchenko80cca772016-11-29 09:44:51 +03003239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241}
3242
Shawn Guoca2cc332011-06-25 02:04:35 +08003243#ifdef CONFIG_OF
Fugang Duan9269e552017-04-11 19:13:06 +08003244static int fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003245{
3246 int err, phy_reset;
Bernhard Walle962d8cd2016-03-03 10:15:55 +01003247 bool active_high = false;
Quentin Schulz159a07602017-05-23 11:48:08 +02003248 int msec = 1, phy_post_delay = 0;
Shawn Guoca2cc332011-06-25 02:04:35 +08003249 struct device_node *np = pdev->dev.of_node;
3250
3251 if (!np)
Fugang Duan9269e552017-04-11 19:13:06 +08003252 return 0;
Shawn Guoca2cc332011-06-25 02:04:35 +08003253
Fugang Duan61e04cc2017-04-11 19:13:03 +08003254 err = of_property_read_u32(np, "phy-reset-duration", &msec);
Shawn Guoa3caad02012-06-27 03:45:24 +00003255 /* A sane reset duration should not be longer than 1s */
Fugang Duan61e04cc2017-04-11 19:13:03 +08003256 if (!err && msec > 1000)
Shawn Guoa3caad02012-06-27 03:45:24 +00003257 msec = 1;
3258
Shawn Guoca2cc332011-06-25 02:04:35 +08003259 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fugang Duan9269e552017-04-11 19:13:06 +08003260 if (phy_reset == -EPROBE_DEFER)
3261 return phy_reset;
3262 else if (!gpio_is_valid(phy_reset))
3263 return 0;
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003264
Quentin Schulz159a07602017-05-23 11:48:08 +02003265 err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
3266 /* valid reset duration should be less than 1s */
3267 if (!err && phy_post_delay > 1000)
3268 return -EINVAL;
3269
Bernhard Walle962d8cd2016-03-03 10:15:55 +01003270 active_high = of_property_read_bool(np, "phy-reset-active-high");
Bernhard Walle64f10f62016-02-08 21:21:13 +01003271
Shawn Guo119fc002012-06-27 03:45:22 +00003272 err = devm_gpio_request_one(&pdev->dev, phy_reset,
Bernhard Walle962d8cd2016-03-03 10:15:55 +01003273 active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Bernhard Walle64f10f62016-02-08 21:21:13 +01003274 "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003275 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003276 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Fugang Duan9269e552017-04-11 19:13:06 +08003277 return err;
Shawn Guoca2cc332011-06-25 02:04:35 +08003278 }
Stefan Wahreneb37c5632016-06-08 20:42:46 +00003279
3280 if (msec > 20)
3281 msleep(msec);
3282 else
3283 usleep_range(msec * 1000, msec * 1000 + 1000);
3284
Bernhard Walle962d8cd2016-03-03 10:15:55 +01003285 gpio_set_value_cansleep(phy_reset, !active_high);
Fugang Duan9269e552017-04-11 19:13:06 +08003286
Quentin Schulz159a07602017-05-23 11:48:08 +02003287 if (!phy_post_delay)
3288 return 0;
3289
3290 if (phy_post_delay > 20)
3291 msleep(phy_post_delay);
3292 else
3293 usleep_range(phy_post_delay * 1000,
3294 phy_post_delay * 1000 + 1000);
3295
Fugang Duan9269e552017-04-11 19:13:06 +08003296 return 0;
Shawn Guoca2cc332011-06-25 02:04:35 +08003297}
3298#else /* CONFIG_OF */
Fugang Duan9269e552017-04-11 19:13:06 +08003299static int fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003300{
3301 /*
3302 * In case of platform probe, the reset has been done
3303 * by machine code.
3304 */
Fugang Duan9269e552017-04-11 19:13:06 +08003305 return 0;
Shawn Guoca2cc332011-06-25 02:04:35 +08003306}
3307#endif /* CONFIG_OF */
3308
Fugang Duan9fc095f2014-09-13 05:00:49 +08003309static void
3310fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3311{
3312 struct device_node *np = pdev->dev.of_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003313
3314 *num_tx = *num_rx = 1;
3315
3316 if (!np || !of_device_is_available(np))
3317 return;
3318
3319 /* parse the num of tx and rx queues */
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303320 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003321
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303322 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003323
3324 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003325 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3326 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003327 *num_tx = 1;
3328 return;
3329 }
3330
3331 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003332 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3333 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003334 *num_rx = 1;
3335 return;
3336 }
3337
3338}
3339
Troy Kisky4ad1cee2017-11-03 10:29:59 -07003340static int fec_enet_get_irq_cnt(struct platform_device *pdev)
3341{
3342 int irq_cnt = platform_irq_count(pdev);
3343
3344 if (irq_cnt > FEC_IRQ_NUM)
3345 irq_cnt = FEC_IRQ_NUM; /* last for pps */
3346 else if (irq_cnt == 2)
3347 irq_cnt = 1; /* last for pps */
3348 else if (irq_cnt <= 0)
3349 irq_cnt = 1; /* At least 1 irq is needed */
3350 return irq_cnt;
3351}
3352
Bill Pemberton33897cc2012-12-03 09:23:58 -05003353static int
Sascha Haueread73182009-01-28 23:03:11 +00003354fec_probe(struct platform_device *pdev)
3355{
3356 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003357 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003358 struct net_device *ndev;
3359 int i, irq, ret = 0;
3360 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003361 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003362 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003363 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003364 int num_tx_qs;
3365 int num_rx_qs;
Troy Kisky4ad1cee2017-11-03 10:29:59 -07003366 char irq_name[8];
3367 int irq_cnt;
Shawn Guoca2cc332011-06-25 02:04:35 +08003368
Fugang Duan9fc095f2014-09-13 05:00:49 +08003369 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3370
Sascha Haueread73182009-01-28 23:03:11 +00003371 /* Init network device */
Nikita Yushchenko80cca772016-11-29 09:44:51 +03003372 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
Nikita Yushchenkof85de662016-12-06 09:26:53 +03003373 FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003374 if (!ndev)
3375 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003376
3377 SET_NETDEV_DEV(ndev, &pdev->dev);
3378
3379 /* setup board info structure */
3380 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003381
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003382 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3383 if (of_id)
3384 pdev->id_entry = of_id->data;
3385 fep->quirks = pdev->id_entry->driver_data;
3386
Hubert Feurstein0c818592015-01-07 14:48:17 +01003387 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003388 fep->num_rx_queues = num_rx_qs;
3389 fep->num_tx_queues = num_tx_qs;
3390
Guenter Roeckd1391932013-06-18 10:04:59 -07003391#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003392 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003393 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003394 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003395#endif
Frank Libaa70a52013-01-16 16:55:58 +00003396
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003397 /* Select default pin state */
3398 pinctrl_pm_select_default_state(&pdev->dev);
3399
Fabio Estevam399db752013-07-21 13:25:03 -03003400 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303401 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3402 if (IS_ERR(fep->hwp)) {
3403 ret = PTR_ERR(fep->hwp);
3404 goto failed_ioremap;
3405 }
3406
Bryan Wue6b043d2010-03-31 02:10:44 +00003407 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003408 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003409
Sascha Haueread73182009-01-28 23:03:11 +00003410 platform_set_drvdata(pdev, ndev);
3411
Lucas Stach29380902016-06-03 18:31:19 +02003412 if ((of_machine_is_compatible("fsl,imx6q") ||
3413 of_machine_is_compatible("fsl,imx6dl")) &&
3414 !of_property_read_bool(np, "fsl,err006687-workaround-present"))
3415 fep->quirks |= FEC_QUIRK_ERR006687;
3416
Nimrod Andyde40ed32014-12-24 17:30:39 +08003417 if (of_get_property(np, "fsl,magic-packet", NULL))
3418 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3419
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003420 phy_node = of_parse_phandle(np, "phy-handle", 0);
3421 if (!phy_node && of_phy_is_fixed_link(np)) {
3422 ret = of_phy_register_fixed_link(np);
3423 if (ret < 0) {
3424 dev_err(&pdev->dev,
3425 "broken fixed-link specification\n");
3426 goto failed_phy;
3427 }
3428 phy_node = of_node_get(np);
3429 }
3430 fep->phy_node = phy_node;
3431
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003432 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003433 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003434 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003435 if (pdata)
3436 fep->phy_interface = pdata->phy;
3437 else
3438 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3439 } else {
3440 fep->phy_interface = ret;
3441 }
3442
Fabio Estevame2f8d552013-02-22 06:40:45 +00003443 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3444 if (IS_ERR(fep->clk_ipg)) {
3445 ret = PTR_ERR(fep->clk_ipg);
3446 goto failed_clk;
3447 }
3448
3449 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3450 if (IS_ERR(fep->clk_ahb)) {
3451 ret = PTR_ERR(fep->clk_ahb);
3452 goto failed_clk;
3453 }
3454
Fugang Duand851b472014-09-17 05:18:52 +08003455 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3456
Linus Torvalds38f56f32013-05-07 11:06:17 -07003457 /* enet_out is optional, depends on board */
3458 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3459 if (IS_ERR(fep->clk_enet_out))
3460 fep->clk_enet_out = NULL;
3461
Nimrod Andy91c0d982014-08-21 17:09:38 +08003462 fep->ptp_clk_on = false;
3463 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003464
3465 /* clk_ref is optional, depends on board */
3466 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3467 if (IS_ERR(fep->clk_ref))
3468 fep->clk_ref = NULL;
3469
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003470 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003471 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003472 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003473 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003474 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003475 }
3476
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003477 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003478 if (ret)
3479 goto failed_clk;
3480
Andrew Lunn8fff7552015-07-25 22:38:02 +02003481 ret = clk_prepare_enable(fep->clk_ipg);
3482 if (ret)
3483 goto failed_clk_ipg;
3484
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003485 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3486 if (!IS_ERR(fep->reg_phy)) {
3487 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003488 if (ret) {
3489 dev_err(&pdev->dev,
3490 "Failed to enable phy regulator: %d\n", ret);
Fugang Duan9269e552017-04-11 19:13:06 +08003491 clk_disable_unprepare(fep->clk_ipg);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003492 goto failed_regulator;
3493 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003494 } else {
Fugang Duan3f38c682018-01-03 10:39:30 +08003495 if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
3496 ret = -EPROBE_DEFER;
3497 goto failed_regulator;
3498 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003499 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003500 }
3501
Andrew Lunn8fff7552015-07-25 22:38:02 +02003502 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3503 pm_runtime_use_autosuspend(&pdev->dev);
Lucas Stach14d2b7c2015-08-03 17:50:11 +02003504 pm_runtime_get_noresume(&pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003505 pm_runtime_set_active(&pdev->dev);
3506 pm_runtime_enable(&pdev->dev);
3507
Fugang Duan9269e552017-04-11 19:13:06 +08003508 ret = fec_reset_phy(pdev);
3509 if (ret)
3510 goto failed_reset;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003511
Troy Kisky4ad1cee2017-11-03 10:29:59 -07003512 irq_cnt = fec_enet_get_irq_cnt(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003513 if (fep->bufdesc_ex)
Troy Kisky4ad1cee2017-11-03 10:29:59 -07003514 fec_ptp_init(pdev, irq_cnt);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003515
3516 ret = fec_enet_init(ndev);
3517 if (ret)
3518 goto failed_init;
3519
Troy Kisky4ad1cee2017-11-03 10:29:59 -07003520 for (i = 0; i < irq_cnt; i++) {
3521 sprintf(irq_name, "int%d", i);
3522 irq = platform_get_irq_byname(pdev, irq_name);
3523 if (irq < 0)
3524 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003525 if (irq < 0) {
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003526 ret = irq;
3527 goto failed_irq;
3528 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003529 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003530 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003531 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003532 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003533
3534 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003535 }
3536
Fugang Duanb4d39b52014-09-13 05:00:55 +08003537 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003538 ret = fec_enet_mii_init(pdev);
3539 if (ret)
3540 goto failed_mii_init;
3541
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003542 /* Carrier starts down, phylib will bring it up */
3543 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003544 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003545 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003546
Sascha Haueread73182009-01-28 23:03:11 +00003547 ret = register_netdev(ndev);
3548 if (ret)
3549 goto failed_register;
3550
Nimrod Andyde40ed32014-12-24 17:30:39 +08003551 device_init_wakeup(&ndev->dev, fep->wol_flag &
3552 FEC_WOL_HAS_MAGIC_PACKET);
3553
Fabio Estevameb1d0642013-04-13 07:25:36 +00003554 if (fep->bufdesc_ex && fep->ptp_clock)
3555 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3556
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003557 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003558 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003559
3560 pm_runtime_mark_last_busy(&pdev->dev);
3561 pm_runtime_put_autosuspend(&pdev->dev);
3562
Sascha Haueread73182009-01-28 23:03:11 +00003563 return 0;
3564
3565failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003566 fec_enet_mii_remove(fep);
3567failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003568failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003569failed_init:
Lucas Stach32cba572015-07-23 16:06:20 +02003570 fec_ptp_stop(pdev);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003571 if (fep->reg_phy)
3572 regulator_disable(fep->reg_phy);
Fugang Duan9269e552017-04-11 19:13:06 +08003573failed_reset:
3574 pm_runtime_put(&pdev->dev);
3575 pm_runtime_disable(&pdev->dev);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003576failed_regulator:
Andrew Lunn8fff7552015-07-25 22:38:02 +02003577failed_clk_ipg:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003578 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003579failed_clk:
Johan Hovold82005b12016-11-28 19:25:00 +01003580 if (of_phy_is_fixed_link(np))
3581 of_phy_deregister_fixed_link(np);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003582 of_node_put(phy_node);
Fugang Duand1616f02018-01-04 10:47:20 +08003583failed_phy:
3584 dev_id--;
Sascha Haueread73182009-01-28 23:03:11 +00003585failed_ioremap:
3586 free_netdev(ndev);
3587
3588 return ret;
3589}
3590
Bill Pemberton33897cc2012-12-03 09:23:58 -05003591static int
Sascha Haueread73182009-01-28 23:03:11 +00003592fec_drv_remove(struct platform_device *pdev)
3593{
3594 struct net_device *ndev = platform_get_drvdata(pdev);
3595 struct fec_enet_private *fep = netdev_priv(ndev);
Johan Hovold82005b12016-11-28 19:25:00 +01003596 struct device_node *np = pdev->dev.of_node;
Sascha Haueread73182009-01-28 23:03:11 +00003597
Russell King36cdc742014-07-08 13:01:44 +01003598 cancel_work_sync(&fep->tx_timeout_work);
Lucas Stach32cba572015-07-23 16:06:20 +02003599 fec_ptp_stop(pdev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003600 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003601 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003602 if (fep->reg_phy)
3603 regulator_disable(fep->reg_phy);
Florian Fainellia0692152018-03-18 12:49:51 -07003604 pm_runtime_put(&pdev->dev);
3605 pm_runtime_disable(&pdev->dev);
Johan Hovold82005b12016-11-28 19:25:00 +01003606 if (of_phy_is_fixed_link(np))
3607 of_phy_deregister_fixed_link(np);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003608 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003609 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003610
Sascha Haueread73182009-01-28 23:03:11 +00003611 return 0;
3612}
3613
Fabio Estevamdd66d382014-07-24 18:24:01 -03003614static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003615{
Eric Benard87cad5c2010-06-18 04:19:54 +00003616 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003617 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003618
Russell Kingda1774e2014-07-08 12:39:57 +01003619 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003620 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003621 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3622 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Philippe Reynes45f5c322016-05-10 00:19:42 +02003623 phy_stop(ndev->phydev);
Russell King31a6de32014-07-08 12:40:23 +01003624 napi_disable(&fep->napi);
3625 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003626 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003627 netif_tx_unlock_bh(ndev);
3628 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003629 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003630 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3631 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003632 }
Russell Kingda1774e2014-07-08 12:39:57 +01003633 rtnl_unlock();
3634
Nimrod Andyde40ed32014-12-24 17:30:39 +08003635 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003636 regulator_disable(fep->reg_phy);
3637
Nimrod Andy858eeb72014-12-11 09:20:31 +08003638 /* SOC supply clock to phy, when clock is disabled, phy link down
3639 * SOC control phy regulator, when regulator is disabled, phy link down
3640 */
3641 if (fep->clk_enet_out || fep->reg_phy)
3642 fep->link = 0;
3643
Sascha Haueread73182009-01-28 23:03:11 +00003644 return 0;
3645}
3646
Fabio Estevamdd66d382014-07-24 18:24:01 -03003647static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003648{
Eric Benard87cad5c2010-06-18 04:19:54 +00003649 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003650 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003651 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003652 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003653 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003654
Nimrod Andyde40ed32014-12-24 17:30:39 +08003655 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003656 ret = regulator_enable(fep->reg_phy);
3657 if (ret)
3658 return ret;
3659 }
Sascha Haueread73182009-01-28 23:03:11 +00003660
Russell Kingda1774e2014-07-08 12:39:57 +01003661 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003662 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003663 ret = fec_enet_clk_enable(ndev, true);
3664 if (ret) {
3665 rtnl_unlock();
3666 goto failed_clk;
3667 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003668 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3669 if (pdata && pdata->sleep_mode_enable)
3670 pdata->sleep_mode_enable(false);
3671 val = readl(fep->hwp + FEC_ECNTRL);
3672 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3673 writel(val, fep->hwp + FEC_ECNTRL);
3674 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3675 } else {
3676 pinctrl_pm_select_default_state(&fep->pdev->dev);
3677 }
Russell Kingef833372014-07-08 12:40:43 +01003678 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003679 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003680 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003681 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003682 napi_enable(&fep->napi);
Philippe Reynes45f5c322016-05-10 00:19:42 +02003683 phy_start(ndev->phydev);
Sascha Haueread73182009-01-28 23:03:11 +00003684 }
Russell Kingda1774e2014-07-08 12:39:57 +01003685 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003686
Sascha Haueread73182009-01-28 23:03:11 +00003687 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003688
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003689failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003690 if (fep->reg_phy)
3691 regulator_disable(fep->reg_phy);
3692 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003693}
3694
Andrew Lunn8fff7552015-07-25 22:38:02 +02003695static int __maybe_unused fec_runtime_suspend(struct device *dev)
3696{
3697 struct net_device *ndev = dev_get_drvdata(dev);
3698 struct fec_enet_private *fep = netdev_priv(ndev);
3699
3700 clk_disable_unprepare(fep->clk_ipg);
3701
3702 return 0;
3703}
3704
3705static int __maybe_unused fec_runtime_resume(struct device *dev)
3706{
3707 struct net_device *ndev = dev_get_drvdata(dev);
3708 struct fec_enet_private *fep = netdev_priv(ndev);
3709
3710 return clk_prepare_enable(fep->clk_ipg);
3711}
3712
3713static const struct dev_pm_ops fec_pm_ops = {
3714 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3715 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3716};
Denis Kirjanov59d42892010-06-02 09:27:04 +00003717
Sascha Haueread73182009-01-28 23:03:11 +00003718static struct platform_driver fec_driver = {
3719 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003720 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003721 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003722 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003723 },
Shawn Guob5680e02011-01-05 21:13:13 +00003724 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003725 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003726 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003727};
3728
Fabio Estevamaaca2372012-01-23 16:44:50 +00003729module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003731MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732MODULE_LICENSE("GPL");