blob: 712e3bb1e0d330b002760b603326a53c48fac325 [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);
393 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800394 }
395
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800396 return bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800397dma_mapping_error:
Troy Kisky7355f272016-02-05 14:52:46 -0700398 bdp = txq->bd.cur;
Nimrod Andy6e909282014-06-12 08:16:22 +0800399 for (i = 0; i < frag; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -0700400 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100401 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
402 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
Nimrod Andy6e909282014-06-12 08:16:22 +0800403 }
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800404 return ERR_PTR(-ENOMEM);
Nimrod Andy6e909282014-06-12 08:16:22 +0800405}
406
Fugang Duan4d494cd2014-09-13 05:00:48 +0800407static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
408 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800409{
410 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800411 int nr_frags = skb_shinfo(skb)->nr_frags;
412 struct bufdesc *bdp, *last_bdp;
413 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100414 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800415 unsigned short status;
416 unsigned short buflen;
417 unsigned int estatus = 0;
418 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800419 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800420
Troy Kisky7355f272016-02-05 14:52:46 -0700421 entries_free = fec_enet_get_free_txdesc_num(txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800422 if (entries_free < MAX_SKB_FRAGS + 1) {
423 dev_kfree_skb_any(skb);
424 if (net_ratelimit())
425 netdev_err(ndev, "NOT enough BD for SG!\n");
426 return NETDEV_TX_OK;
427 }
428
Nimrod Andy6e909282014-06-12 08:16:22 +0800429 /* Protocol checksum off-load for TCP and UDP. */
430 if (fec_enet_clear_csum(skb, ndev)) {
431 dev_kfree_skb_any(skb);
432 return NETDEV_TX_OK;
433 }
434
435 /* Fill in a Tx ring entry */
Troy Kisky7355f272016-02-05 14:52:46 -0700436 bdp = txq->bd.cur;
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800437 last_bdp = bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100438 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800439 status &= ~BD_ENET_TX_STATS;
440
441 /* Set buffer length and buffer pointer */
442 bufaddr = skb->data;
443 buflen = skb_headlen(skb);
444
Troy Kisky7355f272016-02-05 14:52:46 -0700445 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800446 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100447 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800448 memcpy(txq->tx_bounce[index], skb->data, buflen);
449 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800450
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100451 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800452 swap_buffer(bufaddr, buflen);
453 }
454
Russell Kingd6bf3142014-07-08 00:23:19 +0100455 /* Push the data cache so the CPM does not get stale memory data. */
456 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
457 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800458 dev_kfree_skb_any(skb);
459 if (net_ratelimit())
460 netdev_err(ndev, "Tx DMA memory map failed\n");
461 return NETDEV_TX_OK;
462 }
463
464 if (nr_frags) {
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800465 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
466 if (IS_ERR(last_bdp))
467 return NETDEV_TX_OK;
Nimrod Andy6e909282014-06-12 08:16:22 +0800468 } else {
469 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
470 if (fep->bufdesc_ex) {
471 estatus = BD_ENET_TX_INT;
472 if (unlikely(skb_shinfo(skb)->tx_flags &
473 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
474 estatus |= BD_ENET_TX_TS;
475 }
476 }
477
478 if (fep->bufdesc_ex) {
479
480 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
481
482 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
483 fep->hwts_tx_en))
484 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
485
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100486 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700487 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andybefe8212014-09-23 15:40:55 +0800488
Nimrod Andy6e909282014-06-12 08:16:22 +0800489 if (skb->ip_summed == CHECKSUM_PARTIAL)
490 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
491
492 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100493 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800494 }
495
Troy Kisky7355f272016-02-05 14:52:46 -0700496 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
Nimrod Andy6e909282014-06-12 08:16:22 +0800497 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800498 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800499
Johannes Berg5cfa3032016-01-24 16:52:37 +0100500 bdp->cbd_datlen = cpu_to_fec16(buflen);
501 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Nimrod Andy6e909282014-06-12 08:16:22 +0800502
503 /* Send it on its way. Tell FEC it's ready, interrupt when done,
504 * it's the last BD of the frame, and to put the CRC on the end.
505 */
506 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100507 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800508
Sascha Hauer22f6b862009-04-15 01:32:18 +0000509 /* If this was the last BD in the ring, start at the beginning again. */
Troy Kisky7355f272016-02-05 14:52:46 -0700510 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800512 skb_tx_timestamp(skb);
513
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800514 /* Make sure the update to bdp and tx_skbuff are performed before
Troy Kisky7355f272016-02-05 14:52:46 -0700515 * txq->bd.cur.
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800516 */
517 wmb();
Troy Kisky7355f272016-02-05 14:52:46 -0700518 txq->bd.cur = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Frank Lide5fb0a2013-03-03 17:34:25 +0000520 /* Trigger transmission start */
Troy Kisky53bb20d2016-02-05 14:52:47 -0700521 writel(0, txq->bd.reg_desc_active);
Frank Lide5fb0a2013-03-03 17:34:25 +0000522
Nimrod Andy6e909282014-06-12 08:16:22 +0800523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Nimrod Andy79f33912014-06-12 08:16:23 +0800526static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800527fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
528 struct net_device *ndev,
529 struct bufdesc *bdp, int index, char *data,
530 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800531{
532 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200533 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800534 unsigned short status;
535 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100536 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800537
Johannes Berg5cfa3032016-01-24 16:52:37 +0100538 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800539 status &= ~BD_ENET_TX_STATS;
540
541 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800542
Fugang Duan41ef84c2014-09-13 05:00:54 +0800543 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100544 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800545 memcpy(txq->tx_bounce[index], data, size);
546 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800547
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100548 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800549 swap_buffer(data, size);
550 }
551
Russell Kingd6bf3142014-07-08 00:23:19 +0100552 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
553 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800554 dev_kfree_skb_any(skb);
555 if (net_ratelimit())
556 netdev_err(ndev, "Tx DMA memory map failed\n");
557 return NETDEV_TX_BUSY;
558 }
559
Johannes Berg5cfa3032016-01-24 16:52:37 +0100560 bdp->cbd_datlen = cpu_to_fec16(size);
561 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Russell Kingd6bf3142014-07-08 00:23:19 +0100562
Nimrod Andy79f33912014-06-12 08:16:23 +0800563 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100564 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700565 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy79f33912014-06-12 08:16:23 +0800566 if (skb->ip_summed == CHECKSUM_PARTIAL)
567 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
568 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100569 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800570 }
571
572 /* Handle the last BD specially */
573 if (last_tcp)
574 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
575 if (is_last) {
576 status |= BD_ENET_TX_INTR;
577 if (fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100578 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
Nimrod Andy79f33912014-06-12 08:16:23 +0800579 }
580
Johannes Berg5cfa3032016-01-24 16:52:37 +0100581 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800582
583 return 0;
584}
585
586static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800587fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
588 struct sk_buff *skb, struct net_device *ndev,
589 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800590{
591 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800592 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200593 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800594 void *bufaddr;
595 unsigned long dmabuf;
596 unsigned short status;
597 unsigned int estatus = 0;
598
Johannes Berg5cfa3032016-01-24 16:52:37 +0100599 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800600 status &= ~BD_ENET_TX_STATS;
601 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
602
Fugang Duan4d494cd2014-09-13 05:00:48 +0800603 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
604 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800605 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100606 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800607 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
608 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800609
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100610 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800611 swap_buffer(bufaddr, hdr_len);
612
613 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
614 hdr_len, DMA_TO_DEVICE);
615 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
616 dev_kfree_skb_any(skb);
617 if (net_ratelimit())
618 netdev_err(ndev, "Tx DMA memory map failed\n");
619 return NETDEV_TX_BUSY;
620 }
621 }
622
Johannes Berg5cfa3032016-01-24 16:52:37 +0100623 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
624 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
Nimrod Andy79f33912014-06-12 08:16:23 +0800625
626 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100627 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700628 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
Nimrod Andy79f33912014-06-12 08:16:23 +0800629 if (skb->ip_summed == CHECKSUM_PARTIAL)
630 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
631 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100632 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800633 }
634
Johannes Berg5cfa3032016-01-24 16:52:37 +0100635 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800636
637 return 0;
638}
639
Fugang Duan4d494cd2014-09-13 05:00:48 +0800640static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
641 struct sk_buff *skb,
642 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800643{
644 struct fec_enet_private *fep = netdev_priv(ndev);
645 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
646 int total_len, data_left;
Troy Kisky7355f272016-02-05 14:52:46 -0700647 struct bufdesc *bdp = txq->bd.cur;
Nimrod Andy79f33912014-06-12 08:16:23 +0800648 struct tso_t tso;
649 unsigned int index = 0;
650 int ret;
651
Troy Kisky7355f272016-02-05 14:52:46 -0700652 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800653 dev_kfree_skb_any(skb);
654 if (net_ratelimit())
655 netdev_err(ndev, "NOT enough BD for TSO!\n");
656 return NETDEV_TX_OK;
657 }
658
659 /* Protocol checksum off-load for TCP and UDP. */
660 if (fec_enet_clear_csum(skb, ndev)) {
661 dev_kfree_skb_any(skb);
662 return NETDEV_TX_OK;
663 }
664
665 /* Initialize the TSO handler, and prepare the first payload */
666 tso_start(skb, &tso);
667
668 total_len = skb->len - hdr_len;
669 while (total_len > 0) {
670 char *hdr;
671
Troy Kisky7355f272016-02-05 14:52:46 -0700672 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800673 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
674 total_len -= data_left;
675
676 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800677 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800678 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800679 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800680 if (ret)
681 goto err_release;
682
683 while (data_left > 0) {
684 int size;
685
686 size = min_t(int, tso.size, data_left);
Troy Kisky7355f272016-02-05 14:52:46 -0700687 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
688 index = fec_enet_get_bd_index(bdp, &txq->bd);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800689 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
690 bdp, index,
691 tso.data, size,
692 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800693 total_len == 0);
694 if (ret)
695 goto err_release;
696
697 data_left -= size;
698 tso_build_data(skb, &tso, size);
699 }
700
Troy Kisky7355f272016-02-05 14:52:46 -0700701 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Nimrod Andy79f33912014-06-12 08:16:23 +0800702 }
703
704 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800705 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800706
Nimrod Andy79f33912014-06-12 08:16:23 +0800707 skb_tx_timestamp(skb);
Troy Kisky7355f272016-02-05 14:52:46 -0700708 txq->bd.cur = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800709
710 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100711 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Troy Kisky53bb20d2016-02-05 14:52:47 -0700712 !readl(txq->bd.reg_desc_active) ||
713 !readl(txq->bd.reg_desc_active) ||
714 !readl(txq->bd.reg_desc_active) ||
715 !readl(txq->bd.reg_desc_active))
716 writel(0, txq->bd.reg_desc_active);
Nimrod Andy79f33912014-06-12 08:16:23 +0800717
718 return 0;
719
720err_release:
721 /* TODO: Release all used data descriptors for TSO */
722 return ret;
723}
724
Nimrod Andy61a44272014-06-12 08:16:18 +0800725static netdev_tx_t
726fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
727{
728 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800729 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800730 unsigned short queue;
731 struct fec_enet_priv_tx_q *txq;
732 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800733 int ret;
734
Fugang Duan4d494cd2014-09-13 05:00:48 +0800735 queue = skb_get_queue_mapping(skb);
736 txq = fep->tx_queue[queue];
737 nq = netdev_get_tx_queue(ndev, queue);
738
Nimrod Andy79f33912014-06-12 08:16:23 +0800739 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800740 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800741 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800742 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800743 if (ret)
744 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800745
Troy Kisky7355f272016-02-05 14:52:46 -0700746 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800747 if (entries_free <= txq->tx_stop_threshold)
748 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800749
750 return NETDEV_TX_OK;
751}
752
Frank Li14109a52013-03-26 16:12:03 +0000753/* Init RX & TX buffer descriptors
754 */
755static void fec_enet_bd_init(struct net_device *dev)
756{
757 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800758 struct fec_enet_priv_tx_q *txq;
759 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000760 struct bufdesc *bdp;
761 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800762 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000763
Frank Li59d0f7462014-09-13 05:00:50 +0800764 for (q = 0; q < fep->num_rx_queues; q++) {
765 /* Initialize the receive buffer descriptors. */
766 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700767 bdp = rxq->bd.base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800768
Troy Kisky7355f272016-02-05 14:52:46 -0700769 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000770
Frank Li59d0f7462014-09-13 05:00:50 +0800771 /* Initialize the BD for every fragment in the page. */
772 if (bdp->cbd_bufaddr)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100773 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Li59d0f7462014-09-13 05:00:50 +0800774 else
Johannes Berg5cfa3032016-01-24 16:52:37 +0100775 bdp->cbd_sc = cpu_to_fec16(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700776 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Frank Li14109a52013-03-26 16:12:03 +0000777 }
Frank Li59d0f7462014-09-13 05:00:50 +0800778
779 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700780 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100781 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800782
Troy Kisky7355f272016-02-05 14:52:46 -0700783 rxq->bd.cur = rxq->bd.base;
Frank Li14109a52013-03-26 16:12:03 +0000784 }
785
Frank Li59d0f7462014-09-13 05:00:50 +0800786 for (q = 0; q < fep->num_tx_queues; q++) {
787 /* ...and the same for transmit */
788 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -0700789 bdp = txq->bd.base;
790 txq->bd.cur = bdp;
Frank Li59d0f7462014-09-13 05:00:50 +0800791
Troy Kisky7355f272016-02-05 14:52:46 -0700792 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800793 /* Initialize the BD for every fragment in the page. */
Johannes Berg5cfa3032016-01-24 16:52:37 +0100794 bdp->cbd_sc = cpu_to_fec16(0);
Frank Li59d0f7462014-09-13 05:00:50 +0800795 if (txq->tx_skbuff[i]) {
796 dev_kfree_skb_any(txq->tx_skbuff[i]);
797 txq->tx_skbuff[i] = NULL;
798 }
Johannes Berg5cfa3032016-01-24 16:52:37 +0100799 bdp->cbd_bufaddr = cpu_to_fec32(0);
Troy Kisky7355f272016-02-05 14:52:46 -0700800 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Li59d0f7462014-09-13 05:00:50 +0800801 }
802
803 /* Set the last buffer to wrap */
Troy Kisky7355f272016-02-05 14:52:46 -0700804 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100805 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +0800806 txq->dirty_tx = bdp;
807 }
808}
809
Frank Lice99d0d2014-09-13 05:00:52 +0800810static void fec_enet_active_rxring(struct net_device *ndev)
811{
812 struct fec_enet_private *fep = netdev_priv(ndev);
813 int i;
814
815 for (i = 0; i < fep->num_rx_queues; i++)
Troy Kisky53bb20d2016-02-05 14:52:47 -0700816 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
Frank Lice99d0d2014-09-13 05:00:52 +0800817}
818
Frank Li59d0f7462014-09-13 05:00:50 +0800819static void fec_enet_enable_ring(struct net_device *ndev)
820{
821 struct fec_enet_private *fep = netdev_priv(ndev);
822 struct fec_enet_priv_tx_q *txq;
823 struct fec_enet_priv_rx_q *rxq;
824 int i;
825
826 for (i = 0; i < fep->num_rx_queues; i++) {
827 rxq = fep->rx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700828 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
Nimrod Andyd543a762014-11-23 17:23:06 +0800829 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800830
831 /* enable DMA1/2 */
832 if (i)
833 writel(RCMR_MATCHEN | RCMR_CMP(i),
834 fep->hwp + FEC_RCMR(i));
835 }
836
837 for (i = 0; i < fep->num_tx_queues; i++) {
838 txq = fep->tx_queue[i];
Troy Kisky7355f272016-02-05 14:52:46 -0700839 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800840
841 /* enable DMA1/2 */
842 if (i)
843 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
844 fep->hwp + FEC_DMA_CFG(i));
845 }
846}
847
848static void fec_enet_reset_skb(struct net_device *ndev)
849{
850 struct fec_enet_private *fep = netdev_priv(ndev);
851 struct fec_enet_priv_tx_q *txq;
852 int i, j;
853
854 for (i = 0; i < fep->num_tx_queues; i++) {
855 txq = fep->tx_queue[i];
856
Troy Kisky7355f272016-02-05 14:52:46 -0700857 for (j = 0; j < txq->bd.ring_size; j++) {
Frank Li59d0f7462014-09-13 05:00:50 +0800858 if (txq->tx_skbuff[j]) {
859 dev_kfree_skb_any(txq->tx_skbuff[j]);
860 txq->tx_skbuff[j] = NULL;
861 }
862 }
863 }
Frank Li14109a52013-03-26 16:12:03 +0000864}
865
Russell Kingdbc64a82014-07-08 12:40:12 +0100866/*
867 * This function is called to start or restart the FEC during a link
868 * change, transmit timeout, or to reconfigure the FEC. The network
869 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100870 */
871static void
Russell Kingef833372014-07-08 12:40:43 +0100872fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100873{
874 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000875 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100876 u32 temp_mac[2];
877 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000878 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100879
Fugang Duan106c3142014-09-13 05:00:51 +0800880 /* Whack a reset. We should wait for this.
881 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
882 * instead of reset MAC itself.
883 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100884 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800885 writel(0, fep->hwp + FEC_ECNTRL);
886 } else {
887 writel(1, fep->hwp + FEC_ECNTRL);
888 udelay(10);
889 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100890
891 /*
892 * enet-mac reset will reset mac address registers too,
893 * so need to reconfigure it.
894 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100895 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100896 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100897 writel((__force u32)cpu_to_be32(temp_mac[0]),
898 fep->hwp + FEC_ADDR_LOW);
899 writel((__force u32)cpu_to_be32(temp_mac[1]),
900 fep->hwp + FEC_ADDR_HIGH);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100901 }
902
903 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800904 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100905
Frank Li14109a52013-03-26 16:12:03 +0000906 fec_enet_bd_init(ndev);
907
Frank Li59d0f7462014-09-13 05:00:50 +0800908 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100909
Frank Li59d0f7462014-09-13 05:00:50 +0800910 /* Reset tx SKB buffers. */
911 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100912
913 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100914 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100915 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100916 writel(0x04, fep->hwp + FEC_X_CNTRL);
917 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100918 /* No Rcv on Xmit */
919 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100920 writel(0x0, fep->hwp + FEC_X_CNTRL);
921 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100922
Uwe Kleine-König45993652011-01-19 20:47:04 +0100923 /* Set MII speed */
924 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
925
Guenter Roeckd1391932013-06-18 10:04:59 -0700926#if !defined(CONFIG_M5272)
Greg Ungerer18803492015-06-20 15:51:57 +1000927 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
928 /* set RX checksum */
929 val = readl(fep->hwp + FEC_RACC);
930 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
931 val |= FEC_RACC_OPTIONS;
932 else
933 val &= ~FEC_RACC_OPTIONS;
934 writel(val, fep->hwp + FEC_RACC);
935 }
Troy Kisky55cd48c2016-02-05 14:52:43 -0700936 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
Guenter Roeckd1391932013-06-18 10:04:59 -0700937#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000938
Uwe Kleine-König45993652011-01-19 20:47:04 +0100939 /*
940 * The phy interface and speed need to get configured
941 * differently on enet-mac.
942 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100943 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100944 /* Enable flow control and length check */
945 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100946
Shawn Guo230dec62011-09-23 02:12:48 +0000947 /* RGMII, RMII or MII */
Markus Pargmanne813bb22015-04-30 17:07:50 +0200948 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
949 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
950 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
951 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shawn Guo230dec62011-09-23 02:12:48 +0000952 rcntl |= (1 << 6);
953 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100954 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100955 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100956 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100957
Shawn Guo230dec62011-09-23 02:12:48 +0000958 /* 1G, 100M or 10M */
959 if (fep->phy_dev) {
960 if (fep->phy_dev->speed == SPEED_1000)
961 ecntl |= (1 << 5);
962 else if (fep->phy_dev->speed == SPEED_100)
963 rcntl &= ~(1 << 9);
964 else
965 rcntl |= (1 << 9);
966 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100967 } else {
968#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100969 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000970 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100971 /* disable the gasket and wait */
972 writel(0, fep->hwp + FEC_MIIGSK_ENR);
973 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
974 udelay(1);
975
976 /*
977 * configure the gasket:
978 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800979 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100980 */
Eric Benard8d82f212012-01-12 06:10:28 +0000981 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
982 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
983 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
984 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
985 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100986
987 /* re-enable the gasket */
988 writel(2, fep->hwp + FEC_MIIGSK_ENR);
989 }
990#endif
991 }
Frank Libaa70a52013-01-16 16:55:58 +0000992
Guenter Roeckd1391932013-06-18 10:04:59 -0700993#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000994 /* enable pause frame*/
995 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
996 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
997 fep->phy_dev && fep->phy_dev->pause)) {
998 rcntl |= FEC_ENET_FCE;
999
Jim Baxter4c09eed2013-04-19 08:10:49 +00001000 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001001 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1002 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1003 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1004 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1005
1006 /* OPD */
1007 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1008 } else {
1009 rcntl &= ~FEC_ENET_FCE;
1010 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001011#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001012
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001013 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001014
Stefan Wahren84fe6182014-03-12 11:28:19 +01001015 /* Setup multicast filter. */
1016 set_multicast_list(ndev);
1017#ifndef CONFIG_M5272
1018 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1019 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1020#endif
1021
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001022 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001023 /* enable ENET endian swap */
1024 ecntl |= (1 << 8);
1025 /* enable ENET store and forward mode */
1026 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1027 }
1028
Frank Liff43da82013-01-03 16:04:23 +00001029 if (fep->bufdesc_ex)
1030 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001031
Chris Healy38ae92d2013-06-25 23:18:52 -07001032#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001033 /* Enable the MIB statistic event counters */
1034 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001035#endif
1036
Uwe Kleine-König45993652011-01-19 20:47:04 +01001037 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001038 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001039 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001040
Frank Liff43da82013-01-03 16:04:23 +00001041 if (fep->bufdesc_ex)
1042 fec_ptp_start_cyclecounter(ndev);
1043
Uwe Kleine-König45993652011-01-19 20:47:04 +01001044 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001045 if (fep->link)
1046 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1047 else
1048 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001049
1050 /* Init the interrupt coalescing */
1051 fec_enet_itr_coal_init(ndev);
1052
Uwe Kleine-König45993652011-01-19 20:47:04 +01001053}
1054
1055static void
1056fec_stop(struct net_device *ndev)
1057{
1058 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001059 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001060 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001061 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001062
1063 /* We cannot expect a graceful transmit stop without link !!! */
1064 if (fep->link) {
1065 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1066 udelay(10);
1067 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001068 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001069 }
1070
Fugang Duan106c3142014-09-13 05:00:51 +08001071 /* Whack a reset. We should wait for this.
1072 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1073 * instead of reset MAC itself.
1074 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001075 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1076 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1077 writel(0, fep->hwp + FEC_ECNTRL);
1078 } else {
1079 writel(1, fep->hwp + FEC_ECNTRL);
1080 udelay(10);
1081 }
1082 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001083 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001084 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1085 val = readl(fep->hwp + FEC_ECNTRL);
1086 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1087 writel(val, fep->hwp + FEC_ECNTRL);
1088
1089 if (pdata && pdata->sleep_mode_enable)
1090 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001091 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001092 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001093
1094 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001095 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1096 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001097 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001098 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1099 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001100}
1101
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001104fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001106 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Russell King344756f2014-07-08 13:01:59 +01001108 fec_dump(ndev);
1109
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001110 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Russell King36cdc742014-07-08 13:01:44 +01001112 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001113}
1114
Russell King36cdc742014-07-08 13:01:44 +01001115static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001116{
1117 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001118 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001119 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001120
Russell King36cdc742014-07-08 13:01:44 +01001121 rtnl_lock();
1122 if (netif_device_present(ndev) || netif_running(ndev)) {
1123 napi_disable(&fep->napi);
1124 netif_tx_lock_bh(ndev);
1125 fec_restart(ndev);
1126 netif_wake_queue(ndev);
1127 netif_tx_unlock_bh(ndev);
1128 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001129 }
Russell King36cdc742014-07-08 13:01:44 +01001130 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001134fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1135 struct skb_shared_hwtstamps *hwtstamps)
1136{
1137 unsigned long flags;
1138 u64 ns;
1139
1140 spin_lock_irqsave(&fep->tmreg_lock, flags);
1141 ns = timecounter_cyc2time(&fep->tc, ts);
1142 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1143
1144 memset(hwtstamps, 0, sizeof(*hwtstamps));
1145 hwtstamps->hwtstamp = ns_to_ktime(ns);
1146}
1147
1148static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001149fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct fec_enet_private *fep;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001152 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001153 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001155 struct fec_enet_priv_tx_q *txq;
1156 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001157 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001158 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001160 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001161
1162 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1163
1164 txq = fep->tx_queue[queue_id];
1165 /* get next bdp of dirty_tx */
1166 nq = netdev_get_tx_queue(ndev, queue_id);
1167 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Frank Lide5fb0a2013-03-03 17:34:25 +00001169 /* get next bdp of dirty_tx */
Troy Kisky7355f272016-02-05 14:52:46 -07001170 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Frank Lide5fb0a2013-03-03 17:34:25 +00001171
Troy Kisky7355f272016-02-05 14:52:46 -07001172 while (bdp != READ_ONCE(txq->bd.cur)) {
1173 /* Order the load of bd.cur and cbd_sc */
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001174 rmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +01001175 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001176 if (status & BD_ENET_TX_READY)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001177 break;
1178
Troy Kisky7355f272016-02-05 14:52:46 -07001179 index = fec_enet_get_bd_index(bdp, &txq->bd);
Nimrod Andy2b995f62015-01-20 14:10:35 +08001180
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001181 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001182 txq->tx_skbuff[index] = NULL;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001183 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1184 dma_unmap_single(&fep->pdev->dev,
1185 fec32_to_cpu(bdp->cbd_bufaddr),
1186 fec16_to_cpu(bdp->cbd_datlen),
1187 DMA_TO_DEVICE);
1188 bdp->cbd_bufaddr = cpu_to_fec32(0);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001189 if (!skb) {
Troy Kisky7355f272016-02-05 14:52:46 -07001190 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001191 continue;
1192 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001195 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 BD_ENET_TX_RL | BD_ENET_TX_UN |
1197 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001198 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001199 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001200 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001201 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001202 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001203 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001204 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001205 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001206 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001207 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001208 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001210 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001211 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
Frank Liff43da82013-01-03 16:04:23 +00001214 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1215 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001216 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001217 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001218
Johannes Berg5cfa3032016-01-24 16:52:37 +01001219 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001220 skb_tstamp_tx(skb, &shhwtstamps);
1221 }
Frank Liff43da82013-01-03 16:04:23 +00001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /* Deferred means some collisions occurred during transmit,
1224 * but we eventually sent the packet OK.
1225 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001226 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001227 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001228
Sascha Hauer22f6b862009-04-15 01:32:18 +00001229 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001231
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001232 /* Make sure the update to bdp and tx_skbuff are performed
1233 * before dirty_tx
1234 */
1235 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +08001236 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001237
Sascha Hauer22f6b862009-04-15 01:32:18 +00001238 /* Update pointer to next buffer descriptor to be transmitted */
Troy Kisky7355f272016-02-05 14:52:46 -07001239 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001240
Sascha Hauer22f6b862009-04-15 01:32:18 +00001241 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001243 if (netif_queue_stopped(ndev)) {
Troy Kisky7355f272016-02-05 14:52:46 -07001244 entries_free = fec_enet_get_free_txdesc_num(txq);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001245 if (entries_free >= txq->tx_wake_threshold)
1246 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Russell Kingccea2962014-07-08 13:01:38 +01001249
1250 /* ERR006538: Keep the transmitter going */
Troy Kisky7355f272016-02-05 14:52:46 -07001251 if (bdp != txq->bd.cur &&
Troy Kisky53bb20d2016-02-05 14:52:47 -07001252 readl(txq->bd.reg_desc_active) == 0)
1253 writel(0, txq->bd.reg_desc_active);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001254}
1255
1256static void
1257fec_enet_tx(struct net_device *ndev)
1258{
1259 struct fec_enet_private *fep = netdev_priv(ndev);
1260 u16 queue_id;
1261 /* First process class A queue, then Class B and Best Effort queue */
1262 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1263 clear_bit(queue_id, &fep->work_tx);
1264 fec_enet_tx_queue(ndev, queue_id);
1265 }
1266 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001269static int
1270fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1271{
1272 struct fec_enet_private *fep = netdev_priv(ndev);
1273 int off;
1274
1275 off = ((unsigned long)skb->data) & fep->rx_align;
1276 if (off)
1277 skb_reserve(skb, fep->rx_align + 1 - off);
1278
Johannes Berg5cfa3032016-01-24 16:52:37 +01001279 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1280 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001281 if (net_ratelimit())
1282 netdev_err(ndev, "Rx DMA memory map failed\n");
1283 return -ENOMEM;
1284 }
1285
1286 return 0;
1287}
1288
1289static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001290 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001291{
1292 struct fec_enet_private *fep = netdev_priv(ndev);
1293 struct sk_buff *new_skb;
1294
1295 if (length > fep->rx_copybreak)
1296 return false;
1297
1298 new_skb = netdev_alloc_skb(ndev, length);
1299 if (!new_skb)
1300 return false;
1301
Johannes Berg5cfa3032016-01-24 16:52:37 +01001302 dma_sync_single_for_cpu(&fep->pdev->dev,
1303 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001304 FEC_ENET_RX_FRSIZE - fep->rx_align,
1305 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001306 if (!swap)
1307 memcpy(new_skb->data, (*skb)->data, length);
1308 else
1309 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001310 *skb = new_skb;
1311
1312 return true;
1313}
1314
Troy Kisky7355f272016-02-05 14:52:46 -07001315/* During a receive, the bd_rx.cur points to the current incoming buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * When we update through the ring, if the next incoming buffer has
1317 * not been given to the system, we just set the empty indicator,
1318 * effectively tossing the packet.
1319 */
Frank Lidc975382013-01-28 18:31:42 +00001320static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001321fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001323 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001324 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001325 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001326 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001327 struct sk_buff *skb_new = NULL;
1328 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ushort pkt_len;
1330 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001331 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001332 struct bufdesc_ex *ebdp = NULL;
1333 bool vlan_packet_rcvd = false;
1334 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001335 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001336 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001337 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001338
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001339#ifdef CONFIG_M532x
1340 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001341#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001342 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1343 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* First, grab all of the stats for the incoming packet.
1346 * These get messed up if we get called due to a busy condition.
1347 */
Troy Kisky7355f272016-02-05 14:52:46 -07001348 bdp = rxq->bd.cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Johannes Berg5cfa3032016-01-24 16:52:37 +01001350 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Frank Lidc975382013-01-28 18:31:42 +00001352 if (pkt_received >= budget)
1353 break;
1354 pkt_received++;
1355
Russell Kinged63f1d2015-09-02 17:24:14 +08001356 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
Russell Kingdb3421c2014-07-08 13:01:49 +01001357
Sascha Hauer22f6b862009-04-15 01:32:18 +00001358 /* Check for errors. */
Troy Kisky095098e2016-02-05 14:52:44 -07001359 status ^= BD_ENET_RX_LAST;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001360 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Troy Kisky095098e2016-02-05 14:52:44 -07001361 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1362 BD_ENET_RX_CL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001363 ndev->stats.rx_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001364 if (status & BD_ENET_RX_OV) {
1365 /* FIFO overrun */
1366 ndev->stats.rx_fifo_errors++;
1367 goto rx_processing_done;
1368 }
1369 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1370 | BD_ENET_RX_LAST)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +00001371 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001372 ndev->stats.rx_length_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001373 if (status & BD_ENET_RX_LAST)
1374 netdev_err(ndev, "rcv is not +last\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001375 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001376 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001377 ndev->stats.rx_crc_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001378 /* Report late collisions as a frame error. */
1379 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1380 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001381 goto rx_processing_done;
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Sascha Hauer22f6b862009-04-15 01:32:18 +00001384 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001385 ndev->stats.rx_packets++;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001386 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001387 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Troy Kisky7355f272016-02-05 14:52:46 -07001389 index = fec_enet_get_bd_index(bdp, &rxq->bd);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001390 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001391
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001392 /* The packet length includes FCS, but we don't want to
1393 * include that when passing upstream as it messes up
1394 * bridging applications.
1395 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001396 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1397 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001398 if (!is_copybreak) {
1399 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1400 if (unlikely(!skb_new)) {
1401 ndev->stats.rx_dropped++;
1402 goto rx_processing_done;
1403 }
Johannes Berg5cfa3032016-01-24 16:52:37 +01001404 dma_unmap_single(&fep->pdev->dev,
1405 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001406 FEC_ENET_RX_FRSIZE - fep->rx_align,
1407 DMA_FROM_DEVICE);
1408 }
1409
1410 prefetch(skb->data - NET_IP_ALIGN);
1411 skb_put(skb, pkt_len - 4);
1412 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001413 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001414 swap_buffer(data, pkt_len);
1415
Jim Baxtercdffcf12013-07-02 22:52:56 +01001416 /* Extract the enhanced buffer descriptor */
1417 ebdp = NULL;
1418 if (fep->bufdesc_ex)
1419 ebdp = (struct bufdesc_ex *)bdp;
1420
1421 /* If this is a VLAN packet remove the VLAN Tag */
1422 vlan_packet_rcvd = false;
1423 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Johannes Berg5cfa3032016-01-24 16:52:37 +01001424 fep->bufdesc_ex &&
1425 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001426 /* Push and remove the vlan tag */
1427 struct vlan_hdr *vlan_header =
1428 (struct vlan_hdr *) (data + ETH_HLEN);
1429 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001430
1431 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001432
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001433 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001434 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001435 }
1436
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001437 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001439 /* Get receive timestamp from the skb */
1440 if (fep->hwts_rx_en && fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +01001441 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001442 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001443
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001444 if (fep->bufdesc_ex &&
1445 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001446 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001447 /* don't check it */
1448 skb->ip_summed = CHECKSUM_UNNECESSARY;
1449 } else {
1450 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001451 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001452 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001453
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001454 /* Handle received VLAN packets */
1455 if (vlan_packet_rcvd)
1456 __vlan_hwaccel_put_tag(skb,
1457 htons(ETH_P_8021Q),
1458 vlan_tag);
1459
1460 napi_gro_receive(&fep->napi, skb);
1461
1462 if (is_copybreak) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001463 dma_sync_single_for_device(&fep->pdev->dev,
1464 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001465 FEC_ENET_RX_FRSIZE - fep->rx_align,
1466 DMA_FROM_DEVICE);
1467 } else {
1468 rxq->rx_skbuff[index] = skb_new;
1469 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1470 }
1471
Sascha Hauer22f6b862009-04-15 01:32:18 +00001472rx_processing_done:
1473 /* Clear the status flags for this buffer */
1474 status &= ~BD_ENET_RX_STATS;
1475
1476 /* Mark the buffer empty */
1477 status |= BD_ENET_RX_EMPTY;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001478 bdp->cbd_sc = cpu_to_fec16(status);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001479
Frank Liff43da82013-01-03 16:04:23 +00001480 if (fep->bufdesc_ex) {
1481 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1482
Johannes Berg5cfa3032016-01-24 16:52:37 +01001483 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00001484 ebdp->cbd_prot = 0;
1485 ebdp->cbd_bdu = 0;
1486 }
Frank Li6605b732012-10-30 18:25:31 +00001487
Sascha Hauer22f6b862009-04-15 01:32:18 +00001488 /* Update BD pointer to next entry */
Troy Kisky7355f272016-02-05 14:52:46 -07001489 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001490
Sascha Hauer22f6b862009-04-15 01:32:18 +00001491 /* Doing this here will keep the FEC running while we process
1492 * incoming frames. On a heavily loaded network, we should be
1493 * able to keep up at the expense of system resources.
1494 */
Troy Kisky53bb20d2016-02-05 14:52:47 -07001495 writel(0, rxq->bd.reg_desc_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
Troy Kisky7355f272016-02-05 14:52:46 -07001497 rxq->bd.cur = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001498 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Fugang Duan4d494cd2014-09-13 05:00:48 +08001501static int
1502fec_enet_rx(struct net_device *ndev, int budget)
1503{
1504 int pkt_received = 0;
1505 u16 queue_id;
1506 struct fec_enet_private *fep = netdev_priv(ndev);
1507
1508 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1509 clear_bit(queue_id, &fep->work_rx);
1510 pkt_received += fec_enet_rx_queue(ndev,
1511 budget - pkt_received, queue_id);
1512 }
1513 return pkt_received;
1514}
1515
1516static bool
1517fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1518{
1519 if (int_events == 0)
1520 return false;
1521
1522 if (int_events & FEC_ENET_RXF)
1523 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001524 if (int_events & FEC_ENET_RXF_1)
1525 fep->work_rx |= (1 << 0);
1526 if (int_events & FEC_ENET_RXF_2)
1527 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001528
1529 if (int_events & FEC_ENET_TXF)
1530 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001531 if (int_events & FEC_ENET_TXF_1)
1532 fep->work_tx |= (1 << 0);
1533 if (int_events & FEC_ENET_TXF_2)
1534 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001535
1536 return true;
1537}
1538
Uwe Kleine-König45993652011-01-19 20:47:04 +01001539static irqreturn_t
1540fec_enet_interrupt(int irq, void *dev_id)
1541{
1542 struct net_device *ndev = dev_id;
1543 struct fec_enet_private *fep = netdev_priv(ndev);
1544 uint int_events;
1545 irqreturn_t ret = IRQ_NONE;
1546
Russell King7a168072014-07-08 00:22:44 +01001547 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001548 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001549 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001550
Fugang Duan61615cd2015-03-04 07:52:11 +08001551 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001552 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001553
Nimrod Andy94191fd2014-12-16 18:25:58 +08001554 if (napi_schedule_prep(&fep->napi)) {
1555 /* Disable the NAPI interrupts */
1556 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1557 __napi_schedule(&fep->napi);
1558 }
Russell King7a168072014-07-08 00:22:44 +01001559 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001560
Russell King7a168072014-07-08 00:22:44 +01001561 if (int_events & FEC_ENET_MII) {
1562 ret = IRQ_HANDLED;
1563 complete(&fep->mdio_done);
1564 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001565
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001566 if (fep->ptp_clock)
1567 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001568
Uwe Kleine-König45993652011-01-19 20:47:04 +01001569 return ret;
1570}
1571
Frank Lidc975382013-01-28 18:31:42 +00001572static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1573{
1574 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001575 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001576 int pkts;
1577
Russell King7a168072014-07-08 00:22:44 +01001578 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001579
Frank Lide5fb0a2013-03-03 17:34:25 +00001580 fec_enet_tx(ndev);
1581
Frank Lidc975382013-01-28 18:31:42 +00001582 if (pkts < budget) {
1583 napi_complete(napi);
1584 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1585 }
1586 return pkts;
1587}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001590static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001592 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001593 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001594 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Shawn Guo49da97d2011-01-05 21:13:11 +00001596 /*
1597 * try to get mac address in following order:
1598 *
1599 * 1) module parameter via kernel command line in form
1600 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1601 */
1602 iap = macaddr;
1603
1604 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001605 * 2) from device tree data
1606 */
1607 if (!is_valid_ether_addr(iap)) {
1608 struct device_node *np = fep->pdev->dev.of_node;
1609 if (np) {
1610 const char *mac = of_get_mac_address(np);
1611 if (mac)
1612 iap = (unsigned char *) mac;
1613 }
1614 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001615
1616 /*
1617 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001618 */
1619 if (!is_valid_ether_addr(iap)) {
1620#ifdef CONFIG_M5272
1621 if (FEC_FLASHMAC)
1622 iap = (unsigned char *)FEC_FLASHMAC;
1623#else
1624 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001625 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001626#endif
1627 }
1628
1629 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001630 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001631 */
1632 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001633 *((__be32 *) &tmpaddr[0]) =
1634 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1635 *((__be16 *) &tmpaddr[4]) =
1636 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 iap = &tmpaddr[0];
1638 }
1639
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001640 /*
1641 * 5) random mac address
1642 */
1643 if (!is_valid_ether_addr(iap)) {
1644 /* Report it and use a random ethernet address instead */
1645 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1646 eth_hw_addr_random(ndev);
1647 netdev_info(ndev, "Using random MAC address: %pM\n",
1648 ndev->dev_addr);
1649 return;
1650 }
1651
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001652 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Shawn Guo49da97d2011-01-05 21:13:11 +00001654 /* Adjust MAC if using macaddr */
1655 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001656 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659/* ------------------------------------------------------------------------- */
1660
Bryan Wue6b043d2010-03-31 02:10:44 +00001661/*
1662 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001664static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001666 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001667 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001668 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Bryan Wue6b043d2010-03-31 02:10:44 +00001670 /* Prevent a state halted on mii error */
1671 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1672 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001673 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001675
Russell King8ce56242014-07-08 12:40:07 +01001676 /*
1677 * If the netdev is down, or is going down, we're not interested
1678 * in link state events, so just mark our idea of the link as down
1679 * and ignore the event.
1680 */
1681 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1682 fep->link = 0;
1683 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001684 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001685 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001686 status_change = 1;
1687 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001688
Russell Kingef833372014-07-08 12:40:43 +01001689 if (fep->full_duplex != phy_dev->duplex) {
1690 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001691 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001692 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001693
1694 if (phy_dev->speed != fep->speed) {
1695 fep->speed = phy_dev->speed;
1696 status_change = 1;
1697 }
1698
1699 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001700 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001701 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001702 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001703 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001704 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001705 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001706 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001707 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001708 } else {
1709 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001710 napi_disable(&fep->napi);
1711 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001712 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001713 netif_tx_unlock_bh(ndev);
1714 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001715 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001716 status_change = 1;
1717 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001718 }
1719
Bryan Wue6b043d2010-03-31 02:10:44 +00001720 if (status_change)
1721 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Bryan Wue6b043d2010-03-31 02:10:44 +00001724static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Bryan Wue6b043d2010-03-31 02:10:44 +00001726 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001727 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001728 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001729 int ret = 0;
1730
1731 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001732 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001733 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001734
1735 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001736 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001737
1738 /* start a read op */
1739 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1740 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1741 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1742
1743 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001744 time_left = wait_for_completion_timeout(&fep->mdio_done,
1745 usecs_to_jiffies(FEC_MII_TIMEOUT));
1746 if (time_left == 0) {
1747 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001748 netdev_err(fep->netdev, "MDIO read timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001749 ret = -ETIMEDOUT;
1750 goto out;
Bryan Wue6b043d2010-03-31 02:10:44 +00001751 }
1752
Andrew Lunn8fff7552015-07-25 22:38:02 +02001753 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1754
1755out:
1756 pm_runtime_mark_last_busy(dev);
1757 pm_runtime_put_autosuspend(dev);
1758
1759 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001760}
1761
1762static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1763 u16 value)
1764{
1765 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001766 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001767 unsigned long time_left;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001768 int ret;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001769
1770 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001771 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001772 return ret;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001773 else
1774 ret = 0;
Bryan Wue6b043d2010-03-31 02:10:44 +00001775
1776 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001777 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001778
Shawn Guo862f0982011-01-05 21:13:09 +00001779 /* start a write op */
1780 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001781 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1782 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1783 fep->hwp + FEC_MII_DATA);
1784
1785 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001786 time_left = wait_for_completion_timeout(&fep->mdio_done,
1787 usecs_to_jiffies(FEC_MII_TIMEOUT));
1788 if (time_left == 0) {
1789 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001790 netdev_err(fep->netdev, "MDIO write timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001791 ret = -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001792 }
1793
Andrew Lunn8fff7552015-07-25 22:38:02 +02001794 pm_runtime_mark_last_busy(dev);
1795 pm_runtime_put_autosuspend(dev);
1796
1797 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001798}
1799
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001800static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1801{
1802 struct fec_enet_private *fep = netdev_priv(ndev);
1803 int ret;
1804
1805 if (enable) {
1806 ret = clk_prepare_enable(fep->clk_ahb);
1807 if (ret)
1808 return ret;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001809 if (fep->clk_enet_out) {
1810 ret = clk_prepare_enable(fep->clk_enet_out);
1811 if (ret)
1812 goto failed_clk_enet_out;
1813 }
1814 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001815 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001816 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001817 if (ret) {
1818 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001819 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001820 } else {
1821 fep->ptp_clk_on = true;
1822 }
1823 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001824 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001825 if (fep->clk_ref) {
1826 ret = clk_prepare_enable(fep->clk_ref);
1827 if (ret)
1828 goto failed_clk_ref;
1829 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001830 } else {
1831 clk_disable_unprepare(fep->clk_ahb);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001832 if (fep->clk_enet_out)
1833 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001834 if (fep->clk_ptp) {
1835 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001836 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001837 fep->ptp_clk_on = false;
1838 mutex_unlock(&fep->ptp_clk_mutex);
1839 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001840 if (fep->clk_ref)
1841 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001842 }
1843
1844 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001845
1846failed_clk_ref:
1847 if (fep->clk_ref)
1848 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001849failed_clk_ptp:
1850 if (fep->clk_enet_out)
1851 clk_disable_unprepare(fep->clk_enet_out);
1852failed_clk_enet_out:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001853 clk_disable_unprepare(fep->clk_ahb);
1854
1855 return ret;
1856}
1857
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001858static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001859{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001860 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001861 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001862 char mdio_bus_id[MII_BUS_ID_SIZE];
1863 char phy_name[MII_BUS_ID_SIZE + 3];
1864 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001865 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001866
Bryan Wu418bd0d2010-05-28 03:40:39 -07001867 fep->phy_dev = NULL;
1868
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001869 if (fep->phy_node) {
1870 phy_dev = of_phy_connect(ndev, fep->phy_node,
1871 &fec_enet_adjust_link, 0,
1872 fep->phy_interface);
Nimrod Andy213a9922014-12-09 18:46:56 +08001873 if (!phy_dev)
1874 return -ENODEV;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001875 } else {
1876 /* check for attached phy */
1877 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
Andrew Lunn7f854422016-01-06 20:11:18 +01001878 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001879 continue;
1880 if (dev_id--)
1881 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001882 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001883 break;
1884 }
1885
1886 if (phy_id >= PHY_MAX_ADDR) {
1887 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001888 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001889 phy_id = 0;
1890 }
1891
1892 snprintf(phy_name, sizeof(phy_name),
1893 PHY_ID_FMT, mdio_bus_id, phy_id);
1894 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1895 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001896 }
1897
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001898 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001899 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001900 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001901 }
1902
1903 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001904 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001905 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001906 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001907#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001908 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001909#endif
Frank Libaa70a52013-01-16 16:55:58 +00001910 }
Shawn Guo230dec62011-09-23 02:12:48 +00001911 else
1912 phy_dev->supported &= PHY_BASIC_FEATURES;
1913
Bryan Wue6b043d2010-03-31 02:10:44 +00001914 phy_dev->advertising = phy_dev->supported;
1915
1916 fep->phy_dev = phy_dev;
1917 fep->link = 0;
1918 fep->full_duplex = 0;
1919
Andrew Lunn22209432016-01-06 20:11:13 +01001920 phy_attached_info(phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001921
Bryan Wue6b043d2010-03-31 02:10:44 +00001922 return 0;
1923}
1924
1925static int fec_enet_mii_init(struct platform_device *pdev)
1926{
Shawn Guob5680e02011-01-05 21:13:13 +00001927 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001928 struct net_device *ndev = platform_get_drvdata(pdev);
1929 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001930 struct device_node *node;
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001931 int err = -ENXIO;
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001932 u32 mii_speed, holdtime;
Bryan Wue6b043d2010-03-31 02:10:44 +00001933
Shawn Guob5680e02011-01-05 21:13:13 +00001934 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001935 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001936 * Here are the differences:
1937 *
1938 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1939 * - fec0 acts as the 1588 time master while fec1 is slave
1940 * - external phys can only be configured by fec0
1941 *
1942 * That is to say fec1 can not work independently. It only works
1943 * when fec0 is working. The reason behind this design is that the
1944 * second interface is added primarily for Switch mode.
1945 *
1946 * Because of the last point above, both phys are attached on fec0
1947 * mdio interface in board design, and need to be configured by
1948 * fec0 mii_bus.
1949 */
Stefan Agner3d125f92015-01-14 00:20:21 +01001950 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001951 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001952 if (mii_cnt && fec0_mii_bus) {
1953 fep->mii_bus = fec0_mii_bus;
1954 mii_cnt++;
1955 return 0;
1956 }
1957 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001958 }
1959
Bryan Wue6b043d2010-03-31 02:10:44 +00001960 fep->mii_timeout = 0;
1961
1962 /*
1963 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001964 *
1965 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1966 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1967 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1968 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001969 */
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001970 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001971 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001972 mii_speed--;
1973 if (mii_speed > 63) {
1974 dev_err(&pdev->dev,
1975 "fec clock (%lu) to fast to get right mii speed\n",
1976 clk_get_rate(fep->clk_ipg));
1977 err = -EINVAL;
1978 goto err_out;
1979 }
1980
1981 /*
1982 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
1983 * MII_SPEED) register that defines the MDIO output hold time. Earlier
1984 * versions are RAZ there, so just ignore the difference and write the
1985 * register always.
1986 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
1987 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
1988 * output.
1989 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
1990 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
1991 * holdtime cannot result in a value greater than 3.
1992 */
1993 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
1994
1995 fep->phy_speed = mii_speed << 1 | holdtime << 8;
1996
Bryan Wue6b043d2010-03-31 02:10:44 +00001997 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1998
1999 fep->mii_bus = mdiobus_alloc();
2000 if (fep->mii_bus == NULL) {
2001 err = -ENOMEM;
2002 goto err_out;
2003 }
2004
2005 fep->mii_bus->name = "fec_enet_mii_bus";
2006 fep->mii_bus->read = fec_enet_mdio_read;
2007 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002008 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2009 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002010 fep->mii_bus->priv = fep;
2011 fep->mii_bus->parent = &pdev->dev;
2012
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002013 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2014 if (node) {
2015 err = of_mdiobus_register(fep->mii_bus, node);
2016 of_node_put(node);
2017 } else {
2018 err = mdiobus_register(fep->mii_bus);
2019 }
2020
2021 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002022 goto err_out_free_mdiobus;
Bryan Wue6b043d2010-03-31 02:10:44 +00002023
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002024 mii_cnt++;
2025
Shawn Guob5680e02011-01-05 21:13:13 +00002026 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002027 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002028 fec0_mii_bus = fep->mii_bus;
2029
Bryan Wue6b043d2010-03-31 02:10:44 +00002030 return 0;
2031
Bryan Wue6b043d2010-03-31 02:10:44 +00002032err_out_free_mdiobus:
2033 mdiobus_free(fep->mii_bus);
2034err_out:
2035 return err;
2036}
2037
2038static void fec_enet_mii_remove(struct fec_enet_private *fep)
2039{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002040 if (--mii_cnt == 0) {
2041 mdiobus_unregister(fep->mii_bus);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002042 mdiobus_free(fep->mii_bus);
2043 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002044}
2045
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002046static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002047 struct ethtool_cmd *cmd)
2048{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002049 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002050 struct phy_device *phydev = fep->phy_dev;
2051
2052 if (!phydev)
2053 return -ENODEV;
2054
2055 return phy_ethtool_gset(phydev, cmd);
2056}
2057
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002058static int fec_enet_set_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_sset(phydev, cmd);
2068}
2069
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002070static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002071 struct ethtool_drvinfo *info)
2072{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002073 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Jiri Pirko7826d432013-01-06 00:44:26 +00002075 strlcpy(info->driver, fep->pdev->dev.driver->name,
2076 sizeof(info->driver));
2077 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2078 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
Bryan Wue6b043d2010-03-31 02:10:44 +00002080
Philippe Reynesdb65f352015-05-11 00:01:42 +02002081static int fec_enet_get_regs_len(struct net_device *ndev)
2082{
2083 struct fec_enet_private *fep = netdev_priv(ndev);
2084 struct resource *r;
2085 int s = 0;
2086
2087 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2088 if (r)
2089 s = resource_size(r);
2090
2091 return s;
2092}
2093
2094/* List of registers that can be safety be read to dump them with ethtool */
2095#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Johannes Berg05f3b502016-01-25 11:40:50 +01002096 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Philippe Reynesdb65f352015-05-11 00:01:42 +02002097static u32 fec_enet_register_offset[] = {
2098 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2099 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2100 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2101 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2102 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2103 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2104 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2105 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2106 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2107 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2108 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2109 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2110 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2111 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2112 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2113 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2114 RMON_T_P_GTE2048, RMON_T_OCTETS,
2115 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2116 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2117 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2118 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2119 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2120 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2121 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2122 RMON_R_P_GTE2048, RMON_R_OCTETS,
2123 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2124 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2125};
2126#else
2127static u32 fec_enet_register_offset[] = {
2128 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2129 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2130 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2131 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2132 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2133 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2134 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2135 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2136 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2137};
2138#endif
2139
2140static void fec_enet_get_regs(struct net_device *ndev,
2141 struct ethtool_regs *regs, void *regbuf)
2142{
2143 struct fec_enet_private *fep = netdev_priv(ndev);
2144 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2145 u32 *buf = (u32 *)regbuf;
2146 u32 i, off;
2147
2148 memset(buf, 0, regs->len);
2149
2150 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2151 off = fec_enet_register_offset[i] / 4;
2152 buf[off] = readl(&theregs[off]);
2153 }
2154}
2155
Frank Li5ebae4892013-01-06 16:25:07 +00002156static int fec_enet_get_ts_info(struct net_device *ndev,
2157 struct ethtool_ts_info *info)
2158{
2159 struct fec_enet_private *fep = netdev_priv(ndev);
2160
2161 if (fep->bufdesc_ex) {
2162
2163 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2164 SOF_TIMESTAMPING_RX_SOFTWARE |
2165 SOF_TIMESTAMPING_SOFTWARE |
2166 SOF_TIMESTAMPING_TX_HARDWARE |
2167 SOF_TIMESTAMPING_RX_HARDWARE |
2168 SOF_TIMESTAMPING_RAW_HARDWARE;
2169 if (fep->ptp_clock)
2170 info->phc_index = ptp_clock_index(fep->ptp_clock);
2171 else
2172 info->phc_index = -1;
2173
2174 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2175 (1 << HWTSTAMP_TX_ON);
2176
2177 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2178 (1 << HWTSTAMP_FILTER_ALL);
2179 return 0;
2180 } else {
2181 return ethtool_op_get_ts_info(ndev, info);
2182 }
2183}
2184
Guenter Roeckd1391932013-06-18 10:04:59 -07002185#if !defined(CONFIG_M5272)
2186
Frank Libaa70a52013-01-16 16:55:58 +00002187static void fec_enet_get_pauseparam(struct net_device *ndev,
2188 struct ethtool_pauseparam *pause)
2189{
2190 struct fec_enet_private *fep = netdev_priv(ndev);
2191
2192 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2193 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2194 pause->rx_pause = pause->tx_pause;
2195}
2196
2197static int fec_enet_set_pauseparam(struct net_device *ndev,
2198 struct ethtool_pauseparam *pause)
2199{
2200 struct fec_enet_private *fep = netdev_priv(ndev);
2201
Russell King0b146ca2014-07-08 00:22:59 +01002202 if (!fep->phy_dev)
2203 return -ENODEV;
2204
Frank Libaa70a52013-01-16 16:55:58 +00002205 if (pause->tx_pause != pause->rx_pause) {
2206 netdev_info(ndev,
2207 "hardware only support enable/disable both tx and rx");
2208 return -EINVAL;
2209 }
2210
2211 fep->pause_flag = 0;
2212
2213 /* tx pause must be same as rx pause */
2214 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2215 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2216
2217 if (pause->rx_pause || pause->autoneg) {
2218 fep->phy_dev->supported |= ADVERTISED_Pause;
2219 fep->phy_dev->advertising |= ADVERTISED_Pause;
2220 } else {
2221 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2222 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2223 }
2224
2225 if (pause->autoneg) {
2226 if (netif_running(ndev))
2227 fec_stop(ndev);
2228 phy_start_aneg(fep->phy_dev);
2229 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002230 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002231 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002232 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002233 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002234 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002235 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002236 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002237 }
Frank Libaa70a52013-01-16 16:55:58 +00002238
2239 return 0;
2240}
2241
Chris Healy38ae92d2013-06-25 23:18:52 -07002242static const struct fec_stat {
2243 char name[ETH_GSTRING_LEN];
2244 u16 offset;
2245} fec_stats[] = {
2246 /* RMON TX */
2247 { "tx_dropped", RMON_T_DROP },
2248 { "tx_packets", RMON_T_PACKETS },
2249 { "tx_broadcast", RMON_T_BC_PKT },
2250 { "tx_multicast", RMON_T_MC_PKT },
2251 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2252 { "tx_undersize", RMON_T_UNDERSIZE },
2253 { "tx_oversize", RMON_T_OVERSIZE },
2254 { "tx_fragment", RMON_T_FRAG },
2255 { "tx_jabber", RMON_T_JAB },
2256 { "tx_collision", RMON_T_COL },
2257 { "tx_64byte", RMON_T_P64 },
2258 { "tx_65to127byte", RMON_T_P65TO127 },
2259 { "tx_128to255byte", RMON_T_P128TO255 },
2260 { "tx_256to511byte", RMON_T_P256TO511 },
2261 { "tx_512to1023byte", RMON_T_P512TO1023 },
2262 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2263 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2264 { "tx_octets", RMON_T_OCTETS },
2265
2266 /* IEEE TX */
2267 { "IEEE_tx_drop", IEEE_T_DROP },
2268 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2269 { "IEEE_tx_1col", IEEE_T_1COL },
2270 { "IEEE_tx_mcol", IEEE_T_MCOL },
2271 { "IEEE_tx_def", IEEE_T_DEF },
2272 { "IEEE_tx_lcol", IEEE_T_LCOL },
2273 { "IEEE_tx_excol", IEEE_T_EXCOL },
2274 { "IEEE_tx_macerr", IEEE_T_MACERR },
2275 { "IEEE_tx_cserr", IEEE_T_CSERR },
2276 { "IEEE_tx_sqe", IEEE_T_SQE },
2277 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2278 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2279
2280 /* RMON RX */
2281 { "rx_packets", RMON_R_PACKETS },
2282 { "rx_broadcast", RMON_R_BC_PKT },
2283 { "rx_multicast", RMON_R_MC_PKT },
2284 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2285 { "rx_undersize", RMON_R_UNDERSIZE },
2286 { "rx_oversize", RMON_R_OVERSIZE },
2287 { "rx_fragment", RMON_R_FRAG },
2288 { "rx_jabber", RMON_R_JAB },
2289 { "rx_64byte", RMON_R_P64 },
2290 { "rx_65to127byte", RMON_R_P65TO127 },
2291 { "rx_128to255byte", RMON_R_P128TO255 },
2292 { "rx_256to511byte", RMON_R_P256TO511 },
2293 { "rx_512to1023byte", RMON_R_P512TO1023 },
2294 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2295 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2296 { "rx_octets", RMON_R_OCTETS },
2297
2298 /* IEEE RX */
2299 { "IEEE_rx_drop", IEEE_R_DROP },
2300 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2301 { "IEEE_rx_crc", IEEE_R_CRC },
2302 { "IEEE_rx_align", IEEE_R_ALIGN },
2303 { "IEEE_rx_macerr", IEEE_R_MACERR },
2304 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2305 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2306};
2307
2308static void fec_enet_get_ethtool_stats(struct net_device *dev,
2309 struct ethtool_stats *stats, u64 *data)
2310{
2311 struct fec_enet_private *fep = netdev_priv(dev);
2312 int i;
2313
2314 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2315 data[i] = readl(fep->hwp + fec_stats[i].offset);
2316}
2317
2318static void fec_enet_get_strings(struct net_device *netdev,
2319 u32 stringset, u8 *data)
2320{
2321 int i;
2322 switch (stringset) {
2323 case ETH_SS_STATS:
2324 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2325 memcpy(data + i * ETH_GSTRING_LEN,
2326 fec_stats[i].name, ETH_GSTRING_LEN);
2327 break;
2328 }
2329}
2330
2331static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2332{
2333 switch (sset) {
2334 case ETH_SS_STATS:
2335 return ARRAY_SIZE(fec_stats);
2336 default:
2337 return -EOPNOTSUPP;
2338 }
2339}
Guenter Roeckd1391932013-06-18 10:04:59 -07002340#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002341
Chris Healy32bc9b42013-06-17 07:25:06 -07002342static int fec_enet_nway_reset(struct net_device *dev)
2343{
2344 struct fec_enet_private *fep = netdev_priv(dev);
2345 struct phy_device *phydev = fep->phy_dev;
2346
2347 if (!phydev)
2348 return -ENODEV;
2349
2350 return genphy_restart_aneg(phydev);
2351}
2352
Fugang Duand851b472014-09-17 05:18:52 +08002353/* ITR clock source is enet system clock (clk_ahb).
2354 * TCTT unit is cycle_ns * 64 cycle
2355 * So, the ICTT value = X us / (cycle_ns * 64)
2356 */
2357static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2358{
2359 struct fec_enet_private *fep = netdev_priv(ndev);
2360
2361 return us * (fep->itr_clk_rate / 64000) / 1000;
2362}
2363
2364/* Set threshold for interrupt coalescing */
2365static void fec_enet_itr_coal_set(struct net_device *ndev)
2366{
2367 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002368 int rx_itr, tx_itr;
2369
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002370 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002371 return;
2372
2373 /* Must be greater than zero to avoid unpredictable behavior */
2374 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2375 !fep->tx_time_itr || !fep->tx_pkts_itr)
2376 return;
2377
2378 /* Select enet system clock as Interrupt Coalescing
2379 * timer Clock Source
2380 */
2381 rx_itr = FEC_ITR_CLK_SEL;
2382 tx_itr = FEC_ITR_CLK_SEL;
2383
2384 /* set ICFT and ICTT */
2385 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2386 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2387 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2388 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2389
2390 rx_itr |= FEC_ITR_EN;
2391 tx_itr |= FEC_ITR_EN;
2392
2393 writel(tx_itr, fep->hwp + FEC_TXIC0);
2394 writel(rx_itr, fep->hwp + FEC_RXIC0);
2395 writel(tx_itr, fep->hwp + FEC_TXIC1);
2396 writel(rx_itr, fep->hwp + FEC_RXIC1);
2397 writel(tx_itr, fep->hwp + FEC_TXIC2);
2398 writel(rx_itr, fep->hwp + FEC_RXIC2);
2399}
2400
2401static int
2402fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2403{
2404 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002405
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002406 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002407 return -EOPNOTSUPP;
2408
2409 ec->rx_coalesce_usecs = fep->rx_time_itr;
2410 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2411
2412 ec->tx_coalesce_usecs = fep->tx_time_itr;
2413 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2414
2415 return 0;
2416}
2417
2418static int
2419fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2420{
2421 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002422 unsigned int cycle;
2423
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002424 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002425 return -EOPNOTSUPP;
2426
2427 if (ec->rx_max_coalesced_frames > 255) {
2428 pr_err("Rx coalesced frames exceed hardware limiation");
2429 return -EINVAL;
2430 }
2431
2432 if (ec->tx_max_coalesced_frames > 255) {
2433 pr_err("Tx coalesced frame exceed hardware limiation");
2434 return -EINVAL;
2435 }
2436
2437 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2438 if (cycle > 0xFFFF) {
2439 pr_err("Rx coalesed usec exceeed hardware limiation");
2440 return -EINVAL;
2441 }
2442
2443 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2444 if (cycle > 0xFFFF) {
2445 pr_err("Rx coalesed usec exceeed hardware limiation");
2446 return -EINVAL;
2447 }
2448
2449 fep->rx_time_itr = ec->rx_coalesce_usecs;
2450 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2451
2452 fep->tx_time_itr = ec->tx_coalesce_usecs;
2453 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2454
2455 fec_enet_itr_coal_set(ndev);
2456
2457 return 0;
2458}
2459
2460static void fec_enet_itr_coal_init(struct net_device *ndev)
2461{
2462 struct ethtool_coalesce ec;
2463
2464 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2465 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2466
2467 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2468 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2469
2470 fec_enet_set_coalesce(ndev, &ec);
2471}
2472
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002473static int fec_enet_get_tunable(struct net_device *netdev,
2474 const struct ethtool_tunable *tuna,
2475 void *data)
2476{
2477 struct fec_enet_private *fep = netdev_priv(netdev);
2478 int ret = 0;
2479
2480 switch (tuna->id) {
2481 case ETHTOOL_RX_COPYBREAK:
2482 *(u32 *)data = fep->rx_copybreak;
2483 break;
2484 default:
2485 ret = -EINVAL;
2486 break;
2487 }
2488
2489 return ret;
2490}
2491
2492static int fec_enet_set_tunable(struct net_device *netdev,
2493 const struct ethtool_tunable *tuna,
2494 const void *data)
2495{
2496 struct fec_enet_private *fep = netdev_priv(netdev);
2497 int ret = 0;
2498
2499 switch (tuna->id) {
2500 case ETHTOOL_RX_COPYBREAK:
2501 fep->rx_copybreak = *(u32 *)data;
2502 break;
2503 default:
2504 ret = -EINVAL;
2505 break;
2506 }
2507
2508 return ret;
2509}
2510
Nimrod Andyde40ed32014-12-24 17:30:39 +08002511static void
2512fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2513{
2514 struct fec_enet_private *fep = netdev_priv(ndev);
2515
2516 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2517 wol->supported = WAKE_MAGIC;
2518 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2519 } else {
2520 wol->supported = wol->wolopts = 0;
2521 }
2522}
2523
2524static int
2525fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2526{
2527 struct fec_enet_private *fep = netdev_priv(ndev);
2528
2529 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2530 return -EINVAL;
2531
2532 if (wol->wolopts & ~WAKE_MAGIC)
2533 return -EINVAL;
2534
2535 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2536 if (device_may_wakeup(&ndev->dev)) {
2537 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2538 if (fep->irq[0] > 0)
2539 enable_irq_wake(fep->irq[0]);
2540 } else {
2541 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2542 if (fep->irq[0] > 0)
2543 disable_irq_wake(fep->irq[0]);
2544 }
2545
2546 return 0;
2547}
2548
stephen hemminger9b07be42012-01-04 12:59:49 +00002549static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002550 .get_settings = fec_enet_get_settings,
2551 .set_settings = fec_enet_set_settings,
2552 .get_drvinfo = fec_enet_get_drvinfo,
Philippe Reynesdb65f352015-05-11 00:01:42 +02002553 .get_regs_len = fec_enet_get_regs_len,
2554 .get_regs = fec_enet_get_regs,
Chris Healy32bc9b42013-06-17 07:25:06 -07002555 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002556 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002557 .get_coalesce = fec_enet_get_coalesce,
2558 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002559#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002560 .get_pauseparam = fec_enet_get_pauseparam,
2561 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002562 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002563 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002564 .get_sset_count = fec_enet_get_sset_count,
2565#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002566 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002567 .get_tunable = fec_enet_get_tunable,
2568 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002569 .get_wol = fec_enet_get_wol,
2570 .set_wol = fec_enet_set_wol,
Bryan Wue6b043d2010-03-31 02:10:44 +00002571};
2572
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002573static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002574{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002575 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002576 struct phy_device *phydev = fep->phy_dev;
2577
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002578 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002579 return -EINVAL;
2580
2581 if (!phydev)
2582 return -ENODEV;
2583
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002584 if (fep->bufdesc_ex) {
2585 if (cmd == SIOCSHWTSTAMP)
2586 return fec_ptp_set(ndev, rq);
2587 if (cmd == SIOCGHWTSTAMP)
2588 return fec_ptp_get(ndev, rq);
2589 }
Frank Liff43da82013-01-03 16:04:23 +00002590
Richard Cochran28b04112010-07-17 08:48:55 +00002591 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002594static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002595{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002596 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002597 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002598 struct sk_buff *skb;
2599 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002600 struct fec_enet_priv_tx_q *txq;
2601 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002602 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002603
Frank Li59d0f7462014-09-13 05:00:50 +08002604 for (q = 0; q < fep->num_rx_queues; q++) {
2605 rxq = fep->rx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002606 bdp = rxq->bd.base;
2607 for (i = 0; i < rxq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002608 skb = rxq->rx_skbuff[i];
2609 rxq->rx_skbuff[i] = NULL;
2610 if (skb) {
2611 dma_unmap_single(&fep->pdev->dev,
Johannes Berg5cfa3032016-01-24 16:52:37 +01002612 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002613 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002614 DMA_FROM_DEVICE);
2615 dev_kfree_skb(skb);
2616 }
Troy Kisky7355f272016-02-05 14:52:46 -07002617 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Russell King730ee362014-07-08 00:23:14 +01002618 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002619 }
2620
Frank Li59d0f7462014-09-13 05:00:50 +08002621 for (q = 0; q < fep->num_tx_queues; q++) {
2622 txq = fep->tx_queue[q];
Troy Kisky7355f272016-02-05 14:52:46 -07002623 bdp = txq->bd.base;
2624 for (i = 0; i < txq->bd.ring_size; i++) {
Frank Li59d0f7462014-09-13 05:00:50 +08002625 kfree(txq->tx_bounce[i]);
2626 txq->tx_bounce[i] = NULL;
2627 skb = txq->tx_skbuff[i];
2628 txq->tx_skbuff[i] = NULL;
2629 dev_kfree_skb(skb);
2630 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002631 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002632}
2633
Frank Li59d0f7462014-09-13 05:00:50 +08002634static void fec_enet_free_queue(struct net_device *ndev)
2635{
2636 struct fec_enet_private *fep = netdev_priv(ndev);
2637 int i;
2638 struct fec_enet_priv_tx_q *txq;
2639
2640 for (i = 0; i < fep->num_tx_queues; i++)
2641 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2642 txq = fep->tx_queue[i];
2643 dma_free_coherent(NULL,
Troy Kisky7355f272016-02-05 14:52:46 -07002644 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002645 txq->tso_hdrs,
2646 txq->tso_hdrs_dma);
2647 }
2648
2649 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002650 kfree(fep->rx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002651 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002652 kfree(fep->tx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002653}
2654
2655static int fec_enet_alloc_queue(struct net_device *ndev)
2656{
2657 struct fec_enet_private *fep = netdev_priv(ndev);
2658 int i;
2659 int ret = 0;
2660 struct fec_enet_priv_tx_q *txq;
2661
2662 for (i = 0; i < fep->num_tx_queues; i++) {
2663 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2664 if (!txq) {
2665 ret = -ENOMEM;
2666 goto alloc_failed;
2667 }
2668
2669 fep->tx_queue[i] = txq;
Troy Kisky7355f272016-02-05 14:52:46 -07002670 txq->bd.ring_size = TX_RING_SIZE;
2671 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002672
2673 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2674 txq->tx_wake_threshold =
Troy Kisky7355f272016-02-05 14:52:46 -07002675 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
Frank Li59d0f7462014-09-13 05:00:50 +08002676
2677 txq->tso_hdrs = dma_alloc_coherent(NULL,
Troy Kisky7355f272016-02-05 14:52:46 -07002678 txq->bd.ring_size * TSO_HEADER_SIZE,
Frank Li59d0f7462014-09-13 05:00:50 +08002679 &txq->tso_hdrs_dma,
2680 GFP_KERNEL);
2681 if (!txq->tso_hdrs) {
2682 ret = -ENOMEM;
2683 goto alloc_failed;
2684 }
2685 }
2686
2687 for (i = 0; i < fep->num_rx_queues; i++) {
2688 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2689 GFP_KERNEL);
2690 if (!fep->rx_queue[i]) {
2691 ret = -ENOMEM;
2692 goto alloc_failed;
2693 }
2694
Troy Kisky7355f272016-02-05 14:52:46 -07002695 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
2696 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002697 }
2698 return ret;
2699
2700alloc_failed:
2701 fec_enet_free_queue(ndev);
2702 return ret;
2703}
2704
2705static int
2706fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002707{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002708 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002709 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002710 struct sk_buff *skb;
2711 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002712 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002713
Frank Li59d0f7462014-09-13 05:00:50 +08002714 rxq = fep->rx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002715 bdp = rxq->bd.base;
2716 for (i = 0; i < rxq->bd.ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002717 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002718 if (!skb)
2719 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002720
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002721 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002722 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002723 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002724 }
Russell King730ee362014-07-08 00:23:14 +01002725
Fugang Duan4d494cd2014-09-13 05:00:48 +08002726 rxq->rx_skbuff[i] = skb;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002727 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Liff43da82013-01-03 16:04:23 +00002728
2729 if (fep->bufdesc_ex) {
2730 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002731 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002732 }
2733
Troy Kisky7355f272016-02-05 14:52:46 -07002734 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002735 }
2736
2737 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002738 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002739 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f7462014-09-13 05:00:50 +08002740 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002741
Frank Li59d0f7462014-09-13 05:00:50 +08002742 err_alloc:
2743 fec_enet_free_buffers(ndev);
2744 return -ENOMEM;
2745}
2746
2747static int
2748fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2749{
2750 struct fec_enet_private *fep = netdev_priv(ndev);
2751 unsigned int i;
2752 struct bufdesc *bdp;
2753 struct fec_enet_priv_tx_q *txq;
2754
2755 txq = fep->tx_queue[queue];
Troy Kisky7355f272016-02-05 14:52:46 -07002756 bdp = txq->bd.base;
2757 for (i = 0; i < txq->bd.ring_size; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002758 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2759 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002760 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002761
Johannes Berg5cfa3032016-01-24 16:52:37 +01002762 bdp->cbd_sc = cpu_to_fec16(0);
2763 bdp->cbd_bufaddr = cpu_to_fec32(0);
Frank Li6605b732012-10-30 18:25:31 +00002764
Frank Liff43da82013-01-03 16:04:23 +00002765 if (fep->bufdesc_ex) {
2766 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002767 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002768 }
2769
Troy Kisky7355f272016-02-05 14:52:46 -07002770 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002771 }
2772
2773 /* Set the last buffer to wrap. */
Troy Kisky7355f272016-02-05 14:52:46 -07002774 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002775 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002776
2777 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002778
2779 err_alloc:
2780 fec_enet_free_buffers(ndev);
2781 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002782}
2783
Frank Li59d0f7462014-09-13 05:00:50 +08002784static int fec_enet_alloc_buffers(struct net_device *ndev)
2785{
2786 struct fec_enet_private *fep = netdev_priv(ndev);
2787 unsigned int i;
2788
2789 for (i = 0; i < fep->num_rx_queues; i++)
2790 if (fec_enet_alloc_rxq_buffers(ndev, i))
2791 return -ENOMEM;
2792
2793 for (i = 0; i < fep->num_tx_queues; i++)
2794 if (fec_enet_alloc_txq_buffers(ndev, i))
2795 return -ENOMEM;
2796 return 0;
2797}
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002800fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002802 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002803 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Andrew Lunn8fff7552015-07-25 22:38:02 +02002805 ret = pm_runtime_get_sync(&fep->pdev->dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03002806 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002807 return ret;
2808
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002809 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002810 ret = fec_enet_clk_enable(ndev, true);
2811 if (ret)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002812 goto clk_enable;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 /* I should reset the ring buffers here, but I don't yet know
2815 * a simple way to do that.
2816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002818 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002819 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002820 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002821
Nimrod Andy55dd2752015-06-23 15:32:51 +08002822 /* Init MAC prior to mii bus probe */
2823 fec_restart(ndev);
2824
Bryan Wu418bd0d2010-05-28 03:40:39 -07002825 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002826 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002827 if (ret)
2828 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002829
2830 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002831 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002832 netif_tx_start_all_queues(ndev);
2833
Nimrod Andyde40ed32014-12-24 17:30:39 +08002834 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2835 FEC_WOL_FLAG_ENABLE);
2836
Sascha Hauer22f6b862009-04-15 01:32:18 +00002837 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002838
2839err_enet_mii_probe:
2840 fec_enet_free_buffers(ndev);
2841err_enet_alloc:
2842 fec_enet_clk_enable(ndev, false);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002843clk_enable:
2844 pm_runtime_mark_last_busy(&fep->pdev->dev);
2845 pm_runtime_put_autosuspend(&fep->pdev->dev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002846 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
2850static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002851fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002853 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Russell Kingd76cfae2014-07-08 00:23:04 +01002855 phy_stop(fep->phy_dev);
2856
Russell King31a6de32014-07-08 12:40:23 +01002857 if (netif_device_present(ndev)) {
2858 napi_disable(&fep->napi);
2859 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002860 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Russell King635cf172014-07-08 00:22:54 +01002863 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002864 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002865
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002866 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002867 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002868 pm_runtime_mark_last_busy(&fep->pdev->dev);
2869 pm_runtime_put_autosuspend(&fep->pdev->dev);
2870
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002871 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return 0;
2874}
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876/* Set or clear the multicast filter for this adaptor.
2877 * Skeleton taken from sunlance driver.
2878 * The CPM Ethernet implementation allows Multicast as well as individual
2879 * MAC address filtering. Some of the drivers check to make sure it is
2880 * a group multicast address, and discard those that are not. I guess I
2881 * will do the same for now, but just remove the test if you want
2882 * individual filtering as well (do the upper net layers want or support
2883 * this kind of feature?).
2884 */
2885
2886#define HASH_BITS 6 /* #bits in hash */
2887#define CRC32_POLY 0xEDB88320
2888
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002889static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002891 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002892 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002893 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 unsigned char hash;
2895
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002896 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002897 tmp = readl(fep->hwp + FEC_R_CNTRL);
2898 tmp |= 0x8;
2899 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002900 return;
2901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Sascha Hauer4e831832009-04-15 01:32:19 +00002903 tmp = readl(fep->hwp + FEC_R_CNTRL);
2904 tmp &= ~0x8;
2905 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002906
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002907 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002908 /* Catch all multicast addresses, so set the
2909 * filter to all 1's
2910 */
2911 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2912 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Sascha Hauer4e831832009-04-15 01:32:19 +00002914 return;
2915 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002916
Sascha Hauer4e831832009-04-15 01:32:19 +00002917 /* Clear filter and add the addresses in hash register
2918 */
2919 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2920 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002922 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002923 /* calculate crc32 value of mac address */
2924 crc = 0xffffffff;
2925
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002926 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002927 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002928 for (bit = 0; bit < 8; bit++, data >>= 1) {
2929 crc = (crc >> 1) ^
2930 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
2932 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002933
2934 /* only upper 6 bits (HASH_BITS) are used
2935 * which point to specific bit in he hash registers
2936 */
2937 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2938
2939 if (hash > 31) {
2940 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2941 tmp |= 1 << (hash - 32);
2942 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2943 } else {
2944 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2945 tmp |= 1 << hash;
2946 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
2949}
2950
Sascha Hauer22f6b862009-04-15 01:32:18 +00002951/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002952static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002953fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002955 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002956 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Lucas Stach44934fa2014-03-30 21:32:08 +02002958 if (addr) {
2959 if (!is_valid_ether_addr(addr->sa_data))
2960 return -EADDRNOTAVAIL;
2961 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2962 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002963
Nimrod Andy9638d192015-09-10 09:35:39 +08002964 /* Add netif status check here to avoid system hang in below case:
2965 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
2966 * After ethx down, fec all clocks are gated off and then register
2967 * access causes system hang.
2968 */
2969 if (!netif_running(ndev))
2970 return 0;
2971
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002972 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2973 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002974 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002975 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002976 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002980#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002981/**
2982 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002983 * @dev: The FEC network adapter
2984 *
2985 * Polled functionality used by netconsole and others in non interrupt mode
2986 *
2987 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002988static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002989{
2990 int i;
2991 struct fec_enet_private *fep = netdev_priv(dev);
2992
2993 for (i = 0; i < FEC_IRQ_NUM; i++) {
2994 if (fep->irq[i] > 0) {
2995 disable_irq(fep->irq[i]);
2996 fec_enet_interrupt(fep->irq[i], dev);
2997 enable_irq(fep->irq[i]);
2998 }
2999 }
3000}
3001#endif
3002
Nimrod Andy5bc26722014-10-13 10:53:48 +08003003static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00003004 netdev_features_t features)
3005{
3006 struct fec_enet_private *fep = netdev_priv(netdev);
3007 netdev_features_t changed = features ^ netdev->features;
3008
3009 netdev->features = features;
3010
3011 /* Receive checksum has been changed */
3012 if (changed & NETIF_F_RXCSUM) {
3013 if (features & NETIF_F_RXCSUM)
3014 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3015 else
3016 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01003017 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08003018}
Jim Baxter4c09eed2013-04-19 08:10:49 +00003019
Nimrod Andy5bc26722014-10-13 10:53:48 +08003020static int fec_set_features(struct net_device *netdev,
3021 netdev_features_t features)
3022{
3023 struct fec_enet_private *fep = netdev_priv(netdev);
3024 netdev_features_t changed = features ^ netdev->features;
3025
Fabio Estevam5b40f702015-09-25 18:31:32 -03003026 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08003027 napi_disable(&fep->napi);
3028 netif_tx_lock_bh(netdev);
3029 fec_stop(netdev);
3030 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01003031 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003032 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01003033 netif_tx_unlock_bh(netdev);
3034 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08003035 } else {
3036 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00003037 }
3038
3039 return 0;
3040}
3041
Sascha Hauer009fda82009-04-15 01:32:23 +00003042static const struct net_device_ops fec_netdev_ops = {
3043 .ndo_open = fec_enet_open,
3044 .ndo_stop = fec_enet_close,
3045 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003046 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00003047 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00003048 .ndo_validate_addr = eth_validate_addr,
3049 .ndo_tx_timeout = fec_timeout,
3050 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01003051 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003052#ifdef CONFIG_NET_POLL_CONTROLLER
3053 .ndo_poll_controller = fec_poll_controller,
3054#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00003055 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00003056};
3057
Troy Kisky53bb20d2016-02-05 14:52:47 -07003058static const unsigned short offset_des_active_rxq[] = {
3059 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3060};
3061
3062static const unsigned short offset_des_active_txq[] = {
3063 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3064};
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 /*
3067 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00003068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003070static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003072 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00003073 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08003074 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08003075 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08003076 unsigned int i;
Troy Kisky7355f272016-02-05 14:52:46 -07003077 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3078 sizeof(struct bufdesc);
3079 unsigned dsize_log2 = __fls(dsize);
Nimrod Andy55d02182014-06-12 08:16:21 +08003080
Troy Kisky7355f272016-02-05 14:52:46 -07003081 WARN_ON(dsize != (1 << dsize_log2));
Fugang Duan41ef84c2014-09-13 05:00:54 +08003082#if defined(CONFIG_ARM)
3083 fep->rx_align = 0xf;
3084 fep->tx_align = 0xf;
3085#else
3086 fep->rx_align = 0x3;
3087 fep->tx_align = 0x3;
3088#endif
3089
Frank Li59d0f7462014-09-13 05:00:50 +08003090 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003091
Troy Kisky7355f272016-02-05 14:52:46 -07003092 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003094 /* Allocate memory for buffer descriptors. */
Lucas Stachc0a1a0a2015-07-23 16:06:19 +02003095 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3096 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003097 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003098 return -ENOMEM;
3099 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003100
Fugang Duan4d494cd2014-09-13 05:00:48 +08003101 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Shawn Guo49da97d2011-01-05 21:13:11 +00003103 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003104 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003105 /* make sure MAC we just acquired is programmed into the hw */
3106 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003108 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003109 for (i = 0; i < fep->num_rx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003110 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3111 unsigned size = dsize * rxq->bd.ring_size;
3112
3113 rxq->bd.qid = i;
3114 rxq->bd.base = cbd_base;
3115 rxq->bd.cur = cbd_base;
3116 rxq->bd.dma = bd_dma;
3117 rxq->bd.dsize = dsize;
3118 rxq->bd.dsize_log2 = dsize_log2;
Troy Kisky53bb20d2016-02-05 14:52:47 -07003119 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
Troy Kisky7355f272016-02-05 14:52:46 -07003120 bd_dma += size;
3121 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3122 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003123 }
3124
3125 for (i = 0; i < fep->num_tx_queues; i++) {
Troy Kisky7355f272016-02-05 14:52:46 -07003126 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3127 unsigned size = dsize * txq->bd.ring_size;
3128
3129 txq->bd.qid = i;
3130 txq->bd.base = cbd_base;
3131 txq->bd.cur = cbd_base;
3132 txq->bd.dma = bd_dma;
3133 txq->bd.dsize = dsize;
3134 txq->bd.dsize_log2 = dsize_log2;
Troy Kisky53bb20d2016-02-05 14:52:47 -07003135 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
Troy Kisky7355f272016-02-05 14:52:46 -07003136 bd_dma += size;
3137 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3138 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
Frank Li59d0f7462014-09-13 05:00:50 +08003139 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Sascha Hauer22f6b862009-04-15 01:32:18 +00003142 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003143 ndev->watchdog_timeo = TX_TIMEOUT;
3144 ndev->netdev_ops = &fec_netdev_ops;
3145 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003146
Frank Lidc975382013-01-28 18:31:42 +00003147 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003148 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003149
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003150 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003151 /* enable hw VLAN support */
3152 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003153
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003154 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003155 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3156
Shawn Guo48496252013-05-08 21:08:22 +00003157 /* enable hw accelerator */
3158 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003159 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003160 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3161 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003162
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003163 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003164 fep->tx_align = 0;
3165 fep->rx_align = 0x3f;
3166 }
3167
Nimrod Andy09d1e542014-06-12 08:16:20 +08003168 ndev->hw_features = ndev->features;
3169
Russell Kingef833372014-07-08 12:40:43 +01003170 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return 0;
3173}
3174
Shawn Guoca2cc332011-06-25 02:04:35 +08003175#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003176static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003177{
3178 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003179 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003180 struct device_node *np = pdev->dev.of_node;
3181
3182 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003183 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003184
Shawn Guoa3caad02012-06-27 03:45:24 +00003185 of_property_read_u32(np, "phy-reset-duration", &msec);
3186 /* A sane reset duration should not be longer than 1s */
3187 if (msec > 1000)
3188 msec = 1;
3189
Shawn Guoca2cc332011-06-25 02:04:35 +08003190 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003191 if (!gpio_is_valid(phy_reset))
3192 return;
3193
Shawn Guo119fc002012-06-27 03:45:22 +00003194 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3195 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003196 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003197 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003198 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003199 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003200 msleep(msec);
Fabio Estevamf4444572015-10-28 10:20:30 -02003201 gpio_set_value_cansleep(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003202}
3203#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003204static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003205{
3206 /*
3207 * In case of platform probe, the reset has been done
3208 * by machine code.
3209 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003210}
3211#endif /* CONFIG_OF */
3212
Fugang Duan9fc095f2014-09-13 05:00:49 +08003213static void
3214fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3215{
3216 struct device_node *np = pdev->dev.of_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003217
3218 *num_tx = *num_rx = 1;
3219
3220 if (!np || !of_device_is_available(np))
3221 return;
3222
3223 /* parse the num of tx and rx queues */
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303224 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003225
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303226 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003227
3228 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003229 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3230 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003231 *num_tx = 1;
3232 return;
3233 }
3234
3235 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003236 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3237 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003238 *num_rx = 1;
3239 return;
3240 }
3241
3242}
3243
Bill Pemberton33897cc2012-12-03 09:23:58 -05003244static int
Sascha Haueread73182009-01-28 23:03:11 +00003245fec_probe(struct platform_device *pdev)
3246{
3247 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003248 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003249 struct net_device *ndev;
3250 int i, irq, ret = 0;
3251 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003252 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003253 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003254 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003255 int num_tx_qs;
3256 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003257
Fugang Duan9fc095f2014-09-13 05:00:49 +08003258 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3259
Sascha Haueread73182009-01-28 23:03:11 +00003260 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003261 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3262 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003263 if (!ndev)
3264 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003265
3266 SET_NETDEV_DEV(ndev, &pdev->dev);
3267
3268 /* setup board info structure */
3269 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003270
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003271 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3272 if (of_id)
3273 pdev->id_entry = of_id->data;
3274 fep->quirks = pdev->id_entry->driver_data;
3275
Hubert Feurstein0c818592015-01-07 14:48:17 +01003276 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003277 fep->num_rx_queues = num_rx_qs;
3278 fep->num_tx_queues = num_tx_qs;
3279
Guenter Roeckd1391932013-06-18 10:04:59 -07003280#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003281 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003282 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003283 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003284#endif
Frank Libaa70a52013-01-16 16:55:58 +00003285
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003286 /* Select default pin state */
3287 pinctrl_pm_select_default_state(&pdev->dev);
3288
Fabio Estevam399db752013-07-21 13:25:03 -03003289 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303290 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3291 if (IS_ERR(fep->hwp)) {
3292 ret = PTR_ERR(fep->hwp);
3293 goto failed_ioremap;
3294 }
3295
Bryan Wue6b043d2010-03-31 02:10:44 +00003296 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003297 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003298
Sascha Haueread73182009-01-28 23:03:11 +00003299 platform_set_drvdata(pdev, ndev);
3300
Nimrod Andyde40ed32014-12-24 17:30:39 +08003301 if (of_get_property(np, "fsl,magic-packet", NULL))
3302 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3303
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003304 phy_node = of_parse_phandle(np, "phy-handle", 0);
3305 if (!phy_node && of_phy_is_fixed_link(np)) {
3306 ret = of_phy_register_fixed_link(np);
3307 if (ret < 0) {
3308 dev_err(&pdev->dev,
3309 "broken fixed-link specification\n");
3310 goto failed_phy;
3311 }
3312 phy_node = of_node_get(np);
3313 }
3314 fep->phy_node = phy_node;
3315
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003316 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003317 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003318 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003319 if (pdata)
3320 fep->phy_interface = pdata->phy;
3321 else
3322 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3323 } else {
3324 fep->phy_interface = ret;
3325 }
3326
Fabio Estevame2f8d552013-02-22 06:40:45 +00003327 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3328 if (IS_ERR(fep->clk_ipg)) {
3329 ret = PTR_ERR(fep->clk_ipg);
3330 goto failed_clk;
3331 }
3332
3333 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3334 if (IS_ERR(fep->clk_ahb)) {
3335 ret = PTR_ERR(fep->clk_ahb);
3336 goto failed_clk;
3337 }
3338
Fugang Duand851b472014-09-17 05:18:52 +08003339 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3340
Linus Torvalds38f56f32013-05-07 11:06:17 -07003341 /* enet_out is optional, depends on board */
3342 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3343 if (IS_ERR(fep->clk_enet_out))
3344 fep->clk_enet_out = NULL;
3345
Nimrod Andy91c0d982014-08-21 17:09:38 +08003346 fep->ptp_clk_on = false;
3347 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003348
3349 /* clk_ref is optional, depends on board */
3350 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3351 if (IS_ERR(fep->clk_ref))
3352 fep->clk_ref = NULL;
3353
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003354 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003355 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003356 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003357 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003358 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003359 }
3360
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003361 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003362 if (ret)
3363 goto failed_clk;
3364
Andrew Lunn8fff7552015-07-25 22:38:02 +02003365 ret = clk_prepare_enable(fep->clk_ipg);
3366 if (ret)
3367 goto failed_clk_ipg;
3368
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003369 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3370 if (!IS_ERR(fep->reg_phy)) {
3371 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003372 if (ret) {
3373 dev_err(&pdev->dev,
3374 "Failed to enable phy regulator: %d\n", ret);
3375 goto failed_regulator;
3376 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003377 } else {
3378 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003379 }
3380
Andrew Lunn8fff7552015-07-25 22:38:02 +02003381 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3382 pm_runtime_use_autosuspend(&pdev->dev);
Lucas Stach14d2b7c2015-08-03 17:50:11 +02003383 pm_runtime_get_noresume(&pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003384 pm_runtime_set_active(&pdev->dev);
3385 pm_runtime_enable(&pdev->dev);
3386
Fabio Estevame2f8d552013-02-22 06:40:45 +00003387 fec_reset_phy(pdev);
3388
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003389 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003390 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003391
3392 ret = fec_enet_init(ndev);
3393 if (ret)
3394 goto failed_init;
3395
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003396 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003397 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003398 if (irq < 0) {
3399 if (i)
3400 break;
3401 ret = irq;
3402 goto failed_irq;
3403 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003404 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003405 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003406 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003407 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003408
3409 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003410 }
3411
Fugang Duanb4d39b52014-09-13 05:00:55 +08003412 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003413 ret = fec_enet_mii_init(pdev);
3414 if (ret)
3415 goto failed_mii_init;
3416
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003417 /* Carrier starts down, phylib will bring it up */
3418 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003419 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003420 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003421
Sascha Haueread73182009-01-28 23:03:11 +00003422 ret = register_netdev(ndev);
3423 if (ret)
3424 goto failed_register;
3425
Nimrod Andyde40ed32014-12-24 17:30:39 +08003426 device_init_wakeup(&ndev->dev, fep->wol_flag &
3427 FEC_WOL_HAS_MAGIC_PACKET);
3428
Fabio Estevameb1d0642013-04-13 07:25:36 +00003429 if (fep->bufdesc_ex && fep->ptp_clock)
3430 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3431
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003432 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003433 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003434
3435 pm_runtime_mark_last_busy(&pdev->dev);
3436 pm_runtime_put_autosuspend(&pdev->dev);
3437
Sascha Haueread73182009-01-28 23:03:11 +00003438 return 0;
3439
3440failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003441 fec_enet_mii_remove(fep);
3442failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003443failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003444failed_init:
Lucas Stach32cba572015-07-23 16:06:20 +02003445 fec_ptp_stop(pdev);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003446 if (fep->reg_phy)
3447 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003448failed_regulator:
Andrew Lunn8fff7552015-07-25 22:38:02 +02003449 clk_disable_unprepare(fep->clk_ipg);
3450failed_clk_ipg:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003451 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003452failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003453failed_phy:
3454 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003455failed_ioremap:
3456 free_netdev(ndev);
3457
3458 return ret;
3459}
3460
Bill Pemberton33897cc2012-12-03 09:23:58 -05003461static int
Sascha Haueread73182009-01-28 23:03:11 +00003462fec_drv_remove(struct platform_device *pdev)
3463{
3464 struct net_device *ndev = platform_get_drvdata(pdev);
3465 struct fec_enet_private *fep = netdev_priv(ndev);
3466
Russell King36cdc742014-07-08 13:01:44 +01003467 cancel_work_sync(&fep->tx_timeout_work);
Lucas Stach32cba572015-07-23 16:06:20 +02003468 fec_ptp_stop(pdev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003469 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003470 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003471 if (fep->reg_phy)
3472 regulator_disable(fep->reg_phy);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003473 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003474 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003475
Sascha Haueread73182009-01-28 23:03:11 +00003476 return 0;
3477}
3478
Fabio Estevamdd66d382014-07-24 18:24:01 -03003479static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003480{
Eric Benard87cad5c2010-06-18 04:19:54 +00003481 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003482 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003483
Russell Kingda1774e2014-07-08 12:39:57 +01003484 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003485 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003486 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3487 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Russell Kingd76cfae2014-07-08 00:23:04 +01003488 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003489 napi_disable(&fep->napi);
3490 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003491 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003492 netif_tx_unlock_bh(ndev);
3493 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003494 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003495 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3496 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003497 }
Russell Kingda1774e2014-07-08 12:39:57 +01003498 rtnl_unlock();
3499
Nimrod Andyde40ed32014-12-24 17:30:39 +08003500 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003501 regulator_disable(fep->reg_phy);
3502
Nimrod Andy858eeb72014-12-11 09:20:31 +08003503 /* SOC supply clock to phy, when clock is disabled, phy link down
3504 * SOC control phy regulator, when regulator is disabled, phy link down
3505 */
3506 if (fep->clk_enet_out || fep->reg_phy)
3507 fep->link = 0;
3508
Sascha Haueread73182009-01-28 23:03:11 +00003509 return 0;
3510}
3511
Fabio Estevamdd66d382014-07-24 18:24:01 -03003512static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003513{
Eric Benard87cad5c2010-06-18 04:19:54 +00003514 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003515 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003516 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003517 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003518 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003519
Nimrod Andyde40ed32014-12-24 17:30:39 +08003520 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003521 ret = regulator_enable(fep->reg_phy);
3522 if (ret)
3523 return ret;
3524 }
Sascha Haueread73182009-01-28 23:03:11 +00003525
Russell Kingda1774e2014-07-08 12:39:57 +01003526 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003527 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003528 ret = fec_enet_clk_enable(ndev, true);
3529 if (ret) {
3530 rtnl_unlock();
3531 goto failed_clk;
3532 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003533 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3534 if (pdata && pdata->sleep_mode_enable)
3535 pdata->sleep_mode_enable(false);
3536 val = readl(fep->hwp + FEC_ECNTRL);
3537 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3538 writel(val, fep->hwp + FEC_ECNTRL);
3539 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3540 } else {
3541 pinctrl_pm_select_default_state(&fep->pdev->dev);
3542 }
Russell Kingef833372014-07-08 12:40:43 +01003543 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003544 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003545 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003546 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003547 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003548 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003549 }
Russell Kingda1774e2014-07-08 12:39:57 +01003550 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003551
Sascha Haueread73182009-01-28 23:03:11 +00003552 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003553
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003554failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003555 if (fep->reg_phy)
3556 regulator_disable(fep->reg_phy);
3557 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003558}
3559
Andrew Lunn8fff7552015-07-25 22:38:02 +02003560static int __maybe_unused fec_runtime_suspend(struct device *dev)
3561{
3562 struct net_device *ndev = dev_get_drvdata(dev);
3563 struct fec_enet_private *fep = netdev_priv(ndev);
3564
3565 clk_disable_unprepare(fep->clk_ipg);
3566
3567 return 0;
3568}
3569
3570static int __maybe_unused fec_runtime_resume(struct device *dev)
3571{
3572 struct net_device *ndev = dev_get_drvdata(dev);
3573 struct fec_enet_private *fep = netdev_priv(ndev);
3574
3575 return clk_prepare_enable(fep->clk_ipg);
3576}
3577
3578static const struct dev_pm_ops fec_pm_ops = {
3579 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3580 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3581};
Denis Kirjanov59d42892010-06-02 09:27:04 +00003582
Sascha Haueread73182009-01-28 23:03:11 +00003583static struct platform_driver fec_driver = {
3584 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003585 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003586 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003587 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003588 },
Shawn Guob5680e02011-01-05 21:13:13 +00003589 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003590 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003591 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003592};
3593
Fabio Estevamaaca2372012-01-23 16:44:50 +00003594module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003596MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597MODULE_LICENSE("GPL");