blob: f3a6379c12a2a25289b809a69c8e390283d2ee17 [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",
94 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +080095 }, {
Shawn Guo230dec62011-09-23 02:12:48 +000096 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +000097 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +010098 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +080099 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000100 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000101 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000102 .driver_data = FEC_QUIRK_ENET_MAC,
103 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800104 .name = "imx6sx-fec",
105 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
106 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800107 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
Nimrod Andy28b5f052014-10-15 17:30:12 +0800108 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE,
Fugang Duan95a77472014-09-13 05:00:47 +0800109 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800110 /* sentinel */
111 }
Shawn Guob5680e02011-01-05 21:13:13 +0000112};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800113MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000114
Shawn Guoca2cc332011-06-25 02:04:35 +0800115enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000116 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800117 IMX27_FEC, /* runs on i.mx27/35/51 */
118 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000119 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000120 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800121 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800122};
123
124static const struct of_device_id fec_dt_ids[] = {
125 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
126 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
127 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000128 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000129 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800130 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800131 { /* sentinel */ }
132};
133MODULE_DEVICE_TABLE(of, fec_dt_ids);
134
Shawn Guo49da97d2011-01-05 21:13:11 +0000135static unsigned char macaddr[ETH_ALEN];
136module_param_array(macaddr, byte, NULL, 0);
137MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
138
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000139#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Some hardware gets it MAC address out of local flash memory.
142 * if this is non-zero then assume it is the address to get MAC from.
143 */
144#if defined(CONFIG_NETtel)
145#define FEC_FLASHMAC 0xf0006006
146#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
147#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#elif defined(CONFIG_CANCam)
149#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000150#elif defined (CONFIG_M5272C3)
151#define FEC_FLASHMAC (0xffe04000 + 4)
152#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000153#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#else
155#define FEC_FLASHMAC 0
156#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800157#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000158
Jim Baxtercdffcf12013-07-02 22:52:56 +0100159/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100161#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100163#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jim Baxter4c09eed2013-04-19 08:10:49 +0000165/* FEC receive acceleration */
166#define FEC_RACC_IPDIS (1 << 1)
167#define FEC_RACC_PRODIS (1 << 2)
168#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Matt Waddel6b265292006-06-27 13:10:56 +1000171 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * size bits. Other FEC hardware does not, so we need to take that into
173 * account when setting it.
174 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000175#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100176 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
178#else
179#define OPT_FRAME_SIZE 0
180#endif
181
Bryan Wue6b043d2010-03-31 02:10:44 +0000182/* FEC MII MMFR bits definition */
183#define FEC_MMFR_ST (1 << 30)
184#define FEC_MMFR_OP_READ (2 << 28)
185#define FEC_MMFR_OP_WRITE (1 << 28)
186#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
187#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
188#define FEC_MMFR_TA (2 << 16)
189#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500191#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Sascha Hauer22f6b862009-04-15 01:32:18 +0000193/* Transmitter timeout */
194#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Frank Libaa70a52013-01-16 16:55:58 +0000196#define FEC_PAUSE_FLAG_AUTONEG 0x1
197#define FEC_PAUSE_FLAG_ENABLE 0x2
198
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800199#define COPYBREAK_DEFAULT 256
200
Nimrod Andy79f33912014-06-12 08:16:23 +0800201#define TSO_HEADER_SIZE 128
202/* Max number of allowed TCP segments for software TSO */
203#define FEC_MAX_TSO_SEGS 100
204#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
205
206#define IS_TSO_HEADER(txq, addr) \
207 ((addr >= txq->tso_hdrs_dma) && \
208 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
209
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000210static int mii_cnt;
211
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800212static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800213struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
214 struct fec_enet_private *fep,
215 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000216{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800217 struct bufdesc *new_bd = bdp + 1;
218 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800219 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
220 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800221 struct bufdesc_ex *ex_base;
222 struct bufdesc *base;
223 int ring_size;
224
Fugang Duan4d494cd2014-09-13 05:00:48 +0800225 if (bdp >= txq->tx_bd_base) {
226 base = txq->tx_bd_base;
227 ring_size = txq->tx_ring_size;
228 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800229 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800230 base = rxq->rx_bd_base;
231 ring_size = rxq->rx_ring_size;
232 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800233 }
234
235 if (fep->bufdesc_ex)
236 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
237 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000238 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800239 return (new_bd >= (base + ring_size)) ?
240 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000241}
242
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800243static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800244struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
245 struct fec_enet_private *fep,
246 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000247{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800248 struct bufdesc *new_bd = bdp - 1;
249 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800250 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
251 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800252 struct bufdesc_ex *ex_base;
253 struct bufdesc *base;
254 int ring_size;
255
Fugang Duan4d494cd2014-09-13 05:00:48 +0800256 if (bdp >= txq->tx_bd_base) {
257 base = txq->tx_bd_base;
258 ring_size = txq->tx_ring_size;
259 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800260 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800261 base = rxq->rx_bd_base;
262 ring_size = rxq->rx_ring_size;
263 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800264 }
265
266 if (fep->bufdesc_ex)
267 return (struct bufdesc *)((ex_new_bd < ex_base) ?
268 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000269 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800270 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000271}
272
Nimrod Andy61a44272014-06-12 08:16:18 +0800273static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
274 struct fec_enet_private *fep)
275{
276 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
277}
278
Fugang Duan4d494cd2014-09-13 05:00:48 +0800279static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
280 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800281{
282 int entries;
283
Fugang Duan4d494cd2014-09-13 05:00:48 +0800284 entries = ((const char *)txq->dirty_tx -
285 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800286
Fugang Duan4d494cd2014-09-13 05:00:48 +0800287 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800288}
289
Shawn Guob5680e02011-01-05 21:13:13 +0000290static void *swap_buffer(void *bufaddr, int len)
291{
292 int i;
293 unsigned int *buf = bufaddr;
294
Fabio Estevamffed61e2013-05-21 05:44:26 +0000295 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000296 *buf = cpu_to_be32(*buf);
297
298 return bufaddr;
299}
300
Lothar Waßmann1310b542014-11-07 10:02:47 +0100301static void swap_buffer2(void *dst_buf, void *src_buf, int len)
302{
303 int i;
304 unsigned int *src = src_buf;
305 unsigned int *dst = dst_buf;
306
307 for (i = 0; i < len; i += 4, src++, dst++)
308 *dst = swab32p(src);
309}
310
Russell King344756f2014-07-08 13:01:59 +0100311static void fec_dump(struct net_device *ndev)
312{
313 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800314 struct bufdesc *bdp;
315 struct fec_enet_priv_tx_q *txq;
316 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100317
318 netdev_info(ndev, "TX ring dump\n");
319 pr_info("Nr SC addr len SKB\n");
320
Fugang Duan4d494cd2014-09-13 05:00:48 +0800321 txq = fep->tx_queue[0];
322 bdp = txq->tx_bd_base;
323
Russell King344756f2014-07-08 13:01:59 +0100324 do {
325 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
326 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800327 bdp == txq->cur_tx ? 'S' : ' ',
328 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100329 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800330 txq->tx_skbuff[index]);
331 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100332 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800333 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100334}
335
Fugang Duan62a02c92014-06-18 08:33:52 +0800336static inline bool is_ipv4_pkt(struct sk_buff *skb)
337{
338 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
339}
340
Jim Baxter4c09eed2013-04-19 08:10:49 +0000341static int
342fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
343{
344 /* Only run for packets requiring a checksum. */
345 if (skb->ip_summed != CHECKSUM_PARTIAL)
346 return 0;
347
348 if (unlikely(skb_cow_head(skb, 0)))
349 return -1;
350
Fugang Duan62a02c92014-06-18 08:33:52 +0800351 if (is_ipv4_pkt(skb))
352 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000353 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
354
355 return 0;
356}
357
Nimrod Andy6e909282014-06-12 08:16:22 +0800358static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800359fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
360 struct sk_buff *skb,
361 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800362{
363 struct fec_enet_private *fep = netdev_priv(ndev);
364 const struct platform_device_id *id_entry =
365 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800366 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800367 struct bufdesc_ex *ebdp;
368 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800369 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800370 int frag, frag_len;
371 unsigned short status;
372 unsigned int estatus = 0;
373 skb_frag_t *this_frag;
374 unsigned int index;
375 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100376 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800377 int i;
378
379 for (frag = 0; frag < nr_frags; frag++) {
380 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800381 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800382 ebdp = (struct bufdesc_ex *)bdp;
383
384 status = bdp->cbd_sc;
385 status &= ~BD_ENET_TX_STATS;
386 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
387 frag_len = skb_shinfo(skb)->frags[frag].size;
388
389 /* Handle the last BD specially */
390 if (frag == nr_frags - 1) {
391 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
392 if (fep->bufdesc_ex) {
393 estatus |= BD_ENET_TX_INT;
394 if (unlikely(skb_shinfo(skb)->tx_flags &
395 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
396 estatus |= BD_ENET_TX_TS;
397 }
398 }
399
400 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800401 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
402 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800403 if (skb->ip_summed == CHECKSUM_PARTIAL)
404 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
405 ebdp->cbd_bdu = 0;
406 ebdp->cbd_esc = estatus;
407 }
408
409 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
410
Fugang Duan4d494cd2014-09-13 05:00:48 +0800411 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800412 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800413 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800414 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
415 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800416
417 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
418 swap_buffer(bufaddr, frag_len);
419 }
420
Russell Kingd6bf3142014-07-08 00:23:19 +0100421 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
422 DMA_TO_DEVICE);
423 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800424 dev_kfree_skb_any(skb);
425 if (net_ratelimit())
426 netdev_err(ndev, "Tx DMA memory map failed\n");
427 goto dma_mapping_error;
428 }
429
Russell Kingd6bf3142014-07-08 00:23:19 +0100430 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800431 bdp->cbd_datlen = frag_len;
432 bdp->cbd_sc = status;
433 }
434
Fugang Duan4d494cd2014-09-13 05:00:48 +0800435 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800436
437 return 0;
438
439dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800440 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800441 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800442 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800443 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
444 bdp->cbd_datlen, DMA_TO_DEVICE);
445 }
446 return NETDEV_TX_OK;
447}
448
Fugang Duan4d494cd2014-09-13 05:00:48 +0800449static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
450 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800451{
452 struct fec_enet_private *fep = netdev_priv(ndev);
453 const struct platform_device_id *id_entry =
454 platform_get_device_id(fep->pdev);
455 int nr_frags = skb_shinfo(skb)->nr_frags;
456 struct bufdesc *bdp, *last_bdp;
457 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100458 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800459 unsigned short status;
460 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800461 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800462 unsigned int estatus = 0;
463 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800464 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800465 int ret;
466
Fugang Duan4d494cd2014-09-13 05:00:48 +0800467 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800468 if (entries_free < MAX_SKB_FRAGS + 1) {
469 dev_kfree_skb_any(skb);
470 if (net_ratelimit())
471 netdev_err(ndev, "NOT enough BD for SG!\n");
472 return NETDEV_TX_OK;
473 }
474
Nimrod Andy6e909282014-06-12 08:16:22 +0800475 /* Protocol checksum off-load for TCP and UDP. */
476 if (fec_enet_clear_csum(skb, ndev)) {
477 dev_kfree_skb_any(skb);
478 return NETDEV_TX_OK;
479 }
480
481 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800482 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800483 status = bdp->cbd_sc;
484 status &= ~BD_ENET_TX_STATS;
485
486 /* Set buffer length and buffer pointer */
487 bufaddr = skb->data;
488 buflen = skb_headlen(skb);
489
Fugang Duan4d494cd2014-09-13 05:00:48 +0800490 queue = skb_get_queue_mapping(skb);
491 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800492 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800493 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800494 memcpy(txq->tx_bounce[index], skb->data, buflen);
495 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800496
497 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
498 swap_buffer(bufaddr, buflen);
499 }
500
Russell Kingd6bf3142014-07-08 00:23:19 +0100501 /* Push the data cache so the CPM does not get stale memory data. */
502 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
503 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800504 dev_kfree_skb_any(skb);
505 if (net_ratelimit())
506 netdev_err(ndev, "Tx DMA memory map failed\n");
507 return NETDEV_TX_OK;
508 }
509
510 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800511 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800512 if (ret)
513 return ret;
514 } else {
515 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
516 if (fep->bufdesc_ex) {
517 estatus = BD_ENET_TX_INT;
518 if (unlikely(skb_shinfo(skb)->tx_flags &
519 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
520 estatus |= BD_ENET_TX_TS;
521 }
522 }
523
524 if (fep->bufdesc_ex) {
525
526 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
527
528 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
529 fep->hwts_tx_en))
530 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
531
Nimrod Andybefe8212014-09-23 15:40:55 +0800532 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
533 estatus |= FEC_TX_BD_FTYPE(queue);
534
Nimrod Andy6e909282014-06-12 08:16:22 +0800535 if (skb->ip_summed == CHECKSUM_PARTIAL)
536 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
537
538 ebdp->cbd_bdu = 0;
539 ebdp->cbd_esc = estatus;
540 }
541
Fugang Duan4d494cd2014-09-13 05:00:48 +0800542 last_bdp = txq->cur_tx;
543 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800544 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800545 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800546
547 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100548 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800549
550 /* Send it on its way. Tell FEC it's ready, interrupt when done,
551 * it's the last BD of the frame, and to put the CRC on the end.
552 */
553 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
554 bdp->cbd_sc = status;
555
Sascha Hauer22f6b862009-04-15 01:32:18 +0000556 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800557 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800559 skb_tx_timestamp(skb);
560
Fugang Duan4d494cd2014-09-13 05:00:48 +0800561 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Frank Lide5fb0a2013-03-03 17:34:25 +0000563 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800564 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000565
Nimrod Andy6e909282014-06-12 08:16:22 +0800566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Nimrod Andy79f33912014-06-12 08:16:23 +0800569static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800570fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
571 struct net_device *ndev,
572 struct bufdesc *bdp, int index, char *data,
573 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800574{
575 struct fec_enet_private *fep = netdev_priv(ndev);
576 const struct platform_device_id *id_entry =
577 platform_get_device_id(fep->pdev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200578 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800579 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800580 unsigned short status;
581 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100582 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800583
584 status = bdp->cbd_sc;
585 status &= ~BD_ENET_TX_STATS;
586
587 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800588
Fugang Duan41ef84c2014-09-13 05:00:54 +0800589 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800590 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800591 memcpy(txq->tx_bounce[index], data, size);
592 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800593
594 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
595 swap_buffer(data, size);
596 }
597
Russell Kingd6bf3142014-07-08 00:23:19 +0100598 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
599 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800600 dev_kfree_skb_any(skb);
601 if (net_ratelimit())
602 netdev_err(ndev, "Tx DMA memory map failed\n");
603 return NETDEV_TX_BUSY;
604 }
605
Russell Kingd6bf3142014-07-08 00:23:19 +0100606 bdp->cbd_datlen = size;
607 bdp->cbd_bufaddr = addr;
608
Nimrod Andy79f33912014-06-12 08:16:23 +0800609 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800610 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
611 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800612 if (skb->ip_summed == CHECKSUM_PARTIAL)
613 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
614 ebdp->cbd_bdu = 0;
615 ebdp->cbd_esc = estatus;
616 }
617
618 /* Handle the last BD specially */
619 if (last_tcp)
620 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
621 if (is_last) {
622 status |= BD_ENET_TX_INTR;
623 if (fep->bufdesc_ex)
624 ebdp->cbd_esc |= BD_ENET_TX_INT;
625 }
626
627 bdp->cbd_sc = status;
628
629 return 0;
630}
631
632static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800633fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
634 struct sk_buff *skb, struct net_device *ndev,
635 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800636{
637 struct fec_enet_private *fep = netdev_priv(ndev);
638 const struct platform_device_id *id_entry =
639 platform_get_device_id(fep->pdev);
640 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200641 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800642 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800643 void *bufaddr;
644 unsigned long dmabuf;
645 unsigned short status;
646 unsigned int estatus = 0;
647
648 status = bdp->cbd_sc;
649 status &= ~BD_ENET_TX_STATS;
650 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
651
Fugang Duan4d494cd2014-09-13 05:00:48 +0800652 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
653 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800654 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800655 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800656 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
657 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800658
659 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
660 swap_buffer(bufaddr, hdr_len);
661
662 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
663 hdr_len, DMA_TO_DEVICE);
664 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
665 dev_kfree_skb_any(skb);
666 if (net_ratelimit())
667 netdev_err(ndev, "Tx DMA memory map failed\n");
668 return NETDEV_TX_BUSY;
669 }
670 }
671
672 bdp->cbd_bufaddr = dmabuf;
673 bdp->cbd_datlen = hdr_len;
674
675 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800676 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
677 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800678 if (skb->ip_summed == CHECKSUM_PARTIAL)
679 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
680 ebdp->cbd_bdu = 0;
681 ebdp->cbd_esc = estatus;
682 }
683
684 bdp->cbd_sc = status;
685
686 return 0;
687}
688
Fugang Duan4d494cd2014-09-13 05:00:48 +0800689static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
690 struct sk_buff *skb,
691 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800692{
693 struct fec_enet_private *fep = netdev_priv(ndev);
694 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
695 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800696 struct bufdesc *bdp = txq->cur_tx;
697 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800698 struct tso_t tso;
699 unsigned int index = 0;
700 int ret;
Fugang Duan37d60172014-09-17 05:18:54 +0800701 const struct platform_device_id *id_entry =
702 platform_get_device_id(fep->pdev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800703
Fugang Duan4d494cd2014-09-13 05:00:48 +0800704 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800705 dev_kfree_skb_any(skb);
706 if (net_ratelimit())
707 netdev_err(ndev, "NOT enough BD for TSO!\n");
708 return NETDEV_TX_OK;
709 }
710
711 /* Protocol checksum off-load for TCP and UDP. */
712 if (fec_enet_clear_csum(skb, ndev)) {
713 dev_kfree_skb_any(skb);
714 return NETDEV_TX_OK;
715 }
716
717 /* Initialize the TSO handler, and prepare the first payload */
718 tso_start(skb, &tso);
719
720 total_len = skb->len - hdr_len;
721 while (total_len > 0) {
722 char *hdr;
723
Fugang Duan4d494cd2014-09-13 05:00:48 +0800724 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800725 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
726 total_len -= data_left;
727
728 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800729 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800730 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800731 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800732 if (ret)
733 goto err_release;
734
735 while (data_left > 0) {
736 int size;
737
738 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800739 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
740 index = fec_enet_get_bd_index(txq->tx_bd_base,
741 bdp, fep);
742 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
743 bdp, index,
744 tso.data, size,
745 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800746 total_len == 0);
747 if (ret)
748 goto err_release;
749
750 data_left -= size;
751 tso_build_data(skb, &tso, size);
752 }
753
Fugang Duan4d494cd2014-09-13 05:00:48 +0800754 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800755 }
756
757 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800758 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800759
Nimrod Andy79f33912014-06-12 08:16:23 +0800760 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800761 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800762
763 /* Trigger transmission start */
Fugang Duan37d60172014-09-17 05:18:54 +0800764 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
765 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
766 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
767 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
768 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
769 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800770
771 return 0;
772
773err_release:
774 /* TODO: Release all used data descriptors for TSO */
775 return ret;
776}
777
Nimrod Andy61a44272014-06-12 08:16:18 +0800778static netdev_tx_t
779fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
780{
781 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800782 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800783 unsigned short queue;
784 struct fec_enet_priv_tx_q *txq;
785 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800786 int ret;
787
Fugang Duan4d494cd2014-09-13 05:00:48 +0800788 queue = skb_get_queue_mapping(skb);
789 txq = fep->tx_queue[queue];
790 nq = netdev_get_tx_queue(ndev, queue);
791
Nimrod Andy79f33912014-06-12 08:16:23 +0800792 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800793 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800794 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800795 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800796 if (ret)
797 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800798
Fugang Duan4d494cd2014-09-13 05:00:48 +0800799 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
800 if (entries_free <= txq->tx_stop_threshold)
801 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800802
803 return NETDEV_TX_OK;
804}
805
Frank Li14109a52013-03-26 16:12:03 +0000806/* Init RX & TX buffer descriptors
807 */
808static void fec_enet_bd_init(struct net_device *dev)
809{
810 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800811 struct fec_enet_priv_tx_q *txq;
812 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000813 struct bufdesc *bdp;
814 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800815 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000816
Frank Li59d0f7462014-09-13 05:00:50 +0800817 for (q = 0; q < fep->num_rx_queues; q++) {
818 /* Initialize the receive buffer descriptors. */
819 rxq = fep->rx_queue[q];
820 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800821
Frank Li59d0f7462014-09-13 05:00:50 +0800822 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000823
Frank Li59d0f7462014-09-13 05:00:50 +0800824 /* Initialize the BD for every fragment in the page. */
825 if (bdp->cbd_bufaddr)
826 bdp->cbd_sc = BD_ENET_RX_EMPTY;
827 else
828 bdp->cbd_sc = 0;
829 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000830 }
Frank Li59d0f7462014-09-13 05:00:50 +0800831
832 /* Set the last buffer to wrap */
833 bdp = fec_enet_get_prevdesc(bdp, fep, q);
834 bdp->cbd_sc |= BD_SC_WRAP;
835
836 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000837 }
838
Frank Li59d0f7462014-09-13 05:00:50 +0800839 for (q = 0; q < fep->num_tx_queues; q++) {
840 /* ...and the same for transmit */
841 txq = fep->tx_queue[q];
842 bdp = txq->tx_bd_base;
843 txq->cur_tx = bdp;
844
845 for (i = 0; i < txq->tx_ring_size; i++) {
846 /* Initialize the BD for every fragment in the page. */
847 bdp->cbd_sc = 0;
848 if (txq->tx_skbuff[i]) {
849 dev_kfree_skb_any(txq->tx_skbuff[i]);
850 txq->tx_skbuff[i] = NULL;
851 }
852 bdp->cbd_bufaddr = 0;
853 bdp = fec_enet_get_nextdesc(bdp, fep, q);
854 }
855
856 /* Set the last buffer to wrap */
857 bdp = fec_enet_get_prevdesc(bdp, fep, q);
858 bdp->cbd_sc |= BD_SC_WRAP;
859 txq->dirty_tx = bdp;
860 }
861}
862
Frank Lice99d0d2014-09-13 05:00:52 +0800863static void fec_enet_active_rxring(struct net_device *ndev)
864{
865 struct fec_enet_private *fep = netdev_priv(ndev);
866 int i;
867
868 for (i = 0; i < fep->num_rx_queues; i++)
869 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
870}
871
Frank Li59d0f7462014-09-13 05:00:50 +0800872static void fec_enet_enable_ring(struct net_device *ndev)
873{
874 struct fec_enet_private *fep = netdev_priv(ndev);
875 struct fec_enet_priv_tx_q *txq;
876 struct fec_enet_priv_rx_q *rxq;
877 int i;
878
879 for (i = 0; i < fep->num_rx_queues; i++) {
880 rxq = fep->rx_queue[i];
881 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
882
883 /* enable DMA1/2 */
884 if (i)
885 writel(RCMR_MATCHEN | RCMR_CMP(i),
886 fep->hwp + FEC_RCMR(i));
887 }
888
889 for (i = 0; i < fep->num_tx_queues; i++) {
890 txq = fep->tx_queue[i];
891 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
892
893 /* enable DMA1/2 */
894 if (i)
895 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
896 fep->hwp + FEC_DMA_CFG(i));
897 }
898}
899
900static void fec_enet_reset_skb(struct net_device *ndev)
901{
902 struct fec_enet_private *fep = netdev_priv(ndev);
903 struct fec_enet_priv_tx_q *txq;
904 int i, j;
905
906 for (i = 0; i < fep->num_tx_queues; i++) {
907 txq = fep->tx_queue[i];
908
909 for (j = 0; j < txq->tx_ring_size; j++) {
910 if (txq->tx_skbuff[j]) {
911 dev_kfree_skb_any(txq->tx_skbuff[j]);
912 txq->tx_skbuff[j] = NULL;
913 }
914 }
915 }
Frank Li14109a52013-03-26 16:12:03 +0000916}
917
Russell Kingdbc64a82014-07-08 12:40:12 +0100918/*
919 * This function is called to start or restart the FEC during a link
920 * change, transmit timeout, or to reconfigure the FEC. The network
921 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100922 */
923static void
Russell Kingef833372014-07-08 12:40:43 +0100924fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100925{
926 struct fec_enet_private *fep = netdev_priv(ndev);
927 const struct platform_device_id *id_entry =
928 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000929 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100930 u32 temp_mac[2];
931 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000932 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100933
Fugang Duan106c3142014-09-13 05:00:51 +0800934 /* Whack a reset. We should wait for this.
935 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
936 * instead of reset MAC itself.
937 */
938 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
939 writel(0, fep->hwp + FEC_ECNTRL);
940 } else {
941 writel(1, fep->hwp + FEC_ECNTRL);
942 udelay(10);
943 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100944
945 /*
946 * enet-mac reset will reset mac address registers too,
947 * so need to reconfigure it.
948 */
949 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
950 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
951 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
952 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
953 }
954
955 /* Clear any outstanding interrupt. */
956 writel(0xffc00000, fep->hwp + FEC_IEVENT);
957
Uwe Kleine-König45993652011-01-19 20:47:04 +0100958 /* Set maximum receive buffer size. */
959 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
960
Frank Li14109a52013-03-26 16:12:03 +0000961 fec_enet_bd_init(ndev);
962
Frank Li59d0f7462014-09-13 05:00:50 +0800963 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100964
Frank Li59d0f7462014-09-13 05:00:50 +0800965 /* Reset tx SKB buffers. */
966 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100967
968 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100969 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100970 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100971 writel(0x04, fep->hwp + FEC_X_CNTRL);
972 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100973 /* No Rcv on Xmit */
974 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975 writel(0x0, fep->hwp + FEC_X_CNTRL);
976 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100977
Uwe Kleine-König45993652011-01-19 20:47:04 +0100978 /* Set MII speed */
979 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
980
Guenter Roeckd1391932013-06-18 10:04:59 -0700981#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000982 /* set RX checksum */
983 val = readl(fep->hwp + FEC_RACC);
984 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
985 val |= FEC_RACC_OPTIONS;
986 else
987 val &= ~FEC_RACC_OPTIONS;
988 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700989#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000990
Uwe Kleine-König45993652011-01-19 20:47:04 +0100991 /*
992 * The phy interface and speed need to get configured
993 * differently on enet-mac.
994 */
995 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100996 /* Enable flow control and length check */
997 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100998
Shawn Guo230dec62011-09-23 02:12:48 +0000999 /* RGMII, RMII or MII */
1000 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1001 rcntl |= (1 << 6);
1002 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001003 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001004 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001005 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001006
Shawn Guo230dec62011-09-23 02:12:48 +00001007 /* 1G, 100M or 10M */
1008 if (fep->phy_dev) {
1009 if (fep->phy_dev->speed == SPEED_1000)
1010 ecntl |= (1 << 5);
1011 else if (fep->phy_dev->speed == SPEED_100)
1012 rcntl &= ~(1 << 9);
1013 else
1014 rcntl |= (1 << 9);
1015 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001016 } else {
1017#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001018 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001019 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020 /* disable the gasket and wait */
1021 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1022 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1023 udelay(1);
1024
1025 /*
1026 * configure the gasket:
1027 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001028 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001029 */
Eric Benard8d82f212012-01-12 06:10:28 +00001030 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1031 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1032 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1033 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1034 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001035
1036 /* re-enable the gasket */
1037 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1038 }
1039#endif
1040 }
Frank Libaa70a52013-01-16 16:55:58 +00001041
Guenter Roeckd1391932013-06-18 10:04:59 -07001042#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001043 /* enable pause frame*/
1044 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1045 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1046 fep->phy_dev && fep->phy_dev->pause)) {
1047 rcntl |= FEC_ENET_FCE;
1048
Jim Baxter4c09eed2013-04-19 08:10:49 +00001049 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001050 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1051 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1052 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1053 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1054
1055 /* OPD */
1056 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1057 } else {
1058 rcntl &= ~FEC_ENET_FCE;
1059 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001060#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001061
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001062 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001063
Stefan Wahren84fe6182014-03-12 11:28:19 +01001064 /* Setup multicast filter. */
1065 set_multicast_list(ndev);
1066#ifndef CONFIG_M5272
1067 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1068 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1069#endif
1070
Shawn Guo230dec62011-09-23 02:12:48 +00001071 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1072 /* enable ENET endian swap */
1073 ecntl |= (1 << 8);
1074 /* enable ENET store and forward mode */
1075 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1076 }
1077
Frank Liff43da82013-01-03 16:04:23 +00001078 if (fep->bufdesc_ex)
1079 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001080
Chris Healy38ae92d2013-06-25 23:18:52 -07001081#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001082 /* Enable the MIB statistic event counters */
1083 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001084#endif
1085
Uwe Kleine-König45993652011-01-19 20:47:04 +01001086 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001087 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001088 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001089
Frank Liff43da82013-01-03 16:04:23 +00001090 if (fep->bufdesc_ex)
1091 fec_ptp_start_cyclecounter(ndev);
1092
Uwe Kleine-König45993652011-01-19 20:47:04 +01001093 /* Enable interrupts we wish to service */
1094 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001095
1096 /* Init the interrupt coalescing */
1097 fec_enet_itr_coal_init(ndev);
1098
Uwe Kleine-König45993652011-01-19 20:47:04 +01001099}
1100
1101static void
1102fec_stop(struct net_device *ndev)
1103{
1104 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001105 const struct platform_device_id *id_entry =
1106 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001107 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001108
1109 /* We cannot expect a graceful transmit stop without link !!! */
1110 if (fep->link) {
1111 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1112 udelay(10);
1113 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001114 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001115 }
1116
Fugang Duan106c3142014-09-13 05:00:51 +08001117 /* Whack a reset. We should wait for this.
1118 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1119 * instead of reset MAC itself.
1120 */
1121 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1122 writel(0, fep->hwp + FEC_ECNTRL);
1123 } else {
1124 writel(1, fep->hwp + FEC_ECNTRL);
1125 udelay(10);
1126 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001127 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1128 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001129
1130 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001131 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001132 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001133 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1134 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001135}
1136
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001139fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001141 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Russell King344756f2014-07-08 13:01:59 +01001143 fec_dump(ndev);
1144
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001145 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Russell King36cdc742014-07-08 13:01:44 +01001147 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001148}
1149
Russell King36cdc742014-07-08 13:01:44 +01001150static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001151{
1152 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001153 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001154 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001155
Russell King36cdc742014-07-08 13:01:44 +01001156 rtnl_lock();
1157 if (netif_device_present(ndev) || netif_running(ndev)) {
1158 napi_disable(&fep->napi);
1159 netif_tx_lock_bh(ndev);
1160 fec_restart(ndev);
1161 netif_wake_queue(ndev);
1162 netif_tx_unlock_bh(ndev);
1163 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001164 }
Russell King36cdc742014-07-08 13:01:44 +01001165 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001169fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1170 struct skb_shared_hwtstamps *hwtstamps)
1171{
1172 unsigned long flags;
1173 u64 ns;
1174
1175 spin_lock_irqsave(&fep->tmreg_lock, flags);
1176 ns = timecounter_cyc2time(&fep->tc, ts);
1177 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1178
1179 memset(hwtstamps, 0, sizeof(*hwtstamps));
1180 hwtstamps->hwtstamp = ns_to_ktime(ns);
1181}
1182
1183static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001184fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001187 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001188 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001190 struct fec_enet_priv_tx_q *txq;
1191 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001192 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001193 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001195 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001196
1197 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1198
1199 txq = fep->tx_queue[queue_id];
1200 /* get next bdp of dirty_tx */
1201 nq = netdev_get_tx_queue(ndev, queue_id);
1202 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Frank Lide5fb0a2013-03-03 17:34:25 +00001204 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001205 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001206
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001207 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001208
1209 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001210 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001211 break;
1212
Fugang Duan4d494cd2014-09-13 05:00:48 +08001213 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001214
Fugang Duan4d494cd2014-09-13 05:00:48 +08001215 skb = txq->tx_skbuff[index];
1216 txq->tx_skbuff[index] = NULL;
1217 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001218 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1219 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001220 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001221 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001222 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001223 continue;
1224 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001227 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 BD_ENET_TX_RL | BD_ENET_TX_UN |
1229 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001230 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001231 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001232 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001233 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001234 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001235 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001236 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001237 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001238 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001239 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001240 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001242 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001243 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
Frank Liff43da82013-01-03 16:04:23 +00001246 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1247 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001248 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001249 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001250
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001251 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001252 skb_tstamp_tx(skb, &shhwtstamps);
1253 }
Frank Liff43da82013-01-03 16:04:23 +00001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* Deferred means some collisions occurred during transmit,
1256 * but we eventually sent the packet OK.
1257 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001258 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001259 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001260
Sascha Hauer22f6b862009-04-15 01:32:18 +00001261 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001263
Fugang Duan4d494cd2014-09-13 05:00:48 +08001264 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001265
Sascha Hauer22f6b862009-04-15 01:32:18 +00001266 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001267 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001268
Sascha Hauer22f6b862009-04-15 01:32:18 +00001269 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001271 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001272 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1273 if (entries_free >= txq->tx_wake_threshold)
1274 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Russell Kingccea2962014-07-08 13:01:38 +01001277
1278 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001279 if (bdp != txq->cur_tx &&
1280 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1281 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1282}
1283
1284static void
1285fec_enet_tx(struct net_device *ndev)
1286{
1287 struct fec_enet_private *fep = netdev_priv(ndev);
1288 u16 queue_id;
1289 /* First process class A queue, then Class B and Best Effort queue */
1290 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1291 clear_bit(queue_id, &fep->work_tx);
1292 fec_enet_tx_queue(ndev, queue_id);
1293 }
1294 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001297static int
1298fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1299{
1300 struct fec_enet_private *fep = netdev_priv(ndev);
1301 int off;
1302
1303 off = ((unsigned long)skb->data) & fep->rx_align;
1304 if (off)
1305 skb_reserve(skb, fep->rx_align + 1 - off);
1306
1307 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1308 FEC_ENET_RX_FRSIZE - fep->rx_align,
1309 DMA_FROM_DEVICE);
1310 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1311 if (net_ratelimit())
1312 netdev_err(ndev, "Rx DMA memory map failed\n");
1313 return -ENOMEM;
1314 }
1315
1316 return 0;
1317}
1318
1319static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
Lothar Waßmann1310b542014-11-07 10:02:47 +01001320 struct bufdesc *bdp, u32 length, bool swap)
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001321{
1322 struct fec_enet_private *fep = netdev_priv(ndev);
1323 struct sk_buff *new_skb;
1324
1325 if (length > fep->rx_copybreak)
1326 return false;
1327
1328 new_skb = netdev_alloc_skb(ndev, length);
1329 if (!new_skb)
1330 return false;
1331
1332 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1333 FEC_ENET_RX_FRSIZE - fep->rx_align,
1334 DMA_FROM_DEVICE);
Lothar Waßmann1310b542014-11-07 10:02:47 +01001335 if (!swap)
1336 memcpy(new_skb->data, (*skb)->data, length);
1337 else
1338 swap_buffer2(new_skb->data, (*skb)->data, length);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001339 *skb = new_skb;
1340
1341 return true;
1342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/* During a receive, the cur_rx points to the current incoming buffer.
1345 * When we update through the ring, if the next incoming buffer has
1346 * not been given to the system, we just set the empty indicator,
1347 * effectively tossing the packet.
1348 */
Frank Lidc975382013-01-28 18:31:42 +00001349static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001350fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001352 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001353 const struct platform_device_id *id_entry =
1354 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001355 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001356 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001357 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001358 struct sk_buff *skb_new = NULL;
1359 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 ushort pkt_len;
1361 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001362 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001363 struct bufdesc_ex *ebdp = NULL;
1364 bool vlan_packet_rcvd = false;
1365 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001366 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001367 bool is_copybreak;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001368 bool need_swap = id_entry->driver_data & FEC_QUIRK_SWAP_FRAME;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001369
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001370#ifdef CONFIG_M532x
1371 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001372#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001373 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1374 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* First, grab all of the stats for the incoming packet.
1377 * These get messed up if we get called due to a busy condition.
1378 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001379 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Sascha Hauer22f6b862009-04-15 01:32:18 +00001381 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Frank Lidc975382013-01-28 18:31:42 +00001383 if (pkt_received >= budget)
1384 break;
1385 pkt_received++;
1386
Sascha Hauer22f6b862009-04-15 01:32:18 +00001387 /* Since we have allocated space to hold a complete frame,
1388 * the last indicator should be set.
1389 */
1390 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001391 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Russell Kingdb3421c2014-07-08 13:01:49 +01001393
Sascha Hauer22f6b862009-04-15 01:32:18 +00001394 /* Check for errors. */
1395 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001397 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001398 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1399 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001400 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001401 }
1402 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001403 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001404 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001405 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001406 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001407 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001409
1410 /* Report late collisions as a frame error.
1411 * On this error, the BD is closed, but we don't know what we
1412 * have in the buffer. So, just drop this frame on the floor.
1413 */
1414 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001415 ndev->stats.rx_errors++;
1416 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001417 goto rx_processing_done;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Sascha Hauer22f6b862009-04-15 01:32:18 +00001420 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001421 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001422 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001423 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Fugang Duan4d494cd2014-09-13 05:00:48 +08001425 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001426 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001427
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001428 /* The packet length includes FCS, but we don't want to
1429 * include that when passing upstream as it messes up
1430 * bridging applications.
1431 */
Lothar Waßmann1310b542014-11-07 10:02:47 +01001432 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1433 need_swap);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001434 if (!is_copybreak) {
1435 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1436 if (unlikely(!skb_new)) {
1437 ndev->stats.rx_dropped++;
1438 goto rx_processing_done;
1439 }
1440 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1441 FEC_ENET_RX_FRSIZE - fep->rx_align,
1442 DMA_FROM_DEVICE);
1443 }
1444
1445 prefetch(skb->data - NET_IP_ALIGN);
1446 skb_put(skb, pkt_len - 4);
1447 data = skb->data;
Lothar Waßmann1310b542014-11-07 10:02:47 +01001448 if (!is_copybreak && need_swap)
Shawn Guob5680e02011-01-05 21:13:13 +00001449 swap_buffer(data, pkt_len);
1450
Jim Baxtercdffcf12013-07-02 22:52:56 +01001451 /* Extract the enhanced buffer descriptor */
1452 ebdp = NULL;
1453 if (fep->bufdesc_ex)
1454 ebdp = (struct bufdesc_ex *)bdp;
1455
1456 /* If this is a VLAN packet remove the VLAN Tag */
1457 vlan_packet_rcvd = false;
1458 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001459 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001460 /* Push and remove the vlan tag */
1461 struct vlan_hdr *vlan_header =
1462 (struct vlan_hdr *) (data + ETH_HLEN);
1463 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001464
1465 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001466
1467 skb_copy_to_linear_data_offset(skb, VLAN_HLEN,
1468 data, (2 * ETH_ALEN));
1469 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001470 }
1471
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001472 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001474 /* Get receive timestamp from the skb */
1475 if (fep->hwts_rx_en && fep->bufdesc_ex)
1476 fec_enet_hwtstamp(fep, ebdp->ts,
1477 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001478
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001479 if (fep->bufdesc_ex &&
1480 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1481 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1482 /* don't check it */
1483 skb->ip_summed = CHECKSUM_UNNECESSARY;
1484 } else {
1485 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001486 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001487 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001488
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001489 /* Handle received VLAN packets */
1490 if (vlan_packet_rcvd)
1491 __vlan_hwaccel_put_tag(skb,
1492 htons(ETH_P_8021Q),
1493 vlan_tag);
1494
1495 napi_gro_receive(&fep->napi, skb);
1496
1497 if (is_copybreak) {
1498 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1499 FEC_ENET_RX_FRSIZE - fep->rx_align,
1500 DMA_FROM_DEVICE);
1501 } else {
1502 rxq->rx_skbuff[index] = skb_new;
1503 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1504 }
1505
Sascha Hauer22f6b862009-04-15 01:32:18 +00001506rx_processing_done:
1507 /* Clear the status flags for this buffer */
1508 status &= ~BD_ENET_RX_STATS;
1509
1510 /* Mark the buffer empty */
1511 status |= BD_ENET_RX_EMPTY;
1512 bdp->cbd_sc = status;
1513
Frank Liff43da82013-01-03 16:04:23 +00001514 if (fep->bufdesc_ex) {
1515 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1516
1517 ebdp->cbd_esc = BD_ENET_RX_INT;
1518 ebdp->cbd_prot = 0;
1519 ebdp->cbd_bdu = 0;
1520 }
Frank Li6605b732012-10-30 18:25:31 +00001521
Sascha Hauer22f6b862009-04-15 01:32:18 +00001522 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001523 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001524
Sascha Hauer22f6b862009-04-15 01:32:18 +00001525 /* Doing this here will keep the FEC running while we process
1526 * incoming frames. On a heavily loaded network, we should be
1527 * able to keep up at the expense of system resources.
1528 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001529 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001531 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001532 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Fugang Duan4d494cd2014-09-13 05:00:48 +08001535static int
1536fec_enet_rx(struct net_device *ndev, int budget)
1537{
1538 int pkt_received = 0;
1539 u16 queue_id;
1540 struct fec_enet_private *fep = netdev_priv(ndev);
1541
1542 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1543 clear_bit(queue_id, &fep->work_rx);
1544 pkt_received += fec_enet_rx_queue(ndev,
1545 budget - pkt_received, queue_id);
1546 }
1547 return pkt_received;
1548}
1549
1550static bool
1551fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1552{
1553 if (int_events == 0)
1554 return false;
1555
1556 if (int_events & FEC_ENET_RXF)
1557 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001558 if (int_events & FEC_ENET_RXF_1)
1559 fep->work_rx |= (1 << 0);
1560 if (int_events & FEC_ENET_RXF_2)
1561 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001562
1563 if (int_events & FEC_ENET_TXF)
1564 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001565 if (int_events & FEC_ENET_TXF_1)
1566 fep->work_tx |= (1 << 0);
1567 if (int_events & FEC_ENET_TXF_2)
1568 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001569
1570 return true;
1571}
1572
Uwe Kleine-König45993652011-01-19 20:47:04 +01001573static irqreturn_t
1574fec_enet_interrupt(int irq, void *dev_id)
1575{
1576 struct net_device *ndev = dev_id;
1577 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001578 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001579 uint int_events;
1580 irqreturn_t ret = IRQ_NONE;
1581
Russell King7a168072014-07-08 00:22:44 +01001582 int_events = readl(fep->hwp + FEC_IEVENT);
1583 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001584 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001585
Russell King7a168072014-07-08 00:22:44 +01001586 if (int_events & napi_mask) {
1587 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001588
Russell King7a168072014-07-08 00:22:44 +01001589 /* Disable the NAPI interrupts */
1590 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1591 napi_schedule(&fep->napi);
1592 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001593
Russell King7a168072014-07-08 00:22:44 +01001594 if (int_events & FEC_ENET_MII) {
1595 ret = IRQ_HANDLED;
1596 complete(&fep->mdio_done);
1597 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001598
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001599 if (fep->ptp_clock)
1600 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001601
Uwe Kleine-König45993652011-01-19 20:47:04 +01001602 return ret;
1603}
1604
Frank Lidc975382013-01-28 18:31:42 +00001605static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1606{
1607 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001608 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001609 int pkts;
1610
1611 /*
1612 * Clear any pending transmit or receive interrupts before
1613 * processing the rings to avoid racing with the hardware.
1614 */
1615 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1616
1617 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001618
Frank Lide5fb0a2013-03-03 17:34:25 +00001619 fec_enet_tx(ndev);
1620
Frank Lidc975382013-01-28 18:31:42 +00001621 if (pkts < budget) {
1622 napi_complete(napi);
1623 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1624 }
1625 return pkts;
1626}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001629static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001631 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001632 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001633 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Shawn Guo49da97d2011-01-05 21:13:11 +00001635 /*
1636 * try to get mac address in following order:
1637 *
1638 * 1) module parameter via kernel command line in form
1639 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1640 */
1641 iap = macaddr;
1642
1643 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001644 * 2) from device tree data
1645 */
1646 if (!is_valid_ether_addr(iap)) {
1647 struct device_node *np = fep->pdev->dev.of_node;
1648 if (np) {
1649 const char *mac = of_get_mac_address(np);
1650 if (mac)
1651 iap = (unsigned char *) mac;
1652 }
1653 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001654
1655 /*
1656 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001657 */
1658 if (!is_valid_ether_addr(iap)) {
1659#ifdef CONFIG_M5272
1660 if (FEC_FLASHMAC)
1661 iap = (unsigned char *)FEC_FLASHMAC;
1662#else
1663 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001664 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001665#endif
1666 }
1667
1668 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001669 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001670 */
1671 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001672 *((__be32 *) &tmpaddr[0]) =
1673 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1674 *((__be16 *) &tmpaddr[4]) =
1675 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 iap = &tmpaddr[0];
1677 }
1678
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001679 /*
1680 * 5) random mac address
1681 */
1682 if (!is_valid_ether_addr(iap)) {
1683 /* Report it and use a random ethernet address instead */
1684 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1685 eth_hw_addr_random(ndev);
1686 netdev_info(ndev, "Using random MAC address: %pM\n",
1687 ndev->dev_addr);
1688 return;
1689 }
1690
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001691 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Shawn Guo49da97d2011-01-05 21:13:11 +00001693 /* Adjust MAC if using macaddr */
1694 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001695 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698/* ------------------------------------------------------------------------- */
1699
Bryan Wue6b043d2010-03-31 02:10:44 +00001700/*
1701 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001703static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001705 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001706 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001707 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Bryan Wue6b043d2010-03-31 02:10:44 +00001709 /* Prevent a state halted on mii error */
1710 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1711 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001714
Russell King8ce56242014-07-08 12:40:07 +01001715 /*
1716 * If the netdev is down, or is going down, we're not interested
1717 * in link state events, so just mark our idea of the link as down
1718 * and ignore the event.
1719 */
1720 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1721 fep->link = 0;
1722 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001723 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001724 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001725 status_change = 1;
1726 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001727
Russell Kingef833372014-07-08 12:40:43 +01001728 if (fep->full_duplex != phy_dev->duplex) {
1729 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001730 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001731 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001732
1733 if (phy_dev->speed != fep->speed) {
1734 fep->speed = phy_dev->speed;
1735 status_change = 1;
1736 }
1737
1738 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001739 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001740 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001741 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001742 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001743 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001744 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001745 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001746 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001747 } else {
1748 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001749 napi_disable(&fep->napi);
1750 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001751 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001752 netif_tx_unlock_bh(ndev);
1753 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001754 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001755 status_change = 1;
1756 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001757 }
1758
Bryan Wue6b043d2010-03-31 02:10:44 +00001759 if (status_change)
1760 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Bryan Wue6b043d2010-03-31 02:10:44 +00001763static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Bryan Wue6b043d2010-03-31 02:10:44 +00001765 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001766 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001767
1768 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001769 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001770
1771 /* start a read op */
1772 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1773 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1774 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1775
1776 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001777 time_left = wait_for_completion_timeout(&fep->mdio_done,
1778 usecs_to_jiffies(FEC_MII_TIMEOUT));
1779 if (time_left == 0) {
1780 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001781 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001782 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001783 }
1784
1785 /* return value */
1786 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1787}
1788
1789static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1790 u16 value)
1791{
1792 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001793 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001794
1795 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001796 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001797
Shawn Guo862f0982011-01-05 21:13:09 +00001798 /* start a write op */
1799 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001800 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1801 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1802 fep->hwp + FEC_MII_DATA);
1803
1804 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001805 time_left = wait_for_completion_timeout(&fep->mdio_done,
1806 usecs_to_jiffies(FEC_MII_TIMEOUT));
1807 if (time_left == 0) {
1808 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001809 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001810 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001811 }
1812
1813 return 0;
1814}
1815
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001816static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1817{
1818 struct fec_enet_private *fep = netdev_priv(ndev);
1819 int ret;
1820
1821 if (enable) {
1822 ret = clk_prepare_enable(fep->clk_ahb);
1823 if (ret)
1824 return ret;
1825 ret = clk_prepare_enable(fep->clk_ipg);
1826 if (ret)
1827 goto failed_clk_ipg;
1828 if (fep->clk_enet_out) {
1829 ret = clk_prepare_enable(fep->clk_enet_out);
1830 if (ret)
1831 goto failed_clk_enet_out;
1832 }
1833 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001834 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001835 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001836 if (ret) {
1837 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001838 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001839 } else {
1840 fep->ptp_clk_on = true;
1841 }
1842 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001843 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001844 if (fep->clk_ref) {
1845 ret = clk_prepare_enable(fep->clk_ref);
1846 if (ret)
1847 goto failed_clk_ref;
1848 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001849 } else {
1850 clk_disable_unprepare(fep->clk_ahb);
1851 clk_disable_unprepare(fep->clk_ipg);
1852 if (fep->clk_enet_out)
1853 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001854 if (fep->clk_ptp) {
1855 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001856 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001857 fep->ptp_clk_on = false;
1858 mutex_unlock(&fep->ptp_clk_mutex);
1859 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001860 if (fep->clk_ref)
1861 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001862 }
1863
1864 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001865
1866failed_clk_ref:
1867 if (fep->clk_ref)
1868 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001869failed_clk_ptp:
1870 if (fep->clk_enet_out)
1871 clk_disable_unprepare(fep->clk_enet_out);
1872failed_clk_enet_out:
1873 clk_disable_unprepare(fep->clk_ipg);
1874failed_clk_ipg:
1875 clk_disable_unprepare(fep->clk_ahb);
1876
1877 return ret;
1878}
1879
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001880static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001881{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001882 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001883 const struct platform_device_id *id_entry =
1884 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001885 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001886 char mdio_bus_id[MII_BUS_ID_SIZE];
1887 char phy_name[MII_BUS_ID_SIZE + 3];
1888 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001889 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001890
Bryan Wu418bd0d2010-05-28 03:40:39 -07001891 fep->phy_dev = NULL;
1892
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001893 if (fep->phy_node) {
1894 phy_dev = of_phy_connect(ndev, fep->phy_node,
1895 &fec_enet_adjust_link, 0,
1896 fep->phy_interface);
1897 } else {
1898 /* check for attached phy */
1899 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1900 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1901 continue;
1902 if (fep->mii_bus->phy_map[phy_id] == NULL)
1903 continue;
1904 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1905 continue;
1906 if (dev_id--)
1907 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001908 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001909 break;
1910 }
1911
1912 if (phy_id >= PHY_MAX_ADDR) {
1913 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001914 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001915 phy_id = 0;
1916 }
1917
1918 snprintf(phy_name, sizeof(phy_name),
1919 PHY_ID_FMT, mdio_bus_id, phy_id);
1920 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1921 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001922 }
1923
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001924 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001925 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001926 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001927 }
1928
1929 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001930 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001931 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001932 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001933#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001934 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001935#endif
Frank Libaa70a52013-01-16 16:55:58 +00001936 }
Shawn Guo230dec62011-09-23 02:12:48 +00001937 else
1938 phy_dev->supported &= PHY_BASIC_FEATURES;
1939
Bryan Wue6b043d2010-03-31 02:10:44 +00001940 phy_dev->advertising = phy_dev->supported;
1941
1942 fep->phy_dev = phy_dev;
1943 fep->link = 0;
1944 fep->full_duplex = 0;
1945
Joe Perches31b77202013-04-13 19:03:17 +00001946 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1947 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1948 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001949
Bryan Wue6b043d2010-03-31 02:10:44 +00001950 return 0;
1951}
1952
1953static int fec_enet_mii_init(struct platform_device *pdev)
1954{
Shawn Guob5680e02011-01-05 21:13:13 +00001955 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001956 struct net_device *ndev = platform_get_drvdata(pdev);
1957 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001958 const struct platform_device_id *id_entry =
1959 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001960 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001961 int err = -ENXIO, i;
1962
Shawn Guob5680e02011-01-05 21:13:13 +00001963 /*
1964 * The dual fec interfaces are not equivalent with enet-mac.
1965 * Here are the differences:
1966 *
1967 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1968 * - fec0 acts as the 1588 time master while fec1 is slave
1969 * - external phys can only be configured by fec0
1970 *
1971 * That is to say fec1 can not work independently. It only works
1972 * when fec0 is working. The reason behind this design is that the
1973 * second interface is added primarily for Switch mode.
1974 *
1975 * Because of the last point above, both phys are attached on fec0
1976 * mdio interface in board design, and need to be configured by
1977 * fec0 mii_bus.
1978 */
Shawn Guo43af9402011-12-05 05:01:15 +00001979 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001980 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001981 if (mii_cnt && fec0_mii_bus) {
1982 fep->mii_bus = fec0_mii_bus;
1983 mii_cnt++;
1984 return 0;
1985 }
1986 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001987 }
1988
Bryan Wue6b043d2010-03-31 02:10:44 +00001989 fep->mii_timeout = 0;
1990
1991 /*
1992 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001993 *
1994 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1995 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1996 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1997 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001998 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001999 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00002000 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2001 fep->phy_speed--;
2002 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00002003 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2004
2005 fep->mii_bus = mdiobus_alloc();
2006 if (fep->mii_bus == NULL) {
2007 err = -ENOMEM;
2008 goto err_out;
2009 }
2010
2011 fep->mii_bus->name = "fec_enet_mii_bus";
2012 fep->mii_bus->read = fec_enet_mdio_read;
2013 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002014 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2015 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002016 fep->mii_bus->priv = fep;
2017 fep->mii_bus->parent = &pdev->dev;
2018
2019 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2020 if (!fep->mii_bus->irq) {
2021 err = -ENOMEM;
2022 goto err_out_free_mdiobus;
2023 }
2024
2025 for (i = 0; i < PHY_MAX_ADDR; i++)
2026 fep->mii_bus->irq[i] = PHY_POLL;
2027
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002028 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2029 if (node) {
2030 err = of_mdiobus_register(fep->mii_bus, node);
2031 of_node_put(node);
2032 } else {
2033 err = mdiobus_register(fep->mii_bus);
2034 }
2035
2036 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002037 goto err_out_free_mdio_irq;
2038
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002039 mii_cnt++;
2040
Shawn Guob5680e02011-01-05 21:13:13 +00002041 /* save fec0 mii_bus */
2042 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2043 fec0_mii_bus = fep->mii_bus;
2044
Bryan Wue6b043d2010-03-31 02:10:44 +00002045 return 0;
2046
Bryan Wue6b043d2010-03-31 02:10:44 +00002047err_out_free_mdio_irq:
2048 kfree(fep->mii_bus->irq);
2049err_out_free_mdiobus:
2050 mdiobus_free(fep->mii_bus);
2051err_out:
2052 return err;
2053}
2054
2055static void fec_enet_mii_remove(struct fec_enet_private *fep)
2056{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002057 if (--mii_cnt == 0) {
2058 mdiobus_unregister(fep->mii_bus);
2059 kfree(fep->mii_bus->irq);
2060 mdiobus_free(fep->mii_bus);
2061 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002062}
2063
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002064static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002065 struct ethtool_cmd *cmd)
2066{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002067 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002068 struct phy_device *phydev = fep->phy_dev;
2069
2070 if (!phydev)
2071 return -ENODEV;
2072
2073 return phy_ethtool_gset(phydev, cmd);
2074}
2075
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002076static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002077 struct ethtool_cmd *cmd)
2078{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002079 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002080 struct phy_device *phydev = fep->phy_dev;
2081
2082 if (!phydev)
2083 return -ENODEV;
2084
2085 return phy_ethtool_sset(phydev, cmd);
2086}
2087
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002088static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002089 struct ethtool_drvinfo *info)
2090{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002091 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Jiri Pirko7826d432013-01-06 00:44:26 +00002093 strlcpy(info->driver, fep->pdev->dev.driver->name,
2094 sizeof(info->driver));
2095 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2096 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
Bryan Wue6b043d2010-03-31 02:10:44 +00002098
Frank Li5ebae4892013-01-06 16:25:07 +00002099static int fec_enet_get_ts_info(struct net_device *ndev,
2100 struct ethtool_ts_info *info)
2101{
2102 struct fec_enet_private *fep = netdev_priv(ndev);
2103
2104 if (fep->bufdesc_ex) {
2105
2106 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2107 SOF_TIMESTAMPING_RX_SOFTWARE |
2108 SOF_TIMESTAMPING_SOFTWARE |
2109 SOF_TIMESTAMPING_TX_HARDWARE |
2110 SOF_TIMESTAMPING_RX_HARDWARE |
2111 SOF_TIMESTAMPING_RAW_HARDWARE;
2112 if (fep->ptp_clock)
2113 info->phc_index = ptp_clock_index(fep->ptp_clock);
2114 else
2115 info->phc_index = -1;
2116
2117 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2118 (1 << HWTSTAMP_TX_ON);
2119
2120 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2121 (1 << HWTSTAMP_FILTER_ALL);
2122 return 0;
2123 } else {
2124 return ethtool_op_get_ts_info(ndev, info);
2125 }
2126}
2127
Guenter Roeckd1391932013-06-18 10:04:59 -07002128#if !defined(CONFIG_M5272)
2129
Frank Libaa70a52013-01-16 16:55:58 +00002130static void fec_enet_get_pauseparam(struct net_device *ndev,
2131 struct ethtool_pauseparam *pause)
2132{
2133 struct fec_enet_private *fep = netdev_priv(ndev);
2134
2135 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2136 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2137 pause->rx_pause = pause->tx_pause;
2138}
2139
2140static int fec_enet_set_pauseparam(struct net_device *ndev,
2141 struct ethtool_pauseparam *pause)
2142{
2143 struct fec_enet_private *fep = netdev_priv(ndev);
2144
Russell King0b146ca2014-07-08 00:22:59 +01002145 if (!fep->phy_dev)
2146 return -ENODEV;
2147
Frank Libaa70a52013-01-16 16:55:58 +00002148 if (pause->tx_pause != pause->rx_pause) {
2149 netdev_info(ndev,
2150 "hardware only support enable/disable both tx and rx");
2151 return -EINVAL;
2152 }
2153
2154 fep->pause_flag = 0;
2155
2156 /* tx pause must be same as rx pause */
2157 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2158 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2159
2160 if (pause->rx_pause || pause->autoneg) {
2161 fep->phy_dev->supported |= ADVERTISED_Pause;
2162 fep->phy_dev->advertising |= ADVERTISED_Pause;
2163 } else {
2164 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2165 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2166 }
2167
2168 if (pause->autoneg) {
2169 if (netif_running(ndev))
2170 fec_stop(ndev);
2171 phy_start_aneg(fep->phy_dev);
2172 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002173 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002174 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002175 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002176 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002177 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002178 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002179 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002180 }
Frank Libaa70a52013-01-16 16:55:58 +00002181
2182 return 0;
2183}
2184
Chris Healy38ae92d2013-06-25 23:18:52 -07002185static const struct fec_stat {
2186 char name[ETH_GSTRING_LEN];
2187 u16 offset;
2188} fec_stats[] = {
2189 /* RMON TX */
2190 { "tx_dropped", RMON_T_DROP },
2191 { "tx_packets", RMON_T_PACKETS },
2192 { "tx_broadcast", RMON_T_BC_PKT },
2193 { "tx_multicast", RMON_T_MC_PKT },
2194 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2195 { "tx_undersize", RMON_T_UNDERSIZE },
2196 { "tx_oversize", RMON_T_OVERSIZE },
2197 { "tx_fragment", RMON_T_FRAG },
2198 { "tx_jabber", RMON_T_JAB },
2199 { "tx_collision", RMON_T_COL },
2200 { "tx_64byte", RMON_T_P64 },
2201 { "tx_65to127byte", RMON_T_P65TO127 },
2202 { "tx_128to255byte", RMON_T_P128TO255 },
2203 { "tx_256to511byte", RMON_T_P256TO511 },
2204 { "tx_512to1023byte", RMON_T_P512TO1023 },
2205 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2206 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2207 { "tx_octets", RMON_T_OCTETS },
2208
2209 /* IEEE TX */
2210 { "IEEE_tx_drop", IEEE_T_DROP },
2211 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2212 { "IEEE_tx_1col", IEEE_T_1COL },
2213 { "IEEE_tx_mcol", IEEE_T_MCOL },
2214 { "IEEE_tx_def", IEEE_T_DEF },
2215 { "IEEE_tx_lcol", IEEE_T_LCOL },
2216 { "IEEE_tx_excol", IEEE_T_EXCOL },
2217 { "IEEE_tx_macerr", IEEE_T_MACERR },
2218 { "IEEE_tx_cserr", IEEE_T_CSERR },
2219 { "IEEE_tx_sqe", IEEE_T_SQE },
2220 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2221 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2222
2223 /* RMON RX */
2224 { "rx_packets", RMON_R_PACKETS },
2225 { "rx_broadcast", RMON_R_BC_PKT },
2226 { "rx_multicast", RMON_R_MC_PKT },
2227 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2228 { "rx_undersize", RMON_R_UNDERSIZE },
2229 { "rx_oversize", RMON_R_OVERSIZE },
2230 { "rx_fragment", RMON_R_FRAG },
2231 { "rx_jabber", RMON_R_JAB },
2232 { "rx_64byte", RMON_R_P64 },
2233 { "rx_65to127byte", RMON_R_P65TO127 },
2234 { "rx_128to255byte", RMON_R_P128TO255 },
2235 { "rx_256to511byte", RMON_R_P256TO511 },
2236 { "rx_512to1023byte", RMON_R_P512TO1023 },
2237 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2238 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2239 { "rx_octets", RMON_R_OCTETS },
2240
2241 /* IEEE RX */
2242 { "IEEE_rx_drop", IEEE_R_DROP },
2243 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2244 { "IEEE_rx_crc", IEEE_R_CRC },
2245 { "IEEE_rx_align", IEEE_R_ALIGN },
2246 { "IEEE_rx_macerr", IEEE_R_MACERR },
2247 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2248 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2249};
2250
2251static void fec_enet_get_ethtool_stats(struct net_device *dev,
2252 struct ethtool_stats *stats, u64 *data)
2253{
2254 struct fec_enet_private *fep = netdev_priv(dev);
2255 int i;
2256
2257 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2258 data[i] = readl(fep->hwp + fec_stats[i].offset);
2259}
2260
2261static void fec_enet_get_strings(struct net_device *netdev,
2262 u32 stringset, u8 *data)
2263{
2264 int i;
2265 switch (stringset) {
2266 case ETH_SS_STATS:
2267 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2268 memcpy(data + i * ETH_GSTRING_LEN,
2269 fec_stats[i].name, ETH_GSTRING_LEN);
2270 break;
2271 }
2272}
2273
2274static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2275{
2276 switch (sset) {
2277 case ETH_SS_STATS:
2278 return ARRAY_SIZE(fec_stats);
2279 default:
2280 return -EOPNOTSUPP;
2281 }
2282}
Guenter Roeckd1391932013-06-18 10:04:59 -07002283#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002284
Chris Healy32bc9b42013-06-17 07:25:06 -07002285static int fec_enet_nway_reset(struct net_device *dev)
2286{
2287 struct fec_enet_private *fep = netdev_priv(dev);
2288 struct phy_device *phydev = fep->phy_dev;
2289
2290 if (!phydev)
2291 return -ENODEV;
2292
2293 return genphy_restart_aneg(phydev);
2294}
2295
Fugang Duand851b472014-09-17 05:18:52 +08002296/* ITR clock source is enet system clock (clk_ahb).
2297 * TCTT unit is cycle_ns * 64 cycle
2298 * So, the ICTT value = X us / (cycle_ns * 64)
2299 */
2300static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2301{
2302 struct fec_enet_private *fep = netdev_priv(ndev);
2303
2304 return us * (fep->itr_clk_rate / 64000) / 1000;
2305}
2306
2307/* Set threshold for interrupt coalescing */
2308static void fec_enet_itr_coal_set(struct net_device *ndev)
2309{
2310 struct fec_enet_private *fep = netdev_priv(ndev);
2311 const struct platform_device_id *id_entry =
2312 platform_get_device_id(fep->pdev);
2313 int rx_itr, tx_itr;
2314
2315 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2316 return;
2317
2318 /* Must be greater than zero to avoid unpredictable behavior */
2319 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2320 !fep->tx_time_itr || !fep->tx_pkts_itr)
2321 return;
2322
2323 /* Select enet system clock as Interrupt Coalescing
2324 * timer Clock Source
2325 */
2326 rx_itr = FEC_ITR_CLK_SEL;
2327 tx_itr = FEC_ITR_CLK_SEL;
2328
2329 /* set ICFT and ICTT */
2330 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2331 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2332 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2333 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2334
2335 rx_itr |= FEC_ITR_EN;
2336 tx_itr |= FEC_ITR_EN;
2337
2338 writel(tx_itr, fep->hwp + FEC_TXIC0);
2339 writel(rx_itr, fep->hwp + FEC_RXIC0);
2340 writel(tx_itr, fep->hwp + FEC_TXIC1);
2341 writel(rx_itr, fep->hwp + FEC_RXIC1);
2342 writel(tx_itr, fep->hwp + FEC_TXIC2);
2343 writel(rx_itr, fep->hwp + FEC_RXIC2);
2344}
2345
2346static int
2347fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2348{
2349 struct fec_enet_private *fep = netdev_priv(ndev);
2350 const struct platform_device_id *id_entry =
2351 platform_get_device_id(fep->pdev);
2352
2353 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2354 return -EOPNOTSUPP;
2355
2356 ec->rx_coalesce_usecs = fep->rx_time_itr;
2357 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2358
2359 ec->tx_coalesce_usecs = fep->tx_time_itr;
2360 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2361
2362 return 0;
2363}
2364
2365static int
2366fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2367{
2368 struct fec_enet_private *fep = netdev_priv(ndev);
2369 const struct platform_device_id *id_entry =
2370 platform_get_device_id(fep->pdev);
2371
2372 unsigned int cycle;
2373
2374 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2375 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
stephen hemminger9b07be42012-01-04 12:59:49 +00002461static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002462 .get_settings = fec_enet_get_settings,
2463 .set_settings = fec_enet_set_settings,
2464 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002465 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002466 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002467 .get_coalesce = fec_enet_get_coalesce,
2468 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002469#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002470 .get_pauseparam = fec_enet_get_pauseparam,
2471 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002472 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002473 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002474 .get_sset_count = fec_enet_get_sset_count,
2475#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002476 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002477 .get_tunable = fec_enet_get_tunable,
2478 .set_tunable = fec_enet_set_tunable,
Bryan Wue6b043d2010-03-31 02:10:44 +00002479};
2480
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002481static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002482{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002483 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002484 struct phy_device *phydev = fep->phy_dev;
2485
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002486 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002487 return -EINVAL;
2488
2489 if (!phydev)
2490 return -ENODEV;
2491
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002492 if (fep->bufdesc_ex) {
2493 if (cmd == SIOCSHWTSTAMP)
2494 return fec_ptp_set(ndev, rq);
2495 if (cmd == SIOCGHWTSTAMP)
2496 return fec_ptp_get(ndev, rq);
2497 }
Frank Liff43da82013-01-03 16:04:23 +00002498
Richard Cochran28b04112010-07-17 08:48:55 +00002499 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002500}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002502static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002503{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002504 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002505 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002506 struct sk_buff *skb;
2507 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002508 struct fec_enet_priv_tx_q *txq;
2509 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002510 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002511
Frank Li59d0f7462014-09-13 05:00:50 +08002512 for (q = 0; q < fep->num_rx_queues; q++) {
2513 rxq = fep->rx_queue[q];
2514 bdp = rxq->rx_bd_base;
2515 for (i = 0; i < rxq->rx_ring_size; i++) {
2516 skb = rxq->rx_skbuff[i];
2517 rxq->rx_skbuff[i] = NULL;
2518 if (skb) {
2519 dma_unmap_single(&fep->pdev->dev,
2520 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002521 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002522 DMA_FROM_DEVICE);
2523 dev_kfree_skb(skb);
2524 }
2525 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002526 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002527 }
2528
Frank Li59d0f7462014-09-13 05:00:50 +08002529 for (q = 0; q < fep->num_tx_queues; q++) {
2530 txq = fep->tx_queue[q];
2531 bdp = txq->tx_bd_base;
2532 for (i = 0; i < txq->tx_ring_size; i++) {
2533 kfree(txq->tx_bounce[i]);
2534 txq->tx_bounce[i] = NULL;
2535 skb = txq->tx_skbuff[i];
2536 txq->tx_skbuff[i] = NULL;
2537 dev_kfree_skb(skb);
2538 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002539 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002540}
2541
Frank Li59d0f7462014-09-13 05:00:50 +08002542static void fec_enet_free_queue(struct net_device *ndev)
2543{
2544 struct fec_enet_private *fep = netdev_priv(ndev);
2545 int i;
2546 struct fec_enet_priv_tx_q *txq;
2547
2548 for (i = 0; i < fep->num_tx_queues; i++)
2549 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2550 txq = fep->tx_queue[i];
2551 dma_free_coherent(NULL,
2552 txq->tx_ring_size * TSO_HEADER_SIZE,
2553 txq->tso_hdrs,
2554 txq->tso_hdrs_dma);
2555 }
2556
2557 for (i = 0; i < fep->num_rx_queues; i++)
2558 if (fep->rx_queue[i])
2559 kfree(fep->rx_queue[i]);
2560
2561 for (i = 0; i < fep->num_tx_queues; i++)
2562 if (fep->tx_queue[i])
2563 kfree(fep->tx_queue[i]);
2564}
2565
2566static int fec_enet_alloc_queue(struct net_device *ndev)
2567{
2568 struct fec_enet_private *fep = netdev_priv(ndev);
2569 int i;
2570 int ret = 0;
2571 struct fec_enet_priv_tx_q *txq;
2572
2573 for (i = 0; i < fep->num_tx_queues; i++) {
2574 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2575 if (!txq) {
2576 ret = -ENOMEM;
2577 goto alloc_failed;
2578 }
2579
2580 fep->tx_queue[i] = txq;
2581 txq->tx_ring_size = TX_RING_SIZE;
2582 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2583
2584 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2585 txq->tx_wake_threshold =
2586 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2587
2588 txq->tso_hdrs = dma_alloc_coherent(NULL,
2589 txq->tx_ring_size * TSO_HEADER_SIZE,
2590 &txq->tso_hdrs_dma,
2591 GFP_KERNEL);
2592 if (!txq->tso_hdrs) {
2593 ret = -ENOMEM;
2594 goto alloc_failed;
2595 }
2596 }
2597
2598 for (i = 0; i < fep->num_rx_queues; i++) {
2599 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2600 GFP_KERNEL);
2601 if (!fep->rx_queue[i]) {
2602 ret = -ENOMEM;
2603 goto alloc_failed;
2604 }
2605
2606 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2607 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2608 }
2609 return ret;
2610
2611alloc_failed:
2612 fec_enet_free_queue(ndev);
2613 return ret;
2614}
2615
2616static int
2617fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002618{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002619 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002620 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002621 struct sk_buff *skb;
2622 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002623 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002624
Frank Li59d0f7462014-09-13 05:00:50 +08002625 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002626 bdp = rxq->rx_bd_base;
2627 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002628 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002629 if (!skb)
2630 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002631
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002632 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002633 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002634 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002635 }
Russell King730ee362014-07-08 00:23:14 +01002636
Fugang Duan4d494cd2014-09-13 05:00:48 +08002637 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002638 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002639
2640 if (fep->bufdesc_ex) {
2641 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2642 ebdp->cbd_esc = BD_ENET_RX_INT;
2643 }
2644
Frank Li59d0f7462014-09-13 05:00:50 +08002645 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002646 }
2647
2648 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002649 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002650 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002651 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002652
Frank Li59d0f7462014-09-13 05:00:50 +08002653 err_alloc:
2654 fec_enet_free_buffers(ndev);
2655 return -ENOMEM;
2656}
2657
2658static int
2659fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2660{
2661 struct fec_enet_private *fep = netdev_priv(ndev);
2662 unsigned int i;
2663 struct bufdesc *bdp;
2664 struct fec_enet_priv_tx_q *txq;
2665
2666 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002667 bdp = txq->tx_bd_base;
2668 for (i = 0; i < txq->tx_ring_size; i++) {
2669 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2670 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002671 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002672
2673 bdp->cbd_sc = 0;
2674 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002675
Frank Liff43da82013-01-03 16:04:23 +00002676 if (fep->bufdesc_ex) {
2677 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002678 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002679 }
2680
Frank Li59d0f7462014-09-13 05:00:50 +08002681 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002682 }
2683
2684 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002685 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002686 bdp->cbd_sc |= BD_SC_WRAP;
2687
2688 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002689
2690 err_alloc:
2691 fec_enet_free_buffers(ndev);
2692 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002693}
2694
Frank Li59d0f7462014-09-13 05:00:50 +08002695static int fec_enet_alloc_buffers(struct net_device *ndev)
2696{
2697 struct fec_enet_private *fep = netdev_priv(ndev);
2698 unsigned int i;
2699
2700 for (i = 0; i < fep->num_rx_queues; i++)
2701 if (fec_enet_alloc_rxq_buffers(ndev, i))
2702 return -ENOMEM;
2703
2704 for (i = 0; i < fep->num_tx_queues; i++)
2705 if (fec_enet_alloc_txq_buffers(ndev, i))
2706 return -ENOMEM;
2707 return 0;
2708}
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002711fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002713 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002716 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002717 ret = fec_enet_clk_enable(ndev, true);
2718 if (ret)
2719 return ret;
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /* I should reset the ring buffers here, but I don't yet know
2722 * a simple way to do that.
2723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002725 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002726 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002727 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002728
Bryan Wu418bd0d2010-05-28 03:40:39 -07002729 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002730 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002731 if (ret)
2732 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002733
Russell Kingef833372014-07-08 12:40:43 +01002734 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002735 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002736 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002737 netif_tx_start_all_queues(ndev);
2738
Sascha Hauer22f6b862009-04-15 01:32:18 +00002739 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002740
2741err_enet_mii_probe:
2742 fec_enet_free_buffers(ndev);
2743err_enet_alloc:
2744 fec_enet_clk_enable(ndev, false);
2745 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002750fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002752 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Russell Kingd76cfae2014-07-08 00:23:04 +01002754 phy_stop(fep->phy_dev);
2755
Russell King31a6de32014-07-08 12:40:23 +01002756 if (netif_device_present(ndev)) {
2757 napi_disable(&fep->napi);
2758 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002759 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Russell King635cf172014-07-08 00:22:54 +01002762 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002763 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002764
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002765 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002766 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002767 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return 0;
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772/* Set or clear the multicast filter for this adaptor.
2773 * Skeleton taken from sunlance driver.
2774 * The CPM Ethernet implementation allows Multicast as well as individual
2775 * MAC address filtering. Some of the drivers check to make sure it is
2776 * a group multicast address, and discard those that are not. I guess I
2777 * will do the same for now, but just remove the test if you want
2778 * individual filtering as well (do the upper net layers want or support
2779 * this kind of feature?).
2780 */
2781
2782#define HASH_BITS 6 /* #bits in hash */
2783#define CRC32_POLY 0xEDB88320
2784
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002785static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002787 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002788 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002789 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 unsigned char hash;
2791
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002792 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002793 tmp = readl(fep->hwp + FEC_R_CNTRL);
2794 tmp |= 0x8;
2795 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002796 return;
2797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Sascha Hauer4e831832009-04-15 01:32:19 +00002799 tmp = readl(fep->hwp + FEC_R_CNTRL);
2800 tmp &= ~0x8;
2801 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002802
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002803 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002804 /* Catch all multicast addresses, so set the
2805 * filter to all 1's
2806 */
2807 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2808 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Sascha Hauer4e831832009-04-15 01:32:19 +00002810 return;
2811 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002812
Sascha Hauer4e831832009-04-15 01:32:19 +00002813 /* Clear filter and add the addresses in hash register
2814 */
2815 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2816 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002818 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002819 /* calculate crc32 value of mac address */
2820 crc = 0xffffffff;
2821
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002822 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002823 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002824 for (bit = 0; bit < 8; bit++, data >>= 1) {
2825 crc = (crc >> 1) ^
2826 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002829
2830 /* only upper 6 bits (HASH_BITS) are used
2831 * which point to specific bit in he hash registers
2832 */
2833 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2834
2835 if (hash > 31) {
2836 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2837 tmp |= 1 << (hash - 32);
2838 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2839 } else {
2840 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2841 tmp |= 1 << hash;
2842 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845}
2846
Sascha Hauer22f6b862009-04-15 01:32:18 +00002847/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002848static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002849fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002851 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002852 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Lucas Stach44934fa2014-03-30 21:32:08 +02002854 if (addr) {
2855 if (!is_valid_ether_addr(addr->sa_data))
2856 return -EADDRNOTAVAIL;
2857 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2858 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002859
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002860 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2861 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002862 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002863 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002864 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002868#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002869/**
2870 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002871 * @dev: The FEC network adapter
2872 *
2873 * Polled functionality used by netconsole and others in non interrupt mode
2874 *
2875 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002876static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002877{
2878 int i;
2879 struct fec_enet_private *fep = netdev_priv(dev);
2880
2881 for (i = 0; i < FEC_IRQ_NUM; i++) {
2882 if (fep->irq[i] > 0) {
2883 disable_irq(fep->irq[i]);
2884 fec_enet_interrupt(fep->irq[i], dev);
2885 enable_irq(fep->irq[i]);
2886 }
2887 }
2888}
2889#endif
2890
Russell King8506fa12014-07-08 12:40:33 +01002891#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
Nimrod Andy5bc26722014-10-13 10:53:48 +08002892static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00002893 netdev_features_t features)
2894{
2895 struct fec_enet_private *fep = netdev_priv(netdev);
2896 netdev_features_t changed = features ^ netdev->features;
2897
2898 netdev->features = features;
2899
2900 /* Receive checksum has been changed */
2901 if (changed & NETIF_F_RXCSUM) {
2902 if (features & NETIF_F_RXCSUM)
2903 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2904 else
2905 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002906 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08002907}
Jim Baxter4c09eed2013-04-19 08:10:49 +00002908
Nimrod Andy5bc26722014-10-13 10:53:48 +08002909static int fec_set_features(struct net_device *netdev,
2910 netdev_features_t features)
2911{
2912 struct fec_enet_private *fep = netdev_priv(netdev);
2913 netdev_features_t changed = features ^ netdev->features;
2914
Russell King8506fa12014-07-08 12:40:33 +01002915 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08002916 napi_disable(&fep->napi);
2917 netif_tx_lock_bh(netdev);
2918 fec_stop(netdev);
2919 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01002920 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002921 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002922 netif_tx_unlock_bh(netdev);
2923 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08002924 } else {
2925 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002926 }
2927
2928 return 0;
2929}
2930
Sascha Hauer009fda82009-04-15 01:32:23 +00002931static const struct net_device_ops fec_netdev_ops = {
2932 .ndo_open = fec_enet_open,
2933 .ndo_stop = fec_enet_close,
2934 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002935 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002936 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002937 .ndo_validate_addr = eth_validate_addr,
2938 .ndo_tx_timeout = fec_timeout,
2939 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002940 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002941#ifdef CONFIG_NET_POLL_CONTROLLER
2942 .ndo_poll_controller = fec_poll_controller,
2943#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002944 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002945};
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /*
2948 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002949 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002951static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002953 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002954 const struct platform_device_id *id_entry =
2955 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002956 struct fec_enet_priv_tx_q *txq;
2957 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002958 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002959 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002960 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002961 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002962
Fugang Duan41ef84c2014-09-13 05:00:54 +08002963#if defined(CONFIG_ARM)
2964 fep->rx_align = 0xf;
2965 fep->tx_align = 0xf;
2966#else
2967 fep->rx_align = 0x3;
2968 fep->tx_align = 0x3;
2969#endif
2970
Frank Li59d0f7462014-09-13 05:00:50 +08002971 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002972
Nimrod Andy55d02182014-06-12 08:16:21 +08002973 if (fep->bufdesc_ex)
2974 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2975 else
2976 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002977 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002978 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002980 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002981 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002982 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002983 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002984 return -ENOMEM;
2985 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002986
Fugang Duan4d494cd2014-09-13 05:00:48 +08002987 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Shawn Guo49da97d2011-01-05 21:13:11 +00002989 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002990 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002991 /* make sure MAC we just acquired is programmed into the hw */
2992 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002994 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08002995 for (i = 0; i < fep->num_rx_queues; i++) {
2996 rxq = fep->rx_queue[i];
2997 rxq->index = i;
2998 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2999 rxq->bd_dma = bd_dma;
3000 if (fep->bufdesc_ex) {
3001 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3002 cbd_base = (struct bufdesc *)
3003 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3004 } else {
3005 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3006 cbd_base += rxq->rx_ring_size;
3007 }
3008 }
3009
3010 for (i = 0; i < fep->num_tx_queues; i++) {
3011 txq = fep->tx_queue[i];
3012 txq->index = i;
3013 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3014 txq->bd_dma = bd_dma;
3015 if (fep->bufdesc_ex) {
3016 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3017 cbd_base = (struct bufdesc *)
3018 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3019 } else {
3020 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3021 cbd_base += txq->tx_ring_size;
3022 }
3023 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Sascha Hauer22f6b862009-04-15 01:32:18 +00003026 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003027 ndev->watchdog_timeo = TX_TIMEOUT;
3028 ndev->netdev_ops = &fec_netdev_ops;
3029 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003030
Frank Lidc975382013-01-28 18:31:42 +00003031 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003032 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003033
Nimrod Andy09d1e542014-06-12 08:16:20 +08003034 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003035 /* enable hw VLAN support */
3036 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003037
Shawn Guo48496252013-05-08 21:08:22 +00003038 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003039 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3040
Shawn Guo48496252013-05-08 21:08:22 +00003041 /* enable hw accelerator */
3042 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003043 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003044 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3045 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003046
Fugang Duan41ef84c2014-09-13 05:00:54 +08003047 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3048 fep->tx_align = 0;
3049 fep->rx_align = 0x3f;
3050 }
3051
Nimrod Andy09d1e542014-06-12 08:16:20 +08003052 ndev->hw_features = ndev->features;
3053
Russell Kingef833372014-07-08 12:40:43 +01003054 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 0;
3057}
3058
Shawn Guoca2cc332011-06-25 02:04:35 +08003059#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003060static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003061{
3062 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003063 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003064 struct device_node *np = pdev->dev.of_node;
3065
3066 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003067 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003068
Shawn Guoa3caad02012-06-27 03:45:24 +00003069 of_property_read_u32(np, "phy-reset-duration", &msec);
3070 /* A sane reset duration should not be longer than 1s */
3071 if (msec > 1000)
3072 msec = 1;
3073
Shawn Guoca2cc332011-06-25 02:04:35 +08003074 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003075 if (!gpio_is_valid(phy_reset))
3076 return;
3077
Shawn Guo119fc002012-06-27 03:45:22 +00003078 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3079 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003080 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003081 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003082 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003083 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003084 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003085 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003086}
3087#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003088static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003089{
3090 /*
3091 * In case of platform probe, the reset has been done
3092 * by machine code.
3093 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003094}
3095#endif /* CONFIG_OF */
3096
Fugang Duan9fc095f2014-09-13 05:00:49 +08003097static void
3098fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3099{
3100 struct device_node *np = pdev->dev.of_node;
3101 int err;
3102
3103 *num_tx = *num_rx = 1;
3104
3105 if (!np || !of_device_is_available(np))
3106 return;
3107
3108 /* parse the num of tx and rx queues */
3109 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003110 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003111 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003112
3113 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3114 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003115 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003116
3117 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003118 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3119 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003120 *num_tx = 1;
3121 return;
3122 }
3123
3124 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003125 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3126 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003127 *num_rx = 1;
3128 return;
3129 }
3130
3131}
3132
Bill Pemberton33897cc2012-12-03 09:23:58 -05003133static int
Sascha Haueread73182009-01-28 23:03:11 +00003134fec_probe(struct platform_device *pdev)
3135{
3136 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003137 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003138 struct net_device *ndev;
3139 int i, irq, ret = 0;
3140 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003141 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003142 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003143 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003144 int num_tx_qs;
3145 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003146
3147 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3148 if (of_id)
3149 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003150
Fugang Duan9fc095f2014-09-13 05:00:49 +08003151 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3152
Sascha Haueread73182009-01-28 23:03:11 +00003153 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003154 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3155 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003156 if (!ndev)
3157 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003158
3159 SET_NETDEV_DEV(ndev, &pdev->dev);
3160
3161 /* setup board info structure */
3162 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003163
Fugang Duan9fc095f2014-09-13 05:00:49 +08003164 fep->num_rx_queues = num_rx_qs;
3165 fep->num_tx_queues = num_tx_qs;
3166
Guenter Roeckd1391932013-06-18 10:04:59 -07003167#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003168 /* default enable pause frame auto negotiation */
3169 if (pdev->id_entry &&
3170 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3171 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003172#endif
Frank Libaa70a52013-01-16 16:55:58 +00003173
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003174 /* Select default pin state */
3175 pinctrl_pm_select_default_state(&pdev->dev);
3176
Fabio Estevam399db752013-07-21 13:25:03 -03003177 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303178 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3179 if (IS_ERR(fep->hwp)) {
3180 ret = PTR_ERR(fep->hwp);
3181 goto failed_ioremap;
3182 }
3183
Bryan Wue6b043d2010-03-31 02:10:44 +00003184 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003185 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003186
Sascha Haueread73182009-01-28 23:03:11 +00003187 platform_set_drvdata(pdev, ndev);
3188
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003189 phy_node = of_parse_phandle(np, "phy-handle", 0);
3190 if (!phy_node && of_phy_is_fixed_link(np)) {
3191 ret = of_phy_register_fixed_link(np);
3192 if (ret < 0) {
3193 dev_err(&pdev->dev,
3194 "broken fixed-link specification\n");
3195 goto failed_phy;
3196 }
3197 phy_node = of_node_get(np);
3198 }
3199 fep->phy_node = phy_node;
3200
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003201 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003202 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003203 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003204 if (pdata)
3205 fep->phy_interface = pdata->phy;
3206 else
3207 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3208 } else {
3209 fep->phy_interface = ret;
3210 }
3211
Fabio Estevame2f8d552013-02-22 06:40:45 +00003212 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3213 if (IS_ERR(fep->clk_ipg)) {
3214 ret = PTR_ERR(fep->clk_ipg);
3215 goto failed_clk;
3216 }
3217
3218 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3219 if (IS_ERR(fep->clk_ahb)) {
3220 ret = PTR_ERR(fep->clk_ahb);
3221 goto failed_clk;
3222 }
3223
Fugang Duand851b472014-09-17 05:18:52 +08003224 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3225
Linus Torvalds38f56f32013-05-07 11:06:17 -07003226 /* enet_out is optional, depends on board */
3227 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3228 if (IS_ERR(fep->clk_enet_out))
3229 fep->clk_enet_out = NULL;
3230
Nimrod Andy91c0d982014-08-21 17:09:38 +08003231 fep->ptp_clk_on = false;
3232 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003233
3234 /* clk_ref is optional, depends on board */
3235 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3236 if (IS_ERR(fep->clk_ref))
3237 fep->clk_ref = NULL;
3238
Fabio Estevame2f8d552013-02-22 06:40:45 +00003239 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003240 fep->bufdesc_ex =
3241 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003242 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003243 fep->clk_ptp = NULL;
Lothar Waßmann217b5842014-11-17 10:51:20 +01003244 fep->bufdesc_ex = false;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003245 }
3246
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003247 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003248 if (ret)
3249 goto failed_clk;
3250
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003251 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3252 if (!IS_ERR(fep->reg_phy)) {
3253 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003254 if (ret) {
3255 dev_err(&pdev->dev,
3256 "Failed to enable phy regulator: %d\n", ret);
3257 goto failed_regulator;
3258 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003259 } else {
3260 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003261 }
3262
3263 fec_reset_phy(pdev);
3264
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003265 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003266 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003267
3268 ret = fec_enet_init(ndev);
3269 if (ret)
3270 goto failed_init;
3271
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003272 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003273 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003274 if (irq < 0) {
3275 if (i)
3276 break;
3277 ret = irq;
3278 goto failed_irq;
3279 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003280 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003281 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003282 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003283 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003284 }
3285
Fugang Duanb4d39b52014-09-13 05:00:55 +08003286 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003287 ret = fec_enet_mii_init(pdev);
3288 if (ret)
3289 goto failed_mii_init;
3290
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003291 /* Carrier starts down, phylib will bring it up */
3292 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003293 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003294 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003295
Sascha Haueread73182009-01-28 23:03:11 +00003296 ret = register_netdev(ndev);
3297 if (ret)
3298 goto failed_register;
3299
Fabio Estevameb1d0642013-04-13 07:25:36 +00003300 if (fep->bufdesc_ex && fep->ptp_clock)
3301 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3302
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003303 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003304 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003305 return 0;
3306
3307failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003308 fec_enet_mii_remove(fep);
3309failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003310failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003311failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003312 if (fep->reg_phy)
3313 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003314failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003315 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003316failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003317failed_phy:
3318 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003319failed_ioremap:
3320 free_netdev(ndev);
3321
3322 return ret;
3323}
3324
Bill Pemberton33897cc2012-12-03 09:23:58 -05003325static int
Sascha Haueread73182009-01-28 23:03:11 +00003326fec_drv_remove(struct platform_device *pdev)
3327{
3328 struct net_device *ndev = platform_get_drvdata(pdev);
3329 struct fec_enet_private *fep = netdev_priv(ndev);
3330
Nimrod Andy91c0d982014-08-21 17:09:38 +08003331 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003332 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003333 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003334 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003335 if (fep->reg_phy)
3336 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003337 if (fep->ptp_clock)
3338 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003339 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003340 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003341 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003342
Sascha Haueread73182009-01-28 23:03:11 +00003343 return 0;
3344}
3345
Fabio Estevamdd66d382014-07-24 18:24:01 -03003346static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003347{
Eric Benard87cad5c2010-06-18 04:19:54 +00003348 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003349 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003350
Russell Kingda1774e2014-07-08 12:39:57 +01003351 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003352 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003353 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003354 napi_disable(&fep->napi);
3355 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003356 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003357 netif_tx_unlock_bh(ndev);
3358 fec_stop(ndev);
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003359 fec_enet_clk_enable(ndev, false);
3360 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Sascha Haueread73182009-01-28 23:03:11 +00003361 }
Russell Kingda1774e2014-07-08 12:39:57 +01003362 rtnl_unlock();
3363
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003364 if (fep->reg_phy)
3365 regulator_disable(fep->reg_phy);
3366
Sascha Haueread73182009-01-28 23:03:11 +00003367 return 0;
3368}
3369
Fabio Estevamdd66d382014-07-24 18:24:01 -03003370static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003371{
Eric Benard87cad5c2010-06-18 04:19:54 +00003372 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003373 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003374 int ret;
3375
3376 if (fep->reg_phy) {
3377 ret = regulator_enable(fep->reg_phy);
3378 if (ret)
3379 return ret;
3380 }
Sascha Haueread73182009-01-28 23:03:11 +00003381
Russell Kingda1774e2014-07-08 12:39:57 +01003382 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003383 if (netif_running(ndev)) {
Nimrod Andyf4c4a4e2014-11-03 13:26:50 +08003384 pinctrl_pm_select_default_state(&fep->pdev->dev);
3385 ret = fec_enet_clk_enable(ndev, true);
3386 if (ret) {
3387 rtnl_unlock();
3388 goto failed_clk;
3389 }
Russell Kingef833372014-07-08 12:40:43 +01003390 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003391 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003392 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003393 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003394 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003395 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003396 }
Russell Kingda1774e2014-07-08 12:39:57 +01003397 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003398
Sascha Haueread73182009-01-28 23:03:11 +00003399 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003400
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003401failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003402 if (fep->reg_phy)
3403 regulator_disable(fep->reg_phy);
3404 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003405}
3406
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003407static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003408
Sascha Haueread73182009-01-28 23:03:11 +00003409static struct platform_driver fec_driver = {
3410 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003411 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003412 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003413 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003414 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003415 },
Shawn Guob5680e02011-01-05 21:13:13 +00003416 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003417 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003418 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003419};
3420
Fabio Estevamaaca2372012-01-23 16:44:50 +00003421module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003423MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424MODULE_LICENSE("GPL");