blob: 787db5026191ae663000cdcf9a4d1d42dc24be2f [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>
27#include <linux/ptrace.h>
28#include <linux/errno.h>
29#include <linux/ioport.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000036#include <linux/in.h>
37#include <linux/ip.h>
38#include <net/ip.h>
Nimrod Andy79f33912014-06-12 08:16:23 +080039#include <net/tso.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000040#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
44#include <linux/workqueue.h>
45#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000046#include <linux/io.h>
47#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000048#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000049#include <linux/platform_device.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000050#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070051#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080052#include <linux/of.h>
53#include <linux/of_device.h>
54#include <linux/of_gpio.h>
Uwe Kleine-König407066f2014-08-11 17:35:33 +020055#include <linux/of_mdio.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080056#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000057#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010058#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030059#include <linux/pinctrl/consumer.h>
Frank Lic259c132014-10-03 14:29:14 -070060#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Greg Ungerer080853a2007-07-30 16:28:46 +100062#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Christoph Müllner772e42b2013-06-27 21:18:23 +020066static void set_multicast_list(struct net_device *ndev);
Fugang Duand851b472014-09-17 05:18:52 +080067static void fec_enet_itr_coal_init(struct net_device *ndev);
Christoph Müllner772e42b2013-06-27 21:18:23 +020068
Shawn Guob5680e02011-01-05 21:13:13 +000069#define DRIVER_NAME "fec"
70
Fugang Duan4d494cd2014-09-13 05:00:48 +080071#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
72
Frank Libaa70a52013-01-16 16:55:58 +000073/* Pause frame feild and FIFO threshold */
74#define FEC_ENET_FCE (1 << 5)
75#define FEC_ENET_RSEM_V 0x84
76#define FEC_ENET_RSFL_V 16
77#define FEC_ENET_RAEM_V 0x8
78#define FEC_ENET_RAFL_V 0x8
79#define FEC_ENET_OPD_V 0xFFF0
80
Shawn Guob5680e02011-01-05 21:13:13 +000081static struct platform_device_id fec_devtype[] = {
82 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080083 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000084 .name = DRIVER_NAME,
85 .driver_data = 0,
86 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080087 .name = "imx25-fec",
88 .driver_data = FEC_QUIRK_USE_GASKET,
89 }, {
90 .name = "imx27-fec",
91 .driver_data = 0,
92 }, {
Shawn Guob5680e02011-01-05 21:13:13 +000093 .name = "imx28-fec",
Stefan Agner3d125f92015-01-14 00:20:21 +010094 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
95 FEC_QUIRK_SINGLE_MDIO,
Shawn Guo0ca1e292011-07-01 18:11:22 +080096 }, {
Shawn Guo230dec62011-09-23 02:12:48 +000097 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +000098 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +010099 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800100 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000101 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000102 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000103 .driver_data = FEC_QUIRK_ENET_MAC,
104 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800105 .name = "imx6sx-fec",
106 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
107 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800108 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
Nimrod Andy28b5f052014-10-15 17:30:12 +0800109 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE,
Fugang Duan95a77472014-09-13 05:00:47 +0800110 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800111 /* sentinel */
112 }
Shawn Guob5680e02011-01-05 21:13:13 +0000113};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800114MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000115
Shawn Guoca2cc332011-06-25 02:04:35 +0800116enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000117 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800118 IMX27_FEC, /* runs on i.mx27/35/51 */
119 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000120 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000121 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800122 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800123};
124
125static const struct of_device_id fec_dt_ids[] = {
126 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
127 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
128 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000129 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000130 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800131 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800132 { /* sentinel */ }
133};
134MODULE_DEVICE_TABLE(of, fec_dt_ids);
135
Shawn Guo49da97d2011-01-05 21:13:11 +0000136static unsigned char macaddr[ETH_ALEN];
137module_param_array(macaddr, byte, NULL, 0);
138MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
139
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000140#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * Some hardware gets it MAC address out of local flash memory.
143 * if this is non-zero then assume it is the address to get MAC from.
144 */
145#if defined(CONFIG_NETtel)
146#define FEC_FLASHMAC 0xf0006006
147#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
148#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#elif defined(CONFIG_CANCam)
150#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000151#elif defined (CONFIG_M5272C3)
152#define FEC_FLASHMAC (0xffe04000 + 4)
153#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000154#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#else
156#define FEC_FLASHMAC 0
157#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800158#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000159
Jim Baxtercdffcf12013-07-02 22:52:56 +0100160/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100162#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100164#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jim Baxter4c09eed2013-04-19 08:10:49 +0000166/* FEC receive acceleration */
167#define FEC_RACC_IPDIS (1 << 1)
168#define FEC_RACC_PRODIS (1 << 2)
169#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Matt Waddel6b265292006-06-27 13:10:56 +1000172 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * size bits. Other FEC hardware does not, so we need to take that into
174 * account when setting it.
175 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000176#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100177 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
179#else
180#define OPT_FRAME_SIZE 0
181#endif
182
Bryan Wue6b043d2010-03-31 02:10:44 +0000183/* FEC MII MMFR bits definition */
184#define FEC_MMFR_ST (1 << 30)
185#define FEC_MMFR_OP_READ (2 << 28)
186#define FEC_MMFR_OP_WRITE (1 << 28)
187#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
188#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
189#define FEC_MMFR_TA (2 << 16)
190#define FEC_MMFR_DATA(v) (v & 0xffff)
Nimrod Andyde40ed32014-12-24 17:30:39 +0800191/* FEC ECR bits definition */
192#define FEC_ECR_MAGICEN (1 << 2)
193#define FEC_ECR_SLEEP (1 << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500195#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Sascha Hauer22f6b862009-04-15 01:32:18 +0000197/* Transmitter timeout */
198#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Frank Libaa70a52013-01-16 16:55:58 +0000200#define FEC_PAUSE_FLAG_AUTONEG 0x1
201#define FEC_PAUSE_FLAG_ENABLE 0x2
Nimrod Andyde40ed32014-12-24 17:30:39 +0800202#define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
203#define FEC_WOL_FLAG_ENABLE (0x1 << 1)
204#define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
Frank Libaa70a52013-01-16 16:55:58 +0000205
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800206#define COPYBREAK_DEFAULT 256
207
Nimrod Andy79f33912014-06-12 08:16:23 +0800208#define TSO_HEADER_SIZE 128
209/* Max number of allowed TCP segments for software TSO */
210#define FEC_MAX_TSO_SEGS 100
211#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
212
213#define IS_TSO_HEADER(txq, addr) \
214 ((addr >= txq->tso_hdrs_dma) && \
215 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
216
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000217static int mii_cnt;
218
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800219static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800220struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
221 struct fec_enet_private *fep,
222 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000223{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800224 struct bufdesc *new_bd = bdp + 1;
225 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800226 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
227 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800228 struct bufdesc_ex *ex_base;
229 struct bufdesc *base;
230 int ring_size;
231
Fugang Duan4d494cd2014-09-13 05:00:48 +0800232 if (bdp >= txq->tx_bd_base) {
233 base = txq->tx_bd_base;
234 ring_size = txq->tx_ring_size;
235 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800236 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800237 base = rxq->rx_bd_base;
238 ring_size = rxq->rx_ring_size;
239 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800240 }
241
242 if (fep->bufdesc_ex)
243 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
244 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000245 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800246 return (new_bd >= (base + ring_size)) ?
247 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000248}
249
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800250static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800251struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
252 struct fec_enet_private *fep,
253 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000254{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800255 struct bufdesc *new_bd = bdp - 1;
256 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800257 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
258 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800259 struct bufdesc_ex *ex_base;
260 struct bufdesc *base;
261 int ring_size;
262
Fugang Duan4d494cd2014-09-13 05:00:48 +0800263 if (bdp >= txq->tx_bd_base) {
264 base = txq->tx_bd_base;
265 ring_size = txq->tx_ring_size;
266 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800267 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800268 base = rxq->rx_bd_base;
269 ring_size = rxq->rx_ring_size;
270 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800271 }
272
273 if (fep->bufdesc_ex)
274 return (struct bufdesc *)((ex_new_bd < ex_base) ?
275 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000276 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800277 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000278}
279
Nimrod Andy61a44272014-06-12 08:16:18 +0800280static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
281 struct fec_enet_private *fep)
282{
283 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
284}
285
Fugang Duan4d494cd2014-09-13 05:00:48 +0800286static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
287 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800288{
289 int entries;
290
Fugang Duan4d494cd2014-09-13 05:00:48 +0800291 entries = ((const char *)txq->dirty_tx -
292 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800293
Fugang Duan4d494cd2014-09-13 05:00:48 +0800294 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800295}
296
Lothar Waßmannc20e5992014-11-17 10:51:24 +0100297static void swap_buffer(void *bufaddr, int len)
Shawn Guob5680e02011-01-05 21:13:13 +0000298{
299 int i;
300 unsigned int *buf = bufaddr;
301
Lothar Waßmann7b487d02014-11-17 10:51:23 +0100302 for (i = 0; i < len; i += 4, buf++)
Lothar Waßmanne4537892014-11-17 10:51:22 +0100303 swab32s(buf);
Shawn Guob5680e02011-01-05 21:13:13 +0000304}
305
Lothar Waßmann1310b542014-11-07 10:02:47 +0100306static void swap_buffer2(void *dst_buf, void *src_buf, int len)
307{
308 int i;
309 unsigned int *src = src_buf;
310 unsigned int *dst = dst_buf;
311
312 for (i = 0; i < len; i += 4, src++, dst++)
313 *dst = swab32p(src);
314}
315
Russell King344756f2014-07-08 13:01:59 +0100316static void fec_dump(struct net_device *ndev)
317{
318 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800319 struct bufdesc *bdp;
320 struct fec_enet_priv_tx_q *txq;
321 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100322
323 netdev_info(ndev, "TX ring dump\n");
324 pr_info("Nr SC addr len SKB\n");
325
Fugang Duan4d494cd2014-09-13 05:00:48 +0800326 txq = fep->tx_queue[0];
327 bdp = txq->tx_bd_base;
328
Russell King344756f2014-07-08 13:01:59 +0100329 do {
330 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
331 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800332 bdp == txq->cur_tx ? 'S' : ' ',
333 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100334 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800335 txq->tx_skbuff[index]);
336 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100337 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800338 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100339}
340
Fugang Duan62a02c92014-06-18 08:33:52 +0800341static inline bool is_ipv4_pkt(struct sk_buff *skb)
342{
343 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
344}
345
Jim Baxter4c09eed2013-04-19 08:10:49 +0000346static int
347fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
348{
349 /* Only run for packets requiring a checksum. */
350 if (skb->ip_summed != CHECKSUM_PARTIAL)
351 return 0;
352
353 if (unlikely(skb_cow_head(skb, 0)))
354 return -1;
355
Fugang Duan62a02c92014-06-18 08:33:52 +0800356 if (is_ipv4_pkt(skb))
357 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000358 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
359
360 return 0;
361}
362
Nimrod Andy6e909282014-06-12 08:16:22 +0800363static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800364fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
365 struct sk_buff *skb,
366 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800367{
368 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800369 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800370 struct bufdesc_ex *ebdp;
371 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800372 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800373 int frag, frag_len;
374 unsigned short status;
375 unsigned int estatus = 0;
376 skb_frag_t *this_frag;
377 unsigned int index;
378 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100379 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800380 int i;
381
382 for (frag = 0; frag < nr_frags; frag++) {
383 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800384 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800385 ebdp = (struct bufdesc_ex *)bdp;
386
387 status = bdp->cbd_sc;
388 status &= ~BD_ENET_TX_STATS;
389 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
390 frag_len = skb_shinfo(skb)->frags[frag].size;
391
392 /* Handle the last BD specially */
393 if (frag == nr_frags - 1) {
394 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
395 if (fep->bufdesc_ex) {
396 estatus |= BD_ENET_TX_INT;
397 if (unlikely(skb_shinfo(skb)->tx_flags &
398 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
399 estatus |= BD_ENET_TX_TS;
400 }
401 }
402
403 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100404 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800405 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800406 if (skb->ip_summed == CHECKSUM_PARTIAL)
407 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
408 ebdp->cbd_bdu = 0;
409 ebdp->cbd_esc = estatus;
410 }
411
412 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
413
Fugang Duan4d494cd2014-09-13 05:00:48 +0800414 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800415 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100416 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800417 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
418 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800419
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100420 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800421 swap_buffer(bufaddr, frag_len);
422 }
423
Russell Kingd6bf3142014-07-08 00:23:19 +0100424 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
425 DMA_TO_DEVICE);
426 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800427 dev_kfree_skb_any(skb);
428 if (net_ratelimit())
429 netdev_err(ndev, "Tx DMA memory map failed\n");
430 goto dma_mapping_error;
431 }
432
Russell Kingd6bf3142014-07-08 00:23:19 +0100433 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800434 bdp->cbd_datlen = frag_len;
435 bdp->cbd_sc = status;
436 }
437
Fugang Duan4d494cd2014-09-13 05:00:48 +0800438 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800439
440 return 0;
441
442dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800443 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800444 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800445 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800446 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
447 bdp->cbd_datlen, DMA_TO_DEVICE);
448 }
449 return NETDEV_TX_OK;
450}
451
Fugang Duan4d494cd2014-09-13 05:00:48 +0800452static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
453 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800454{
455 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800456 int nr_frags = skb_shinfo(skb)->nr_frags;
457 struct bufdesc *bdp, *last_bdp;
458 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100459 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800460 unsigned short status;
461 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800462 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800463 unsigned int estatus = 0;
464 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800465 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800466 int ret;
467
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;
Nimrod Andy6e909282014-06-12 08:16:22 +0800484 status = bdp->cbd_sc;
485 status &= ~BD_ENET_TX_STATS;
486
487 /* Set buffer length and buffer pointer */
488 bufaddr = skb->data;
489 buflen = skb_headlen(skb);
490
Fugang Duan4d494cd2014-09-13 05:00:48 +0800491 queue = skb_get_queue_mapping(skb);
492 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800493 if (((unsigned long) bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100494 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800495 memcpy(txq->tx_bounce[index], skb->data, buflen);
496 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800497
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100498 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy6e909282014-06-12 08:16:22 +0800499 swap_buffer(bufaddr, buflen);
500 }
501
Russell Kingd6bf3142014-07-08 00:23:19 +0100502 /* Push the data cache so the CPM does not get stale memory data. */
503 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
504 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800505 dev_kfree_skb_any(skb);
506 if (net_ratelimit())
507 netdev_err(ndev, "Tx DMA memory map failed\n");
508 return NETDEV_TX_OK;
509 }
510
511 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800512 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800513 if (ret)
514 return ret;
515 } else {
516 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
517 if (fep->bufdesc_ex) {
518 estatus = BD_ENET_TX_INT;
519 if (unlikely(skb_shinfo(skb)->tx_flags &
520 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
521 estatus |= BD_ENET_TX_TS;
522 }
523 }
524
525 if (fep->bufdesc_ex) {
526
527 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
528
529 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
530 fep->hwts_tx_en))
531 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
532
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100533 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800534 estatus |= FEC_TX_BD_FTYPE(queue);
535
Nimrod Andy6e909282014-06-12 08:16:22 +0800536 if (skb->ip_summed == CHECKSUM_PARTIAL)
537 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
538
539 ebdp->cbd_bdu = 0;
540 ebdp->cbd_esc = estatus;
541 }
542
Fugang Duan4d494cd2014-09-13 05:00:48 +0800543 last_bdp = txq->cur_tx;
544 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
Fugang Duan4d494cd2014-09-13 05:00:48 +0800562 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Frank Lide5fb0a2013-03-03 17:34:25 +0000564 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800565 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000566
Nimrod Andy6e909282014-06-12 08:16:22 +0800567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Nimrod Andy79f33912014-06-12 08:16:23 +0800570static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800571fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
572 struct net_device *ndev,
573 struct bufdesc *bdp, int index, char *data,
574 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800575{
576 struct fec_enet_private *fep = netdev_priv(ndev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200577 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800578 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800579 unsigned short status;
580 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100581 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800582
583 status = bdp->cbd_sc;
584 status &= ~BD_ENET_TX_STATS;
585
586 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800587
Fugang Duan41ef84c2014-09-13 05:00:54 +0800588 if (((unsigned long) data) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100589 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800590 memcpy(txq->tx_bounce[index], data, size);
591 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800592
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100593 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800594 swap_buffer(data, size);
595 }
596
Russell Kingd6bf3142014-07-08 00:23:19 +0100597 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
598 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800599 dev_kfree_skb_any(skb);
600 if (net_ratelimit())
601 netdev_err(ndev, "Tx DMA memory map failed\n");
602 return NETDEV_TX_BUSY;
603 }
604
Russell Kingd6bf3142014-07-08 00:23:19 +0100605 bdp->cbd_datlen = size;
606 bdp->cbd_bufaddr = addr;
607
Nimrod Andy79f33912014-06-12 08:16:23 +0800608 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100609 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800610 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800611 if (skb->ip_summed == CHECKSUM_PARTIAL)
612 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
613 ebdp->cbd_bdu = 0;
614 ebdp->cbd_esc = estatus;
615 }
616
617 /* Handle the last BD specially */
618 if (last_tcp)
619 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
620 if (is_last) {
621 status |= BD_ENET_TX_INTR;
622 if (fep->bufdesc_ex)
623 ebdp->cbd_esc |= BD_ENET_TX_INT;
624 }
625
626 bdp->cbd_sc = status;
627
628 return 0;
629}
630
631static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800632fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
633 struct sk_buff *skb, struct net_device *ndev,
634 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800635{
636 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800637 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200638 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800639 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800640 void *bufaddr;
641 unsigned long dmabuf;
642 unsigned short status;
643 unsigned int estatus = 0;
644
645 status = bdp->cbd_sc;
646 status &= ~BD_ENET_TX_STATS;
647 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
648
Fugang Duan4d494cd2014-09-13 05:00:48 +0800649 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
650 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800651 if (((unsigned long)bufaddr) & fep->tx_align ||
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100652 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800653 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
654 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800655
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100656 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
Nimrod Andy79f33912014-06-12 08:16:23 +0800657 swap_buffer(bufaddr, hdr_len);
658
659 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
660 hdr_len, DMA_TO_DEVICE);
661 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
662 dev_kfree_skb_any(skb);
663 if (net_ratelimit())
664 netdev_err(ndev, "Tx DMA memory map failed\n");
665 return NETDEV_TX_BUSY;
666 }
667 }
668
669 bdp->cbd_bufaddr = dmabuf;
670 bdp->cbd_datlen = hdr_len;
671
672 if (fep->bufdesc_ex) {
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100673 if (fep->quirks & FEC_QUIRK_HAS_AVB)
Nimrod Andybefe8212014-09-23 15:40:55 +0800674 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800675 if (skb->ip_summed == CHECKSUM_PARTIAL)
676 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
677 ebdp->cbd_bdu = 0;
678 ebdp->cbd_esc = estatus;
679 }
680
681 bdp->cbd_sc = status;
682
683 return 0;
684}
685
Fugang Duan4d494cd2014-09-13 05:00:48 +0800686static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
687 struct sk_buff *skb,
688 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800689{
690 struct fec_enet_private *fep = netdev_priv(ndev);
691 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
692 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800693 struct bufdesc *bdp = txq->cur_tx;
694 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800695 struct tso_t tso;
696 unsigned int index = 0;
697 int ret;
698
Fugang Duan4d494cd2014-09-13 05:00:48 +0800699 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800700 dev_kfree_skb_any(skb);
701 if (net_ratelimit())
702 netdev_err(ndev, "NOT enough BD for TSO!\n");
703 return NETDEV_TX_OK;
704 }
705
706 /* Protocol checksum off-load for TCP and UDP. */
707 if (fec_enet_clear_csum(skb, ndev)) {
708 dev_kfree_skb_any(skb);
709 return NETDEV_TX_OK;
710 }
711
712 /* Initialize the TSO handler, and prepare the first payload */
713 tso_start(skb, &tso);
714
715 total_len = skb->len - hdr_len;
716 while (total_len > 0) {
717 char *hdr;
718
Fugang Duan4d494cd2014-09-13 05:00:48 +0800719 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800720 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
721 total_len -= data_left;
722
723 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800724 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800725 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800726 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800727 if (ret)
728 goto err_release;
729
730 while (data_left > 0) {
731 int size;
732
733 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800734 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
735 index = fec_enet_get_bd_index(txq->tx_bd_base,
736 bdp, fep);
737 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
738 bdp, index,
739 tso.data, size,
740 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800741 total_len == 0);
742 if (ret)
743 goto err_release;
744
745 data_left -= size;
746 tso_build_data(skb, &tso, size);
747 }
748
Fugang Duan4d494cd2014-09-13 05:00:48 +0800749 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800750 }
751
752 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800753 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800754
Nimrod Andy79f33912014-06-12 08:16:23 +0800755 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800756 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800757
758 /* Trigger transmission start */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100759 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
Fugang Duan37d60172014-09-17 05:18:54 +0800760 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
761 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
762 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
763 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
764 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800765
766 return 0;
767
768err_release:
769 /* TODO: Release all used data descriptors for TSO */
770 return ret;
771}
772
Nimrod Andy61a44272014-06-12 08:16:18 +0800773static netdev_tx_t
774fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
775{
776 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800777 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800778 unsigned short queue;
779 struct fec_enet_priv_tx_q *txq;
780 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800781 int ret;
782
Fugang Duan4d494cd2014-09-13 05:00:48 +0800783 queue = skb_get_queue_mapping(skb);
784 txq = fep->tx_queue[queue];
785 nq = netdev_get_tx_queue(ndev, queue);
786
Nimrod Andy79f33912014-06-12 08:16:23 +0800787 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800788 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800789 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800790 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800791 if (ret)
792 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800793
Fugang Duan4d494cd2014-09-13 05:00:48 +0800794 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
795 if (entries_free <= txq->tx_stop_threshold)
796 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800797
798 return NETDEV_TX_OK;
799}
800
Frank Li14109a52013-03-26 16:12:03 +0000801/* Init RX & TX buffer descriptors
802 */
803static void fec_enet_bd_init(struct net_device *dev)
804{
805 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800806 struct fec_enet_priv_tx_q *txq;
807 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000808 struct bufdesc *bdp;
809 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800810 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000811
Frank Li59d0f7462014-09-13 05:00:50 +0800812 for (q = 0; q < fep->num_rx_queues; q++) {
813 /* Initialize the receive buffer descriptors. */
814 rxq = fep->rx_queue[q];
815 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800816
Frank Li59d0f7462014-09-13 05:00:50 +0800817 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000818
Frank Li59d0f7462014-09-13 05:00:50 +0800819 /* Initialize the BD for every fragment in the page. */
820 if (bdp->cbd_bufaddr)
821 bdp->cbd_sc = BD_ENET_RX_EMPTY;
822 else
823 bdp->cbd_sc = 0;
824 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000825 }
Frank Li59d0f7462014-09-13 05:00:50 +0800826
827 /* Set the last buffer to wrap */
828 bdp = fec_enet_get_prevdesc(bdp, fep, q);
829 bdp->cbd_sc |= BD_SC_WRAP;
830
831 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000832 }
833
Frank Li59d0f7462014-09-13 05:00:50 +0800834 for (q = 0; q < fep->num_tx_queues; q++) {
835 /* ...and the same for transmit */
836 txq = fep->tx_queue[q];
837 bdp = txq->tx_bd_base;
838 txq->cur_tx = bdp;
839
840 for (i = 0; i < txq->tx_ring_size; i++) {
841 /* Initialize the BD for every fragment in the page. */
842 bdp->cbd_sc = 0;
843 if (txq->tx_skbuff[i]) {
844 dev_kfree_skb_any(txq->tx_skbuff[i]);
845 txq->tx_skbuff[i] = NULL;
846 }
847 bdp->cbd_bufaddr = 0;
848 bdp = fec_enet_get_nextdesc(bdp, fep, q);
849 }
850
851 /* Set the last buffer to wrap */
852 bdp = fec_enet_get_prevdesc(bdp, fep, q);
853 bdp->cbd_sc |= BD_SC_WRAP;
854 txq->dirty_tx = bdp;
855 }
856}
857
Frank Lice99d0d2014-09-13 05:00:52 +0800858static void fec_enet_active_rxring(struct net_device *ndev)
859{
860 struct fec_enet_private *fep = netdev_priv(ndev);
861 int i;
862
863 for (i = 0; i < fep->num_rx_queues; i++)
864 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
865}
866
Frank Li59d0f7462014-09-13 05:00:50 +0800867static void fec_enet_enable_ring(struct net_device *ndev)
868{
869 struct fec_enet_private *fep = netdev_priv(ndev);
870 struct fec_enet_priv_tx_q *txq;
871 struct fec_enet_priv_rx_q *rxq;
872 int i;
873
874 for (i = 0; i < fep->num_rx_queues; i++) {
875 rxq = fep->rx_queue[i];
876 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
Nimrod Andyd543a762014-11-23 17:23:06 +0800877 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
Frank Li59d0f7462014-09-13 05:00:50 +0800878
879 /* enable DMA1/2 */
880 if (i)
881 writel(RCMR_MATCHEN | RCMR_CMP(i),
882 fep->hwp + FEC_RCMR(i));
883 }
884
885 for (i = 0; i < fep->num_tx_queues; i++) {
886 txq = fep->tx_queue[i];
887 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
888
889 /* enable DMA1/2 */
890 if (i)
891 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
892 fep->hwp + FEC_DMA_CFG(i));
893 }
894}
895
896static void fec_enet_reset_skb(struct net_device *ndev)
897{
898 struct fec_enet_private *fep = netdev_priv(ndev);
899 struct fec_enet_priv_tx_q *txq;
900 int i, j;
901
902 for (i = 0; i < fep->num_tx_queues; i++) {
903 txq = fep->tx_queue[i];
904
905 for (j = 0; j < txq->tx_ring_size; j++) {
906 if (txq->tx_skbuff[j]) {
907 dev_kfree_skb_any(txq->tx_skbuff[j]);
908 txq->tx_skbuff[j] = NULL;
909 }
910 }
911 }
Frank Li14109a52013-03-26 16:12:03 +0000912}
913
Russell Kingdbc64a82014-07-08 12:40:12 +0100914/*
915 * This function is called to start or restart the FEC during a link
916 * change, transmit timeout, or to reconfigure the FEC. The network
917 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100918 */
919static void
Russell Kingef833372014-07-08 12:40:43 +0100920fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100921{
922 struct fec_enet_private *fep = netdev_priv(ndev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000923 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100924 u32 temp_mac[2];
925 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000926 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100927
Fugang Duan106c3142014-09-13 05:00:51 +0800928 /* Whack a reset. We should wait for this.
929 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
930 * instead of reset MAC itself.
931 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100932 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan106c3142014-09-13 05:00:51 +0800933 writel(0, fep->hwp + FEC_ECNTRL);
934 } else {
935 writel(1, fep->hwp + FEC_ECNTRL);
936 udelay(10);
937 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100938
939 /*
940 * enet-mac reset will reset mac address registers too,
941 * so need to reconfigure it.
942 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100943 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100944 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
945 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
946 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
947 }
948
949 /* Clear any outstanding interrupt. */
Nimrod Andye17f7fe2014-12-11 09:20:32 +0800950 writel(0xffffffff, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100951
Frank Li14109a52013-03-26 16:12:03 +0000952 fec_enet_bd_init(ndev);
953
Frank Li59d0f7462014-09-13 05:00:50 +0800954 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100955
Frank Li59d0f7462014-09-13 05:00:50 +0800956 /* Reset tx SKB buffers. */
957 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100958
959 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100960 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100961 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100962 writel(0x04, fep->hwp + FEC_X_CNTRL);
963 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100964 /* No Rcv on Xmit */
965 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100966 writel(0x0, fep->hwp + FEC_X_CNTRL);
967 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100968
Uwe Kleine-König45993652011-01-19 20:47:04 +0100969 /* Set MII speed */
970 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
971
Guenter Roeckd1391932013-06-18 10:04:59 -0700972#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000973 /* set RX checksum */
974 val = readl(fep->hwp + FEC_RACC);
975 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
976 val |= FEC_RACC_OPTIONS;
977 else
978 val &= ~FEC_RACC_OPTIONS;
979 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700980#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000981
Uwe Kleine-König45993652011-01-19 20:47:04 +0100982 /*
983 * The phy interface and speed need to get configured
984 * differently on enet-mac.
985 */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +0100986 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100987 /* Enable flow control and length check */
988 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100989
Shawn Guo230dec62011-09-23 02:12:48 +0000990 /* RGMII, RMII or MII */
991 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
992 rcntl |= (1 << 6);
993 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100994 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100995 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100996 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100997
Shawn Guo230dec62011-09-23 02:12:48 +0000998 /* 1G, 100M or 10M */
999 if (fep->phy_dev) {
1000 if (fep->phy_dev->speed == SPEED_1000)
1001 ecntl |= (1 << 5);
1002 else if (fep->phy_dev->speed == SPEED_100)
1003 rcntl &= ~(1 << 9);
1004 else
1005 rcntl |= (1 << 9);
1006 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001007 } else {
1008#ifdef FEC_MIIGSK_ENR
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001009 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001010 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001011 /* disable the gasket and wait */
1012 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1013 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1014 udelay(1);
1015
1016 /*
1017 * configure the gasket:
1018 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001019 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020 */
Eric Benard8d82f212012-01-12 06:10:28 +00001021 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1022 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1023 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1024 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1025 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001026
1027 /* re-enable the gasket */
1028 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1029 }
1030#endif
1031 }
Frank Libaa70a52013-01-16 16:55:58 +00001032
Guenter Roeckd1391932013-06-18 10:04:59 -07001033#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001034 /* enable pause frame*/
1035 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1036 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1037 fep->phy_dev && fep->phy_dev->pause)) {
1038 rcntl |= FEC_ENET_FCE;
1039
Jim Baxter4c09eed2013-04-19 08:10:49 +00001040 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001041 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1042 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1043 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1044 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1045
1046 /* OPD */
1047 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1048 } else {
1049 rcntl &= ~FEC_ENET_FCE;
1050 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001051#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001052
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001053 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001054
Stefan Wahren84fe6182014-03-12 11:28:19 +01001055 /* Setup multicast filter. */
1056 set_multicast_list(ndev);
1057#ifndef CONFIG_M5272
1058 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1059 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1060#endif
1061
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001062 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001063 /* enable ENET endian swap */
1064 ecntl |= (1 << 8);
1065 /* enable ENET store and forward mode */
1066 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1067 }
1068
Frank Liff43da82013-01-03 16:04:23 +00001069 if (fep->bufdesc_ex)
1070 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001071
Chris Healy38ae92d2013-06-25 23:18:52 -07001072#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001073 /* Enable the MIB statistic event counters */
1074 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001075#endif
1076
Uwe Kleine-König45993652011-01-19 20:47:04 +01001077 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001078 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001079 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001080
Frank Liff43da82013-01-03 16:04:23 +00001081 if (fep->bufdesc_ex)
1082 fec_ptp_start_cyclecounter(ndev);
1083
Uwe Kleine-König45993652011-01-19 20:47:04 +01001084 /* Enable interrupts we wish to service */
Nimrod Andy0c5a3ae2014-12-11 09:20:33 +08001085 if (fep->link)
1086 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1087 else
1088 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001089
1090 /* Init the interrupt coalescing */
1091 fec_enet_itr_coal_init(ndev);
1092
Uwe Kleine-König45993652011-01-19 20:47:04 +01001093}
1094
1095static void
1096fec_stop(struct net_device *ndev)
1097{
1098 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001099 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001100 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Nimrod Andyde40ed32014-12-24 17:30:39 +08001101 u32 val;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001102
1103 /* We cannot expect a graceful transmit stop without link !!! */
1104 if (fep->link) {
1105 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1106 udelay(10);
1107 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001108 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001109 }
1110
Fugang Duan106c3142014-09-13 05:00:51 +08001111 /* Whack a reset. We should wait for this.
1112 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1113 * instead of reset MAC itself.
1114 */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001115 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1116 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1117 writel(0, fep->hwp + FEC_ECNTRL);
1118 } else {
1119 writel(1, fep->hwp + FEC_ECNTRL);
1120 udelay(10);
1121 }
1122 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duan106c3142014-09-13 05:00:51 +08001123 } else {
Nimrod Andyde40ed32014-12-24 17:30:39 +08001124 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1125 val = readl(fep->hwp + FEC_ECNTRL);
1126 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1127 writel(val, fep->hwp + FEC_ECNTRL);
1128
1129 if (pdata && pdata->sleep_mode_enable)
1130 pdata->sleep_mode_enable(true);
Fugang Duan106c3142014-09-13 05:00:51 +08001131 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001132 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
Shawn Guo230dec62011-09-23 02:12:48 +00001133
1134 /* We have to keep ENET enabled to have MII interrupt stay working */
Nimrod Andyde40ed32014-12-24 17:30:39 +08001135 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1136 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
Shawn Guo230dec62011-09-23 02:12:48 +00001137 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001138 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1139 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001140}
1141
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001144fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001146 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Russell King344756f2014-07-08 13:01:59 +01001148 fec_dump(ndev);
1149
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001150 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Russell King36cdc742014-07-08 13:01:44 +01001152 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001153}
1154
Russell King36cdc742014-07-08 13:01:44 +01001155static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001156{
1157 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001158 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001159 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001160
Russell King36cdc742014-07-08 13:01:44 +01001161 rtnl_lock();
1162 if (netif_device_present(ndev) || netif_running(ndev)) {
1163 napi_disable(&fep->napi);
1164 netif_tx_lock_bh(ndev);
1165 fec_restart(ndev);
1166 netif_wake_queue(ndev);
1167 netif_tx_unlock_bh(ndev);
1168 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001169 }
Russell King36cdc742014-07-08 13:01:44 +01001170 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001174fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1175 struct skb_shared_hwtstamps *hwtstamps)
1176{
1177 unsigned long flags;
1178 u64 ns;
1179
1180 spin_lock_irqsave(&fep->tmreg_lock, flags);
1181 ns = timecounter_cyc2time(&fep->tc, ts);
1182 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1183
1184 memset(hwtstamps, 0, sizeof(*hwtstamps));
1185 hwtstamps->hwtstamp = ns_to_ktime(ns);
1186}
1187
1188static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001189fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct fec_enet_private *fep;
Nimrod Andy2b995f62015-01-20 14:10:35 +08001192 struct bufdesc *bdp, *bdp_t;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001193 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001195 struct fec_enet_priv_tx_q *txq;
1196 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001197 int index = 0;
Nimrod Andy2b995f62015-01-20 14:10:35 +08001198 int i, bdnum;
Nimrod Andy79f33912014-06-12 08:16:23 +08001199 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001201 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001202
1203 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1204
1205 txq = fep->tx_queue[queue_id];
1206 /* get next bdp of dirty_tx */
1207 nq = netdev_get_tx_queue(ndev, queue_id);
1208 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Frank Lide5fb0a2013-03-03 17:34:25 +00001210 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001211 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001212
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001213 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001214
1215 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001216 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001217 break;
1218
Nimrod Andy2b995f62015-01-20 14:10:35 +08001219 bdp_t = bdp;
1220 bdnum = 1;
1221 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp_t, fep);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001222 skb = txq->tx_skbuff[index];
Nimrod Andy2b995f62015-01-20 14:10:35 +08001223 while (!skb) {
1224 bdp_t = fec_enet_get_nextdesc(bdp_t, fep, queue_id);
1225 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp_t, fep);
1226 skb = txq->tx_skbuff[index];
1227 bdnum++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001228 }
Nimrod Andy2b995f62015-01-20 14:10:35 +08001229 if (skb_shinfo(skb)->nr_frags &&
1230 (status = bdp_t->cbd_sc) & BD_ENET_TX_READY)
1231 break;
1232
1233 for (i = 0; i < bdnum; i++) {
1234 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
1235 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1236 bdp->cbd_datlen, DMA_TO_DEVICE);
1237 bdp->cbd_bufaddr = 0;
1238 if (i < bdnum - 1)
1239 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1240 }
1241 txq->tx_skbuff[index] = NULL;
Frank Lide5fb0a2013-03-03 17:34:25 +00001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001244 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 BD_ENET_TX_RL | BD_ENET_TX_UN |
1246 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001248 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001250 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001251 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001252 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001253 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001254 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001255 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001256 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001257 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001259 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001260 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
Frank Liff43da82013-01-03 16:04:23 +00001263 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1264 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001265 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001266 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001267
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001268 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001269 skb_tstamp_tx(skb, &shhwtstamps);
1270 }
Frank Liff43da82013-01-03 16:04:23 +00001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /* Deferred means some collisions occurred during transmit,
1273 * but we eventually sent the packet OK.
1274 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001275 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001276 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001277
Sascha Hauer22f6b862009-04-15 01:32:18 +00001278 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001280
Fugang Duan4d494cd2014-09-13 05:00:48 +08001281 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001282
Sascha Hauer22f6b862009-04-15 01:32:18 +00001283 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001284 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001285
Sascha Hauer22f6b862009-04-15 01:32:18 +00001286 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001288 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001289 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1290 if (entries_free >= txq->tx_wake_threshold)
1291 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Russell Kingccea2962014-07-08 13:01:38 +01001294
1295 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001296 if (bdp != txq->cur_tx &&
1297 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1298 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1299}
1300
1301static void
1302fec_enet_tx(struct net_device *ndev)
1303{
1304 struct fec_enet_private *fep = netdev_priv(ndev);
1305 u16 queue_id;
1306 /* First process class A queue, then Class B and Best Effort queue */
1307 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1308 clear_bit(queue_id, &fep->work_tx);
1309 fec_enet_tx_queue(ndev, queue_id);
1310 }
1311 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001314static int
1315fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1316{
1317 struct fec_enet_private *fep = netdev_priv(ndev);
1318 int off;
1319
1320 off = ((unsigned long)skb->data) & fep->rx_align;
1321 if (off)
1322 skb_reserve(skb, fep->rx_align + 1 - off);
1323
1324 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1325 FEC_ENET_RX_FRSIZE - fep->rx_align,
1326 DMA_FROM_DEVICE);
1327 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1328 if (net_ratelimit())
1329 netdev_err(ndev, "Rx DMA memory map failed\n");
1330 return -ENOMEM;
1331 }
1332
1333 return 0;
1334}
1335
1336static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001337 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001338{
1339 struct fec_enet_private *fep = netdev_priv(ndev);
1340 struct sk_buff *new_skb;
1341
1342 if (length > fep->rx_copybreak)
1343 return false;
1344
1345 new_skb = netdev_alloc_skb(ndev, length);
1346 if (!new_skb)
1347 return false;
1348
1349 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1350 FEC_ENET_RX_FRSIZE - fep->rx_align,
1351 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001352 if (!swap)
1353 memcpy(new_skb->data, (*skb)->data, length);
1354 else
1355 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001356 *skb = new_skb;
1357
1358 return true;
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/* During a receive, the cur_rx points to the current incoming buffer.
1362 * When we update through the ring, if the next incoming buffer has
1363 * not been given to the system, we just set the empty indicator,
1364 * effectively tossing the packet.
1365 */
Frank Lidc975382013-01-28 18:31:42 +00001366static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001367fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001369 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001370 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001371 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001372 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001373 struct sk_buff *skb_new = NULL;
1374 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ushort pkt_len;
1376 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001377 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001378 struct bufdesc_ex *ebdp = NULL;
1379 bool vlan_packet_rcvd = false;
1380 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001381 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001382 bool is_copybreak;
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001383 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001384
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001385#ifdef CONFIG_M532x
1386 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001387#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001388 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1389 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* First, grab all of the stats for the incoming packet.
1392 * These get messed up if we get called due to a busy condition.
1393 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001394 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Sascha Hauer22f6b862009-04-15 01:32:18 +00001396 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Frank Lidc975382013-01-28 18:31:42 +00001398 if (pkt_received >= budget)
1399 break;
1400 pkt_received++;
1401
Sascha Hauer22f6b862009-04-15 01:32:18 +00001402 /* Since we have allocated space to hold a complete frame,
1403 * the last indicator should be set.
1404 */
1405 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001406 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Russell Kingdb3421c2014-07-08 13:01:49 +01001408
Sascha Hauer22f6b862009-04-15 01:32:18 +00001409 /* Check for errors. */
1410 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001412 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001413 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1414 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001415 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001416 }
1417 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001418 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001419 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001420 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001421 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001422 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001424
1425 /* Report late collisions as a frame error.
1426 * On this error, the BD is closed, but we don't know what we
1427 * have in the buffer. So, just drop this frame on the floor.
1428 */
1429 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001430 ndev->stats.rx_errors++;
1431 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001432 goto rx_processing_done;
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Sascha Hauer22f6b862009-04-15 01:32:18 +00001435 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001436 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001437 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001438 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Fugang Duan4d494cd2014-09-13 05:00:48 +08001440 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001441 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001442
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001443 /* The packet length includes FCS, but we don't want to
1444 * include that when passing upstream as it messes up
1445 * bridging applications.
1446 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001447 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1448 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001449 if (!is_copybreak) {
1450 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1451 if (unlikely(!skb_new)) {
1452 ndev->stats.rx_dropped++;
1453 goto rx_processing_done;
1454 }
1455 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1456 FEC_ENET_RX_FRSIZE - fep->rx_align,
1457 DMA_FROM_DEVICE);
1458 }
1459
1460 prefetch(skb->data - NET_IP_ALIGN);
1461 skb_put(skb, pkt_len - 4);
1462 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001463 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001464 swap_buffer(data, pkt_len);
1465
Jim Baxtercdffcf12013-07-02 22:52:56 +01001466 /* Extract the enhanced buffer descriptor */
1467 ebdp = NULL;
1468 if (fep->bufdesc_ex)
1469 ebdp = (struct bufdesc_ex *)bdp;
1470
1471 /* If this is a VLAN packet remove the VLAN Tag */
1472 vlan_packet_rcvd = false;
1473 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001474 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001475 /* Push and remove the vlan tag */
1476 struct vlan_hdr *vlan_header =
1477 (struct vlan_hdr *) (data + ETH_HLEN);
1478 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001479
1480 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001481
Nimrod Andyaf5cbc92015-03-10 19:09:41 +08001482 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001483 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001484 }
1485
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001486 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001488 /* Get receive timestamp from the skb */
1489 if (fep->hwts_rx_en && fep->bufdesc_ex)
1490 fec_enet_hwtstamp(fep, ebdp->ts,
1491 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001492
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001493 if (fep->bufdesc_ex &&
1494 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1495 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1496 /* don't check it */
1497 skb->ip_summed = CHECKSUM_UNNECESSARY;
1498 } else {
1499 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001500 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001501 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001502
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001503 /* Handle received VLAN packets */
1504 if (vlan_packet_rcvd)
1505 __vlan_hwaccel_put_tag(skb,
1506 htons(ETH_P_8021Q),
1507 vlan_tag);
1508
1509 napi_gro_receive(&fep->napi, skb);
1510
1511 if (is_copybreak) {
1512 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1513 FEC_ENET_RX_FRSIZE - fep->rx_align,
1514 DMA_FROM_DEVICE);
1515 } else {
1516 rxq->rx_skbuff[index] = skb_new;
1517 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1518 }
1519
Sascha Hauer22f6b862009-04-15 01:32:18 +00001520rx_processing_done:
1521 /* Clear the status flags for this buffer */
1522 status &= ~BD_ENET_RX_STATS;
1523
1524 /* Mark the buffer empty */
1525 status |= BD_ENET_RX_EMPTY;
1526 bdp->cbd_sc = status;
1527
Frank Liff43da82013-01-03 16:04:23 +00001528 if (fep->bufdesc_ex) {
1529 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1530
1531 ebdp->cbd_esc = BD_ENET_RX_INT;
1532 ebdp->cbd_prot = 0;
1533 ebdp->cbd_bdu = 0;
1534 }
Frank Li6605b732012-10-30 18:25:31 +00001535
Sascha Hauer22f6b862009-04-15 01:32:18 +00001536 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001537 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001538
Sascha Hauer22f6b862009-04-15 01:32:18 +00001539 /* Doing this here will keep the FEC running while we process
1540 * incoming frames. On a heavily loaded network, we should be
1541 * able to keep up at the expense of system resources.
1542 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001543 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001545 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001546 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Fugang Duan4d494cd2014-09-13 05:00:48 +08001549static int
1550fec_enet_rx(struct net_device *ndev, int budget)
1551{
1552 int pkt_received = 0;
1553 u16 queue_id;
1554 struct fec_enet_private *fep = netdev_priv(ndev);
1555
1556 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1557 clear_bit(queue_id, &fep->work_rx);
1558 pkt_received += fec_enet_rx_queue(ndev,
1559 budget - pkt_received, queue_id);
1560 }
1561 return pkt_received;
1562}
1563
1564static bool
1565fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1566{
1567 if (int_events == 0)
1568 return false;
1569
1570 if (int_events & FEC_ENET_RXF)
1571 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001572 if (int_events & FEC_ENET_RXF_1)
1573 fep->work_rx |= (1 << 0);
1574 if (int_events & FEC_ENET_RXF_2)
1575 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001576
1577 if (int_events & FEC_ENET_TXF)
1578 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001579 if (int_events & FEC_ENET_TXF_1)
1580 fep->work_tx |= (1 << 0);
1581 if (int_events & FEC_ENET_TXF_2)
1582 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001583
1584 return true;
1585}
1586
Uwe Kleine-König45993652011-01-19 20:47:04 +01001587static irqreturn_t
1588fec_enet_interrupt(int irq, void *dev_id)
1589{
1590 struct net_device *ndev = dev_id;
1591 struct fec_enet_private *fep = netdev_priv(ndev);
1592 uint int_events;
1593 irqreturn_t ret = IRQ_NONE;
1594
Russell King7a168072014-07-08 00:22:44 +01001595 int_events = readl(fep->hwp + FEC_IEVENT);
Nimrod Andy94191fd2014-12-16 18:25:58 +08001596 writel(int_events, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001597 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001598
Fugang Duan61615cd2015-03-04 07:52:11 +08001599 if ((fep->work_tx || fep->work_rx) && fep->link) {
Russell King7a168072014-07-08 00:22:44 +01001600 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001601
Nimrod Andy94191fd2014-12-16 18:25:58 +08001602 if (napi_schedule_prep(&fep->napi)) {
1603 /* Disable the NAPI interrupts */
1604 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1605 __napi_schedule(&fep->napi);
1606 }
Russell King7a168072014-07-08 00:22:44 +01001607 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001608
Russell King7a168072014-07-08 00:22:44 +01001609 if (int_events & FEC_ENET_MII) {
1610 ret = IRQ_HANDLED;
1611 complete(&fep->mdio_done);
1612 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001613
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001614 if (fep->ptp_clock)
1615 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001616
Uwe Kleine-König45993652011-01-19 20:47:04 +01001617 return ret;
1618}
1619
Frank Lidc975382013-01-28 18:31:42 +00001620static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1621{
1622 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001623 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001624 int pkts;
1625
Russell King7a168072014-07-08 00:22:44 +01001626 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001627
Frank Lide5fb0a2013-03-03 17:34:25 +00001628 fec_enet_tx(ndev);
1629
Frank Lidc975382013-01-28 18:31:42 +00001630 if (pkts < budget) {
1631 napi_complete(napi);
1632 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1633 }
1634 return pkts;
1635}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001638static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001640 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001641 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001642 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Shawn Guo49da97d2011-01-05 21:13:11 +00001644 /*
1645 * try to get mac address in following order:
1646 *
1647 * 1) module parameter via kernel command line in form
1648 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1649 */
1650 iap = macaddr;
1651
1652 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001653 * 2) from device tree data
1654 */
1655 if (!is_valid_ether_addr(iap)) {
1656 struct device_node *np = fep->pdev->dev.of_node;
1657 if (np) {
1658 const char *mac = of_get_mac_address(np);
1659 if (mac)
1660 iap = (unsigned char *) mac;
1661 }
1662 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001663
1664 /*
1665 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001666 */
1667 if (!is_valid_ether_addr(iap)) {
1668#ifdef CONFIG_M5272
1669 if (FEC_FLASHMAC)
1670 iap = (unsigned char *)FEC_FLASHMAC;
1671#else
1672 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001673 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001674#endif
1675 }
1676
1677 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001678 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001679 */
1680 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001681 *((__be32 *) &tmpaddr[0]) =
1682 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1683 *((__be16 *) &tmpaddr[4]) =
1684 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 iap = &tmpaddr[0];
1686 }
1687
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001688 /*
1689 * 5) random mac address
1690 */
1691 if (!is_valid_ether_addr(iap)) {
1692 /* Report it and use a random ethernet address instead */
1693 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1694 eth_hw_addr_random(ndev);
1695 netdev_info(ndev, "Using random MAC address: %pM\n",
1696 ndev->dev_addr);
1697 return;
1698 }
1699
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001700 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Shawn Guo49da97d2011-01-05 21:13:11 +00001702 /* Adjust MAC if using macaddr */
1703 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001704 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707/* ------------------------------------------------------------------------- */
1708
Bryan Wue6b043d2010-03-31 02:10:44 +00001709/*
1710 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001712static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001714 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001715 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001716 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Bryan Wue6b043d2010-03-31 02:10:44 +00001718 /* Prevent a state halted on mii error */
1719 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1720 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001721 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001723
Russell King8ce56242014-07-08 12:40:07 +01001724 /*
1725 * If the netdev is down, or is going down, we're not interested
1726 * in link state events, so just mark our idea of the link as down
1727 * and ignore the event.
1728 */
1729 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1730 fep->link = 0;
1731 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001732 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001733 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001734 status_change = 1;
1735 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001736
Russell Kingef833372014-07-08 12:40:43 +01001737 if (fep->full_duplex != phy_dev->duplex) {
1738 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001739 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001740 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001741
1742 if (phy_dev->speed != fep->speed) {
1743 fep->speed = phy_dev->speed;
1744 status_change = 1;
1745 }
1746
1747 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001748 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001749 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001750 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001751 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001752 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001753 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001754 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001755 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001756 } else {
1757 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001758 napi_disable(&fep->napi);
1759 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001760 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001761 netif_tx_unlock_bh(ndev);
1762 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001763 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001764 status_change = 1;
1765 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001766 }
1767
Bryan Wue6b043d2010-03-31 02:10:44 +00001768 if (status_change)
1769 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Bryan Wue6b043d2010-03-31 02:10:44 +00001772static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Bryan Wue6b043d2010-03-31 02:10:44 +00001774 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001775 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001776
1777 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001778 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001779
1780 /* start a read op */
1781 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1782 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1783 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1784
1785 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001786 time_left = wait_for_completion_timeout(&fep->mdio_done,
1787 usecs_to_jiffies(FEC_MII_TIMEOUT));
1788 if (time_left == 0) {
1789 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001790 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001791 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001792 }
1793
1794 /* return value */
1795 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1796}
1797
1798static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1799 u16 value)
1800{
1801 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001802 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001803
1804 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001805 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001806
Shawn Guo862f0982011-01-05 21:13:09 +00001807 /* start a write op */
1808 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001809 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1810 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1811 fep->hwp + FEC_MII_DATA);
1812
1813 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001814 time_left = wait_for_completion_timeout(&fep->mdio_done,
1815 usecs_to_jiffies(FEC_MII_TIMEOUT));
1816 if (time_left == 0) {
1817 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001818 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001819 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001820 }
1821
1822 return 0;
1823}
1824
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001825static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1826{
1827 struct fec_enet_private *fep = netdev_priv(ndev);
1828 int ret;
1829
1830 if (enable) {
1831 ret = clk_prepare_enable(fep->clk_ahb);
1832 if (ret)
1833 return ret;
1834 ret = clk_prepare_enable(fep->clk_ipg);
1835 if (ret)
1836 goto failed_clk_ipg;
1837 if (fep->clk_enet_out) {
1838 ret = clk_prepare_enable(fep->clk_enet_out);
1839 if (ret)
1840 goto failed_clk_enet_out;
1841 }
1842 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001843 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001844 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001845 if (ret) {
1846 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001847 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001848 } else {
1849 fep->ptp_clk_on = true;
1850 }
1851 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001852 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001853 if (fep->clk_ref) {
1854 ret = clk_prepare_enable(fep->clk_ref);
1855 if (ret)
1856 goto failed_clk_ref;
1857 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001858 } else {
1859 clk_disable_unprepare(fep->clk_ahb);
1860 clk_disable_unprepare(fep->clk_ipg);
1861 if (fep->clk_enet_out)
1862 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001863 if (fep->clk_ptp) {
1864 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001865 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001866 fep->ptp_clk_on = false;
1867 mutex_unlock(&fep->ptp_clk_mutex);
1868 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001869 if (fep->clk_ref)
1870 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001871 }
1872
1873 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001874
1875failed_clk_ref:
1876 if (fep->clk_ref)
1877 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001878failed_clk_ptp:
1879 if (fep->clk_enet_out)
1880 clk_disable_unprepare(fep->clk_enet_out);
1881failed_clk_enet_out:
1882 clk_disable_unprepare(fep->clk_ipg);
1883failed_clk_ipg:
1884 clk_disable_unprepare(fep->clk_ahb);
1885
1886 return ret;
1887}
1888
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001889static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001890{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001891 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001892 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001893 char mdio_bus_id[MII_BUS_ID_SIZE];
1894 char phy_name[MII_BUS_ID_SIZE + 3];
1895 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001896 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001897
Bryan Wu418bd0d2010-05-28 03:40:39 -07001898 fep->phy_dev = NULL;
1899
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001900 if (fep->phy_node) {
1901 phy_dev = of_phy_connect(ndev, fep->phy_node,
1902 &fec_enet_adjust_link, 0,
1903 fep->phy_interface);
Nimrod Andy213a9922014-12-09 18:46:56 +08001904 if (!phy_dev)
1905 return -ENODEV;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001906 } else {
1907 /* check for attached phy */
1908 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1909 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1910 continue;
1911 if (fep->mii_bus->phy_map[phy_id] == NULL)
1912 continue;
1913 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1914 continue;
1915 if (dev_id--)
1916 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001917 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001918 break;
1919 }
1920
1921 if (phy_id >= PHY_MAX_ADDR) {
1922 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001923 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001924 phy_id = 0;
1925 }
1926
1927 snprintf(phy_name, sizeof(phy_name),
1928 PHY_ID_FMT, mdio_bus_id, phy_id);
1929 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1930 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001931 }
1932
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001933 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001934 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001935 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001936 }
1937
1938 /* mask with MAC supported features */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01001939 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001940 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001941 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001942#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001943 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001944#endif
Frank Libaa70a52013-01-16 16:55:58 +00001945 }
Shawn Guo230dec62011-09-23 02:12:48 +00001946 else
1947 phy_dev->supported &= PHY_BASIC_FEATURES;
1948
Bryan Wue6b043d2010-03-31 02:10:44 +00001949 phy_dev->advertising = phy_dev->supported;
1950
1951 fep->phy_dev = phy_dev;
1952 fep->link = 0;
1953 fep->full_duplex = 0;
1954
Joe Perches31b77202013-04-13 19:03:17 +00001955 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1956 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1957 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001958
Bryan Wue6b043d2010-03-31 02:10:44 +00001959 return 0;
1960}
1961
1962static int fec_enet_mii_init(struct platform_device *pdev)
1963{
Shawn Guob5680e02011-01-05 21:13:13 +00001964 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001965 struct net_device *ndev = platform_get_drvdata(pdev);
1966 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001967 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001968 int err = -ENXIO, i;
1969
Shawn Guob5680e02011-01-05 21:13:13 +00001970 /*
Stefan Agner3d125f92015-01-14 00:20:21 +01001971 * The i.MX28 dual fec interfaces are not equal.
Shawn Guob5680e02011-01-05 21:13:13 +00001972 * Here are the differences:
1973 *
1974 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1975 * - fec0 acts as the 1588 time master while fec1 is slave
1976 * - external phys can only be configured by fec0
1977 *
1978 * That is to say fec1 can not work independently. It only works
1979 * when fec0 is working. The reason behind this design is that the
1980 * second interface is added primarily for Switch mode.
1981 *
1982 * Because of the last point above, both phys are attached on fec0
1983 * mdio interface in board design, and need to be configured by
1984 * fec0 mii_bus.
1985 */
Stefan Agner3d125f92015-01-14 00:20:21 +01001986 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001987 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001988 if (mii_cnt && fec0_mii_bus) {
1989 fep->mii_bus = fec0_mii_bus;
1990 mii_cnt++;
1991 return 0;
1992 }
1993 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001994 }
1995
Bryan Wue6b043d2010-03-31 02:10:44 +00001996 fep->mii_timeout = 0;
1997
1998 /*
1999 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002000 *
2001 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2002 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2003 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2004 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002005 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08002006 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002007 if (fep->quirks & FEC_QUIRK_ENET_MAC)
Shawn Guo230dec62011-09-23 02:12:48 +00002008 fep->phy_speed--;
2009 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00002010 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2011
2012 fep->mii_bus = mdiobus_alloc();
2013 if (fep->mii_bus == NULL) {
2014 err = -ENOMEM;
2015 goto err_out;
2016 }
2017
2018 fep->mii_bus->name = "fec_enet_mii_bus";
2019 fep->mii_bus->read = fec_enet_mdio_read;
2020 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002021 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2022 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002023 fep->mii_bus->priv = fep;
2024 fep->mii_bus->parent = &pdev->dev;
2025
2026 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2027 if (!fep->mii_bus->irq) {
2028 err = -ENOMEM;
2029 goto err_out_free_mdiobus;
2030 }
2031
2032 for (i = 0; i < PHY_MAX_ADDR; i++)
2033 fep->mii_bus->irq[i] = PHY_POLL;
2034
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002035 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2036 if (node) {
2037 err = of_mdiobus_register(fep->mii_bus, node);
2038 of_node_put(node);
2039 } else {
2040 err = mdiobus_register(fep->mii_bus);
2041 }
2042
2043 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002044 goto err_out_free_mdio_irq;
2045
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002046 mii_cnt++;
2047
Shawn Guob5680e02011-01-05 21:13:13 +00002048 /* save fec0 mii_bus */
Stefan Agner3d125f92015-01-14 00:20:21 +01002049 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
Shawn Guob5680e02011-01-05 21:13:13 +00002050 fec0_mii_bus = fep->mii_bus;
2051
Bryan Wue6b043d2010-03-31 02:10:44 +00002052 return 0;
2053
Bryan Wue6b043d2010-03-31 02:10:44 +00002054err_out_free_mdio_irq:
2055 kfree(fep->mii_bus->irq);
2056err_out_free_mdiobus:
2057 mdiobus_free(fep->mii_bus);
2058err_out:
2059 return err;
2060}
2061
2062static void fec_enet_mii_remove(struct fec_enet_private *fep)
2063{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002064 if (--mii_cnt == 0) {
2065 mdiobus_unregister(fep->mii_bus);
2066 kfree(fep->mii_bus->irq);
2067 mdiobus_free(fep->mii_bus);
2068 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002069}
2070
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002071static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002072 struct ethtool_cmd *cmd)
2073{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002074 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002075 struct phy_device *phydev = fep->phy_dev;
2076
2077 if (!phydev)
2078 return -ENODEV;
2079
2080 return phy_ethtool_gset(phydev, cmd);
2081}
2082
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002083static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002084 struct ethtool_cmd *cmd)
2085{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002086 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002087 struct phy_device *phydev = fep->phy_dev;
2088
2089 if (!phydev)
2090 return -ENODEV;
2091
2092 return phy_ethtool_sset(phydev, cmd);
2093}
2094
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002095static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002096 struct ethtool_drvinfo *info)
2097{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002098 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Jiri Pirko7826d432013-01-06 00:44:26 +00002100 strlcpy(info->driver, fep->pdev->dev.driver->name,
2101 sizeof(info->driver));
2102 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2103 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
Bryan Wue6b043d2010-03-31 02:10:44 +00002105
Frank Li5ebae4892013-01-06 16:25:07 +00002106static int fec_enet_get_ts_info(struct net_device *ndev,
2107 struct ethtool_ts_info *info)
2108{
2109 struct fec_enet_private *fep = netdev_priv(ndev);
2110
2111 if (fep->bufdesc_ex) {
2112
2113 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2114 SOF_TIMESTAMPING_RX_SOFTWARE |
2115 SOF_TIMESTAMPING_SOFTWARE |
2116 SOF_TIMESTAMPING_TX_HARDWARE |
2117 SOF_TIMESTAMPING_RX_HARDWARE |
2118 SOF_TIMESTAMPING_RAW_HARDWARE;
2119 if (fep->ptp_clock)
2120 info->phc_index = ptp_clock_index(fep->ptp_clock);
2121 else
2122 info->phc_index = -1;
2123
2124 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2125 (1 << HWTSTAMP_TX_ON);
2126
2127 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2128 (1 << HWTSTAMP_FILTER_ALL);
2129 return 0;
2130 } else {
2131 return ethtool_op_get_ts_info(ndev, info);
2132 }
2133}
2134
Guenter Roeckd1391932013-06-18 10:04:59 -07002135#if !defined(CONFIG_M5272)
2136
Frank Libaa70a52013-01-16 16:55:58 +00002137static void fec_enet_get_pauseparam(struct net_device *ndev,
2138 struct ethtool_pauseparam *pause)
2139{
2140 struct fec_enet_private *fep = netdev_priv(ndev);
2141
2142 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2143 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2144 pause->rx_pause = pause->tx_pause;
2145}
2146
2147static int fec_enet_set_pauseparam(struct net_device *ndev,
2148 struct ethtool_pauseparam *pause)
2149{
2150 struct fec_enet_private *fep = netdev_priv(ndev);
2151
Russell King0b146ca2014-07-08 00:22:59 +01002152 if (!fep->phy_dev)
2153 return -ENODEV;
2154
Frank Libaa70a52013-01-16 16:55:58 +00002155 if (pause->tx_pause != pause->rx_pause) {
2156 netdev_info(ndev,
2157 "hardware only support enable/disable both tx and rx");
2158 return -EINVAL;
2159 }
2160
2161 fep->pause_flag = 0;
2162
2163 /* tx pause must be same as rx pause */
2164 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2165 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2166
2167 if (pause->rx_pause || pause->autoneg) {
2168 fep->phy_dev->supported |= ADVERTISED_Pause;
2169 fep->phy_dev->advertising |= ADVERTISED_Pause;
2170 } else {
2171 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2172 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2173 }
2174
2175 if (pause->autoneg) {
2176 if (netif_running(ndev))
2177 fec_stop(ndev);
2178 phy_start_aneg(fep->phy_dev);
2179 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002180 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002181 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002182 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002183 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002184 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002185 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002186 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002187 }
Frank Libaa70a52013-01-16 16:55:58 +00002188
2189 return 0;
2190}
2191
Chris Healy38ae92d2013-06-25 23:18:52 -07002192static const struct fec_stat {
2193 char name[ETH_GSTRING_LEN];
2194 u16 offset;
2195} fec_stats[] = {
2196 /* RMON TX */
2197 { "tx_dropped", RMON_T_DROP },
2198 { "tx_packets", RMON_T_PACKETS },
2199 { "tx_broadcast", RMON_T_BC_PKT },
2200 { "tx_multicast", RMON_T_MC_PKT },
2201 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2202 { "tx_undersize", RMON_T_UNDERSIZE },
2203 { "tx_oversize", RMON_T_OVERSIZE },
2204 { "tx_fragment", RMON_T_FRAG },
2205 { "tx_jabber", RMON_T_JAB },
2206 { "tx_collision", RMON_T_COL },
2207 { "tx_64byte", RMON_T_P64 },
2208 { "tx_65to127byte", RMON_T_P65TO127 },
2209 { "tx_128to255byte", RMON_T_P128TO255 },
2210 { "tx_256to511byte", RMON_T_P256TO511 },
2211 { "tx_512to1023byte", RMON_T_P512TO1023 },
2212 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2213 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2214 { "tx_octets", RMON_T_OCTETS },
2215
2216 /* IEEE TX */
2217 { "IEEE_tx_drop", IEEE_T_DROP },
2218 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2219 { "IEEE_tx_1col", IEEE_T_1COL },
2220 { "IEEE_tx_mcol", IEEE_T_MCOL },
2221 { "IEEE_tx_def", IEEE_T_DEF },
2222 { "IEEE_tx_lcol", IEEE_T_LCOL },
2223 { "IEEE_tx_excol", IEEE_T_EXCOL },
2224 { "IEEE_tx_macerr", IEEE_T_MACERR },
2225 { "IEEE_tx_cserr", IEEE_T_CSERR },
2226 { "IEEE_tx_sqe", IEEE_T_SQE },
2227 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2228 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2229
2230 /* RMON RX */
2231 { "rx_packets", RMON_R_PACKETS },
2232 { "rx_broadcast", RMON_R_BC_PKT },
2233 { "rx_multicast", RMON_R_MC_PKT },
2234 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2235 { "rx_undersize", RMON_R_UNDERSIZE },
2236 { "rx_oversize", RMON_R_OVERSIZE },
2237 { "rx_fragment", RMON_R_FRAG },
2238 { "rx_jabber", RMON_R_JAB },
2239 { "rx_64byte", RMON_R_P64 },
2240 { "rx_65to127byte", RMON_R_P65TO127 },
2241 { "rx_128to255byte", RMON_R_P128TO255 },
2242 { "rx_256to511byte", RMON_R_P256TO511 },
2243 { "rx_512to1023byte", RMON_R_P512TO1023 },
2244 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2245 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2246 { "rx_octets", RMON_R_OCTETS },
2247
2248 /* IEEE RX */
2249 { "IEEE_rx_drop", IEEE_R_DROP },
2250 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2251 { "IEEE_rx_crc", IEEE_R_CRC },
2252 { "IEEE_rx_align", IEEE_R_ALIGN },
2253 { "IEEE_rx_macerr", IEEE_R_MACERR },
2254 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2255 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2256};
2257
2258static void fec_enet_get_ethtool_stats(struct net_device *dev,
2259 struct ethtool_stats *stats, u64 *data)
2260{
2261 struct fec_enet_private *fep = netdev_priv(dev);
2262 int i;
2263
2264 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2265 data[i] = readl(fep->hwp + fec_stats[i].offset);
2266}
2267
2268static void fec_enet_get_strings(struct net_device *netdev,
2269 u32 stringset, u8 *data)
2270{
2271 int i;
2272 switch (stringset) {
2273 case ETH_SS_STATS:
2274 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2275 memcpy(data + i * ETH_GSTRING_LEN,
2276 fec_stats[i].name, ETH_GSTRING_LEN);
2277 break;
2278 }
2279}
2280
2281static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2282{
2283 switch (sset) {
2284 case ETH_SS_STATS:
2285 return ARRAY_SIZE(fec_stats);
2286 default:
2287 return -EOPNOTSUPP;
2288 }
2289}
Guenter Roeckd1391932013-06-18 10:04:59 -07002290#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002291
Chris Healy32bc9b42013-06-17 07:25:06 -07002292static int fec_enet_nway_reset(struct net_device *dev)
2293{
2294 struct fec_enet_private *fep = netdev_priv(dev);
2295 struct phy_device *phydev = fep->phy_dev;
2296
2297 if (!phydev)
2298 return -ENODEV;
2299
2300 return genphy_restart_aneg(phydev);
2301}
2302
Fugang Duand851b472014-09-17 05:18:52 +08002303/* ITR clock source is enet system clock (clk_ahb).
2304 * TCTT unit is cycle_ns * 64 cycle
2305 * So, the ICTT value = X us / (cycle_ns * 64)
2306 */
2307static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2308{
2309 struct fec_enet_private *fep = netdev_priv(ndev);
2310
2311 return us * (fep->itr_clk_rate / 64000) / 1000;
2312}
2313
2314/* Set threshold for interrupt coalescing */
2315static void fec_enet_itr_coal_set(struct net_device *ndev)
2316{
2317 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002318 int rx_itr, tx_itr;
2319
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002320 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002321 return;
2322
2323 /* Must be greater than zero to avoid unpredictable behavior */
2324 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2325 !fep->tx_time_itr || !fep->tx_pkts_itr)
2326 return;
2327
2328 /* Select enet system clock as Interrupt Coalescing
2329 * timer Clock Source
2330 */
2331 rx_itr = FEC_ITR_CLK_SEL;
2332 tx_itr = FEC_ITR_CLK_SEL;
2333
2334 /* set ICFT and ICTT */
2335 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2336 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2337 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2338 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2339
2340 rx_itr |= FEC_ITR_EN;
2341 tx_itr |= FEC_ITR_EN;
2342
2343 writel(tx_itr, fep->hwp + FEC_TXIC0);
2344 writel(rx_itr, fep->hwp + FEC_RXIC0);
2345 writel(tx_itr, fep->hwp + FEC_TXIC1);
2346 writel(rx_itr, fep->hwp + FEC_RXIC1);
2347 writel(tx_itr, fep->hwp + FEC_TXIC2);
2348 writel(rx_itr, fep->hwp + FEC_RXIC2);
2349}
2350
2351static int
2352fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2353{
2354 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002355
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002356 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002357 return -EOPNOTSUPP;
2358
2359 ec->rx_coalesce_usecs = fep->rx_time_itr;
2360 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2361
2362 ec->tx_coalesce_usecs = fep->tx_time_itr;
2363 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2364
2365 return 0;
2366}
2367
2368static int
2369fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2370{
2371 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duand851b472014-09-17 05:18:52 +08002372 unsigned int cycle;
2373
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01002374 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
Fugang Duand851b472014-09-17 05:18:52 +08002375 return -EOPNOTSUPP;
2376
2377 if (ec->rx_max_coalesced_frames > 255) {
2378 pr_err("Rx coalesced frames exceed hardware limiation");
2379 return -EINVAL;
2380 }
2381
2382 if (ec->tx_max_coalesced_frames > 255) {
2383 pr_err("Tx coalesced frame exceed hardware limiation");
2384 return -EINVAL;
2385 }
2386
2387 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2388 if (cycle > 0xFFFF) {
2389 pr_err("Rx coalesed usec exceeed hardware limiation");
2390 return -EINVAL;
2391 }
2392
2393 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2394 if (cycle > 0xFFFF) {
2395 pr_err("Rx coalesed usec exceeed hardware limiation");
2396 return -EINVAL;
2397 }
2398
2399 fep->rx_time_itr = ec->rx_coalesce_usecs;
2400 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2401
2402 fep->tx_time_itr = ec->tx_coalesce_usecs;
2403 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2404
2405 fec_enet_itr_coal_set(ndev);
2406
2407 return 0;
2408}
2409
2410static void fec_enet_itr_coal_init(struct net_device *ndev)
2411{
2412 struct ethtool_coalesce ec;
2413
2414 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2415 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2416
2417 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2418 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2419
2420 fec_enet_set_coalesce(ndev, &ec);
2421}
2422
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002423static int fec_enet_get_tunable(struct net_device *netdev,
2424 const struct ethtool_tunable *tuna,
2425 void *data)
2426{
2427 struct fec_enet_private *fep = netdev_priv(netdev);
2428 int ret = 0;
2429
2430 switch (tuna->id) {
2431 case ETHTOOL_RX_COPYBREAK:
2432 *(u32 *)data = fep->rx_copybreak;
2433 break;
2434 default:
2435 ret = -EINVAL;
2436 break;
2437 }
2438
2439 return ret;
2440}
2441
2442static int fec_enet_set_tunable(struct net_device *netdev,
2443 const struct ethtool_tunable *tuna,
2444 const void *data)
2445{
2446 struct fec_enet_private *fep = netdev_priv(netdev);
2447 int ret = 0;
2448
2449 switch (tuna->id) {
2450 case ETHTOOL_RX_COPYBREAK:
2451 fep->rx_copybreak = *(u32 *)data;
2452 break;
2453 default:
2454 ret = -EINVAL;
2455 break;
2456 }
2457
2458 return ret;
2459}
2460
Nimrod Andyde40ed32014-12-24 17:30:39 +08002461static void
2462fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2463{
2464 struct fec_enet_private *fep = netdev_priv(ndev);
2465
2466 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2467 wol->supported = WAKE_MAGIC;
2468 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2469 } else {
2470 wol->supported = wol->wolopts = 0;
2471 }
2472}
2473
2474static int
2475fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2476{
2477 struct fec_enet_private *fep = netdev_priv(ndev);
2478
2479 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2480 return -EINVAL;
2481
2482 if (wol->wolopts & ~WAKE_MAGIC)
2483 return -EINVAL;
2484
2485 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2486 if (device_may_wakeup(&ndev->dev)) {
2487 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2488 if (fep->irq[0] > 0)
2489 enable_irq_wake(fep->irq[0]);
2490 } else {
2491 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2492 if (fep->irq[0] > 0)
2493 disable_irq_wake(fep->irq[0]);
2494 }
2495
2496 return 0;
2497}
2498
stephen hemminger9b07be42012-01-04 12:59:49 +00002499static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002500 .get_settings = fec_enet_get_settings,
2501 .set_settings = fec_enet_set_settings,
2502 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002503 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002504 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002505 .get_coalesce = fec_enet_get_coalesce,
2506 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002507#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002508 .get_pauseparam = fec_enet_get_pauseparam,
2509 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002510 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002511 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002512 .get_sset_count = fec_enet_get_sset_count,
2513#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002514 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002515 .get_tunable = fec_enet_get_tunable,
2516 .set_tunable = fec_enet_set_tunable,
Nimrod Andyde40ed32014-12-24 17:30:39 +08002517 .get_wol = fec_enet_get_wol,
2518 .set_wol = fec_enet_set_wol,
Bryan Wue6b043d2010-03-31 02:10:44 +00002519};
2520
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002521static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002522{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002523 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002524 struct phy_device *phydev = fep->phy_dev;
2525
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002526 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002527 return -EINVAL;
2528
2529 if (!phydev)
2530 return -ENODEV;
2531
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002532 if (fep->bufdesc_ex) {
2533 if (cmd == SIOCSHWTSTAMP)
2534 return fec_ptp_set(ndev, rq);
2535 if (cmd == SIOCGHWTSTAMP)
2536 return fec_ptp_get(ndev, rq);
2537 }
Frank Liff43da82013-01-03 16:04:23 +00002538
Richard Cochran28b04112010-07-17 08:48:55 +00002539 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002542static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002543{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002544 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002545 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002546 struct sk_buff *skb;
2547 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002548 struct fec_enet_priv_tx_q *txq;
2549 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002550 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002551
Frank Li59d0f7462014-09-13 05:00:50 +08002552 for (q = 0; q < fep->num_rx_queues; q++) {
2553 rxq = fep->rx_queue[q];
2554 bdp = rxq->rx_bd_base;
2555 for (i = 0; i < rxq->rx_ring_size; i++) {
2556 skb = rxq->rx_skbuff[i];
2557 rxq->rx_skbuff[i] = NULL;
2558 if (skb) {
2559 dma_unmap_single(&fep->pdev->dev,
2560 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002561 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002562 DMA_FROM_DEVICE);
2563 dev_kfree_skb(skb);
2564 }
2565 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002566 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002567 }
2568
Frank Li59d0f7462014-09-13 05:00:50 +08002569 for (q = 0; q < fep->num_tx_queues; q++) {
2570 txq = fep->tx_queue[q];
2571 bdp = txq->tx_bd_base;
2572 for (i = 0; i < txq->tx_ring_size; i++) {
2573 kfree(txq->tx_bounce[i]);
2574 txq->tx_bounce[i] = NULL;
2575 skb = txq->tx_skbuff[i];
2576 txq->tx_skbuff[i] = NULL;
2577 dev_kfree_skb(skb);
2578 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002579 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002580}
2581
Frank Li59d0f7462014-09-13 05:00:50 +08002582static void fec_enet_free_queue(struct net_device *ndev)
2583{
2584 struct fec_enet_private *fep = netdev_priv(ndev);
2585 int i;
2586 struct fec_enet_priv_tx_q *txq;
2587
2588 for (i = 0; i < fep->num_tx_queues; i++)
2589 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2590 txq = fep->tx_queue[i];
2591 dma_free_coherent(NULL,
2592 txq->tx_ring_size * TSO_HEADER_SIZE,
2593 txq->tso_hdrs,
2594 txq->tso_hdrs_dma);
2595 }
2596
2597 for (i = 0; i < fep->num_rx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002598 kfree(fep->rx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002599 for (i = 0; i < fep->num_tx_queues; i++)
Markus Elfring1b4b32c2015-02-04 12:56:42 +01002600 kfree(fep->tx_queue[i]);
Frank Li59d0f7462014-09-13 05:00:50 +08002601}
2602
2603static int fec_enet_alloc_queue(struct net_device *ndev)
2604{
2605 struct fec_enet_private *fep = netdev_priv(ndev);
2606 int i;
2607 int ret = 0;
2608 struct fec_enet_priv_tx_q *txq;
2609
2610 for (i = 0; i < fep->num_tx_queues; i++) {
2611 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2612 if (!txq) {
2613 ret = -ENOMEM;
2614 goto alloc_failed;
2615 }
2616
2617 fep->tx_queue[i] = txq;
2618 txq->tx_ring_size = TX_RING_SIZE;
2619 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2620
2621 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2622 txq->tx_wake_threshold =
2623 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2624
2625 txq->tso_hdrs = dma_alloc_coherent(NULL,
2626 txq->tx_ring_size * TSO_HEADER_SIZE,
2627 &txq->tso_hdrs_dma,
2628 GFP_KERNEL);
2629 if (!txq->tso_hdrs) {
2630 ret = -ENOMEM;
2631 goto alloc_failed;
2632 }
2633 }
2634
2635 for (i = 0; i < fep->num_rx_queues; i++) {
2636 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2637 GFP_KERNEL);
2638 if (!fep->rx_queue[i]) {
2639 ret = -ENOMEM;
2640 goto alloc_failed;
2641 }
2642
2643 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2644 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2645 }
2646 return ret;
2647
2648alloc_failed:
2649 fec_enet_free_queue(ndev);
2650 return ret;
2651}
2652
2653static int
2654fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002655{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002656 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002657 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002658 struct sk_buff *skb;
2659 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002660 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002661
Frank Li59d0f7462014-09-13 05:00:50 +08002662 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002663 bdp = rxq->rx_bd_base;
2664 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002665 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002666 if (!skb)
2667 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002668
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002669 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002670 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002671 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002672 }
Russell King730ee362014-07-08 00:23:14 +01002673
Fugang Duan4d494cd2014-09-13 05:00:48 +08002674 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002675 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002676
2677 if (fep->bufdesc_ex) {
2678 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2679 ebdp->cbd_esc = BD_ENET_RX_INT;
2680 }
2681
Frank Li59d0f7462014-09-13 05:00:50 +08002682 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002683 }
2684
2685 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002686 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002687 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002688 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002689
Frank Li59d0f7462014-09-13 05:00:50 +08002690 err_alloc:
2691 fec_enet_free_buffers(ndev);
2692 return -ENOMEM;
2693}
2694
2695static int
2696fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2697{
2698 struct fec_enet_private *fep = netdev_priv(ndev);
2699 unsigned int i;
2700 struct bufdesc *bdp;
2701 struct fec_enet_priv_tx_q *txq;
2702
2703 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002704 bdp = txq->tx_bd_base;
2705 for (i = 0; i < txq->tx_ring_size; i++) {
2706 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2707 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002708 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002709
2710 bdp->cbd_sc = 0;
2711 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002712
Frank Liff43da82013-01-03 16:04:23 +00002713 if (fep->bufdesc_ex) {
2714 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002715 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002716 }
2717
Frank Li59d0f7462014-09-13 05:00:50 +08002718 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002719 }
2720
2721 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002722 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002723 bdp->cbd_sc |= BD_SC_WRAP;
2724
2725 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002726
2727 err_alloc:
2728 fec_enet_free_buffers(ndev);
2729 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002730}
2731
Frank Li59d0f7462014-09-13 05:00:50 +08002732static int fec_enet_alloc_buffers(struct net_device *ndev)
2733{
2734 struct fec_enet_private *fep = netdev_priv(ndev);
2735 unsigned int i;
2736
2737 for (i = 0; i < fep->num_rx_queues; i++)
2738 if (fec_enet_alloc_rxq_buffers(ndev, i))
2739 return -ENOMEM;
2740
2741 for (i = 0; i < fep->num_tx_queues; i++)
2742 if (fec_enet_alloc_txq_buffers(ndev, i))
2743 return -ENOMEM;
2744 return 0;
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002748fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002750 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002751 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002753 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002754 ret = fec_enet_clk_enable(ndev, true);
2755 if (ret)
2756 return ret;
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 /* I should reset the ring buffers here, but I don't yet know
2759 * a simple way to do that.
2760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002762 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002763 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002764 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002765
Bryan Wu418bd0d2010-05-28 03:40:39 -07002766 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002767 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002768 if (ret)
2769 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002770
Russell Kingef833372014-07-08 12:40:43 +01002771 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002772 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002773 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002774 netif_tx_start_all_queues(ndev);
2775
Nimrod Andyde40ed32014-12-24 17:30:39 +08002776 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2777 FEC_WOL_FLAG_ENABLE);
2778
Sascha Hauer22f6b862009-04-15 01:32:18 +00002779 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002780
2781err_enet_mii_probe:
2782 fec_enet_free_buffers(ndev);
2783err_enet_alloc:
2784 fec_enet_clk_enable(ndev, false);
2785 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002790fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002792 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Russell Kingd76cfae2014-07-08 00:23:04 +01002794 phy_stop(fep->phy_dev);
2795
Russell King31a6de32014-07-08 12:40:23 +01002796 if (netif_device_present(ndev)) {
2797 napi_disable(&fep->napi);
2798 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002799 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Russell King635cf172014-07-08 00:22:54 +01002802 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002803 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002804
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002805 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002806 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002807 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return 0;
2810}
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812/* Set or clear the multicast filter for this adaptor.
2813 * Skeleton taken from sunlance driver.
2814 * The CPM Ethernet implementation allows Multicast as well as individual
2815 * MAC address filtering. Some of the drivers check to make sure it is
2816 * a group multicast address, and discard those that are not. I guess I
2817 * will do the same for now, but just remove the test if you want
2818 * individual filtering as well (do the upper net layers want or support
2819 * this kind of feature?).
2820 */
2821
2822#define HASH_BITS 6 /* #bits in hash */
2823#define CRC32_POLY 0xEDB88320
2824
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002825static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002827 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002828 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002829 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 unsigned char hash;
2831
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002832 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002833 tmp = readl(fep->hwp + FEC_R_CNTRL);
2834 tmp |= 0x8;
2835 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002836 return;
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Sascha Hauer4e831832009-04-15 01:32:19 +00002839 tmp = readl(fep->hwp + FEC_R_CNTRL);
2840 tmp &= ~0x8;
2841 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002842
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002843 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002844 /* Catch all multicast addresses, so set the
2845 * filter to all 1's
2846 */
2847 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2848 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Sascha Hauer4e831832009-04-15 01:32:19 +00002850 return;
2851 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002852
Sascha Hauer4e831832009-04-15 01:32:19 +00002853 /* Clear filter and add the addresses in hash register
2854 */
2855 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2856 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002858 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002859 /* calculate crc32 value of mac address */
2860 crc = 0xffffffff;
2861
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002862 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002863 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002864 for (bit = 0; bit < 8; bit++, data >>= 1) {
2865 crc = (crc >> 1) ^
2866 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002869
2870 /* only upper 6 bits (HASH_BITS) are used
2871 * which point to specific bit in he hash registers
2872 */
2873 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2874
2875 if (hash > 31) {
2876 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2877 tmp |= 1 << (hash - 32);
2878 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2879 } else {
2880 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2881 tmp |= 1 << hash;
2882 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885}
2886
Sascha Hauer22f6b862009-04-15 01:32:18 +00002887/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002888static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002889fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002891 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002892 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Lucas Stach44934fa2014-03-30 21:32:08 +02002894 if (addr) {
2895 if (!is_valid_ether_addr(addr->sa_data))
2896 return -EADDRNOTAVAIL;
2897 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2898 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002899
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002900 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2901 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002902 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002903 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002904 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002908#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002909/**
2910 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002911 * @dev: The FEC network adapter
2912 *
2913 * Polled functionality used by netconsole and others in non interrupt mode
2914 *
2915 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002916static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002917{
2918 int i;
2919 struct fec_enet_private *fep = netdev_priv(dev);
2920
2921 for (i = 0; i < FEC_IRQ_NUM; i++) {
2922 if (fep->irq[i] > 0) {
2923 disable_irq(fep->irq[i]);
2924 fec_enet_interrupt(fep->irq[i], dev);
2925 enable_irq(fep->irq[i]);
2926 }
2927 }
2928}
2929#endif
2930
Russell King8506fa12014-07-08 12:40:33 +01002931#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
Nimrod Andy5bc26722014-10-13 10:53:48 +08002932static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00002933 netdev_features_t features)
2934{
2935 struct fec_enet_private *fep = netdev_priv(netdev);
2936 netdev_features_t changed = features ^ netdev->features;
2937
2938 netdev->features = features;
2939
2940 /* Receive checksum has been changed */
2941 if (changed & NETIF_F_RXCSUM) {
2942 if (features & NETIF_F_RXCSUM)
2943 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2944 else
2945 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002946 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08002947}
Jim Baxter4c09eed2013-04-19 08:10:49 +00002948
Nimrod Andy5bc26722014-10-13 10:53:48 +08002949static int fec_set_features(struct net_device *netdev,
2950 netdev_features_t features)
2951{
2952 struct fec_enet_private *fep = netdev_priv(netdev);
2953 netdev_features_t changed = features ^ netdev->features;
2954
Russell King8506fa12014-07-08 12:40:33 +01002955 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08002956 napi_disable(&fep->napi);
2957 netif_tx_lock_bh(netdev);
2958 fec_stop(netdev);
2959 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01002960 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002961 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002962 netif_tx_unlock_bh(netdev);
2963 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08002964 } else {
2965 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002966 }
2967
2968 return 0;
2969}
2970
Sascha Hauer009fda82009-04-15 01:32:23 +00002971static const struct net_device_ops fec_netdev_ops = {
2972 .ndo_open = fec_enet_open,
2973 .ndo_stop = fec_enet_close,
2974 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002975 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002976 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002977 .ndo_validate_addr = eth_validate_addr,
2978 .ndo_tx_timeout = fec_timeout,
2979 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002980 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002981#ifdef CONFIG_NET_POLL_CONTROLLER
2982 .ndo_poll_controller = fec_poll_controller,
2983#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002984 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002985};
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 /*
2988 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002989 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002991static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002993 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002994 struct fec_enet_priv_tx_q *txq;
2995 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002996 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002997 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002998 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002999 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08003000
Fugang Duan41ef84c2014-09-13 05:00:54 +08003001#if defined(CONFIG_ARM)
3002 fep->rx_align = 0xf;
3003 fep->tx_align = 0xf;
3004#else
3005 fep->rx_align = 0x3;
3006 fep->tx_align = 0x3;
3007#endif
3008
Frank Li59d0f7462014-09-13 05:00:50 +08003009 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08003010
Nimrod Andy55d02182014-06-12 08:16:21 +08003011 if (fep->bufdesc_ex)
3012 fep->bufdesc_size = sizeof(struct bufdesc_ex);
3013 else
3014 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003015 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08003016 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003018 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08003019 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00003020 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003021 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003022 return -ENOMEM;
3023 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003024
Fugang Duan4d494cd2014-09-13 05:00:48 +08003025 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Shawn Guo49da97d2011-01-05 21:13:11 +00003027 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003028 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003029 /* make sure MAC we just acquired is programmed into the hw */
3030 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003032 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003033 for (i = 0; i < fep->num_rx_queues; i++) {
3034 rxq = fep->rx_queue[i];
3035 rxq->index = i;
3036 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3037 rxq->bd_dma = bd_dma;
3038 if (fep->bufdesc_ex) {
3039 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3040 cbd_base = (struct bufdesc *)
3041 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3042 } else {
3043 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3044 cbd_base += rxq->rx_ring_size;
3045 }
3046 }
3047
3048 for (i = 0; i < fep->num_tx_queues; i++) {
3049 txq = fep->tx_queue[i];
3050 txq->index = i;
3051 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3052 txq->bd_dma = bd_dma;
3053 if (fep->bufdesc_ex) {
3054 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3055 cbd_base = (struct bufdesc *)
3056 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3057 } else {
3058 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3059 cbd_base += txq->tx_ring_size;
3060 }
3061 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Sascha Hauer22f6b862009-04-15 01:32:18 +00003064 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003065 ndev->watchdog_timeo = TX_TIMEOUT;
3066 ndev->netdev_ops = &fec_netdev_ops;
3067 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003068
Frank Lidc975382013-01-28 18:31:42 +00003069 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003070 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003071
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003072 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003073 /* enable hw VLAN support */
3074 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003075
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003076 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003077 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3078
Shawn Guo48496252013-05-08 21:08:22 +00003079 /* enable hw accelerator */
3080 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003081 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003082 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3083 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003084
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003085 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
Fugang Duan41ef84c2014-09-13 05:00:54 +08003086 fep->tx_align = 0;
3087 fep->rx_align = 0x3f;
3088 }
3089
Nimrod Andy09d1e542014-06-12 08:16:20 +08003090 ndev->hw_features = ndev->features;
3091
Russell Kingef833372014-07-08 12:40:43 +01003092 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return 0;
3095}
3096
Shawn Guoca2cc332011-06-25 02:04:35 +08003097#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003098static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003099{
3100 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003101 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003102 struct device_node *np = pdev->dev.of_node;
3103
3104 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003105 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003106
Shawn Guoa3caad02012-06-27 03:45:24 +00003107 of_property_read_u32(np, "phy-reset-duration", &msec);
3108 /* A sane reset duration should not be longer than 1s */
3109 if (msec > 1000)
3110 msec = 1;
3111
Shawn Guoca2cc332011-06-25 02:04:35 +08003112 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003113 if (!gpio_is_valid(phy_reset))
3114 return;
3115
Shawn Guo119fc002012-06-27 03:45:22 +00003116 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3117 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003118 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003119 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003120 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003121 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003122 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003123 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003124}
3125#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003126static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003127{
3128 /*
3129 * In case of platform probe, the reset has been done
3130 * by machine code.
3131 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003132}
3133#endif /* CONFIG_OF */
3134
Fugang Duan9fc095f2014-09-13 05:00:49 +08003135static void
3136fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3137{
3138 struct device_node *np = pdev->dev.of_node;
3139 int err;
3140
3141 *num_tx = *num_rx = 1;
3142
3143 if (!np || !of_device_is_available(np))
3144 return;
3145
3146 /* parse the num of tx and rx queues */
3147 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003148 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003149 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003150
3151 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3152 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003153 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003154
3155 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003156 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3157 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003158 *num_tx = 1;
3159 return;
3160 }
3161
3162 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003163 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3164 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003165 *num_rx = 1;
3166 return;
3167 }
3168
3169}
3170
Bill Pemberton33897cc2012-12-03 09:23:58 -05003171static int
Sascha Haueread73182009-01-28 23:03:11 +00003172fec_probe(struct platform_device *pdev)
3173{
3174 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003175 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003176 struct net_device *ndev;
3177 int i, irq, ret = 0;
3178 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003179 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003180 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003181 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003182 int num_tx_qs;
3183 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003184
Fugang Duan9fc095f2014-09-13 05:00:49 +08003185 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3186
Sascha Haueread73182009-01-28 23:03:11 +00003187 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003188 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3189 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003190 if (!ndev)
3191 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003192
3193 SET_NETDEV_DEV(ndev, &pdev->dev);
3194
3195 /* setup board info structure */
3196 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003197
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003198 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3199 if (of_id)
3200 pdev->id_entry = of_id->data;
3201 fep->quirks = pdev->id_entry->driver_data;
3202
Hubert Feurstein0c818592015-01-07 14:48:17 +01003203 fep->netdev = ndev;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003204 fep->num_rx_queues = num_rx_qs;
3205 fep->num_tx_queues = num_tx_qs;
3206
Guenter Roeckd1391932013-06-18 10:04:59 -07003207#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003208 /* default enable pause frame auto negotiation */
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003209 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
Frank Libaa70a52013-01-16 16:55:58 +00003210 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003211#endif
Frank Libaa70a52013-01-16 16:55:58 +00003212
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003213 /* Select default pin state */
3214 pinctrl_pm_select_default_state(&pdev->dev);
3215
Fabio Estevam399db752013-07-21 13:25:03 -03003216 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303217 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3218 if (IS_ERR(fep->hwp)) {
3219 ret = PTR_ERR(fep->hwp);
3220 goto failed_ioremap;
3221 }
3222
Bryan Wue6b043d2010-03-31 02:10:44 +00003223 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003224 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003225
Sascha Haueread73182009-01-28 23:03:11 +00003226 platform_set_drvdata(pdev, ndev);
3227
Nimrod Andyde40ed32014-12-24 17:30:39 +08003228 if (of_get_property(np, "fsl,magic-packet", NULL))
3229 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3230
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003231 phy_node = of_parse_phandle(np, "phy-handle", 0);
3232 if (!phy_node && of_phy_is_fixed_link(np)) {
3233 ret = of_phy_register_fixed_link(np);
3234 if (ret < 0) {
3235 dev_err(&pdev->dev,
3236 "broken fixed-link specification\n");
3237 goto failed_phy;
3238 }
3239 phy_node = of_node_get(np);
3240 }
3241 fep->phy_node = phy_node;
3242
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003243 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003244 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003245 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003246 if (pdata)
3247 fep->phy_interface = pdata->phy;
3248 else
3249 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3250 } else {
3251 fep->phy_interface = ret;
3252 }
3253
Fabio Estevame2f8d552013-02-22 06:40:45 +00003254 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3255 if (IS_ERR(fep->clk_ipg)) {
3256 ret = PTR_ERR(fep->clk_ipg);
3257 goto failed_clk;
3258 }
3259
3260 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3261 if (IS_ERR(fep->clk_ahb)) {
3262 ret = PTR_ERR(fep->clk_ahb);
3263 goto failed_clk;
3264 }
3265
Fugang Duand851b472014-09-17 05:18:52 +08003266 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3267
Linus Torvalds38f56f32013-05-07 11:06:17 -07003268 /* enet_out is optional, depends on board */
3269 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3270 if (IS_ERR(fep->clk_enet_out))
3271 fep->clk_enet_out = NULL;
3272
Nimrod Andy91c0d982014-08-21 17:09:38 +08003273 fep->ptp_clk_on = false;
3274 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003275
3276 /* clk_ref is optional, depends on board */
3277 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3278 if (IS_ERR(fep->clk_ref))
3279 fep->clk_ref = NULL;
3280
Lothar Waßmann6b7e4002014-11-17 10:51:21 +01003281 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003282 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevame2f8d552013-02-22 06:40:45 +00003283 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003284 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003285 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003286 }
3287
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003288 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003289 if (ret)
3290 goto failed_clk;
3291
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003292 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3293 if (!IS_ERR(fep->reg_phy)) {
3294 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003295 if (ret) {
3296 dev_err(&pdev->dev,
3297 "Failed to enable phy regulator: %d\n", ret);
3298 goto failed_regulator;
3299 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003300 } else {
3301 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003302 }
3303
3304 fec_reset_phy(pdev);
3305
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003306 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003307 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003308
3309 ret = fec_enet_init(ndev);
3310 if (ret)
3311 goto failed_init;
3312
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003313 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003314 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003315 if (irq < 0) {
3316 if (i)
3317 break;
3318 ret = irq;
3319 goto failed_irq;
3320 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003321 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003322 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003323 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003324 goto failed_irq;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003325
3326 fep->irq[i] = irq;
Sascha Haueread73182009-01-28 23:03:11 +00003327 }
3328
Fugang Duanb4d39b52014-09-13 05:00:55 +08003329 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003330 ret = fec_enet_mii_init(pdev);
3331 if (ret)
3332 goto failed_mii_init;
3333
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003334 /* Carrier starts down, phylib will bring it up */
3335 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003336 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003337 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003338
Sascha Haueread73182009-01-28 23:03:11 +00003339 ret = register_netdev(ndev);
3340 if (ret)
3341 goto failed_register;
3342
Nimrod Andyde40ed32014-12-24 17:30:39 +08003343 device_init_wakeup(&ndev->dev, fep->wol_flag &
3344 FEC_WOL_HAS_MAGIC_PACKET);
3345
Fabio Estevameb1d0642013-04-13 07:25:36 +00003346 if (fep->bufdesc_ex && fep->ptp_clock)
3347 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3348
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003349 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003350 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003351 return 0;
3352
3353failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003354 fec_enet_mii_remove(fep);
3355failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003356failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003357failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003358 if (fep->reg_phy)
3359 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003360failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003361 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003362failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003363failed_phy:
3364 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003365failed_ioremap:
3366 free_netdev(ndev);
3367
3368 return ret;
3369}
3370
Bill Pemberton33897cc2012-12-03 09:23:58 -05003371static int
Sascha Haueread73182009-01-28 23:03:11 +00003372fec_drv_remove(struct platform_device *pdev)
3373{
3374 struct net_device *ndev = platform_get_drvdata(pdev);
3375 struct fec_enet_private *fep = netdev_priv(ndev);
3376
Nimrod Andy91c0d982014-08-21 17:09:38 +08003377 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003378 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003379 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003380 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003381 if (fep->reg_phy)
3382 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003383 if (fep->ptp_clock)
3384 ptp_clock_unregister(fep->ptp_clock);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003385 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003386 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003387
Sascha Haueread73182009-01-28 23:03:11 +00003388 return 0;
3389}
3390
Fabio Estevamdd66d382014-07-24 18:24:01 -03003391static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003392{
Eric Benard87cad5c2010-06-18 04:19:54 +00003393 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003394 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003395
Russell Kingda1774e2014-07-08 12:39:57 +01003396 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003397 if (netif_running(ndev)) {
Nimrod Andyde40ed32014-12-24 17:30:39 +08003398 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3399 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
Russell Kingd76cfae2014-07-08 00:23:04 +01003400 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003401 napi_disable(&fep->napi);
3402 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003403 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003404 netif_tx_unlock_bh(ndev);
3405 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003406 fec_enet_clk_enable(ndev, false);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003407 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3408 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003409 }
Russell Kingda1774e2014-07-08 12:39:57 +01003410 rtnl_unlock();
3411
Nimrod Andyde40ed32014-12-24 17:30:39 +08003412 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003413 regulator_disable(fep->reg_phy);
3414
Nimrod Andy858eeb72014-12-11 09:20:31 +08003415 /* SOC supply clock to phy, when clock is disabled, phy link down
3416 * SOC control phy regulator, when regulator is disabled, phy link down
3417 */
3418 if (fep->clk_enet_out || fep->reg_phy)
3419 fep->link = 0;
3420
Sascha Haueread73182009-01-28 23:03:11 +00003421 return 0;
3422}
3423
Fabio Estevamdd66d382014-07-24 18:24:01 -03003424static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003425{
Eric Benard87cad5c2010-06-18 04:19:54 +00003426 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003427 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andyde40ed32014-12-24 17:30:39 +08003428 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003429 int ret;
Nimrod Andyde40ed32014-12-24 17:30:39 +08003430 int val;
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003431
Nimrod Andyde40ed32014-12-24 17:30:39 +08003432 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003433 ret = regulator_enable(fep->reg_phy);
3434 if (ret)
3435 return ret;
3436 }
Sascha Haueread73182009-01-28 23:03:11 +00003437
Russell Kingda1774e2014-07-08 12:39:57 +01003438 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003439 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003440 ret = fec_enet_clk_enable(ndev, true);
3441 if (ret) {
3442 rtnl_unlock();
3443 goto failed_clk;
3444 }
Nimrod Andyde40ed32014-12-24 17:30:39 +08003445 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3446 if (pdata && pdata->sleep_mode_enable)
3447 pdata->sleep_mode_enable(false);
3448 val = readl(fep->hwp + FEC_ECNTRL);
3449 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3450 writel(val, fep->hwp + FEC_ECNTRL);
3451 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3452 } else {
3453 pinctrl_pm_select_default_state(&fep->pdev->dev);
3454 }
Russell Kingef833372014-07-08 12:40:43 +01003455 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003456 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003457 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003458 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003459 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003460 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003461 }
Russell Kingda1774e2014-07-08 12:39:57 +01003462 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003463
Sascha Haueread73182009-01-28 23:03:11 +00003464 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003465
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003466failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003467 if (fep->reg_phy)
3468 regulator_disable(fep->reg_phy);
3469 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003470}
3471
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003472static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003473
Sascha Haueread73182009-01-28 23:03:11 +00003474static struct platform_driver fec_driver = {
3475 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003476 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003477 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003478 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003479 },
Shawn Guob5680e02011-01-05 21:13:13 +00003480 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003481 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003482 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003483};
3484
Fabio Estevamaaca2372012-01-23 16:44:50 +00003485module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003487MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488MODULE_LICENSE("GPL");