blob: 4d5c1ba6b64c6489b2ee2305eff4deebd42bdb2f [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>
Bryan Wue6b043d2010-03-31 02:10:44 +000051#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070052#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080053#include <linux/of.h>
54#include <linux/of_device.h>
55#include <linux/of_gpio.h>
Uwe Kleine-König407066f2014-08-11 17:35:33 +020056#include <linux/of_mdio.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080057#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000058#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010059#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030060#include <linux/pinctrl/consumer.h>
Frank Lic259c132014-10-03 14:29:14 -070061#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Greg Ungerer080853a2007-07-30 16:28:46 +100063#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christoph Müllner772e42b2013-06-27 21:18:23 +020067static void set_multicast_list(struct net_device *ndev);
Fugang Duand851b472014-09-17 05:18:52 +080068static void fec_enet_itr_coal_init(struct net_device *ndev);
Christoph Müllner772e42b2013-06-27 21:18:23 +020069
Shawn Guob5680e02011-01-05 21:13:13 +000070#define DRIVER_NAME "fec"
71
Fugang Duan4d494cd2014-09-13 05:00:48 +080072#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
73
Frank Libaa70a52013-01-16 16:55:58 +000074/* Pause frame feild and FIFO threshold */
75#define FEC_ENET_FCE (1 << 5)
76#define FEC_ENET_RSEM_V 0x84
77#define FEC_ENET_RSFL_V 16
78#define FEC_ENET_RAEM_V 0x8
79#define FEC_ENET_RAFL_V 0x8
80#define FEC_ENET_OPD_V 0xFFF0
Andrew Lunn8fff7552015-07-25 22:38:02 +020081#define FEC_MDIO_PM_TIMEOUT 100 /* ms */
Frank Libaa70a52013-01-16 16:55:58 +000082
Shawn Guob5680e02011-01-05 21:13:13 +000083static struct platform_device_id fec_devtype[] = {
84 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080085 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000086 .name = DRIVER_NAME,
87 .driver_data = 0,
88 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080089 .name = "imx25-fec",
Greg Ungerer18803492015-06-20 15:51:57 +100090 .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +080091 }, {
92 .name = "imx27-fec",
Greg Ungerer18803492015-06-20 15:51:57 +100093 .driver_data = FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +080094 }, {
Shawn Guob5680e02011-01-05 21:13:13 +000095 .name = "imx28-fec",
Stefan Agner3d125f92015-01-14 00:20:21 +010096 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
Greg Ungerer18803492015-06-20 15:51:57 +100097 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
Shawn Guo0ca1e292011-07-01 18:11:22 +080098 }, {
Shawn Guo230dec62011-09-23 02:12:48 +000099 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000100 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100101 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Greg Ungerer18803492015-06-20 15:51:57 +1000102 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
103 FEC_QUIRK_HAS_RACC,
Shawn Guo230dec62011-09-23 02:12:48 +0000104 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000105 .name = "mvf600-fec",
Greg Ungerer18803492015-06-20 15:51:57 +1000106 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
Jingchang Luca7c4a42013-04-11 21:12:45 +0000107 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800108 .name = "imx6sx-fec",
109 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
110 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800111 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
Greg Ungerer18803492015-06-20 15:51:57 +1000112 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
113 FEC_QUIRK_HAS_RACC,
Fugang Duan95a77472014-09-13 05:00:47 +0800114 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800115 /* sentinel */
116 }
Shawn Guob5680e02011-01-05 21:13:13 +0000117};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800118MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000119
Shawn Guoca2cc332011-06-25 02:04:35 +0800120enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000121 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800122 IMX27_FEC, /* runs on i.mx27/35/51 */
123 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000124 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000125 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800126 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800127};
128
129static const struct of_device_id fec_dt_ids[] = {
130 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
131 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
132 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000133 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000134 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800135 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800136 { /* sentinel */ }
137};
138MODULE_DEVICE_TABLE(of, fec_dt_ids);
139
Shawn Guo49da97d2011-01-05 21:13:11 +0000140static unsigned char macaddr[ETH_ALEN];
141module_param_array(macaddr, byte, NULL, 0);
142MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
143
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000144#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * Some hardware gets it MAC address out of local flash memory.
147 * if this is non-zero then assume it is the address to get MAC from.
148 */
149#if defined(CONFIG_NETtel)
150#define FEC_FLASHMAC 0xf0006006
151#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
152#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#elif defined(CONFIG_CANCam)
154#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000155#elif defined (CONFIG_M5272C3)
156#define FEC_FLASHMAC (0xffe04000 + 4)
157#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000158#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#else
160#define FEC_FLASHMAC 0
161#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800162#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000163
Jim Baxtercdffcf12013-07-02 22:52:56 +0100164/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100166#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100168#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jim Baxter4c09eed2013-04-19 08:10:49 +0000170/* FEC receive acceleration */
171#define FEC_RACC_IPDIS (1 << 1)
172#define FEC_RACC_PRODIS (1 << 2)
173#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Matt Waddel6b265292006-06-27 13:10:56 +1000176 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * size bits. Other FEC hardware does not, so we need to take that into
178 * account when setting it.
179 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000180#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100181 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
183#else
184#define OPT_FRAME_SIZE 0
185#endif
186
Bryan Wue6b043d2010-03-31 02:10:44 +0000187/* FEC MII MMFR bits definition */
188#define FEC_MMFR_ST (1 << 30)
189#define FEC_MMFR_OP_READ (2 << 28)
190#define FEC_MMFR_OP_WRITE (1 << 28)
191#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
192#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
193#define FEC_MMFR_TA (2 << 16)
194#define FEC_MMFR_DATA(v) (v & 0xffff)
Nimrod Andyde40ed32014-12-24 17:30:39 +0800195/* FEC ECR bits definition */
196#define FEC_ECR_MAGICEN (1 << 2)
197#define FEC_ECR_SLEEP (1 << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500199#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Sascha Hauer22f6b862009-04-15 01:32:18 +0000201/* Transmitter timeout */
202#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Frank Libaa70a52013-01-16 16:55:58 +0000204#define FEC_PAUSE_FLAG_AUTONEG 0x1
205#define FEC_PAUSE_FLAG_ENABLE 0x2
Nimrod Andyde40ed32014-12-24 17:30:39 +0800206#define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
207#define FEC_WOL_FLAG_ENABLE (0x1 << 1)
208#define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
Frank Libaa70a52013-01-16 16:55:58 +0000209
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800210#define COPYBREAK_DEFAULT 256
211
Nimrod Andy79f33912014-06-12 08:16:23 +0800212#define TSO_HEADER_SIZE 128
213/* Max number of allowed TCP segments for software TSO */
214#define FEC_MAX_TSO_SEGS 100
215#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
216
217#define IS_TSO_HEADER(txq, addr) \
218 ((addr >= txq->tso_hdrs_dma) && \
219 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
220
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000221static int mii_cnt;
222
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800223static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800224struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
225 struct fec_enet_private *fep,
226 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000227{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800228 struct bufdesc *new_bd = bdp + 1;
229 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800230 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
231 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800232 struct bufdesc_ex *ex_base;
233 struct bufdesc *base;
234 int ring_size;
235
Fugang Duan4d494cd2014-09-13 05:00:48 +0800236 if (bdp >= txq->tx_bd_base) {
237 base = txq->tx_bd_base;
238 ring_size = txq->tx_ring_size;
239 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800240 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800241 base = rxq->rx_bd_base;
242 ring_size = rxq->rx_ring_size;
243 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800244 }
245
246 if (fep->bufdesc_ex)
247 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
248 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000249 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800250 return (new_bd >= (base + ring_size)) ?
251 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000252}
253
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800254static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800255struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
256 struct fec_enet_private *fep,
257 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000258{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800259 struct bufdesc *new_bd = bdp - 1;
260 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800261 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
262 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800263 struct bufdesc_ex *ex_base;
264 struct bufdesc *base;
265 int ring_size;
266
Fugang Duan4d494cd2014-09-13 05:00:48 +0800267 if (bdp >= txq->tx_bd_base) {
268 base = txq->tx_bd_base;
269 ring_size = txq->tx_ring_size;
270 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800271 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800272 base = rxq->rx_bd_base;
273 ring_size = rxq->rx_ring_size;
274 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800275 }
276
277 if (fep->bufdesc_ex)
278 return (struct bufdesc *)((ex_new_bd < ex_base) ?
279 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000280 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800281 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000282}
283
Nimrod Andy61a44272014-06-12 08:16:18 +0800284static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
285 struct fec_enet_private *fep)
286{
287 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
288}
289
Fugang Duan4d494cd2014-09-13 05:00:48 +0800290static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
291 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800292{
293 int entries;
294
Fugang Duan4d494cd2014-09-13 05:00:48 +0800295 entries = ((const char *)txq->dirty_tx -
296 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800297
Fugang Duan4d494cd2014-09-13 05:00:48 +0800298 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800299}
300
Lothar Waßmannc20e5992014-11-17 10:51:24 +0100301static void swap_buffer(void *bufaddr, int len)
Shawn Guob5680e02011-01-05 21:13:13 +0000302{
303 int i;
304 unsigned int *buf = bufaddr;
305
Lothar Waßmann7b487d02014-11-17 10:51:23 +0100306 for (i = 0; i < len; i += 4, buf++)
Lothar Waßmanne4537892014-11-17 10:51:22 +0100307 swab32s(buf);
Shawn Guob5680e02011-01-05 21:13:13 +0000308}
309
Lothar Waßmann1310b542014-11-07 10:02:47 +0100310static void swap_buffer2(void *dst_buf, void *src_buf, int len)
311{
312 int i;
313 unsigned int *src = src_buf;
314 unsigned int *dst = dst_buf;
315
316 for (i = 0; i < len; i += 4, src++, dst++)
317 *dst = swab32p(src);
318}
319
Russell King344756f2014-07-08 13:01:59 +0100320static void fec_dump(struct net_device *ndev)
321{
322 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800323 struct bufdesc *bdp;
324 struct fec_enet_priv_tx_q *txq;
325 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100326
327 netdev_info(ndev, "TX ring dump\n");
328 pr_info("Nr SC addr len SKB\n");
329
Fugang Duan4d494cd2014-09-13 05:00:48 +0800330 txq = fep->tx_queue[0];
331 bdp = txq->tx_bd_base;
332
Russell King344756f2014-07-08 13:01:59 +0100333 do {
334 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
335 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800336 bdp == txq->cur_tx ? 'S' : ' ',
337 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100338 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800339 txq->tx_skbuff[index]);
340 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100341 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800342 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100343}
344
Fugang Duan62a02c92014-06-18 08:33:52 +0800345static inline bool is_ipv4_pkt(struct sk_buff *skb)
346{
347 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
348}
349
Jim Baxter4c09eed2013-04-19 08:10:49 +0000350static int
351fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
352{
353 /* Only run for packets requiring a checksum. */
354 if (skb->ip_summed != CHECKSUM_PARTIAL)
355 return 0;
356
357 if (unlikely(skb_cow_head(skb, 0)))
358 return -1;
359
Fugang Duan62a02c92014-06-18 08:33:52 +0800360 if (is_ipv4_pkt(skb))
361 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000362 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
363
364 return 0;
365}
366
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800367static struct bufdesc *
Fugang Duan4d494cd2014-09-13 05:00:48 +0800368fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
369 struct sk_buff *skb,
370 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800371{
372 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800373 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800374 struct bufdesc_ex *ebdp;
375 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800376 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800377 int frag, frag_len;
378 unsigned short status;
379 unsigned int estatus = 0;
380 skb_frag_t *this_frag;
381 unsigned int index;
382 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100383 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800384 int i;
385
386 for (frag = 0; frag < nr_frags; frag++) {
387 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800388 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800389 ebdp = (struct bufdesc_ex *)bdp;
390
391 status = bdp->cbd_sc;
392 status &= ~BD_ENET_TX_STATS;
393 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
394 frag_len = skb_shinfo(skb)->frags[frag].size;
395
396 /* Handle the last BD specially */
397 if (frag == nr_frags - 1) {
398 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
399 if (fep->bufdesc_ex) {
400 estatus |= BD_ENET_TX_INT;
401 if (unlikely(skb_shinfo(skb)->tx_flags &
402 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
403 estatus |= BD_ENET_TX_TS;
404 }
405 }
406
407 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100408 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800409 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800410 if (skb->ip_summed == CHECKSUM_PARTIAL)
411 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
412 ebdp->cbd_bdu = 0;
413 ebdp->cbd_esc = estatus;
414 }
415
416 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
417
Fugang Duan4d494cd2014-09-13 05:00:48 +0800418 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800419 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100420 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800421 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
422 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800423
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100424 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800425 swap_buffer(bufaddr, frag_len);
426 }
427
Russell Kingd6bf3142014-07-08 00:23:19 +0100428 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
429 DMA_TO_DEVICE);
430 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800431 dev_kfree_skb_any(skb);
432 if (net_ratelimit())
433 netdev_err(ndev, "Tx DMA memory map failed\n");
434 goto dma_mapping_error;
435 }
436
Russell Kingd6bf3142014-07-08 00:23:19 +0100437 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800438 bdp->cbd_datlen = frag_len;
439 bdp->cbd_sc = status;
440 }
441
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800442 return bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800443dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800444 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800445 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800446 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800447 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
448 bdp->cbd_datlen, DMA_TO_DEVICE);
449 }
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800450 return ERR_PTR(-ENOMEM);
Nimrod Andy6e909282014-06-12 08:16:22 +0800451}
452
Fugang Duan4d494cd2014-09-13 05:00:48 +0800453static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
454 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800455{
456 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800457 int nr_frags = skb_shinfo(skb)->nr_frags;
458 struct bufdesc *bdp, *last_bdp;
459 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100460 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800461 unsigned short status;
462 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800463 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800464 unsigned int estatus = 0;
465 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800466 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800467
Fugang Duan4d494cd2014-09-13 05:00:48 +0800468 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800469 if (entries_free < MAX_SKB_FRAGS + 1) {
470 dev_kfree_skb_any(skb);
471 if (net_ratelimit())
472 netdev_err(ndev, "NOT enough BD for SG!\n");
473 return NETDEV_TX_OK;
474 }
475
Nimrod Andy6e909282014-06-12 08:16:22 +0800476 /* Protocol checksum off-load for TCP and UDP. */
477 if (fec_enet_clear_csum(skb, ndev)) {
478 dev_kfree_skb_any(skb);
479 return NETDEV_TX_OK;
480 }
481
482 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800483 bdp = txq->cur_tx;
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800484 last_bdp = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800485 status = bdp->cbd_sc;
486 status &= ~BD_ENET_TX_STATS;
487
488 /* Set buffer length and buffer pointer */
489 bufaddr = skb->data;
490 buflen = skb_headlen(skb);
491
Fugang Duan4d494cd2014-09-13 05:00:48 +0800492 queue = skb_get_queue_mapping(skb);
493 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800494 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100495 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800496 memcpy(txq->tx_bounce[index], skb->data, buflen);
497 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800498
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100499 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800500 swap_buffer(bufaddr, buflen);
501 }
502
Russell Kingd6bf3142014-07-08 00:23:19 +0100503 /* Push the data cache so the CPM does not get stale memory data. */
504 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
505 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800506 dev_kfree_skb_any(skb);
507 if (net_ratelimit())
508 netdev_err(ndev, "Tx DMA memory map failed\n");
509 return NETDEV_TX_OK;
510 }
511
512 if (nr_frags) {
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800513 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
514 if (IS_ERR(last_bdp))
515 return NETDEV_TX_OK;
Nimrod Andy6e909282014-06-12 08:16:22 +0800516 } else {
517 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
518 if (fep->bufdesc_ex) {
519 estatus = BD_ENET_TX_INT;
520 if (unlikely(skb_shinfo(skb)->tx_flags &
521 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
522 estatus |= BD_ENET_TX_TS;
523 }
524 }
525
526 if (fep->bufdesc_ex) {
527
528 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
529
530 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
531 fep->hwts_tx_en))
532 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
533
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100534 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800535 estatus |= FEC_TX_BD_FTYPE(queue);
536
Nimrod Andy6e909282014-06-12 08:16:22 +0800537 if (skb->ip_summed == CHECKSUM_PARTIAL)
538 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
539
540 ebdp->cbd_bdu = 0;
541 ebdp->cbd_esc = estatus;
542 }
543
Fugang Duan4d494cd2014-09-13 05:00:48 +0800544 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800545 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800546 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800547
548 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100549 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800550
551 /* Send it on its way. Tell FEC it's ready, interrupt when done,
552 * it's the last BD of the frame, and to put the CRC on the end.
553 */
554 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
555 bdp->cbd_sc = status;
556
Sascha Hauer22f6b862009-04-15 01:32:18 +0000557 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800558 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800560 skb_tx_timestamp(skb);
561
Kevin Haoc4bc44c2015-08-07 13:52:37 +0800562 /* Make sure the update to bdp and tx_skbuff are performed before
563 * cur_tx.
564 */
565 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +0800566 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Frank Lide5fb0a2013-03-03 17:34:25 +0000568 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800569 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000570
Nimrod Andy6e909282014-06-12 08:16:22 +0800571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Nimrod Andy79f33912014-06-12 08:16:23 +0800574static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800575fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
576 struct net_device *ndev,
577 struct bufdesc *bdp, int index, char *data,
578 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800579{
580 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200581 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800582 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800583 unsigned short status;
584 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100585 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800586
587 status = bdp->cbd_sc;
588 status &= ~BD_ENET_TX_STATS;
589
590 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800591
Fugang Duan41ef84c2014-09-13 05:00:54 +0800592 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100593 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800594 memcpy(txq->tx_bounce[index], data, size);
595 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800596
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100597 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800598 swap_buffer(data, size);
599 }
600
Russell Kingd6bf3142014-07-08 00:23:19 +0100601 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
602 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800603 dev_kfree_skb_any(skb);
604 if (net_ratelimit())
605 netdev_err(ndev, "Tx DMA memory map failed\n");
606 return NETDEV_TX_BUSY;
607 }
608
Russell Kingd6bf3142014-07-08 00:23:19 +0100609 bdp->cbd_datlen = size;
610 bdp->cbd_bufaddr = addr;
611
Nimrod Andy79f33912014-06-12 08:16:23 +0800612 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100613 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800614 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800615 if (skb->ip_summed == CHECKSUM_PARTIAL)
616 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
617 ebdp->cbd_bdu = 0;
618 ebdp->cbd_esc = estatus;
619 }
620
621 /* Handle the last BD specially */
622 if (last_tcp)
623 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
624 if (is_last) {
625 status |= BD_ENET_TX_INTR;
626 if (fep->bufdesc_ex)
627 ebdp->cbd_esc |= BD_ENET_TX_INT;
628 }
629
630 bdp->cbd_sc = status;
631
632 return 0;
633}
634
635static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800636fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
637 struct sk_buff *skb, struct net_device *ndev,
638 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800639{
640 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800641 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200642 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800643 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800644 void *bufaddr;
645 unsigned long dmabuf;
646 unsigned short status;
647 unsigned int estatus = 0;
648
649 status = bdp->cbd_sc;
650 status &= ~BD_ENET_TX_STATS;
651 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
652
Fugang Duan4d494cd2014-09-13 05:00:48 +0800653 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
654 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800655 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100656 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800657 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
658 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800659
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100660 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800661 swap_buffer(bufaddr, hdr_len);
662
663 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
664 hdr_len, DMA_TO_DEVICE);
665 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
666 dev_kfree_skb_any(skb);
667 if (net_ratelimit())
668 netdev_err(ndev, "Tx DMA memory map failed\n");
669 return NETDEV_TX_BUSY;
670 }
671 }
672
673 bdp->cbd_bufaddr = dmabuf;
674 bdp->cbd_datlen = hdr_len;
675
676 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100677 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800678 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800679 if (skb->ip_summed == CHECKSUM_PARTIAL)
680 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
681 ebdp->cbd_bdu = 0;
682 ebdp->cbd_esc = estatus;
683 }
684
685 bdp->cbd_sc = status;
686
687 return 0;
688}
689
Fugang Duan4d494cd2014-09-13 05:00:48 +0800690static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
691 struct sk_buff *skb,
692 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800693{
694 struct fec_enet_private *fep = netdev_priv(ndev);
695 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
696 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800697 struct bufdesc *bdp = txq->cur_tx;
698 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800699 struct tso_t tso;
700 unsigned int index = 0;
701 int ret;
702
Fugang Duan4d494cd2014-09-13 05:00:48 +0800703 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800704 dev_kfree_skb_any(skb);
705 if (net_ratelimit())
706 netdev_err(ndev, "NOT enough BD for TSO!\n");
707 return NETDEV_TX_OK;
708 }
709
710 /* Protocol checksum off-load for TCP and UDP. */
711 if (fec_enet_clear_csum(skb, ndev)) {
712 dev_kfree_skb_any(skb);
713 return NETDEV_TX_OK;
714 }
715
716 /* Initialize the TSO handler, and prepare the first payload */
717 tso_start(skb, &tso);
718
719 total_len = skb->len - hdr_len;
720 while (total_len > 0) {
721 char *hdr;
722
Fugang Duan4d494cd2014-09-13 05:00:48 +0800723 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800724 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
725 total_len -= data_left;
726
727 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800728 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800729 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800730 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800731 if (ret)
732 goto err_release;
733
734 while (data_left > 0) {
735 int size;
736
737 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800738 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
739 index = fec_enet_get_bd_index(txq->tx_bd_base,
740 bdp, fep);
741 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
742 bdp, index,
743 tso.data, size,
744 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800745 total_len == 0);
746 if (ret)
747 goto err_release;
748
749 data_left -= size;
750 tso_build_data(skb, &tso, size);
751 }
752
Fugang Duan4d494cd2014-09-13 05:00:48 +0800753 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800754 }
755
756 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800757 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800758
Nimrod Andy79f33912014-06-12 08:16:23 +0800759 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800760 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800761
762 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100763 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Fugang Duan37d60172014-09-17 05:18:54 +0800764 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
765 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
766 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
767 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
768 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800769
770 return 0;
771
772err_release:
773 /* TODO: Release all used data descriptors for TSO */
774 return ret;
775}
776
Nimrod Andy61a44272014-06-12 08:16:18 +0800777static netdev_tx_t
778fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
779{
780 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800781 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800782 unsigned short queue;
783 struct fec_enet_priv_tx_q *txq;
784 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800785 int ret;
786
Fugang Duan4d494cd2014-09-13 05:00:48 +0800787 queue = skb_get_queue_mapping(skb);
788 txq = fep->tx_queue[queue];
789 nq = netdev_get_tx_queue(ndev, queue);
790
Nimrod Andy79f33912014-06-12 08:16:23 +0800791 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800792 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800793 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800794 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800795 if (ret)
796 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800797
Fugang Duan4d494cd2014-09-13 05:00:48 +0800798 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
799 if (entries_free <= txq->tx_stop_threshold)
800 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800801
802 return NETDEV_TX_OK;
803}
804
Frank Li14109a52013-03-26 16:12:03 +0000805/* Init RX & TX buffer descriptors
806 */
807static void fec_enet_bd_init(struct net_device *dev)
808{
809 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800810 struct fec_enet_priv_tx_q *txq;
811 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000812 struct bufdesc *bdp;
813 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800814 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000815
Frank Li59d0f7462014-09-13 05:00:50 +0800816 for (q = 0; q < fep->num_rx_queues; q++) {
817 /* Initialize the receive buffer descriptors. */
818 rxq = fep->rx_queue[q];
819 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800820
Frank Li59d0f7462014-09-13 05:00:50 +0800821 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000822
Frank Li59d0f7462014-09-13 05:00:50 +0800823 /* Initialize the BD for every fragment in the page. */
824 if (bdp->cbd_bufaddr)
825 bdp->cbd_sc = BD_ENET_RX_EMPTY;
826 else
827 bdp->cbd_sc = 0;
828 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000829 }
Frank Li59d0f7462014-09-13 05:00:50 +0800830
831 /* Set the last buffer to wrap */
832 bdp = fec_enet_get_prevdesc(bdp, fep, q);
833 bdp->cbd_sc |= BD_SC_WRAP;
834
835 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000836 }
837
Frank Li59d0f7462014-09-13 05:00:50 +0800838 for (q = 0; q < fep->num_tx_queues; q++) {
839 /* ...and the same for transmit */
840 txq = fep->tx_queue[q];
841 bdp = txq->tx_bd_base;
842 txq->cur_tx = bdp;
843
844 for (i = 0; i < txq->tx_ring_size; i++) {
845 /* Initialize the BD for every fragment in the page. */
846 bdp->cbd_sc = 0;
847 if (txq->tx_skbuff[i]) {
848 dev_kfree_skb_any(txq->tx_skbuff[i]);
849 txq->tx_skbuff[i] = NULL;
850 }
851 bdp->cbd_bufaddr = 0;
852 bdp = fec_enet_get_nextdesc(bdp, fep, q);
853 }
854
855 /* Set the last buffer to wrap */
856 bdp = fec_enet_get_prevdesc(bdp, fep, q);
857 bdp->cbd_sc |= BD_SC_WRAP;
858 txq->dirty_tx = bdp;
859 }
860}
861
Frank Lice99d0d2014-09-13 05:00:52 +0800862static void fec_enet_active_rxring(struct net_device *ndev)
863{
864 struct fec_enet_private *fep = netdev_priv(ndev);
865 int i;
866
867 for (i = 0; i < fep->num_rx_queues; i++)
868 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
869}
870
Frank Li59d0f7462014-09-13 05:00:50 +0800871static void fec_enet_enable_ring(struct net_device *ndev)
872{
873 struct fec_enet_private *fep = netdev_priv(ndev);
874 struct fec_enet_priv_tx_q *txq;
875 struct fec_enet_priv_rx_q *rxq;
876 int i;
877
878 for (i = 0; i < fep->num_rx_queues; i++) {
879 rxq = fep->rx_queue[i];
880 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
Nimrod Andyd543a762014-11-23 17:23:06 +0800881 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800882
883 /* enable DMA1/2 */
884 if (i)
885 writel(RCMR_MATCHEN | RCMR_CMP(i),
886 fep->hwp + FEC_RCMR(i));
887 }
888
889 for (i = 0; i < fep->num_tx_queues; i++) {
890 txq = fep->tx_queue[i];
891 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
892
893 /* enable DMA1/2 */
894 if (i)
895 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
896 fep->hwp + FEC_DMA_CFG(i));
897 }
898}
899
900static void fec_enet_reset_skb(struct net_device *ndev)
901{
902 struct fec_enet_private *fep = netdev_priv(ndev);
903 struct fec_enet_priv_tx_q *txq;
904 int i, j;
905
906 for (i = 0; i < fep->num_tx_queues; i++) {
907 txq = fep->tx_queue[i];
908
909 for (j = 0; j < txq->tx_ring_size; j++) {
910 if (txq->tx_skbuff[j]) {
911 dev_kfree_skb_any(txq->tx_skbuff[j]);
912 txq->tx_skbuff[j] = NULL;
913 }
914 }
915 }
Frank Li14109a52013-03-26 16:12:03 +0000916}
917
Russell Kingdbc64a82014-07-08 12:40:12 +0100918/*
919 * This function is called to start or restart the FEC during a link
920 * change, transmit timeout, or to reconfigure the FEC. The network
921 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100922 */
923static void
Russell Kingef833372014-07-08 12:40:43 +0100924fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100925{
926 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000927 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100928 u32 temp_mac[2];
929 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000930 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100931
Fugang Duan106c3142014-09-13 05:00:51 +0800932 /* Whack a reset. We should wait for this.
933 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
934 * instead of reset MAC itself.
935 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100936 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800937 writel(0, fep->hwp + FEC_ECNTRL);
938 } else {
939 writel(1, fep->hwp + FEC_ECNTRL);
940 udelay(10);
941 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100942
943 /*
944 * enet-mac reset will reset mac address registers too,
945 * so need to reconfigure it.
946 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100947 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100948 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
949 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
950 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
951 }
952
953 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800954 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100955
Frank Li14109a52013-03-26 16:12:03 +0000956 fec_enet_bd_init(ndev);
957
Frank Li59d0f7462014-09-13 05:00:50 +0800958 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100959
Frank Li59d0f7462014-09-13 05:00:50 +0800960 /* Reset tx SKB buffers. */
961 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100962
963 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100964 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100965 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100966 writel(0x04, fep->hwp + FEC_X_CNTRL);
967 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100968 /* No Rcv on Xmit */
969 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100970 writel(0x0, fep->hwp + FEC_X_CNTRL);
971 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100972
Uwe Kleine-König45993652011-01-19 20:47:04 +0100973 /* Set MII speed */
974 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
975
Guenter Roeckd1391932013-06-18 10:04:59 -0700976#if !defined(CONFIG_M5272)
Greg Ungerer18803492015-06-20 15:51:57 +1000977 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
978 /* set RX checksum */
979 val = readl(fep->hwp + FEC_RACC);
980 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
981 val |= FEC_RACC_OPTIONS;
982 else
983 val &= ~FEC_RACC_OPTIONS;
984 writel(val, fep->hwp + FEC_RACC);
985 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700986#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000987
Uwe Kleine-König45993652011-01-19 20:47:04 +0100988 /*
989 * The phy interface and speed need to get configured
990 * differently on enet-mac.
991 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100992 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100993 /* Enable flow control and length check */
994 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100995
Shawn Guo230dec62011-09-23 02:12:48 +0000996 /* RGMII, RMII or MII */
Markus Pargmanne813bb22015-04-30 17:07:50 +0200997 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
998 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
999 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1000 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shawn Guo230dec62011-09-23 02:12:48 +00001001 rcntl |= (1 << 6);
1002 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001003 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001004 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001005 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001006
Shawn Guo230dec62011-09-23 02:12:48 +00001007 /* 1G, 100M or 10M */
1008 if (fep->phy_dev) {
1009 if (fep->phy_dev->speed == SPEED_1000)
1010 ecntl |= (1 << 5);
1011 else if (fep->phy_dev->speed == SPEED_100)
1012 rcntl &= ~(1 << 9);
1013 else
1014 rcntl |= (1 << 9);
1015 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001016 } else {
1017#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001018 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001019 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020 /* disable the gasket and wait */
1021 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1022 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1023 udelay(1);
1024
1025 /*
1026 * configure the gasket:
1027 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001028 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001029 */
Eric Benard8d82f212012-01-12 06:10:28 +00001030 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1031 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1032 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1033 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1034 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001035
1036 /* re-enable the gasket */
1037 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1038 }
1039#endif
1040 }
Frank Libaa70a52013-01-16 16:55:58 +00001041
Guenter Roeckd1391932013-06-18 10:04:59 -07001042#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001043 /* enable pause frame*/
1044 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1045 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1046 fep->phy_dev && fep->phy_dev->pause)) {
1047 rcntl |= FEC_ENET_FCE;
1048
Jim Baxter4c09eed2013-04-19 08:10:49 +00001049 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001050 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1051 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1052 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1053 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1054
1055 /* OPD */
1056 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1057 } else {
1058 rcntl &= ~FEC_ENET_FCE;
1059 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001060#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001061
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001062 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001063
Stefan Wahren84fe6182014-03-12 11:28:19 +01001064 /* Setup multicast filter. */
1065 set_multicast_list(ndev);
1066#ifndef CONFIG_M5272
1067 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1068 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1069#endif
1070
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001071 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001072 /* enable ENET endian swap */
1073 ecntl |= (1 << 8);
1074 /* enable ENET store and forward mode */
1075 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1076 }
1077
Frank Liff43da82013-01-03 16:04:23 +00001078 if (fep->bufdesc_ex)
1079 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001080
Chris Healy38ae92d2013-06-25 23:18:52 -07001081#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001082 /* Enable the MIB statistic event counters */
1083 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001084#endif
1085
Uwe Kleine-König45993652011-01-19 20:47:04 +01001086 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001087 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001088 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001089
Frank Liff43da82013-01-03 16:04:23 +00001090 if (fep->bufdesc_ex)
1091 fec_ptp_start_cyclecounter(ndev);
1092
Uwe Kleine-König45993652011-01-19 20:47:04 +01001093 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001094 if (fep->link)
1095 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1096 else
1097 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001098
1099 /* Init the interrupt coalescing */
1100 fec_enet_itr_coal_init(ndev);
1101
Uwe Kleine-König45993652011-01-19 20:47:04 +01001102}
1103
1104static void
1105fec_stop(struct net_device *ndev)
1106{
1107 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001108 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001109 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001110 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001111
1112 /* We cannot expect a graceful transmit stop without link !!! */
1113 if (fep->link) {
1114 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1115 udelay(10);
1116 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001117 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001118 }
1119
Fugang Duan106c3142014-09-13 05:00:51 +08001120 /* Whack a reset. We should wait for this.
1121 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1122 * instead of reset MAC itself.
1123 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001124 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1125 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1126 writel(0, fep->hwp + FEC_ECNTRL);
1127 } else {
1128 writel(1, fep->hwp + FEC_ECNTRL);
1129 udelay(10);
1130 }
1131 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001132 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001133 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1134 val = readl(fep->hwp + FEC_ECNTRL);
1135 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1136 writel(val, fep->hwp + FEC_ECNTRL);
1137
1138 if (pdata && pdata->sleep_mode_enable)
1139 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001140 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001141 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001142
1143 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001144 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1145 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001146 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001147 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1148 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001149}
1150
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001153fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001155 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Russell King344756f2014-07-08 13:01:59 +01001157 fec_dump(ndev);
1158
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001159 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Russell King36cdc742014-07-08 13:01:44 +01001161 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001162}
1163
Russell King36cdc742014-07-08 13:01:44 +01001164static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001165{
1166 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001167 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001168 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001169
Russell King36cdc742014-07-08 13:01:44 +01001170 rtnl_lock();
1171 if (netif_device_present(ndev) || netif_running(ndev)) {
1172 napi_disable(&fep->napi);
1173 netif_tx_lock_bh(ndev);
1174 fec_restart(ndev);
1175 netif_wake_queue(ndev);
1176 netif_tx_unlock_bh(ndev);
1177 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001178 }
Russell King36cdc742014-07-08 13:01:44 +01001179 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001183fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1184 struct skb_shared_hwtstamps *hwtstamps)
1185{
1186 unsigned long flags;
1187 u64 ns;
1188
1189 spin_lock_irqsave(&fep->tmreg_lock, flags);
1190 ns = timecounter_cyc2time(&fep->tc, ts);
1191 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1192
1193 memset(hwtstamps, 0, sizeof(*hwtstamps));
1194 hwtstamps->hwtstamp = ns_to_ktime(ns);
1195}
1196
1197static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001198fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct fec_enet_private *fep;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001201 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001202 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001204 struct fec_enet_priv_tx_q *txq;
1205 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001206 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001207 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001209 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001210
1211 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1212
1213 txq = fep->tx_queue[queue_id];
1214 /* get next bdp of dirty_tx */
1215 nq = netdev_get_tx_queue(ndev, queue_id);
1216 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Frank Lide5fb0a2013-03-03 17:34:25 +00001218 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001219 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001220
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001221 while (bdp != READ_ONCE(txq->cur_tx)) {
1222 /* Order the load of cur_tx and cbd_sc */
1223 rmb();
1224 status = READ_ONCE(bdp->cbd_sc);
1225 if (status & BD_ENET_TX_READY)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001226 break;
1227
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001228 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy2b995f62015-01-20 14:10:35 +08001229
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001230 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001231 txq->tx_skbuff[index] = NULL;
Fabio Estevama2fe37b2015-03-13 14:07:54 -03001232 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
1233 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1234 bdp->cbd_datlen, DMA_TO_DEVICE);
1235 bdp->cbd_bufaddr = 0;
1236 if (!skb) {
1237 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1238 continue;
1239 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001242 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 BD_ENET_TX_RL | BD_ENET_TX_UN |
1244 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001245 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001246 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001248 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001250 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001251 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001252 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001253 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001254 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001255 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001257 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001258 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Frank Liff43da82013-01-03 16:04:23 +00001261 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1262 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001263 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001264 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001265
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001266 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001267 skb_tstamp_tx(skb, &shhwtstamps);
1268 }
Frank Liff43da82013-01-03 16:04:23 +00001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* Deferred means some collisions occurred during transmit,
1271 * but we eventually sent the packet OK.
1272 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001273 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001274 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001275
Sascha Hauer22f6b862009-04-15 01:32:18 +00001276 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001278
Kevin Haoc4bc44c2015-08-07 13:52:37 +08001279 /* Make sure the update to bdp and tx_skbuff are performed
1280 * before dirty_tx
1281 */
1282 wmb();
Fugang Duan4d494cd2014-09-13 05:00:48 +08001283 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001284
Sascha Hauer22f6b862009-04-15 01:32:18 +00001285 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001286 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001287
Sascha Hauer22f6b862009-04-15 01:32:18 +00001288 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001290 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001291 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1292 if (entries_free >= txq->tx_wake_threshold)
1293 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Russell Kingccea2962014-07-08 13:01:38 +01001296
1297 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001298 if (bdp != txq->cur_tx &&
1299 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1300 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1301}
1302
1303static void
1304fec_enet_tx(struct net_device *ndev)
1305{
1306 struct fec_enet_private *fep = netdev_priv(ndev);
1307 u16 queue_id;
1308 /* First process class A queue, then Class B and Best Effort queue */
1309 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1310 clear_bit(queue_id, &fep->work_tx);
1311 fec_enet_tx_queue(ndev, queue_id);
1312 }
1313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001316static int
1317fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1318{
1319 struct fec_enet_private *fep = netdev_priv(ndev);
1320 int off;
1321
1322 off = ((unsigned long)skb->data) & fep->rx_align;
1323 if (off)
1324 skb_reserve(skb, fep->rx_align + 1 - off);
1325
1326 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1327 FEC_ENET_RX_FRSIZE - fep->rx_align,
1328 DMA_FROM_DEVICE);
1329 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1330 if (net_ratelimit())
1331 netdev_err(ndev, "Rx DMA memory map failed\n");
1332 return -ENOMEM;
1333 }
1334
1335 return 0;
1336}
1337
1338static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001339 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001340{
1341 struct fec_enet_private *fep = netdev_priv(ndev);
1342 struct sk_buff *new_skb;
1343
1344 if (length > fep->rx_copybreak)
1345 return false;
1346
1347 new_skb = netdev_alloc_skb(ndev, length);
1348 if (!new_skb)
1349 return false;
1350
1351 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1352 FEC_ENET_RX_FRSIZE - fep->rx_align,
1353 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001354 if (!swap)
1355 memcpy(new_skb->data, (*skb)->data, length);
1356 else
1357 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001358 *skb = new_skb;
1359
1360 return true;
1361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/* During a receive, the cur_rx points to the current incoming buffer.
1364 * When we update through the ring, if the next incoming buffer has
1365 * not been given to the system, we just set the empty indicator,
1366 * effectively tossing the packet.
1367 */
Frank Lidc975382013-01-28 18:31:42 +00001368static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001369fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001371 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001372 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001373 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001374 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001375 struct sk_buff *skb_new = NULL;
1376 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ushort pkt_len;
1378 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001379 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001380 struct bufdesc_ex *ebdp = NULL;
1381 bool vlan_packet_rcvd = false;
1382 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001383 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001384 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001385 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001386
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001387#ifdef CONFIG_M532x
1388 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001389#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001390 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1391 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /* First, grab all of the stats for the incoming packet.
1394 * These get messed up if we get called due to a busy condition.
1395 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001396 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Sascha Hauer22f6b862009-04-15 01:32:18 +00001398 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Frank Lidc975382013-01-28 18:31:42 +00001400 if (pkt_received >= budget)
1401 break;
1402 pkt_received++;
1403
Sascha Hauer22f6b862009-04-15 01:32:18 +00001404 /* Since we have allocated space to hold a complete frame,
1405 * the last indicator should be set.
1406 */
1407 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001408 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Russell Kingdb3421c2014-07-08 13:01:49 +01001410
Sascha Hauer22f6b862009-04-15 01:32:18 +00001411 /* Check for errors. */
1412 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001414 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001415 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1416 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001417 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001418 }
1419 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001420 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001421 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001422 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001423 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001424 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001426
1427 /* Report late collisions as a frame error.
1428 * On this error, the BD is closed, but we don't know what we
1429 * have in the buffer. So, just drop this frame on the floor.
1430 */
1431 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001432 ndev->stats.rx_errors++;
1433 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001434 goto rx_processing_done;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Sascha Hauer22f6b862009-04-15 01:32:18 +00001437 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001438 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001439 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001440 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Fugang Duan4d494cd2014-09-13 05:00:48 +08001442 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001443 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001444
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001445 /* The packet length includes FCS, but we don't want to
1446 * include that when passing upstream as it messes up
1447 * bridging applications.
1448 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001449 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1450 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001451 if (!is_copybreak) {
1452 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1453 if (unlikely(!skb_new)) {
1454 ndev->stats.rx_dropped++;
1455 goto rx_processing_done;
1456 }
1457 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1458 FEC_ENET_RX_FRSIZE - fep->rx_align,
1459 DMA_FROM_DEVICE);
1460 }
1461
1462 prefetch(skb->data - NET_IP_ALIGN);
1463 skb_put(skb, pkt_len - 4);
1464 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001465 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001466 swap_buffer(data, pkt_len);
1467
Jim Baxtercdffcf12013-07-02 22:52:56 +01001468 /* Extract the enhanced buffer descriptor */
1469 ebdp = NULL;
1470 if (fep->bufdesc_ex)
1471 ebdp = (struct bufdesc_ex *)bdp;
1472
1473 /* If this is a VLAN packet remove the VLAN Tag */
1474 vlan_packet_rcvd = false;
1475 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001476 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001477 /* Push and remove the vlan tag */
1478 struct vlan_hdr *vlan_header =
1479 (struct vlan_hdr *) (data + ETH_HLEN);
1480 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001481
1482 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001483
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001484 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001485 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001486 }
1487
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001488 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001490 /* Get receive timestamp from the skb */
1491 if (fep->hwts_rx_en && fep->bufdesc_ex)
1492 fec_enet_hwtstamp(fep, ebdp->ts,
1493 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001494
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001495 if (fep->bufdesc_ex &&
1496 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1497 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1498 /* don't check it */
1499 skb->ip_summed = CHECKSUM_UNNECESSARY;
1500 } else {
1501 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001502 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001503 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001504
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001505 /* Handle received VLAN packets */
1506 if (vlan_packet_rcvd)
1507 __vlan_hwaccel_put_tag(skb,
1508 htons(ETH_P_8021Q),
1509 vlan_tag);
1510
1511 napi_gro_receive(&fep->napi, skb);
1512
1513 if (is_copybreak) {
1514 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1515 FEC_ENET_RX_FRSIZE - fep->rx_align,
1516 DMA_FROM_DEVICE);
1517 } else {
1518 rxq->rx_skbuff[index] = skb_new;
1519 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1520 }
1521
Sascha Hauer22f6b862009-04-15 01:32:18 +00001522rx_processing_done:
1523 /* Clear the status flags for this buffer */
1524 status &= ~BD_ENET_RX_STATS;
1525
1526 /* Mark the buffer empty */
1527 status |= BD_ENET_RX_EMPTY;
1528 bdp->cbd_sc = status;
1529
Frank Liff43da82013-01-03 16:04:23 +00001530 if (fep->bufdesc_ex) {
1531 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1532
1533 ebdp->cbd_esc = BD_ENET_RX_INT;
1534 ebdp->cbd_prot = 0;
1535 ebdp->cbd_bdu = 0;
1536 }
Frank Li6605b732012-10-30 18:25:31 +00001537
Sascha Hauer22f6b862009-04-15 01:32:18 +00001538 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001539 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001540
Sascha Hauer22f6b862009-04-15 01:32:18 +00001541 /* Doing this here will keep the FEC running while we process
1542 * incoming frames. On a heavily loaded network, we should be
1543 * able to keep up at the expense of system resources.
1544 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001545 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001547 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001548 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Fugang Duan4d494cd2014-09-13 05:00:48 +08001551static int
1552fec_enet_rx(struct net_device *ndev, int budget)
1553{
1554 int pkt_received = 0;
1555 u16 queue_id;
1556 struct fec_enet_private *fep = netdev_priv(ndev);
1557
1558 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1559 clear_bit(queue_id, &fep->work_rx);
1560 pkt_received += fec_enet_rx_queue(ndev,
1561 budget - pkt_received, queue_id);
1562 }
1563 return pkt_received;
1564}
1565
1566static bool
1567fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1568{
1569 if (int_events == 0)
1570 return false;
1571
1572 if (int_events & FEC_ENET_RXF)
1573 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001574 if (int_events & FEC_ENET_RXF_1)
1575 fep->work_rx |= (1 << 0);
1576 if (int_events & FEC_ENET_RXF_2)
1577 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001578
1579 if (int_events & FEC_ENET_TXF)
1580 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001581 if (int_events & FEC_ENET_TXF_1)
1582 fep->work_tx |= (1 << 0);
1583 if (int_events & FEC_ENET_TXF_2)
1584 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001585
1586 return true;
1587}
1588
Uwe Kleine-König45993652011-01-19 20:47:04 +01001589static irqreturn_t
1590fec_enet_interrupt(int irq, void *dev_id)
1591{
1592 struct net_device *ndev = dev_id;
1593 struct fec_enet_private *fep = netdev_priv(ndev);
1594 uint int_events;
1595 irqreturn_t ret = IRQ_NONE;
1596
Russell King7a168072014-07-08 00:22:44 +01001597 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001598 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001599 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001600
Fugang Duan61615cd2015-03-04 07:52:11 +08001601 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001602 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001603
Nimrod Andy94191fd2014-12-16 18:25:58 +08001604 if (napi_schedule_prep(&fep->napi)) {
1605 /* Disable the NAPI interrupts */
1606 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1607 __napi_schedule(&fep->napi);
1608 }
Russell King7a168072014-07-08 00:22:44 +01001609 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001610
Russell King7a168072014-07-08 00:22:44 +01001611 if (int_events & FEC_ENET_MII) {
1612 ret = IRQ_HANDLED;
1613 complete(&fep->mdio_done);
1614 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001615
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001616 if (fep->ptp_clock)
1617 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001618
Uwe Kleine-König45993652011-01-19 20:47:04 +01001619 return ret;
1620}
1621
Frank Lidc975382013-01-28 18:31:42 +00001622static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1623{
1624 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001625 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001626 int pkts;
1627
Russell King7a168072014-07-08 00:22:44 +01001628 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001629
Frank Lide5fb0a2013-03-03 17:34:25 +00001630 fec_enet_tx(ndev);
1631
Frank Lidc975382013-01-28 18:31:42 +00001632 if (pkts < budget) {
1633 napi_complete(napi);
1634 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1635 }
1636 return pkts;
1637}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001640static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001642 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001643 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001644 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Shawn Guo49da97d2011-01-05 21:13:11 +00001646 /*
1647 * try to get mac address in following order:
1648 *
1649 * 1) module parameter via kernel command line in form
1650 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1651 */
1652 iap = macaddr;
1653
1654 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001655 * 2) from device tree data
1656 */
1657 if (!is_valid_ether_addr(iap)) {
1658 struct device_node *np = fep->pdev->dev.of_node;
1659 if (np) {
1660 const char *mac = of_get_mac_address(np);
1661 if (mac)
1662 iap = (unsigned char *) mac;
1663 }
1664 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001665
1666 /*
1667 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001668 */
1669 if (!is_valid_ether_addr(iap)) {
1670#ifdef CONFIG_M5272
1671 if (FEC_FLASHMAC)
1672 iap = (unsigned char *)FEC_FLASHMAC;
1673#else
1674 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001675 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001676#endif
1677 }
1678
1679 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001680 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001681 */
1682 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001683 *((__be32 *) &tmpaddr[0]) =
1684 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1685 *((__be16 *) &tmpaddr[4]) =
1686 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 iap = &tmpaddr[0];
1688 }
1689
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001690 /*
1691 * 5) random mac address
1692 */
1693 if (!is_valid_ether_addr(iap)) {
1694 /* Report it and use a random ethernet address instead */
1695 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1696 eth_hw_addr_random(ndev);
1697 netdev_info(ndev, "Using random MAC address: %pM\n",
1698 ndev->dev_addr);
1699 return;
1700 }
1701
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001702 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Shawn Guo49da97d2011-01-05 21:13:11 +00001704 /* Adjust MAC if using macaddr */
1705 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001706 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709/* ------------------------------------------------------------------------- */
1710
Bryan Wue6b043d2010-03-31 02:10:44 +00001711/*
1712 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001714static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001716 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001717 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001718 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Bryan Wue6b043d2010-03-31 02:10:44 +00001720 /* Prevent a state halted on mii error */
1721 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1722 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001725
Russell King8ce56242014-07-08 12:40:07 +01001726 /*
1727 * If the netdev is down, or is going down, we're not interested
1728 * in link state events, so just mark our idea of the link as down
1729 * and ignore the event.
1730 */
1731 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1732 fep->link = 0;
1733 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001734 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001735 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001736 status_change = 1;
1737 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001738
Russell Kingef833372014-07-08 12:40:43 +01001739 if (fep->full_duplex != phy_dev->duplex) {
1740 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001741 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001742 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001743
1744 if (phy_dev->speed != fep->speed) {
1745 fep->speed = phy_dev->speed;
1746 status_change = 1;
1747 }
1748
1749 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001750 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001751 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001752 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001753 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001754 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001755 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001756 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001757 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001758 } else {
1759 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001760 napi_disable(&fep->napi);
1761 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001762 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001763 netif_tx_unlock_bh(ndev);
1764 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001765 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001766 status_change = 1;
1767 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001768 }
1769
Bryan Wue6b043d2010-03-31 02:10:44 +00001770 if (status_change)
1771 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Bryan Wue6b043d2010-03-31 02:10:44 +00001774static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Bryan Wue6b043d2010-03-31 02:10:44 +00001776 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001777 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001778 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001779 int ret = 0;
1780
1781 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001782 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001783 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001784
1785 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001786 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001787
1788 /* start a read op */
1789 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1790 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1791 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1792
1793 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001794 time_left = wait_for_completion_timeout(&fep->mdio_done,
1795 usecs_to_jiffies(FEC_MII_TIMEOUT));
1796 if (time_left == 0) {
1797 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001798 netdev_err(fep->netdev, "MDIO read timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001799 ret = -ETIMEDOUT;
1800 goto out;
Bryan Wue6b043d2010-03-31 02:10:44 +00001801 }
1802
Andrew Lunn8fff7552015-07-25 22:38:02 +02001803 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1804
1805out:
1806 pm_runtime_mark_last_busy(dev);
1807 pm_runtime_put_autosuspend(dev);
1808
1809 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001810}
1811
1812static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1813 u16 value)
1814{
1815 struct fec_enet_private *fep = bus->priv;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001816 struct device *dev = &fep->pdev->dev;
Baruch Siach97b72e42010-07-11 21:12:51 +00001817 unsigned long time_left;
Andrew Lunn8fff7552015-07-25 22:38:02 +02001818 int ret = 0;
1819
1820 ret = pm_runtime_get_sync(dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03001821 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02001822 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001823
1824 fep->mii_timeout = 0;
Russell Kingaac27c72015-08-25 09:49:53 +01001825 reinit_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001826
Shawn Guo862f0982011-01-05 21:13:09 +00001827 /* start a write op */
1828 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001829 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1830 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1831 fep->hwp + FEC_MII_DATA);
1832
1833 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001834 time_left = wait_for_completion_timeout(&fep->mdio_done,
1835 usecs_to_jiffies(FEC_MII_TIMEOUT));
1836 if (time_left == 0) {
1837 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001838 netdev_err(fep->netdev, "MDIO write timeout\n");
Andrew Lunn8fff7552015-07-25 22:38:02 +02001839 ret = -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001840 }
1841
Andrew Lunn8fff7552015-07-25 22:38:02 +02001842 pm_runtime_mark_last_busy(dev);
1843 pm_runtime_put_autosuspend(dev);
1844
1845 return ret;
Bryan Wue6b043d2010-03-31 02:10:44 +00001846}
1847
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001848static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1849{
1850 struct fec_enet_private *fep = netdev_priv(ndev);
1851 int ret;
1852
1853 if (enable) {
1854 ret = clk_prepare_enable(fep->clk_ahb);
1855 if (ret)
1856 return ret;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001857 if (fep->clk_enet_out) {
1858 ret = clk_prepare_enable(fep->clk_enet_out);
1859 if (ret)
1860 goto failed_clk_enet_out;
1861 }
1862 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001863 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001864 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001865 if (ret) {
1866 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001867 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001868 } else {
1869 fep->ptp_clk_on = true;
1870 }
1871 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001872 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001873 if (fep->clk_ref) {
1874 ret = clk_prepare_enable(fep->clk_ref);
1875 if (ret)
1876 goto failed_clk_ref;
1877 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001878 } else {
1879 clk_disable_unprepare(fep->clk_ahb);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001880 if (fep->clk_enet_out)
1881 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001882 if (fep->clk_ptp) {
1883 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001884 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001885 fep->ptp_clk_on = false;
1886 mutex_unlock(&fep->ptp_clk_mutex);
1887 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001888 if (fep->clk_ref)
1889 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001890 }
1891
1892 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001893
1894failed_clk_ref:
1895 if (fep->clk_ref)
1896 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001897failed_clk_ptp:
1898 if (fep->clk_enet_out)
1899 clk_disable_unprepare(fep->clk_enet_out);
1900failed_clk_enet_out:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001901 clk_disable_unprepare(fep->clk_ahb);
1902
1903 return ret;
1904}
1905
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001906static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001907{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001908 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001909 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001910 char mdio_bus_id[MII_BUS_ID_SIZE];
1911 char phy_name[MII_BUS_ID_SIZE + 3];
1912 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001913 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001914
Bryan Wu418bd0d2010-05-28 03:40:39 -07001915 fep->phy_dev = NULL;
1916
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001917 if (fep->phy_node) {
1918 phy_dev = of_phy_connect(ndev, fep->phy_node,
1919 &fec_enet_adjust_link, 0,
1920 fep->phy_interface);
Nimrod Andy213a9922014-12-09 18:46:56 +08001921 if (!phy_dev)
1922 return -ENODEV;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001923 } else {
1924 /* check for attached phy */
1925 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1926 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1927 continue;
1928 if (fep->mii_bus->phy_map[phy_id] == NULL)
1929 continue;
1930 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1931 continue;
1932 if (dev_id--)
1933 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001934 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001935 break;
1936 }
1937
1938 if (phy_id >= PHY_MAX_ADDR) {
1939 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001940 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001941 phy_id = 0;
1942 }
1943
1944 snprintf(phy_name, sizeof(phy_name),
1945 PHY_ID_FMT, mdio_bus_id, phy_id);
1946 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1947 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001948 }
1949
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001950 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001951 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001952 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001953 }
1954
1955 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001956 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001957 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001958 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001959#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001960 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001961#endif
Frank Libaa70a52013-01-16 16:55:58 +00001962 }
Shawn Guo230dec62011-09-23 02:12:48 +00001963 else
1964 phy_dev->supported &= PHY_BASIC_FEATURES;
1965
Bryan Wue6b043d2010-03-31 02:10:44 +00001966 phy_dev->advertising = phy_dev->supported;
1967
1968 fep->phy_dev = phy_dev;
1969 fep->link = 0;
1970 fep->full_duplex = 0;
1971
Joe Perches31b77202013-04-13 19:03:17 +00001972 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1973 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1974 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001975
Bryan Wue6b043d2010-03-31 02:10:44 +00001976 return 0;
1977}
1978
1979static int fec_enet_mii_init(struct platform_device *pdev)
1980{
Shawn Guob5680e02011-01-05 21:13:13 +00001981 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001982 struct net_device *ndev = platform_get_drvdata(pdev);
1983 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001984 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001985 int err = -ENXIO, i;
Uwe Kleine-König63c60732015-03-27 11:08:32 +01001986 u32 mii_speed, holdtime;
Bryan Wue6b043d2010-03-31 02:10:44 +00001987
Shawn Guob5680e02011-01-05 21:13:13 +00001988 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001989 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001990 * Here are the differences:
1991 *
1992 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1993 * - fec0 acts as the 1588 time master while fec1 is slave
1994 * - external phys can only be configured by fec0
1995 *
1996 * That is to say fec1 can not work independently. It only works
1997 * when fec0 is working. The reason behind this design is that the
1998 * second interface is added primarily for Switch mode.
1999 *
2000 * Because of the last point above, both phys are attached on fec0
2001 * mdio interface in board design, and need to be configured by
2002 * fec0 mii_bus.
2003 */
Stefan Agner3d125f92015-01-14 00:20:21 +01002004 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00002005 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002006 if (mii_cnt && fec0_mii_bus) {
2007 fep->mii_bus = fec0_mii_bus;
2008 mii_cnt++;
2009 return 0;
2010 }
2011 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00002012 }
2013
Bryan Wue6b043d2010-03-31 02:10:44 +00002014 fep->mii_timeout = 0;
2015
2016 /*
2017 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002018 *
2019 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2020 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2021 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2022 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002023 */
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002024 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002025 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Uwe Kleine-König63c60732015-03-27 11:08:32 +01002026 mii_speed--;
2027 if (mii_speed > 63) {
2028 dev_err(&pdev->dev,
2029 "fec clock (%lu) to fast to get right mii speed\n",
2030 clk_get_rate(fep->clk_ipg));
2031 err = -EINVAL;
2032 goto err_out;
2033 }
2034
2035 /*
2036 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2037 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2038 * versions are RAZ there, so just ignore the difference and write the
2039 * register always.
2040 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2041 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2042 * output.
2043 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2044 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2045 * holdtime cannot result in a value greater than 3.
2046 */
2047 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2048
2049 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2050
Bryan Wue6b043d2010-03-31 02:10:44 +00002051 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2052
2053 fep->mii_bus = mdiobus_alloc();
2054 if (fep->mii_bus == NULL) {
2055 err = -ENOMEM;
2056 goto err_out;
2057 }
2058
2059 fep->mii_bus->name = "fec_enet_mii_bus";
2060 fep->mii_bus->read = fec_enet_mdio_read;
2061 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002062 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2063 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002064 fep->mii_bus->priv = fep;
2065 fep->mii_bus->parent = &pdev->dev;
2066
2067 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2068 if (!fep->mii_bus->irq) {
2069 err = -ENOMEM;
2070 goto err_out_free_mdiobus;
2071 }
2072
2073 for (i = 0; i < PHY_MAX_ADDR; i++)
2074 fep->mii_bus->irq[i] = PHY_POLL;
2075
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002076 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2077 if (node) {
2078 err = of_mdiobus_register(fep->mii_bus, node);
2079 of_node_put(node);
2080 } else {
2081 err = mdiobus_register(fep->mii_bus);
2082 }
2083
2084 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002085 goto err_out_free_mdio_irq;
2086
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002087 mii_cnt++;
2088
Shawn Guob5680e02011-01-05 21:13:13 +00002089 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002090 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002091 fec0_mii_bus = fep->mii_bus;
2092
Bryan Wue6b043d2010-03-31 02:10:44 +00002093 return 0;
2094
Bryan Wue6b043d2010-03-31 02:10:44 +00002095err_out_free_mdio_irq:
2096 kfree(fep->mii_bus->irq);
2097err_out_free_mdiobus:
2098 mdiobus_free(fep->mii_bus);
2099err_out:
2100 return err;
2101}
2102
2103static void fec_enet_mii_remove(struct fec_enet_private *fep)
2104{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002105 if (--mii_cnt == 0) {
2106 mdiobus_unregister(fep->mii_bus);
2107 kfree(fep->mii_bus->irq);
2108 mdiobus_free(fep->mii_bus);
2109 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002110}
2111
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002112static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002113 struct ethtool_cmd *cmd)
2114{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002115 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002116 struct phy_device *phydev = fep->phy_dev;
2117
2118 if (!phydev)
2119 return -ENODEV;
2120
2121 return phy_ethtool_gset(phydev, cmd);
2122}
2123
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002124static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002125 struct ethtool_cmd *cmd)
2126{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002127 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002128 struct phy_device *phydev = fep->phy_dev;
2129
2130 if (!phydev)
2131 return -ENODEV;
2132
2133 return phy_ethtool_sset(phydev, cmd);
2134}
2135
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002136static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002137 struct ethtool_drvinfo *info)
2138{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002139 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Jiri Pirko7826d432013-01-06 00:44:26 +00002141 strlcpy(info->driver, fep->pdev->dev.driver->name,
2142 sizeof(info->driver));
2143 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2144 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
Bryan Wue6b043d2010-03-31 02:10:44 +00002146
Philippe Reynesdb65f352015-05-11 00:01:42 +02002147static int fec_enet_get_regs_len(struct net_device *ndev)
2148{
2149 struct fec_enet_private *fep = netdev_priv(ndev);
2150 struct resource *r;
2151 int s = 0;
2152
2153 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2154 if (r)
2155 s = resource_size(r);
2156
2157 return s;
2158}
2159
2160/* List of registers that can be safety be read to dump them with ethtool */
2161#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2162 defined(CONFIG_M520x) || defined(CONFIG_M532x) || \
2163 defined(CONFIG_ARCH_MXC) || defined(CONFIG_SOC_IMX28)
2164static u32 fec_enet_register_offset[] = {
2165 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2166 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2167 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2168 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2169 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2170 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2171 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2172 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2173 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2174 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2175 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2176 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2177 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2178 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2179 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2180 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2181 RMON_T_P_GTE2048, RMON_T_OCTETS,
2182 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2183 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2184 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2185 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2186 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2187 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2188 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2189 RMON_R_P_GTE2048, RMON_R_OCTETS,
2190 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2191 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2192};
2193#else
2194static u32 fec_enet_register_offset[] = {
2195 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2196 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2197 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2198 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2199 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2200 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2201 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2202 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2203 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2204};
2205#endif
2206
2207static void fec_enet_get_regs(struct net_device *ndev,
2208 struct ethtool_regs *regs, void *regbuf)
2209{
2210 struct fec_enet_private *fep = netdev_priv(ndev);
2211 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2212 u32 *buf = (u32 *)regbuf;
2213 u32 i, off;
2214
2215 memset(buf, 0, regs->len);
2216
2217 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2218 off = fec_enet_register_offset[i] / 4;
2219 buf[off] = readl(&theregs[off]);
2220 }
2221}
2222
Frank Li5ebae4892013-01-06 16:25:07 +00002223static int fec_enet_get_ts_info(struct net_device *ndev,
2224 struct ethtool_ts_info *info)
2225{
2226 struct fec_enet_private *fep = netdev_priv(ndev);
2227
2228 if (fep->bufdesc_ex) {
2229
2230 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2231 SOF_TIMESTAMPING_RX_SOFTWARE |
2232 SOF_TIMESTAMPING_SOFTWARE |
2233 SOF_TIMESTAMPING_TX_HARDWARE |
2234 SOF_TIMESTAMPING_RX_HARDWARE |
2235 SOF_TIMESTAMPING_RAW_HARDWARE;
2236 if (fep->ptp_clock)
2237 info->phc_index = ptp_clock_index(fep->ptp_clock);
2238 else
2239 info->phc_index = -1;
2240
2241 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2242 (1 << HWTSTAMP_TX_ON);
2243
2244 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2245 (1 << HWTSTAMP_FILTER_ALL);
2246 return 0;
2247 } else {
2248 return ethtool_op_get_ts_info(ndev, info);
2249 }
2250}
2251
Guenter Roeckd1391932013-06-18 10:04:59 -07002252#if !defined(CONFIG_M5272)
2253
Frank Libaa70a52013-01-16 16:55:58 +00002254static void fec_enet_get_pauseparam(struct net_device *ndev,
2255 struct ethtool_pauseparam *pause)
2256{
2257 struct fec_enet_private *fep = netdev_priv(ndev);
2258
2259 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2260 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2261 pause->rx_pause = pause->tx_pause;
2262}
2263
2264static int fec_enet_set_pauseparam(struct net_device *ndev,
2265 struct ethtool_pauseparam *pause)
2266{
2267 struct fec_enet_private *fep = netdev_priv(ndev);
2268
Russell King0b146ca2014-07-08 00:22:59 +01002269 if (!fep->phy_dev)
2270 return -ENODEV;
2271
Frank Libaa70a52013-01-16 16:55:58 +00002272 if (pause->tx_pause != pause->rx_pause) {
2273 netdev_info(ndev,
2274 "hardware only support enable/disable both tx and rx");
2275 return -EINVAL;
2276 }
2277
2278 fep->pause_flag = 0;
2279
2280 /* tx pause must be same as rx pause */
2281 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2282 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2283
2284 if (pause->rx_pause || pause->autoneg) {
2285 fep->phy_dev->supported |= ADVERTISED_Pause;
2286 fep->phy_dev->advertising |= ADVERTISED_Pause;
2287 } else {
2288 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2289 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2290 }
2291
2292 if (pause->autoneg) {
2293 if (netif_running(ndev))
2294 fec_stop(ndev);
2295 phy_start_aneg(fep->phy_dev);
2296 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002297 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002298 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002299 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002300 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002301 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002302 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002303 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002304 }
Frank Libaa70a52013-01-16 16:55:58 +00002305
2306 return 0;
2307}
2308
Chris Healy38ae92d2013-06-25 23:18:52 -07002309static const struct fec_stat {
2310 char name[ETH_GSTRING_LEN];
2311 u16 offset;
2312} fec_stats[] = {
2313 /* RMON TX */
2314 { "tx_dropped", RMON_T_DROP },
2315 { "tx_packets", RMON_T_PACKETS },
2316 { "tx_broadcast", RMON_T_BC_PKT },
2317 { "tx_multicast", RMON_T_MC_PKT },
2318 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2319 { "tx_undersize", RMON_T_UNDERSIZE },
2320 { "tx_oversize", RMON_T_OVERSIZE },
2321 { "tx_fragment", RMON_T_FRAG },
2322 { "tx_jabber", RMON_T_JAB },
2323 { "tx_collision", RMON_T_COL },
2324 { "tx_64byte", RMON_T_P64 },
2325 { "tx_65to127byte", RMON_T_P65TO127 },
2326 { "tx_128to255byte", RMON_T_P128TO255 },
2327 { "tx_256to511byte", RMON_T_P256TO511 },
2328 { "tx_512to1023byte", RMON_T_P512TO1023 },
2329 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2330 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2331 { "tx_octets", RMON_T_OCTETS },
2332
2333 /* IEEE TX */
2334 { "IEEE_tx_drop", IEEE_T_DROP },
2335 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2336 { "IEEE_tx_1col", IEEE_T_1COL },
2337 { "IEEE_tx_mcol", IEEE_T_MCOL },
2338 { "IEEE_tx_def", IEEE_T_DEF },
2339 { "IEEE_tx_lcol", IEEE_T_LCOL },
2340 { "IEEE_tx_excol", IEEE_T_EXCOL },
2341 { "IEEE_tx_macerr", IEEE_T_MACERR },
2342 { "IEEE_tx_cserr", IEEE_T_CSERR },
2343 { "IEEE_tx_sqe", IEEE_T_SQE },
2344 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2345 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2346
2347 /* RMON RX */
2348 { "rx_packets", RMON_R_PACKETS },
2349 { "rx_broadcast", RMON_R_BC_PKT },
2350 { "rx_multicast", RMON_R_MC_PKT },
2351 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2352 { "rx_undersize", RMON_R_UNDERSIZE },
2353 { "rx_oversize", RMON_R_OVERSIZE },
2354 { "rx_fragment", RMON_R_FRAG },
2355 { "rx_jabber", RMON_R_JAB },
2356 { "rx_64byte", RMON_R_P64 },
2357 { "rx_65to127byte", RMON_R_P65TO127 },
2358 { "rx_128to255byte", RMON_R_P128TO255 },
2359 { "rx_256to511byte", RMON_R_P256TO511 },
2360 { "rx_512to1023byte", RMON_R_P512TO1023 },
2361 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2362 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2363 { "rx_octets", RMON_R_OCTETS },
2364
2365 /* IEEE RX */
2366 { "IEEE_rx_drop", IEEE_R_DROP },
2367 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2368 { "IEEE_rx_crc", IEEE_R_CRC },
2369 { "IEEE_rx_align", IEEE_R_ALIGN },
2370 { "IEEE_rx_macerr", IEEE_R_MACERR },
2371 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2372 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2373};
2374
2375static void fec_enet_get_ethtool_stats(struct net_device *dev,
2376 struct ethtool_stats *stats, u64 *data)
2377{
2378 struct fec_enet_private *fep = netdev_priv(dev);
2379 int i;
2380
2381 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2382 data[i] = readl(fep->hwp + fec_stats[i].offset);
2383}
2384
2385static void fec_enet_get_strings(struct net_device *netdev,
2386 u32 stringset, u8 *data)
2387{
2388 int i;
2389 switch (stringset) {
2390 case ETH_SS_STATS:
2391 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2392 memcpy(data + i * ETH_GSTRING_LEN,
2393 fec_stats[i].name, ETH_GSTRING_LEN);
2394 break;
2395 }
2396}
2397
2398static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2399{
2400 switch (sset) {
2401 case ETH_SS_STATS:
2402 return ARRAY_SIZE(fec_stats);
2403 default:
2404 return -EOPNOTSUPP;
2405 }
2406}
Guenter Roeckd1391932013-06-18 10:04:59 -07002407#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002408
Chris Healy32bc9b42013-06-17 07:25:06 -07002409static int fec_enet_nway_reset(struct net_device *dev)
2410{
2411 struct fec_enet_private *fep = netdev_priv(dev);
2412 struct phy_device *phydev = fep->phy_dev;
2413
2414 if (!phydev)
2415 return -ENODEV;
2416
2417 return genphy_restart_aneg(phydev);
2418}
2419
Fugang Duand851b472014-09-17 05:18:52 +08002420/* ITR clock source is enet system clock (clk_ahb).
2421 * TCTT unit is cycle_ns * 64 cycle
2422 * So, the ICTT value = X us / (cycle_ns * 64)
2423 */
2424static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2425{
2426 struct fec_enet_private *fep = netdev_priv(ndev);
2427
2428 return us * (fep->itr_clk_rate / 64000) / 1000;
2429}
2430
2431/* Set threshold for interrupt coalescing */
2432static void fec_enet_itr_coal_set(struct net_device *ndev)
2433{
2434 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002435 int rx_itr, tx_itr;
2436
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002437 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002438 return;
2439
2440 /* Must be greater than zero to avoid unpredictable behavior */
2441 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2442 !fep->tx_time_itr || !fep->tx_pkts_itr)
2443 return;
2444
2445 /* Select enet system clock as Interrupt Coalescing
2446 * timer Clock Source
2447 */
2448 rx_itr = FEC_ITR_CLK_SEL;
2449 tx_itr = FEC_ITR_CLK_SEL;
2450
2451 /* set ICFT and ICTT */
2452 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2453 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2454 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2455 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2456
2457 rx_itr |= FEC_ITR_EN;
2458 tx_itr |= FEC_ITR_EN;
2459
2460 writel(tx_itr, fep->hwp + FEC_TXIC0);
2461 writel(rx_itr, fep->hwp + FEC_RXIC0);
2462 writel(tx_itr, fep->hwp + FEC_TXIC1);
2463 writel(rx_itr, fep->hwp + FEC_RXIC1);
2464 writel(tx_itr, fep->hwp + FEC_TXIC2);
2465 writel(rx_itr, fep->hwp + FEC_RXIC2);
2466}
2467
2468static int
2469fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2470{
2471 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002472
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002473 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002474 return -EOPNOTSUPP;
2475
2476 ec->rx_coalesce_usecs = fep->rx_time_itr;
2477 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2478
2479 ec->tx_coalesce_usecs = fep->tx_time_itr;
2480 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2481
2482 return 0;
2483}
2484
2485static int
2486fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2487{
2488 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002489 unsigned int cycle;
2490
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002491 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002492 return -EOPNOTSUPP;
2493
2494 if (ec->rx_max_coalesced_frames > 255) {
2495 pr_err("Rx coalesced frames exceed hardware limiation");
2496 return -EINVAL;
2497 }
2498
2499 if (ec->tx_max_coalesced_frames > 255) {
2500 pr_err("Tx coalesced frame exceed hardware limiation");
2501 return -EINVAL;
2502 }
2503
2504 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2505 if (cycle > 0xFFFF) {
2506 pr_err("Rx coalesed usec exceeed hardware limiation");
2507 return -EINVAL;
2508 }
2509
2510 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2511 if (cycle > 0xFFFF) {
2512 pr_err("Rx coalesed usec exceeed hardware limiation");
2513 return -EINVAL;
2514 }
2515
2516 fep->rx_time_itr = ec->rx_coalesce_usecs;
2517 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2518
2519 fep->tx_time_itr = ec->tx_coalesce_usecs;
2520 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2521
2522 fec_enet_itr_coal_set(ndev);
2523
2524 return 0;
2525}
2526
2527static void fec_enet_itr_coal_init(struct net_device *ndev)
2528{
2529 struct ethtool_coalesce ec;
2530
2531 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2532 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2533
2534 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2535 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2536
2537 fec_enet_set_coalesce(ndev, &ec);
2538}
2539
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002540static int fec_enet_get_tunable(struct net_device *netdev,
2541 const struct ethtool_tunable *tuna,
2542 void *data)
2543{
2544 struct fec_enet_private *fep = netdev_priv(netdev);
2545 int ret = 0;
2546
2547 switch (tuna->id) {
2548 case ETHTOOL_RX_COPYBREAK:
2549 *(u32 *)data = fep->rx_copybreak;
2550 break;
2551 default:
2552 ret = -EINVAL;
2553 break;
2554 }
2555
2556 return ret;
2557}
2558
2559static int fec_enet_set_tunable(struct net_device *netdev,
2560 const struct ethtool_tunable *tuna,
2561 const void *data)
2562{
2563 struct fec_enet_private *fep = netdev_priv(netdev);
2564 int ret = 0;
2565
2566 switch (tuna->id) {
2567 case ETHTOOL_RX_COPYBREAK:
2568 fep->rx_copybreak = *(u32 *)data;
2569 break;
2570 default:
2571 ret = -EINVAL;
2572 break;
2573 }
2574
2575 return ret;
2576}
2577
Nimrod Andyde40ed32014-12-24 17:30:39 +08002578static void
2579fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2580{
2581 struct fec_enet_private *fep = netdev_priv(ndev);
2582
2583 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2584 wol->supported = WAKE_MAGIC;
2585 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2586 } else {
2587 wol->supported = wol->wolopts = 0;
2588 }
2589}
2590
2591static int
2592fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2593{
2594 struct fec_enet_private *fep = netdev_priv(ndev);
2595
2596 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2597 return -EINVAL;
2598
2599 if (wol->wolopts & ~WAKE_MAGIC)
2600 return -EINVAL;
2601
2602 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2603 if (device_may_wakeup(&ndev->dev)) {
2604 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2605 if (fep->irq[0] > 0)
2606 enable_irq_wake(fep->irq[0]);
2607 } else {
2608 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2609 if (fep->irq[0] > 0)
2610 disable_irq_wake(fep->irq[0]);
2611 }
2612
2613 return 0;
2614}
2615
stephen hemminger9b07be42012-01-04 12:59:49 +00002616static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002617 .get_settings = fec_enet_get_settings,
2618 .set_settings = fec_enet_set_settings,
2619 .get_drvinfo = fec_enet_get_drvinfo,
Philippe Reynesdb65f352015-05-11 00:01:42 +02002620 .get_regs_len = fec_enet_get_regs_len,
2621 .get_regs = fec_enet_get_regs,
Chris Healy32bc9b42013-06-17 07:25:06 -07002622 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002623 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002624 .get_coalesce = fec_enet_get_coalesce,
2625 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002626#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002627 .get_pauseparam = fec_enet_get_pauseparam,
2628 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002629 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002630 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002631 .get_sset_count = fec_enet_get_sset_count,
2632#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002633 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002634 .get_tunable = fec_enet_get_tunable,
2635 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002636 .get_wol = fec_enet_get_wol,
2637 .set_wol = fec_enet_set_wol,
Bryan Wue6b043d2010-03-31 02:10:44 +00002638};
2639
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002640static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002641{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002642 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002643 struct phy_device *phydev = fep->phy_dev;
2644
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002645 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002646 return -EINVAL;
2647
2648 if (!phydev)
2649 return -ENODEV;
2650
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002651 if (fep->bufdesc_ex) {
2652 if (cmd == SIOCSHWTSTAMP)
2653 return fec_ptp_set(ndev, rq);
2654 if (cmd == SIOCGHWTSTAMP)
2655 return fec_ptp_get(ndev, rq);
2656 }
Frank Liff43da82013-01-03 16:04:23 +00002657
Richard Cochran28b04112010-07-17 08:48:55 +00002658 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002661static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002662{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002663 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002664 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002665 struct sk_buff *skb;
2666 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002667 struct fec_enet_priv_tx_q *txq;
2668 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002669 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002670
Frank Li59d0f7462014-09-13 05:00:50 +08002671 for (q = 0; q < fep->num_rx_queues; q++) {
2672 rxq = fep->rx_queue[q];
2673 bdp = rxq->rx_bd_base;
2674 for (i = 0; i < rxq->rx_ring_size; i++) {
2675 skb = rxq->rx_skbuff[i];
2676 rxq->rx_skbuff[i] = NULL;
2677 if (skb) {
2678 dma_unmap_single(&fep->pdev->dev,
2679 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002680 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002681 DMA_FROM_DEVICE);
2682 dev_kfree_skb(skb);
2683 }
2684 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002685 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002686 }
2687
Frank Li59d0f7462014-09-13 05:00:50 +08002688 for (q = 0; q < fep->num_tx_queues; q++) {
2689 txq = fep->tx_queue[q];
2690 bdp = txq->tx_bd_base;
2691 for (i = 0; i < txq->tx_ring_size; i++) {
2692 kfree(txq->tx_bounce[i]);
2693 txq->tx_bounce[i] = NULL;
2694 skb = txq->tx_skbuff[i];
2695 txq->tx_skbuff[i] = NULL;
2696 dev_kfree_skb(skb);
2697 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002698 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002699}
2700
Frank Li59d0f7462014-09-13 05:00:50 +08002701static void fec_enet_free_queue(struct net_device *ndev)
2702{
2703 struct fec_enet_private *fep = netdev_priv(ndev);
2704 int i;
2705 struct fec_enet_priv_tx_q *txq;
2706
2707 for (i = 0; i < fep->num_tx_queues; i++)
2708 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2709 txq = fep->tx_queue[i];
2710 dma_free_coherent(NULL,
2711 txq->tx_ring_size * TSO_HEADER_SIZE,
2712 txq->tso_hdrs,
2713 txq->tso_hdrs_dma);
2714 }
2715
2716 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002717 kfree(fep->rx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002718 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002719 kfree(fep->tx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002720}
2721
2722static int fec_enet_alloc_queue(struct net_device *ndev)
2723{
2724 struct fec_enet_private *fep = netdev_priv(ndev);
2725 int i;
2726 int ret = 0;
2727 struct fec_enet_priv_tx_q *txq;
2728
2729 for (i = 0; i < fep->num_tx_queues; i++) {
2730 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2731 if (!txq) {
2732 ret = -ENOMEM;
2733 goto alloc_failed;
2734 }
2735
2736 fep->tx_queue[i] = txq;
2737 txq->tx_ring_size = TX_RING_SIZE;
2738 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2739
2740 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2741 txq->tx_wake_threshold =
2742 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2743
2744 txq->tso_hdrs = dma_alloc_coherent(NULL,
2745 txq->tx_ring_size * TSO_HEADER_SIZE,
2746 &txq->tso_hdrs_dma,
2747 GFP_KERNEL);
2748 if (!txq->tso_hdrs) {
2749 ret = -ENOMEM;
2750 goto alloc_failed;
2751 }
2752 }
2753
2754 for (i = 0; i < fep->num_rx_queues; i++) {
2755 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2756 GFP_KERNEL);
2757 if (!fep->rx_queue[i]) {
2758 ret = -ENOMEM;
2759 goto alloc_failed;
2760 }
2761
2762 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2763 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2764 }
2765 return ret;
2766
2767alloc_failed:
2768 fec_enet_free_queue(ndev);
2769 return ret;
2770}
2771
2772static int
2773fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002774{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002775 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002776 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002777 struct sk_buff *skb;
2778 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002779 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002780
Frank Li59d0f7462014-09-13 05:00:50 +08002781 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002782 bdp = rxq->rx_bd_base;
2783 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002784 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002785 if (!skb)
2786 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002787
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002788 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002789 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002790 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002791 }
Russell King730ee362014-07-08 00:23:14 +01002792
Fugang Duan4d494cd2014-09-13 05:00:48 +08002793 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002794 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002795
2796 if (fep->bufdesc_ex) {
2797 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2798 ebdp->cbd_esc = BD_ENET_RX_INT;
2799 }
2800
Frank Li59d0f7462014-09-13 05:00:50 +08002801 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002802 }
2803
2804 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002805 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002806 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002807 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002808
Frank Li59d0f7462014-09-13 05:00:50 +08002809 err_alloc:
2810 fec_enet_free_buffers(ndev);
2811 return -ENOMEM;
2812}
2813
2814static int
2815fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2816{
2817 struct fec_enet_private *fep = netdev_priv(ndev);
2818 unsigned int i;
2819 struct bufdesc *bdp;
2820 struct fec_enet_priv_tx_q *txq;
2821
2822 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002823 bdp = txq->tx_bd_base;
2824 for (i = 0; i < txq->tx_ring_size; i++) {
2825 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2826 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002827 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002828
2829 bdp->cbd_sc = 0;
2830 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002831
Frank Liff43da82013-01-03 16:04:23 +00002832 if (fep->bufdesc_ex) {
2833 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002834 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002835 }
2836
Frank Li59d0f7462014-09-13 05:00:50 +08002837 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002838 }
2839
2840 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002841 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002842 bdp->cbd_sc |= BD_SC_WRAP;
2843
2844 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002845
2846 err_alloc:
2847 fec_enet_free_buffers(ndev);
2848 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002849}
2850
Frank Li59d0f7462014-09-13 05:00:50 +08002851static int fec_enet_alloc_buffers(struct net_device *ndev)
2852{
2853 struct fec_enet_private *fep = netdev_priv(ndev);
2854 unsigned int i;
2855
2856 for (i = 0; i < fep->num_rx_queues; i++)
2857 if (fec_enet_alloc_rxq_buffers(ndev, i))
2858 return -ENOMEM;
2859
2860 for (i = 0; i < fep->num_tx_queues; i++)
2861 if (fec_enet_alloc_txq_buffers(ndev, i))
2862 return -ENOMEM;
2863 return 0;
2864}
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002867fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002869 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002870 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Andrew Lunn8fff7552015-07-25 22:38:02 +02002872 ret = pm_runtime_get_sync(&fep->pdev->dev);
Fabio Estevamb0c6ce22015-08-12 12:10:23 -03002873 if (ret < 0)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002874 return ret;
2875
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002876 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002877 ret = fec_enet_clk_enable(ndev, true);
2878 if (ret)
Andrew Lunn8fff7552015-07-25 22:38:02 +02002879 goto clk_enable;
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /* I should reset the ring buffers here, but I don't yet know
2882 * a simple way to do that.
2883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002885 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002886 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002887 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002888
Nimrod Andy55dd2752015-06-23 15:32:51 +08002889 /* Init MAC prior to mii bus probe */
2890 fec_restart(ndev);
2891
Bryan Wu418bd0d2010-05-28 03:40:39 -07002892 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002893 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002894 if (ret)
2895 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002896
2897 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002898 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002899 netif_tx_start_all_queues(ndev);
2900
Nimrod Andyde40ed32014-12-24 17:30:39 +08002901 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2902 FEC_WOL_FLAG_ENABLE);
2903
Sascha Hauer22f6b862009-04-15 01:32:18 +00002904 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002905
2906err_enet_mii_probe:
2907 fec_enet_free_buffers(ndev);
2908err_enet_alloc:
2909 fec_enet_clk_enable(ndev, false);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002910clk_enable:
2911 pm_runtime_mark_last_busy(&fep->pdev->dev);
2912 pm_runtime_put_autosuspend(&fep->pdev->dev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002913 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2914 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
2917static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002918fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002920 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Russell Kingd76cfae2014-07-08 00:23:04 +01002922 phy_stop(fep->phy_dev);
2923
Russell King31a6de32014-07-08 12:40:23 +01002924 if (netif_device_present(ndev)) {
2925 napi_disable(&fep->napi);
2926 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002927 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Russell King635cf172014-07-08 00:22:54 +01002930 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002931 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002932
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002933 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002934 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02002935 pm_runtime_mark_last_busy(&fep->pdev->dev);
2936 pm_runtime_put_autosuspend(&fep->pdev->dev);
2937
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002938 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return 0;
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943/* Set or clear the multicast filter for this adaptor.
2944 * Skeleton taken from sunlance driver.
2945 * The CPM Ethernet implementation allows Multicast as well as individual
2946 * MAC address filtering. Some of the drivers check to make sure it is
2947 * a group multicast address, and discard those that are not. I guess I
2948 * will do the same for now, but just remove the test if you want
2949 * individual filtering as well (do the upper net layers want or support
2950 * this kind of feature?).
2951 */
2952
2953#define HASH_BITS 6 /* #bits in hash */
2954#define CRC32_POLY 0xEDB88320
2955
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002956static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002958 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002959 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002960 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 unsigned char hash;
2962
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002963 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002964 tmp = readl(fep->hwp + FEC_R_CNTRL);
2965 tmp |= 0x8;
2966 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002967 return;
2968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Sascha Hauer4e831832009-04-15 01:32:19 +00002970 tmp = readl(fep->hwp + FEC_R_CNTRL);
2971 tmp &= ~0x8;
2972 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002973
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002974 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002975 /* Catch all multicast addresses, so set the
2976 * filter to all 1's
2977 */
2978 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2979 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Sascha Hauer4e831832009-04-15 01:32:19 +00002981 return;
2982 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002983
Sascha Hauer4e831832009-04-15 01:32:19 +00002984 /* Clear filter and add the addresses in hash register
2985 */
2986 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2987 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002989 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002990 /* calculate crc32 value of mac address */
2991 crc = 0xffffffff;
2992
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002993 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002994 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002995 for (bit = 0; bit < 8; bit++, data >>= 1) {
2996 crc = (crc >> 1) ^
2997 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
2999 }
Sascha Hauer4e831832009-04-15 01:32:19 +00003000
3001 /* only upper 6 bits (HASH_BITS) are used
3002 * which point to specific bit in he hash registers
3003 */
3004 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
3005
3006 if (hash > 31) {
3007 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3008 tmp |= 1 << (hash - 32);
3009 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3010 } else {
3011 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3012 tmp |= 1 << hash;
3013 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016}
3017
Sascha Hauer22f6b862009-04-15 01:32:18 +00003018/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00003019static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003020fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003022 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00003023 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Lucas Stach44934fa2014-03-30 21:32:08 +02003025 if (addr) {
3026 if (!is_valid_ether_addr(addr->sa_data))
3027 return -EADDRNOTAVAIL;
3028 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3029 }
Sascha Hauer009fda82009-04-15 01:32:23 +00003030
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003031 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3032 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00003033 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003034 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07003035 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00003036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003039#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00003040/**
3041 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003042 * @dev: The FEC network adapter
3043 *
3044 * Polled functionality used by netconsole and others in non interrupt mode
3045 *
3046 */
Wei Yongjun47a52472013-03-20 05:06:11 +00003047static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003048{
3049 int i;
3050 struct fec_enet_private *fep = netdev_priv(dev);
3051
3052 for (i = 0; i < FEC_IRQ_NUM; i++) {
3053 if (fep->irq[i] > 0) {
3054 disable_irq(fep->irq[i]);
3055 fec_enet_interrupt(fep->irq[i], dev);
3056 enable_irq(fep->irq[i]);
3057 }
3058 }
3059}
3060#endif
3061
Russell King8506fa12014-07-08 12:40:33 +01003062#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
Nimrod Andy5bc26722014-10-13 10:53:48 +08003063static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00003064 netdev_features_t features)
3065{
3066 struct fec_enet_private *fep = netdev_priv(netdev);
3067 netdev_features_t changed = features ^ netdev->features;
3068
3069 netdev->features = features;
3070
3071 /* Receive checksum has been changed */
3072 if (changed & NETIF_F_RXCSUM) {
3073 if (features & NETIF_F_RXCSUM)
3074 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3075 else
3076 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01003077 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08003078}
Jim Baxter4c09eed2013-04-19 08:10:49 +00003079
Nimrod Andy5bc26722014-10-13 10:53:48 +08003080static int fec_set_features(struct net_device *netdev,
3081 netdev_features_t features)
3082{
3083 struct fec_enet_private *fep = netdev_priv(netdev);
3084 netdev_features_t changed = features ^ netdev->features;
3085
Russell King8506fa12014-07-08 12:40:33 +01003086 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08003087 napi_disable(&fep->napi);
3088 netif_tx_lock_bh(netdev);
3089 fec_stop(netdev);
3090 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01003091 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003092 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01003093 netif_tx_unlock_bh(netdev);
3094 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08003095 } else {
3096 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00003097 }
3098
3099 return 0;
3100}
3101
Sascha Hauer009fda82009-04-15 01:32:23 +00003102static const struct net_device_ops fec_netdev_ops = {
3103 .ndo_open = fec_enet_open,
3104 .ndo_stop = fec_enet_close,
3105 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003106 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00003107 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00003108 .ndo_validate_addr = eth_validate_addr,
3109 .ndo_tx_timeout = fec_timeout,
3110 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01003111 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00003112#ifdef CONFIG_NET_POLL_CONTROLLER
3113 .ndo_poll_controller = fec_poll_controller,
3114#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00003115 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00003116};
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 /*
3119 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00003120 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003122static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003124 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003125 struct fec_enet_priv_tx_q *txq;
3126 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00003127 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08003128 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08003129 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08003130 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08003131
Fugang Duan41ef84c2014-09-13 05:00:54 +08003132#if defined(CONFIG_ARM)
3133 fep->rx_align = 0xf;
3134 fep->tx_align = 0xf;
3135#else
3136 fep->rx_align = 0x3;
3137 fep->tx_align = 0x3;
3138#endif
3139
Frank Li59d0f7462014-09-13 05:00:50 +08003140 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003141
Nimrod Andy55d02182014-06-12 08:16:21 +08003142 if (fep->bufdesc_ex)
3143 fep->bufdesc_size = sizeof(struct bufdesc_ex);
3144 else
3145 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003146 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08003147 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003149 /* Allocate memory for buffer descriptors. */
Lucas Stachc0a1a0a2015-07-23 16:06:19 +02003150 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3151 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003152 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003153 return -ENOMEM;
3154 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003155
Fugang Duan4d494cd2014-09-13 05:00:48 +08003156 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Shawn Guo49da97d2011-01-05 21:13:11 +00003158 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003159 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003160 /* make sure MAC we just acquired is programmed into the hw */
3161 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003163 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003164 for (i = 0; i < fep->num_rx_queues; i++) {
3165 rxq = fep->rx_queue[i];
3166 rxq->index = i;
3167 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3168 rxq->bd_dma = bd_dma;
3169 if (fep->bufdesc_ex) {
3170 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3171 cbd_base = (struct bufdesc *)
3172 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3173 } else {
3174 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3175 cbd_base += rxq->rx_ring_size;
3176 }
3177 }
3178
3179 for (i = 0; i < fep->num_tx_queues; i++) {
3180 txq = fep->tx_queue[i];
3181 txq->index = i;
3182 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3183 txq->bd_dma = bd_dma;
3184 if (fep->bufdesc_ex) {
3185 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3186 cbd_base = (struct bufdesc *)
3187 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3188 } else {
3189 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3190 cbd_base += txq->tx_ring_size;
3191 }
3192 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Sascha Hauer22f6b862009-04-15 01:32:18 +00003195 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003196 ndev->watchdog_timeo = TX_TIMEOUT;
3197 ndev->netdev_ops = &fec_netdev_ops;
3198 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003199
Frank Lidc975382013-01-28 18:31:42 +00003200 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003201 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003202
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003203 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003204 /* enable hw VLAN support */
3205 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003206
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003207 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003208 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3209
Shawn Guo48496252013-05-08 21:08:22 +00003210 /* enable hw accelerator */
3211 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003212 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003213 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3214 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003215
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003216 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003217 fep->tx_align = 0;
3218 fep->rx_align = 0x3f;
3219 }
3220
Nimrod Andy09d1e542014-06-12 08:16:20 +08003221 ndev->hw_features = ndev->features;
3222
Russell Kingef833372014-07-08 12:40:43 +01003223 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return 0;
3226}
3227
Shawn Guoca2cc332011-06-25 02:04:35 +08003228#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003229static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003230{
3231 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003232 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003233 struct device_node *np = pdev->dev.of_node;
3234
3235 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003236 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003237
Shawn Guoa3caad02012-06-27 03:45:24 +00003238 of_property_read_u32(np, "phy-reset-duration", &msec);
3239 /* A sane reset duration should not be longer than 1s */
3240 if (msec > 1000)
3241 msec = 1;
3242
Shawn Guoca2cc332011-06-25 02:04:35 +08003243 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003244 if (!gpio_is_valid(phy_reset))
3245 return;
3246
Shawn Guo119fc002012-06-27 03:45:22 +00003247 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3248 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003249 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003250 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003251 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003252 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003253 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003254 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003255}
3256#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003257static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003258{
3259 /*
3260 * In case of platform probe, the reset has been done
3261 * by machine code.
3262 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003263}
3264#endif /* CONFIG_OF */
3265
Fugang Duan9fc095f2014-09-13 05:00:49 +08003266static void
3267fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3268{
3269 struct device_node *np = pdev->dev.of_node;
3270 int err;
3271
3272 *num_tx = *num_rx = 1;
3273
3274 if (!np || !of_device_is_available(np))
3275 return;
3276
3277 /* parse the num of tx and rx queues */
3278 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003279 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003280 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003281
3282 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3283 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003284 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003285
3286 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003287 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3288 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003289 *num_tx = 1;
3290 return;
3291 }
3292
3293 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003294 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3295 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003296 *num_rx = 1;
3297 return;
3298 }
3299
3300}
3301
Bill Pemberton33897cc2012-12-03 09:23:58 -05003302static int
Sascha Haueread73182009-01-28 23:03:11 +00003303fec_probe(struct platform_device *pdev)
3304{
3305 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003306 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003307 struct net_device *ndev;
3308 int i, irq, ret = 0;
3309 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003310 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003311 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003312 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003313 int num_tx_qs;
3314 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003315
Fugang Duan9fc095f2014-09-13 05:00:49 +08003316 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3317
Sascha Haueread73182009-01-28 23:03:11 +00003318 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003319 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3320 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003321 if (!ndev)
3322 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003323
3324 SET_NETDEV_DEV(ndev, &pdev->dev);
3325
3326 /* setup board info structure */
3327 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003328
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003329 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3330 if (of_id)
3331 pdev->id_entry = of_id->data;
3332 fep->quirks = pdev->id_entry->driver_data;
3333
Hubert Feurstein0c818592015-01-07 14:48:17 +01003334 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003335 fep->num_rx_queues = num_rx_qs;
3336 fep->num_tx_queues = num_tx_qs;
3337
Guenter Roeckd1391932013-06-18 10:04:59 -07003338#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003339 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003340 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003341 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003342#endif
Frank Libaa70a52013-01-16 16:55:58 +00003343
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003344 /* Select default pin state */
3345 pinctrl_pm_select_default_state(&pdev->dev);
3346
Fabio Estevam399db752013-07-21 13:25:03 -03003347 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303348 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3349 if (IS_ERR(fep->hwp)) {
3350 ret = PTR_ERR(fep->hwp);
3351 goto failed_ioremap;
3352 }
3353
Bryan Wue6b043d2010-03-31 02:10:44 +00003354 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003355 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003356
Sascha Haueread73182009-01-28 23:03:11 +00003357 platform_set_drvdata(pdev, ndev);
3358
Nimrod Andyde40ed32014-12-24 17:30:39 +08003359 if (of_get_property(np, "fsl,magic-packet", NULL))
3360 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3361
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003362 phy_node = of_parse_phandle(np, "phy-handle", 0);
3363 if (!phy_node && of_phy_is_fixed_link(np)) {
3364 ret = of_phy_register_fixed_link(np);
3365 if (ret < 0) {
3366 dev_err(&pdev->dev,
3367 "broken fixed-link specification\n");
3368 goto failed_phy;
3369 }
3370 phy_node = of_node_get(np);
3371 }
3372 fep->phy_node = phy_node;
3373
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003374 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003375 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003376 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003377 if (pdata)
3378 fep->phy_interface = pdata->phy;
3379 else
3380 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3381 } else {
3382 fep->phy_interface = ret;
3383 }
3384
Fabio Estevame2f8d552013-02-22 06:40:45 +00003385 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3386 if (IS_ERR(fep->clk_ipg)) {
3387 ret = PTR_ERR(fep->clk_ipg);
3388 goto failed_clk;
3389 }
3390
3391 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3392 if (IS_ERR(fep->clk_ahb)) {
3393 ret = PTR_ERR(fep->clk_ahb);
3394 goto failed_clk;
3395 }
3396
Fugang Duand851b472014-09-17 05:18:52 +08003397 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3398
Linus Torvalds38f56f32013-05-07 11:06:17 -07003399 /* enet_out is optional, depends on board */
3400 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3401 if (IS_ERR(fep->clk_enet_out))
3402 fep->clk_enet_out = NULL;
3403
Nimrod Andy91c0d982014-08-21 17:09:38 +08003404 fep->ptp_clk_on = false;
3405 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003406
3407 /* clk_ref is optional, depends on board */
3408 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3409 if (IS_ERR(fep->clk_ref))
3410 fep->clk_ref = NULL;
3411
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003412 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003413 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003414 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003415 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003416 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003417 }
3418
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003419 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003420 if (ret)
3421 goto failed_clk;
3422
Andrew Lunn8fff7552015-07-25 22:38:02 +02003423 ret = clk_prepare_enable(fep->clk_ipg);
3424 if (ret)
3425 goto failed_clk_ipg;
3426
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003427 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3428 if (!IS_ERR(fep->reg_phy)) {
3429 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003430 if (ret) {
3431 dev_err(&pdev->dev,
3432 "Failed to enable phy regulator: %d\n", ret);
3433 goto failed_regulator;
3434 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003435 } else {
3436 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003437 }
3438
Andrew Lunn8fff7552015-07-25 22:38:02 +02003439 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3440 pm_runtime_use_autosuspend(&pdev->dev);
Lucas Stach14d2b7c2015-08-03 17:50:11 +02003441 pm_runtime_get_noresume(&pdev->dev);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003442 pm_runtime_set_active(&pdev->dev);
3443 pm_runtime_enable(&pdev->dev);
3444
Fabio Estevame2f8d552013-02-22 06:40:45 +00003445 fec_reset_phy(pdev);
3446
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003447 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003448 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003449
3450 ret = fec_enet_init(ndev);
3451 if (ret)
3452 goto failed_init;
3453
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003454 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003455 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003456 if (irq < 0) {
3457 if (i)
3458 break;
3459 ret = irq;
3460 goto failed_irq;
3461 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003462 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003463 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003464 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003465 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003466
3467 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003468 }
3469
Fugang Duanb4d39b52014-09-13 05:00:55 +08003470 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003471 ret = fec_enet_mii_init(pdev);
3472 if (ret)
3473 goto failed_mii_init;
3474
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003475 /* Carrier starts down, phylib will bring it up */
3476 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003477 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003478 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003479
Sascha Haueread73182009-01-28 23:03:11 +00003480 ret = register_netdev(ndev);
3481 if (ret)
3482 goto failed_register;
3483
Nimrod Andyde40ed32014-12-24 17:30:39 +08003484 device_init_wakeup(&ndev->dev, fep->wol_flag &
3485 FEC_WOL_HAS_MAGIC_PACKET);
3486
Fabio Estevameb1d0642013-04-13 07:25:36 +00003487 if (fep->bufdesc_ex && fep->ptp_clock)
3488 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3489
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003490 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003491 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Andrew Lunn8fff7552015-07-25 22:38:02 +02003492
3493 pm_runtime_mark_last_busy(&pdev->dev);
3494 pm_runtime_put_autosuspend(&pdev->dev);
3495
Sascha Haueread73182009-01-28 23:03:11 +00003496 return 0;
3497
3498failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003499 fec_enet_mii_remove(fep);
3500failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003501failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003502failed_init:
Lucas Stach32cba572015-07-23 16:06:20 +02003503 fec_ptp_stop(pdev);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003504 if (fep->reg_phy)
3505 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003506failed_regulator:
Andrew Lunn8fff7552015-07-25 22:38:02 +02003507 clk_disable_unprepare(fep->clk_ipg);
3508failed_clk_ipg:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003509 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003510failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003511failed_phy:
3512 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003513failed_ioremap:
3514 free_netdev(ndev);
3515
3516 return ret;
3517}
3518
Bill Pemberton33897cc2012-12-03 09:23:58 -05003519static int
Sascha Haueread73182009-01-28 23:03:11 +00003520fec_drv_remove(struct platform_device *pdev)
3521{
3522 struct net_device *ndev = platform_get_drvdata(pdev);
3523 struct fec_enet_private *fep = netdev_priv(ndev);
3524
Russell King36cdc742014-07-08 13:01:44 +01003525 cancel_work_sync(&fep->tx_timeout_work);
Lucas Stach32cba572015-07-23 16:06:20 +02003526 fec_ptp_stop(pdev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003527 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003528 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003529 if (fep->reg_phy)
3530 regulator_disable(fep->reg_phy);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003531 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003532 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003533
Sascha Haueread73182009-01-28 23:03:11 +00003534 return 0;
3535}
3536
Fabio Estevamdd66d382014-07-24 18:24:01 -03003537static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003538{
Eric Benard87cad5c2010-06-18 04:19:54 +00003539 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003540 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003541
Russell Kingda1774e2014-07-08 12:39:57 +01003542 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003543 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003544 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3545 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Russell Kingd76cfae2014-07-08 00:23:04 +01003546 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003547 napi_disable(&fep->napi);
3548 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003549 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003550 netif_tx_unlock_bh(ndev);
3551 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003552 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003553 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3554 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003555 }
Russell Kingda1774e2014-07-08 12:39:57 +01003556 rtnl_unlock();
3557
Nimrod Andyde40ed32014-12-24 17:30:39 +08003558 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003559 regulator_disable(fep->reg_phy);
3560
Nimrod Andy858eeb72014-12-11 09:20:31 +08003561 /* SOC supply clock to phy, when clock is disabled, phy link down
3562 * SOC control phy regulator, when regulator is disabled, phy link down
3563 */
3564 if (fep->clk_enet_out || fep->reg_phy)
3565 fep->link = 0;
3566
Sascha Haueread73182009-01-28 23:03:11 +00003567 return 0;
3568}
3569
Fabio Estevamdd66d382014-07-24 18:24:01 -03003570static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003571{
Eric Benard87cad5c2010-06-18 04:19:54 +00003572 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003573 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003574 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003575 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003576 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003577
Nimrod Andyde40ed32014-12-24 17:30:39 +08003578 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003579 ret = regulator_enable(fep->reg_phy);
3580 if (ret)
3581 return ret;
3582 }
Sascha Haueread73182009-01-28 23:03:11 +00003583
Russell Kingda1774e2014-07-08 12:39:57 +01003584 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003585 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003586 ret = fec_enet_clk_enable(ndev, true);
3587 if (ret) {
3588 rtnl_unlock();
3589 goto failed_clk;
3590 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003591 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3592 if (pdata && pdata->sleep_mode_enable)
3593 pdata->sleep_mode_enable(false);
3594 val = readl(fep->hwp + FEC_ECNTRL);
3595 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3596 writel(val, fep->hwp + FEC_ECNTRL);
3597 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3598 } else {
3599 pinctrl_pm_select_default_state(&fep->pdev->dev);
3600 }
Russell Kingef833372014-07-08 12:40:43 +01003601 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003602 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003603 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003604 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003605 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003606 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003607 }
Russell Kingda1774e2014-07-08 12:39:57 +01003608 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003609
Sascha Haueread73182009-01-28 23:03:11 +00003610 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003611
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003612failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003613 if (fep->reg_phy)
3614 regulator_disable(fep->reg_phy);
3615 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003616}
3617
Andrew Lunn8fff7552015-07-25 22:38:02 +02003618static int __maybe_unused fec_runtime_suspend(struct device *dev)
3619{
3620 struct net_device *ndev = dev_get_drvdata(dev);
3621 struct fec_enet_private *fep = netdev_priv(ndev);
3622
3623 clk_disable_unprepare(fep->clk_ipg);
3624
3625 return 0;
3626}
3627
3628static int __maybe_unused fec_runtime_resume(struct device *dev)
3629{
3630 struct net_device *ndev = dev_get_drvdata(dev);
3631 struct fec_enet_private *fep = netdev_priv(ndev);
3632
3633 return clk_prepare_enable(fep->clk_ipg);
3634}
3635
3636static const struct dev_pm_ops fec_pm_ops = {
3637 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3638 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3639};
Denis Kirjanov59d42892010-06-02 09:27:04 +00003640
Sascha Haueread73182009-01-28 23:03:11 +00003641static struct platform_driver fec_driver = {
3642 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003643 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003644 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003645 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003646 },
Shawn Guob5680e02011-01-05 21:13:13 +00003647 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003648 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003649 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003650};
3651
Fabio Estevamaaca2372012-01-23 16:44:50 +00003652module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003654MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655MODULE_LICENSE("GPL");