blob: b039288f5c983ffd274d9b48e8efe91b534917c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10005 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
10 * small packets.
11 *
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
14 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100015 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100017 *
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100019 * Copyright (c) 2004-2006 Macq Electronique SA.
Shawn Guob5680e02011-01-05 21:13:13 +000020 *
Shawn Guo230dec62011-09-23 02:12:48 +000021 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/string.h>
Andrew Lunn8fff7552015-07-25 22:38:02 +020027#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/ptrace.h>
29#include <linux/errno.h>
30#include <linux/ioport.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000037#include <linux/in.h>
38#include <linux/ip.h>
39#include <net/ip.h>
Nimrod Andy79f33912014-06-12 08:16:23 +080040#include <net/tso.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
45#include <linux/workqueue.h>
46#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000047#include <linux/io.h>
48#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000049#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000050#include <linux/platform_device.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010051#include <linux/mdio.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000052#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070053#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080054#include <linux/of.h>
55#include <linux/of_device.h>
56#include <linux/of_gpio.h>
Uwe Kleine-König407066f2014-08-11 17:35:33 +020057#include <linux/of_mdio.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080058#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000059#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010060#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030061#include <linux/pinctrl/consumer.h>
Frank Lic259c132014-10-03 14:29:14 -070062#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Greg Ungerer080853a2007-07-30 16:28:46 +100064#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Christoph Müllner772e42b2013-06-27 21:18:23 +020068static void set_multicast_list(struct net_device *ndev);
Fugang Duand851b472014-09-17 05:18:52 +080069static void fec_enet_itr_coal_init(struct net_device *ndev);
Christoph Müllner772e42b2013-06-27 21:18:23 +020070
Shawn Guob5680e02011-01-05 21:13:13 +000071#define DRIVER_NAME "fec"
72
Fugang Duan4d494cd2014-09-13 05:00:48 +080073#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
74
Frank Libaa70a52013-01-16 16:55:58 +000075/* Pause frame feild and FIFO threshold */
76#define FEC_ENET_FCE (1 << 5)
77#define FEC_ENET_RSEM_V 0x84
78#define FEC_ENET_RSFL_V 16
79#define FEC_ENET_RAEM_V 0x8
80#define FEC_ENET_RAFL_V 0x8
81#define FEC_ENET_OPD_V 0xFFF0
Andrew Lunn8fff7552015-07-25 22:38:02 +020082#define FEC_MDIO_PM_TIMEOUT 100 /* ms */
Frank Libaa70a52013-01-16 16:55:58 +000083
Shawn Guob5680e02011-01-05 21:13:13 +000084static struct platform_device_id fec_devtype[] = {
85 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080086 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000087 .name = DRIVER_NAME,
88 .driver_data = 0,
89 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080090 .name = "imx25-fec",
Greg Ungerer18803492015-06-20 15:51:57 +100091 .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +080092 }, {
93 .name = "imx27-fec",
Greg Ungerer18803492015-06-20 15:51:57 +100094 .driver_data = FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +080095 }, {
Shawn Guob5680e02011-01-05 21:13:13 +000096 .name = "imx28-fec",
Stefan Agner3d125f92015-01-14 00:20:21 +010097 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
Greg Ungerer18803492015-06-20 15:51:57 +100098 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +080099 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000100 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000101 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100102 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Greg Ungerer18803492015-06-20 15:51:57 +1000103 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
104 FEC_QUIRK_HAS_RACC,
Shawn Guo230dec62011-09-23 02:12:48 +0000105 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000106 .name = "mvf600-fec",
Greg Ungerer18803492015-06-20 15:51:57 +1000107 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
Jingchang Luca7c4a42013-04-11 21:12:45 +0000108 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800109 .name = "imx6sx-fec",
110 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
111 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800112 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
Greg Ungerer18803492015-06-20 15:51:57 +1000113 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
114 FEC_QUIRK_HAS_RACC,
Fugang Duan95a77472014-09-13 05:00:47 +0800115 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800116 /* sentinel */
117 }
Shawn Guob5680e02011-01-05 21:13:13 +0000118};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800119MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000120
Shawn Guoca2cc332011-06-25 02:04:35 +0800121enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000122 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800123 IMX27_FEC, /* runs on i.mx27/35/51 */
124 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000125 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000126 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800127 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800128};
129
130static const struct of_device_id fec_dt_ids[] = {
131 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
132 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
133 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000134 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000135 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800136 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800137 { /* sentinel */ }
138};
139MODULE_DEVICE_TABLE(of, fec_dt_ids);
140
Shawn Guo49da97d2011-01-05 21:13:11 +0000141static unsigned char macaddr[ETH_ALEN];
142module_param_array(macaddr, byte, NULL, 0);
143MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
144
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000145#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * Some hardware gets it MAC address out of local flash memory.
148 * if this is non-zero then assume it is the address to get MAC from.
149 */
150#if defined(CONFIG_NETtel)
151#define FEC_FLASHMAC 0xf0006006
152#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
153#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#elif defined(CONFIG_CANCam)
155#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000156#elif defined (CONFIG_M5272C3)
157#define FEC_FLASHMAC (0xffe04000 + 4)
158#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000159#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#else
161#define FEC_FLASHMAC 0
162#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800163#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000164
Jim Baxtercdffcf12013-07-02 22:52:56 +0100165/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100167#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100169#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jim Baxter4c09eed2013-04-19 08:10:49 +0000171/* FEC receive acceleration */
172#define FEC_RACC_IPDIS (1 << 1)
173#define FEC_RACC_PRODIS (1 << 2)
174#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
Matt Waddel6b265292006-06-27 13:10:56 +1000177 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * size bits. Other FEC hardware does not, so we need to take that into
179 * account when setting it.
180 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000181#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100182 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
184#else
185#define OPT_FRAME_SIZE 0
186#endif
187
Bryan Wue6b043d2010-03-31 02:10:44 +0000188/* FEC MII MMFR bits definition */
189#define FEC_MMFR_ST (1 << 30)
190#define FEC_MMFR_OP_READ (2 << 28)
191#define FEC_MMFR_OP_WRITE (1 << 28)
192#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
193#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
194#define FEC_MMFR_TA (2 << 16)
195#define FEC_MMFR_DATA(v) (v & 0xffff)
Nimrod Andyde40ed32014-12-24 17:30:39 +0800196/* FEC ECR bits definition */
197#define FEC_ECR_MAGICEN (1 << 2)
198#define FEC_ECR_SLEEP (1 << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500200#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Sascha Hauer22f6b862009-04-15 01:32:18 +0000202/* Transmitter timeout */
203#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Frank Libaa70a52013-01-16 16:55:58 +0000205#define FEC_PAUSE_FLAG_AUTONEG 0x1
206#define FEC_PAUSE_FLAG_ENABLE 0x2
Nimrod Andyde40ed32014-12-24 17:30:39 +0800207#define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
208#define FEC_WOL_FLAG_ENABLE (0x1 << 1)
209#define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
Frank Libaa70a52013-01-16 16:55:58 +0000210
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800211#define COPYBREAK_DEFAULT 256
212
Nimrod Andy79f33912014-06-12 08:16:23 +0800213#define TSO_HEADER_SIZE 128
214/* Max number of allowed TCP segments for software TSO */
215#define FEC_MAX_TSO_SEGS 100
216#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
217
218#define IS_TSO_HEADER(txq, addr) \
219 ((addr >= txq->tso_hdrs_dma) && \
Troy Kisky7355f272016-02-05 14:52:46 -0700220 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
Nimrod Andy79f33912014-06-12 08:16:23 +0800221
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000222static int mii_cnt;
223
Troy Kisky7355f272016-02-05 14:52:46 -0700224static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
225 struct bufdesc_prop *bd)
Frank Liff43da82013-01-03 16:04:23 +0000226{
Troy Kisky7355f272016-02-05 14:52:46 -0700227 return (bdp >= bd->last) ? bd->base
228 : (struct bufdesc *)(((unsigned)bdp) + bd->dsize);
Frank Liff43da82013-01-03 16:04:23 +0000229}
230
Troy Kisky7355f272016-02-05 14:52:46 -0700231static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
232 struct bufdesc_prop *bd)
Frank Liff43da82013-01-03 16:04:23 +0000233{
Troy Kisky7355f272016-02-05 14:52:46 -0700234 return (bdp <= bd->base) ? bd->last
235 : (struct bufdesc *)(((unsigned)bdp) - bd->dsize);
Frank Liff43da82013-01-03 16:04:23 +0000236}
237
Troy Kisky7355f272016-02-05 14:52:46 -0700238static int fec_enet_get_bd_index(struct bufdesc *bdp,
239 struct bufdesc_prop *bd)
Nimrod Andy61a44272014-06-12 08:16:18 +0800240{
Troy Kisky7355f272016-02-05 14:52:46 -0700241 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
Nimrod Andy61a44272014-06-12 08:16:18 +0800242}
243
Troy Kisky7355f272016-02-05 14:52:46 -0700244static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800245{
246 int entries;
247
Troy Kisky7355f272016-02-05 14:52:46 -0700248 entries = (((const char *)txq->dirty_tx -
249 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800250
Troy Kisky7355f272016-02-05 14:52:46 -0700251 return entries >= 0 ? entries : entries + txq->bd.ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800252}
253
Lothar Waßmannc20e5992014-11-17 10:51:24 +0100254static void swap_buffer(void *bufaddr, int len)
Shawn Guob5680e02011-01-05 21:13:13 +0000255{
256 int i;
257 unsigned int *buf = bufaddr;
258
Lothar Waßmann7b487d02014-11-17 10:51:23 +0100259 for (i = 0; i < len; i += 4, buf++)
Lothar Waßmanne4537892014-11-17 10:51:22 +0100260 swab32s(buf);
Shawn Guob5680e02011-01-05 21:13:13 +0000261}
262
Lothar Waßmann1310b542014-11-07 10:02:47 +0100263static void swap_buffer2(void *dst_buf, void *src_buf, int len)
264{
265 int i;
266 unsigned int *src = src_buf;
267 unsigned int *dst = dst_buf;
268
269 for (i = 0; i < len; i += 4, src++, dst++)
270 *dst = swab32p(src);
271}
272
Russell King344756f2014-07-08 13:01:59 +0100273static void fec_dump(struct net_device *ndev)
274{
275 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800276 struct bufdesc *bdp;
277 struct fec_enet_priv_tx_q *txq;
278 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100279
280 netdev_info(ndev, "TX ring dump\n");
281 pr_info("Nr SC addr len SKB\n");
282
Fugang Duan4d494cd2014-09-13 05:00:48 +0800283 txq = fep->tx_queue[0];
Troy Kisky7355f272016-02-05 14:52:46 -0700284 bdp = txq->bd.base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800285
Russell King344756f2014-07-08 13:01:59 +0100286 do {
Johannes Berg5cfa3032016-01-24 16:52:37 +0100287 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
Russell King344756f2014-07-08 13:01:59 +0100288 index,
Troy Kisky7355f272016-02-05 14:52:46 -0700289 bdp == txq->bd.cur ? 'S' : ' ',
Fugang Duan4d494cd2014-09-13 05:00:48 +0800290 bdp == txq->dirty_tx ? 'H' : ' ',
Johannes Berg5cfa3032016-01-24 16:52:37 +0100291 fec16_to_cpu(bdp->cbd_sc),
292 fec32_to_cpu(bdp->cbd_bufaddr),
293 fec16_to_cpu(bdp->cbd_datlen),
Fugang Duan4d494cd2014-09-13 05:00:48 +0800294 txq->tx_skbuff[index]);
Troy Kisky7355f272016-02-05 14:52:46 -0700295 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Russell King344756f2014-07-08 13:01:59 +0100296 index++;
Troy Kisky7355f272016-02-05 14:52:46 -0700297 } while (bdp != txq->bd.base);
Russell King344756f2014-07-08 13:01:59 +0100298}
299
Fugang Duan62a02c92014-06-18 08:33:52 +0800300static inline bool is_ipv4_pkt(struct sk_buff *skb)
301{
302 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
303}
304
Jim Baxter4c09eed2013-04-19 08:10:49 +0000305static int
306fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
307{
308 /* Only run for packets requiring a checksum. */
309 if (skb->ip_summed != CHECKSUM_PARTIAL)
310 return 0;
311
312 if (unlikely(skb_cow_head(skb, 0)))
313 return -1;
314
Fugang Duan62a02c92014-06-18 08:33:52 +0800315 if (is_ipv4_pkt(skb))
316 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000317 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
318
319 return 0;
320}
321
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800322static struct bufdesc *
Fugang Duan4d494cd2014-09-13 05:00:48 +0800323fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
324 struct sk_buff *skb,
325 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800326{
327 struct fec_enet_private *fep = netdev_priv(ndev);
Troy Kisky7355f272016-02-05 14:52:46 -0700328 struct bufdesc *bdp = txq->bd.cur;
Nimrod Andy6e909282014-06-12 08:16:22 +0800329 struct bufdesc_ex *ebdp;
330 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800331 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800332 int frag, frag_len;
333 unsigned short status;
334 unsigned int estatus = 0;
335 skb_frag_t *this_frag;
336 unsigned int index;
337 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100338 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800339 int i;
340
341 for (frag = 0; frag < nr_frags; frag++) {
342 this_frag = &skb_shinfo(skb)->frags[frag];
Troy Kisky7355f272016-02-05 14:52:46 -0700343 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800344 ebdp = (struct bufdesc_ex *)bdp;
345
Johannes Berg5cfa3032016-01-24 16:52:37 +0100346 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800347 status &= ~BD_ENET_TX_STATS;
348 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
349 frag_len = skb_shinfo(skb)->frags[frag].size;
350
351 /* Handle the last BD specially */
352 if (frag == nr_frags - 1) {
353 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
354 if (fep->bufdesc_ex) {
355 estatus |= BD_ENET_TX_INT;
356 if (unlikely(skb_shinfo(skb)->tx_flags &
357 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
358 estatus |= BD_ENET_TX_TS;
359 }
360 }
361
362 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100363 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800364 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800365 if (skb->ip_summed == CHECKSUM_PARTIAL)
366 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
367 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100368 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800369 }
370
371 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
372
Troy Kisky7355f272016-02-05 14:52:46 -0700373 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800374 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100375 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800376 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
377 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800378
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100379 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800380 swap_buffer(bufaddr, frag_len);
381 }
382
Russell Kingd6bf3142014-07-08 00:23:19 +0100383 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
384 DMA_TO_DEVICE);
385 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800386 dev_kfree_skb_any(skb);
387 if (net_ratelimit())
388 netdev_err(ndev, "Tx DMA memory map failed\n");
389 goto dma_mapping_error;
390 }
391
Johannes Berg5cfa3032016-01-24 16:52:37 +0100392 bdp->cbd_bufaddr = cpu_to_fec32(addr);
393 bdp->cbd_datlen = cpu_to_fec16(frag_len);
394 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800395 }
396
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800397 return bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800398dma_mapping_error:
Troy Kisky7355f272016-02-05 14:52:46 -0700399 bdp = txq->bd.cur;
Nimrod Andy6e909282014-06-12 08:16:22 +0800400 for (i = 0; i < frag; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -0700401 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100402 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
403 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
Nimrod Andy6e909282014-06-12 08:16:22 +0800404 }
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800405 return ERR_PTR(-ENOMEM);
Nimrod Andy6e909282014-06-12 08:16:22 +0800406}
407
Fugang Duan4d494cd2014-09-13 05:00:48 +0800408static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
409 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800410{
411 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800412 int nr_frags = skb_shinfo(skb)->nr_frags;
413 struct bufdesc *bdp, *last_bdp;
414 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100415 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800416 unsigned short status;
417 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800418 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800419 unsigned int estatus = 0;
420 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800421 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800422
Troy Kisky7355f272016-02-05 14:52:46 -0700423 entries_free = fec_enet_get_free_txdesc_num(txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800424 if (entries_free < MAX_SKB_FRAGS + 1) {
425 dev_kfree_skb_any(skb);
426 if (net_ratelimit())
427 netdev_err(ndev, "NOT enough BD for SG!\n");
428 return NETDEV_TX_OK;
429 }
430
Nimrod Andy6e909282014-06-12 08:16:22 +0800431 /* Protocol checksum off-load for TCP and UDP. */
432 if (fec_enet_clear_csum(skb, ndev)) {
433 dev_kfree_skb_any(skb);
434 return NETDEV_TX_OK;
435 }
436
437 /* Fill in a Tx ring entry */
Troy Kisky7355f272016-02-05 14:52:46 -0700438 bdp = txq->bd.cur;
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800439 last_bdp = bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100440 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800441 status &= ~BD_ENET_TX_STATS;
442
443 /* Set buffer length and buffer pointer */
444 bufaddr = skb->data;
445 buflen = skb_headlen(skb);
446
Fugang Duan4d494cd2014-09-13 05:00:48 +0800447 queue = skb_get_queue_mapping(skb);
Troy Kisky7355f272016-02-05 14:52:46 -0700448 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800449 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100450 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800451 memcpy(txq->tx_bounce[index], skb->data, buflen);
452 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800453
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100454 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800455 swap_buffer(bufaddr, buflen);
456 }
457
Russell Kingd6bf3142014-07-08 00:23:19 +0100458 /* Push the data cache so the CPM does not get stale memory data. */
459 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
460 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800461 dev_kfree_skb_any(skb);
462 if (net_ratelimit())
463 netdev_err(ndev, "Tx DMA memory map failed\n");
464 return NETDEV_TX_OK;
465 }
466
467 if (nr_frags) {
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800468 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
469 if (IS_ERR(last_bdp))
470 return NETDEV_TX_OK;
Nimrod Andy6e909282014-06-12 08:16:22 +0800471 } else {
472 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
473 if (fep->bufdesc_ex) {
474 estatus = BD_ENET_TX_INT;
475 if (unlikely(skb_shinfo(skb)->tx_flags &
476 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
477 estatus |= BD_ENET_TX_TS;
478 }
479 }
480
481 if (fep->bufdesc_ex) {
482
483 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
484
485 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
486 fep->hwts_tx_en))
487 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
488
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100489 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800490 estatus |= FEC_TX_BD_FTYPE(queue);
491
Nimrod Andy6e909282014-06-12 08:16:22 +0800492 if (skb->ip_summed == CHECKSUM_PARTIAL)
493 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
494
495 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100496 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800497 }
498
Troy Kisky7355f272016-02-05 14:52:46 -0700499 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800500 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800501 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800502
Johannes Berg5cfa3032016-01-24 16:52:37 +0100503 bdp->cbd_datlen = cpu_to_fec16(buflen);
504 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Nimrod Andy6e909282014-06-12 08:16:22 +0800505
506 /* Send it on its way. Tell FEC it's ready, interrupt when done,
507 * it's the last BD of the frame, and to put the CRC on the end.
508 */
509 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100510 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800511
Sascha Hauer22f6b862009-04-15 01:32:18 +0000512 /* If this was the last BD in the ring, start at the beginning again. */
Troy Kisky7355f272016-02-05 14:52:46 -0700513 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800515 skb_tx_timestamp(skb);
516
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800517 /* Make sure the update to bdp and tx_skbuff are performed before
Troy Kisky7355f272016-02-05 14:52:46 -0700518 * txq->bd.cur.
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800519 */
520 wmb();
Troy Kisky7355f272016-02-05 14:52:46 -0700521 txq->bd.cur = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Frank Lide5fb0a2013-03-03 17:34:25 +0000523 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800524 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000525
Nimrod Andy6e909282014-06-12 08:16:22 +0800526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Nimrod Andy79f33912014-06-12 08:16:23 +0800529static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800530fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
531 struct net_device *ndev,
532 struct bufdesc *bdp, int index, char *data,
533 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800534{
535 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200536 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800537 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800538 unsigned short status;
539 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100540 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800541
Johannes Berg5cfa3032016-01-24 16:52:37 +0100542 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800543 status &= ~BD_ENET_TX_STATS;
544
545 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800546
Fugang Duan41ef84c2014-09-13 05:00:54 +0800547 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100548 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800549 memcpy(txq->tx_bounce[index], data, size);
550 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800551
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100552 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800553 swap_buffer(data, size);
554 }
555
Russell Kingd6bf3142014-07-08 00:23:19 +0100556 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
557 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800558 dev_kfree_skb_any(skb);
559 if (net_ratelimit())
560 netdev_err(ndev, "Tx DMA memory map failed\n");
561 return NETDEV_TX_BUSY;
562 }
563
Johannes Berg5cfa3032016-01-24 16:52:37 +0100564 bdp->cbd_datlen = cpu_to_fec16(size);
565 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Russell Kingd6bf3142014-07-08 00:23:19 +0100566
Nimrod Andy79f33912014-06-12 08:16:23 +0800567 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100568 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800569 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800570 if (skb->ip_summed == CHECKSUM_PARTIAL)
571 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
572 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100573 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800574 }
575
576 /* Handle the last BD specially */
577 if (last_tcp)
578 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
579 if (is_last) {
580 status |= BD_ENET_TX_INTR;
581 if (fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100582 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
Nimrod Andy79f33912014-06-12 08:16:23 +0800583 }
584
Johannes Berg5cfa3032016-01-24 16:52:37 +0100585 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800586
587 return 0;
588}
589
590static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800591fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
592 struct sk_buff *skb, struct net_device *ndev,
593 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800594{
595 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800596 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200597 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800598 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800599 void *bufaddr;
600 unsigned long dmabuf;
601 unsigned short status;
602 unsigned int estatus = 0;
603
Johannes Berg5cfa3032016-01-24 16:52:37 +0100604 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800605 status &= ~BD_ENET_TX_STATS;
606 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
607
Fugang Duan4d494cd2014-09-13 05:00:48 +0800608 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
609 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800610 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100611 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800612 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
613 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800614
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100615 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800616 swap_buffer(bufaddr, hdr_len);
617
618 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
619 hdr_len, DMA_TO_DEVICE);
620 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
621 dev_kfree_skb_any(skb);
622 if (net_ratelimit())
623 netdev_err(ndev, "Tx DMA memory map failed\n");
624 return NETDEV_TX_BUSY;
625 }
626 }
627
Johannes Berg5cfa3032016-01-24 16:52:37 +0100628 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
629 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
Nimrod Andy79f33912014-06-12 08:16:23 +0800630
631 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100632 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800633 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800634 if (skb->ip_summed == CHECKSUM_PARTIAL)
635 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
636 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100637 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800638 }
639
Johannes Berg5cfa3032016-01-24 16:52:37 +0100640 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800641
642 return 0;
643}
644
Fugang Duan4d494cd2014-09-13 05:00:48 +0800645static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
646 struct sk_buff *skb,
647 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800648{
649 struct fec_enet_private *fep = netdev_priv(ndev);
650 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
651 int total_len, data_left;
Troy Kisky7355f272016-02-05 14:52:46 -0700652 struct bufdesc *bdp = txq->bd.cur;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800653 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800654 struct tso_t tso;
655 unsigned int index = 0;
656 int ret;
657
Troy Kisky7355f272016-02-05 14:52:46 -0700658 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800659 dev_kfree_skb_any(skb);
660 if (net_ratelimit())
661 netdev_err(ndev, "NOT enough BD for TSO!\n");
662 return NETDEV_TX_OK;
663 }
664
665 /* Protocol checksum off-load for TCP and UDP. */
666 if (fec_enet_clear_csum(skb, ndev)) {
667 dev_kfree_skb_any(skb);
668 return NETDEV_TX_OK;
669 }
670
671 /* Initialize the TSO handler, and prepare the first payload */
672 tso_start(skb, &tso);
673
674 total_len = skb->len - hdr_len;
675 while (total_len > 0) {
676 char *hdr;
677
Troy Kisky7355f272016-02-05 14:52:46 -0700678 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800679 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
680 total_len -= data_left;
681
682 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800683 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800684 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800685 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800686 if (ret)
687 goto err_release;
688
689 while (data_left > 0) {
690 int size;
691
692 size = min_t(int, tso.size, data_left);
Troy Kisky7355f272016-02-05 14:52:46 -0700693 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
694 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800695 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
696 bdp, index,
697 tso.data, size,
698 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800699 total_len == 0);
700 if (ret)
701 goto err_release;
702
703 data_left -= size;
704 tso_build_data(skb, &tso, size);
705 }
706
Troy Kisky7355f272016-02-05 14:52:46 -0700707 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800708 }
709
710 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800711 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800712
Nimrod Andy79f33912014-06-12 08:16:23 +0800713 skb_tx_timestamp(skb);
Troy Kisky7355f272016-02-05 14:52:46 -0700714 txq->bd.cur = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800715
716 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100717 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Fugang Duan37d60172014-09-17 05:18:54 +0800718 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
719 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
720 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
721 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
722 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800723
724 return 0;
725
726err_release:
727 /* TODO: Release all used data descriptors for TSO */
728 return ret;
729}
730
Nimrod Andy61a44272014-06-12 08:16:18 +0800731static netdev_tx_t
732fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
733{
734 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800735 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800736 unsigned short queue;
737 struct fec_enet_priv_tx_q *txq;
738 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800739 int ret;
740
Fugang Duan4d494cd2014-09-13 05:00:48 +0800741 queue = skb_get_queue_mapping(skb);
742 txq = fep->tx_queue[queue];
743 nq = netdev_get_tx_queue(ndev, queue);
744
Nimrod Andy79f33912014-06-12 08:16:23 +0800745 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800746 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800747 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800748 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800749 if (ret)
750 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800751
Troy Kisky7355f272016-02-05 14:52:46 -0700752 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800753 if (entries_free <= txq->tx_stop_threshold)
754 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800755
756 return NETDEV_TX_OK;
757}
758
Frank Li14109a52013-03-26 16:12:03 +0000759/* Init RX & TX buffer descriptors
760 */
761static void fec_enet_bd_init(struct net_device *dev)
762{
763 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800764 struct fec_enet_priv_tx_q *txq;
765 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000766 struct bufdesc *bdp;
767 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800768 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000769
Frank Li59d0f7462014-09-13 05:00:50 +0800770 for (q = 0; q < fep->num_rx_queues; q++) {
771 /* Initialize the receive buffer descriptors. */
772 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700773 bdp = rxq->bd.base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800774
Troy Kisky7355f272016-02-05 14:52:46 -0700775 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000776
Frank Li59d0f7462014-09-13 05:00:50 +0800777 /* Initialize the BD for every fragment in the page. */
778 if (bdp->cbd_bufaddr)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100779 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Li59d0f7462014-09-13 05:00:50 +0800780 else
Johannes Berg5cfa3032016-01-24 16:52:37 +0100781 bdp->cbd_sc = cpu_to_fec16(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700782 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Frank Li14109a52013-03-26 16:12:03 +0000783 }
Frank Li59d0f7462014-09-13 05:00:50 +0800784
785 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700786 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100787 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800788
Troy Kisky7355f272016-02-05 14:52:46 -0700789 rxq->bd.cur = rxq->bd.base;
Frank Li14109a52013-03-26 16:12:03 +0000790 }
791
Frank Li59d0f7462014-09-13 05:00:50 +0800792 for (q = 0; q < fep->num_tx_queues; q++) {
793 /* ...and the same for transmit */
794 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700795 bdp = txq->bd.base;
796 txq->bd.cur = bdp;
Frank Li59d0f7462014-09-13 05:00:50 +0800797
Troy Kisky7355f272016-02-05 14:52:46 -0700798 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800799 /* Initialize the BD for every fragment in the page. */
Johannes Berg5cfa3032016-01-24 16:52:37 +0100800 bdp->cbd_sc = cpu_to_fec16(0);
Frank Li59d0f7462014-09-13 05:00:50 +0800801 if (txq->tx_skbuff[i]) {
802 dev_kfree_skb_any(txq->tx_skbuff[i]);
803 txq->tx_skbuff[i] = NULL;
804 }
Johannes Berg5cfa3032016-01-24 16:52:37 +0100805 bdp->cbd_bufaddr = cpu_to_fec32(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700806 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Li59d0f7462014-09-13 05:00:50 +0800807 }
808
809 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700810 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100811 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800812 txq->dirty_tx = bdp;
813 }
814}
815
Frank Lice99d0d2014-09-13 05:00:52 +0800816static void fec_enet_active_rxring(struct net_device *ndev)
817{
818 struct fec_enet_private *fep = netdev_priv(ndev);
819 int i;
820
821 for (i = 0; i < fep->num_rx_queues; i++)
822 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
823}
824
Frank Li59d0f7462014-09-13 05:00:50 +0800825static void fec_enet_enable_ring(struct net_device *ndev)
826{
827 struct fec_enet_private *fep = netdev_priv(ndev);
828 struct fec_enet_priv_tx_q *txq;
829 struct fec_enet_priv_rx_q *rxq;
830 int i;
831
832 for (i = 0; i < fep->num_rx_queues; i++) {
833 rxq = fep->rx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700834 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
Nimrod Andyd543a762014-11-23 17:23:06 +0800835 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800836
837 /* enable DMA1/2 */
838 if (i)
839 writel(RCMR_MATCHEN | RCMR_CMP(i),
840 fep->hwp + FEC_RCMR(i));
841 }
842
843 for (i = 0; i < fep->num_tx_queues; i++) {
844 txq = fep->tx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700845 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800846
847 /* enable DMA1/2 */
848 if (i)
849 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
850 fep->hwp + FEC_DMA_CFG(i));
851 }
852}
853
854static void fec_enet_reset_skb(struct net_device *ndev)
855{
856 struct fec_enet_private *fep = netdev_priv(ndev);
857 struct fec_enet_priv_tx_q *txq;
858 int i, j;
859
860 for (i = 0; i < fep->num_tx_queues; i++) {
861 txq = fep->tx_queue[i];
862
Troy Kisky7355f272016-02-05 14:52:46 -0700863 for (j = 0; j < txq->bd.ring_size; j++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800864 if (txq->tx_skbuff[j]) {
865 dev_kfree_skb_any(txq->tx_skbuff[j]);
866 txq->tx_skbuff[j] = NULL;
867 }
868 }
869 }
Frank Li14109a52013-03-26 16:12:03 +0000870}
871
Russell Kingdbc64a82014-07-08 12:40:12 +0100872/*
873 * This function is called to start or restart the FEC during a link
874 * change, transmit timeout, or to reconfigure the FEC. The network
875 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100876 */
877static void
Russell Kingef833372014-07-08 12:40:43 +0100878fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100879{
880 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000881 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100882 u32 temp_mac[2];
883 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000884 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100885
Fugang Duan106c3142014-09-13 05:00:51 +0800886 /* Whack a reset. We should wait for this.
887 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
888 * instead of reset MAC itself.
889 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100890 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800891 writel(0, fep->hwp + FEC_ECNTRL);
892 } else {
893 writel(1, fep->hwp + FEC_ECNTRL);
894 udelay(10);
895 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100896
897 /*
898 * enet-mac reset will reset mac address registers too,
899 * so need to reconfigure it.
900 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100901 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100902 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100903 writel((__force u32)cpu_to_be32(temp_mac[0]),
904 fep->hwp + FEC_ADDR_LOW);
905 writel((__force u32)cpu_to_be32(temp_mac[1]),
906 fep->hwp + FEC_ADDR_HIGH);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100907 }
908
909 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800910 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100911
Frank Li14109a52013-03-26 16:12:03 +0000912 fec_enet_bd_init(ndev);
913
Frank Li59d0f7462014-09-13 05:00:50 +0800914 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100915
Frank Li59d0f7462014-09-13 05:00:50 +0800916 /* Reset tx SKB buffers. */
917 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100918
919 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100920 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100921 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100922 writel(0x04, fep->hwp + FEC_X_CNTRL);
923 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100924 /* No Rcv on Xmit */
925 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100926 writel(0x0, fep->hwp + FEC_X_CNTRL);
927 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100928
Uwe Kleine-König45993652011-01-19 20:47:04 +0100929 /* Set MII speed */
930 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
931
Guenter Roeckd1391932013-06-18 10:04:59 -0700932#if !defined(CONFIG_M5272)
Greg Ungerer18803492015-06-20 15:51:57 +1000933 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
934 /* set RX checksum */
935 val = readl(fep->hwp + FEC_RACC);
936 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
937 val |= FEC_RACC_OPTIONS;
938 else
939 val &= ~FEC_RACC_OPTIONS;
940 writel(val, fep->hwp + FEC_RACC);
941 }
Troy Kisky55cd48c2016-02-05 14:52:43 -0700942 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
Guenter Roeckd1391932013-06-18 10:04:59 -0700943#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000944
Uwe Kleine-König45993652011-01-19 20:47:04 +0100945 /*
946 * The phy interface and speed need to get configured
947 * differently on enet-mac.
948 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100949 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100950 /* Enable flow control and length check */
951 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100952
Shawn Guo230dec62011-09-23 02:12:48 +0000953 /* RGMII, RMII or MII */
Markus Pargmanne813bb22015-04-30 17:07:50 +0200954 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
955 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
956 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
957 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shawn Guo230dec62011-09-23 02:12:48 +0000958 rcntl |= (1 << 6);
959 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100960 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100961 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100962 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100963
Shawn Guo230dec62011-09-23 02:12:48 +0000964 /* 1G, 100M or 10M */
965 if (fep->phy_dev) {
966 if (fep->phy_dev->speed == SPEED_1000)
967 ecntl |= (1 << 5);
968 else if (fep->phy_dev->speed == SPEED_100)
969 rcntl &= ~(1 << 9);
970 else
971 rcntl |= (1 << 9);
972 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100973 } else {
974#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100975 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000976 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100977 /* disable the gasket and wait */
978 writel(0, fep->hwp + FEC_MIIGSK_ENR);
979 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
980 udelay(1);
981
982 /*
983 * configure the gasket:
984 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800985 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100986 */
Eric Benard8d82f212012-01-12 06:10:28 +0000987 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
988 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
989 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
990 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
991 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100992
993 /* re-enable the gasket */
994 writel(2, fep->hwp + FEC_MIIGSK_ENR);
995 }
996#endif
997 }
Frank Libaa70a52013-01-16 16:55:58 +0000998
Guenter Roeckd1391932013-06-18 10:04:59 -0700999#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001000 /* enable pause frame*/
1001 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1002 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1003 fep->phy_dev && fep->phy_dev->pause)) {
1004 rcntl |= FEC_ENET_FCE;
1005
Jim Baxter4c09eed2013-04-19 08:10:49 +00001006 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001007 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1008 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1009 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1010 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1011
1012 /* OPD */
1013 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1014 } else {
1015 rcntl &= ~FEC_ENET_FCE;
1016 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001017#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001018
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001019 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020
Stefan Wahren84fe6182014-03-12 11:28:19 +01001021 /* Setup multicast filter. */
1022 set_multicast_list(ndev);
1023#ifndef CONFIG_M5272
1024 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1025 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1026#endif
1027
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001028 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001029 /* enable ENET endian swap */
1030 ecntl |= (1 << 8);
1031 /* enable ENET store and forward mode */
1032 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1033 }
1034
Frank Liff43da82013-01-03 16:04:23 +00001035 if (fep->bufdesc_ex)
1036 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001037
Chris Healy38ae92d2013-06-25 23:18:52 -07001038#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001039 /* Enable the MIB statistic event counters */
1040 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001041#endif
1042
Uwe Kleine-König45993652011-01-19 20:47:04 +01001043 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001044 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001045 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001046
Frank Liff43da82013-01-03 16:04:23 +00001047 if (fep->bufdesc_ex)
1048 fec_ptp_start_cyclecounter(ndev);
1049
Uwe Kleine-König45993652011-01-19 20:47:04 +01001050 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001051 if (fep->link)
1052 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1053 else
1054 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001055
1056 /* Init the interrupt coalescing */
1057 fec_enet_itr_coal_init(ndev);
1058
Uwe Kleine-König45993652011-01-19 20:47:04 +01001059}
1060
1061static void
1062fec_stop(struct net_device *ndev)
1063{
1064 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001065 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001066 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001067 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001068
1069 /* We cannot expect a graceful transmit stop without link !!! */
1070 if (fep->link) {
1071 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1072 udelay(10);
1073 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001074 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001075 }
1076
Fugang Duan106c3142014-09-13 05:00:51 +08001077 /* Whack a reset. We should wait for this.
1078 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1079 * instead of reset MAC itself.
1080 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001081 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1082 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1083 writel(0, fep->hwp + FEC_ECNTRL);
1084 } else {
1085 writel(1, fep->hwp + FEC_ECNTRL);
1086 udelay(10);
1087 }
1088 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001089 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001090 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1091 val = readl(fep->hwp + FEC_ECNTRL);
1092 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1093 writel(val, fep->hwp + FEC_ECNTRL);
1094
1095 if (pdata && pdata->sleep_mode_enable)
1096 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001097 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001098 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001099
1100 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001101 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1102 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001103 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001104 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1105 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001106}
1107
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001110fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001112 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Russell King344756f2014-07-08 13:01:59 +01001114 fec_dump(ndev);
1115
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001116 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Russell King36cdc742014-07-08 13:01:44 +01001118 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001119}
1120
Russell King36cdc742014-07-08 13:01:44 +01001121static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001122{
1123 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001124 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001125 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001126
Russell King36cdc742014-07-08 13:01:44 +01001127 rtnl_lock();
1128 if (netif_device_present(ndev) || netif_running(ndev)) {
1129 napi_disable(&fep->napi);
1130 netif_tx_lock_bh(ndev);
1131 fec_restart(ndev);
1132 netif_wake_queue(ndev);
1133 netif_tx_unlock_bh(ndev);
1134 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001135 }
Russell King36cdc742014-07-08 13:01:44 +01001136 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001140fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1141 struct skb_shared_hwtstamps *hwtstamps)
1142{
1143 unsigned long flags;
1144 u64 ns;
1145
1146 spin_lock_irqsave(&fep->tmreg_lock, flags);
1147 ns = timecounter_cyc2time(&fep->tc, ts);
1148 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1149
1150 memset(hwtstamps, 0, sizeof(*hwtstamps));
1151 hwtstamps->hwtstamp = ns_to_ktime(ns);
1152}
1153
1154static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001155fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct fec_enet_private *fep;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001158 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001159 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001161 struct fec_enet_priv_tx_q *txq;
1162 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001163 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001164 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001166 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001167
1168 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1169
1170 txq = fep->tx_queue[queue_id];
1171 /* get next bdp of dirty_tx */
1172 nq = netdev_get_tx_queue(ndev, queue_id);
1173 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Frank Lide5fb0a2013-03-03 17:34:25 +00001175 /* get next bdp of dirty_tx */
Troy Kisky7355f272016-02-05 14:52:46 -07001176 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Lide5fb0a2013-03-03 17:34:25 +00001177
Troy Kisky7355f272016-02-05 14:52:46 -07001178 while (bdp != READ_ONCE(txq->bd.cur)) {
1179 /* Order the load of bd.cur and cbd_sc */
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001180 rmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +01001181 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001182 if (status & BD_ENET_TX_READY)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001183 break;
1184
Troy Kisky7355f272016-02-05 14:52:46 -07001185 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy2b995f62015-01-20 14:10:35 +08001186
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001187 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001188 txq->tx_skbuff[index] = NULL;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001189 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1190 dma_unmap_single(&fep->pdev->dev,
1191 fec32_to_cpu(bdp->cbd_bufaddr),
1192 fec16_to_cpu(bdp->cbd_datlen),
1193 DMA_TO_DEVICE);
1194 bdp->cbd_bufaddr = cpu_to_fec32(0);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001195 if (!skb) {
Troy Kisky7355f272016-02-05 14:52:46 -07001196 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001197 continue;
1198 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001201 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 BD_ENET_TX_RL | BD_ENET_TX_UN |
1203 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001204 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001205 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001206 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001207 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001208 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001209 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001210 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001211 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001212 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001213 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001214 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001216 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001217 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
Frank Liff43da82013-01-03 16:04:23 +00001220 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1221 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001222 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001223 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001224
Johannes Berg5cfa3032016-01-24 16:52:37 +01001225 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001226 skb_tstamp_tx(skb, &shhwtstamps);
1227 }
Frank Liff43da82013-01-03 16:04:23 +00001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Deferred means some collisions occurred during transmit,
1230 * but we eventually sent the packet OK.
1231 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001232 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001233 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001234
Sascha Hauer22f6b862009-04-15 01:32:18 +00001235 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001237
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001238 /* Make sure the update to bdp and tx_skbuff are performed
1239 * before dirty_tx
1240 */
1241 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +08001242 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001243
Sascha Hauer22f6b862009-04-15 01:32:18 +00001244 /* Update pointer to next buffer descriptor to be transmitted */
Troy Kisky7355f272016-02-05 14:52:46 -07001245 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001246
Sascha Hauer22f6b862009-04-15 01:32:18 +00001247 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001249 if (netif_queue_stopped(ndev)) {
Troy Kisky7355f272016-02-05 14:52:46 -07001250 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001251 if (entries_free >= txq->tx_wake_threshold)
1252 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Russell Kingccea2962014-07-08 13:01:38 +01001255
1256 /* ERR006538: Keep the transmitter going */
Troy Kisky7355f272016-02-05 14:52:46 -07001257 if (bdp != txq->bd.cur &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001258 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1259 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1260}
1261
1262static void
1263fec_enet_tx(struct net_device *ndev)
1264{
1265 struct fec_enet_private *fep = netdev_priv(ndev);
1266 u16 queue_id;
1267 /* First process class A queue, then Class B and Best Effort queue */
1268 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1269 clear_bit(queue_id, &fep->work_tx);
1270 fec_enet_tx_queue(ndev, queue_id);
1271 }
1272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001275static int
1276fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1277{
1278 struct fec_enet_private *fep = netdev_priv(ndev);
1279 int off;
1280
1281 off = ((unsigned long)skb->data) & fep->rx_align;
1282 if (off)
1283 skb_reserve(skb, fep->rx_align + 1 - off);
1284
Johannes Berg5cfa3032016-01-24 16:52:37 +01001285 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1286 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001287 if (net_ratelimit())
1288 netdev_err(ndev, "Rx DMA memory map failed\n");
1289 return -ENOMEM;
1290 }
1291
1292 return 0;
1293}
1294
1295static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001296 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001297{
1298 struct fec_enet_private *fep = netdev_priv(ndev);
1299 struct sk_buff *new_skb;
1300
1301 if (length > fep->rx_copybreak)
1302 return false;
1303
1304 new_skb = netdev_alloc_skb(ndev, length);
1305 if (!new_skb)
1306 return false;
1307
Johannes Berg5cfa3032016-01-24 16:52:37 +01001308 dma_sync_single_for_cpu(&fep->pdev->dev,
1309 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001310 FEC_ENET_RX_FRSIZE - fep->rx_align,
1311 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001312 if (!swap)
1313 memcpy(new_skb->data, (*skb)->data, length);
1314 else
1315 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001316 *skb = new_skb;
1317
1318 return true;
1319}
1320
Troy Kisky7355f272016-02-05 14:52:46 -07001321/* During a receive, the bd_rx.cur points to the current incoming buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * When we update through the ring, if the next incoming buffer has
1323 * not been given to the system, we just set the empty indicator,
1324 * effectively tossing the packet.
1325 */
Frank Lidc975382013-01-28 18:31:42 +00001326static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001327fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001329 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001330 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001331 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001332 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001333 struct sk_buff *skb_new = NULL;
1334 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ushort pkt_len;
1336 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001337 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001338 struct bufdesc_ex *ebdp = NULL;
1339 bool vlan_packet_rcvd = false;
1340 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001341 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001342 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001343 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001344
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001345#ifdef CONFIG_M532x
1346 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001347#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001348 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1349 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* First, grab all of the stats for the incoming packet.
1352 * These get messed up if we get called due to a busy condition.
1353 */
Troy Kisky7355f272016-02-05 14:52:46 -07001354 bdp = rxq->bd.cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Johannes Berg5cfa3032016-01-24 16:52:37 +01001356 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Frank Lidc975382013-01-28 18:31:42 +00001358 if (pkt_received >= budget)
1359 break;
1360 pkt_received++;
1361
Russell Kinged63f1d2015-09-02 17:24:14 +08001362 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
Russell Kingdb3421c2014-07-08 13:01:49 +01001363
Sascha Hauer22f6b862009-04-15 01:32:18 +00001364 /* Check for errors. */
Troy Kisky095098e2016-02-05 14:52:44 -07001365 status ^= BD_ENET_RX_LAST;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001366 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Troy Kisky095098e2016-02-05 14:52:44 -07001367 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1368 BD_ENET_RX_CL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001369 ndev->stats.rx_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001370 if (status & BD_ENET_RX_OV) {
1371 /* FIFO overrun */
1372 ndev->stats.rx_fifo_errors++;
1373 goto rx_processing_done;
1374 }
1375 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1376 | BD_ENET_RX_LAST)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +00001377 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001378 ndev->stats.rx_length_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001379 if (status & BD_ENET_RX_LAST)
1380 netdev_err(ndev, "rcv is not +last\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001381 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001382 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001383 ndev->stats.rx_crc_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001384 /* Report late collisions as a frame error. */
1385 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1386 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001387 goto rx_processing_done;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Sascha Hauer22f6b862009-04-15 01:32:18 +00001390 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001391 ndev->stats.rx_packets++;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001392 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001393 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Troy Kisky7355f272016-02-05 14:52:46 -07001395 index = fec_enet_get_bd_index(bdp, &rxq->bd);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001396 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001397
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001398 /* The packet length includes FCS, but we don't want to
1399 * include that when passing upstream as it messes up
1400 * bridging applications.
1401 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001402 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1403 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001404 if (!is_copybreak) {
1405 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1406 if (unlikely(!skb_new)) {
1407 ndev->stats.rx_dropped++;
1408 goto rx_processing_done;
1409 }
Johannes Berg5cfa3032016-01-24 16:52:37 +01001410 dma_unmap_single(&fep->pdev->dev,
1411 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001412 FEC_ENET_RX_FRSIZE - fep->rx_align,
1413 DMA_FROM_DEVICE);
1414 }
1415
1416 prefetch(skb->data - NET_IP_ALIGN);
1417 skb_put(skb, pkt_len - 4);
1418 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001419 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001420 swap_buffer(data, pkt_len);
1421
Jim Baxtercdffcf12013-07-02 22:52:56 +01001422 /* Extract the enhanced buffer descriptor */
1423 ebdp = NULL;
1424 if (fep->bufdesc_ex)
1425 ebdp = (struct bufdesc_ex *)bdp;
1426
1427 /* If this is a VLAN packet remove the VLAN Tag */
1428 vlan_packet_rcvd = false;
1429 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Johannes Berg5cfa3032016-01-24 16:52:37 +01001430 fep->bufdesc_ex &&
1431 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001432 /* Push and remove the vlan tag */
1433 struct vlan_hdr *vlan_header =
1434 (struct vlan_hdr *) (data + ETH_HLEN);
1435 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001436
1437 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001438
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001439 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001440 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001441 }
1442
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001443 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001445 /* Get receive timestamp from the skb */
1446 if (fep->hwts_rx_en && fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +01001447 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001448 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001449
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001450 if (fep->bufdesc_ex &&
1451 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001452 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001453 /* don't check it */
1454 skb->ip_summed = CHECKSUM_UNNECESSARY;
1455 } else {
1456 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001457 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001458 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001459
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001460 /* Handle received VLAN packets */
1461 if (vlan_packet_rcvd)
1462 __vlan_hwaccel_put_tag(skb,
1463 htons(ETH_P_8021Q),
1464 vlan_tag);
1465
1466 napi_gro_receive(&fep->napi, skb);
1467
1468 if (is_copybreak) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001469 dma_sync_single_for_device(&fep->pdev->dev,
1470 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001471 FEC_ENET_RX_FRSIZE - fep->rx_align,
1472 DMA_FROM_DEVICE);
1473 } else {
1474 rxq->rx_skbuff[index] = skb_new;
1475 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1476 }
1477
Sascha Hauer22f6b862009-04-15 01:32:18 +00001478rx_processing_done:
1479 /* Clear the status flags for this buffer */
1480 status &= ~BD_ENET_RX_STATS;
1481
1482 /* Mark the buffer empty */
1483 status |= BD_ENET_RX_EMPTY;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001484 bdp->cbd_sc = cpu_to_fec16(status);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001485
Frank Liff43da82013-01-03 16:04:23 +00001486 if (fep->bufdesc_ex) {
1487 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1488
Johannes Berg5cfa3032016-01-24 16:52:37 +01001489 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00001490 ebdp->cbd_prot = 0;
1491 ebdp->cbd_bdu = 0;
1492 }
Frank Li6605b732012-10-30 18:25:31 +00001493
Sascha Hauer22f6b862009-04-15 01:32:18 +00001494 /* Update BD pointer to next entry */
Troy Kisky7355f272016-02-05 14:52:46 -07001495 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001496
Sascha Hauer22f6b862009-04-15 01:32:18 +00001497 /* Doing this here will keep the FEC running while we process
1498 * incoming frames. On a heavily loaded network, we should be
1499 * able to keep up at the expense of system resources.
1500 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001501 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Troy Kisky7355f272016-02-05 14:52:46 -07001503 rxq->bd.cur = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001504 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Fugang Duan4d494cd2014-09-13 05:00:48 +08001507static int
1508fec_enet_rx(struct net_device *ndev, int budget)
1509{
1510 int pkt_received = 0;
1511 u16 queue_id;
1512 struct fec_enet_private *fep = netdev_priv(ndev);
1513
1514 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1515 clear_bit(queue_id, &fep->work_rx);
1516 pkt_received += fec_enet_rx_queue(ndev,
1517 budget - pkt_received, queue_id);
1518 }
1519 return pkt_received;
1520}
1521
1522static bool
1523fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1524{
1525 if (int_events == 0)
1526 return false;
1527
1528 if (int_events & FEC_ENET_RXF)
1529 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001530 if (int_events & FEC_ENET_RXF_1)
1531 fep->work_rx |= (1 << 0);
1532 if (int_events & FEC_ENET_RXF_2)
1533 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001534
1535 if (int_events & FEC_ENET_TXF)
1536 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001537 if (int_events & FEC_ENET_TXF_1)
1538 fep->work_tx |= (1 << 0);
1539 if (int_events & FEC_ENET_TXF_2)
1540 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001541
1542 return true;
1543}
1544
Uwe Kleine-König45993652011-01-19 20:47:04 +01001545static irqreturn_t
1546fec_enet_interrupt(int irq, void *dev_id)
1547{
1548 struct net_device *ndev = dev_id;
1549 struct fec_enet_private *fep = netdev_priv(ndev);
1550 uint int_events;
1551 irqreturn_t ret = IRQ_NONE;
1552
Russell King7a168072014-07-08 00:22:44 +01001553 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001554 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001555 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001556
Fugang Duan61615cd2015-03-04 07:52:11 +08001557 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001558 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001559
Nimrod Andy94191fd2014-12-16 18:25:58 +08001560 if (napi_schedule_prep(&fep->napi)) {
1561 /* Disable the NAPI interrupts */
1562 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1563 __napi_schedule(&fep->napi);
1564 }
Russell King7a168072014-07-08 00:22:44 +01001565 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001566
Russell King7a168072014-07-08 00:22:44 +01001567 if (int_events & FEC_ENET_MII) {
1568 ret = IRQ_HANDLED;
1569 complete(&fep->mdio_done);
1570 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001571
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001572 if (fep->ptp_clock)
1573 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001574
Uwe Kleine-König45993652011-01-19 20:47:04 +01001575 return ret;
1576}
1577
Frank Lidc975382013-01-28 18:31:42 +00001578static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1579{
1580 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001581 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001582 int pkts;
1583
Russell King7a168072014-07-08 00:22:44 +01001584 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001585
Frank Lide5fb0a2013-03-03 17:34:25 +00001586 fec_enet_tx(ndev);
1587
Frank Lidc975382013-01-28 18:31:42 +00001588 if (pkts < budget) {
1589 napi_complete(napi);
1590 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1591 }
1592 return pkts;
1593}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001596static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001598 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001599 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001600 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Shawn Guo49da97d2011-01-05 21:13:11 +00001602 /*
1603 * try to get mac address in following order:
1604 *
1605 * 1) module parameter via kernel command line in form
1606 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1607 */
1608 iap = macaddr;
1609
1610 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001611 * 2) from device tree data
1612 */
1613 if (!is_valid_ether_addr(iap)) {
1614 struct device_node *np = fep->pdev->dev.of_node;
1615 if (np) {
1616 const char *mac = of_get_mac_address(np);
1617 if (mac)
1618 iap = (unsigned char *) mac;
1619 }
1620 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001621
1622 /*
1623 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001624 */
1625 if (!is_valid_ether_addr(iap)) {
1626#ifdef CONFIG_M5272
1627 if (FEC_FLASHMAC)
1628 iap = (unsigned char *)FEC_FLASHMAC;
1629#else
1630 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001631 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001632#endif
1633 }
1634
1635 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001636 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001637 */
1638 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001639 *((__be32 *) &tmpaddr[0]) =
1640 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1641 *((__be16 *) &tmpaddr[4]) =
1642 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 iap = &tmpaddr[0];
1644 }
1645
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001646 /*
1647 * 5) random mac address
1648 */
1649 if (!is_valid_ether_addr(iap)) {
1650 /* Report it and use a random ethernet address instead */
1651 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1652 eth_hw_addr_random(ndev);
1653 netdev_info(ndev, "Using random MAC address: %pM\n",
1654 ndev->dev_addr);
1655 return;
1656 }
1657
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001658 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Shawn Guo49da97d2011-01-05 21:13:11 +00001660 /* Adjust MAC if using macaddr */
1661 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001662 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665/* ------------------------------------------------------------------------- */
1666
Bryan Wue6b043d2010-03-31 02:10:44 +00001667/*
1668 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001670static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001672 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001673 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001674 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Bryan Wue6b043d2010-03-31 02:10:44 +00001676 /* Prevent a state halted on mii error */
1677 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1678 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001681
Russell King8ce56242014-07-08 12:40:07 +01001682 /*
1683 * If the netdev is down, or is going down, we're not interested
1684 * in link state events, so just mark our idea of the link as down
1685 * and ignore the event.
1686 */
1687 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1688 fep->link = 0;
1689 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001690 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001691 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001692 status_change = 1;
1693 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001694
Russell Kingef833372014-07-08 12:40:43 +01001695 if (fep->full_duplex != phy_dev->duplex) {
1696 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001697 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001698 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001699
1700 if (phy_dev->speed != fep->speed) {
1701 fep->speed = phy_dev->speed;
1702 status_change = 1;
1703 }
1704
1705 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001706 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001707 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001708 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001709 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001710 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001711 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001712 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001713 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001714 } else {
1715 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001716 napi_disable(&fep->napi);
1717 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001718 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001719 netif_tx_unlock_bh(ndev);
1720 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001721 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001722 status_change = 1;
1723 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001724 }
1725
Bryan Wue6b043d2010-03-31 02:10:44 +00001726 if (status_change)
1727 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Bryan Wue6b043d2010-03-31 02:10:44 +00001730static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Bryan Wue6b043d2010-03-31 02:10:44 +00001732 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001733 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001734 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001735 int ret = 0;
1736
1737 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001738 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001739 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001740
1741 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001742 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001743
1744 /* start a read op */
1745 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1746 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1747 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1748
1749 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001750 time_left = wait_for_completion_timeout(&fep->mdio_done,
1751 usecs_to_jiffies(FEC_MII_TIMEOUT));
1752 if (time_left == 0) {
1753 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001754 netdev_err(fep->netdev, "MDIO read timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001755 ret = -ETIMEDOUT;
1756 goto out;
Bryan Wue6b043d2010-03-31 02:10:44 +00001757 }
1758
Andrew Lunn8fff7552015-07-25 22:38:02 +02001759 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1760
1761out:
1762 pm_runtime_mark_last_busy(dev);
1763 pm_runtime_put_autosuspend(dev);
1764
1765 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001766}
1767
1768static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1769 u16 value)
1770{
1771 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001772 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001773 unsigned long time_left;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001774 int ret;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001775
1776 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001777 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001778 return ret;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001779 else
1780 ret = 0;
Bryan Wue6b043d2010-03-31 02:10:44 +00001781
1782 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001783 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001784
Shawn Guo862f0982011-01-05 21:13:09 +00001785 /* start a write op */
1786 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001787 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1788 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1789 fep->hwp + FEC_MII_DATA);
1790
1791 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001792 time_left = wait_for_completion_timeout(&fep->mdio_done,
1793 usecs_to_jiffies(FEC_MII_TIMEOUT));
1794 if (time_left == 0) {
1795 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001796 netdev_err(fep->netdev, "MDIO write timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001797 ret = -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001798 }
1799
Andrew Lunn8fff7552015-07-25 22:38:02 +02001800 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
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001806static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1807{
1808 struct fec_enet_private *fep = netdev_priv(ndev);
1809 int ret;
1810
1811 if (enable) {
1812 ret = clk_prepare_enable(fep->clk_ahb);
1813 if (ret)
1814 return ret;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001815 if (fep->clk_enet_out) {
1816 ret = clk_prepare_enable(fep->clk_enet_out);
1817 if (ret)
1818 goto failed_clk_enet_out;
1819 }
1820 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001821 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001822 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001823 if (ret) {
1824 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001825 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001826 } else {
1827 fep->ptp_clk_on = true;
1828 }
1829 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001830 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001831 if (fep->clk_ref) {
1832 ret = clk_prepare_enable(fep->clk_ref);
1833 if (ret)
1834 goto failed_clk_ref;
1835 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001836 } else {
1837 clk_disable_unprepare(fep->clk_ahb);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001838 if (fep->clk_enet_out)
1839 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001840 if (fep->clk_ptp) {
1841 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001842 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001843 fep->ptp_clk_on = false;
1844 mutex_unlock(&fep->ptp_clk_mutex);
1845 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001846 if (fep->clk_ref)
1847 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001848 }
1849
1850 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001851
1852failed_clk_ref:
1853 if (fep->clk_ref)
1854 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001855failed_clk_ptp:
1856 if (fep->clk_enet_out)
1857 clk_disable_unprepare(fep->clk_enet_out);
1858failed_clk_enet_out:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001859 clk_disable_unprepare(fep->clk_ahb);
1860
1861 return ret;
1862}
1863
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001864static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001865{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001866 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001867 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001868 char mdio_bus_id[MII_BUS_ID_SIZE];
1869 char phy_name[MII_BUS_ID_SIZE + 3];
1870 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001871 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001872
Bryan Wu418bd0d2010-05-28 03:40:39 -07001873 fep->phy_dev = NULL;
1874
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001875 if (fep->phy_node) {
1876 phy_dev = of_phy_connect(ndev, fep->phy_node,
1877 &fec_enet_adjust_link, 0,
1878 fep->phy_interface);
Nimrod Andy213a9922014-12-09 18:46:56 +08001879 if (!phy_dev)
1880 return -ENODEV;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001881 } else {
1882 /* check for attached phy */
1883 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
Andrew Lunn7f854422016-01-06 20:11:18 +01001884 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001885 continue;
1886 if (dev_id--)
1887 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001888 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001889 break;
1890 }
1891
1892 if (phy_id >= PHY_MAX_ADDR) {
1893 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001894 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001895 phy_id = 0;
1896 }
1897
1898 snprintf(phy_name, sizeof(phy_name),
1899 PHY_ID_FMT, mdio_bus_id, phy_id);
1900 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1901 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001902 }
1903
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001904 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001905 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001906 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001907 }
1908
1909 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001910 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001911 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001912 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001913#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001914 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001915#endif
Frank Libaa70a52013-01-16 16:55:58 +00001916 }
Shawn Guo230dec62011-09-23 02:12:48 +00001917 else
1918 phy_dev->supported &= PHY_BASIC_FEATURES;
1919
Bryan Wue6b043d2010-03-31 02:10:44 +00001920 phy_dev->advertising = phy_dev->supported;
1921
1922 fep->phy_dev = phy_dev;
1923 fep->link = 0;
1924 fep->full_duplex = 0;
1925
Andrew Lunn22209432016-01-06 20:11:13 +01001926 phy_attached_info(phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001927
Bryan Wue6b043d2010-03-31 02:10:44 +00001928 return 0;
1929}
1930
1931static int fec_enet_mii_init(struct platform_device *pdev)
1932{
Shawn Guob5680e02011-01-05 21:13:13 +00001933 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001934 struct net_device *ndev = platform_get_drvdata(pdev);
1935 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001936 struct device_node *node;
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001937 int err = -ENXIO;
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001938 u32 mii_speed, holdtime;
Bryan Wue6b043d2010-03-31 02:10:44 +00001939
Shawn Guob5680e02011-01-05 21:13:13 +00001940 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001941 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001942 * Here are the differences:
1943 *
1944 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1945 * - fec0 acts as the 1588 time master while fec1 is slave
1946 * - external phys can only be configured by fec0
1947 *
1948 * That is to say fec1 can not work independently. It only works
1949 * when fec0 is working. The reason behind this design is that the
1950 * second interface is added primarily for Switch mode.
1951 *
1952 * Because of the last point above, both phys are attached on fec0
1953 * mdio interface in board design, and need to be configured by
1954 * fec0 mii_bus.
1955 */
Stefan Agner3d125f92015-01-14 00:20:21 +01001956 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001957 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001958 if (mii_cnt && fec0_mii_bus) {
1959 fep->mii_bus = fec0_mii_bus;
1960 mii_cnt++;
1961 return 0;
1962 }
1963 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001964 }
1965
Bryan Wue6b043d2010-03-31 02:10:44 +00001966 fep->mii_timeout = 0;
1967
1968 /*
1969 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001970 *
1971 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1972 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1973 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1974 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001975 */
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001976 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001977 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001978 mii_speed--;
1979 if (mii_speed > 63) {
1980 dev_err(&pdev->dev,
1981 "fec clock (%lu) to fast to get right mii speed\n",
1982 clk_get_rate(fep->clk_ipg));
1983 err = -EINVAL;
1984 goto err_out;
1985 }
1986
1987 /*
1988 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
1989 * MII_SPEED) register that defines the MDIO output hold time. Earlier
1990 * versions are RAZ there, so just ignore the difference and write the
1991 * register always.
1992 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
1993 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
1994 * output.
1995 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
1996 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
1997 * holdtime cannot result in a value greater than 3.
1998 */
1999 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2000
2001 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2002
Bryan Wue6b043d2010-03-31 02:10:44 +00002003 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2004
2005 fep->mii_bus = mdiobus_alloc();
2006 if (fep->mii_bus == NULL) {
2007 err = -ENOMEM;
2008 goto err_out;
2009 }
2010
2011 fep->mii_bus->name = "fec_enet_mii_bus";
2012 fep->mii_bus->read = fec_enet_mdio_read;
2013 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002014 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2015 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002016 fep->mii_bus->priv = fep;
2017 fep->mii_bus->parent = &pdev->dev;
2018
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002019 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2020 if (node) {
2021 err = of_mdiobus_register(fep->mii_bus, node);
2022 of_node_put(node);
2023 } else {
2024 err = mdiobus_register(fep->mii_bus);
2025 }
2026
2027 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002028 goto err_out_free_mdiobus;
Bryan Wue6b043d2010-03-31 02:10:44 +00002029
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002030 mii_cnt++;
2031
Shawn Guob5680e02011-01-05 21:13:13 +00002032 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002033 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002034 fec0_mii_bus = fep->mii_bus;
2035
Bryan Wue6b043d2010-03-31 02:10:44 +00002036 return 0;
2037
Bryan Wue6b043d2010-03-31 02:10:44 +00002038err_out_free_mdiobus:
2039 mdiobus_free(fep->mii_bus);
2040err_out:
2041 return err;
2042}
2043
2044static void fec_enet_mii_remove(struct fec_enet_private *fep)
2045{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002046 if (--mii_cnt == 0) {
2047 mdiobus_unregister(fep->mii_bus);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002048 mdiobus_free(fep->mii_bus);
2049 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002050}
2051
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002052static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002053 struct ethtool_cmd *cmd)
2054{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002055 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002056 struct phy_device *phydev = fep->phy_dev;
2057
2058 if (!phydev)
2059 return -ENODEV;
2060
2061 return phy_ethtool_gset(phydev, cmd);
2062}
2063
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002064static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002065 struct ethtool_cmd *cmd)
2066{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002067 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002068 struct phy_device *phydev = fep->phy_dev;
2069
2070 if (!phydev)
2071 return -ENODEV;
2072
2073 return phy_ethtool_sset(phydev, cmd);
2074}
2075
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002076static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002077 struct ethtool_drvinfo *info)
2078{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002079 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Jiri Pirko7826d432013-01-06 00:44:26 +00002081 strlcpy(info->driver, fep->pdev->dev.driver->name,
2082 sizeof(info->driver));
2083 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2084 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
Bryan Wue6b043d2010-03-31 02:10:44 +00002086
Philippe Reynesdb65f352015-05-11 00:01:42 +02002087static int fec_enet_get_regs_len(struct net_device *ndev)
2088{
2089 struct fec_enet_private *fep = netdev_priv(ndev);
2090 struct resource *r;
2091 int s = 0;
2092
2093 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2094 if (r)
2095 s = resource_size(r);
2096
2097 return s;
2098}
2099
2100/* List of registers that can be safety be read to dump them with ethtool */
2101#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Johannes Berg05f3b502016-01-25 11:40:50 +01002102 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Philippe Reynesdb65f352015-05-11 00:01:42 +02002103static u32 fec_enet_register_offset[] = {
2104 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2105 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2106 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2107 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2108 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2109 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2110 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2111 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2112 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2113 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2114 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2115 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2116 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2117 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2118 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2119 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2120 RMON_T_P_GTE2048, RMON_T_OCTETS,
2121 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2122 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2123 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2124 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2125 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2126 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2127 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2128 RMON_R_P_GTE2048, RMON_R_OCTETS,
2129 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2130 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2131};
2132#else
2133static u32 fec_enet_register_offset[] = {
2134 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2135 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2136 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2137 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2138 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2139 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2140 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2141 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2142 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2143};
2144#endif
2145
2146static void fec_enet_get_regs(struct net_device *ndev,
2147 struct ethtool_regs *regs, void *regbuf)
2148{
2149 struct fec_enet_private *fep = netdev_priv(ndev);
2150 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2151 u32 *buf = (u32 *)regbuf;
2152 u32 i, off;
2153
2154 memset(buf, 0, regs->len);
2155
2156 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2157 off = fec_enet_register_offset[i] / 4;
2158 buf[off] = readl(&theregs[off]);
2159 }
2160}
2161
Frank Li5ebae4892013-01-06 16:25:07 +00002162static int fec_enet_get_ts_info(struct net_device *ndev,
2163 struct ethtool_ts_info *info)
2164{
2165 struct fec_enet_private *fep = netdev_priv(ndev);
2166
2167 if (fep->bufdesc_ex) {
2168
2169 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2170 SOF_TIMESTAMPING_RX_SOFTWARE |
2171 SOF_TIMESTAMPING_SOFTWARE |
2172 SOF_TIMESTAMPING_TX_HARDWARE |
2173 SOF_TIMESTAMPING_RX_HARDWARE |
2174 SOF_TIMESTAMPING_RAW_HARDWARE;
2175 if (fep->ptp_clock)
2176 info->phc_index = ptp_clock_index(fep->ptp_clock);
2177 else
2178 info->phc_index = -1;
2179
2180 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2181 (1 << HWTSTAMP_TX_ON);
2182
2183 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2184 (1 << HWTSTAMP_FILTER_ALL);
2185 return 0;
2186 } else {
2187 return ethtool_op_get_ts_info(ndev, info);
2188 }
2189}
2190
Guenter Roeckd1391932013-06-18 10:04:59 -07002191#if !defined(CONFIG_M5272)
2192
Frank Libaa70a52013-01-16 16:55:58 +00002193static void fec_enet_get_pauseparam(struct net_device *ndev,
2194 struct ethtool_pauseparam *pause)
2195{
2196 struct fec_enet_private *fep = netdev_priv(ndev);
2197
2198 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2199 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2200 pause->rx_pause = pause->tx_pause;
2201}
2202
2203static int fec_enet_set_pauseparam(struct net_device *ndev,
2204 struct ethtool_pauseparam *pause)
2205{
2206 struct fec_enet_private *fep = netdev_priv(ndev);
2207
Russell King0b146ca2014-07-08 00:22:59 +01002208 if (!fep->phy_dev)
2209 return -ENODEV;
2210
Frank Libaa70a52013-01-16 16:55:58 +00002211 if (pause->tx_pause != pause->rx_pause) {
2212 netdev_info(ndev,
2213 "hardware only support enable/disable both tx and rx");
2214 return -EINVAL;
2215 }
2216
2217 fep->pause_flag = 0;
2218
2219 /* tx pause must be same as rx pause */
2220 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2221 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2222
2223 if (pause->rx_pause || pause->autoneg) {
2224 fep->phy_dev->supported |= ADVERTISED_Pause;
2225 fep->phy_dev->advertising |= ADVERTISED_Pause;
2226 } else {
2227 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2228 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2229 }
2230
2231 if (pause->autoneg) {
2232 if (netif_running(ndev))
2233 fec_stop(ndev);
2234 phy_start_aneg(fep->phy_dev);
2235 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002236 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002237 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002238 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002239 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002240 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002241 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002242 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002243 }
Frank Libaa70a52013-01-16 16:55:58 +00002244
2245 return 0;
2246}
2247
Chris Healy38ae92d2013-06-25 23:18:52 -07002248static const struct fec_stat {
2249 char name[ETH_GSTRING_LEN];
2250 u16 offset;
2251} fec_stats[] = {
2252 /* RMON TX */
2253 { "tx_dropped", RMON_T_DROP },
2254 { "tx_packets", RMON_T_PACKETS },
2255 { "tx_broadcast", RMON_T_BC_PKT },
2256 { "tx_multicast", RMON_T_MC_PKT },
2257 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2258 { "tx_undersize", RMON_T_UNDERSIZE },
2259 { "tx_oversize", RMON_T_OVERSIZE },
2260 { "tx_fragment", RMON_T_FRAG },
2261 { "tx_jabber", RMON_T_JAB },
2262 { "tx_collision", RMON_T_COL },
2263 { "tx_64byte", RMON_T_P64 },
2264 { "tx_65to127byte", RMON_T_P65TO127 },
2265 { "tx_128to255byte", RMON_T_P128TO255 },
2266 { "tx_256to511byte", RMON_T_P256TO511 },
2267 { "tx_512to1023byte", RMON_T_P512TO1023 },
2268 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2269 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2270 { "tx_octets", RMON_T_OCTETS },
2271
2272 /* IEEE TX */
2273 { "IEEE_tx_drop", IEEE_T_DROP },
2274 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2275 { "IEEE_tx_1col", IEEE_T_1COL },
2276 { "IEEE_tx_mcol", IEEE_T_MCOL },
2277 { "IEEE_tx_def", IEEE_T_DEF },
2278 { "IEEE_tx_lcol", IEEE_T_LCOL },
2279 { "IEEE_tx_excol", IEEE_T_EXCOL },
2280 { "IEEE_tx_macerr", IEEE_T_MACERR },
2281 { "IEEE_tx_cserr", IEEE_T_CSERR },
2282 { "IEEE_tx_sqe", IEEE_T_SQE },
2283 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2284 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2285
2286 /* RMON RX */
2287 { "rx_packets", RMON_R_PACKETS },
2288 { "rx_broadcast", RMON_R_BC_PKT },
2289 { "rx_multicast", RMON_R_MC_PKT },
2290 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2291 { "rx_undersize", RMON_R_UNDERSIZE },
2292 { "rx_oversize", RMON_R_OVERSIZE },
2293 { "rx_fragment", RMON_R_FRAG },
2294 { "rx_jabber", RMON_R_JAB },
2295 { "rx_64byte", RMON_R_P64 },
2296 { "rx_65to127byte", RMON_R_P65TO127 },
2297 { "rx_128to255byte", RMON_R_P128TO255 },
2298 { "rx_256to511byte", RMON_R_P256TO511 },
2299 { "rx_512to1023byte", RMON_R_P512TO1023 },
2300 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2301 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2302 { "rx_octets", RMON_R_OCTETS },
2303
2304 /* IEEE RX */
2305 { "IEEE_rx_drop", IEEE_R_DROP },
2306 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2307 { "IEEE_rx_crc", IEEE_R_CRC },
2308 { "IEEE_rx_align", IEEE_R_ALIGN },
2309 { "IEEE_rx_macerr", IEEE_R_MACERR },
2310 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2311 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2312};
2313
2314static void fec_enet_get_ethtool_stats(struct net_device *dev,
2315 struct ethtool_stats *stats, u64 *data)
2316{
2317 struct fec_enet_private *fep = netdev_priv(dev);
2318 int i;
2319
2320 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2321 data[i] = readl(fep->hwp + fec_stats[i].offset);
2322}
2323
2324static void fec_enet_get_strings(struct net_device *netdev,
2325 u32 stringset, u8 *data)
2326{
2327 int i;
2328 switch (stringset) {
2329 case ETH_SS_STATS:
2330 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2331 memcpy(data + i * ETH_GSTRING_LEN,
2332 fec_stats[i].name, ETH_GSTRING_LEN);
2333 break;
2334 }
2335}
2336
2337static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2338{
2339 switch (sset) {
2340 case ETH_SS_STATS:
2341 return ARRAY_SIZE(fec_stats);
2342 default:
2343 return -EOPNOTSUPP;
2344 }
2345}
Guenter Roeckd1391932013-06-18 10:04:59 -07002346#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002347
Chris Healy32bc9b42013-06-17 07:25:06 -07002348static int fec_enet_nway_reset(struct net_device *dev)
2349{
2350 struct fec_enet_private *fep = netdev_priv(dev);
2351 struct phy_device *phydev = fep->phy_dev;
2352
2353 if (!phydev)
2354 return -ENODEV;
2355
2356 return genphy_restart_aneg(phydev);
2357}
2358
Fugang Duand851b472014-09-17 05:18:52 +08002359/* ITR clock source is enet system clock (clk_ahb).
2360 * TCTT unit is cycle_ns * 64 cycle
2361 * So, the ICTT value = X us / (cycle_ns * 64)
2362 */
2363static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2364{
2365 struct fec_enet_private *fep = netdev_priv(ndev);
2366
2367 return us * (fep->itr_clk_rate / 64000) / 1000;
2368}
2369
2370/* Set threshold for interrupt coalescing */
2371static void fec_enet_itr_coal_set(struct net_device *ndev)
2372{
2373 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002374 int rx_itr, tx_itr;
2375
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002376 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002377 return;
2378
2379 /* Must be greater than zero to avoid unpredictable behavior */
2380 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2381 !fep->tx_time_itr || !fep->tx_pkts_itr)
2382 return;
2383
2384 /* Select enet system clock as Interrupt Coalescing
2385 * timer Clock Source
2386 */
2387 rx_itr = FEC_ITR_CLK_SEL;
2388 tx_itr = FEC_ITR_CLK_SEL;
2389
2390 /* set ICFT and ICTT */
2391 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2392 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2393 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2394 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2395
2396 rx_itr |= FEC_ITR_EN;
2397 tx_itr |= FEC_ITR_EN;
2398
2399 writel(tx_itr, fep->hwp + FEC_TXIC0);
2400 writel(rx_itr, fep->hwp + FEC_RXIC0);
2401 writel(tx_itr, fep->hwp + FEC_TXIC1);
2402 writel(rx_itr, fep->hwp + FEC_RXIC1);
2403 writel(tx_itr, fep->hwp + FEC_TXIC2);
2404 writel(rx_itr, fep->hwp + FEC_RXIC2);
2405}
2406
2407static int
2408fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2409{
2410 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002411
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002412 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002413 return -EOPNOTSUPP;
2414
2415 ec->rx_coalesce_usecs = fep->rx_time_itr;
2416 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2417
2418 ec->tx_coalesce_usecs = fep->tx_time_itr;
2419 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2420
2421 return 0;
2422}
2423
2424static int
2425fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2426{
2427 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002428 unsigned int cycle;
2429
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002430 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002431 return -EOPNOTSUPP;
2432
2433 if (ec->rx_max_coalesced_frames > 255) {
2434 pr_err("Rx coalesced frames exceed hardware limiation");
2435 return -EINVAL;
2436 }
2437
2438 if (ec->tx_max_coalesced_frames > 255) {
2439 pr_err("Tx coalesced frame exceed hardware limiation");
2440 return -EINVAL;
2441 }
2442
2443 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2444 if (cycle > 0xFFFF) {
2445 pr_err("Rx coalesed usec exceeed hardware limiation");
2446 return -EINVAL;
2447 }
2448
2449 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2450 if (cycle > 0xFFFF) {
2451 pr_err("Rx coalesed usec exceeed hardware limiation");
2452 return -EINVAL;
2453 }
2454
2455 fep->rx_time_itr = ec->rx_coalesce_usecs;
2456 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2457
2458 fep->tx_time_itr = ec->tx_coalesce_usecs;
2459 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2460
2461 fec_enet_itr_coal_set(ndev);
2462
2463 return 0;
2464}
2465
2466static void fec_enet_itr_coal_init(struct net_device *ndev)
2467{
2468 struct ethtool_coalesce ec;
2469
2470 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2471 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2472
2473 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2474 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2475
2476 fec_enet_set_coalesce(ndev, &ec);
2477}
2478
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002479static int fec_enet_get_tunable(struct net_device *netdev,
2480 const struct ethtool_tunable *tuna,
2481 void *data)
2482{
2483 struct fec_enet_private *fep = netdev_priv(netdev);
2484 int ret = 0;
2485
2486 switch (tuna->id) {
2487 case ETHTOOL_RX_COPYBREAK:
2488 *(u32 *)data = fep->rx_copybreak;
2489 break;
2490 default:
2491 ret = -EINVAL;
2492 break;
2493 }
2494
2495 return ret;
2496}
2497
2498static int fec_enet_set_tunable(struct net_device *netdev,
2499 const struct ethtool_tunable *tuna,
2500 const void *data)
2501{
2502 struct fec_enet_private *fep = netdev_priv(netdev);
2503 int ret = 0;
2504
2505 switch (tuna->id) {
2506 case ETHTOOL_RX_COPYBREAK:
2507 fep->rx_copybreak = *(u32 *)data;
2508 break;
2509 default:
2510 ret = -EINVAL;
2511 break;
2512 }
2513
2514 return ret;
2515}
2516
Nimrod Andyde40ed32014-12-24 17:30:39 +08002517static void
2518fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2519{
2520 struct fec_enet_private *fep = netdev_priv(ndev);
2521
2522 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2523 wol->supported = WAKE_MAGIC;
2524 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2525 } else {
2526 wol->supported = wol->wolopts = 0;
2527 }
2528}
2529
2530static int
2531fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2532{
2533 struct fec_enet_private *fep = netdev_priv(ndev);
2534
2535 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2536 return -EINVAL;
2537
2538 if (wol->wolopts & ~WAKE_MAGIC)
2539 return -EINVAL;
2540
2541 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2542 if (device_may_wakeup(&ndev->dev)) {
2543 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2544 if (fep->irq[0] > 0)
2545 enable_irq_wake(fep->irq[0]);
2546 } else {
2547 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2548 if (fep->irq[0] > 0)
2549 disable_irq_wake(fep->irq[0]);
2550 }
2551
2552 return 0;
2553}
2554
stephen hemminger9b07be42012-01-04 12:59:49 +00002555static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002556 .get_settings = fec_enet_get_settings,
2557 .set_settings = fec_enet_set_settings,
2558 .get_drvinfo = fec_enet_get_drvinfo,
Philippe Reynesdb65f352015-05-11 00:01:42 +02002559 .get_regs_len = fec_enet_get_regs_len,
2560 .get_regs = fec_enet_get_regs,
Chris Healy32bc9b42013-06-17 07:25:06 -07002561 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002562 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002563 .get_coalesce = fec_enet_get_coalesce,
2564 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002565#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002566 .get_pauseparam = fec_enet_get_pauseparam,
2567 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002568 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002569 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002570 .get_sset_count = fec_enet_get_sset_count,
2571#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002572 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002573 .get_tunable = fec_enet_get_tunable,
2574 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002575 .get_wol = fec_enet_get_wol,
2576 .set_wol = fec_enet_set_wol,
Bryan Wue6b043d2010-03-31 02:10:44 +00002577};
2578
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002579static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002580{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002581 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002582 struct phy_device *phydev = fep->phy_dev;
2583
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002584 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002585 return -EINVAL;
2586
2587 if (!phydev)
2588 return -ENODEV;
2589
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002590 if (fep->bufdesc_ex) {
2591 if (cmd == SIOCSHWTSTAMP)
2592 return fec_ptp_set(ndev, rq);
2593 if (cmd == SIOCGHWTSTAMP)
2594 return fec_ptp_get(ndev, rq);
2595 }
Frank Liff43da82013-01-03 16:04:23 +00002596
Richard Cochran28b04112010-07-17 08:48:55 +00002597 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002600static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002601{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002602 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002603 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002604 struct sk_buff *skb;
2605 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002606 struct fec_enet_priv_tx_q *txq;
2607 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002608 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002609
Frank Li59d0f7462014-09-13 05:00:50 +08002610 for (q = 0; q < fep->num_rx_queues; q++) {
2611 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002612 bdp = rxq->bd.base;
2613 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002614 skb = rxq->rx_skbuff[i];
2615 rxq->rx_skbuff[i] = NULL;
2616 if (skb) {
2617 dma_unmap_single(&fep->pdev->dev,
Johannes Berg5cfa3032016-01-24 16:52:37 +01002618 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002619 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002620 DMA_FROM_DEVICE);
2621 dev_kfree_skb(skb);
2622 }
Troy Kisky7355f272016-02-05 14:52:46 -07002623 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Russell King730ee362014-07-08 00:23:14 +01002624 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002625 }
2626
Frank Li59d0f7462014-09-13 05:00:50 +08002627 for (q = 0; q < fep->num_tx_queues; q++) {
2628 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002629 bdp = txq->bd.base;
2630 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002631 kfree(txq->tx_bounce[i]);
2632 txq->tx_bounce[i] = NULL;
2633 skb = txq->tx_skbuff[i];
2634 txq->tx_skbuff[i] = NULL;
2635 dev_kfree_skb(skb);
2636 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002637 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002638}
2639
Frank Li59d0f7462014-09-13 05:00:50 +08002640static void fec_enet_free_queue(struct net_device *ndev)
2641{
2642 struct fec_enet_private *fep = netdev_priv(ndev);
2643 int i;
2644 struct fec_enet_priv_tx_q *txq;
2645
2646 for (i = 0; i < fep->num_tx_queues; i++)
2647 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2648 txq = fep->tx_queue[i];
2649 dma_free_coherent(NULL,
Troy Kisky7355f272016-02-05 14:52:46 -07002650 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002651 txq->tso_hdrs,
2652 txq->tso_hdrs_dma);
2653 }
2654
2655 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002656 kfree(fep->rx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002657 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002658 kfree(fep->tx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002659}
2660
2661static int fec_enet_alloc_queue(struct net_device *ndev)
2662{
2663 struct fec_enet_private *fep = netdev_priv(ndev);
2664 int i;
2665 int ret = 0;
2666 struct fec_enet_priv_tx_q *txq;
2667
2668 for (i = 0; i < fep->num_tx_queues; i++) {
2669 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2670 if (!txq) {
2671 ret = -ENOMEM;
2672 goto alloc_failed;
2673 }
2674
2675 fep->tx_queue[i] = txq;
Troy Kisky7355f272016-02-05 14:52:46 -07002676 txq->bd.ring_size = TX_RING_SIZE;
2677 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002678
2679 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2680 txq->tx_wake_threshold =
Troy Kisky7355f272016-02-05 14:52:46 -07002681 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
Frank Li59d0f7462014-09-13 05:00:50 +08002682
2683 txq->tso_hdrs = dma_alloc_coherent(NULL,
Troy Kisky7355f272016-02-05 14:52:46 -07002684 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002685 &txq->tso_hdrs_dma,
2686 GFP_KERNEL);
2687 if (!txq->tso_hdrs) {
2688 ret = -ENOMEM;
2689 goto alloc_failed;
2690 }
2691 }
2692
2693 for (i = 0; i < fep->num_rx_queues; i++) {
2694 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2695 GFP_KERNEL);
2696 if (!fep->rx_queue[i]) {
2697 ret = -ENOMEM;
2698 goto alloc_failed;
2699 }
2700
Troy Kisky7355f272016-02-05 14:52:46 -07002701 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
2702 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002703 }
2704 return ret;
2705
2706alloc_failed:
2707 fec_enet_free_queue(ndev);
2708 return ret;
2709}
2710
2711static int
2712fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002713{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002714 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002715 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002716 struct sk_buff *skb;
2717 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002718 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002719
Frank Li59d0f7462014-09-13 05:00:50 +08002720 rxq = fep->rx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002721 bdp = rxq->bd.base;
2722 for (i = 0; i < rxq->bd.ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002723 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002724 if (!skb)
2725 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002726
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002727 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002728 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002729 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002730 }
Russell King730ee362014-07-08 00:23:14 +01002731
Fugang Duan4d494cd2014-09-13 05:00:48 +08002732 rxq->rx_skbuff[i] = skb;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002733 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Liff43da82013-01-03 16:04:23 +00002734
2735 if (fep->bufdesc_ex) {
2736 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002737 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002738 }
2739
Troy Kisky7355f272016-02-05 14:52:46 -07002740 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002741 }
2742
2743 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002744 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002745 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +08002746 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002747
Frank Li59d0f7462014-09-13 05:00:50 +08002748 err_alloc:
2749 fec_enet_free_buffers(ndev);
2750 return -ENOMEM;
2751}
2752
2753static int
2754fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2755{
2756 struct fec_enet_private *fep = netdev_priv(ndev);
2757 unsigned int i;
2758 struct bufdesc *bdp;
2759 struct fec_enet_priv_tx_q *txq;
2760
2761 txq = fep->tx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002762 bdp = txq->bd.base;
2763 for (i = 0; i < txq->bd.ring_size; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002764 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2765 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002766 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002767
Johannes Berg5cfa3032016-01-24 16:52:37 +01002768 bdp->cbd_sc = cpu_to_fec16(0);
2769 bdp->cbd_bufaddr = cpu_to_fec32(0);
Frank Li6605b732012-10-30 18:25:31 +00002770
Frank Liff43da82013-01-03 16:04:23 +00002771 if (fep->bufdesc_ex) {
2772 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002773 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002774 }
2775
Troy Kisky7355f272016-02-05 14:52:46 -07002776 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002777 }
2778
2779 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002780 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002781 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002782
2783 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002784
2785 err_alloc:
2786 fec_enet_free_buffers(ndev);
2787 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002788}
2789
Frank Li59d0f7462014-09-13 05:00:50 +08002790static int fec_enet_alloc_buffers(struct net_device *ndev)
2791{
2792 struct fec_enet_private *fep = netdev_priv(ndev);
2793 unsigned int i;
2794
2795 for (i = 0; i < fep->num_rx_queues; i++)
2796 if (fec_enet_alloc_rxq_buffers(ndev, i))
2797 return -ENOMEM;
2798
2799 for (i = 0; i < fep->num_tx_queues; i++)
2800 if (fec_enet_alloc_txq_buffers(ndev, i))
2801 return -ENOMEM;
2802 return 0;
2803}
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002806fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002808 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002809 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Andrew Lunn8fff7552015-07-25 22:38:02 +02002811 ret = pm_runtime_get_sync(&fep->pdev->dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03002812 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002813 return ret;
2814
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002815 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002816 ret = fec_enet_clk_enable(ndev, true);
2817 if (ret)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002818 goto clk_enable;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 /* I should reset the ring buffers here, but I don't yet know
2821 * a simple way to do that.
2822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002824 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002825 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002826 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002827
Nimrod Andy55dd2752015-06-23 15:32:51 +08002828 /* Init MAC prior to mii bus probe */
2829 fec_restart(ndev);
2830
Bryan Wu418bd0d2010-05-28 03:40:39 -07002831 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002832 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002833 if (ret)
2834 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002835
2836 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002837 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002838 netif_tx_start_all_queues(ndev);
2839
Nimrod Andyde40ed32014-12-24 17:30:39 +08002840 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2841 FEC_WOL_FLAG_ENABLE);
2842
Sascha Hauer22f6b862009-04-15 01:32:18 +00002843 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002844
2845err_enet_mii_probe:
2846 fec_enet_free_buffers(ndev);
2847err_enet_alloc:
2848 fec_enet_clk_enable(ndev, false);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002849clk_enable:
2850 pm_runtime_mark_last_busy(&fep->pdev->dev);
2851 pm_runtime_put_autosuspend(&fep->pdev->dev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002852 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2853 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
2856static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002857fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002859 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Russell Kingd76cfae2014-07-08 00:23:04 +01002861 phy_stop(fep->phy_dev);
2862
Russell King31a6de32014-07-08 12:40:23 +01002863 if (netif_device_present(ndev)) {
2864 napi_disable(&fep->napi);
2865 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002866 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Russell King635cf172014-07-08 00:22:54 +01002869 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002870 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002871
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002872 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002873 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002874 pm_runtime_mark_last_busy(&fep->pdev->dev);
2875 pm_runtime_put_autosuspend(&fep->pdev->dev);
2876
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002877 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 0;
2880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/* Set or clear the multicast filter for this adaptor.
2883 * Skeleton taken from sunlance driver.
2884 * The CPM Ethernet implementation allows Multicast as well as individual
2885 * MAC address filtering. Some of the drivers check to make sure it is
2886 * a group multicast address, and discard those that are not. I guess I
2887 * will do the same for now, but just remove the test if you want
2888 * individual filtering as well (do the upper net layers want or support
2889 * this kind of feature?).
2890 */
2891
2892#define HASH_BITS 6 /* #bits in hash */
2893#define CRC32_POLY 0xEDB88320
2894
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002895static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002897 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002898 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002899 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 unsigned char hash;
2901
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002902 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002903 tmp = readl(fep->hwp + FEC_R_CNTRL);
2904 tmp |= 0x8;
2905 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002906 return;
2907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Sascha Hauer4e831832009-04-15 01:32:19 +00002909 tmp = readl(fep->hwp + FEC_R_CNTRL);
2910 tmp &= ~0x8;
2911 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002912
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002913 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002914 /* Catch all multicast addresses, so set the
2915 * filter to all 1's
2916 */
2917 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2918 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Sascha Hauer4e831832009-04-15 01:32:19 +00002920 return;
2921 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002922
Sascha Hauer4e831832009-04-15 01:32:19 +00002923 /* Clear filter and add the addresses in hash register
2924 */
2925 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2926 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002928 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002929 /* calculate crc32 value of mac address */
2930 crc = 0xffffffff;
2931
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002932 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002933 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002934 for (bit = 0; bit < 8; bit++, data >>= 1) {
2935 crc = (crc >> 1) ^
2936 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002939
2940 /* only upper 6 bits (HASH_BITS) are used
2941 * which point to specific bit in he hash registers
2942 */
2943 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2944
2945 if (hash > 31) {
2946 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2947 tmp |= 1 << (hash - 32);
2948 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2949 } else {
2950 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2951 tmp |= 1 << hash;
2952 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
2955}
2956
Sascha Hauer22f6b862009-04-15 01:32:18 +00002957/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002958static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002959fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002961 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002962 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Lucas Stach44934fa2014-03-30 21:32:08 +02002964 if (addr) {
2965 if (!is_valid_ether_addr(addr->sa_data))
2966 return -EADDRNOTAVAIL;
2967 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2968 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002969
Nimrod Andy9638d192015-09-10 09:35:39 +08002970 /* Add netif status check here to avoid system hang in below case:
2971 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
2972 * After ethx down, fec all clocks are gated off and then register
2973 * access causes system hang.
2974 */
2975 if (!netif_running(ndev))
2976 return 0;
2977
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002978 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2979 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002980 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002981 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002982 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002986#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002987/**
2988 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002989 * @dev: The FEC network adapter
2990 *
2991 * Polled functionality used by netconsole and others in non interrupt mode
2992 *
2993 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002994static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002995{
2996 int i;
2997 struct fec_enet_private *fep = netdev_priv(dev);
2998
2999 for (i = 0; i < FEC_IRQ_NUM; i++) {
3000 if (fep->irq[i] > 0) {
3001 disable_irq(fep->irq[i]);
3002 fec_enet_interrupt(fep->irq[i], dev);
3003 enable_irq(fep->irq[i]);
3004 }
3005 }
3006}
3007#endif
3008
Nimrod Andy5bc26722014-10-13 10:53:48 +08003009static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00003010 netdev_features_t features)
3011{
3012 struct fec_enet_private *fep = netdev_priv(netdev);
3013 netdev_features_t changed = features ^ netdev->features;
3014
3015 netdev->features = features;
3016
3017 /* Receive checksum has been changed */
3018 if (changed & NETIF_F_RXCSUM) {
3019 if (features & NETIF_F_RXCSUM)
3020 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3021 else
3022 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01003023 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08003024}
Jim Baxter4c09eed2013-04-19 08:10:49 +00003025
Nimrod Andy5bc26722014-10-13 10:53:48 +08003026static int fec_set_features(struct net_device *netdev,
3027 netdev_features_t features)
3028{
3029 struct fec_enet_private *fep = netdev_priv(netdev);
3030 netdev_features_t changed = features ^ netdev->features;
3031
Fabio Estevam5b40f702015-09-25 18:31:32 -03003032 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08003033 napi_disable(&fep->napi);
3034 netif_tx_lock_bh(netdev);
3035 fec_stop(netdev);
3036 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01003037 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003038 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01003039 netif_tx_unlock_bh(netdev);
3040 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08003041 } else {
3042 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00003043 }
3044
3045 return 0;
3046}
3047
Sascha Hauer009fda82009-04-15 01:32:23 +00003048static const struct net_device_ops fec_netdev_ops = {
3049 .ndo_open = fec_enet_open,
3050 .ndo_stop = fec_enet_close,
3051 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003052 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00003053 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00003054 .ndo_validate_addr = eth_validate_addr,
3055 .ndo_tx_timeout = fec_timeout,
3056 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01003057 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003058#ifdef CONFIG_NET_POLL_CONTROLLER
3059 .ndo_poll_controller = fec_poll_controller,
3060#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00003061 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00003062};
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 /*
3065 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00003066 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003068static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003070 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00003071 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08003072 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08003073 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08003074 unsigned int i;
Troy Kisky7355f272016-02-05 14:52:46 -07003075 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3076 sizeof(struct bufdesc);
3077 unsigned dsize_log2 = __fls(dsize);
Nimrod Andy55d02182014-06-12 08:16:21 +08003078
Troy Kisky7355f272016-02-05 14:52:46 -07003079 WARN_ON(dsize != (1 << dsize_log2));
Fugang Duan41ef84c2014-09-13 05:00:54 +08003080#if defined(CONFIG_ARM)
3081 fep->rx_align = 0xf;
3082 fep->tx_align = 0xf;
3083#else
3084 fep->rx_align = 0x3;
3085 fep->tx_align = 0x3;
3086#endif
3087
Frank Li59d0f7462014-09-13 05:00:50 +08003088 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003089
Troy Kisky7355f272016-02-05 14:52:46 -07003090 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003092 /* Allocate memory for buffer descriptors. */
Lucas Stachc0a1a0a2015-07-23 16:06:19 +02003093 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3094 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003095 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003096 return -ENOMEM;
3097 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003098
Fugang Duan4d494cd2014-09-13 05:00:48 +08003099 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Shawn Guo49da97d2011-01-05 21:13:11 +00003101 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003102 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003103 /* make sure MAC we just acquired is programmed into the hw */
3104 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003106 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003107 for (i = 0; i < fep->num_rx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003108 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3109 unsigned size = dsize * rxq->bd.ring_size;
3110
3111 rxq->bd.qid = i;
3112 rxq->bd.base = cbd_base;
3113 rxq->bd.cur = cbd_base;
3114 rxq->bd.dma = bd_dma;
3115 rxq->bd.dsize = dsize;
3116 rxq->bd.dsize_log2 = dsize_log2;
3117 bd_dma += size;
3118 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3119 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003120 }
3121
3122 for (i = 0; i < fep->num_tx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003123 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3124 unsigned size = dsize * txq->bd.ring_size;
3125
3126 txq->bd.qid = i;
3127 txq->bd.base = cbd_base;
3128 txq->bd.cur = cbd_base;
3129 txq->bd.dma = bd_dma;
3130 txq->bd.dsize = dsize;
3131 txq->bd.dsize_log2 = dsize_log2;
3132 bd_dma += size;
3133 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3134 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003135 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Sascha Hauer22f6b862009-04-15 01:32:18 +00003138 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003139 ndev->watchdog_timeo = TX_TIMEOUT;
3140 ndev->netdev_ops = &fec_netdev_ops;
3141 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003142
Frank Lidc975382013-01-28 18:31:42 +00003143 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003144 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003145
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003146 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003147 /* enable hw VLAN support */
3148 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003149
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003150 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003151 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3152
Shawn Guo48496252013-05-08 21:08:22 +00003153 /* enable hw accelerator */
3154 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003155 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003156 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3157 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003158
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003159 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003160 fep->tx_align = 0;
3161 fep->rx_align = 0x3f;
3162 }
3163
Nimrod Andy09d1e542014-06-12 08:16:20 +08003164 ndev->hw_features = ndev->features;
3165
Russell Kingef833372014-07-08 12:40:43 +01003166 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return 0;
3169}
3170
Shawn Guoca2cc332011-06-25 02:04:35 +08003171#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003172static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003173{
3174 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003175 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003176 struct device_node *np = pdev->dev.of_node;
3177
3178 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003179 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003180
Shawn Guoa3caad02012-06-27 03:45:24 +00003181 of_property_read_u32(np, "phy-reset-duration", &msec);
3182 /* A sane reset duration should not be longer than 1s */
3183 if (msec > 1000)
3184 msec = 1;
3185
Shawn Guoca2cc332011-06-25 02:04:35 +08003186 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003187 if (!gpio_is_valid(phy_reset))
3188 return;
3189
Shawn Guo119fc002012-06-27 03:45:22 +00003190 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3191 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003192 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003193 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003194 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003195 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003196 msleep(msec);
Fabio Estevamf4444572015-10-28 10:20:30 -02003197 gpio_set_value_cansleep(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003198}
3199#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003200static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003201{
3202 /*
3203 * In case of platform probe, the reset has been done
3204 * by machine code.
3205 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003206}
3207#endif /* CONFIG_OF */
3208
Fugang Duan9fc095f2014-09-13 05:00:49 +08003209static void
3210fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3211{
3212 struct device_node *np = pdev->dev.of_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003213
3214 *num_tx = *num_rx = 1;
3215
3216 if (!np || !of_device_is_available(np))
3217 return;
3218
3219 /* parse the num of tx and rx queues */
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303220 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003221
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303222 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003223
3224 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003225 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3226 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003227 *num_tx = 1;
3228 return;
3229 }
3230
3231 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003232 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3233 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003234 *num_rx = 1;
3235 return;
3236 }
3237
3238}
3239
Bill Pemberton33897cc2012-12-03 09:23:58 -05003240static int
Sascha Haueread73182009-01-28 23:03:11 +00003241fec_probe(struct platform_device *pdev)
3242{
3243 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003244 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003245 struct net_device *ndev;
3246 int i, irq, ret = 0;
3247 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003248 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003249 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003250 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003251 int num_tx_qs;
3252 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003253
Fugang Duan9fc095f2014-09-13 05:00:49 +08003254 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3255
Sascha Haueread73182009-01-28 23:03:11 +00003256 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003257 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3258 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003259 if (!ndev)
3260 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003261
3262 SET_NETDEV_DEV(ndev, &pdev->dev);
3263
3264 /* setup board info structure */
3265 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003266
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003267 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3268 if (of_id)
3269 pdev->id_entry = of_id->data;
3270 fep->quirks = pdev->id_entry->driver_data;
3271
Hubert Feurstein0c818592015-01-07 14:48:17 +01003272 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003273 fep->num_rx_queues = num_rx_qs;
3274 fep->num_tx_queues = num_tx_qs;
3275
Guenter Roeckd1391932013-06-18 10:04:59 -07003276#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003277 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003278 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003279 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003280#endif
Frank Libaa70a52013-01-16 16:55:58 +00003281
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003282 /* Select default pin state */
3283 pinctrl_pm_select_default_state(&pdev->dev);
3284
Fabio Estevam399db752013-07-21 13:25:03 -03003285 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303286 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3287 if (IS_ERR(fep->hwp)) {
3288 ret = PTR_ERR(fep->hwp);
3289 goto failed_ioremap;
3290 }
3291
Bryan Wue6b043d2010-03-31 02:10:44 +00003292 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003293 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003294
Sascha Haueread73182009-01-28 23:03:11 +00003295 platform_set_drvdata(pdev, ndev);
3296
Nimrod Andyde40ed32014-12-24 17:30:39 +08003297 if (of_get_property(np, "fsl,magic-packet", NULL))
3298 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3299
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003300 phy_node = of_parse_phandle(np, "phy-handle", 0);
3301 if (!phy_node && of_phy_is_fixed_link(np)) {
3302 ret = of_phy_register_fixed_link(np);
3303 if (ret < 0) {
3304 dev_err(&pdev->dev,
3305 "broken fixed-link specification\n");
3306 goto failed_phy;
3307 }
3308 phy_node = of_node_get(np);
3309 }
3310 fep->phy_node = phy_node;
3311
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003312 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003313 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003314 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003315 if (pdata)
3316 fep->phy_interface = pdata->phy;
3317 else
3318 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3319 } else {
3320 fep->phy_interface = ret;
3321 }
3322
Fabio Estevame2f8d552013-02-22 06:40:45 +00003323 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3324 if (IS_ERR(fep->clk_ipg)) {
3325 ret = PTR_ERR(fep->clk_ipg);
3326 goto failed_clk;
3327 }
3328
3329 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3330 if (IS_ERR(fep->clk_ahb)) {
3331 ret = PTR_ERR(fep->clk_ahb);
3332 goto failed_clk;
3333 }
3334
Fugang Duand851b472014-09-17 05:18:52 +08003335 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3336
Linus Torvalds38f56f32013-05-07 11:06:17 -07003337 /* enet_out is optional, depends on board */
3338 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3339 if (IS_ERR(fep->clk_enet_out))
3340 fep->clk_enet_out = NULL;
3341
Nimrod Andy91c0d982014-08-21 17:09:38 +08003342 fep->ptp_clk_on = false;
3343 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003344
3345 /* clk_ref is optional, depends on board */
3346 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3347 if (IS_ERR(fep->clk_ref))
3348 fep->clk_ref = NULL;
3349
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003350 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003351 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003352 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003353 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003354 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003355 }
3356
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003357 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003358 if (ret)
3359 goto failed_clk;
3360
Andrew Lunn8fff7552015-07-25 22:38:02 +02003361 ret = clk_prepare_enable(fep->clk_ipg);
3362 if (ret)
3363 goto failed_clk_ipg;
3364
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003365 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3366 if (!IS_ERR(fep->reg_phy)) {
3367 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003368 if (ret) {
3369 dev_err(&pdev->dev,
3370 "Failed to enable phy regulator: %d\n", ret);
3371 goto failed_regulator;
3372 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003373 } else {
3374 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003375 }
3376
Andrew Lunn8fff7552015-07-25 22:38:02 +02003377 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3378 pm_runtime_use_autosuspend(&pdev->dev);
Lucas Stach14d2b7c2015-08-03 17:50:11 +02003379 pm_runtime_get_noresume(&pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003380 pm_runtime_set_active(&pdev->dev);
3381 pm_runtime_enable(&pdev->dev);
3382
Fabio Estevame2f8d552013-02-22 06:40:45 +00003383 fec_reset_phy(pdev);
3384
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003385 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003386 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003387
3388 ret = fec_enet_init(ndev);
3389 if (ret)
3390 goto failed_init;
3391
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003392 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003393 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003394 if (irq < 0) {
3395 if (i)
3396 break;
3397 ret = irq;
3398 goto failed_irq;
3399 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003400 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003401 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003402 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003403 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003404
3405 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003406 }
3407
Fugang Duanb4d39b52014-09-13 05:00:55 +08003408 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003409 ret = fec_enet_mii_init(pdev);
3410 if (ret)
3411 goto failed_mii_init;
3412
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003413 /* Carrier starts down, phylib will bring it up */
3414 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003415 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003416 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003417
Sascha Haueread73182009-01-28 23:03:11 +00003418 ret = register_netdev(ndev);
3419 if (ret)
3420 goto failed_register;
3421
Nimrod Andyde40ed32014-12-24 17:30:39 +08003422 device_init_wakeup(&ndev->dev, fep->wol_flag &
3423 FEC_WOL_HAS_MAGIC_PACKET);
3424
Fabio Estevameb1d0642013-04-13 07:25:36 +00003425 if (fep->bufdesc_ex && fep->ptp_clock)
3426 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3427
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003428 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003429 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003430
3431 pm_runtime_mark_last_busy(&pdev->dev);
3432 pm_runtime_put_autosuspend(&pdev->dev);
3433
Sascha Haueread73182009-01-28 23:03:11 +00003434 return 0;
3435
3436failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003437 fec_enet_mii_remove(fep);
3438failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003439failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003440failed_init:
Lucas Stach32cba572015-07-23 16:06:20 +02003441 fec_ptp_stop(pdev);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003442 if (fep->reg_phy)
3443 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003444failed_regulator:
Andrew Lunn8fff7552015-07-25 22:38:02 +02003445 clk_disable_unprepare(fep->clk_ipg);
3446failed_clk_ipg:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003447 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003448failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003449failed_phy:
3450 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003451failed_ioremap:
3452 free_netdev(ndev);
3453
3454 return ret;
3455}
3456
Bill Pemberton33897cc2012-12-03 09:23:58 -05003457static int
Sascha Haueread73182009-01-28 23:03:11 +00003458fec_drv_remove(struct platform_device *pdev)
3459{
3460 struct net_device *ndev = platform_get_drvdata(pdev);
3461 struct fec_enet_private *fep = netdev_priv(ndev);
3462
Russell King36cdc742014-07-08 13:01:44 +01003463 cancel_work_sync(&fep->tx_timeout_work);
Lucas Stach32cba572015-07-23 16:06:20 +02003464 fec_ptp_stop(pdev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003465 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003466 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003467 if (fep->reg_phy)
3468 regulator_disable(fep->reg_phy);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003469 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003470 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003471
Sascha Haueread73182009-01-28 23:03:11 +00003472 return 0;
3473}
3474
Fabio Estevamdd66d382014-07-24 18:24:01 -03003475static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003476{
Eric Benard87cad5c2010-06-18 04:19:54 +00003477 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003478 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003479
Russell Kingda1774e2014-07-08 12:39:57 +01003480 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003481 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003482 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3483 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Russell Kingd76cfae2014-07-08 00:23:04 +01003484 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003485 napi_disable(&fep->napi);
3486 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003487 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003488 netif_tx_unlock_bh(ndev);
3489 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003490 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003491 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3492 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003493 }
Russell Kingda1774e2014-07-08 12:39:57 +01003494 rtnl_unlock();
3495
Nimrod Andyde40ed32014-12-24 17:30:39 +08003496 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003497 regulator_disable(fep->reg_phy);
3498
Nimrod Andy858eeb72014-12-11 09:20:31 +08003499 /* SOC supply clock to phy, when clock is disabled, phy link down
3500 * SOC control phy regulator, when regulator is disabled, phy link down
3501 */
3502 if (fep->clk_enet_out || fep->reg_phy)
3503 fep->link = 0;
3504
Sascha Haueread73182009-01-28 23:03:11 +00003505 return 0;
3506}
3507
Fabio Estevamdd66d382014-07-24 18:24:01 -03003508static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003509{
Eric Benard87cad5c2010-06-18 04:19:54 +00003510 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003511 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003512 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003513 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003514 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003515
Nimrod Andyde40ed32014-12-24 17:30:39 +08003516 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003517 ret = regulator_enable(fep->reg_phy);
3518 if (ret)
3519 return ret;
3520 }
Sascha Haueread73182009-01-28 23:03:11 +00003521
Russell Kingda1774e2014-07-08 12:39:57 +01003522 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003523 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003524 ret = fec_enet_clk_enable(ndev, true);
3525 if (ret) {
3526 rtnl_unlock();
3527 goto failed_clk;
3528 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003529 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3530 if (pdata && pdata->sleep_mode_enable)
3531 pdata->sleep_mode_enable(false);
3532 val = readl(fep->hwp + FEC_ECNTRL);
3533 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3534 writel(val, fep->hwp + FEC_ECNTRL);
3535 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3536 } else {
3537 pinctrl_pm_select_default_state(&fep->pdev->dev);
3538 }
Russell Kingef833372014-07-08 12:40:43 +01003539 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003540 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003541 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003542 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003543 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003544 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003545 }
Russell Kingda1774e2014-07-08 12:39:57 +01003546 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003547
Sascha Haueread73182009-01-28 23:03:11 +00003548 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003549
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003550failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003551 if (fep->reg_phy)
3552 regulator_disable(fep->reg_phy);
3553 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003554}
3555
Andrew Lunn8fff7552015-07-25 22:38:02 +02003556static int __maybe_unused fec_runtime_suspend(struct device *dev)
3557{
3558 struct net_device *ndev = dev_get_drvdata(dev);
3559 struct fec_enet_private *fep = netdev_priv(ndev);
3560
3561 clk_disable_unprepare(fep->clk_ipg);
3562
3563 return 0;
3564}
3565
3566static int __maybe_unused fec_runtime_resume(struct device *dev)
3567{
3568 struct net_device *ndev = dev_get_drvdata(dev);
3569 struct fec_enet_private *fep = netdev_priv(ndev);
3570
3571 return clk_prepare_enable(fep->clk_ipg);
3572}
3573
3574static const struct dev_pm_ops fec_pm_ops = {
3575 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3576 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3577};
Denis Kirjanov59d42892010-06-02 09:27:04 +00003578
Sascha Haueread73182009-01-28 23:03:11 +00003579static struct platform_driver fec_driver = {
3580 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003581 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003582 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003583 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003584 },
Shawn Guob5680e02011-01-05 21:13:13 +00003585 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003586 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003587 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003588};
3589
Fabio Estevamaaca2372012-01-23 16:44:50 +00003590module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003592MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593MODULE_LICENSE("GPL");