blob: 162fa59e3eae8bf4aec3cc3b6e6e7e229383fde5 [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) && \
220 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
221
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000222static int mii_cnt;
223
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800224static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800225struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
226 struct fec_enet_private *fep,
227 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000228{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800229 struct bufdesc *new_bd = bdp + 1;
230 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800231 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
232 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800233 struct bufdesc_ex *ex_base;
234 struct bufdesc *base;
235 int ring_size;
236
Fugang Duan4d494cd2014-09-13 05:00:48 +0800237 if (bdp >= txq->tx_bd_base) {
238 base = txq->tx_bd_base;
239 ring_size = txq->tx_ring_size;
240 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800241 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800242 base = rxq->rx_bd_base;
243 ring_size = rxq->rx_ring_size;
244 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800245 }
246
247 if (fep->bufdesc_ex)
248 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
249 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000250 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800251 return (new_bd >= (base + ring_size)) ?
252 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000253}
254
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800255static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800256struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
257 struct fec_enet_private *fep,
258 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000259{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800260 struct bufdesc *new_bd = bdp - 1;
261 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800262 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
263 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800264 struct bufdesc_ex *ex_base;
265 struct bufdesc *base;
266 int ring_size;
267
Fugang Duan4d494cd2014-09-13 05:00:48 +0800268 if (bdp >= txq->tx_bd_base) {
269 base = txq->tx_bd_base;
270 ring_size = txq->tx_ring_size;
271 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800272 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800273 base = rxq->rx_bd_base;
274 ring_size = rxq->rx_ring_size;
275 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800276 }
277
278 if (fep->bufdesc_ex)
279 return (struct bufdesc *)((ex_new_bd < ex_base) ?
280 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000281 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800282 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000283}
284
Nimrod Andy61a44272014-06-12 08:16:18 +0800285static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
286 struct fec_enet_private *fep)
287{
288 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
289}
290
Fugang Duan4d494cd2014-09-13 05:00:48 +0800291static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
292 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800293{
294 int entries;
295
Fugang Duan4d494cd2014-09-13 05:00:48 +0800296 entries = ((const char *)txq->dirty_tx -
297 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800298
Fugang Duan4d494cd2014-09-13 05:00:48 +0800299 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800300}
301
Lothar Waßmannc20e5992014-11-17 10:51:24 +0100302static void swap_buffer(void *bufaddr, int len)
Shawn Guob5680e02011-01-05 21:13:13 +0000303{
304 int i;
305 unsigned int *buf = bufaddr;
306
Lothar Waßmann7b487d02014-11-17 10:51:23 +0100307 for (i = 0; i < len; i += 4, buf++)
Lothar Waßmanne4537892014-11-17 10:51:22 +0100308 swab32s(buf);
Shawn Guob5680e02011-01-05 21:13:13 +0000309}
310
Lothar Waßmann1310b542014-11-07 10:02:47 +0100311static void swap_buffer2(void *dst_buf, void *src_buf, int len)
312{
313 int i;
314 unsigned int *src = src_buf;
315 unsigned int *dst = dst_buf;
316
317 for (i = 0; i < len; i += 4, src++, dst++)
318 *dst = swab32p(src);
319}
320
Russell King344756f2014-07-08 13:01:59 +0100321static void fec_dump(struct net_device *ndev)
322{
323 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800324 struct bufdesc *bdp;
325 struct fec_enet_priv_tx_q *txq;
326 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100327
328 netdev_info(ndev, "TX ring dump\n");
329 pr_info("Nr SC addr len SKB\n");
330
Fugang Duan4d494cd2014-09-13 05:00:48 +0800331 txq = fep->tx_queue[0];
332 bdp = txq->tx_bd_base;
333
Russell King344756f2014-07-08 13:01:59 +0100334 do {
Johannes Berg5cfa3032016-01-24 16:52:37 +0100335 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
Russell King344756f2014-07-08 13:01:59 +0100336 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800337 bdp == txq->cur_tx ? 'S' : ' ',
338 bdp == txq->dirty_tx ? 'H' : ' ',
Johannes Berg5cfa3032016-01-24 16:52:37 +0100339 fec16_to_cpu(bdp->cbd_sc),
340 fec32_to_cpu(bdp->cbd_bufaddr),
341 fec16_to_cpu(bdp->cbd_datlen),
Fugang Duan4d494cd2014-09-13 05:00:48 +0800342 txq->tx_skbuff[index]);
343 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100344 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800345 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100346}
347
Fugang Duan62a02c92014-06-18 08:33:52 +0800348static inline bool is_ipv4_pkt(struct sk_buff *skb)
349{
350 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
351}
352
Jim Baxter4c09eed2013-04-19 08:10:49 +0000353static int
354fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
355{
356 /* Only run for packets requiring a checksum. */
357 if (skb->ip_summed != CHECKSUM_PARTIAL)
358 return 0;
359
360 if (unlikely(skb_cow_head(skb, 0)))
361 return -1;
362
Fugang Duan62a02c92014-06-18 08:33:52 +0800363 if (is_ipv4_pkt(skb))
364 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000365 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
366
367 return 0;
368}
369
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800370static struct bufdesc *
Fugang Duan4d494cd2014-09-13 05:00:48 +0800371fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
372 struct sk_buff *skb,
373 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800374{
375 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800376 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800377 struct bufdesc_ex *ebdp;
378 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800379 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800380 int frag, frag_len;
381 unsigned short status;
382 unsigned int estatus = 0;
383 skb_frag_t *this_frag;
384 unsigned int index;
385 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100386 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800387 int i;
388
389 for (frag = 0; frag < nr_frags; frag++) {
390 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800391 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800392 ebdp = (struct bufdesc_ex *)bdp;
393
Johannes Berg5cfa3032016-01-24 16:52:37 +0100394 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800395 status &= ~BD_ENET_TX_STATS;
396 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
397 frag_len = skb_shinfo(skb)->frags[frag].size;
398
399 /* Handle the last BD specially */
400 if (frag == nr_frags - 1) {
401 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
402 if (fep->bufdesc_ex) {
403 estatus |= BD_ENET_TX_INT;
404 if (unlikely(skb_shinfo(skb)->tx_flags &
405 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
406 estatus |= BD_ENET_TX_TS;
407 }
408 }
409
410 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100411 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800412 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800413 if (skb->ip_summed == CHECKSUM_PARTIAL)
414 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
415 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100416 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800417 }
418
419 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
420
Fugang Duan4d494cd2014-09-13 05:00:48 +0800421 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800422 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100423 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800424 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
425 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800426
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100427 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800428 swap_buffer(bufaddr, frag_len);
429 }
430
Russell Kingd6bf3142014-07-08 00:23:19 +0100431 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
432 DMA_TO_DEVICE);
433 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800434 dev_kfree_skb_any(skb);
435 if (net_ratelimit())
436 netdev_err(ndev, "Tx DMA memory map failed\n");
437 goto dma_mapping_error;
438 }
439
Johannes Berg5cfa3032016-01-24 16:52:37 +0100440 bdp->cbd_bufaddr = cpu_to_fec32(addr);
441 bdp->cbd_datlen = cpu_to_fec16(frag_len);
442 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800443 }
444
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800445 return bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800446dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800447 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800448 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800449 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100450 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
451 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
Nimrod Andy6e909282014-06-12 08:16:22 +0800452 }
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800453 return ERR_PTR(-ENOMEM);
Nimrod Andy6e909282014-06-12 08:16:22 +0800454}
455
Fugang Duan4d494cd2014-09-13 05:00:48 +0800456static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
457 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800458{
459 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800460 int nr_frags = skb_shinfo(skb)->nr_frags;
461 struct bufdesc *bdp, *last_bdp;
462 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100463 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800464 unsigned short status;
465 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800466 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800467 unsigned int estatus = 0;
468 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800469 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800470
Fugang Duan4d494cd2014-09-13 05:00:48 +0800471 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800472 if (entries_free < MAX_SKB_FRAGS + 1) {
473 dev_kfree_skb_any(skb);
474 if (net_ratelimit())
475 netdev_err(ndev, "NOT enough BD for SG!\n");
476 return NETDEV_TX_OK;
477 }
478
Nimrod Andy6e909282014-06-12 08:16:22 +0800479 /* Protocol checksum off-load for TCP and UDP. */
480 if (fec_enet_clear_csum(skb, ndev)) {
481 dev_kfree_skb_any(skb);
482 return NETDEV_TX_OK;
483 }
484
485 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800486 bdp = txq->cur_tx;
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800487 last_bdp = bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100488 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy6e909282014-06-12 08:16:22 +0800489 status &= ~BD_ENET_TX_STATS;
490
491 /* Set buffer length and buffer pointer */
492 bufaddr = skb->data;
493 buflen = skb_headlen(skb);
494
Fugang Duan4d494cd2014-09-13 05:00:48 +0800495 queue = skb_get_queue_mapping(skb);
496 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800497 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100498 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800499 memcpy(txq->tx_bounce[index], skb->data, buflen);
500 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800501
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100502 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800503 swap_buffer(bufaddr, buflen);
504 }
505
Russell Kingd6bf3142014-07-08 00:23:19 +0100506 /* Push the data cache so the CPM does not get stale memory data. */
507 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
508 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800509 dev_kfree_skb_any(skb);
510 if (net_ratelimit())
511 netdev_err(ndev, "Tx DMA memory map failed\n");
512 return NETDEV_TX_OK;
513 }
514
515 if (nr_frags) {
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800516 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
517 if (IS_ERR(last_bdp))
518 return NETDEV_TX_OK;
Nimrod Andy6e909282014-06-12 08:16:22 +0800519 } else {
520 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
521 if (fep->bufdesc_ex) {
522 estatus = BD_ENET_TX_INT;
523 if (unlikely(skb_shinfo(skb)->tx_flags &
524 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
525 estatus |= BD_ENET_TX_TS;
526 }
527 }
528
529 if (fep->bufdesc_ex) {
530
531 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
532
533 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
534 fep->hwts_tx_en))
535 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
536
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100537 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800538 estatus |= FEC_TX_BD_FTYPE(queue);
539
Nimrod Andy6e909282014-06-12 08:16:22 +0800540 if (skb->ip_summed == CHECKSUM_PARTIAL)
541 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
542
543 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100544 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy6e909282014-06-12 08:16:22 +0800545 }
546
Fugang Duan4d494cd2014-09-13 05:00:48 +0800547 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800548 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800549 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800550
Johannes Berg5cfa3032016-01-24 16:52:37 +0100551 bdp->cbd_datlen = cpu_to_fec16(buflen);
552 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Nimrod Andy6e909282014-06-12 08:16:22 +0800553
554 /* Send it on its way. Tell FEC it's ready, interrupt when done,
555 * it's the last BD of the frame, and to put the CRC on the end.
556 */
557 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100558 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy6e909282014-06-12 08:16:22 +0800559
Sascha Hauer22f6b862009-04-15 01:32:18 +0000560 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800561 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800563 skb_tx_timestamp(skb);
564
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800565 /* Make sure the update to bdp and tx_skbuff are performed before
566 * cur_tx.
567 */
568 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +0800569 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Frank Lide5fb0a2013-03-03 17:34:25 +0000571 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800572 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000573
Nimrod Andy6e909282014-06-12 08:16:22 +0800574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Nimrod Andy79f33912014-06-12 08:16:23 +0800577static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800578fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
579 struct net_device *ndev,
580 struct bufdesc *bdp, int index, char *data,
581 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800582{
583 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200584 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800585 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800586 unsigned short status;
587 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100588 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800589
Johannes Berg5cfa3032016-01-24 16:52:37 +0100590 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800591 status &= ~BD_ENET_TX_STATS;
592
593 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800594
Fugang Duan41ef84c2014-09-13 05:00:54 +0800595 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100596 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800597 memcpy(txq->tx_bounce[index], data, size);
598 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800599
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100600 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800601 swap_buffer(data, size);
602 }
603
Russell Kingd6bf3142014-07-08 00:23:19 +0100604 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
605 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800606 dev_kfree_skb_any(skb);
607 if (net_ratelimit())
608 netdev_err(ndev, "Tx DMA memory map failed\n");
609 return NETDEV_TX_BUSY;
610 }
611
Johannes Berg5cfa3032016-01-24 16:52:37 +0100612 bdp->cbd_datlen = cpu_to_fec16(size);
613 bdp->cbd_bufaddr = cpu_to_fec32(addr);
Russell Kingd6bf3142014-07-08 00:23:19 +0100614
Nimrod Andy79f33912014-06-12 08:16:23 +0800615 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100616 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800617 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800618 if (skb->ip_summed == CHECKSUM_PARTIAL)
619 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
620 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100621 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800622 }
623
624 /* Handle the last BD specially */
625 if (last_tcp)
626 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
627 if (is_last) {
628 status |= BD_ENET_TX_INTR;
629 if (fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100630 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
Nimrod Andy79f33912014-06-12 08:16:23 +0800631 }
632
Johannes Berg5cfa3032016-01-24 16:52:37 +0100633 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800634
635 return 0;
636}
637
638static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800639fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
640 struct sk_buff *skb, struct net_device *ndev,
641 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800642{
643 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800644 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200645 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800646 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800647 void *bufaddr;
648 unsigned long dmabuf;
649 unsigned short status;
650 unsigned int estatus = 0;
651
Johannes Berg5cfa3032016-01-24 16:52:37 +0100652 status = fec16_to_cpu(bdp->cbd_sc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800653 status &= ~BD_ENET_TX_STATS;
654 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
655
Fugang Duan4d494cd2014-09-13 05:00:48 +0800656 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
657 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800658 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100659 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800660 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
661 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800662
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100663 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800664 swap_buffer(bufaddr, hdr_len);
665
666 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
667 hdr_len, DMA_TO_DEVICE);
668 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
669 dev_kfree_skb_any(skb);
670 if (net_ratelimit())
671 netdev_err(ndev, "Tx DMA memory map failed\n");
672 return NETDEV_TX_BUSY;
673 }
674 }
675
Johannes Berg5cfa3032016-01-24 16:52:37 +0100676 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
677 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
Nimrod Andy79f33912014-06-12 08:16:23 +0800678
679 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100680 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800681 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800682 if (skb->ip_summed == CHECKSUM_PARTIAL)
683 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
684 ebdp->cbd_bdu = 0;
Johannes Berg5cfa3032016-01-24 16:52:37 +0100685 ebdp->cbd_esc = cpu_to_fec32(estatus);
Nimrod Andy79f33912014-06-12 08:16:23 +0800686 }
687
Johannes Berg5cfa3032016-01-24 16:52:37 +0100688 bdp->cbd_sc = cpu_to_fec16(status);
Nimrod Andy79f33912014-06-12 08:16:23 +0800689
690 return 0;
691}
692
Fugang Duan4d494cd2014-09-13 05:00:48 +0800693static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
694 struct sk_buff *skb,
695 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800696{
697 struct fec_enet_private *fep = netdev_priv(ndev);
698 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
699 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800700 struct bufdesc *bdp = txq->cur_tx;
701 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800702 struct tso_t tso;
703 unsigned int index = 0;
704 int ret;
705
Fugang Duan4d494cd2014-09-13 05:00:48 +0800706 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800707 dev_kfree_skb_any(skb);
708 if (net_ratelimit())
709 netdev_err(ndev, "NOT enough BD for TSO!\n");
710 return NETDEV_TX_OK;
711 }
712
713 /* Protocol checksum off-load for TCP and UDP. */
714 if (fec_enet_clear_csum(skb, ndev)) {
715 dev_kfree_skb_any(skb);
716 return NETDEV_TX_OK;
717 }
718
719 /* Initialize the TSO handler, and prepare the first payload */
720 tso_start(skb, &tso);
721
722 total_len = skb->len - hdr_len;
723 while (total_len > 0) {
724 char *hdr;
725
Fugang Duan4d494cd2014-09-13 05:00:48 +0800726 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800727 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
728 total_len -= data_left;
729
730 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800731 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800732 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800733 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800734 if (ret)
735 goto err_release;
736
737 while (data_left > 0) {
738 int size;
739
740 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800741 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
742 index = fec_enet_get_bd_index(txq->tx_bd_base,
743 bdp, fep);
744 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
745 bdp, index,
746 tso.data, size,
747 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800748 total_len == 0);
749 if (ret)
750 goto err_release;
751
752 data_left -= size;
753 tso_build_data(skb, &tso, size);
754 }
755
Fugang Duan4d494cd2014-09-13 05:00:48 +0800756 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800757 }
758
759 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800760 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800761
Nimrod Andy79f33912014-06-12 08:16:23 +0800762 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800763 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800764
765 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100766 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Fugang Duan37d60172014-09-17 05:18:54 +0800767 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
768 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
769 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
770 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
771 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800772
773 return 0;
774
775err_release:
776 /* TODO: Release all used data descriptors for TSO */
777 return ret;
778}
779
Nimrod Andy61a44272014-06-12 08:16:18 +0800780static netdev_tx_t
781fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
782{
783 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800784 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800785 unsigned short queue;
786 struct fec_enet_priv_tx_q *txq;
787 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800788 int ret;
789
Fugang Duan4d494cd2014-09-13 05:00:48 +0800790 queue = skb_get_queue_mapping(skb);
791 txq = fep->tx_queue[queue];
792 nq = netdev_get_tx_queue(ndev, queue);
793
Nimrod Andy79f33912014-06-12 08:16:23 +0800794 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800795 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800796 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800797 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800798 if (ret)
799 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800800
Fugang Duan4d494cd2014-09-13 05:00:48 +0800801 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
802 if (entries_free <= txq->tx_stop_threshold)
803 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800804
805 return NETDEV_TX_OK;
806}
807
Frank Li14109a52013-03-26 16:12:03 +0000808/* Init RX & TX buffer descriptors
809 */
810static void fec_enet_bd_init(struct net_device *dev)
811{
812 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800813 struct fec_enet_priv_tx_q *txq;
814 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000815 struct bufdesc *bdp;
816 unsigned int i;
Frank Li59d0f742014-09-13 05:00:50 +0800817 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000818
Frank Li59d0f742014-09-13 05:00:50 +0800819 for (q = 0; q < fep->num_rx_queues; q++) {
820 /* Initialize the receive buffer descriptors. */
821 rxq = fep->rx_queue[q];
822 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800823
Frank Li59d0f742014-09-13 05:00:50 +0800824 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000825
Frank Li59d0f742014-09-13 05:00:50 +0800826 /* Initialize the BD for every fragment in the page. */
827 if (bdp->cbd_bufaddr)
Johannes Berg5cfa3032016-01-24 16:52:37 +0100828 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Li59d0f742014-09-13 05:00:50 +0800829 else
Johannes Berg5cfa3032016-01-24 16:52:37 +0100830 bdp->cbd_sc = cpu_to_fec16(0);
Frank Li59d0f742014-09-13 05:00:50 +0800831 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000832 }
Frank Li59d0f742014-09-13 05:00:50 +0800833
834 /* Set the last buffer to wrap */
835 bdp = fec_enet_get_prevdesc(bdp, fep, q);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100836 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f742014-09-13 05:00:50 +0800837
838 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000839 }
840
Frank Li59d0f742014-09-13 05:00:50 +0800841 for (q = 0; q < fep->num_tx_queues; q++) {
842 /* ...and the same for transmit */
843 txq = fep->tx_queue[q];
844 bdp = txq->tx_bd_base;
845 txq->cur_tx = bdp;
846
847 for (i = 0; i < txq->tx_ring_size; i++) {
848 /* Initialize the BD for every fragment in the page. */
Johannes Berg5cfa3032016-01-24 16:52:37 +0100849 bdp->cbd_sc = cpu_to_fec16(0);
Frank Li59d0f742014-09-13 05:00:50 +0800850 if (txq->tx_skbuff[i]) {
851 dev_kfree_skb_any(txq->tx_skbuff[i]);
852 txq->tx_skbuff[i] = NULL;
853 }
Johannes Berg5cfa3032016-01-24 16:52:37 +0100854 bdp->cbd_bufaddr = cpu_to_fec32(0);
Frank Li59d0f742014-09-13 05:00:50 +0800855 bdp = fec_enet_get_nextdesc(bdp, fep, q);
856 }
857
858 /* Set the last buffer to wrap */
859 bdp = fec_enet_get_prevdesc(bdp, fep, q);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100860 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f742014-09-13 05:00:50 +0800861 txq->dirty_tx = bdp;
862 }
863}
864
Frank Lice99d0d2014-09-13 05:00:52 +0800865static void fec_enet_active_rxring(struct net_device *ndev)
866{
867 struct fec_enet_private *fep = netdev_priv(ndev);
868 int i;
869
870 for (i = 0; i < fep->num_rx_queues; i++)
871 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
872}
873
Frank Li59d0f742014-09-13 05:00:50 +0800874static void fec_enet_enable_ring(struct net_device *ndev)
875{
876 struct fec_enet_private *fep = netdev_priv(ndev);
877 struct fec_enet_priv_tx_q *txq;
878 struct fec_enet_priv_rx_q *rxq;
879 int i;
880
881 for (i = 0; i < fep->num_rx_queues; i++) {
882 rxq = fep->rx_queue[i];
883 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
Nimrod Andyd543a762014-11-23 17:23:06 +0800884 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f742014-09-13 05:00:50 +0800885
886 /* enable DMA1/2 */
887 if (i)
888 writel(RCMR_MATCHEN | RCMR_CMP(i),
889 fep->hwp + FEC_RCMR(i));
890 }
891
892 for (i = 0; i < fep->num_tx_queues; i++) {
893 txq = fep->tx_queue[i];
894 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
895
896 /* enable DMA1/2 */
897 if (i)
898 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
899 fep->hwp + FEC_DMA_CFG(i));
900 }
901}
902
903static void fec_enet_reset_skb(struct net_device *ndev)
904{
905 struct fec_enet_private *fep = netdev_priv(ndev);
906 struct fec_enet_priv_tx_q *txq;
907 int i, j;
908
909 for (i = 0; i < fep->num_tx_queues; i++) {
910 txq = fep->tx_queue[i];
911
912 for (j = 0; j < txq->tx_ring_size; j++) {
913 if (txq->tx_skbuff[j]) {
914 dev_kfree_skb_any(txq->tx_skbuff[j]);
915 txq->tx_skbuff[j] = NULL;
916 }
917 }
918 }
Frank Li14109a52013-03-26 16:12:03 +0000919}
920
Russell Kingdbc64a82014-07-08 12:40:12 +0100921/*
922 * This function is called to start or restart the FEC during a link
923 * change, transmit timeout, or to reconfigure the FEC. The network
924 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100925 */
926static void
Russell Kingef833372014-07-08 12:40:43 +0100927fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100928{
929 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000930 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100931 u32 temp_mac[2];
932 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000933 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100934
Fugang Duan106c3142014-09-13 05:00:51 +0800935 /* Whack a reset. We should wait for this.
936 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
937 * instead of reset MAC itself.
938 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100939 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800940 writel(0, fep->hwp + FEC_ECNTRL);
941 } else {
942 writel(1, fep->hwp + FEC_ECNTRL);
943 udelay(10);
944 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100945
946 /*
947 * enet-mac reset will reset mac address registers too,
948 * so need to reconfigure it.
949 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100950 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100951 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
Johannes Berg5cfa3032016-01-24 16:52:37 +0100952 writel((__force u32)cpu_to_be32(temp_mac[0]),
953 fep->hwp + FEC_ADDR_LOW);
954 writel((__force u32)cpu_to_be32(temp_mac[1]),
955 fep->hwp + FEC_ADDR_HIGH);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100956 }
957
958 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800959 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100960
Frank Li14109a52013-03-26 16:12:03 +0000961 fec_enet_bd_init(ndev);
962
Frank Li59d0f742014-09-13 05:00:50 +0800963 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100964
Frank Li59d0f742014-09-13 05:00:50 +0800965 /* Reset tx SKB buffers. */
966 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100967
968 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100969 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100970 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100971 writel(0x04, fep->hwp + FEC_X_CNTRL);
972 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100973 /* No Rcv on Xmit */
974 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975 writel(0x0, fep->hwp + FEC_X_CNTRL);
976 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100977
Uwe Kleine-König45993652011-01-19 20:47:04 +0100978 /* Set MII speed */
979 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
980
Guenter Roeckd1391932013-06-18 10:04:59 -0700981#if !defined(CONFIG_M5272)
Greg Ungerer18803492015-06-20 15:51:57 +1000982 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
983 /* set RX checksum */
984 val = readl(fep->hwp + FEC_RACC);
985 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
986 val |= FEC_RACC_OPTIONS;
987 else
988 val &= ~FEC_RACC_OPTIONS;
989 writel(val, fep->hwp + FEC_RACC);
990 }
Troy Kisky55cd48c2016-02-05 14:52:43 -0700991 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
Guenter Roeckd1391932013-06-18 10:04:59 -0700992#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000993
Uwe Kleine-König45993652011-01-19 20:47:04 +0100994 /*
995 * The phy interface and speed need to get configured
996 * differently on enet-mac.
997 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100998 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100999 /* Enable flow control and length check */
1000 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001001
Shawn Guo230dec62011-09-23 02:12:48 +00001002 /* RGMII, RMII or MII */
Markus Pargmanne813bb22015-04-30 17:07:50 +02001003 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1004 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1005 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1006 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shawn Guo230dec62011-09-23 02:12:48 +00001007 rcntl |= (1 << 6);
1008 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001009 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001010 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001011 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001012
Shawn Guo230dec62011-09-23 02:12:48 +00001013 /* 1G, 100M or 10M */
1014 if (fep->phy_dev) {
1015 if (fep->phy_dev->speed == SPEED_1000)
1016 ecntl |= (1 << 5);
1017 else if (fep->phy_dev->speed == SPEED_100)
1018 rcntl &= ~(1 << 9);
1019 else
1020 rcntl |= (1 << 9);
1021 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001022 } else {
1023#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001024 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001025 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001026 /* disable the gasket and wait */
1027 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1028 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1029 udelay(1);
1030
1031 /*
1032 * configure the gasket:
1033 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001034 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001035 */
Eric Benard8d82f212012-01-12 06:10:28 +00001036 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1037 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1038 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1039 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1040 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001041
1042 /* re-enable the gasket */
1043 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1044 }
1045#endif
1046 }
Frank Libaa70a52013-01-16 16:55:58 +00001047
Guenter Roeckd1391932013-06-18 10:04:59 -07001048#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001049 /* enable pause frame*/
1050 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1051 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1052 fep->phy_dev && fep->phy_dev->pause)) {
1053 rcntl |= FEC_ENET_FCE;
1054
Jim Baxter4c09eed2013-04-19 08:10:49 +00001055 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001056 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1057 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1058 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1059 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1060
1061 /* OPD */
1062 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1063 } else {
1064 rcntl &= ~FEC_ENET_FCE;
1065 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001066#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001067
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001068 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001069
Stefan Wahren84fe6182014-03-12 11:28:19 +01001070 /* Setup multicast filter. */
1071 set_multicast_list(ndev);
1072#ifndef CONFIG_M5272
1073 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1074 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1075#endif
1076
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001077 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001078 /* enable ENET endian swap */
1079 ecntl |= (1 << 8);
1080 /* enable ENET store and forward mode */
1081 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1082 }
1083
Frank Liff43da82013-01-03 16:04:23 +00001084 if (fep->bufdesc_ex)
1085 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001086
Chris Healy38ae92d2013-06-25 23:18:52 -07001087#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001088 /* Enable the MIB statistic event counters */
1089 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001090#endif
1091
Uwe Kleine-König45993652011-01-19 20:47:04 +01001092 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001093 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001094 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001095
Frank Liff43da82013-01-03 16:04:23 +00001096 if (fep->bufdesc_ex)
1097 fec_ptp_start_cyclecounter(ndev);
1098
Uwe Kleine-König45993652011-01-19 20:47:04 +01001099 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001100 if (fep->link)
1101 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1102 else
1103 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001104
1105 /* Init the interrupt coalescing */
1106 fec_enet_itr_coal_init(ndev);
1107
Uwe Kleine-König45993652011-01-19 20:47:04 +01001108}
1109
1110static void
1111fec_stop(struct net_device *ndev)
1112{
1113 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001114 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001115 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001116 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001117
1118 /* We cannot expect a graceful transmit stop without link !!! */
1119 if (fep->link) {
1120 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1121 udelay(10);
1122 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001123 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001124 }
1125
Fugang Duan106c3142014-09-13 05:00:51 +08001126 /* Whack a reset. We should wait for this.
1127 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1128 * instead of reset MAC itself.
1129 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001130 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1131 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1132 writel(0, fep->hwp + FEC_ECNTRL);
1133 } else {
1134 writel(1, fep->hwp + FEC_ECNTRL);
1135 udelay(10);
1136 }
1137 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001138 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001139 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1140 val = readl(fep->hwp + FEC_ECNTRL);
1141 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1142 writel(val, fep->hwp + FEC_ECNTRL);
1143
1144 if (pdata && pdata->sleep_mode_enable)
1145 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001146 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001147 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001148
1149 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001150 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1151 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001152 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001153 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1154 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001155}
1156
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001159fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001161 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Russell King344756f2014-07-08 13:01:59 +01001163 fec_dump(ndev);
1164
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001165 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Russell King36cdc742014-07-08 13:01:44 +01001167 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001168}
1169
Russell King36cdc742014-07-08 13:01:44 +01001170static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001171{
1172 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001173 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001174 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001175
Russell King36cdc742014-07-08 13:01:44 +01001176 rtnl_lock();
1177 if (netif_device_present(ndev) || netif_running(ndev)) {
1178 napi_disable(&fep->napi);
1179 netif_tx_lock_bh(ndev);
1180 fec_restart(ndev);
1181 netif_wake_queue(ndev);
1182 netif_tx_unlock_bh(ndev);
1183 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001184 }
Russell King36cdc742014-07-08 13:01:44 +01001185 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001189fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1190 struct skb_shared_hwtstamps *hwtstamps)
1191{
1192 unsigned long flags;
1193 u64 ns;
1194
1195 spin_lock_irqsave(&fep->tmreg_lock, flags);
1196 ns = timecounter_cyc2time(&fep->tc, ts);
1197 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1198
1199 memset(hwtstamps, 0, sizeof(*hwtstamps));
1200 hwtstamps->hwtstamp = ns_to_ktime(ns);
1201}
1202
1203static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001204fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct fec_enet_private *fep;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001207 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001208 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001210 struct fec_enet_priv_tx_q *txq;
1211 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001212 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001213 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001215 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001216
1217 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1218
1219 txq = fep->tx_queue[queue_id];
1220 /* get next bdp of dirty_tx */
1221 nq = netdev_get_tx_queue(ndev, queue_id);
1222 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Frank Lide5fb0a2013-03-03 17:34:25 +00001224 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001225 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001226
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001227 while (bdp != READ_ONCE(txq->cur_tx)) {
1228 /* Order the load of cur_tx and cbd_sc */
1229 rmb();
Johannes Berg5cfa3032016-01-24 16:52:37 +01001230 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001231 if (status & BD_ENET_TX_READY)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001232 break;
1233
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001234 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy2b995f62015-01-20 14:10:35 +08001235
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001236 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001237 txq->tx_skbuff[index] = NULL;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001238 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1239 dma_unmap_single(&fep->pdev->dev,
1240 fec32_to_cpu(bdp->cbd_bufaddr),
1241 fec16_to_cpu(bdp->cbd_datlen),
1242 DMA_TO_DEVICE);
1243 bdp->cbd_bufaddr = cpu_to_fec32(0);
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001244 if (!skb) {
1245 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1246 continue;
1247 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001250 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 BD_ENET_TX_RL | BD_ENET_TX_UN |
1252 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001253 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001254 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001255 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001256 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001257 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001258 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001259 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001260 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001261 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001262 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001263 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001265 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001266 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
Frank Liff43da82013-01-03 16:04:23 +00001269 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1270 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001271 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001272 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001273
Johannes Berg5cfa3032016-01-24 16:52:37 +01001274 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001275 skb_tstamp_tx(skb, &shhwtstamps);
1276 }
Frank Liff43da82013-01-03 16:04:23 +00001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* Deferred means some collisions occurred during transmit,
1279 * but we eventually sent the packet OK.
1280 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001281 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001282 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001283
Sascha Hauer22f6b862009-04-15 01:32:18 +00001284 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001286
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001287 /* Make sure the update to bdp and tx_skbuff are performed
1288 * before dirty_tx
1289 */
1290 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +08001291 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001292
Sascha Hauer22f6b862009-04-15 01:32:18 +00001293 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001294 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001295
Sascha Hauer22f6b862009-04-15 01:32:18 +00001296 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001298 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001299 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1300 if (entries_free >= txq->tx_wake_threshold)
1301 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Russell Kingccea2962014-07-08 13:01:38 +01001304
1305 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001306 if (bdp != txq->cur_tx &&
1307 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1308 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1309}
1310
1311static void
1312fec_enet_tx(struct net_device *ndev)
1313{
1314 struct fec_enet_private *fep = netdev_priv(ndev);
1315 u16 queue_id;
1316 /* First process class A queue, then Class B and Best Effort queue */
1317 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1318 clear_bit(queue_id, &fep->work_tx);
1319 fec_enet_tx_queue(ndev, queue_id);
1320 }
1321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001324static int
1325fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1326{
1327 struct fec_enet_private *fep = netdev_priv(ndev);
1328 int off;
1329
1330 off = ((unsigned long)skb->data) & fep->rx_align;
1331 if (off)
1332 skb_reserve(skb, fep->rx_align + 1 - off);
1333
Johannes Berg5cfa3032016-01-24 16:52:37 +01001334 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1335 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001336 if (net_ratelimit())
1337 netdev_err(ndev, "Rx DMA memory map failed\n");
1338 return -ENOMEM;
1339 }
1340
1341 return 0;
1342}
1343
1344static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001345 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001346{
1347 struct fec_enet_private *fep = netdev_priv(ndev);
1348 struct sk_buff *new_skb;
1349
1350 if (length > fep->rx_copybreak)
1351 return false;
1352
1353 new_skb = netdev_alloc_skb(ndev, length);
1354 if (!new_skb)
1355 return false;
1356
Johannes Berg5cfa3032016-01-24 16:52:37 +01001357 dma_sync_single_for_cpu(&fep->pdev->dev,
1358 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001359 FEC_ENET_RX_FRSIZE - fep->rx_align,
1360 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001361 if (!swap)
1362 memcpy(new_skb->data, (*skb)->data, length);
1363 else
1364 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001365 *skb = new_skb;
1366
1367 return true;
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/* During a receive, the cur_rx points to the current incoming buffer.
1371 * When we update through the ring, if the next incoming buffer has
1372 * not been given to the system, we just set the empty indicator,
1373 * effectively tossing the packet.
1374 */
Frank Lidc975382013-01-28 18:31:42 +00001375static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001376fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001378 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001379 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001380 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001381 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001382 struct sk_buff *skb_new = NULL;
1383 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 ushort pkt_len;
1385 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001386 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001387 struct bufdesc_ex *ebdp = NULL;
1388 bool vlan_packet_rcvd = false;
1389 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001390 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001391 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001392 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001393
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001394#ifdef CONFIG_M532x
1395 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001396#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001397 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1398 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* First, grab all of the stats for the incoming packet.
1401 * These get messed up if we get called due to a busy condition.
1402 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001403 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Johannes Berg5cfa3032016-01-24 16:52:37 +01001405 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Frank Lidc975382013-01-28 18:31:42 +00001407 if (pkt_received >= budget)
1408 break;
1409 pkt_received++;
1410
Russell Kinged63f1d2015-09-02 17:24:14 +08001411 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
Russell Kingdb3421c2014-07-08 13:01:49 +01001412
Sascha Hauer22f6b862009-04-15 01:32:18 +00001413 /* Check for errors. */
Troy Kisky095098e2016-02-05 14:52:44 -07001414 status ^= BD_ENET_RX_LAST;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001415 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Troy Kisky095098e2016-02-05 14:52:44 -07001416 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1417 BD_ENET_RX_CL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001418 ndev->stats.rx_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001419 if (status & BD_ENET_RX_OV) {
1420 /* FIFO overrun */
1421 ndev->stats.rx_fifo_errors++;
1422 goto rx_processing_done;
1423 }
1424 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1425 | BD_ENET_RX_LAST)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +00001426 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001427 ndev->stats.rx_length_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001428 if (status & BD_ENET_RX_LAST)
1429 netdev_err(ndev, "rcv is not +last\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001430 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001431 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001432 ndev->stats.rx_crc_errors++;
Troy Kisky095098e2016-02-05 14:52:44 -07001433 /* Report late collisions as a frame error. */
1434 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1435 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001436 goto rx_processing_done;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Sascha Hauer22f6b862009-04-15 01:32:18 +00001439 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001440 ndev->stats.rx_packets++;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001441 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001442 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Fugang Duan4d494cd2014-09-13 05:00:48 +08001444 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001445 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001446
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001447 /* The packet length includes FCS, but we don't want to
1448 * include that when passing upstream as it messes up
1449 * bridging applications.
1450 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001451 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1452 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001453 if (!is_copybreak) {
1454 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1455 if (unlikely(!skb_new)) {
1456 ndev->stats.rx_dropped++;
1457 goto rx_processing_done;
1458 }
Johannes Berg5cfa3032016-01-24 16:52:37 +01001459 dma_unmap_single(&fep->pdev->dev,
1460 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001461 FEC_ENET_RX_FRSIZE - fep->rx_align,
1462 DMA_FROM_DEVICE);
1463 }
1464
1465 prefetch(skb->data - NET_IP_ALIGN);
1466 skb_put(skb, pkt_len - 4);
1467 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001468 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001469 swap_buffer(data, pkt_len);
1470
Jim Baxtercdffcf12013-07-02 22:52:56 +01001471 /* Extract the enhanced buffer descriptor */
1472 ebdp = NULL;
1473 if (fep->bufdesc_ex)
1474 ebdp = (struct bufdesc_ex *)bdp;
1475
1476 /* If this is a VLAN packet remove the VLAN Tag */
1477 vlan_packet_rcvd = false;
1478 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Johannes Berg5cfa3032016-01-24 16:52:37 +01001479 fep->bufdesc_ex &&
1480 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001481 /* Push and remove the vlan tag */
1482 struct vlan_hdr *vlan_header =
1483 (struct vlan_hdr *) (data + ETH_HLEN);
1484 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001485
1486 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001487
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001488 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001489 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001490 }
1491
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001492 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001494 /* Get receive timestamp from the skb */
1495 if (fep->hwts_rx_en && fep->bufdesc_ex)
Johannes Berg5cfa3032016-01-24 16:52:37 +01001496 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001497 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001498
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001499 if (fep->bufdesc_ex &&
1500 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001501 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001502 /* don't check it */
1503 skb->ip_summed = CHECKSUM_UNNECESSARY;
1504 } else {
1505 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001506 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001507 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001508
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001509 /* Handle received VLAN packets */
1510 if (vlan_packet_rcvd)
1511 __vlan_hwaccel_put_tag(skb,
1512 htons(ETH_P_8021Q),
1513 vlan_tag);
1514
1515 napi_gro_receive(&fep->napi, skb);
1516
1517 if (is_copybreak) {
Johannes Berg5cfa3032016-01-24 16:52:37 +01001518 dma_sync_single_for_device(&fep->pdev->dev,
1519 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001520 FEC_ENET_RX_FRSIZE - fep->rx_align,
1521 DMA_FROM_DEVICE);
1522 } else {
1523 rxq->rx_skbuff[index] = skb_new;
1524 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1525 }
1526
Sascha Hauer22f6b862009-04-15 01:32:18 +00001527rx_processing_done:
1528 /* Clear the status flags for this buffer */
1529 status &= ~BD_ENET_RX_STATS;
1530
1531 /* Mark the buffer empty */
1532 status |= BD_ENET_RX_EMPTY;
Johannes Berg5cfa3032016-01-24 16:52:37 +01001533 bdp->cbd_sc = cpu_to_fec16(status);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001534
Frank Liff43da82013-01-03 16:04:23 +00001535 if (fep->bufdesc_ex) {
1536 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1537
Johannes Berg5cfa3032016-01-24 16:52:37 +01001538 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00001539 ebdp->cbd_prot = 0;
1540 ebdp->cbd_bdu = 0;
1541 }
Frank Li6605b732012-10-30 18:25:31 +00001542
Sascha Hauer22f6b862009-04-15 01:32:18 +00001543 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001544 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001545
Sascha Hauer22f6b862009-04-15 01:32:18 +00001546 /* Doing this here will keep the FEC running while we process
1547 * incoming frames. On a heavily loaded network, we should be
1548 * able to keep up at the expense of system resources.
1549 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001550 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001552 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001553 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Fugang Duan4d494cd2014-09-13 05:00:48 +08001556static int
1557fec_enet_rx(struct net_device *ndev, int budget)
1558{
1559 int pkt_received = 0;
1560 u16 queue_id;
1561 struct fec_enet_private *fep = netdev_priv(ndev);
1562
1563 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1564 clear_bit(queue_id, &fep->work_rx);
1565 pkt_received += fec_enet_rx_queue(ndev,
1566 budget - pkt_received, queue_id);
1567 }
1568 return pkt_received;
1569}
1570
1571static bool
1572fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1573{
1574 if (int_events == 0)
1575 return false;
1576
1577 if (int_events & FEC_ENET_RXF)
1578 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001579 if (int_events & FEC_ENET_RXF_1)
1580 fep->work_rx |= (1 << 0);
1581 if (int_events & FEC_ENET_RXF_2)
1582 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001583
1584 if (int_events & FEC_ENET_TXF)
1585 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001586 if (int_events & FEC_ENET_TXF_1)
1587 fep->work_tx |= (1 << 0);
1588 if (int_events & FEC_ENET_TXF_2)
1589 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001590
1591 return true;
1592}
1593
Uwe Kleine-König45993652011-01-19 20:47:04 +01001594static irqreturn_t
1595fec_enet_interrupt(int irq, void *dev_id)
1596{
1597 struct net_device *ndev = dev_id;
1598 struct fec_enet_private *fep = netdev_priv(ndev);
1599 uint int_events;
1600 irqreturn_t ret = IRQ_NONE;
1601
Russell King7a168072014-07-08 00:22:44 +01001602 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001603 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001604 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001605
Fugang Duan61615cd2015-03-04 07:52:11 +08001606 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001607 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001608
Nimrod Andy94191fd2014-12-16 18:25:58 +08001609 if (napi_schedule_prep(&fep->napi)) {
1610 /* Disable the NAPI interrupts */
1611 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1612 __napi_schedule(&fep->napi);
1613 }
Russell King7a168072014-07-08 00:22:44 +01001614 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001615
Russell King7a168072014-07-08 00:22:44 +01001616 if (int_events & FEC_ENET_MII) {
1617 ret = IRQ_HANDLED;
1618 complete(&fep->mdio_done);
1619 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001620
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001621 if (fep->ptp_clock)
1622 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001623
Uwe Kleine-König45993652011-01-19 20:47:04 +01001624 return ret;
1625}
1626
Frank Lidc975382013-01-28 18:31:42 +00001627static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1628{
1629 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001630 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001631 int pkts;
1632
Russell King7a168072014-07-08 00:22:44 +01001633 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001634
Frank Lide5fb0a2013-03-03 17:34:25 +00001635 fec_enet_tx(ndev);
1636
Frank Lidc975382013-01-28 18:31:42 +00001637 if (pkts < budget) {
1638 napi_complete(napi);
1639 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1640 }
1641 return pkts;
1642}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001645static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001647 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001648 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001649 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Shawn Guo49da97d2011-01-05 21:13:11 +00001651 /*
1652 * try to get mac address in following order:
1653 *
1654 * 1) module parameter via kernel command line in form
1655 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1656 */
1657 iap = macaddr;
1658
1659 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001660 * 2) from device tree data
1661 */
1662 if (!is_valid_ether_addr(iap)) {
1663 struct device_node *np = fep->pdev->dev.of_node;
1664 if (np) {
1665 const char *mac = of_get_mac_address(np);
1666 if (mac)
1667 iap = (unsigned char *) mac;
1668 }
1669 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001670
1671 /*
1672 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001673 */
1674 if (!is_valid_ether_addr(iap)) {
1675#ifdef CONFIG_M5272
1676 if (FEC_FLASHMAC)
1677 iap = (unsigned char *)FEC_FLASHMAC;
1678#else
1679 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001680 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001681#endif
1682 }
1683
1684 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001685 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001686 */
1687 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001688 *((__be32 *) &tmpaddr[0]) =
1689 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1690 *((__be16 *) &tmpaddr[4]) =
1691 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 iap = &tmpaddr[0];
1693 }
1694
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001695 /*
1696 * 5) random mac address
1697 */
1698 if (!is_valid_ether_addr(iap)) {
1699 /* Report it and use a random ethernet address instead */
1700 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1701 eth_hw_addr_random(ndev);
1702 netdev_info(ndev, "Using random MAC address: %pM\n",
1703 ndev->dev_addr);
1704 return;
1705 }
1706
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001707 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Shawn Guo49da97d2011-01-05 21:13:11 +00001709 /* Adjust MAC if using macaddr */
1710 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001711 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714/* ------------------------------------------------------------------------- */
1715
Bryan Wue6b043d2010-03-31 02:10:44 +00001716/*
1717 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001719static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001721 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001722 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001723 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Bryan Wue6b043d2010-03-31 02:10:44 +00001725 /* Prevent a state halted on mii error */
1726 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1727 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001728 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001730
Russell King8ce56242014-07-08 12:40:07 +01001731 /*
1732 * If the netdev is down, or is going down, we're not interested
1733 * in link state events, so just mark our idea of the link as down
1734 * and ignore the event.
1735 */
1736 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1737 fep->link = 0;
1738 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001739 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001740 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001741 status_change = 1;
1742 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001743
Russell Kingef833372014-07-08 12:40:43 +01001744 if (fep->full_duplex != phy_dev->duplex) {
1745 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001746 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001747 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001748
1749 if (phy_dev->speed != fep->speed) {
1750 fep->speed = phy_dev->speed;
1751 status_change = 1;
1752 }
1753
1754 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001755 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001756 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001757 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001758 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001759 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001760 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001761 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001762 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001763 } else {
1764 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001765 napi_disable(&fep->napi);
1766 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001767 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001768 netif_tx_unlock_bh(ndev);
1769 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001770 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001771 status_change = 1;
1772 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001773 }
1774
Bryan Wue6b043d2010-03-31 02:10:44 +00001775 if (status_change)
1776 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Bryan Wue6b043d2010-03-31 02:10:44 +00001779static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Bryan Wue6b043d2010-03-31 02:10:44 +00001781 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001782 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001783 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001784 int ret = 0;
1785
1786 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001787 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001788 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001789
1790 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001791 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001792
1793 /* start a read op */
1794 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1795 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1796 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1797
1798 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001799 time_left = wait_for_completion_timeout(&fep->mdio_done,
1800 usecs_to_jiffies(FEC_MII_TIMEOUT));
1801 if (time_left == 0) {
1802 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001803 netdev_err(fep->netdev, "MDIO read timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001804 ret = -ETIMEDOUT;
1805 goto out;
Bryan Wue6b043d2010-03-31 02:10:44 +00001806 }
1807
Andrew Lunn8fff7552015-07-25 22:38:02 +02001808 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1809
1810out:
1811 pm_runtime_mark_last_busy(dev);
1812 pm_runtime_put_autosuspend(dev);
1813
1814 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001815}
1816
1817static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1818 u16 value)
1819{
1820 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001821 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001822 unsigned long time_left;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001823 int ret;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001824
1825 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001826 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001827 return ret;
Maciej S. Szmigiero42ea4452015-09-03 21:38:30 +02001828 else
1829 ret = 0;
Bryan Wue6b043d2010-03-31 02:10:44 +00001830
1831 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001832 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001833
Shawn Guo862f0982011-01-05 21:13:09 +00001834 /* start a write op */
1835 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001836 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1837 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1838 fep->hwp + FEC_MII_DATA);
1839
1840 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001841 time_left = wait_for_completion_timeout(&fep->mdio_done,
1842 usecs_to_jiffies(FEC_MII_TIMEOUT));
1843 if (time_left == 0) {
1844 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001845 netdev_err(fep->netdev, "MDIO write timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001846 ret = -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001847 }
1848
Andrew Lunn8fff7552015-07-25 22:38:02 +02001849 pm_runtime_mark_last_busy(dev);
1850 pm_runtime_put_autosuspend(dev);
1851
1852 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001853}
1854
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001855static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1856{
1857 struct fec_enet_private *fep = netdev_priv(ndev);
1858 int ret;
1859
1860 if (enable) {
1861 ret = clk_prepare_enable(fep->clk_ahb);
1862 if (ret)
1863 return ret;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001864 if (fep->clk_enet_out) {
1865 ret = clk_prepare_enable(fep->clk_enet_out);
1866 if (ret)
1867 goto failed_clk_enet_out;
1868 }
1869 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001870 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001871 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001872 if (ret) {
1873 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001874 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001875 } else {
1876 fep->ptp_clk_on = true;
1877 }
1878 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001879 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001880 if (fep->clk_ref) {
1881 ret = clk_prepare_enable(fep->clk_ref);
1882 if (ret)
1883 goto failed_clk_ref;
1884 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001885 } else {
1886 clk_disable_unprepare(fep->clk_ahb);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001887 if (fep->clk_enet_out)
1888 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001889 if (fep->clk_ptp) {
1890 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001891 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001892 fep->ptp_clk_on = false;
1893 mutex_unlock(&fep->ptp_clk_mutex);
1894 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001895 if (fep->clk_ref)
1896 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001897 }
1898
1899 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001900
1901failed_clk_ref:
1902 if (fep->clk_ref)
1903 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001904failed_clk_ptp:
1905 if (fep->clk_enet_out)
1906 clk_disable_unprepare(fep->clk_enet_out);
1907failed_clk_enet_out:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001908 clk_disable_unprepare(fep->clk_ahb);
1909
1910 return ret;
1911}
1912
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001913static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001914{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001915 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001916 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001917 char mdio_bus_id[MII_BUS_ID_SIZE];
1918 char phy_name[MII_BUS_ID_SIZE + 3];
1919 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001920 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001921
Bryan Wu418bd0d2010-05-28 03:40:39 -07001922 fep->phy_dev = NULL;
1923
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001924 if (fep->phy_node) {
1925 phy_dev = of_phy_connect(ndev, fep->phy_node,
1926 &fec_enet_adjust_link, 0,
1927 fep->phy_interface);
Nimrod Andy213a9922014-12-09 18:46:56 +08001928 if (!phy_dev)
1929 return -ENODEV;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001930 } else {
1931 /* check for attached phy */
1932 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
Andrew Lunn7f854422016-01-06 20:11:18 +01001933 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001934 continue;
1935 if (dev_id--)
1936 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001937 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001938 break;
1939 }
1940
1941 if (phy_id >= PHY_MAX_ADDR) {
1942 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001943 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001944 phy_id = 0;
1945 }
1946
1947 snprintf(phy_name, sizeof(phy_name),
1948 PHY_ID_FMT, mdio_bus_id, phy_id);
1949 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1950 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001951 }
1952
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001953 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001954 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001955 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001956 }
1957
1958 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001959 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001960 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001961 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001962#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001963 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001964#endif
Frank Libaa70a52013-01-16 16:55:58 +00001965 }
Shawn Guo230dec62011-09-23 02:12:48 +00001966 else
1967 phy_dev->supported &= PHY_BASIC_FEATURES;
1968
Bryan Wue6b043d2010-03-31 02:10:44 +00001969 phy_dev->advertising = phy_dev->supported;
1970
1971 fep->phy_dev = phy_dev;
1972 fep->link = 0;
1973 fep->full_duplex = 0;
1974
Andrew Lunn22209432016-01-06 20:11:13 +01001975 phy_attached_info(phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001976
Bryan Wue6b043d2010-03-31 02:10:44 +00001977 return 0;
1978}
1979
1980static int fec_enet_mii_init(struct platform_device *pdev)
1981{
Shawn Guob5680e02011-01-05 21:13:13 +00001982 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001983 struct net_device *ndev = platform_get_drvdata(pdev);
1984 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001985 struct device_node *node;
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001986 int err = -ENXIO;
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001987 u32 mii_speed, holdtime;
Bryan Wue6b043d2010-03-31 02:10:44 +00001988
Shawn Guob5680e02011-01-05 21:13:13 +00001989 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001990 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001991 * Here are the differences:
1992 *
1993 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1994 * - fec0 acts as the 1588 time master while fec1 is slave
1995 * - external phys can only be configured by fec0
1996 *
1997 * That is to say fec1 can not work independently. It only works
1998 * when fec0 is working. The reason behind this design is that the
1999 * second interface is added primarily for Switch mode.
2000 *
2001 * Because of the last point above, both phys are attached on fec0
2002 * mdio interface in board design, and need to be configured by
2003 * fec0 mii_bus.
2004 */
Stefan Agner3d125f92015-01-14 00:20:21 +01002005 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00002006 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002007 if (mii_cnt && fec0_mii_bus) {
2008 fep->mii_bus = fec0_mii_bus;
2009 mii_cnt++;
2010 return 0;
2011 }
2012 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00002013 }
2014
Bryan Wue6b043d2010-03-31 02:10:44 +00002015 fep->mii_timeout = 0;
2016
2017 /*
2018 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002019 *
2020 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2021 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2022 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2023 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002024 */
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002025 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002026 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002027 mii_speed--;
2028 if (mii_speed > 63) {
2029 dev_err(&pdev->dev,
2030 "fec clock (%lu) to fast to get right mii speed\n",
2031 clk_get_rate(fep->clk_ipg));
2032 err = -EINVAL;
2033 goto err_out;
2034 }
2035
2036 /*
2037 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2038 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2039 * versions are RAZ there, so just ignore the difference and write the
2040 * register always.
2041 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2042 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2043 * output.
2044 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2045 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2046 * holdtime cannot result in a value greater than 3.
2047 */
2048 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2049
2050 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2051
Bryan Wue6b043d2010-03-31 02:10:44 +00002052 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2053
2054 fep->mii_bus = mdiobus_alloc();
2055 if (fep->mii_bus == NULL) {
2056 err = -ENOMEM;
2057 goto err_out;
2058 }
2059
2060 fep->mii_bus->name = "fec_enet_mii_bus";
2061 fep->mii_bus->read = fec_enet_mdio_read;
2062 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002063 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2064 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002065 fep->mii_bus->priv = fep;
2066 fep->mii_bus->parent = &pdev->dev;
2067
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002068 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2069 if (node) {
2070 err = of_mdiobus_register(fep->mii_bus, node);
2071 of_node_put(node);
2072 } else {
2073 err = mdiobus_register(fep->mii_bus);
2074 }
2075
2076 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002077 goto err_out_free_mdiobus;
Bryan Wue6b043d2010-03-31 02:10:44 +00002078
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002079 mii_cnt++;
2080
Shawn Guob5680e02011-01-05 21:13:13 +00002081 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002082 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002083 fec0_mii_bus = fep->mii_bus;
2084
Bryan Wue6b043d2010-03-31 02:10:44 +00002085 return 0;
2086
Bryan Wue6b043d2010-03-31 02:10:44 +00002087err_out_free_mdiobus:
2088 mdiobus_free(fep->mii_bus);
2089err_out:
2090 return err;
2091}
2092
2093static void fec_enet_mii_remove(struct fec_enet_private *fep)
2094{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002095 if (--mii_cnt == 0) {
2096 mdiobus_unregister(fep->mii_bus);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002097 mdiobus_free(fep->mii_bus);
2098 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002099}
2100
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002101static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002102 struct ethtool_cmd *cmd)
2103{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002104 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002105 struct phy_device *phydev = fep->phy_dev;
2106
2107 if (!phydev)
2108 return -ENODEV;
2109
2110 return phy_ethtool_gset(phydev, cmd);
2111}
2112
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002113static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002114 struct ethtool_cmd *cmd)
2115{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002116 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002117 struct phy_device *phydev = fep->phy_dev;
2118
2119 if (!phydev)
2120 return -ENODEV;
2121
2122 return phy_ethtool_sset(phydev, cmd);
2123}
2124
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002125static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002126 struct ethtool_drvinfo *info)
2127{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002128 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Jiri Pirko7826d432013-01-06 00:44:26 +00002130 strlcpy(info->driver, fep->pdev->dev.driver->name,
2131 sizeof(info->driver));
2132 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2133 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
Bryan Wue6b043d2010-03-31 02:10:44 +00002135
Philippe Reynesdb65f352015-05-11 00:01:42 +02002136static int fec_enet_get_regs_len(struct net_device *ndev)
2137{
2138 struct fec_enet_private *fep = netdev_priv(ndev);
2139 struct resource *r;
2140 int s = 0;
2141
2142 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2143 if (r)
2144 s = resource_size(r);
2145
2146 return s;
2147}
2148
2149/* List of registers that can be safety be read to dump them with ethtool */
2150#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Johannes Berg05f3b502016-01-25 11:40:50 +01002151 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Philippe Reynesdb65f352015-05-11 00:01:42 +02002152static u32 fec_enet_register_offset[] = {
2153 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2154 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2155 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2156 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2157 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2158 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2159 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2160 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2161 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2162 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2163 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2164 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2165 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2166 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2167 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2168 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2169 RMON_T_P_GTE2048, RMON_T_OCTETS,
2170 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2171 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2172 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2173 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2174 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2175 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2176 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2177 RMON_R_P_GTE2048, RMON_R_OCTETS,
2178 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2179 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2180};
2181#else
2182static u32 fec_enet_register_offset[] = {
2183 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2184 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2185 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2186 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2187 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2188 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2189 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2190 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2191 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2192};
2193#endif
2194
2195static void fec_enet_get_regs(struct net_device *ndev,
2196 struct ethtool_regs *regs, void *regbuf)
2197{
2198 struct fec_enet_private *fep = netdev_priv(ndev);
2199 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2200 u32 *buf = (u32 *)regbuf;
2201 u32 i, off;
2202
2203 memset(buf, 0, regs->len);
2204
2205 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2206 off = fec_enet_register_offset[i] / 4;
2207 buf[off] = readl(&theregs[off]);
2208 }
2209}
2210
Frank Li5ebae4892013-01-06 16:25:07 +00002211static int fec_enet_get_ts_info(struct net_device *ndev,
2212 struct ethtool_ts_info *info)
2213{
2214 struct fec_enet_private *fep = netdev_priv(ndev);
2215
2216 if (fep->bufdesc_ex) {
2217
2218 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2219 SOF_TIMESTAMPING_RX_SOFTWARE |
2220 SOF_TIMESTAMPING_SOFTWARE |
2221 SOF_TIMESTAMPING_TX_HARDWARE |
2222 SOF_TIMESTAMPING_RX_HARDWARE |
2223 SOF_TIMESTAMPING_RAW_HARDWARE;
2224 if (fep->ptp_clock)
2225 info->phc_index = ptp_clock_index(fep->ptp_clock);
2226 else
2227 info->phc_index = -1;
2228
2229 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2230 (1 << HWTSTAMP_TX_ON);
2231
2232 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2233 (1 << HWTSTAMP_FILTER_ALL);
2234 return 0;
2235 } else {
2236 return ethtool_op_get_ts_info(ndev, info);
2237 }
2238}
2239
Guenter Roeckd1391932013-06-18 10:04:59 -07002240#if !defined(CONFIG_M5272)
2241
Frank Libaa70a52013-01-16 16:55:58 +00002242static void fec_enet_get_pauseparam(struct net_device *ndev,
2243 struct ethtool_pauseparam *pause)
2244{
2245 struct fec_enet_private *fep = netdev_priv(ndev);
2246
2247 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2248 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2249 pause->rx_pause = pause->tx_pause;
2250}
2251
2252static int fec_enet_set_pauseparam(struct net_device *ndev,
2253 struct ethtool_pauseparam *pause)
2254{
2255 struct fec_enet_private *fep = netdev_priv(ndev);
2256
Russell King0b146ca2014-07-08 00:22:59 +01002257 if (!fep->phy_dev)
2258 return -ENODEV;
2259
Frank Libaa70a52013-01-16 16:55:58 +00002260 if (pause->tx_pause != pause->rx_pause) {
2261 netdev_info(ndev,
2262 "hardware only support enable/disable both tx and rx");
2263 return -EINVAL;
2264 }
2265
2266 fep->pause_flag = 0;
2267
2268 /* tx pause must be same as rx pause */
2269 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2270 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2271
2272 if (pause->rx_pause || pause->autoneg) {
2273 fep->phy_dev->supported |= ADVERTISED_Pause;
2274 fep->phy_dev->advertising |= ADVERTISED_Pause;
2275 } else {
2276 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2277 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2278 }
2279
2280 if (pause->autoneg) {
2281 if (netif_running(ndev))
2282 fec_stop(ndev);
2283 phy_start_aneg(fep->phy_dev);
2284 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002285 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002286 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002287 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002288 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002289 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002290 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002291 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002292 }
Frank Libaa70a52013-01-16 16:55:58 +00002293
2294 return 0;
2295}
2296
Chris Healy38ae92d2013-06-25 23:18:52 -07002297static const struct fec_stat {
2298 char name[ETH_GSTRING_LEN];
2299 u16 offset;
2300} fec_stats[] = {
2301 /* RMON TX */
2302 { "tx_dropped", RMON_T_DROP },
2303 { "tx_packets", RMON_T_PACKETS },
2304 { "tx_broadcast", RMON_T_BC_PKT },
2305 { "tx_multicast", RMON_T_MC_PKT },
2306 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2307 { "tx_undersize", RMON_T_UNDERSIZE },
2308 { "tx_oversize", RMON_T_OVERSIZE },
2309 { "tx_fragment", RMON_T_FRAG },
2310 { "tx_jabber", RMON_T_JAB },
2311 { "tx_collision", RMON_T_COL },
2312 { "tx_64byte", RMON_T_P64 },
2313 { "tx_65to127byte", RMON_T_P65TO127 },
2314 { "tx_128to255byte", RMON_T_P128TO255 },
2315 { "tx_256to511byte", RMON_T_P256TO511 },
2316 { "tx_512to1023byte", RMON_T_P512TO1023 },
2317 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2318 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2319 { "tx_octets", RMON_T_OCTETS },
2320
2321 /* IEEE TX */
2322 { "IEEE_tx_drop", IEEE_T_DROP },
2323 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2324 { "IEEE_tx_1col", IEEE_T_1COL },
2325 { "IEEE_tx_mcol", IEEE_T_MCOL },
2326 { "IEEE_tx_def", IEEE_T_DEF },
2327 { "IEEE_tx_lcol", IEEE_T_LCOL },
2328 { "IEEE_tx_excol", IEEE_T_EXCOL },
2329 { "IEEE_tx_macerr", IEEE_T_MACERR },
2330 { "IEEE_tx_cserr", IEEE_T_CSERR },
2331 { "IEEE_tx_sqe", IEEE_T_SQE },
2332 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2333 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2334
2335 /* RMON RX */
2336 { "rx_packets", RMON_R_PACKETS },
2337 { "rx_broadcast", RMON_R_BC_PKT },
2338 { "rx_multicast", RMON_R_MC_PKT },
2339 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2340 { "rx_undersize", RMON_R_UNDERSIZE },
2341 { "rx_oversize", RMON_R_OVERSIZE },
2342 { "rx_fragment", RMON_R_FRAG },
2343 { "rx_jabber", RMON_R_JAB },
2344 { "rx_64byte", RMON_R_P64 },
2345 { "rx_65to127byte", RMON_R_P65TO127 },
2346 { "rx_128to255byte", RMON_R_P128TO255 },
2347 { "rx_256to511byte", RMON_R_P256TO511 },
2348 { "rx_512to1023byte", RMON_R_P512TO1023 },
2349 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2350 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2351 { "rx_octets", RMON_R_OCTETS },
2352
2353 /* IEEE RX */
2354 { "IEEE_rx_drop", IEEE_R_DROP },
2355 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2356 { "IEEE_rx_crc", IEEE_R_CRC },
2357 { "IEEE_rx_align", IEEE_R_ALIGN },
2358 { "IEEE_rx_macerr", IEEE_R_MACERR },
2359 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2360 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2361};
2362
2363static void fec_enet_get_ethtool_stats(struct net_device *dev,
2364 struct ethtool_stats *stats, u64 *data)
2365{
2366 struct fec_enet_private *fep = netdev_priv(dev);
2367 int i;
2368
2369 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2370 data[i] = readl(fep->hwp + fec_stats[i].offset);
2371}
2372
2373static void fec_enet_get_strings(struct net_device *netdev,
2374 u32 stringset, u8 *data)
2375{
2376 int i;
2377 switch (stringset) {
2378 case ETH_SS_STATS:
2379 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2380 memcpy(data + i * ETH_GSTRING_LEN,
2381 fec_stats[i].name, ETH_GSTRING_LEN);
2382 break;
2383 }
2384}
2385
2386static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2387{
2388 switch (sset) {
2389 case ETH_SS_STATS:
2390 return ARRAY_SIZE(fec_stats);
2391 default:
2392 return -EOPNOTSUPP;
2393 }
2394}
Guenter Roeckd1391932013-06-18 10:04:59 -07002395#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002396
Chris Healy32bc9b42013-06-17 07:25:06 -07002397static int fec_enet_nway_reset(struct net_device *dev)
2398{
2399 struct fec_enet_private *fep = netdev_priv(dev);
2400 struct phy_device *phydev = fep->phy_dev;
2401
2402 if (!phydev)
2403 return -ENODEV;
2404
2405 return genphy_restart_aneg(phydev);
2406}
2407
Fugang Duand851b472014-09-17 05:18:52 +08002408/* ITR clock source is enet system clock (clk_ahb).
2409 * TCTT unit is cycle_ns * 64 cycle
2410 * So, the ICTT value = X us / (cycle_ns * 64)
2411 */
2412static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2413{
2414 struct fec_enet_private *fep = netdev_priv(ndev);
2415
2416 return us * (fep->itr_clk_rate / 64000) / 1000;
2417}
2418
2419/* Set threshold for interrupt coalescing */
2420static void fec_enet_itr_coal_set(struct net_device *ndev)
2421{
2422 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002423 int rx_itr, tx_itr;
2424
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002425 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002426 return;
2427
2428 /* Must be greater than zero to avoid unpredictable behavior */
2429 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2430 !fep->tx_time_itr || !fep->tx_pkts_itr)
2431 return;
2432
2433 /* Select enet system clock as Interrupt Coalescing
2434 * timer Clock Source
2435 */
2436 rx_itr = FEC_ITR_CLK_SEL;
2437 tx_itr = FEC_ITR_CLK_SEL;
2438
2439 /* set ICFT and ICTT */
2440 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2441 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2442 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2443 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2444
2445 rx_itr |= FEC_ITR_EN;
2446 tx_itr |= FEC_ITR_EN;
2447
2448 writel(tx_itr, fep->hwp + FEC_TXIC0);
2449 writel(rx_itr, fep->hwp + FEC_RXIC0);
2450 writel(tx_itr, fep->hwp + FEC_TXIC1);
2451 writel(rx_itr, fep->hwp + FEC_RXIC1);
2452 writel(tx_itr, fep->hwp + FEC_TXIC2);
2453 writel(rx_itr, fep->hwp + FEC_RXIC2);
2454}
2455
2456static int
2457fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2458{
2459 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002460
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002461 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002462 return -EOPNOTSUPP;
2463
2464 ec->rx_coalesce_usecs = fep->rx_time_itr;
2465 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2466
2467 ec->tx_coalesce_usecs = fep->tx_time_itr;
2468 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2469
2470 return 0;
2471}
2472
2473static int
2474fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2475{
2476 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002477 unsigned int cycle;
2478
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002479 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002480 return -EOPNOTSUPP;
2481
2482 if (ec->rx_max_coalesced_frames > 255) {
2483 pr_err("Rx coalesced frames exceed hardware limiation");
2484 return -EINVAL;
2485 }
2486
2487 if (ec->tx_max_coalesced_frames > 255) {
2488 pr_err("Tx coalesced frame exceed hardware limiation");
2489 return -EINVAL;
2490 }
2491
2492 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2493 if (cycle > 0xFFFF) {
2494 pr_err("Rx coalesed usec exceeed hardware limiation");
2495 return -EINVAL;
2496 }
2497
2498 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2499 if (cycle > 0xFFFF) {
2500 pr_err("Rx coalesed usec exceeed hardware limiation");
2501 return -EINVAL;
2502 }
2503
2504 fep->rx_time_itr = ec->rx_coalesce_usecs;
2505 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2506
2507 fep->tx_time_itr = ec->tx_coalesce_usecs;
2508 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2509
2510 fec_enet_itr_coal_set(ndev);
2511
2512 return 0;
2513}
2514
2515static void fec_enet_itr_coal_init(struct net_device *ndev)
2516{
2517 struct ethtool_coalesce ec;
2518
2519 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2520 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2521
2522 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2523 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2524
2525 fec_enet_set_coalesce(ndev, &ec);
2526}
2527
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002528static int fec_enet_get_tunable(struct net_device *netdev,
2529 const struct ethtool_tunable *tuna,
2530 void *data)
2531{
2532 struct fec_enet_private *fep = netdev_priv(netdev);
2533 int ret = 0;
2534
2535 switch (tuna->id) {
2536 case ETHTOOL_RX_COPYBREAK:
2537 *(u32 *)data = fep->rx_copybreak;
2538 break;
2539 default:
2540 ret = -EINVAL;
2541 break;
2542 }
2543
2544 return ret;
2545}
2546
2547static int fec_enet_set_tunable(struct net_device *netdev,
2548 const struct ethtool_tunable *tuna,
2549 const void *data)
2550{
2551 struct fec_enet_private *fep = netdev_priv(netdev);
2552 int ret = 0;
2553
2554 switch (tuna->id) {
2555 case ETHTOOL_RX_COPYBREAK:
2556 fep->rx_copybreak = *(u32 *)data;
2557 break;
2558 default:
2559 ret = -EINVAL;
2560 break;
2561 }
2562
2563 return ret;
2564}
2565
Nimrod Andyde40ed32014-12-24 17:30:39 +08002566static void
2567fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2568{
2569 struct fec_enet_private *fep = netdev_priv(ndev);
2570
2571 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2572 wol->supported = WAKE_MAGIC;
2573 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2574 } else {
2575 wol->supported = wol->wolopts = 0;
2576 }
2577}
2578
2579static int
2580fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2581{
2582 struct fec_enet_private *fep = netdev_priv(ndev);
2583
2584 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2585 return -EINVAL;
2586
2587 if (wol->wolopts & ~WAKE_MAGIC)
2588 return -EINVAL;
2589
2590 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2591 if (device_may_wakeup(&ndev->dev)) {
2592 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2593 if (fep->irq[0] > 0)
2594 enable_irq_wake(fep->irq[0]);
2595 } else {
2596 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2597 if (fep->irq[0] > 0)
2598 disable_irq_wake(fep->irq[0]);
2599 }
2600
2601 return 0;
2602}
2603
stephen hemminger9b07be42012-01-04 12:59:49 +00002604static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002605 .get_settings = fec_enet_get_settings,
2606 .set_settings = fec_enet_set_settings,
2607 .get_drvinfo = fec_enet_get_drvinfo,
Philippe Reynesdb65f352015-05-11 00:01:42 +02002608 .get_regs_len = fec_enet_get_regs_len,
2609 .get_regs = fec_enet_get_regs,
Chris Healy32bc9b42013-06-17 07:25:06 -07002610 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002611 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002612 .get_coalesce = fec_enet_get_coalesce,
2613 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002614#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002615 .get_pauseparam = fec_enet_get_pauseparam,
2616 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002617 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002618 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002619 .get_sset_count = fec_enet_get_sset_count,
2620#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002621 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002622 .get_tunable = fec_enet_get_tunable,
2623 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002624 .get_wol = fec_enet_get_wol,
2625 .set_wol = fec_enet_set_wol,
Bryan Wue6b043d2010-03-31 02:10:44 +00002626};
2627
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002628static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002629{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002630 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002631 struct phy_device *phydev = fep->phy_dev;
2632
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002633 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002634 return -EINVAL;
2635
2636 if (!phydev)
2637 return -ENODEV;
2638
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002639 if (fep->bufdesc_ex) {
2640 if (cmd == SIOCSHWTSTAMP)
2641 return fec_ptp_set(ndev, rq);
2642 if (cmd == SIOCGHWTSTAMP)
2643 return fec_ptp_get(ndev, rq);
2644 }
Frank Liff43da82013-01-03 16:04:23 +00002645
Richard Cochran28b04112010-07-17 08:48:55 +00002646 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002649static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002650{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002651 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002652 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002653 struct sk_buff *skb;
2654 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002655 struct fec_enet_priv_tx_q *txq;
2656 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f742014-09-13 05:00:50 +08002657 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002658
Frank Li59d0f742014-09-13 05:00:50 +08002659 for (q = 0; q < fep->num_rx_queues; q++) {
2660 rxq = fep->rx_queue[q];
2661 bdp = rxq->rx_bd_base;
2662 for (i = 0; i < rxq->rx_ring_size; i++) {
2663 skb = rxq->rx_skbuff[i];
2664 rxq->rx_skbuff[i] = NULL;
2665 if (skb) {
2666 dma_unmap_single(&fep->pdev->dev,
Johannes Berg5cfa3032016-01-24 16:52:37 +01002667 fec32_to_cpu(bdp->cbd_bufaddr),
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002668 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f742014-09-13 05:00:50 +08002669 DMA_FROM_DEVICE);
2670 dev_kfree_skb(skb);
2671 }
2672 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002673 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002674 }
2675
Frank Li59d0f742014-09-13 05:00:50 +08002676 for (q = 0; q < fep->num_tx_queues; q++) {
2677 txq = fep->tx_queue[q];
2678 bdp = txq->tx_bd_base;
2679 for (i = 0; i < txq->tx_ring_size; i++) {
2680 kfree(txq->tx_bounce[i]);
2681 txq->tx_bounce[i] = NULL;
2682 skb = txq->tx_skbuff[i];
2683 txq->tx_skbuff[i] = NULL;
2684 dev_kfree_skb(skb);
2685 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002686 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002687}
2688
Frank Li59d0f742014-09-13 05:00:50 +08002689static void fec_enet_free_queue(struct net_device *ndev)
2690{
2691 struct fec_enet_private *fep = netdev_priv(ndev);
2692 int i;
2693 struct fec_enet_priv_tx_q *txq;
2694
2695 for (i = 0; i < fep->num_tx_queues; i++)
2696 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2697 txq = fep->tx_queue[i];
2698 dma_free_coherent(NULL,
2699 txq->tx_ring_size * TSO_HEADER_SIZE,
2700 txq->tso_hdrs,
2701 txq->tso_hdrs_dma);
2702 }
2703
2704 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002705 kfree(fep->rx_queue[i]);
Frank Li59d0f742014-09-13 05:00:50 +08002706 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002707 kfree(fep->tx_queue[i]);
Frank Li59d0f742014-09-13 05:00:50 +08002708}
2709
2710static int fec_enet_alloc_queue(struct net_device *ndev)
2711{
2712 struct fec_enet_private *fep = netdev_priv(ndev);
2713 int i;
2714 int ret = 0;
2715 struct fec_enet_priv_tx_q *txq;
2716
2717 for (i = 0; i < fep->num_tx_queues; i++) {
2718 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2719 if (!txq) {
2720 ret = -ENOMEM;
2721 goto alloc_failed;
2722 }
2723
2724 fep->tx_queue[i] = txq;
2725 txq->tx_ring_size = TX_RING_SIZE;
2726 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2727
2728 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2729 txq->tx_wake_threshold =
2730 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2731
2732 txq->tso_hdrs = dma_alloc_coherent(NULL,
2733 txq->tx_ring_size * TSO_HEADER_SIZE,
2734 &txq->tso_hdrs_dma,
2735 GFP_KERNEL);
2736 if (!txq->tso_hdrs) {
2737 ret = -ENOMEM;
2738 goto alloc_failed;
2739 }
2740 }
2741
2742 for (i = 0; i < fep->num_rx_queues; i++) {
2743 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2744 GFP_KERNEL);
2745 if (!fep->rx_queue[i]) {
2746 ret = -ENOMEM;
2747 goto alloc_failed;
2748 }
2749
2750 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2751 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2752 }
2753 return ret;
2754
2755alloc_failed:
2756 fec_enet_free_queue(ndev);
2757 return ret;
2758}
2759
2760static int
2761fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002762{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002763 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002764 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002765 struct sk_buff *skb;
2766 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002767 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002768
Frank Li59d0f742014-09-13 05:00:50 +08002769 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002770 bdp = rxq->rx_bd_base;
2771 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002772 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002773 if (!skb)
2774 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002775
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002776 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002777 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002778 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002779 }
Russell King730ee362014-07-08 00:23:14 +01002780
Fugang Duan4d494cd2014-09-13 05:00:48 +08002781 rxq->rx_skbuff[i] = skb;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002782 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
Frank Liff43da82013-01-03 16:04:23 +00002783
2784 if (fep->bufdesc_ex) {
2785 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002786 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002787 }
2788
Frank Li59d0f742014-09-13 05:00:50 +08002789 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002790 }
2791
2792 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002793 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002794 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Frank Li59d0f742014-09-13 05:00:50 +08002795 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002796
Frank Li59d0f742014-09-13 05:00:50 +08002797 err_alloc:
2798 fec_enet_free_buffers(ndev);
2799 return -ENOMEM;
2800}
2801
2802static int
2803fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2804{
2805 struct fec_enet_private *fep = netdev_priv(ndev);
2806 unsigned int i;
2807 struct bufdesc *bdp;
2808 struct fec_enet_priv_tx_q *txq;
2809
2810 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002811 bdp = txq->tx_bd_base;
2812 for (i = 0; i < txq->tx_ring_size; i++) {
2813 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2814 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002815 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002816
Johannes Berg5cfa3032016-01-24 16:52:37 +01002817 bdp->cbd_sc = cpu_to_fec16(0);
2818 bdp->cbd_bufaddr = cpu_to_fec32(0);
Frank Li6605b732012-10-30 18:25:31 +00002819
Frank Liff43da82013-01-03 16:04:23 +00002820 if (fep->bufdesc_ex) {
2821 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Johannes Berg5cfa3032016-01-24 16:52:37 +01002822 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
Frank Liff43da82013-01-03 16:04:23 +00002823 }
2824
Frank Li59d0f742014-09-13 05:00:50 +08002825 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002826 }
2827
2828 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002829 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Johannes Berg5cfa3032016-01-24 16:52:37 +01002830 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002831
2832 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002833
2834 err_alloc:
2835 fec_enet_free_buffers(ndev);
2836 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002837}
2838
Frank Li59d0f742014-09-13 05:00:50 +08002839static int fec_enet_alloc_buffers(struct net_device *ndev)
2840{
2841 struct fec_enet_private *fep = netdev_priv(ndev);
2842 unsigned int i;
2843
2844 for (i = 0; i < fep->num_rx_queues; i++)
2845 if (fec_enet_alloc_rxq_buffers(ndev, i))
2846 return -ENOMEM;
2847
2848 for (i = 0; i < fep->num_tx_queues; i++)
2849 if (fec_enet_alloc_txq_buffers(ndev, i))
2850 return -ENOMEM;
2851 return 0;
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002855fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002857 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002858 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Andrew Lunn8fff7552015-07-25 22:38:02 +02002860 ret = pm_runtime_get_sync(&fep->pdev->dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03002861 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002862 return ret;
2863
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002864 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002865 ret = fec_enet_clk_enable(ndev, true);
2866 if (ret)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002867 goto clk_enable;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /* I should reset the ring buffers here, but I don't yet know
2870 * a simple way to do that.
2871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002873 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002874 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002875 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002876
Nimrod Andy55dd2752015-06-23 15:32:51 +08002877 /* Init MAC prior to mii bus probe */
2878 fec_restart(ndev);
2879
Bryan Wu418bd0d2010-05-28 03:40:39 -07002880 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002881 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002882 if (ret)
2883 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002884
2885 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002886 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002887 netif_tx_start_all_queues(ndev);
2888
Nimrod Andyde40ed32014-12-24 17:30:39 +08002889 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2890 FEC_WOL_FLAG_ENABLE);
2891
Sascha Hauer22f6b862009-04-15 01:32:18 +00002892 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002893
2894err_enet_mii_probe:
2895 fec_enet_free_buffers(ndev);
2896err_enet_alloc:
2897 fec_enet_clk_enable(ndev, false);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002898clk_enable:
2899 pm_runtime_mark_last_busy(&fep->pdev->dev);
2900 pm_runtime_put_autosuspend(&fep->pdev->dev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002901 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
2905static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002906fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002908 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Russell Kingd76cfae2014-07-08 00:23:04 +01002910 phy_stop(fep->phy_dev);
2911
Russell King31a6de32014-07-08 12:40:23 +01002912 if (netif_device_present(ndev)) {
2913 napi_disable(&fep->napi);
2914 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002915 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Russell King635cf172014-07-08 00:22:54 +01002918 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002919 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002920
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002921 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002922 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002923 pm_runtime_mark_last_busy(&fep->pdev->dev);
2924 pm_runtime_put_autosuspend(&fep->pdev->dev);
2925
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002926 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return 0;
2929}
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/* Set or clear the multicast filter for this adaptor.
2932 * Skeleton taken from sunlance driver.
2933 * The CPM Ethernet implementation allows Multicast as well as individual
2934 * MAC address filtering. Some of the drivers check to make sure it is
2935 * a group multicast address, and discard those that are not. I guess I
2936 * will do the same for now, but just remove the test if you want
2937 * individual filtering as well (do the upper net layers want or support
2938 * this kind of feature?).
2939 */
2940
2941#define HASH_BITS 6 /* #bits in hash */
2942#define CRC32_POLY 0xEDB88320
2943
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002944static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002946 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002947 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002948 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 unsigned char hash;
2950
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002951 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002952 tmp = readl(fep->hwp + FEC_R_CNTRL);
2953 tmp |= 0x8;
2954 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002955 return;
2956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Sascha Hauer4e831832009-04-15 01:32:19 +00002958 tmp = readl(fep->hwp + FEC_R_CNTRL);
2959 tmp &= ~0x8;
2960 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002961
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002962 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002963 /* Catch all multicast addresses, so set the
2964 * filter to all 1's
2965 */
2966 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2967 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Sascha Hauer4e831832009-04-15 01:32:19 +00002969 return;
2970 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002971
Sascha Hauer4e831832009-04-15 01:32:19 +00002972 /* Clear filter and add the addresses in hash register
2973 */
2974 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2975 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002977 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002978 /* calculate crc32 value of mac address */
2979 crc = 0xffffffff;
2980
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002981 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002982 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002983 for (bit = 0; bit < 8; bit++, data >>= 1) {
2984 crc = (crc >> 1) ^
2985 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002988
2989 /* only upper 6 bits (HASH_BITS) are used
2990 * which point to specific bit in he hash registers
2991 */
2992 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2993
2994 if (hash > 31) {
2995 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2996 tmp |= 1 << (hash - 32);
2997 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2998 } else {
2999 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3000 tmp |= 1 << hash;
3001 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
3004}
3005
Sascha Hauer22f6b862009-04-15 01:32:18 +00003006/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00003007static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003008fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003010 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00003011 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Lucas Stach44934fa2014-03-30 21:32:08 +02003013 if (addr) {
3014 if (!is_valid_ether_addr(addr->sa_data))
3015 return -EADDRNOTAVAIL;
3016 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3017 }
Sascha Hauer009fda82009-04-15 01:32:23 +00003018
Nimrod Andy9638d192015-09-10 09:35:39 +08003019 /* Add netif status check here to avoid system hang in below case:
3020 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3021 * After ethx down, fec all clocks are gated off and then register
3022 * access causes system hang.
3023 */
3024 if (!netif_running(ndev))
3025 return 0;
3026
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003027 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3028 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00003029 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003030 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07003031 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00003032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003035#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00003036/**
3037 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003038 * @dev: The FEC network adapter
3039 *
3040 * Polled functionality used by netconsole and others in non interrupt mode
3041 *
3042 */
Wei Yongjun47a52472013-03-20 05:06:11 +00003043static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003044{
3045 int i;
3046 struct fec_enet_private *fep = netdev_priv(dev);
3047
3048 for (i = 0; i < FEC_IRQ_NUM; i++) {
3049 if (fep->irq[i] > 0) {
3050 disable_irq(fep->irq[i]);
3051 fec_enet_interrupt(fep->irq[i], dev);
3052 enable_irq(fep->irq[i]);
3053 }
3054 }
3055}
3056#endif
3057
Nimrod Andy5bc26722014-10-13 10:53:48 +08003058static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00003059 netdev_features_t features)
3060{
3061 struct fec_enet_private *fep = netdev_priv(netdev);
3062 netdev_features_t changed = features ^ netdev->features;
3063
3064 netdev->features = features;
3065
3066 /* Receive checksum has been changed */
3067 if (changed & NETIF_F_RXCSUM) {
3068 if (features & NETIF_F_RXCSUM)
3069 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3070 else
3071 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01003072 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08003073}
Jim Baxter4c09eed2013-04-19 08:10:49 +00003074
Nimrod Andy5bc26722014-10-13 10:53:48 +08003075static int fec_set_features(struct net_device *netdev,
3076 netdev_features_t features)
3077{
3078 struct fec_enet_private *fep = netdev_priv(netdev);
3079 netdev_features_t changed = features ^ netdev->features;
3080
Fabio Estevam5b40f702015-09-25 18:31:32 -03003081 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08003082 napi_disable(&fep->napi);
3083 netif_tx_lock_bh(netdev);
3084 fec_stop(netdev);
3085 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01003086 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003087 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01003088 netif_tx_unlock_bh(netdev);
3089 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08003090 } else {
3091 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00003092 }
3093
3094 return 0;
3095}
3096
Sascha Hauer009fda82009-04-15 01:32:23 +00003097static const struct net_device_ops fec_netdev_ops = {
3098 .ndo_open = fec_enet_open,
3099 .ndo_stop = fec_enet_close,
3100 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003101 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00003102 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00003103 .ndo_validate_addr = eth_validate_addr,
3104 .ndo_tx_timeout = fec_timeout,
3105 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01003106 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003107#ifdef CONFIG_NET_POLL_CONTROLLER
3108 .ndo_poll_controller = fec_poll_controller,
3109#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00003110 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00003111};
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /*
3114 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00003115 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003117static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003119 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003120 struct fec_enet_priv_tx_q *txq;
3121 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00003122 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08003123 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08003124 int bd_size;
Frank Li59d0f742014-09-13 05:00:50 +08003125 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08003126
Fugang Duan41ef84c2014-09-13 05:00:54 +08003127#if defined(CONFIG_ARM)
3128 fep->rx_align = 0xf;
3129 fep->tx_align = 0xf;
3130#else
3131 fep->rx_align = 0x3;
3132 fep->tx_align = 0x3;
3133#endif
3134
Frank Li59d0f742014-09-13 05:00:50 +08003135 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003136
Nimrod Andy55d02182014-06-12 08:16:21 +08003137 if (fep->bufdesc_ex)
3138 fep->bufdesc_size = sizeof(struct bufdesc_ex);
3139 else
3140 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003141 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08003142 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003144 /* Allocate memory for buffer descriptors. */
Lucas Stachc0a1a0a2015-07-23 16:06:19 +02003145 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3146 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003147 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003148 return -ENOMEM;
3149 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003150
Fugang Duan4d494cd2014-09-13 05:00:48 +08003151 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Shawn Guo49da97d2011-01-05 21:13:11 +00003153 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003154 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003155 /* make sure MAC we just acquired is programmed into the hw */
3156 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003158 /* Set receive and transmit descriptor base. */
Frank Li59d0f742014-09-13 05:00:50 +08003159 for (i = 0; i < fep->num_rx_queues; i++) {
3160 rxq = fep->rx_queue[i];
3161 rxq->index = i;
3162 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3163 rxq->bd_dma = bd_dma;
3164 if (fep->bufdesc_ex) {
3165 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3166 cbd_base = (struct bufdesc *)
3167 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3168 } else {
3169 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3170 cbd_base += rxq->rx_ring_size;
3171 }
3172 }
3173
3174 for (i = 0; i < fep->num_tx_queues; i++) {
3175 txq = fep->tx_queue[i];
3176 txq->index = i;
3177 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3178 txq->bd_dma = bd_dma;
3179 if (fep->bufdesc_ex) {
3180 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3181 cbd_base = (struct bufdesc *)
3182 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3183 } else {
3184 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3185 cbd_base += txq->tx_ring_size;
3186 }
3187 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Sascha Hauer22f6b862009-04-15 01:32:18 +00003190 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003191 ndev->watchdog_timeo = TX_TIMEOUT;
3192 ndev->netdev_ops = &fec_netdev_ops;
3193 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003194
Frank Lidc975382013-01-28 18:31:42 +00003195 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003196 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003197
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003198 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003199 /* enable hw VLAN support */
3200 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003201
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003202 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003203 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3204
Shawn Guo48496252013-05-08 21:08:22 +00003205 /* enable hw accelerator */
3206 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003207 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003208 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3209 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003210
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003211 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003212 fep->tx_align = 0;
3213 fep->rx_align = 0x3f;
3214 }
3215
Nimrod Andy09d1e542014-06-12 08:16:20 +08003216 ndev->hw_features = ndev->features;
3217
Russell Kingef833372014-07-08 12:40:43 +01003218 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return 0;
3221}
3222
Shawn Guoca2cc332011-06-25 02:04:35 +08003223#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003224static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003225{
3226 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003227 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003228 struct device_node *np = pdev->dev.of_node;
3229
3230 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003231 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003232
Shawn Guoa3caad02012-06-27 03:45:24 +00003233 of_property_read_u32(np, "phy-reset-duration", &msec);
3234 /* A sane reset duration should not be longer than 1s */
3235 if (msec > 1000)
3236 msec = 1;
3237
Shawn Guoca2cc332011-06-25 02:04:35 +08003238 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003239 if (!gpio_is_valid(phy_reset))
3240 return;
3241
Shawn Guo119fc002012-06-27 03:45:22 +00003242 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3243 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003244 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003245 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003246 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003247 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003248 msleep(msec);
Fabio Estevamf4444572015-10-28 10:20:30 -02003249 gpio_set_value_cansleep(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003250}
3251#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003252static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003253{
3254 /*
3255 * In case of platform probe, the reset has been done
3256 * by machine code.
3257 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003258}
3259#endif /* CONFIG_OF */
3260
Fugang Duan9fc095f2014-09-13 05:00:49 +08003261static void
3262fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3263{
3264 struct device_node *np = pdev->dev.of_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003265
3266 *num_tx = *num_rx = 1;
3267
3268 if (!np || !of_device_is_available(np))
3269 return;
3270
3271 /* parse the num of tx and rx queues */
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303272 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003273
Saurabh Sengar73b1c902015-11-23 19:21:48 +05303274 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003275
3276 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003277 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3278 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003279 *num_tx = 1;
3280 return;
3281 }
3282
3283 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003284 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3285 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003286 *num_rx = 1;
3287 return;
3288 }
3289
3290}
3291
Bill Pemberton33897cc2012-12-03 09:23:58 -05003292static int
Sascha Haueread73182009-01-28 23:03:11 +00003293fec_probe(struct platform_device *pdev)
3294{
3295 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003296 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003297 struct net_device *ndev;
3298 int i, irq, ret = 0;
3299 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003300 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003301 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003302 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003303 int num_tx_qs;
3304 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003305
Fugang Duan9fc095f2014-09-13 05:00:49 +08003306 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3307
Sascha Haueread73182009-01-28 23:03:11 +00003308 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003309 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3310 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003311 if (!ndev)
3312 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003313
3314 SET_NETDEV_DEV(ndev, &pdev->dev);
3315
3316 /* setup board info structure */
3317 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003318
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003319 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3320 if (of_id)
3321 pdev->id_entry = of_id->data;
3322 fep->quirks = pdev->id_entry->driver_data;
3323
Hubert Feurstein0c818592015-01-07 14:48:17 +01003324 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003325 fep->num_rx_queues = num_rx_qs;
3326 fep->num_tx_queues = num_tx_qs;
3327
Guenter Roeckd1391932013-06-18 10:04:59 -07003328#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003329 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003330 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003331 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003332#endif
Frank Libaa70a52013-01-16 16:55:58 +00003333
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003334 /* Select default pin state */
3335 pinctrl_pm_select_default_state(&pdev->dev);
3336
Fabio Estevam399db752013-07-21 13:25:03 -03003337 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303338 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3339 if (IS_ERR(fep->hwp)) {
3340 ret = PTR_ERR(fep->hwp);
3341 goto failed_ioremap;
3342 }
3343
Bryan Wue6b043d2010-03-31 02:10:44 +00003344 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003345 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003346
Sascha Haueread73182009-01-28 23:03:11 +00003347 platform_set_drvdata(pdev, ndev);
3348
Nimrod Andyde40ed32014-12-24 17:30:39 +08003349 if (of_get_property(np, "fsl,magic-packet", NULL))
3350 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3351
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003352 phy_node = of_parse_phandle(np, "phy-handle", 0);
3353 if (!phy_node && of_phy_is_fixed_link(np)) {
3354 ret = of_phy_register_fixed_link(np);
3355 if (ret < 0) {
3356 dev_err(&pdev->dev,
3357 "broken fixed-link specification\n");
3358 goto failed_phy;
3359 }
3360 phy_node = of_node_get(np);
3361 }
3362 fep->phy_node = phy_node;
3363
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003364 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003365 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003366 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003367 if (pdata)
3368 fep->phy_interface = pdata->phy;
3369 else
3370 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3371 } else {
3372 fep->phy_interface = ret;
3373 }
3374
Fabio Estevame2f8d552013-02-22 06:40:45 +00003375 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3376 if (IS_ERR(fep->clk_ipg)) {
3377 ret = PTR_ERR(fep->clk_ipg);
3378 goto failed_clk;
3379 }
3380
3381 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3382 if (IS_ERR(fep->clk_ahb)) {
3383 ret = PTR_ERR(fep->clk_ahb);
3384 goto failed_clk;
3385 }
3386
Fugang Duand851b472014-09-17 05:18:52 +08003387 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3388
Linus Torvalds38f56f32013-05-07 11:06:17 -07003389 /* enet_out is optional, depends on board */
3390 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3391 if (IS_ERR(fep->clk_enet_out))
3392 fep->clk_enet_out = NULL;
3393
Nimrod Andy91c0d982014-08-21 17:09:38 +08003394 fep->ptp_clk_on = false;
3395 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003396
3397 /* clk_ref is optional, depends on board */
3398 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3399 if (IS_ERR(fep->clk_ref))
3400 fep->clk_ref = NULL;
3401
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003402 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003403 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003404 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003405 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003406 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003407 }
3408
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003409 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003410 if (ret)
3411 goto failed_clk;
3412
Andrew Lunn8fff7552015-07-25 22:38:02 +02003413 ret = clk_prepare_enable(fep->clk_ipg);
3414 if (ret)
3415 goto failed_clk_ipg;
3416
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003417 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3418 if (!IS_ERR(fep->reg_phy)) {
3419 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003420 if (ret) {
3421 dev_err(&pdev->dev,
3422 "Failed to enable phy regulator: %d\n", ret);
3423 goto failed_regulator;
3424 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003425 } else {
3426 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003427 }
3428
Andrew Lunn8fff7552015-07-25 22:38:02 +02003429 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3430 pm_runtime_use_autosuspend(&pdev->dev);
Lucas Stach14d2b7c2015-08-03 17:50:11 +02003431 pm_runtime_get_noresume(&pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003432 pm_runtime_set_active(&pdev->dev);
3433 pm_runtime_enable(&pdev->dev);
3434
Fabio Estevame2f8d552013-02-22 06:40:45 +00003435 fec_reset_phy(pdev);
3436
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003437 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003438 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003439
3440 ret = fec_enet_init(ndev);
3441 if (ret)
3442 goto failed_init;
3443
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003444 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003445 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003446 if (irq < 0) {
3447 if (i)
3448 break;
3449 ret = irq;
3450 goto failed_irq;
3451 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003452 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003453 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003454 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003455 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003456
3457 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003458 }
3459
Fugang Duanb4d39b52014-09-13 05:00:55 +08003460 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003461 ret = fec_enet_mii_init(pdev);
3462 if (ret)
3463 goto failed_mii_init;
3464
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003465 /* Carrier starts down, phylib will bring it up */
3466 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003467 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003468 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003469
Sascha Haueread73182009-01-28 23:03:11 +00003470 ret = register_netdev(ndev);
3471 if (ret)
3472 goto failed_register;
3473
Nimrod Andyde40ed32014-12-24 17:30:39 +08003474 device_init_wakeup(&ndev->dev, fep->wol_flag &
3475 FEC_WOL_HAS_MAGIC_PACKET);
3476
Fabio Estevameb1d0642013-04-13 07:25:36 +00003477 if (fep->bufdesc_ex && fep->ptp_clock)
3478 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3479
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003480 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003481 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003482
3483 pm_runtime_mark_last_busy(&pdev->dev);
3484 pm_runtime_put_autosuspend(&pdev->dev);
3485
Sascha Haueread73182009-01-28 23:03:11 +00003486 return 0;
3487
3488failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003489 fec_enet_mii_remove(fep);
3490failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003491failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003492failed_init:
Lucas Stach32cba572015-07-23 16:06:20 +02003493 fec_ptp_stop(pdev);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003494 if (fep->reg_phy)
3495 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003496failed_regulator:
Andrew Lunn8fff7552015-07-25 22:38:02 +02003497 clk_disable_unprepare(fep->clk_ipg);
3498failed_clk_ipg:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003499 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003500failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003501failed_phy:
3502 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003503failed_ioremap:
3504 free_netdev(ndev);
3505
3506 return ret;
3507}
3508
Bill Pemberton33897cc2012-12-03 09:23:58 -05003509static int
Sascha Haueread73182009-01-28 23:03:11 +00003510fec_drv_remove(struct platform_device *pdev)
3511{
3512 struct net_device *ndev = platform_get_drvdata(pdev);
3513 struct fec_enet_private *fep = netdev_priv(ndev);
3514
Russell King36cdc742014-07-08 13:01:44 +01003515 cancel_work_sync(&fep->tx_timeout_work);
Lucas Stach32cba572015-07-23 16:06:20 +02003516 fec_ptp_stop(pdev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003517 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003518 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003519 if (fep->reg_phy)
3520 regulator_disable(fep->reg_phy);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003521 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003522 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003523
Sascha Haueread73182009-01-28 23:03:11 +00003524 return 0;
3525}
3526
Fabio Estevamdd66d382014-07-24 18:24:01 -03003527static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003528{
Eric Benard87cad5c2010-06-18 04:19:54 +00003529 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003530 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003531
Russell Kingda1774e2014-07-08 12:39:57 +01003532 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003533 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003534 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3535 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Russell Kingd76cfae2014-07-08 00:23:04 +01003536 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003537 napi_disable(&fep->napi);
3538 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003539 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003540 netif_tx_unlock_bh(ndev);
3541 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003542 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003543 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3544 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003545 }
Russell Kingda1774e2014-07-08 12:39:57 +01003546 rtnl_unlock();
3547
Nimrod Andyde40ed32014-12-24 17:30:39 +08003548 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003549 regulator_disable(fep->reg_phy);
3550
Nimrod Andy858eeb72014-12-11 09:20:31 +08003551 /* SOC supply clock to phy, when clock is disabled, phy link down
3552 * SOC control phy regulator, when regulator is disabled, phy link down
3553 */
3554 if (fep->clk_enet_out || fep->reg_phy)
3555 fep->link = 0;
3556
Sascha Haueread73182009-01-28 23:03:11 +00003557 return 0;
3558}
3559
Fabio Estevamdd66d382014-07-24 18:24:01 -03003560static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003561{
Eric Benard87cad5c2010-06-18 04:19:54 +00003562 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003563 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003564 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003565 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003566 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003567
Nimrod Andyde40ed32014-12-24 17:30:39 +08003568 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003569 ret = regulator_enable(fep->reg_phy);
3570 if (ret)
3571 return ret;
3572 }
Sascha Haueread73182009-01-28 23:03:11 +00003573
Russell Kingda1774e2014-07-08 12:39:57 +01003574 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003575 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003576 ret = fec_enet_clk_enable(ndev, true);
3577 if (ret) {
3578 rtnl_unlock();
3579 goto failed_clk;
3580 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003581 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3582 if (pdata && pdata->sleep_mode_enable)
3583 pdata->sleep_mode_enable(false);
3584 val = readl(fep->hwp + FEC_ECNTRL);
3585 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3586 writel(val, fep->hwp + FEC_ECNTRL);
3587 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3588 } else {
3589 pinctrl_pm_select_default_state(&fep->pdev->dev);
3590 }
Russell Kingef833372014-07-08 12:40:43 +01003591 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003592 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003593 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003594 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003595 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003596 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003597 }
Russell Kingda1774e2014-07-08 12:39:57 +01003598 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003599
Sascha Haueread73182009-01-28 23:03:11 +00003600 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003601
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003602failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003603 if (fep->reg_phy)
3604 regulator_disable(fep->reg_phy);
3605 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003606}
3607
Andrew Lunn8fff7552015-07-25 22:38:02 +02003608static int __maybe_unused fec_runtime_suspend(struct device *dev)
3609{
3610 struct net_device *ndev = dev_get_drvdata(dev);
3611 struct fec_enet_private *fep = netdev_priv(ndev);
3612
3613 clk_disable_unprepare(fep->clk_ipg);
3614
3615 return 0;
3616}
3617
3618static int __maybe_unused fec_runtime_resume(struct device *dev)
3619{
3620 struct net_device *ndev = dev_get_drvdata(dev);
3621 struct fec_enet_private *fep = netdev_priv(ndev);
3622
3623 return clk_prepare_enable(fep->clk_ipg);
3624}
3625
3626static const struct dev_pm_ops fec_pm_ops = {
3627 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3628 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3629};
Denis Kirjanov59d42892010-06-02 09:27:04 +00003630
Sascha Haueread73182009-01-28 23:03:11 +00003631static struct platform_driver fec_driver = {
3632 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003633 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003634 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003635 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003636 },
Shawn Guob5680e02011-01-05 21:13:13 +00003637 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003638 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003639 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003640};
3641
Fabio Estevamaaca2372012-01-23 16:44:50 +00003642module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003644MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645MODULE_LICENSE("GPL");