blob: 97ca72a6aaf4b8035c192ce0c7700ec01d6ee7ce [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;
331 int frag, frag_len;
332 unsigned short status;
333 unsigned int estatus = 0;
334 skb_frag_t *this_frag;
335 unsigned int index;
336 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100337 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800338 int i;
339
340 for (frag = 0; frag < nr_frags; frag++) {
341 this_frag = &skb_shinfo(skb)->frags[frag];
Troy Kisky7355f272016-02-05 14:52:46 -0700342 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800343 ebdp = (struct bufdesc_ex *)bdp;
344
Johannes Berg5cfa3032016-01-24 16:52:37 +0100345 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800346 status &= ~BD_ENET_TX_STATS;
347 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
348 frag_len = skb_shinfo(skb)->frags[frag].size;
349
350 /* Handle the last BD specially */
351 if (frag == nr_frags - 1) {
352 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
353 if (fep->bufdesc_ex) {
354 estatus |= BD_ENET_TX_INT;
355 if (unlikely(skb_shinfo(skb)->tx_flags &
356 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
357 estatus |= BD_ENET_TX_TS;
358 }
359 }
360
361 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100362 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700363 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy6e909282014-06-12 08:16:22 +0800364 if (skb->ip_summed == CHECKSUM_PARTIAL)
365 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
366 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100367 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800368 }
369
370 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
371
Troy Kisky7355f272016-02-05 14:52:46 -0700372 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800373 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100374 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800375 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
376 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800377
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100378 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800379 swap_buffer(bufaddr, frag_len);
380 }
381
Russell Kingd6bf3142014-07-08 00:23:19 +0100382 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
383 DMA_TO_DEVICE);
384 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800385 dev_kfree_skb_any(skb);
386 if (net_ratelimit())
387 netdev_err(ndev, "Tx DMA memory map failed\n");
388 goto dma_mapping_error;
389 }
390
Johannes Berg5cfa3032016-01-24 16:52:37 +0100391 bdp->cbd_bufaddr = cpu_to_fec32(addr);
392 bdp->cbd_datlen = cpu_to_fec16(frag_len);
Troy Kiskybe293462016-02-05 14:52:49 -0700393 /* Make sure the updates to rest of the descriptor are
394 * performed before transferring ownership.
395 */
396 wmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +0100397 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800398 }
399
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800400 return bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800401dma_mapping_error:
Troy Kisky7355f272016-02-05 14:52:46 -0700402 bdp = txq->bd.cur;
Nimrod Andy6e909282014-06-12 08:16:22 +0800403 for (i = 0; i < frag; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -0700404 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100405 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
406 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
Nimrod Andy6e909282014-06-12 08:16:22 +0800407 }
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800408 return ERR_PTR(-ENOMEM);
Nimrod Andy6e909282014-06-12 08:16:22 +0800409}
410
Fugang Duan4d494cd2014-09-13 05:00:48 +0800411static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
412 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800413{
414 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800415 int nr_frags = skb_shinfo(skb)->nr_frags;
416 struct bufdesc *bdp, *last_bdp;
417 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100418 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800419 unsigned short status;
420 unsigned short buflen;
421 unsigned int estatus = 0;
422 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800423 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800424
Troy Kisky7355f272016-02-05 14:52:46 -0700425 entries_free = fec_enet_get_free_txdesc_num(txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800426 if (entries_free < MAX_SKB_FRAGS + 1) {
427 dev_kfree_skb_any(skb);
428 if (net_ratelimit())
429 netdev_err(ndev, "NOT enough BD for SG!\n");
430 return NETDEV_TX_OK;
431 }
432
Nimrod Andy6e909282014-06-12 08:16:22 +0800433 /* Protocol checksum off-load for TCP and UDP. */
434 if (fec_enet_clear_csum(skb, ndev)) {
435 dev_kfree_skb_any(skb);
436 return NETDEV_TX_OK;
437 }
438
439 /* Fill in a Tx ring entry */
Troy Kisky7355f272016-02-05 14:52:46 -0700440 bdp = txq->bd.cur;
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800441 last_bdp = bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100442 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800443 status &= ~BD_ENET_TX_STATS;
444
445 /* Set buffer length and buffer pointer */
446 bufaddr = skb->data;
447 buflen = skb_headlen(skb);
448
Troy Kisky7355f272016-02-05 14:52:46 -0700449 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800450 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100451 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800452 memcpy(txq->tx_bounce[index], skb->data, buflen);
453 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800454
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100455 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800456 swap_buffer(bufaddr, buflen);
457 }
458
Russell Kingd6bf3142014-07-08 00:23:19 +0100459 /* Push the data cache so the CPM does not get stale memory data. */
460 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
461 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800462 dev_kfree_skb_any(skb);
463 if (net_ratelimit())
464 netdev_err(ndev, "Tx DMA memory map failed\n");
465 return NETDEV_TX_OK;
466 }
467
468 if (nr_frags) {
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800469 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
470 if (IS_ERR(last_bdp))
471 return NETDEV_TX_OK;
Nimrod Andy6e909282014-06-12 08:16:22 +0800472 } else {
473 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
474 if (fep->bufdesc_ex) {
475 estatus = BD_ENET_TX_INT;
476 if (unlikely(skb_shinfo(skb)->tx_flags &
477 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
478 estatus |= BD_ENET_TX_TS;
479 }
480 }
481
482 if (fep->bufdesc_ex) {
483
484 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
485
486 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
487 fep->hwts_tx_en))
488 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
489
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100490 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700491 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andybefe8212014-09-23 15:40:55 +0800492
Nimrod Andy6e909282014-06-12 08:16:22 +0800493 if (skb->ip_summed == CHECKSUM_PARTIAL)
494 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
495
496 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100497 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800498 }
499
Troy Kisky7355f272016-02-05 14:52:46 -0700500 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800501 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800502 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800503
Johannes Berg5cfa3032016-01-24 16:52:37 +0100504 bdp->cbd_datlen = cpu_to_fec16(buflen);
505 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Troy Kiskybe293462016-02-05 14:52:49 -0700506 /* Make sure the updates to rest of the descriptor are performed before
507 * transferring ownership.
508 */
509 wmb();
Nimrod Andy6e909282014-06-12 08:16:22 +0800510
511 /* Send it on its way. Tell FEC it's ready, interrupt when done,
512 * it's the last BD of the frame, and to put the CRC on the end.
513 */
514 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100515 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800516
Sascha Hauer22f6b862009-04-15 01:32:18 +0000517 /* If this was the last BD in the ring, start at the beginning again. */
Troy Kisky7355f272016-02-05 14:52:46 -0700518 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800520 skb_tx_timestamp(skb);
521
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800522 /* Make sure the update to bdp and tx_skbuff are performed before
Troy Kisky7355f272016-02-05 14:52:46 -0700523 * txq->bd.cur.
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800524 */
525 wmb();
Troy Kisky7355f272016-02-05 14:52:46 -0700526 txq->bd.cur = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Frank Lide5fb0a2013-03-03 17:34:25 +0000528 /* Trigger transmission start */
Troy Kisky53bb20d2016-02-05 14:52:47 -0700529 writel(0, txq->bd.reg_desc_active);
Frank Lide5fb0a2013-03-03 17:34:25 +0000530
Nimrod Andy6e909282014-06-12 08:16:22 +0800531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Nimrod Andy79f33912014-06-12 08:16:23 +0800534static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800535fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
536 struct net_device *ndev,
537 struct bufdesc *bdp, int index, char *data,
538 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800539{
540 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200541 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800542 unsigned short status;
543 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100544 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800545
Johannes Berg5cfa3032016-01-24 16:52:37 +0100546 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800547 status &= ~BD_ENET_TX_STATS;
548
549 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800550
Fugang Duan41ef84c2014-09-13 05:00:54 +0800551 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100552 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800553 memcpy(txq->tx_bounce[index], data, size);
554 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800555
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100556 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800557 swap_buffer(data, size);
558 }
559
Russell Kingd6bf3142014-07-08 00:23:19 +0100560 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
561 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800562 dev_kfree_skb_any(skb);
563 if (net_ratelimit())
564 netdev_err(ndev, "Tx DMA memory map failed\n");
565 return NETDEV_TX_BUSY;
566 }
567
Johannes Berg5cfa3032016-01-24 16:52:37 +0100568 bdp->cbd_datlen = cpu_to_fec16(size);
569 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Russell Kingd6bf3142014-07-08 00:23:19 +0100570
Nimrod Andy79f33912014-06-12 08:16:23 +0800571 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100572 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700573 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy79f33912014-06-12 08:16:23 +0800574 if (skb->ip_summed == CHECKSUM_PARTIAL)
575 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
576 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100577 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800578 }
579
580 /* Handle the last BD specially */
581 if (last_tcp)
582 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
583 if (is_last) {
584 status |= BD_ENET_TX_INTR;
585 if (fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100586 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
Nimrod Andy79f33912014-06-12 08:16:23 +0800587 }
588
Johannes Berg5cfa3032016-01-24 16:52:37 +0100589 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800590
591 return 0;
592}
593
594static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800595fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
596 struct sk_buff *skb, struct net_device *ndev,
597 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800598{
599 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800600 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200601 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800602 void *bufaddr;
603 unsigned long dmabuf;
604 unsigned short status;
605 unsigned int estatus = 0;
606
Johannes Berg5cfa3032016-01-24 16:52:37 +0100607 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800608 status &= ~BD_ENET_TX_STATS;
609 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
610
Fugang Duan4d494cd2014-09-13 05:00:48 +0800611 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
612 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800613 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100614 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800615 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
616 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800617
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100618 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800619 swap_buffer(bufaddr, hdr_len);
620
621 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
622 hdr_len, DMA_TO_DEVICE);
623 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
624 dev_kfree_skb_any(skb);
625 if (net_ratelimit())
626 netdev_err(ndev, "Tx DMA memory map failed\n");
627 return NETDEV_TX_BUSY;
628 }
629 }
630
Johannes Berg5cfa3032016-01-24 16:52:37 +0100631 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
632 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
Nimrod Andy79f33912014-06-12 08:16:23 +0800633
634 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100635 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700636 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy79f33912014-06-12 08:16:23 +0800637 if (skb->ip_summed == CHECKSUM_PARTIAL)
638 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
639 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100640 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800641 }
642
Johannes Berg5cfa3032016-01-24 16:52:37 +0100643 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800644
645 return 0;
646}
647
Fugang Duan4d494cd2014-09-13 05:00:48 +0800648static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
649 struct sk_buff *skb,
650 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800651{
652 struct fec_enet_private *fep = netdev_priv(ndev);
653 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
654 int total_len, data_left;
Troy Kisky7355f272016-02-05 14:52:46 -0700655 struct bufdesc *bdp = txq->bd.cur;
Nimrod Andy79f33912014-06-12 08:16:23 +0800656 struct tso_t tso;
657 unsigned int index = 0;
658 int ret;
659
Troy Kisky7355f272016-02-05 14:52:46 -0700660 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800661 dev_kfree_skb_any(skb);
662 if (net_ratelimit())
663 netdev_err(ndev, "NOT enough BD for TSO!\n");
664 return NETDEV_TX_OK;
665 }
666
667 /* Protocol checksum off-load for TCP and UDP. */
668 if (fec_enet_clear_csum(skb, ndev)) {
669 dev_kfree_skb_any(skb);
670 return NETDEV_TX_OK;
671 }
672
673 /* Initialize the TSO handler, and prepare the first payload */
674 tso_start(skb, &tso);
675
676 total_len = skb->len - hdr_len;
677 while (total_len > 0) {
678 char *hdr;
679
Troy Kisky7355f272016-02-05 14:52:46 -0700680 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800681 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
682 total_len -= data_left;
683
684 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800685 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800686 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800687 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800688 if (ret)
689 goto err_release;
690
691 while (data_left > 0) {
692 int size;
693
694 size = min_t(int, tso.size, data_left);
Troy Kisky7355f272016-02-05 14:52:46 -0700695 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
696 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800697 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
698 bdp, index,
699 tso.data, size,
700 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800701 total_len == 0);
702 if (ret)
703 goto err_release;
704
705 data_left -= size;
706 tso_build_data(skb, &tso, size);
707 }
708
Troy Kisky7355f272016-02-05 14:52:46 -0700709 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800710 }
711
712 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800713 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800714
Nimrod Andy79f33912014-06-12 08:16:23 +0800715 skb_tx_timestamp(skb);
Troy Kisky7355f272016-02-05 14:52:46 -0700716 txq->bd.cur = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800717
718 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100719 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Troy Kisky53bb20d2016-02-05 14:52:47 -0700720 !readl(txq->bd.reg_desc_active) ||
721 !readl(txq->bd.reg_desc_active) ||
722 !readl(txq->bd.reg_desc_active) ||
723 !readl(txq->bd.reg_desc_active))
724 writel(0, txq->bd.reg_desc_active);
Nimrod Andy79f33912014-06-12 08:16:23 +0800725
726 return 0;
727
728err_release:
729 /* TODO: Release all used data descriptors for TSO */
730 return ret;
731}
732
Nimrod Andy61a44272014-06-12 08:16:18 +0800733static netdev_tx_t
734fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
735{
736 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800737 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800738 unsigned short queue;
739 struct fec_enet_priv_tx_q *txq;
740 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800741 int ret;
742
Fugang Duan4d494cd2014-09-13 05:00:48 +0800743 queue = skb_get_queue_mapping(skb);
744 txq = fep->tx_queue[queue];
745 nq = netdev_get_tx_queue(ndev, queue);
746
Nimrod Andy79f33912014-06-12 08:16:23 +0800747 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800748 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800749 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800750 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800751 if (ret)
752 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800753
Troy Kisky7355f272016-02-05 14:52:46 -0700754 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800755 if (entries_free <= txq->tx_stop_threshold)
756 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800757
758 return NETDEV_TX_OK;
759}
760
Frank Li14109a52013-03-26 16:12:03 +0000761/* Init RX & TX buffer descriptors
762 */
763static void fec_enet_bd_init(struct net_device *dev)
764{
765 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800766 struct fec_enet_priv_tx_q *txq;
767 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000768 struct bufdesc *bdp;
769 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800770 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000771
Frank Li59d0f7462014-09-13 05:00:50 +0800772 for (q = 0; q < fep->num_rx_queues; q++) {
773 /* Initialize the receive buffer descriptors. */
774 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700775 bdp = rxq->bd.base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800776
Troy Kisky7355f272016-02-05 14:52:46 -0700777 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000778
Frank Li59d0f7462014-09-13 05:00:50 +0800779 /* Initialize the BD for every fragment in the page. */
780 if (bdp->cbd_bufaddr)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100781 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Li59d0f7462014-09-13 05:00:50 +0800782 else
Johannes Berg5cfa3032016-01-24 16:52:37 +0100783 bdp->cbd_sc = cpu_to_fec16(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700784 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Frank Li14109a52013-03-26 16:12:03 +0000785 }
Frank Li59d0f7462014-09-13 05:00:50 +0800786
787 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700788 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100789 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800790
Troy Kisky7355f272016-02-05 14:52:46 -0700791 rxq->bd.cur = rxq->bd.base;
Frank Li14109a52013-03-26 16:12:03 +0000792 }
793
Frank Li59d0f7462014-09-13 05:00:50 +0800794 for (q = 0; q < fep->num_tx_queues; q++) {
795 /* ...and the same for transmit */
796 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700797 bdp = txq->bd.base;
798 txq->bd.cur = bdp;
Frank Li59d0f7462014-09-13 05:00:50 +0800799
Troy Kisky7355f272016-02-05 14:52:46 -0700800 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800801 /* Initialize the BD for every fragment in the page. */
Johannes Berg5cfa3032016-01-24 16:52:37 +0100802 bdp->cbd_sc = cpu_to_fec16(0);
Frank Li59d0f7462014-09-13 05:00:50 +0800803 if (txq->tx_skbuff[i]) {
804 dev_kfree_skb_any(txq->tx_skbuff[i]);
805 txq->tx_skbuff[i] = NULL;
806 }
Johannes Berg5cfa3032016-01-24 16:52:37 +0100807 bdp->cbd_bufaddr = cpu_to_fec32(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700808 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Li59d0f7462014-09-13 05:00:50 +0800809 }
810
811 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700812 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100813 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800814 txq->dirty_tx = bdp;
815 }
816}
817
Frank Lice99d0d2014-09-13 05:00:52 +0800818static void fec_enet_active_rxring(struct net_device *ndev)
819{
820 struct fec_enet_private *fep = netdev_priv(ndev);
821 int i;
822
823 for (i = 0; i < fep->num_rx_queues; i++)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700824 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
Frank Lice99d0d2014-09-13 05:00:52 +0800825}
826
Frank Li59d0f7462014-09-13 05:00:50 +0800827static void fec_enet_enable_ring(struct net_device *ndev)
828{
829 struct fec_enet_private *fep = netdev_priv(ndev);
830 struct fec_enet_priv_tx_q *txq;
831 struct fec_enet_priv_rx_q *rxq;
832 int i;
833
834 for (i = 0; i < fep->num_rx_queues; i++) {
835 rxq = fep->rx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700836 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
Nimrod Andyd543a762014-11-23 17:23:06 +0800837 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800838
839 /* enable DMA1/2 */
840 if (i)
841 writel(RCMR_MATCHEN | RCMR_CMP(i),
842 fep->hwp + FEC_RCMR(i));
843 }
844
845 for (i = 0; i < fep->num_tx_queues; i++) {
846 txq = fep->tx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700847 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800848
849 /* enable DMA1/2 */
850 if (i)
851 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
852 fep->hwp + FEC_DMA_CFG(i));
853 }
854}
855
856static void fec_enet_reset_skb(struct net_device *ndev)
857{
858 struct fec_enet_private *fep = netdev_priv(ndev);
859 struct fec_enet_priv_tx_q *txq;
860 int i, j;
861
862 for (i = 0; i < fep->num_tx_queues; i++) {
863 txq = fep->tx_queue[i];
864
Troy Kisky7355f272016-02-05 14:52:46 -0700865 for (j = 0; j < txq->bd.ring_size; j++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800866 if (txq->tx_skbuff[j]) {
867 dev_kfree_skb_any(txq->tx_skbuff[j]);
868 txq->tx_skbuff[j] = NULL;
869 }
870 }
871 }
Frank Li14109a52013-03-26 16:12:03 +0000872}
873
Russell Kingdbc64a82014-07-08 12:40:12 +0100874/*
875 * This function is called to start or restart the FEC during a link
876 * change, transmit timeout, or to reconfigure the FEC. The network
877 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100878 */
879static void
Russell Kingef833372014-07-08 12:40:43 +0100880fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100881{
882 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000883 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100884 u32 temp_mac[2];
885 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000886 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100887
Fugang Duan106c3142014-09-13 05:00:51 +0800888 /* Whack a reset. We should wait for this.
889 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
890 * instead of reset MAC itself.
891 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100892 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800893 writel(0, fep->hwp + FEC_ECNTRL);
894 } else {
895 writel(1, fep->hwp + FEC_ECNTRL);
896 udelay(10);
897 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100898
899 /*
900 * enet-mac reset will reset mac address registers too,
901 * so need to reconfigure it.
902 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100903 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100904 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100905 writel((__force u32)cpu_to_be32(temp_mac[0]),
906 fep->hwp + FEC_ADDR_LOW);
907 writel((__force u32)cpu_to_be32(temp_mac[1]),
908 fep->hwp + FEC_ADDR_HIGH);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100909 }
910
911 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800912 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100913
Frank Li14109a52013-03-26 16:12:03 +0000914 fec_enet_bd_init(ndev);
915
Frank Li59d0f7462014-09-13 05:00:50 +0800916 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100917
Frank Li59d0f7462014-09-13 05:00:50 +0800918 /* Reset tx SKB buffers. */
919 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100920
921 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100922 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100923 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100924 writel(0x04, fep->hwp + FEC_X_CNTRL);
925 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100926 /* No Rcv on Xmit */
927 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100928 writel(0x0, fep->hwp + FEC_X_CNTRL);
929 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100930
Uwe Kleine-König45993652011-01-19 20:47:04 +0100931 /* Set MII speed */
932 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
933
Guenter Roeckd1391932013-06-18 10:04:59 -0700934#if !defined(CONFIG_M5272)
Greg Ungerer18803492015-06-20 15:51:57 +1000935 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
936 /* set RX checksum */
937 val = readl(fep->hwp + FEC_RACC);
938 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
939 val |= FEC_RACC_OPTIONS;
940 else
941 val &= ~FEC_RACC_OPTIONS;
942 writel(val, fep->hwp + FEC_RACC);
943 }
Troy Kisky55cd48c2016-02-05 14:52:43 -0700944 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
Guenter Roeckd1391932013-06-18 10:04:59 -0700945#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000946
Uwe Kleine-König45993652011-01-19 20:47:04 +0100947 /*
948 * The phy interface and speed need to get configured
949 * differently on enet-mac.
950 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100951 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100952 /* Enable flow control and length check */
953 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100954
Shawn Guo230dec62011-09-23 02:12:48 +0000955 /* RGMII, RMII or MII */
Markus Pargmanne813bb22015-04-30 17:07:50 +0200956 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
957 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
958 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
959 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shawn Guo230dec62011-09-23 02:12:48 +0000960 rcntl |= (1 << 6);
961 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100962 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100963 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100964 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100965
Shawn Guo230dec62011-09-23 02:12:48 +0000966 /* 1G, 100M or 10M */
967 if (fep->phy_dev) {
968 if (fep->phy_dev->speed == SPEED_1000)
969 ecntl |= (1 << 5);
970 else if (fep->phy_dev->speed == SPEED_100)
971 rcntl &= ~(1 << 9);
972 else
973 rcntl |= (1 << 9);
974 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975 } else {
976#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100977 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000978 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100979 /* disable the gasket and wait */
980 writel(0, fep->hwp + FEC_MIIGSK_ENR);
981 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
982 udelay(1);
983
984 /*
985 * configure the gasket:
986 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800987 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100988 */
Eric Benard8d82f212012-01-12 06:10:28 +0000989 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
990 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
991 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
992 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
993 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100994
995 /* re-enable the gasket */
996 writel(2, fep->hwp + FEC_MIIGSK_ENR);
997 }
998#endif
999 }
Frank Libaa70a52013-01-16 16:55:58 +00001000
Guenter Roeckd1391932013-06-18 10:04:59 -07001001#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001002 /* enable pause frame*/
1003 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1004 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1005 fep->phy_dev && fep->phy_dev->pause)) {
1006 rcntl |= FEC_ENET_FCE;
1007
Jim Baxter4c09eed2013-04-19 08:10:49 +00001008 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001009 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1010 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1011 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1012 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1013
1014 /* OPD */
1015 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1016 } else {
1017 rcntl &= ~FEC_ENET_FCE;
1018 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001019#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001020
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001021 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001022
Stefan Wahren84fe6182014-03-12 11:28:19 +01001023 /* Setup multicast filter. */
1024 set_multicast_list(ndev);
1025#ifndef CONFIG_M5272
1026 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1027 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1028#endif
1029
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001030 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001031 /* enable ENET endian swap */
1032 ecntl |= (1 << 8);
1033 /* enable ENET store and forward mode */
1034 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1035 }
1036
Frank Liff43da82013-01-03 16:04:23 +00001037 if (fep->bufdesc_ex)
1038 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001039
Chris Healy38ae92d2013-06-25 23:18:52 -07001040#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001041 /* Enable the MIB statistic event counters */
1042 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001043#endif
1044
Uwe Kleine-König45993652011-01-19 20:47:04 +01001045 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001046 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001047 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001048
Frank Liff43da82013-01-03 16:04:23 +00001049 if (fep->bufdesc_ex)
1050 fec_ptp_start_cyclecounter(ndev);
1051
Uwe Kleine-König45993652011-01-19 20:47:04 +01001052 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001053 if (fep->link)
1054 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1055 else
1056 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001057
1058 /* Init the interrupt coalescing */
1059 fec_enet_itr_coal_init(ndev);
1060
Uwe Kleine-König45993652011-01-19 20:47:04 +01001061}
1062
1063static void
1064fec_stop(struct net_device *ndev)
1065{
1066 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001067 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001068 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001069 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001070
1071 /* We cannot expect a graceful transmit stop without link !!! */
1072 if (fep->link) {
1073 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1074 udelay(10);
1075 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001076 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001077 }
1078
Fugang Duan106c3142014-09-13 05:00:51 +08001079 /* Whack a reset. We should wait for this.
1080 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1081 * instead of reset MAC itself.
1082 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001083 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1084 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1085 writel(0, fep->hwp + FEC_ECNTRL);
1086 } else {
1087 writel(1, fep->hwp + FEC_ECNTRL);
1088 udelay(10);
1089 }
1090 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001091 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001092 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1093 val = readl(fep->hwp + FEC_ECNTRL);
1094 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1095 writel(val, fep->hwp + FEC_ECNTRL);
1096
1097 if (pdata && pdata->sleep_mode_enable)
1098 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001099 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001100 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001101
1102 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001103 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1104 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001105 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001106 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1107 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001108}
1109
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001112fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001114 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Russell King344756f2014-07-08 13:01:59 +01001116 fec_dump(ndev);
1117
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001118 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Russell King36cdc742014-07-08 13:01:44 +01001120 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001121}
1122
Russell King36cdc742014-07-08 13:01:44 +01001123static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001124{
1125 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001126 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001127 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001128
Russell King36cdc742014-07-08 13:01:44 +01001129 rtnl_lock();
1130 if (netif_device_present(ndev) || netif_running(ndev)) {
1131 napi_disable(&fep->napi);
1132 netif_tx_lock_bh(ndev);
1133 fec_restart(ndev);
1134 netif_wake_queue(ndev);
1135 netif_tx_unlock_bh(ndev);
1136 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001137 }
Russell King36cdc742014-07-08 13:01:44 +01001138 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001142fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1143 struct skb_shared_hwtstamps *hwtstamps)
1144{
1145 unsigned long flags;
1146 u64 ns;
1147
1148 spin_lock_irqsave(&fep->tmreg_lock, flags);
1149 ns = timecounter_cyc2time(&fep->tc, ts);
1150 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1151
1152 memset(hwtstamps, 0, sizeof(*hwtstamps));
1153 hwtstamps->hwtstamp = ns_to_ktime(ns);
1154}
1155
1156static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001157fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct fec_enet_private *fep;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001160 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001161 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001163 struct fec_enet_priv_tx_q *txq;
1164 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001165 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001166 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001168 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001169
1170 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1171
1172 txq = fep->tx_queue[queue_id];
1173 /* get next bdp of dirty_tx */
1174 nq = netdev_get_tx_queue(ndev, queue_id);
1175 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Frank Lide5fb0a2013-03-03 17:34:25 +00001177 /* get next bdp of dirty_tx */
Troy Kisky7355f272016-02-05 14:52:46 -07001178 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Lide5fb0a2013-03-03 17:34:25 +00001179
Troy Kisky7355f272016-02-05 14:52:46 -07001180 while (bdp != READ_ONCE(txq->bd.cur)) {
1181 /* Order the load of bd.cur and cbd_sc */
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001182 rmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +01001183 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001184 if (status & BD_ENET_TX_READY)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001185 break;
1186
Troy Kisky7355f272016-02-05 14:52:46 -07001187 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy2b995f62015-01-20 14:10:35 +08001188
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001189 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001190 txq->tx_skbuff[index] = NULL;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001191 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1192 dma_unmap_single(&fep->pdev->dev,
1193 fec32_to_cpu(bdp->cbd_bufaddr),
1194 fec16_to_cpu(bdp->cbd_datlen),
1195 DMA_TO_DEVICE);
1196 bdp->cbd_bufaddr = cpu_to_fec32(0);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001197 if (!skb) {
Troy Kisky7355f272016-02-05 14:52:46 -07001198 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001199 continue;
1200 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001203 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 BD_ENET_TX_RL | BD_ENET_TX_UN |
1205 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001206 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001207 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001208 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001209 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001210 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001211 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001212 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001213 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001214 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001215 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001216 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001218 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001219 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
Frank Liff43da82013-01-03 16:04:23 +00001222 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1223 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001224 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001225 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001226
Johannes Berg5cfa3032016-01-24 16:52:37 +01001227 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001228 skb_tstamp_tx(skb, &shhwtstamps);
1229 }
Frank Liff43da82013-01-03 16:04:23 +00001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Deferred means some collisions occurred during transmit,
1232 * but we eventually sent the packet OK.
1233 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001234 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001235 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001236
Sascha Hauer22f6b862009-04-15 01:32:18 +00001237 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001239
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001240 /* Make sure the update to bdp and tx_skbuff are performed
1241 * before dirty_tx
1242 */
1243 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +08001244 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001245
Sascha Hauer22f6b862009-04-15 01:32:18 +00001246 /* Update pointer to next buffer descriptor to be transmitted */
Troy Kisky7355f272016-02-05 14:52:46 -07001247 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001248
Sascha Hauer22f6b862009-04-15 01:32:18 +00001249 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001251 if (netif_queue_stopped(ndev)) {
Troy Kisky7355f272016-02-05 14:52:46 -07001252 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001253 if (entries_free >= txq->tx_wake_threshold)
1254 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
Russell Kingccea2962014-07-08 13:01:38 +01001257
1258 /* ERR006538: Keep the transmitter going */
Troy Kisky7355f272016-02-05 14:52:46 -07001259 if (bdp != txq->bd.cur &&
Troy Kisky53bb20d2016-02-05 14:52:47 -07001260 readl(txq->bd.reg_desc_active) == 0)
1261 writel(0, txq->bd.reg_desc_active);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001262}
1263
1264static void
1265fec_enet_tx(struct net_device *ndev)
1266{
1267 struct fec_enet_private *fep = netdev_priv(ndev);
1268 u16 queue_id;
1269 /* First process class A queue, then Class B and Best Effort queue */
1270 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1271 clear_bit(queue_id, &fep->work_tx);
1272 fec_enet_tx_queue(ndev, queue_id);
1273 }
1274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001277static int
1278fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1279{
1280 struct fec_enet_private *fep = netdev_priv(ndev);
1281 int off;
1282
1283 off = ((unsigned long)skb->data) & fep->rx_align;
1284 if (off)
1285 skb_reserve(skb, fep->rx_align + 1 - off);
1286
Johannes Berg5cfa3032016-01-24 16:52:37 +01001287 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1288 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001289 if (net_ratelimit())
1290 netdev_err(ndev, "Rx DMA memory map failed\n");
1291 return -ENOMEM;
1292 }
1293
1294 return 0;
1295}
1296
1297static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001298 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001299{
1300 struct fec_enet_private *fep = netdev_priv(ndev);
1301 struct sk_buff *new_skb;
1302
1303 if (length > fep->rx_copybreak)
1304 return false;
1305
1306 new_skb = netdev_alloc_skb(ndev, length);
1307 if (!new_skb)
1308 return false;
1309
Johannes Berg5cfa3032016-01-24 16:52:37 +01001310 dma_sync_single_for_cpu(&fep->pdev->dev,
1311 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001312 FEC_ENET_RX_FRSIZE - fep->rx_align,
1313 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001314 if (!swap)
1315 memcpy(new_skb->data, (*skb)->data, length);
1316 else
1317 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001318 *skb = new_skb;
1319
1320 return true;
1321}
1322
Troy Kisky7355f272016-02-05 14:52:46 -07001323/* During a receive, the bd_rx.cur points to the current incoming buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * When we update through the ring, if the next incoming buffer has
1325 * not been given to the system, we just set the empty indicator,
1326 * effectively tossing the packet.
1327 */
Frank Lidc975382013-01-28 18:31:42 +00001328static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001329fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001331 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001332 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001333 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001334 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001335 struct sk_buff *skb_new = NULL;
1336 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 ushort pkt_len;
1338 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001339 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001340 struct bufdesc_ex *ebdp = NULL;
1341 bool vlan_packet_rcvd = false;
1342 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001343 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001344 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001345 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001346
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001347#ifdef CONFIG_M532x
1348 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001349#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001350 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1351 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* First, grab all of the stats for the incoming packet.
1354 * These get messed up if we get called due to a busy condition.
1355 */
Troy Kisky7355f272016-02-05 14:52:46 -07001356 bdp = rxq->bd.cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Johannes Berg5cfa3032016-01-24 16:52:37 +01001358 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Frank Lidc975382013-01-28 18:31:42 +00001360 if (pkt_received >= budget)
1361 break;
1362 pkt_received++;
1363
Russell Kinged63f1d2015-09-02 17:24:14 +08001364 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
Russell Kingdb3421c2014-07-08 13:01:49 +01001365
Sascha Hauer22f6b862009-04-15 01:32:18 +00001366 /* Check for errors. */
Troy Kisky095098e2016-02-05 14:52:44 -07001367 status ^= BD_ENET_RX_LAST;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001368 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Troy Kisky095098e2016-02-05 14:52:44 -07001369 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1370 BD_ENET_RX_CL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001371 ndev->stats.rx_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001372 if (status & BD_ENET_RX_OV) {
1373 /* FIFO overrun */
1374 ndev->stats.rx_fifo_errors++;
1375 goto rx_processing_done;
1376 }
1377 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1378 | BD_ENET_RX_LAST)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +00001379 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001380 ndev->stats.rx_length_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001381 if (status & BD_ENET_RX_LAST)
1382 netdev_err(ndev, "rcv is not +last\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001383 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001384 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001385 ndev->stats.rx_crc_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001386 /* Report late collisions as a frame error. */
1387 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1388 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001389 goto rx_processing_done;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Sascha Hauer22f6b862009-04-15 01:32:18 +00001392 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001393 ndev->stats.rx_packets++;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001394 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001395 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Troy Kisky7355f272016-02-05 14:52:46 -07001397 index = fec_enet_get_bd_index(bdp, &rxq->bd);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001398 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001399
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001400 /* The packet length includes FCS, but we don't want to
1401 * include that when passing upstream as it messes up
1402 * bridging applications.
1403 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001404 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1405 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001406 if (!is_copybreak) {
1407 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1408 if (unlikely(!skb_new)) {
1409 ndev->stats.rx_dropped++;
1410 goto rx_processing_done;
1411 }
Johannes Berg5cfa3032016-01-24 16:52:37 +01001412 dma_unmap_single(&fep->pdev->dev,
1413 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001414 FEC_ENET_RX_FRSIZE - fep->rx_align,
1415 DMA_FROM_DEVICE);
1416 }
1417
1418 prefetch(skb->data - NET_IP_ALIGN);
1419 skb_put(skb, pkt_len - 4);
1420 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001421 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001422 swap_buffer(data, pkt_len);
1423
Jim Baxtercdffcf12013-07-02 22:52:56 +01001424 /* Extract the enhanced buffer descriptor */
1425 ebdp = NULL;
1426 if (fep->bufdesc_ex)
1427 ebdp = (struct bufdesc_ex *)bdp;
1428
1429 /* If this is a VLAN packet remove the VLAN Tag */
1430 vlan_packet_rcvd = false;
1431 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Johannes Berg5cfa3032016-01-24 16:52:37 +01001432 fep->bufdesc_ex &&
1433 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001434 /* Push and remove the vlan tag */
1435 struct vlan_hdr *vlan_header =
1436 (struct vlan_hdr *) (data + ETH_HLEN);
1437 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001438
1439 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001440
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001441 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001442 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001443 }
1444
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001445 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001447 /* Get receive timestamp from the skb */
1448 if (fep->hwts_rx_en && fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +01001449 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001450 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001451
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001452 if (fep->bufdesc_ex &&
1453 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001454 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001455 /* don't check it */
1456 skb->ip_summed = CHECKSUM_UNNECESSARY;
1457 } else {
1458 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001459 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001460 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001461
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001462 /* Handle received VLAN packets */
1463 if (vlan_packet_rcvd)
1464 __vlan_hwaccel_put_tag(skb,
1465 htons(ETH_P_8021Q),
1466 vlan_tag);
1467
1468 napi_gro_receive(&fep->napi, skb);
1469
1470 if (is_copybreak) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001471 dma_sync_single_for_device(&fep->pdev->dev,
1472 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001473 FEC_ENET_RX_FRSIZE - fep->rx_align,
1474 DMA_FROM_DEVICE);
1475 } else {
1476 rxq->rx_skbuff[index] = skb_new;
1477 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1478 }
1479
Sascha Hauer22f6b862009-04-15 01:32:18 +00001480rx_processing_done:
1481 /* Clear the status flags for this buffer */
1482 status &= ~BD_ENET_RX_STATS;
1483
1484 /* Mark the buffer empty */
1485 status |= BD_ENET_RX_EMPTY;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001486
Frank Liff43da82013-01-03 16:04:23 +00001487 if (fep->bufdesc_ex) {
1488 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1489
Johannes Berg5cfa3032016-01-24 16:52:37 +01001490 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00001491 ebdp->cbd_prot = 0;
1492 ebdp->cbd_bdu = 0;
1493 }
Troy Kiskybe293462016-02-05 14:52:49 -07001494 /* Make sure the updates to rest of the descriptor are
1495 * performed before transferring ownership.
1496 */
1497 wmb();
1498 bdp->cbd_sc = cpu_to_fec16(status);
Frank Li6605b732012-10-30 18:25:31 +00001499
Sascha Hauer22f6b862009-04-15 01:32:18 +00001500 /* Update BD pointer to next entry */
Troy Kisky7355f272016-02-05 14:52:46 -07001501 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001502
Sascha Hauer22f6b862009-04-15 01:32:18 +00001503 /* Doing this here will keep the FEC running while we process
1504 * incoming frames. On a heavily loaded network, we should be
1505 * able to keep up at the expense of system resources.
1506 */
Troy Kisky53bb20d2016-02-05 14:52:47 -07001507 writel(0, rxq->bd.reg_desc_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Troy Kisky7355f272016-02-05 14:52:46 -07001509 rxq->bd.cur = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001510 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Fugang Duan4d494cd2014-09-13 05:00:48 +08001513static int
1514fec_enet_rx(struct net_device *ndev, int budget)
1515{
1516 int pkt_received = 0;
1517 u16 queue_id;
1518 struct fec_enet_private *fep = netdev_priv(ndev);
1519
1520 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1521 clear_bit(queue_id, &fep->work_rx);
1522 pkt_received += fec_enet_rx_queue(ndev,
1523 budget - pkt_received, queue_id);
1524 }
1525 return pkt_received;
1526}
1527
1528static bool
1529fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1530{
1531 if (int_events == 0)
1532 return false;
1533
1534 if (int_events & FEC_ENET_RXF)
1535 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001536 if (int_events & FEC_ENET_RXF_1)
1537 fep->work_rx |= (1 << 0);
1538 if (int_events & FEC_ENET_RXF_2)
1539 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001540
1541 if (int_events & FEC_ENET_TXF)
1542 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001543 if (int_events & FEC_ENET_TXF_1)
1544 fep->work_tx |= (1 << 0);
1545 if (int_events & FEC_ENET_TXF_2)
1546 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001547
1548 return true;
1549}
1550
Uwe Kleine-König45993652011-01-19 20:47:04 +01001551static irqreturn_t
1552fec_enet_interrupt(int irq, void *dev_id)
1553{
1554 struct net_device *ndev = dev_id;
1555 struct fec_enet_private *fep = netdev_priv(ndev);
1556 uint int_events;
1557 irqreturn_t ret = IRQ_NONE;
1558
Russell King7a168072014-07-08 00:22:44 +01001559 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001560 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001561 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001562
Fugang Duan61615cd2015-03-04 07:52:11 +08001563 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001564 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001565
Nimrod Andy94191fd2014-12-16 18:25:58 +08001566 if (napi_schedule_prep(&fep->napi)) {
1567 /* Disable the NAPI interrupts */
Troy Kisky80dc6a92016-02-05 14:52:48 -07001568 writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001569 __napi_schedule(&fep->napi);
1570 }
Russell King7a168072014-07-08 00:22:44 +01001571 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001572
Russell King7a168072014-07-08 00:22:44 +01001573 if (int_events & FEC_ENET_MII) {
1574 ret = IRQ_HANDLED;
1575 complete(&fep->mdio_done);
1576 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001577
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001578 if (fep->ptp_clock)
1579 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001580
Uwe Kleine-König45993652011-01-19 20:47:04 +01001581 return ret;
1582}
1583
Frank Lidc975382013-01-28 18:31:42 +00001584static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1585{
1586 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001587 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001588 int pkts;
1589
Russell King7a168072014-07-08 00:22:44 +01001590 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001591
Frank Lide5fb0a2013-03-03 17:34:25 +00001592 fec_enet_tx(ndev);
1593
Frank Lidc975382013-01-28 18:31:42 +00001594 if (pkts < budget) {
1595 napi_complete(napi);
1596 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1597 }
1598 return pkts;
1599}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001602static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001604 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001605 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001606 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Shawn Guo49da97d2011-01-05 21:13:11 +00001608 /*
1609 * try to get mac address in following order:
1610 *
1611 * 1) module parameter via kernel command line in form
1612 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1613 */
1614 iap = macaddr;
1615
1616 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001617 * 2) from device tree data
1618 */
1619 if (!is_valid_ether_addr(iap)) {
1620 struct device_node *np = fep->pdev->dev.of_node;
1621 if (np) {
1622 const char *mac = of_get_mac_address(np);
1623 if (mac)
1624 iap = (unsigned char *) mac;
1625 }
1626 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001627
1628 /*
1629 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001630 */
1631 if (!is_valid_ether_addr(iap)) {
1632#ifdef CONFIG_M5272
1633 if (FEC_FLASHMAC)
1634 iap = (unsigned char *)FEC_FLASHMAC;
1635#else
1636 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001637 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001638#endif
1639 }
1640
1641 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001642 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001643 */
1644 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001645 *((__be32 *) &tmpaddr[0]) =
1646 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1647 *((__be16 *) &tmpaddr[4]) =
1648 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 iap = &tmpaddr[0];
1650 }
1651
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001652 /*
1653 * 5) random mac address
1654 */
1655 if (!is_valid_ether_addr(iap)) {
1656 /* Report it and use a random ethernet address instead */
1657 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1658 eth_hw_addr_random(ndev);
1659 netdev_info(ndev, "Using random MAC address: %pM\n",
1660 ndev->dev_addr);
1661 return;
1662 }
1663
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001664 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Shawn Guo49da97d2011-01-05 21:13:11 +00001666 /* Adjust MAC if using macaddr */
1667 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001668 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671/* ------------------------------------------------------------------------- */
1672
Bryan Wue6b043d2010-03-31 02:10:44 +00001673/*
1674 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001676static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001678 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001679 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001680 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Bryan Wue6b043d2010-03-31 02:10:44 +00001682 /* Prevent a state halted on mii error */
1683 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1684 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001687
Russell King8ce56242014-07-08 12:40:07 +01001688 /*
1689 * If the netdev is down, or is going down, we're not interested
1690 * in link state events, so just mark our idea of the link as down
1691 * and ignore the event.
1692 */
1693 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1694 fep->link = 0;
1695 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001696 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001697 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001698 status_change = 1;
1699 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001700
Russell Kingef833372014-07-08 12:40:43 +01001701 if (fep->full_duplex != phy_dev->duplex) {
1702 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001703 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001704 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001705
1706 if (phy_dev->speed != fep->speed) {
1707 fep->speed = phy_dev->speed;
1708 status_change = 1;
1709 }
1710
1711 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001712 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001713 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001714 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001715 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001716 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001717 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001718 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001719 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001720 } else {
1721 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001722 napi_disable(&fep->napi);
1723 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001724 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001725 netif_tx_unlock_bh(ndev);
1726 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001727 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001728 status_change = 1;
1729 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001730 }
1731
Bryan Wue6b043d2010-03-31 02:10:44 +00001732 if (status_change)
1733 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Bryan Wue6b043d2010-03-31 02:10:44 +00001736static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Bryan Wue6b043d2010-03-31 02:10:44 +00001738 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001739 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001740 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001741 int ret = 0;
1742
1743 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001744 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001745 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001746
1747 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001748 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001749
1750 /* start a read op */
1751 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1752 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1753 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1754
1755 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001756 time_left = wait_for_completion_timeout(&fep->mdio_done,
1757 usecs_to_jiffies(FEC_MII_TIMEOUT));
1758 if (time_left == 0) {
1759 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001760 netdev_err(fep->netdev, "MDIO read timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001761 ret = -ETIMEDOUT;
1762 goto out;
Bryan Wue6b043d2010-03-31 02:10:44 +00001763 }
1764
Andrew Lunn8fff7552015-07-25 22:38:02 +02001765 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1766
1767out:
1768 pm_runtime_mark_last_busy(dev);
1769 pm_runtime_put_autosuspend(dev);
1770
1771 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001772}
1773
1774static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1775 u16 value)
1776{
1777 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001778 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001779 unsigned long time_left;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001780 int ret;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001781
1782 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001783 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001784 return ret;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001785 else
1786 ret = 0;
Bryan Wue6b043d2010-03-31 02:10:44 +00001787
1788 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001789 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001790
Shawn Guo862f0982011-01-05 21:13:09 +00001791 /* start a write op */
1792 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001793 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1794 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1795 fep->hwp + FEC_MII_DATA);
1796
1797 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001798 time_left = wait_for_completion_timeout(&fep->mdio_done,
1799 usecs_to_jiffies(FEC_MII_TIMEOUT));
1800 if (time_left == 0) {
1801 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001802 netdev_err(fep->netdev, "MDIO write timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001803 ret = -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001804 }
1805
Andrew Lunn8fff7552015-07-25 22:38:02 +02001806 pm_runtime_mark_last_busy(dev);
1807 pm_runtime_put_autosuspend(dev);
1808
1809 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001810}
1811
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001812static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1813{
1814 struct fec_enet_private *fep = netdev_priv(ndev);
1815 int ret;
1816
1817 if (enable) {
1818 ret = clk_prepare_enable(fep->clk_ahb);
1819 if (ret)
1820 return ret;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001821 if (fep->clk_enet_out) {
1822 ret = clk_prepare_enable(fep->clk_enet_out);
1823 if (ret)
1824 goto failed_clk_enet_out;
1825 }
1826 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001827 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001828 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001829 if (ret) {
1830 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001831 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001832 } else {
1833 fep->ptp_clk_on = true;
1834 }
1835 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001836 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001837 if (fep->clk_ref) {
1838 ret = clk_prepare_enable(fep->clk_ref);
1839 if (ret)
1840 goto failed_clk_ref;
1841 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001842 } else {
1843 clk_disable_unprepare(fep->clk_ahb);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001844 if (fep->clk_enet_out)
1845 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001846 if (fep->clk_ptp) {
1847 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001848 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001849 fep->ptp_clk_on = false;
1850 mutex_unlock(&fep->ptp_clk_mutex);
1851 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001852 if (fep->clk_ref)
1853 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001854 }
1855
1856 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001857
1858failed_clk_ref:
1859 if (fep->clk_ref)
1860 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001861failed_clk_ptp:
1862 if (fep->clk_enet_out)
1863 clk_disable_unprepare(fep->clk_enet_out);
1864failed_clk_enet_out:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001865 clk_disable_unprepare(fep->clk_ahb);
1866
1867 return ret;
1868}
1869
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001870static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001871{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001872 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001873 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001874 char mdio_bus_id[MII_BUS_ID_SIZE];
1875 char phy_name[MII_BUS_ID_SIZE + 3];
1876 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001877 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001878
Bryan Wu418bd0d2010-05-28 03:40:39 -07001879 fep->phy_dev = NULL;
1880
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001881 if (fep->phy_node) {
1882 phy_dev = of_phy_connect(ndev, fep->phy_node,
1883 &fec_enet_adjust_link, 0,
1884 fep->phy_interface);
Nimrod Andy213a9922014-12-09 18:46:56 +08001885 if (!phy_dev)
1886 return -ENODEV;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001887 } else {
1888 /* check for attached phy */
1889 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
Andrew Lunn7f854422016-01-06 20:11:18 +01001890 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001891 continue;
1892 if (dev_id--)
1893 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001894 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001895 break;
1896 }
1897
1898 if (phy_id >= PHY_MAX_ADDR) {
1899 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001900 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001901 phy_id = 0;
1902 }
1903
1904 snprintf(phy_name, sizeof(phy_name),
1905 PHY_ID_FMT, mdio_bus_id, phy_id);
1906 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1907 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001908 }
1909
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001910 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001911 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001912 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001913 }
1914
1915 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001916 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001917 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001918 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001919#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001920 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001921#endif
Frank Libaa70a52013-01-16 16:55:58 +00001922 }
Shawn Guo230dec62011-09-23 02:12:48 +00001923 else
1924 phy_dev->supported &= PHY_BASIC_FEATURES;
1925
Bryan Wue6b043d2010-03-31 02:10:44 +00001926 phy_dev->advertising = phy_dev->supported;
1927
1928 fep->phy_dev = phy_dev;
1929 fep->link = 0;
1930 fep->full_duplex = 0;
1931
Andrew Lunn22209432016-01-06 20:11:13 +01001932 phy_attached_info(phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001933
Bryan Wue6b043d2010-03-31 02:10:44 +00001934 return 0;
1935}
1936
1937static int fec_enet_mii_init(struct platform_device *pdev)
1938{
Shawn Guob5680e02011-01-05 21:13:13 +00001939 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001940 struct net_device *ndev = platform_get_drvdata(pdev);
1941 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001942 struct device_node *node;
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001943 int err = -ENXIO;
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001944 u32 mii_speed, holdtime;
Bryan Wue6b043d2010-03-31 02:10:44 +00001945
Shawn Guob5680e02011-01-05 21:13:13 +00001946 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001947 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001948 * Here are the differences:
1949 *
1950 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1951 * - fec0 acts as the 1588 time master while fec1 is slave
1952 * - external phys can only be configured by fec0
1953 *
1954 * That is to say fec1 can not work independently. It only works
1955 * when fec0 is working. The reason behind this design is that the
1956 * second interface is added primarily for Switch mode.
1957 *
1958 * Because of the last point above, both phys are attached on fec0
1959 * mdio interface in board design, and need to be configured by
1960 * fec0 mii_bus.
1961 */
Stefan Agner3d125f92015-01-14 00:20:21 +01001962 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001963 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001964 if (mii_cnt && fec0_mii_bus) {
1965 fep->mii_bus = fec0_mii_bus;
1966 mii_cnt++;
1967 return 0;
1968 }
1969 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001970 }
1971
Bryan Wue6b043d2010-03-31 02:10:44 +00001972 fep->mii_timeout = 0;
1973
1974 /*
1975 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001976 *
1977 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1978 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1979 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1980 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001981 */
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001982 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001983 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001984 mii_speed--;
1985 if (mii_speed > 63) {
1986 dev_err(&pdev->dev,
1987 "fec clock (%lu) to fast to get right mii speed\n",
1988 clk_get_rate(fep->clk_ipg));
1989 err = -EINVAL;
1990 goto err_out;
1991 }
1992
1993 /*
1994 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
1995 * MII_SPEED) register that defines the MDIO output hold time. Earlier
1996 * versions are RAZ there, so just ignore the difference and write the
1997 * register always.
1998 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
1999 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2000 * output.
2001 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2002 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2003 * holdtime cannot result in a value greater than 3.
2004 */
2005 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2006
2007 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2008
Bryan Wue6b043d2010-03-31 02:10:44 +00002009 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2010
2011 fep->mii_bus = mdiobus_alloc();
2012 if (fep->mii_bus == NULL) {
2013 err = -ENOMEM;
2014 goto err_out;
2015 }
2016
2017 fep->mii_bus->name = "fec_enet_mii_bus";
2018 fep->mii_bus->read = fec_enet_mdio_read;
2019 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002020 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2021 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002022 fep->mii_bus->priv = fep;
2023 fep->mii_bus->parent = &pdev->dev;
2024
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002025 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2026 if (node) {
2027 err = of_mdiobus_register(fep->mii_bus, node);
2028 of_node_put(node);
2029 } else {
2030 err = mdiobus_register(fep->mii_bus);
2031 }
2032
2033 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002034 goto err_out_free_mdiobus;
Bryan Wue6b043d2010-03-31 02:10:44 +00002035
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002036 mii_cnt++;
2037
Shawn Guob5680e02011-01-05 21:13:13 +00002038 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002039 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002040 fec0_mii_bus = fep->mii_bus;
2041
Bryan Wue6b043d2010-03-31 02:10:44 +00002042 return 0;
2043
Bryan Wue6b043d2010-03-31 02:10:44 +00002044err_out_free_mdiobus:
2045 mdiobus_free(fep->mii_bus);
2046err_out:
2047 return err;
2048}
2049
2050static void fec_enet_mii_remove(struct fec_enet_private *fep)
2051{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002052 if (--mii_cnt == 0) {
2053 mdiobus_unregister(fep->mii_bus);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002054 mdiobus_free(fep->mii_bus);
2055 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002056}
2057
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002058static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002059 struct ethtool_cmd *cmd)
2060{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002061 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002062 struct phy_device *phydev = fep->phy_dev;
2063
2064 if (!phydev)
2065 return -ENODEV;
2066
2067 return phy_ethtool_gset(phydev, cmd);
2068}
2069
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002070static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002071 struct ethtool_cmd *cmd)
2072{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002073 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002074 struct phy_device *phydev = fep->phy_dev;
2075
2076 if (!phydev)
2077 return -ENODEV;
2078
2079 return phy_ethtool_sset(phydev, cmd);
2080}
2081
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002082static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002083 struct ethtool_drvinfo *info)
2084{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002085 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Jiri Pirko7826d432013-01-06 00:44:26 +00002087 strlcpy(info->driver, fep->pdev->dev.driver->name,
2088 sizeof(info->driver));
2089 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2090 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
Bryan Wue6b043d2010-03-31 02:10:44 +00002092
Philippe Reynesdb65f352015-05-11 00:01:42 +02002093static int fec_enet_get_regs_len(struct net_device *ndev)
2094{
2095 struct fec_enet_private *fep = netdev_priv(ndev);
2096 struct resource *r;
2097 int s = 0;
2098
2099 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2100 if (r)
2101 s = resource_size(r);
2102
2103 return s;
2104}
2105
2106/* List of registers that can be safety be read to dump them with ethtool */
2107#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Johannes Berg05f3b502016-01-25 11:40:50 +01002108 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Philippe Reynesdb65f352015-05-11 00:01:42 +02002109static u32 fec_enet_register_offset[] = {
2110 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2111 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2112 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2113 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2114 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2115 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2116 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2117 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2118 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2119 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2120 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2121 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2122 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2123 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2124 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2125 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2126 RMON_T_P_GTE2048, RMON_T_OCTETS,
2127 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2128 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2129 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2130 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2131 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2132 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2133 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2134 RMON_R_P_GTE2048, RMON_R_OCTETS,
2135 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2136 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2137};
2138#else
2139static u32 fec_enet_register_offset[] = {
2140 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2141 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2142 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2143 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2144 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2145 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2146 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2147 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2148 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2149};
2150#endif
2151
2152static void fec_enet_get_regs(struct net_device *ndev,
2153 struct ethtool_regs *regs, void *regbuf)
2154{
2155 struct fec_enet_private *fep = netdev_priv(ndev);
2156 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2157 u32 *buf = (u32 *)regbuf;
2158 u32 i, off;
2159
2160 memset(buf, 0, regs->len);
2161
2162 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2163 off = fec_enet_register_offset[i] / 4;
2164 buf[off] = readl(&theregs[off]);
2165 }
2166}
2167
Frank Li5ebae4892013-01-06 16:25:07 +00002168static int fec_enet_get_ts_info(struct net_device *ndev,
2169 struct ethtool_ts_info *info)
2170{
2171 struct fec_enet_private *fep = netdev_priv(ndev);
2172
2173 if (fep->bufdesc_ex) {
2174
2175 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2176 SOF_TIMESTAMPING_RX_SOFTWARE |
2177 SOF_TIMESTAMPING_SOFTWARE |
2178 SOF_TIMESTAMPING_TX_HARDWARE |
2179 SOF_TIMESTAMPING_RX_HARDWARE |
2180 SOF_TIMESTAMPING_RAW_HARDWARE;
2181 if (fep->ptp_clock)
2182 info->phc_index = ptp_clock_index(fep->ptp_clock);
2183 else
2184 info->phc_index = -1;
2185
2186 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2187 (1 << HWTSTAMP_TX_ON);
2188
2189 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2190 (1 << HWTSTAMP_FILTER_ALL);
2191 return 0;
2192 } else {
2193 return ethtool_op_get_ts_info(ndev, info);
2194 }
2195}
2196
Guenter Roeckd1391932013-06-18 10:04:59 -07002197#if !defined(CONFIG_M5272)
2198
Frank Libaa70a52013-01-16 16:55:58 +00002199static void fec_enet_get_pauseparam(struct net_device *ndev,
2200 struct ethtool_pauseparam *pause)
2201{
2202 struct fec_enet_private *fep = netdev_priv(ndev);
2203
2204 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2205 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2206 pause->rx_pause = pause->tx_pause;
2207}
2208
2209static int fec_enet_set_pauseparam(struct net_device *ndev,
2210 struct ethtool_pauseparam *pause)
2211{
2212 struct fec_enet_private *fep = netdev_priv(ndev);
2213
Russell King0b146ca2014-07-08 00:22:59 +01002214 if (!fep->phy_dev)
2215 return -ENODEV;
2216
Frank Libaa70a52013-01-16 16:55:58 +00002217 if (pause->tx_pause != pause->rx_pause) {
2218 netdev_info(ndev,
2219 "hardware only support enable/disable both tx and rx");
2220 return -EINVAL;
2221 }
2222
2223 fep->pause_flag = 0;
2224
2225 /* tx pause must be same as rx pause */
2226 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2227 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2228
2229 if (pause->rx_pause || pause->autoneg) {
2230 fep->phy_dev->supported |= ADVERTISED_Pause;
2231 fep->phy_dev->advertising |= ADVERTISED_Pause;
2232 } else {
2233 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2234 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2235 }
2236
2237 if (pause->autoneg) {
2238 if (netif_running(ndev))
2239 fec_stop(ndev);
2240 phy_start_aneg(fep->phy_dev);
2241 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002242 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002243 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002244 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002245 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002246 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002247 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002248 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002249 }
Frank Libaa70a52013-01-16 16:55:58 +00002250
2251 return 0;
2252}
2253
Chris Healy38ae92d2013-06-25 23:18:52 -07002254static const struct fec_stat {
2255 char name[ETH_GSTRING_LEN];
2256 u16 offset;
2257} fec_stats[] = {
2258 /* RMON TX */
2259 { "tx_dropped", RMON_T_DROP },
2260 { "tx_packets", RMON_T_PACKETS },
2261 { "tx_broadcast", RMON_T_BC_PKT },
2262 { "tx_multicast", RMON_T_MC_PKT },
2263 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2264 { "tx_undersize", RMON_T_UNDERSIZE },
2265 { "tx_oversize", RMON_T_OVERSIZE },
2266 { "tx_fragment", RMON_T_FRAG },
2267 { "tx_jabber", RMON_T_JAB },
2268 { "tx_collision", RMON_T_COL },
2269 { "tx_64byte", RMON_T_P64 },
2270 { "tx_65to127byte", RMON_T_P65TO127 },
2271 { "tx_128to255byte", RMON_T_P128TO255 },
2272 { "tx_256to511byte", RMON_T_P256TO511 },
2273 { "tx_512to1023byte", RMON_T_P512TO1023 },
2274 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2275 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2276 { "tx_octets", RMON_T_OCTETS },
2277
2278 /* IEEE TX */
2279 { "IEEE_tx_drop", IEEE_T_DROP },
2280 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2281 { "IEEE_tx_1col", IEEE_T_1COL },
2282 { "IEEE_tx_mcol", IEEE_T_MCOL },
2283 { "IEEE_tx_def", IEEE_T_DEF },
2284 { "IEEE_tx_lcol", IEEE_T_LCOL },
2285 { "IEEE_tx_excol", IEEE_T_EXCOL },
2286 { "IEEE_tx_macerr", IEEE_T_MACERR },
2287 { "IEEE_tx_cserr", IEEE_T_CSERR },
2288 { "IEEE_tx_sqe", IEEE_T_SQE },
2289 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2290 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2291
2292 /* RMON RX */
2293 { "rx_packets", RMON_R_PACKETS },
2294 { "rx_broadcast", RMON_R_BC_PKT },
2295 { "rx_multicast", RMON_R_MC_PKT },
2296 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2297 { "rx_undersize", RMON_R_UNDERSIZE },
2298 { "rx_oversize", RMON_R_OVERSIZE },
2299 { "rx_fragment", RMON_R_FRAG },
2300 { "rx_jabber", RMON_R_JAB },
2301 { "rx_64byte", RMON_R_P64 },
2302 { "rx_65to127byte", RMON_R_P65TO127 },
2303 { "rx_128to255byte", RMON_R_P128TO255 },
2304 { "rx_256to511byte", RMON_R_P256TO511 },
2305 { "rx_512to1023byte", RMON_R_P512TO1023 },
2306 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2307 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2308 { "rx_octets", RMON_R_OCTETS },
2309
2310 /* IEEE RX */
2311 { "IEEE_rx_drop", IEEE_R_DROP },
2312 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2313 { "IEEE_rx_crc", IEEE_R_CRC },
2314 { "IEEE_rx_align", IEEE_R_ALIGN },
2315 { "IEEE_rx_macerr", IEEE_R_MACERR },
2316 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2317 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2318};
2319
2320static void fec_enet_get_ethtool_stats(struct net_device *dev,
2321 struct ethtool_stats *stats, u64 *data)
2322{
2323 struct fec_enet_private *fep = netdev_priv(dev);
2324 int i;
2325
2326 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2327 data[i] = readl(fep->hwp + fec_stats[i].offset);
2328}
2329
2330static void fec_enet_get_strings(struct net_device *netdev,
2331 u32 stringset, u8 *data)
2332{
2333 int i;
2334 switch (stringset) {
2335 case ETH_SS_STATS:
2336 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2337 memcpy(data + i * ETH_GSTRING_LEN,
2338 fec_stats[i].name, ETH_GSTRING_LEN);
2339 break;
2340 }
2341}
2342
2343static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2344{
2345 switch (sset) {
2346 case ETH_SS_STATS:
2347 return ARRAY_SIZE(fec_stats);
2348 default:
2349 return -EOPNOTSUPP;
2350 }
2351}
Guenter Roeckd1391932013-06-18 10:04:59 -07002352#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002353
Chris Healy32bc9b42013-06-17 07:25:06 -07002354static int fec_enet_nway_reset(struct net_device *dev)
2355{
2356 struct fec_enet_private *fep = netdev_priv(dev);
2357 struct phy_device *phydev = fep->phy_dev;
2358
2359 if (!phydev)
2360 return -ENODEV;
2361
2362 return genphy_restart_aneg(phydev);
2363}
2364
Fugang Duand851b472014-09-17 05:18:52 +08002365/* ITR clock source is enet system clock (clk_ahb).
2366 * TCTT unit is cycle_ns * 64 cycle
2367 * So, the ICTT value = X us / (cycle_ns * 64)
2368 */
2369static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2370{
2371 struct fec_enet_private *fep = netdev_priv(ndev);
2372
2373 return us * (fep->itr_clk_rate / 64000) / 1000;
2374}
2375
2376/* Set threshold for interrupt coalescing */
2377static void fec_enet_itr_coal_set(struct net_device *ndev)
2378{
2379 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002380 int rx_itr, tx_itr;
2381
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002382 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002383 return;
2384
2385 /* Must be greater than zero to avoid unpredictable behavior */
2386 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2387 !fep->tx_time_itr || !fep->tx_pkts_itr)
2388 return;
2389
2390 /* Select enet system clock as Interrupt Coalescing
2391 * timer Clock Source
2392 */
2393 rx_itr = FEC_ITR_CLK_SEL;
2394 tx_itr = FEC_ITR_CLK_SEL;
2395
2396 /* set ICFT and ICTT */
2397 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2398 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2399 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2400 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2401
2402 rx_itr |= FEC_ITR_EN;
2403 tx_itr |= FEC_ITR_EN;
2404
2405 writel(tx_itr, fep->hwp + FEC_TXIC0);
2406 writel(rx_itr, fep->hwp + FEC_RXIC0);
2407 writel(tx_itr, fep->hwp + FEC_TXIC1);
2408 writel(rx_itr, fep->hwp + FEC_RXIC1);
2409 writel(tx_itr, fep->hwp + FEC_TXIC2);
2410 writel(rx_itr, fep->hwp + FEC_RXIC2);
2411}
2412
2413static int
2414fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2415{
2416 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002417
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002418 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002419 return -EOPNOTSUPP;
2420
2421 ec->rx_coalesce_usecs = fep->rx_time_itr;
2422 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2423
2424 ec->tx_coalesce_usecs = fep->tx_time_itr;
2425 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2426
2427 return 0;
2428}
2429
2430static int
2431fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2432{
2433 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002434 unsigned int cycle;
2435
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002436 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002437 return -EOPNOTSUPP;
2438
2439 if (ec->rx_max_coalesced_frames > 255) {
2440 pr_err("Rx coalesced frames exceed hardware limiation");
2441 return -EINVAL;
2442 }
2443
2444 if (ec->tx_max_coalesced_frames > 255) {
2445 pr_err("Tx coalesced frame exceed hardware limiation");
2446 return -EINVAL;
2447 }
2448
2449 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2450 if (cycle > 0xFFFF) {
2451 pr_err("Rx coalesed usec exceeed hardware limiation");
2452 return -EINVAL;
2453 }
2454
2455 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2456 if (cycle > 0xFFFF) {
2457 pr_err("Rx coalesed usec exceeed hardware limiation");
2458 return -EINVAL;
2459 }
2460
2461 fep->rx_time_itr = ec->rx_coalesce_usecs;
2462 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2463
2464 fep->tx_time_itr = ec->tx_coalesce_usecs;
2465 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2466
2467 fec_enet_itr_coal_set(ndev);
2468
2469 return 0;
2470}
2471
2472static void fec_enet_itr_coal_init(struct net_device *ndev)
2473{
2474 struct ethtool_coalesce ec;
2475
2476 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2477 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2478
2479 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2480 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2481
2482 fec_enet_set_coalesce(ndev, &ec);
2483}
2484
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002485static int fec_enet_get_tunable(struct net_device *netdev,
2486 const struct ethtool_tunable *tuna,
2487 void *data)
2488{
2489 struct fec_enet_private *fep = netdev_priv(netdev);
2490 int ret = 0;
2491
2492 switch (tuna->id) {
2493 case ETHTOOL_RX_COPYBREAK:
2494 *(u32 *)data = fep->rx_copybreak;
2495 break;
2496 default:
2497 ret = -EINVAL;
2498 break;
2499 }
2500
2501 return ret;
2502}
2503
2504static int fec_enet_set_tunable(struct net_device *netdev,
2505 const struct ethtool_tunable *tuna,
2506 const void *data)
2507{
2508 struct fec_enet_private *fep = netdev_priv(netdev);
2509 int ret = 0;
2510
2511 switch (tuna->id) {
2512 case ETHTOOL_RX_COPYBREAK:
2513 fep->rx_copybreak = *(u32 *)data;
2514 break;
2515 default:
2516 ret = -EINVAL;
2517 break;
2518 }
2519
2520 return ret;
2521}
2522
Nimrod Andyde40ed32014-12-24 17:30:39 +08002523static void
2524fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2525{
2526 struct fec_enet_private *fep = netdev_priv(ndev);
2527
2528 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2529 wol->supported = WAKE_MAGIC;
2530 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2531 } else {
2532 wol->supported = wol->wolopts = 0;
2533 }
2534}
2535
2536static int
2537fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2538{
2539 struct fec_enet_private *fep = netdev_priv(ndev);
2540
2541 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2542 return -EINVAL;
2543
2544 if (wol->wolopts & ~WAKE_MAGIC)
2545 return -EINVAL;
2546
2547 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2548 if (device_may_wakeup(&ndev->dev)) {
2549 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2550 if (fep->irq[0] > 0)
2551 enable_irq_wake(fep->irq[0]);
2552 } else {
2553 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2554 if (fep->irq[0] > 0)
2555 disable_irq_wake(fep->irq[0]);
2556 }
2557
2558 return 0;
2559}
2560
stephen hemminger9b07be42012-01-04 12:59:49 +00002561static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002562 .get_settings = fec_enet_get_settings,
2563 .set_settings = fec_enet_set_settings,
2564 .get_drvinfo = fec_enet_get_drvinfo,
Philippe Reynesdb65f352015-05-11 00:01:42 +02002565 .get_regs_len = fec_enet_get_regs_len,
2566 .get_regs = fec_enet_get_regs,
Chris Healy32bc9b42013-06-17 07:25:06 -07002567 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002568 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002569 .get_coalesce = fec_enet_get_coalesce,
2570 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002571#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002572 .get_pauseparam = fec_enet_get_pauseparam,
2573 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002574 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002575 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002576 .get_sset_count = fec_enet_get_sset_count,
2577#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002578 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002579 .get_tunable = fec_enet_get_tunable,
2580 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002581 .get_wol = fec_enet_get_wol,
2582 .set_wol = fec_enet_set_wol,
Bryan Wue6b043d2010-03-31 02:10:44 +00002583};
2584
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002585static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002586{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002587 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002588 struct phy_device *phydev = fep->phy_dev;
2589
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002590 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002591 return -EINVAL;
2592
2593 if (!phydev)
2594 return -ENODEV;
2595
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002596 if (fep->bufdesc_ex) {
2597 if (cmd == SIOCSHWTSTAMP)
2598 return fec_ptp_set(ndev, rq);
2599 if (cmd == SIOCGHWTSTAMP)
2600 return fec_ptp_get(ndev, rq);
2601 }
Frank Liff43da82013-01-03 16:04:23 +00002602
Richard Cochran28b04112010-07-17 08:48:55 +00002603 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002606static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002607{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002608 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002609 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002610 struct sk_buff *skb;
2611 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002612 struct fec_enet_priv_tx_q *txq;
2613 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002614 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002615
Frank Li59d0f7462014-09-13 05:00:50 +08002616 for (q = 0; q < fep->num_rx_queues; q++) {
2617 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002618 bdp = rxq->bd.base;
2619 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002620 skb = rxq->rx_skbuff[i];
2621 rxq->rx_skbuff[i] = NULL;
2622 if (skb) {
2623 dma_unmap_single(&fep->pdev->dev,
Johannes Berg5cfa3032016-01-24 16:52:37 +01002624 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002625 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002626 DMA_FROM_DEVICE);
2627 dev_kfree_skb(skb);
2628 }
Troy Kisky7355f272016-02-05 14:52:46 -07002629 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Russell King730ee362014-07-08 00:23:14 +01002630 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002631 }
2632
Frank Li59d0f7462014-09-13 05:00:50 +08002633 for (q = 0; q < fep->num_tx_queues; q++) {
2634 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002635 bdp = txq->bd.base;
2636 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002637 kfree(txq->tx_bounce[i]);
2638 txq->tx_bounce[i] = NULL;
2639 skb = txq->tx_skbuff[i];
2640 txq->tx_skbuff[i] = NULL;
2641 dev_kfree_skb(skb);
2642 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002643 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002644}
2645
Frank Li59d0f7462014-09-13 05:00:50 +08002646static void fec_enet_free_queue(struct net_device *ndev)
2647{
2648 struct fec_enet_private *fep = netdev_priv(ndev);
2649 int i;
2650 struct fec_enet_priv_tx_q *txq;
2651
2652 for (i = 0; i < fep->num_tx_queues; i++)
2653 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2654 txq = fep->tx_queue[i];
2655 dma_free_coherent(NULL,
Troy Kisky7355f272016-02-05 14:52:46 -07002656 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002657 txq->tso_hdrs,
2658 txq->tso_hdrs_dma);
2659 }
2660
2661 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002662 kfree(fep->rx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002663 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002664 kfree(fep->tx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002665}
2666
2667static int fec_enet_alloc_queue(struct net_device *ndev)
2668{
2669 struct fec_enet_private *fep = netdev_priv(ndev);
2670 int i;
2671 int ret = 0;
2672 struct fec_enet_priv_tx_q *txq;
2673
2674 for (i = 0; i < fep->num_tx_queues; i++) {
2675 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2676 if (!txq) {
2677 ret = -ENOMEM;
2678 goto alloc_failed;
2679 }
2680
2681 fep->tx_queue[i] = txq;
Troy Kisky7355f272016-02-05 14:52:46 -07002682 txq->bd.ring_size = TX_RING_SIZE;
2683 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002684
2685 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2686 txq->tx_wake_threshold =
Troy Kisky7355f272016-02-05 14:52:46 -07002687 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
Frank Li59d0f7462014-09-13 05:00:50 +08002688
2689 txq->tso_hdrs = dma_alloc_coherent(NULL,
Troy Kisky7355f272016-02-05 14:52:46 -07002690 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002691 &txq->tso_hdrs_dma,
2692 GFP_KERNEL);
2693 if (!txq->tso_hdrs) {
2694 ret = -ENOMEM;
2695 goto alloc_failed;
2696 }
2697 }
2698
2699 for (i = 0; i < fep->num_rx_queues; i++) {
2700 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2701 GFP_KERNEL);
2702 if (!fep->rx_queue[i]) {
2703 ret = -ENOMEM;
2704 goto alloc_failed;
2705 }
2706
Troy Kisky7355f272016-02-05 14:52:46 -07002707 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
2708 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002709 }
2710 return ret;
2711
2712alloc_failed:
2713 fec_enet_free_queue(ndev);
2714 return ret;
2715}
2716
2717static int
2718fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002719{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002720 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002721 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002722 struct sk_buff *skb;
2723 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002724 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002725
Frank Li59d0f7462014-09-13 05:00:50 +08002726 rxq = fep->rx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002727 bdp = rxq->bd.base;
2728 for (i = 0; i < rxq->bd.ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002729 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002730 if (!skb)
2731 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002732
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002733 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002734 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002735 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002736 }
Russell King730ee362014-07-08 00:23:14 +01002737
Fugang Duan4d494cd2014-09-13 05:00:48 +08002738 rxq->rx_skbuff[i] = skb;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002739 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Liff43da82013-01-03 16:04:23 +00002740
2741 if (fep->bufdesc_ex) {
2742 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002743 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002744 }
2745
Troy Kisky7355f272016-02-05 14:52:46 -07002746 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002747 }
2748
2749 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002750 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002751 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +08002752 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002753
Frank Li59d0f7462014-09-13 05:00:50 +08002754 err_alloc:
2755 fec_enet_free_buffers(ndev);
2756 return -ENOMEM;
2757}
2758
2759static int
2760fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2761{
2762 struct fec_enet_private *fep = netdev_priv(ndev);
2763 unsigned int i;
2764 struct bufdesc *bdp;
2765 struct fec_enet_priv_tx_q *txq;
2766
2767 txq = fep->tx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002768 bdp = txq->bd.base;
2769 for (i = 0; i < txq->bd.ring_size; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002770 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2771 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002772 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002773
Johannes Berg5cfa3032016-01-24 16:52:37 +01002774 bdp->cbd_sc = cpu_to_fec16(0);
2775 bdp->cbd_bufaddr = cpu_to_fec32(0);
Frank Li6605b732012-10-30 18:25:31 +00002776
Frank Liff43da82013-01-03 16:04:23 +00002777 if (fep->bufdesc_ex) {
2778 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002779 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002780 }
2781
Troy Kisky7355f272016-02-05 14:52:46 -07002782 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002783 }
2784
2785 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002786 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002787 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002788
2789 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002790
2791 err_alloc:
2792 fec_enet_free_buffers(ndev);
2793 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002794}
2795
Frank Li59d0f7462014-09-13 05:00:50 +08002796static int fec_enet_alloc_buffers(struct net_device *ndev)
2797{
2798 struct fec_enet_private *fep = netdev_priv(ndev);
2799 unsigned int i;
2800
2801 for (i = 0; i < fep->num_rx_queues; i++)
2802 if (fec_enet_alloc_rxq_buffers(ndev, i))
2803 return -ENOMEM;
2804
2805 for (i = 0; i < fep->num_tx_queues; i++)
2806 if (fec_enet_alloc_txq_buffers(ndev, i))
2807 return -ENOMEM;
2808 return 0;
2809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002812fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002814 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002815 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Andrew Lunn8fff7552015-07-25 22:38:02 +02002817 ret = pm_runtime_get_sync(&fep->pdev->dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03002818 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002819 return ret;
2820
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002821 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002822 ret = fec_enet_clk_enable(ndev, true);
2823 if (ret)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002824 goto clk_enable;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /* I should reset the ring buffers here, but I don't yet know
2827 * a simple way to do that.
2828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002830 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002831 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002832 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002833
Nimrod Andy55dd2752015-06-23 15:32:51 +08002834 /* Init MAC prior to mii bus probe */
2835 fec_restart(ndev);
2836
Bryan Wu418bd0d2010-05-28 03:40:39 -07002837 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002838 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002839 if (ret)
2840 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002841
2842 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002843 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002844 netif_tx_start_all_queues(ndev);
2845
Nimrod Andyde40ed32014-12-24 17:30:39 +08002846 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2847 FEC_WOL_FLAG_ENABLE);
2848
Sascha Hauer22f6b862009-04-15 01:32:18 +00002849 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002850
2851err_enet_mii_probe:
2852 fec_enet_free_buffers(ndev);
2853err_enet_alloc:
2854 fec_enet_clk_enable(ndev, false);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002855clk_enable:
2856 pm_runtime_mark_last_busy(&fep->pdev->dev);
2857 pm_runtime_put_autosuspend(&fep->pdev->dev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002858 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2859 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
2862static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002863fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002865 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Russell Kingd76cfae2014-07-08 00:23:04 +01002867 phy_stop(fep->phy_dev);
2868
Russell King31a6de32014-07-08 12:40:23 +01002869 if (netif_device_present(ndev)) {
2870 napi_disable(&fep->napi);
2871 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002872 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Russell King635cf172014-07-08 00:22:54 +01002875 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002876 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002877
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002878 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002879 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002880 pm_runtime_mark_last_busy(&fep->pdev->dev);
2881 pm_runtime_put_autosuspend(&fep->pdev->dev);
2882
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002883 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return 0;
2886}
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888/* Set or clear the multicast filter for this adaptor.
2889 * Skeleton taken from sunlance driver.
2890 * The CPM Ethernet implementation allows Multicast as well as individual
2891 * MAC address filtering. Some of the drivers check to make sure it is
2892 * a group multicast address, and discard those that are not. I guess I
2893 * will do the same for now, but just remove the test if you want
2894 * individual filtering as well (do the upper net layers want or support
2895 * this kind of feature?).
2896 */
2897
2898#define HASH_BITS 6 /* #bits in hash */
2899#define CRC32_POLY 0xEDB88320
2900
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002901static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002903 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002904 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002905 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 unsigned char hash;
2907
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002908 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002909 tmp = readl(fep->hwp + FEC_R_CNTRL);
2910 tmp |= 0x8;
2911 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002912 return;
2913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Sascha Hauer4e831832009-04-15 01:32:19 +00002915 tmp = readl(fep->hwp + FEC_R_CNTRL);
2916 tmp &= ~0x8;
2917 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002918
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002919 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002920 /* Catch all multicast addresses, so set the
2921 * filter to all 1's
2922 */
2923 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2924 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Sascha Hauer4e831832009-04-15 01:32:19 +00002926 return;
2927 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002928
Sascha Hauer4e831832009-04-15 01:32:19 +00002929 /* Clear filter and add the addresses in hash register
2930 */
2931 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2932 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002934 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002935 /* calculate crc32 value of mac address */
2936 crc = 0xffffffff;
2937
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002938 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002939 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002940 for (bit = 0; bit < 8; bit++, data >>= 1) {
2941 crc = (crc >> 1) ^
2942 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002945
2946 /* only upper 6 bits (HASH_BITS) are used
2947 * which point to specific bit in he hash registers
2948 */
2949 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2950
2951 if (hash > 31) {
2952 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2953 tmp |= 1 << (hash - 32);
2954 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2955 } else {
2956 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2957 tmp |= 1 << hash;
2958 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
2961}
2962
Sascha Hauer22f6b862009-04-15 01:32:18 +00002963/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002964static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002965fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002967 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002968 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Lucas Stach44934fa2014-03-30 21:32:08 +02002970 if (addr) {
2971 if (!is_valid_ether_addr(addr->sa_data))
2972 return -EADDRNOTAVAIL;
2973 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2974 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002975
Nimrod Andy9638d192015-09-10 09:35:39 +08002976 /* Add netif status check here to avoid system hang in below case:
2977 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
2978 * After ethx down, fec all clocks are gated off and then register
2979 * access causes system hang.
2980 */
2981 if (!netif_running(ndev))
2982 return 0;
2983
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002984 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2985 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002986 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002987 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002988 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002992#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002993/**
2994 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002995 * @dev: The FEC network adapter
2996 *
2997 * Polled functionality used by netconsole and others in non interrupt mode
2998 *
2999 */
Wei Yongjun47a52472013-03-20 05:06:11 +00003000static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003001{
3002 int i;
3003 struct fec_enet_private *fep = netdev_priv(dev);
3004
3005 for (i = 0; i < FEC_IRQ_NUM; i++) {
3006 if (fep->irq[i] > 0) {
3007 disable_irq(fep->irq[i]);
3008 fec_enet_interrupt(fep->irq[i], dev);
3009 enable_irq(fep->irq[i]);
3010 }
3011 }
3012}
3013#endif
3014
Nimrod Andy5bc26722014-10-13 10:53:48 +08003015static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00003016 netdev_features_t features)
3017{
3018 struct fec_enet_private *fep = netdev_priv(netdev);
3019 netdev_features_t changed = features ^ netdev->features;
3020
3021 netdev->features = features;
3022
3023 /* Receive checksum has been changed */
3024 if (changed & NETIF_F_RXCSUM) {
3025 if (features & NETIF_F_RXCSUM)
3026 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3027 else
3028 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01003029 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08003030}
Jim Baxter4c09eed2013-04-19 08:10:49 +00003031
Nimrod Andy5bc26722014-10-13 10:53:48 +08003032static int fec_set_features(struct net_device *netdev,
3033 netdev_features_t features)
3034{
3035 struct fec_enet_private *fep = netdev_priv(netdev);
3036 netdev_features_t changed = features ^ netdev->features;
3037
Fabio Estevam5b40f702015-09-25 18:31:32 -03003038 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08003039 napi_disable(&fep->napi);
3040 netif_tx_lock_bh(netdev);
3041 fec_stop(netdev);
3042 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01003043 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003044 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01003045 netif_tx_unlock_bh(netdev);
3046 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08003047 } else {
3048 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00003049 }
3050
3051 return 0;
3052}
3053
Sascha Hauer009fda82009-04-15 01:32:23 +00003054static const struct net_device_ops fec_netdev_ops = {
3055 .ndo_open = fec_enet_open,
3056 .ndo_stop = fec_enet_close,
3057 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003058 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00003059 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00003060 .ndo_validate_addr = eth_validate_addr,
3061 .ndo_tx_timeout = fec_timeout,
3062 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01003063 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003064#ifdef CONFIG_NET_POLL_CONTROLLER
3065 .ndo_poll_controller = fec_poll_controller,
3066#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00003067 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00003068};
3069
Troy Kisky53bb20d2016-02-05 14:52:47 -07003070static const unsigned short offset_des_active_rxq[] = {
3071 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3072};
3073
3074static const unsigned short offset_des_active_txq[] = {
3075 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3076};
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 /*
3079 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00003080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003082static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003084 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00003085 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08003086 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08003087 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08003088 unsigned int i;
Troy Kisky7355f272016-02-05 14:52:46 -07003089 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3090 sizeof(struct bufdesc);
3091 unsigned dsize_log2 = __fls(dsize);
Nimrod Andy55d02182014-06-12 08:16:21 +08003092
Troy Kisky7355f272016-02-05 14:52:46 -07003093 WARN_ON(dsize != (1 << dsize_log2));
Fugang Duan41ef84c2014-09-13 05:00:54 +08003094#if defined(CONFIG_ARM)
3095 fep->rx_align = 0xf;
3096 fep->tx_align = 0xf;
3097#else
3098 fep->rx_align = 0x3;
3099 fep->tx_align = 0x3;
3100#endif
3101
Frank Li59d0f7462014-09-13 05:00:50 +08003102 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003103
Troy Kisky7355f272016-02-05 14:52:46 -07003104 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003106 /* Allocate memory for buffer descriptors. */
Lucas Stachc0a1a0a2015-07-23 16:06:19 +02003107 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3108 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003109 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003110 return -ENOMEM;
3111 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003112
Fugang Duan4d494cd2014-09-13 05:00:48 +08003113 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Shawn Guo49da97d2011-01-05 21:13:11 +00003115 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003116 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003117 /* make sure MAC we just acquired is programmed into the hw */
3118 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003120 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003121 for (i = 0; i < fep->num_rx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003122 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3123 unsigned size = dsize * rxq->bd.ring_size;
3124
3125 rxq->bd.qid = i;
3126 rxq->bd.base = cbd_base;
3127 rxq->bd.cur = cbd_base;
3128 rxq->bd.dma = bd_dma;
3129 rxq->bd.dsize = dsize;
3130 rxq->bd.dsize_log2 = dsize_log2;
Troy Kisky53bb20d2016-02-05 14:52:47 -07003131 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
Troy Kisky7355f272016-02-05 14:52:46 -07003132 bd_dma += size;
3133 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3134 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003135 }
3136
3137 for (i = 0; i < fep->num_tx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003138 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3139 unsigned size = dsize * txq->bd.ring_size;
3140
3141 txq->bd.qid = i;
3142 txq->bd.base = cbd_base;
3143 txq->bd.cur = cbd_base;
3144 txq->bd.dma = bd_dma;
3145 txq->bd.dsize = dsize;
3146 txq->bd.dsize_log2 = dsize_log2;
Troy Kisky53bb20d2016-02-05 14:52:47 -07003147 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
Troy Kisky7355f272016-02-05 14:52:46 -07003148 bd_dma += size;
3149 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3150 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003151 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Sascha Hauer22f6b862009-04-15 01:32:18 +00003154 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003155 ndev->watchdog_timeo = TX_TIMEOUT;
3156 ndev->netdev_ops = &fec_netdev_ops;
3157 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003158
Frank Lidc975382013-01-28 18:31:42 +00003159 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003160 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003161
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003162 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003163 /* enable hw VLAN support */
3164 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003165
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003166 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003167 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3168
Shawn Guo48496252013-05-08 21:08:22 +00003169 /* enable hw accelerator */
3170 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003171 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003172 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3173 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003174
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003175 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003176 fep->tx_align = 0;
3177 fep->rx_align = 0x3f;
3178 }
3179
Nimrod Andy09d1e542014-06-12 08:16:20 +08003180 ndev->hw_features = ndev->features;
3181
Russell Kingef833372014-07-08 12:40:43 +01003182 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return 0;
3185}
3186
Shawn Guoca2cc332011-06-25 02:04:35 +08003187#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003188static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003189{
3190 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003191 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003192 struct device_node *np = pdev->dev.of_node;
3193
3194 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003195 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003196
Shawn Guoa3caad02012-06-27 03:45:24 +00003197 of_property_read_u32(np, "phy-reset-duration", &msec);
3198 /* A sane reset duration should not be longer than 1s */
3199 if (msec > 1000)
3200 msec = 1;
3201
Shawn Guoca2cc332011-06-25 02:04:35 +08003202 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003203 if (!gpio_is_valid(phy_reset))
3204 return;
3205
Shawn Guo119fc002012-06-27 03:45:22 +00003206 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3207 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003208 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003209 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003210 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003211 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003212 msleep(msec);
Fabio Estevamf4444572015-10-28 10:20:30 -02003213 gpio_set_value_cansleep(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003214}
3215#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003216static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003217{
3218 /*
3219 * In case of platform probe, the reset has been done
3220 * by machine code.
3221 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003222}
3223#endif /* CONFIG_OF */
3224
Fugang Duan9fc095f2014-09-13 05:00:49 +08003225static void
3226fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3227{
3228 struct device_node *np = pdev->dev.of_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003229
3230 *num_tx = *num_rx = 1;
3231
3232 if (!np || !of_device_is_available(np))
3233 return;
3234
3235 /* parse the num of tx and rx queues */
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303236 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003237
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303238 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003239
3240 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003241 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3242 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003243 *num_tx = 1;
3244 return;
3245 }
3246
3247 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003248 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3249 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003250 *num_rx = 1;
3251 return;
3252 }
3253
3254}
3255
Bill Pemberton33897cc2012-12-03 09:23:58 -05003256static int
Sascha Haueread73182009-01-28 23:03:11 +00003257fec_probe(struct platform_device *pdev)
3258{
3259 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003260 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003261 struct net_device *ndev;
3262 int i, irq, ret = 0;
3263 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003264 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003265 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003266 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003267 int num_tx_qs;
3268 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003269
Fugang Duan9fc095f2014-09-13 05:00:49 +08003270 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3271
Sascha Haueread73182009-01-28 23:03:11 +00003272 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003273 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3274 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003275 if (!ndev)
3276 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003277
3278 SET_NETDEV_DEV(ndev, &pdev->dev);
3279
3280 /* setup board info structure */
3281 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003282
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003283 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3284 if (of_id)
3285 pdev->id_entry = of_id->data;
3286 fep->quirks = pdev->id_entry->driver_data;
3287
Hubert Feurstein0c818592015-01-07 14:48:17 +01003288 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003289 fep->num_rx_queues = num_rx_qs;
3290 fep->num_tx_queues = num_tx_qs;
3291
Guenter Roeckd1391932013-06-18 10:04:59 -07003292#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003293 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003294 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003295 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003296#endif
Frank Libaa70a52013-01-16 16:55:58 +00003297
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003298 /* Select default pin state */
3299 pinctrl_pm_select_default_state(&pdev->dev);
3300
Fabio Estevam399db752013-07-21 13:25:03 -03003301 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303302 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3303 if (IS_ERR(fep->hwp)) {
3304 ret = PTR_ERR(fep->hwp);
3305 goto failed_ioremap;
3306 }
3307
Bryan Wue6b043d2010-03-31 02:10:44 +00003308 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003309 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003310
Sascha Haueread73182009-01-28 23:03:11 +00003311 platform_set_drvdata(pdev, ndev);
3312
Nimrod Andyde40ed32014-12-24 17:30:39 +08003313 if (of_get_property(np, "fsl,magic-packet", NULL))
3314 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3315
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003316 phy_node = of_parse_phandle(np, "phy-handle", 0);
3317 if (!phy_node && of_phy_is_fixed_link(np)) {
3318 ret = of_phy_register_fixed_link(np);
3319 if (ret < 0) {
3320 dev_err(&pdev->dev,
3321 "broken fixed-link specification\n");
3322 goto failed_phy;
3323 }
3324 phy_node = of_node_get(np);
3325 }
3326 fep->phy_node = phy_node;
3327
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003328 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003329 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003330 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003331 if (pdata)
3332 fep->phy_interface = pdata->phy;
3333 else
3334 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3335 } else {
3336 fep->phy_interface = ret;
3337 }
3338
Fabio Estevame2f8d552013-02-22 06:40:45 +00003339 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3340 if (IS_ERR(fep->clk_ipg)) {
3341 ret = PTR_ERR(fep->clk_ipg);
3342 goto failed_clk;
3343 }
3344
3345 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3346 if (IS_ERR(fep->clk_ahb)) {
3347 ret = PTR_ERR(fep->clk_ahb);
3348 goto failed_clk;
3349 }
3350
Fugang Duand851b472014-09-17 05:18:52 +08003351 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3352
Linus Torvalds38f56f32013-05-07 11:06:17 -07003353 /* enet_out is optional, depends on board */
3354 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3355 if (IS_ERR(fep->clk_enet_out))
3356 fep->clk_enet_out = NULL;
3357
Nimrod Andy91c0d982014-08-21 17:09:38 +08003358 fep->ptp_clk_on = false;
3359 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003360
3361 /* clk_ref is optional, depends on board */
3362 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3363 if (IS_ERR(fep->clk_ref))
3364 fep->clk_ref = NULL;
3365
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003366 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003367 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003368 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003369 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003370 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003371 }
3372
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003373 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003374 if (ret)
3375 goto failed_clk;
3376
Andrew Lunn8fff7552015-07-25 22:38:02 +02003377 ret = clk_prepare_enable(fep->clk_ipg);
3378 if (ret)
3379 goto failed_clk_ipg;
3380
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003381 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3382 if (!IS_ERR(fep->reg_phy)) {
3383 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003384 if (ret) {
3385 dev_err(&pdev->dev,
3386 "Failed to enable phy regulator: %d\n", ret);
3387 goto failed_regulator;
3388 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003389 } else {
3390 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003391 }
3392
Andrew Lunn8fff7552015-07-25 22:38:02 +02003393 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3394 pm_runtime_use_autosuspend(&pdev->dev);
Lucas Stach14d2b7c2015-08-03 17:50:11 +02003395 pm_runtime_get_noresume(&pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003396 pm_runtime_set_active(&pdev->dev);
3397 pm_runtime_enable(&pdev->dev);
3398
Fabio Estevame2f8d552013-02-22 06:40:45 +00003399 fec_reset_phy(pdev);
3400
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003401 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003402 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003403
3404 ret = fec_enet_init(ndev);
3405 if (ret)
3406 goto failed_init;
3407
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003408 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003409 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003410 if (irq < 0) {
3411 if (i)
3412 break;
3413 ret = irq;
3414 goto failed_irq;
3415 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003416 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003417 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003418 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003419 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003420
3421 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003422 }
3423
Fugang Duanb4d39b52014-09-13 05:00:55 +08003424 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003425 ret = fec_enet_mii_init(pdev);
3426 if (ret)
3427 goto failed_mii_init;
3428
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003429 /* Carrier starts down, phylib will bring it up */
3430 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003431 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003432 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003433
Sascha Haueread73182009-01-28 23:03:11 +00003434 ret = register_netdev(ndev);
3435 if (ret)
3436 goto failed_register;
3437
Nimrod Andyde40ed32014-12-24 17:30:39 +08003438 device_init_wakeup(&ndev->dev, fep->wol_flag &
3439 FEC_WOL_HAS_MAGIC_PACKET);
3440
Fabio Estevameb1d0642013-04-13 07:25:36 +00003441 if (fep->bufdesc_ex && fep->ptp_clock)
3442 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3443
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003444 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003445 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003446
3447 pm_runtime_mark_last_busy(&pdev->dev);
3448 pm_runtime_put_autosuspend(&pdev->dev);
3449
Sascha Haueread73182009-01-28 23:03:11 +00003450 return 0;
3451
3452failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003453 fec_enet_mii_remove(fep);
3454failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003455failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003456failed_init:
Lucas Stach32cba572015-07-23 16:06:20 +02003457 fec_ptp_stop(pdev);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003458 if (fep->reg_phy)
3459 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003460failed_regulator:
Andrew Lunn8fff7552015-07-25 22:38:02 +02003461 clk_disable_unprepare(fep->clk_ipg);
3462failed_clk_ipg:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003463 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003464failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003465failed_phy:
3466 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003467failed_ioremap:
3468 free_netdev(ndev);
3469
3470 return ret;
3471}
3472
Bill Pemberton33897cc2012-12-03 09:23:58 -05003473static int
Sascha Haueread73182009-01-28 23:03:11 +00003474fec_drv_remove(struct platform_device *pdev)
3475{
3476 struct net_device *ndev = platform_get_drvdata(pdev);
3477 struct fec_enet_private *fep = netdev_priv(ndev);
3478
Russell King36cdc742014-07-08 13:01:44 +01003479 cancel_work_sync(&fep->tx_timeout_work);
Lucas Stach32cba572015-07-23 16:06:20 +02003480 fec_ptp_stop(pdev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003481 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003482 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003483 if (fep->reg_phy)
3484 regulator_disable(fep->reg_phy);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003485 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003486 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003487
Sascha Haueread73182009-01-28 23:03:11 +00003488 return 0;
3489}
3490
Fabio Estevamdd66d382014-07-24 18:24:01 -03003491static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003492{
Eric Benard87cad5c2010-06-18 04:19:54 +00003493 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003494 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003495
Russell Kingda1774e2014-07-08 12:39:57 +01003496 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003497 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003498 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3499 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Russell Kingd76cfae2014-07-08 00:23:04 +01003500 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003501 napi_disable(&fep->napi);
3502 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003503 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003504 netif_tx_unlock_bh(ndev);
3505 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003506 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003507 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3508 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003509 }
Russell Kingda1774e2014-07-08 12:39:57 +01003510 rtnl_unlock();
3511
Nimrod Andyde40ed32014-12-24 17:30:39 +08003512 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003513 regulator_disable(fep->reg_phy);
3514
Nimrod Andy858eeb72014-12-11 09:20:31 +08003515 /* SOC supply clock to phy, when clock is disabled, phy link down
3516 * SOC control phy regulator, when regulator is disabled, phy link down
3517 */
3518 if (fep->clk_enet_out || fep->reg_phy)
3519 fep->link = 0;
3520
Sascha Haueread73182009-01-28 23:03:11 +00003521 return 0;
3522}
3523
Fabio Estevamdd66d382014-07-24 18:24:01 -03003524static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003525{
Eric Benard87cad5c2010-06-18 04:19:54 +00003526 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003527 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003528 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003529 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003530 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003531
Nimrod Andyde40ed32014-12-24 17:30:39 +08003532 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003533 ret = regulator_enable(fep->reg_phy);
3534 if (ret)
3535 return ret;
3536 }
Sascha Haueread73182009-01-28 23:03:11 +00003537
Russell Kingda1774e2014-07-08 12:39:57 +01003538 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003539 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003540 ret = fec_enet_clk_enable(ndev, true);
3541 if (ret) {
3542 rtnl_unlock();
3543 goto failed_clk;
3544 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003545 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3546 if (pdata && pdata->sleep_mode_enable)
3547 pdata->sleep_mode_enable(false);
3548 val = readl(fep->hwp + FEC_ECNTRL);
3549 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3550 writel(val, fep->hwp + FEC_ECNTRL);
3551 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3552 } else {
3553 pinctrl_pm_select_default_state(&fep->pdev->dev);
3554 }
Russell Kingef833372014-07-08 12:40:43 +01003555 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003556 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003557 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003558 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003559 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003560 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003561 }
Russell Kingda1774e2014-07-08 12:39:57 +01003562 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003563
Sascha Haueread73182009-01-28 23:03:11 +00003564 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003565
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003566failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003567 if (fep->reg_phy)
3568 regulator_disable(fep->reg_phy);
3569 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003570}
3571
Andrew Lunn8fff7552015-07-25 22:38:02 +02003572static int __maybe_unused fec_runtime_suspend(struct device *dev)
3573{
3574 struct net_device *ndev = dev_get_drvdata(dev);
3575 struct fec_enet_private *fep = netdev_priv(ndev);
3576
3577 clk_disable_unprepare(fep->clk_ipg);
3578
3579 return 0;
3580}
3581
3582static int __maybe_unused fec_runtime_resume(struct device *dev)
3583{
3584 struct net_device *ndev = dev_get_drvdata(dev);
3585 struct fec_enet_private *fep = netdev_priv(ndev);
3586
3587 return clk_prepare_enable(fep->clk_ipg);
3588}
3589
3590static const struct dev_pm_ops fec_pm_ops = {
3591 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3592 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3593};
Denis Kirjanov59d42892010-06-02 09:27:04 +00003594
Sascha Haueread73182009-01-28 23:03:11 +00003595static struct platform_driver fec_driver = {
3596 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003597 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003598 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003599 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003600 },
Shawn Guob5680e02011-01-05 21:13:13 +00003601 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003602 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003603 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003604};
3605
Fabio Estevamaaca2372012-01-23 16:44:50 +00003606module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003608MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609MODULE_LICENSE("GPL");