blob: 50a851db2852e28979a0b78f481347fc9e29778a [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
Russell King344756f2014-07-08 13:01:59 +0100301static void fec_dump(struct net_device *ndev)
302{
303 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800304 struct bufdesc *bdp;
305 struct fec_enet_priv_tx_q *txq;
306 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100307
308 netdev_info(ndev, "TX ring dump\n");
309 pr_info("Nr SC addr len SKB\n");
310
Fugang Duan4d494cd2014-09-13 05:00:48 +0800311 txq = fep->tx_queue[0];
312 bdp = txq->tx_bd_base;
313
Russell King344756f2014-07-08 13:01:59 +0100314 do {
315 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
316 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800317 bdp == txq->cur_tx ? 'S' : ' ',
318 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100319 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800320 txq->tx_skbuff[index]);
321 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100322 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800323 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100324}
325
Fugang Duan62a02c92014-06-18 08:33:52 +0800326static inline bool is_ipv4_pkt(struct sk_buff *skb)
327{
328 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
329}
330
Jim Baxter4c09eed2013-04-19 08:10:49 +0000331static int
332fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
333{
334 /* Only run for packets requiring a checksum. */
335 if (skb->ip_summed != CHECKSUM_PARTIAL)
336 return 0;
337
338 if (unlikely(skb_cow_head(skb, 0)))
339 return -1;
340
Fugang Duan62a02c92014-06-18 08:33:52 +0800341 if (is_ipv4_pkt(skb))
342 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000343 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
344
345 return 0;
346}
347
Nimrod Andy6e909282014-06-12 08:16:22 +0800348static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800349fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
350 struct sk_buff *skb,
351 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800352{
353 struct fec_enet_private *fep = netdev_priv(ndev);
354 const struct platform_device_id *id_entry =
355 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800356 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800357 struct bufdesc_ex *ebdp;
358 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800359 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800360 int frag, frag_len;
361 unsigned short status;
362 unsigned int estatus = 0;
363 skb_frag_t *this_frag;
364 unsigned int index;
365 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100366 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800367 int i;
368
369 for (frag = 0; frag < nr_frags; frag++) {
370 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800371 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800372 ebdp = (struct bufdesc_ex *)bdp;
373
374 status = bdp->cbd_sc;
375 status &= ~BD_ENET_TX_STATS;
376 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
377 frag_len = skb_shinfo(skb)->frags[frag].size;
378
379 /* Handle the last BD specially */
380 if (frag == nr_frags - 1) {
381 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
382 if (fep->bufdesc_ex) {
383 estatus |= BD_ENET_TX_INT;
384 if (unlikely(skb_shinfo(skb)->tx_flags &
385 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
386 estatus |= BD_ENET_TX_TS;
387 }
388 }
389
390 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800391 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
392 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800393 if (skb->ip_summed == CHECKSUM_PARTIAL)
394 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
395 ebdp->cbd_bdu = 0;
396 ebdp->cbd_esc = estatus;
397 }
398
399 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
400
Fugang Duan4d494cd2014-09-13 05:00:48 +0800401 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800402 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800403 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800404 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
405 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800406
407 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
408 swap_buffer(bufaddr, frag_len);
409 }
410
Russell Kingd6bf3142014-07-08 00:23:19 +0100411 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
412 DMA_TO_DEVICE);
413 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800414 dev_kfree_skb_any(skb);
415 if (net_ratelimit())
416 netdev_err(ndev, "Tx DMA memory map failed\n");
417 goto dma_mapping_error;
418 }
419
Russell Kingd6bf3142014-07-08 00:23:19 +0100420 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800421 bdp->cbd_datlen = frag_len;
422 bdp->cbd_sc = status;
423 }
424
Fugang Duan4d494cd2014-09-13 05:00:48 +0800425 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800426
427 return 0;
428
429dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800430 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800431 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800432 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800433 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
434 bdp->cbd_datlen, DMA_TO_DEVICE);
435 }
436 return NETDEV_TX_OK;
437}
438
Fugang Duan4d494cd2014-09-13 05:00:48 +0800439static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
440 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800441{
442 struct fec_enet_private *fep = netdev_priv(ndev);
443 const struct platform_device_id *id_entry =
444 platform_get_device_id(fep->pdev);
445 int nr_frags = skb_shinfo(skb)->nr_frags;
446 struct bufdesc *bdp, *last_bdp;
447 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100448 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800449 unsigned short status;
450 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800451 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800452 unsigned int estatus = 0;
453 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800454 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800455 int ret;
456
Fugang Duan4d494cd2014-09-13 05:00:48 +0800457 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800458 if (entries_free < MAX_SKB_FRAGS + 1) {
459 dev_kfree_skb_any(skb);
460 if (net_ratelimit())
461 netdev_err(ndev, "NOT enough BD for SG!\n");
462 return NETDEV_TX_OK;
463 }
464
Nimrod Andy6e909282014-06-12 08:16:22 +0800465 /* Protocol checksum off-load for TCP and UDP. */
466 if (fec_enet_clear_csum(skb, ndev)) {
467 dev_kfree_skb_any(skb);
468 return NETDEV_TX_OK;
469 }
470
471 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800472 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800473 status = bdp->cbd_sc;
474 status &= ~BD_ENET_TX_STATS;
475
476 /* Set buffer length and buffer pointer */
477 bufaddr = skb->data;
478 buflen = skb_headlen(skb);
479
Fugang Duan4d494cd2014-09-13 05:00:48 +0800480 queue = skb_get_queue_mapping(skb);
481 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800482 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800483 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800484 memcpy(txq->tx_bounce[index], skb->data, buflen);
485 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800486
487 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
488 swap_buffer(bufaddr, buflen);
489 }
490
Russell Kingd6bf3142014-07-08 00:23:19 +0100491 /* Push the data cache so the CPM does not get stale memory data. */
492 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
493 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800494 dev_kfree_skb_any(skb);
495 if (net_ratelimit())
496 netdev_err(ndev, "Tx DMA memory map failed\n");
497 return NETDEV_TX_OK;
498 }
499
500 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800501 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800502 if (ret)
503 return ret;
504 } else {
505 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
506 if (fep->bufdesc_ex) {
507 estatus = BD_ENET_TX_INT;
508 if (unlikely(skb_shinfo(skb)->tx_flags &
509 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
510 estatus |= BD_ENET_TX_TS;
511 }
512 }
513
514 if (fep->bufdesc_ex) {
515
516 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
517
518 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
519 fep->hwts_tx_en))
520 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
521
Nimrod Andybefe8212014-09-23 15:40:55 +0800522 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
523 estatus |= FEC_TX_BD_FTYPE(queue);
524
Nimrod Andy6e909282014-06-12 08:16:22 +0800525 if (skb->ip_summed == CHECKSUM_PARTIAL)
526 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
527
528 ebdp->cbd_bdu = 0;
529 ebdp->cbd_esc = estatus;
530 }
531
Fugang Duan4d494cd2014-09-13 05:00:48 +0800532 last_bdp = txq->cur_tx;
533 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800534 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800535 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800536
537 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100538 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800539
540 /* Send it on its way. Tell FEC it's ready, interrupt when done,
541 * it's the last BD of the frame, and to put the CRC on the end.
542 */
543 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
544 bdp->cbd_sc = status;
545
Sascha Hauer22f6b862009-04-15 01:32:18 +0000546 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800547 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800549 skb_tx_timestamp(skb);
550
Fugang Duan4d494cd2014-09-13 05:00:48 +0800551 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Frank Lide5fb0a2013-03-03 17:34:25 +0000553 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800554 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000555
Nimrod Andy6e909282014-06-12 08:16:22 +0800556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Nimrod Andy79f33912014-06-12 08:16:23 +0800559static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800560fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
561 struct net_device *ndev,
562 struct bufdesc *bdp, int index, char *data,
563 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800564{
565 struct fec_enet_private *fep = netdev_priv(ndev);
566 const struct platform_device_id *id_entry =
567 platform_get_device_id(fep->pdev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200568 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800569 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800570 unsigned short status;
571 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100572 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800573
574 status = bdp->cbd_sc;
575 status &= ~BD_ENET_TX_STATS;
576
577 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800578
Fugang Duan41ef84c2014-09-13 05:00:54 +0800579 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800580 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800581 memcpy(txq->tx_bounce[index], data, size);
582 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800583
584 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
585 swap_buffer(data, size);
586 }
587
Russell Kingd6bf3142014-07-08 00:23:19 +0100588 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
589 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800590 dev_kfree_skb_any(skb);
591 if (net_ratelimit())
592 netdev_err(ndev, "Tx DMA memory map failed\n");
593 return NETDEV_TX_BUSY;
594 }
595
Russell Kingd6bf3142014-07-08 00:23:19 +0100596 bdp->cbd_datlen = size;
597 bdp->cbd_bufaddr = addr;
598
Nimrod Andy79f33912014-06-12 08:16:23 +0800599 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800600 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
601 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800602 if (skb->ip_summed == CHECKSUM_PARTIAL)
603 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
604 ebdp->cbd_bdu = 0;
605 ebdp->cbd_esc = estatus;
606 }
607
608 /* Handle the last BD specially */
609 if (last_tcp)
610 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
611 if (is_last) {
612 status |= BD_ENET_TX_INTR;
613 if (fep->bufdesc_ex)
614 ebdp->cbd_esc |= BD_ENET_TX_INT;
615 }
616
617 bdp->cbd_sc = status;
618
619 return 0;
620}
621
622static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800623fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
624 struct sk_buff *skb, struct net_device *ndev,
625 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800626{
627 struct fec_enet_private *fep = netdev_priv(ndev);
628 const struct platform_device_id *id_entry =
629 platform_get_device_id(fep->pdev);
630 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200631 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800632 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800633 void *bufaddr;
634 unsigned long dmabuf;
635 unsigned short status;
636 unsigned int estatus = 0;
637
638 status = bdp->cbd_sc;
639 status &= ~BD_ENET_TX_STATS;
640 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
641
Fugang Duan4d494cd2014-09-13 05:00:48 +0800642 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
643 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800644 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800645 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800646 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
647 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800648
649 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
650 swap_buffer(bufaddr, hdr_len);
651
652 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
653 hdr_len, DMA_TO_DEVICE);
654 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
655 dev_kfree_skb_any(skb);
656 if (net_ratelimit())
657 netdev_err(ndev, "Tx DMA memory map failed\n");
658 return NETDEV_TX_BUSY;
659 }
660 }
661
662 bdp->cbd_bufaddr = dmabuf;
663 bdp->cbd_datlen = hdr_len;
664
665 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800666 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
667 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800668 if (skb->ip_summed == CHECKSUM_PARTIAL)
669 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
670 ebdp->cbd_bdu = 0;
671 ebdp->cbd_esc = estatus;
672 }
673
674 bdp->cbd_sc = status;
675
676 return 0;
677}
678
Fugang Duan4d494cd2014-09-13 05:00:48 +0800679static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
680 struct sk_buff *skb,
681 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800682{
683 struct fec_enet_private *fep = netdev_priv(ndev);
684 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
685 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800686 struct bufdesc *bdp = txq->cur_tx;
687 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800688 struct tso_t tso;
689 unsigned int index = 0;
690 int ret;
Fugang Duan37d60172014-09-17 05:18:54 +0800691 const struct platform_device_id *id_entry =
692 platform_get_device_id(fep->pdev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800693
Fugang Duan4d494cd2014-09-13 05:00:48 +0800694 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800695 dev_kfree_skb_any(skb);
696 if (net_ratelimit())
697 netdev_err(ndev, "NOT enough BD for TSO!\n");
698 return NETDEV_TX_OK;
699 }
700
701 /* Protocol checksum off-load for TCP and UDP. */
702 if (fec_enet_clear_csum(skb, ndev)) {
703 dev_kfree_skb_any(skb);
704 return NETDEV_TX_OK;
705 }
706
707 /* Initialize the TSO handler, and prepare the first payload */
708 tso_start(skb, &tso);
709
710 total_len = skb->len - hdr_len;
711 while (total_len > 0) {
712 char *hdr;
713
Fugang Duan4d494cd2014-09-13 05:00:48 +0800714 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800715 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
716 total_len -= data_left;
717
718 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800719 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800720 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800721 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800722 if (ret)
723 goto err_release;
724
725 while (data_left > 0) {
726 int size;
727
728 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800729 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
730 index = fec_enet_get_bd_index(txq->tx_bd_base,
731 bdp, fep);
732 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
733 bdp, index,
734 tso.data, size,
735 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800736 total_len == 0);
737 if (ret)
738 goto err_release;
739
740 data_left -= size;
741 tso_build_data(skb, &tso, size);
742 }
743
Fugang Duan4d494cd2014-09-13 05:00:48 +0800744 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800745 }
746
747 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800748 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800749
Nimrod Andy79f33912014-06-12 08:16:23 +0800750 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800751 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800752
753 /* Trigger transmission start */
Fugang Duan37d60172014-09-17 05:18:54 +0800754 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
755 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
756 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
757 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
758 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
759 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800760
761 return 0;
762
763err_release:
764 /* TODO: Release all used data descriptors for TSO */
765 return ret;
766}
767
Nimrod Andy61a44272014-06-12 08:16:18 +0800768static netdev_tx_t
769fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
770{
771 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800772 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800773 unsigned short queue;
774 struct fec_enet_priv_tx_q *txq;
775 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800776 int ret;
777
Fugang Duan4d494cd2014-09-13 05:00:48 +0800778 queue = skb_get_queue_mapping(skb);
779 txq = fep->tx_queue[queue];
780 nq = netdev_get_tx_queue(ndev, queue);
781
Nimrod Andy79f33912014-06-12 08:16:23 +0800782 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800783 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800784 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800785 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800786 if (ret)
787 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800788
Fugang Duan4d494cd2014-09-13 05:00:48 +0800789 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
790 if (entries_free <= txq->tx_stop_threshold)
791 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800792
793 return NETDEV_TX_OK;
794}
795
Frank Li14109a52013-03-26 16:12:03 +0000796/* Init RX & TX buffer descriptors
797 */
798static void fec_enet_bd_init(struct net_device *dev)
799{
800 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800801 struct fec_enet_priv_tx_q *txq;
802 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000803 struct bufdesc *bdp;
804 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800805 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000806
Frank Li59d0f7462014-09-13 05:00:50 +0800807 for (q = 0; q < fep->num_rx_queues; q++) {
808 /* Initialize the receive buffer descriptors. */
809 rxq = fep->rx_queue[q];
810 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800811
Frank Li59d0f7462014-09-13 05:00:50 +0800812 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000813
Frank Li59d0f7462014-09-13 05:00:50 +0800814 /* Initialize the BD for every fragment in the page. */
815 if (bdp->cbd_bufaddr)
816 bdp->cbd_sc = BD_ENET_RX_EMPTY;
817 else
818 bdp->cbd_sc = 0;
819 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000820 }
Frank Li59d0f7462014-09-13 05:00:50 +0800821
822 /* Set the last buffer to wrap */
823 bdp = fec_enet_get_prevdesc(bdp, fep, q);
824 bdp->cbd_sc |= BD_SC_WRAP;
825
826 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000827 }
828
Frank Li59d0f7462014-09-13 05:00:50 +0800829 for (q = 0; q < fep->num_tx_queues; q++) {
830 /* ...and the same for transmit */
831 txq = fep->tx_queue[q];
832 bdp = txq->tx_bd_base;
833 txq->cur_tx = bdp;
834
835 for (i = 0; i < txq->tx_ring_size; i++) {
836 /* Initialize the BD for every fragment in the page. */
837 bdp->cbd_sc = 0;
838 if (txq->tx_skbuff[i]) {
839 dev_kfree_skb_any(txq->tx_skbuff[i]);
840 txq->tx_skbuff[i] = NULL;
841 }
842 bdp->cbd_bufaddr = 0;
843 bdp = fec_enet_get_nextdesc(bdp, fep, q);
844 }
845
846 /* Set the last buffer to wrap */
847 bdp = fec_enet_get_prevdesc(bdp, fep, q);
848 bdp->cbd_sc |= BD_SC_WRAP;
849 txq->dirty_tx = bdp;
850 }
851}
852
Frank Lice99d0d2014-09-13 05:00:52 +0800853static void fec_enet_active_rxring(struct net_device *ndev)
854{
855 struct fec_enet_private *fep = netdev_priv(ndev);
856 int i;
857
858 for (i = 0; i < fep->num_rx_queues; i++)
859 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
860}
861
Frank Li59d0f7462014-09-13 05:00:50 +0800862static void fec_enet_enable_ring(struct net_device *ndev)
863{
864 struct fec_enet_private *fep = netdev_priv(ndev);
865 struct fec_enet_priv_tx_q *txq;
866 struct fec_enet_priv_rx_q *rxq;
867 int i;
868
869 for (i = 0; i < fep->num_rx_queues; i++) {
870 rxq = fep->rx_queue[i];
871 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
872
873 /* enable DMA1/2 */
874 if (i)
875 writel(RCMR_MATCHEN | RCMR_CMP(i),
876 fep->hwp + FEC_RCMR(i));
877 }
878
879 for (i = 0; i < fep->num_tx_queues; i++) {
880 txq = fep->tx_queue[i];
881 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
882
883 /* enable DMA1/2 */
884 if (i)
885 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
886 fep->hwp + FEC_DMA_CFG(i));
887 }
888}
889
890static void fec_enet_reset_skb(struct net_device *ndev)
891{
892 struct fec_enet_private *fep = netdev_priv(ndev);
893 struct fec_enet_priv_tx_q *txq;
894 int i, j;
895
896 for (i = 0; i < fep->num_tx_queues; i++) {
897 txq = fep->tx_queue[i];
898
899 for (j = 0; j < txq->tx_ring_size; j++) {
900 if (txq->tx_skbuff[j]) {
901 dev_kfree_skb_any(txq->tx_skbuff[j]);
902 txq->tx_skbuff[j] = NULL;
903 }
904 }
905 }
Frank Li14109a52013-03-26 16:12:03 +0000906}
907
Russell Kingdbc64a82014-07-08 12:40:12 +0100908/*
909 * This function is called to start or restart the FEC during a link
910 * change, transmit timeout, or to reconfigure the FEC. The network
911 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100912 */
913static void
Russell Kingef833372014-07-08 12:40:43 +0100914fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100915{
916 struct fec_enet_private *fep = netdev_priv(ndev);
917 const struct platform_device_id *id_entry =
918 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000919 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100920 u32 temp_mac[2];
921 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000922 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100923
Fugang Duan106c3142014-09-13 05:00:51 +0800924 /* Whack a reset. We should wait for this.
925 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
926 * instead of reset MAC itself.
927 */
928 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
929 writel(0, fep->hwp + FEC_ECNTRL);
930 } else {
931 writel(1, fep->hwp + FEC_ECNTRL);
932 udelay(10);
933 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100934
935 /*
936 * enet-mac reset will reset mac address registers too,
937 * so need to reconfigure it.
938 */
939 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
940 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
941 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
942 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
943 }
944
945 /* Clear any outstanding interrupt. */
946 writel(0xffc00000, fep->hwp + FEC_IEVENT);
947
Uwe Kleine-König45993652011-01-19 20:47:04 +0100948 /* Set maximum receive buffer size. */
949 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
950
Frank Li14109a52013-03-26 16:12:03 +0000951 fec_enet_bd_init(ndev);
952
Frank Li59d0f7462014-09-13 05:00:50 +0800953 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100954
Frank Li59d0f7462014-09-13 05:00:50 +0800955 /* Reset tx SKB buffers. */
956 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100957
958 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100959 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100960 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100961 writel(0x04, fep->hwp + FEC_X_CNTRL);
962 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100963 /* No Rcv on Xmit */
964 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100965 writel(0x0, fep->hwp + FEC_X_CNTRL);
966 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100967
Uwe Kleine-König45993652011-01-19 20:47:04 +0100968 /* Set MII speed */
969 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
970
Guenter Roeckd1391932013-06-18 10:04:59 -0700971#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000972 /* set RX checksum */
973 val = readl(fep->hwp + FEC_RACC);
974 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
975 val |= FEC_RACC_OPTIONS;
976 else
977 val &= ~FEC_RACC_OPTIONS;
978 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700979#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000980
Uwe Kleine-König45993652011-01-19 20:47:04 +0100981 /*
982 * The phy interface and speed need to get configured
983 * differently on enet-mac.
984 */
985 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100986 /* Enable flow control and length check */
987 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100988
Shawn Guo230dec62011-09-23 02:12:48 +0000989 /* RGMII, RMII or MII */
990 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
991 rcntl |= (1 << 6);
992 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100993 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100994 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100995 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100996
Shawn Guo230dec62011-09-23 02:12:48 +0000997 /* 1G, 100M or 10M */
998 if (fep->phy_dev) {
999 if (fep->phy_dev->speed == SPEED_1000)
1000 ecntl |= (1 << 5);
1001 else if (fep->phy_dev->speed == SPEED_100)
1002 rcntl &= ~(1 << 9);
1003 else
1004 rcntl |= (1 << 9);
1005 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001006 } else {
1007#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001008 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001009 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001010 /* disable the gasket and wait */
1011 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1012 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1013 udelay(1);
1014
1015 /*
1016 * configure the gasket:
1017 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001018 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001019 */
Eric Benard8d82f212012-01-12 06:10:28 +00001020 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1021 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1022 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1023 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1024 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001025
1026 /* re-enable the gasket */
1027 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1028 }
1029#endif
1030 }
Frank Libaa70a52013-01-16 16:55:58 +00001031
Guenter Roeckd1391932013-06-18 10:04:59 -07001032#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001033 /* enable pause frame*/
1034 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1035 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1036 fep->phy_dev && fep->phy_dev->pause)) {
1037 rcntl |= FEC_ENET_FCE;
1038
Jim Baxter4c09eed2013-04-19 08:10:49 +00001039 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001040 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1041 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1042 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1043 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1044
1045 /* OPD */
1046 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1047 } else {
1048 rcntl &= ~FEC_ENET_FCE;
1049 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001050#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001051
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001052 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001053
Stefan Wahren84fe6182014-03-12 11:28:19 +01001054 /* Setup multicast filter. */
1055 set_multicast_list(ndev);
1056#ifndef CONFIG_M5272
1057 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1058 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1059#endif
1060
Shawn Guo230dec62011-09-23 02:12:48 +00001061 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1062 /* enable ENET endian swap */
1063 ecntl |= (1 << 8);
1064 /* enable ENET store and forward mode */
1065 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1066 }
1067
Frank Liff43da82013-01-03 16:04:23 +00001068 if (fep->bufdesc_ex)
1069 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001070
Chris Healy38ae92d2013-06-25 23:18:52 -07001071#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001072 /* Enable the MIB statistic event counters */
1073 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001074#endif
1075
Uwe Kleine-König45993652011-01-19 20:47:04 +01001076 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001077 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001078 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001079
Frank Liff43da82013-01-03 16:04:23 +00001080 if (fep->bufdesc_ex)
1081 fec_ptp_start_cyclecounter(ndev);
1082
Uwe Kleine-König45993652011-01-19 20:47:04 +01001083 /* Enable interrupts we wish to service */
1084 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001085
1086 /* Init the interrupt coalescing */
1087 fec_enet_itr_coal_init(ndev);
1088
Uwe Kleine-König45993652011-01-19 20:47:04 +01001089}
1090
1091static void
1092fec_stop(struct net_device *ndev)
1093{
1094 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001095 const struct platform_device_id *id_entry =
1096 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001097 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001098
1099 /* We cannot expect a graceful transmit stop without link !!! */
1100 if (fep->link) {
1101 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1102 udelay(10);
1103 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001104 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001105 }
1106
Fugang Duan106c3142014-09-13 05:00:51 +08001107 /* Whack a reset. We should wait for this.
1108 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1109 * instead of reset MAC itself.
1110 */
1111 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1112 writel(0, fep->hwp + FEC_ECNTRL);
1113 } else {
1114 writel(1, fep->hwp + FEC_ECNTRL);
1115 udelay(10);
1116 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001117 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1118 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001119
1120 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001121 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001122 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001123 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1124 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001125}
1126
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001129fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001131 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Russell King344756f2014-07-08 13:01:59 +01001133 fec_dump(ndev);
1134
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001135 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Russell King36cdc742014-07-08 13:01:44 +01001137 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001138}
1139
Russell King36cdc742014-07-08 13:01:44 +01001140static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001141{
1142 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001143 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001144 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001145
Russell King36cdc742014-07-08 13:01:44 +01001146 rtnl_lock();
1147 if (netif_device_present(ndev) || netif_running(ndev)) {
1148 napi_disable(&fep->napi);
1149 netif_tx_lock_bh(ndev);
1150 fec_restart(ndev);
1151 netif_wake_queue(ndev);
1152 netif_tx_unlock_bh(ndev);
1153 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001154 }
Russell King36cdc742014-07-08 13:01:44 +01001155 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001159fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1160 struct skb_shared_hwtstamps *hwtstamps)
1161{
1162 unsigned long flags;
1163 u64 ns;
1164
1165 spin_lock_irqsave(&fep->tmreg_lock, flags);
1166 ns = timecounter_cyc2time(&fep->tc, ts);
1167 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1168
1169 memset(hwtstamps, 0, sizeof(*hwtstamps));
1170 hwtstamps->hwtstamp = ns_to_ktime(ns);
1171}
1172
1173static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001174fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001177 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001178 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001180 struct fec_enet_priv_tx_q *txq;
1181 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001182 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001183 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001185 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001186
1187 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1188
1189 txq = fep->tx_queue[queue_id];
1190 /* get next bdp of dirty_tx */
1191 nq = netdev_get_tx_queue(ndev, queue_id);
1192 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Frank Lide5fb0a2013-03-03 17:34:25 +00001194 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001195 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001196
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001197 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001198
1199 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001200 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001201 break;
1202
Fugang Duan4d494cd2014-09-13 05:00:48 +08001203 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001204
Fugang Duan4d494cd2014-09-13 05:00:48 +08001205 skb = txq->tx_skbuff[index];
1206 txq->tx_skbuff[index] = NULL;
1207 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001208 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1209 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001210 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001211 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001212 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001213 continue;
1214 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001217 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 BD_ENET_TX_RL | BD_ENET_TX_UN |
1219 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001220 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001221 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001222 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001223 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001224 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001225 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001226 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001227 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001228 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001229 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001230 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001232 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001233 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Frank Liff43da82013-01-03 16:04:23 +00001236 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1237 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001238 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001239 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001240
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001241 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001242 skb_tstamp_tx(skb, &shhwtstamps);
1243 }
Frank Liff43da82013-01-03 16:04:23 +00001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* Deferred means some collisions occurred during transmit,
1246 * but we eventually sent the packet OK.
1247 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001248 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001250
Sascha Hauer22f6b862009-04-15 01:32:18 +00001251 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001253
Fugang Duan4d494cd2014-09-13 05:00:48 +08001254 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001255
Sascha Hauer22f6b862009-04-15 01:32:18 +00001256 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001257 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001258
Sascha Hauer22f6b862009-04-15 01:32:18 +00001259 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001261 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001262 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1263 if (entries_free >= txq->tx_wake_threshold)
1264 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Russell Kingccea2962014-07-08 13:01:38 +01001267
1268 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001269 if (bdp != txq->cur_tx &&
1270 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1271 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1272}
1273
1274static void
1275fec_enet_tx(struct net_device *ndev)
1276{
1277 struct fec_enet_private *fep = netdev_priv(ndev);
1278 u16 queue_id;
1279 /* First process class A queue, then Class B and Best Effort queue */
1280 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1281 clear_bit(queue_id, &fep->work_tx);
1282 fec_enet_tx_queue(ndev, queue_id);
1283 }
1284 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001287static int
1288fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1289{
1290 struct fec_enet_private *fep = netdev_priv(ndev);
1291 int off;
1292
1293 off = ((unsigned long)skb->data) & fep->rx_align;
1294 if (off)
1295 skb_reserve(skb, fep->rx_align + 1 - off);
1296
1297 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1298 FEC_ENET_RX_FRSIZE - fep->rx_align,
1299 DMA_FROM_DEVICE);
1300 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1301 if (net_ratelimit())
1302 netdev_err(ndev, "Rx DMA memory map failed\n");
1303 return -ENOMEM;
1304 }
1305
1306 return 0;
1307}
1308
1309static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1310 struct bufdesc *bdp, u32 length)
1311{
1312 struct fec_enet_private *fep = netdev_priv(ndev);
1313 struct sk_buff *new_skb;
1314
1315 if (length > fep->rx_copybreak)
1316 return false;
1317
1318 new_skb = netdev_alloc_skb(ndev, length);
1319 if (!new_skb)
1320 return false;
1321
1322 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1323 FEC_ENET_RX_FRSIZE - fep->rx_align,
1324 DMA_FROM_DEVICE);
1325 memcpy(new_skb->data, (*skb)->data, length);
1326 *skb = new_skb;
1327
1328 return true;
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/* During a receive, the cur_rx points to the current incoming buffer.
1332 * When we update through the ring, if the next incoming buffer has
1333 * not been given to the system, we just set the empty indicator,
1334 * effectively tossing the packet.
1335 */
Frank Lidc975382013-01-28 18:31:42 +00001336static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001337fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001339 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001340 const struct platform_device_id *id_entry =
1341 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001342 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001343 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001344 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001345 struct sk_buff *skb_new = NULL;
1346 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 ushort pkt_len;
1348 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001349 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001350 struct bufdesc_ex *ebdp = NULL;
1351 bool vlan_packet_rcvd = false;
1352 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001353 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001354 bool is_copybreak;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001355
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001356#ifdef CONFIG_M532x
1357 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001358#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001359 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1360 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* First, grab all of the stats for the incoming packet.
1363 * These get messed up if we get called due to a busy condition.
1364 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001365 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Sascha Hauer22f6b862009-04-15 01:32:18 +00001367 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Frank Lidc975382013-01-28 18:31:42 +00001369 if (pkt_received >= budget)
1370 break;
1371 pkt_received++;
1372
Sascha Hauer22f6b862009-04-15 01:32:18 +00001373 /* Since we have allocated space to hold a complete frame,
1374 * the last indicator should be set.
1375 */
1376 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001377 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Russell Kingdb3421c2014-07-08 13:01:49 +01001379
Sascha Hauer22f6b862009-04-15 01:32:18 +00001380 /* Check for errors. */
1381 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001383 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001384 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1385 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001386 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001387 }
1388 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001389 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001390 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001391 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001392 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001393 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001395
1396 /* Report late collisions as a frame error.
1397 * On this error, the BD is closed, but we don't know what we
1398 * have in the buffer. So, just drop this frame on the floor.
1399 */
1400 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001401 ndev->stats.rx_errors++;
1402 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001403 goto rx_processing_done;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Sascha Hauer22f6b862009-04-15 01:32:18 +00001406 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001407 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001408 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001409 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Fugang Duan4d494cd2014-09-13 05:00:48 +08001411 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001412 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001413
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001414 /* The packet length includes FCS, but we don't want to
1415 * include that when passing upstream as it messes up
1416 * bridging applications.
1417 */
1418 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4);
1419 if (!is_copybreak) {
1420 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1421 if (unlikely(!skb_new)) {
1422 ndev->stats.rx_dropped++;
1423 goto rx_processing_done;
1424 }
1425 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1426 FEC_ENET_RX_FRSIZE - fep->rx_align,
1427 DMA_FROM_DEVICE);
1428 }
1429
1430 prefetch(skb->data - NET_IP_ALIGN);
1431 skb_put(skb, pkt_len - 4);
1432 data = skb->data;
Shawn Guob5680e02011-01-05 21:13:13 +00001433 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1434 swap_buffer(data, pkt_len);
1435
Jim Baxtercdffcf12013-07-02 22:52:56 +01001436 /* Extract the enhanced buffer descriptor */
1437 ebdp = NULL;
1438 if (fep->bufdesc_ex)
1439 ebdp = (struct bufdesc_ex *)bdp;
1440
1441 /* If this is a VLAN packet remove the VLAN Tag */
1442 vlan_packet_rcvd = false;
1443 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001444 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001445 /* Push and remove the vlan tag */
1446 struct vlan_hdr *vlan_header =
1447 (struct vlan_hdr *) (data + ETH_HLEN);
1448 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001449
1450 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001451
1452 skb_copy_to_linear_data_offset(skb, VLAN_HLEN,
1453 data, (2 * ETH_ALEN));
1454 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001455 }
1456
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001457 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001459 /* Get receive timestamp from the skb */
1460 if (fep->hwts_rx_en && fep->bufdesc_ex)
1461 fec_enet_hwtstamp(fep, ebdp->ts,
1462 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001463
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001464 if (fep->bufdesc_ex &&
1465 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1466 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1467 /* don't check it */
1468 skb->ip_summed = CHECKSUM_UNNECESSARY;
1469 } else {
1470 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001471 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001472 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001473
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001474 /* Handle received VLAN packets */
1475 if (vlan_packet_rcvd)
1476 __vlan_hwaccel_put_tag(skb,
1477 htons(ETH_P_8021Q),
1478 vlan_tag);
1479
1480 napi_gro_receive(&fep->napi, skb);
1481
1482 if (is_copybreak) {
1483 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1484 FEC_ENET_RX_FRSIZE - fep->rx_align,
1485 DMA_FROM_DEVICE);
1486 } else {
1487 rxq->rx_skbuff[index] = skb_new;
1488 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1489 }
1490
Sascha Hauer22f6b862009-04-15 01:32:18 +00001491rx_processing_done:
1492 /* Clear the status flags for this buffer */
1493 status &= ~BD_ENET_RX_STATS;
1494
1495 /* Mark the buffer empty */
1496 status |= BD_ENET_RX_EMPTY;
1497 bdp->cbd_sc = status;
1498
Frank Liff43da82013-01-03 16:04:23 +00001499 if (fep->bufdesc_ex) {
1500 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1501
1502 ebdp->cbd_esc = BD_ENET_RX_INT;
1503 ebdp->cbd_prot = 0;
1504 ebdp->cbd_bdu = 0;
1505 }
Frank Li6605b732012-10-30 18:25:31 +00001506
Sascha Hauer22f6b862009-04-15 01:32:18 +00001507 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001508 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001509
Sascha Hauer22f6b862009-04-15 01:32:18 +00001510 /* Doing this here will keep the FEC running while we process
1511 * incoming frames. On a heavily loaded network, we should be
1512 * able to keep up at the expense of system resources.
1513 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001514 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001516 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001517 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Fugang Duan4d494cd2014-09-13 05:00:48 +08001520static int
1521fec_enet_rx(struct net_device *ndev, int budget)
1522{
1523 int pkt_received = 0;
1524 u16 queue_id;
1525 struct fec_enet_private *fep = netdev_priv(ndev);
1526
1527 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1528 clear_bit(queue_id, &fep->work_rx);
1529 pkt_received += fec_enet_rx_queue(ndev,
1530 budget - pkt_received, queue_id);
1531 }
1532 return pkt_received;
1533}
1534
1535static bool
1536fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1537{
1538 if (int_events == 0)
1539 return false;
1540
1541 if (int_events & FEC_ENET_RXF)
1542 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001543 if (int_events & FEC_ENET_RXF_1)
1544 fep->work_rx |= (1 << 0);
1545 if (int_events & FEC_ENET_RXF_2)
1546 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001547
1548 if (int_events & FEC_ENET_TXF)
1549 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001550 if (int_events & FEC_ENET_TXF_1)
1551 fep->work_tx |= (1 << 0);
1552 if (int_events & FEC_ENET_TXF_2)
1553 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001554
1555 return true;
1556}
1557
Uwe Kleine-König45993652011-01-19 20:47:04 +01001558static irqreturn_t
1559fec_enet_interrupt(int irq, void *dev_id)
1560{
1561 struct net_device *ndev = dev_id;
1562 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001563 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001564 uint int_events;
1565 irqreturn_t ret = IRQ_NONE;
1566
Russell King7a168072014-07-08 00:22:44 +01001567 int_events = readl(fep->hwp + FEC_IEVENT);
1568 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001569 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001570
Russell King7a168072014-07-08 00:22:44 +01001571 if (int_events & napi_mask) {
1572 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001573
Russell King7a168072014-07-08 00:22:44 +01001574 /* Disable the NAPI interrupts */
1575 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1576 napi_schedule(&fep->napi);
1577 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001578
Russell King7a168072014-07-08 00:22:44 +01001579 if (int_events & FEC_ENET_MII) {
1580 ret = IRQ_HANDLED;
1581 complete(&fep->mdio_done);
1582 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001583
Philipp Zabel81f35ff2014-10-22 16:34:35 +02001584 if (fep->ptp_clock)
1585 fec_ptp_check_pps_event(fep);
Luwei Zhou278d2402014-10-10 13:15:30 +08001586
Uwe Kleine-König45993652011-01-19 20:47:04 +01001587 return ret;
1588}
1589
Frank Lidc975382013-01-28 18:31:42 +00001590static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1591{
1592 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001593 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001594 int pkts;
1595
1596 /*
1597 * Clear any pending transmit or receive interrupts before
1598 * processing the rings to avoid racing with the hardware.
1599 */
1600 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1601
1602 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001603
Frank Lide5fb0a2013-03-03 17:34:25 +00001604 fec_enet_tx(ndev);
1605
Frank Lidc975382013-01-28 18:31:42 +00001606 if (pkts < budget) {
1607 napi_complete(napi);
1608 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1609 }
1610 return pkts;
1611}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001614static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001616 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001617 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001618 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Shawn Guo49da97d2011-01-05 21:13:11 +00001620 /*
1621 * try to get mac address in following order:
1622 *
1623 * 1) module parameter via kernel command line in form
1624 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1625 */
1626 iap = macaddr;
1627
1628 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001629 * 2) from device tree data
1630 */
1631 if (!is_valid_ether_addr(iap)) {
1632 struct device_node *np = fep->pdev->dev.of_node;
1633 if (np) {
1634 const char *mac = of_get_mac_address(np);
1635 if (mac)
1636 iap = (unsigned char *) mac;
1637 }
1638 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001639
1640 /*
1641 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001642 */
1643 if (!is_valid_ether_addr(iap)) {
1644#ifdef CONFIG_M5272
1645 if (FEC_FLASHMAC)
1646 iap = (unsigned char *)FEC_FLASHMAC;
1647#else
1648 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001649 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001650#endif
1651 }
1652
1653 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001654 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001655 */
1656 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001657 *((__be32 *) &tmpaddr[0]) =
1658 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1659 *((__be16 *) &tmpaddr[4]) =
1660 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 iap = &tmpaddr[0];
1662 }
1663
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001664 /*
1665 * 5) random mac address
1666 */
1667 if (!is_valid_ether_addr(iap)) {
1668 /* Report it and use a random ethernet address instead */
1669 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1670 eth_hw_addr_random(ndev);
1671 netdev_info(ndev, "Using random MAC address: %pM\n",
1672 ndev->dev_addr);
1673 return;
1674 }
1675
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001676 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Shawn Guo49da97d2011-01-05 21:13:11 +00001678 /* Adjust MAC if using macaddr */
1679 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001680 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683/* ------------------------------------------------------------------------- */
1684
Bryan Wue6b043d2010-03-31 02:10:44 +00001685/*
1686 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001688static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001690 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001691 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001692 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Bryan Wue6b043d2010-03-31 02:10:44 +00001694 /* Prevent a state halted on mii error */
1695 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1696 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001699
Russell King8ce56242014-07-08 12:40:07 +01001700 /*
1701 * If the netdev is down, or is going down, we're not interested
1702 * in link state events, so just mark our idea of the link as down
1703 * and ignore the event.
1704 */
1705 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1706 fep->link = 0;
1707 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001708 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001709 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001710 status_change = 1;
1711 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001712
Russell Kingef833372014-07-08 12:40:43 +01001713 if (fep->full_duplex != phy_dev->duplex) {
1714 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001715 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001716 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001717
1718 if (phy_dev->speed != fep->speed) {
1719 fep->speed = phy_dev->speed;
1720 status_change = 1;
1721 }
1722
1723 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001724 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001725 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001726 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001727 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001728 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001729 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001730 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001731 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001732 } else {
1733 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001734 napi_disable(&fep->napi);
1735 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001736 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001737 netif_tx_unlock_bh(ndev);
1738 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001739 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001740 status_change = 1;
1741 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001742 }
1743
Bryan Wue6b043d2010-03-31 02:10:44 +00001744 if (status_change)
1745 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Bryan Wue6b043d2010-03-31 02:10:44 +00001748static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Bryan Wue6b043d2010-03-31 02:10:44 +00001750 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001751 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001752
1753 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001754 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001755
1756 /* start a read op */
1757 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1758 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1759 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1760
1761 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001762 time_left = wait_for_completion_timeout(&fep->mdio_done,
1763 usecs_to_jiffies(FEC_MII_TIMEOUT));
1764 if (time_left == 0) {
1765 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001766 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001767 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001768 }
1769
1770 /* return value */
1771 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1772}
1773
1774static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1775 u16 value)
1776{
1777 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001778 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001779
1780 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001781 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001782
Shawn Guo862f0982011-01-05 21:13:09 +00001783 /* start a write op */
1784 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001785 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1786 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1787 fep->hwp + FEC_MII_DATA);
1788
1789 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001790 time_left = wait_for_completion_timeout(&fep->mdio_done,
1791 usecs_to_jiffies(FEC_MII_TIMEOUT));
1792 if (time_left == 0) {
1793 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001794 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001795 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001796 }
1797
1798 return 0;
1799}
1800
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001801static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1802{
1803 struct fec_enet_private *fep = netdev_priv(ndev);
1804 int ret;
1805
1806 if (enable) {
1807 ret = clk_prepare_enable(fep->clk_ahb);
1808 if (ret)
1809 return ret;
1810 ret = clk_prepare_enable(fep->clk_ipg);
1811 if (ret)
1812 goto failed_clk_ipg;
1813 if (fep->clk_enet_out) {
1814 ret = clk_prepare_enable(fep->clk_enet_out);
1815 if (ret)
1816 goto failed_clk_enet_out;
1817 }
1818 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001819 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001820 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001821 if (ret) {
1822 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001823 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001824 } else {
1825 fep->ptp_clk_on = true;
1826 }
1827 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001828 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001829 if (fep->clk_ref) {
1830 ret = clk_prepare_enable(fep->clk_ref);
1831 if (ret)
1832 goto failed_clk_ref;
1833 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001834 } else {
1835 clk_disable_unprepare(fep->clk_ahb);
1836 clk_disable_unprepare(fep->clk_ipg);
1837 if (fep->clk_enet_out)
1838 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001839 if (fep->clk_ptp) {
1840 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001841 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001842 fep->ptp_clk_on = false;
1843 mutex_unlock(&fep->ptp_clk_mutex);
1844 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001845 if (fep->clk_ref)
1846 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001847 }
1848
1849 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001850
1851failed_clk_ref:
1852 if (fep->clk_ref)
1853 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001854failed_clk_ptp:
1855 if (fep->clk_enet_out)
1856 clk_disable_unprepare(fep->clk_enet_out);
1857failed_clk_enet_out:
1858 clk_disable_unprepare(fep->clk_ipg);
1859failed_clk_ipg:
1860 clk_disable_unprepare(fep->clk_ahb);
1861
1862 return ret;
1863}
1864
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001865static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001866{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001867 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001868 const struct platform_device_id *id_entry =
1869 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001870 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001871 char mdio_bus_id[MII_BUS_ID_SIZE];
1872 char phy_name[MII_BUS_ID_SIZE + 3];
1873 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001874 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001875
Bryan Wu418bd0d2010-05-28 03:40:39 -07001876 fep->phy_dev = NULL;
1877
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001878 if (fep->phy_node) {
1879 phy_dev = of_phy_connect(ndev, fep->phy_node,
1880 &fec_enet_adjust_link, 0,
1881 fep->phy_interface);
1882 } else {
1883 /* check for attached phy */
1884 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1885 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1886 continue;
1887 if (fep->mii_bus->phy_map[phy_id] == NULL)
1888 continue;
1889 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1890 continue;
1891 if (dev_id--)
1892 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001893 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001894 break;
1895 }
1896
1897 if (phy_id >= PHY_MAX_ADDR) {
1898 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001899 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001900 phy_id = 0;
1901 }
1902
1903 snprintf(phy_name, sizeof(phy_name),
1904 PHY_ID_FMT, mdio_bus_id, phy_id);
1905 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1906 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001907 }
1908
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001909 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001910 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001911 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001912 }
1913
1914 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001915 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001916 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001917 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001918#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001919 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001920#endif
Frank Libaa70a52013-01-16 16:55:58 +00001921 }
Shawn Guo230dec62011-09-23 02:12:48 +00001922 else
1923 phy_dev->supported &= PHY_BASIC_FEATURES;
1924
Bryan Wue6b043d2010-03-31 02:10:44 +00001925 phy_dev->advertising = phy_dev->supported;
1926
1927 fep->phy_dev = phy_dev;
1928 fep->link = 0;
1929 fep->full_duplex = 0;
1930
Joe Perches31b77202013-04-13 19:03:17 +00001931 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1932 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1933 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001934
Bryan Wue6b043d2010-03-31 02:10:44 +00001935 return 0;
1936}
1937
1938static int fec_enet_mii_init(struct platform_device *pdev)
1939{
Shawn Guob5680e02011-01-05 21:13:13 +00001940 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001941 struct net_device *ndev = platform_get_drvdata(pdev);
1942 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001943 const struct platform_device_id *id_entry =
1944 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001945 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001946 int err = -ENXIO, i;
1947
Shawn Guob5680e02011-01-05 21:13:13 +00001948 /*
1949 * The dual fec interfaces are not equivalent with enet-mac.
1950 * Here are the differences:
1951 *
1952 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1953 * - fec0 acts as the 1588 time master while fec1 is slave
1954 * - external phys can only be configured by fec0
1955 *
1956 * That is to say fec1 can not work independently. It only works
1957 * when fec0 is working. The reason behind this design is that the
1958 * second interface is added primarily for Switch mode.
1959 *
1960 * Because of the last point above, both phys are attached on fec0
1961 * mdio interface in board design, and need to be configured by
1962 * fec0 mii_bus.
1963 */
Shawn Guo43af9402011-12-05 05:01:15 +00001964 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001965 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001966 if (mii_cnt && fec0_mii_bus) {
1967 fep->mii_bus = fec0_mii_bus;
1968 mii_cnt++;
1969 return 0;
1970 }
1971 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001972 }
1973
Bryan Wue6b043d2010-03-31 02:10:44 +00001974 fep->mii_timeout = 0;
1975
1976 /*
1977 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001978 *
1979 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1980 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1981 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1982 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001983 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001984 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001985 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1986 fep->phy_speed--;
1987 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001988 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1989
1990 fep->mii_bus = mdiobus_alloc();
1991 if (fep->mii_bus == NULL) {
1992 err = -ENOMEM;
1993 goto err_out;
1994 }
1995
1996 fep->mii_bus->name = "fec_enet_mii_bus";
1997 fep->mii_bus->read = fec_enet_mdio_read;
1998 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001999 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2000 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002001 fep->mii_bus->priv = fep;
2002 fep->mii_bus->parent = &pdev->dev;
2003
2004 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2005 if (!fep->mii_bus->irq) {
2006 err = -ENOMEM;
2007 goto err_out_free_mdiobus;
2008 }
2009
2010 for (i = 0; i < PHY_MAX_ADDR; i++)
2011 fep->mii_bus->irq[i] = PHY_POLL;
2012
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002013 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2014 if (node) {
2015 err = of_mdiobus_register(fep->mii_bus, node);
2016 of_node_put(node);
2017 } else {
2018 err = mdiobus_register(fep->mii_bus);
2019 }
2020
2021 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002022 goto err_out_free_mdio_irq;
2023
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002024 mii_cnt++;
2025
Shawn Guob5680e02011-01-05 21:13:13 +00002026 /* save fec0 mii_bus */
2027 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2028 fec0_mii_bus = fep->mii_bus;
2029
Bryan Wue6b043d2010-03-31 02:10:44 +00002030 return 0;
2031
Bryan Wue6b043d2010-03-31 02:10:44 +00002032err_out_free_mdio_irq:
2033 kfree(fep->mii_bus->irq);
2034err_out_free_mdiobus:
2035 mdiobus_free(fep->mii_bus);
2036err_out:
2037 return err;
2038}
2039
2040static void fec_enet_mii_remove(struct fec_enet_private *fep)
2041{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002042 if (--mii_cnt == 0) {
2043 mdiobus_unregister(fep->mii_bus);
2044 kfree(fep->mii_bus->irq);
2045 mdiobus_free(fep->mii_bus);
2046 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002047}
2048
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002049static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002050 struct ethtool_cmd *cmd)
2051{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002052 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002053 struct phy_device *phydev = fep->phy_dev;
2054
2055 if (!phydev)
2056 return -ENODEV;
2057
2058 return phy_ethtool_gset(phydev, cmd);
2059}
2060
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002061static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002062 struct ethtool_cmd *cmd)
2063{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002064 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002065 struct phy_device *phydev = fep->phy_dev;
2066
2067 if (!phydev)
2068 return -ENODEV;
2069
2070 return phy_ethtool_sset(phydev, cmd);
2071}
2072
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002073static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002074 struct ethtool_drvinfo *info)
2075{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002076 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Jiri Pirko7826d432013-01-06 00:44:26 +00002078 strlcpy(info->driver, fep->pdev->dev.driver->name,
2079 sizeof(info->driver));
2080 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2081 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
Bryan Wue6b043d2010-03-31 02:10:44 +00002083
Frank Li5ebae4892013-01-06 16:25:07 +00002084static int fec_enet_get_ts_info(struct net_device *ndev,
2085 struct ethtool_ts_info *info)
2086{
2087 struct fec_enet_private *fep = netdev_priv(ndev);
2088
2089 if (fep->bufdesc_ex) {
2090
2091 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2092 SOF_TIMESTAMPING_RX_SOFTWARE |
2093 SOF_TIMESTAMPING_SOFTWARE |
2094 SOF_TIMESTAMPING_TX_HARDWARE |
2095 SOF_TIMESTAMPING_RX_HARDWARE |
2096 SOF_TIMESTAMPING_RAW_HARDWARE;
2097 if (fep->ptp_clock)
2098 info->phc_index = ptp_clock_index(fep->ptp_clock);
2099 else
2100 info->phc_index = -1;
2101
2102 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2103 (1 << HWTSTAMP_TX_ON);
2104
2105 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2106 (1 << HWTSTAMP_FILTER_ALL);
2107 return 0;
2108 } else {
2109 return ethtool_op_get_ts_info(ndev, info);
2110 }
2111}
2112
Guenter Roeckd1391932013-06-18 10:04:59 -07002113#if !defined(CONFIG_M5272)
2114
Frank Libaa70a52013-01-16 16:55:58 +00002115static void fec_enet_get_pauseparam(struct net_device *ndev,
2116 struct ethtool_pauseparam *pause)
2117{
2118 struct fec_enet_private *fep = netdev_priv(ndev);
2119
2120 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2121 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2122 pause->rx_pause = pause->tx_pause;
2123}
2124
2125static int fec_enet_set_pauseparam(struct net_device *ndev,
2126 struct ethtool_pauseparam *pause)
2127{
2128 struct fec_enet_private *fep = netdev_priv(ndev);
2129
Russell King0b146ca2014-07-08 00:22:59 +01002130 if (!fep->phy_dev)
2131 return -ENODEV;
2132
Frank Libaa70a52013-01-16 16:55:58 +00002133 if (pause->tx_pause != pause->rx_pause) {
2134 netdev_info(ndev,
2135 "hardware only support enable/disable both tx and rx");
2136 return -EINVAL;
2137 }
2138
2139 fep->pause_flag = 0;
2140
2141 /* tx pause must be same as rx pause */
2142 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2143 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2144
2145 if (pause->rx_pause || pause->autoneg) {
2146 fep->phy_dev->supported |= ADVERTISED_Pause;
2147 fep->phy_dev->advertising |= ADVERTISED_Pause;
2148 } else {
2149 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2150 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2151 }
2152
2153 if (pause->autoneg) {
2154 if (netif_running(ndev))
2155 fec_stop(ndev);
2156 phy_start_aneg(fep->phy_dev);
2157 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002158 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002159 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002160 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002161 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002162 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002163 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002164 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002165 }
Frank Libaa70a52013-01-16 16:55:58 +00002166
2167 return 0;
2168}
2169
Chris Healy38ae92d2013-06-25 23:18:52 -07002170static const struct fec_stat {
2171 char name[ETH_GSTRING_LEN];
2172 u16 offset;
2173} fec_stats[] = {
2174 /* RMON TX */
2175 { "tx_dropped", RMON_T_DROP },
2176 { "tx_packets", RMON_T_PACKETS },
2177 { "tx_broadcast", RMON_T_BC_PKT },
2178 { "tx_multicast", RMON_T_MC_PKT },
2179 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2180 { "tx_undersize", RMON_T_UNDERSIZE },
2181 { "tx_oversize", RMON_T_OVERSIZE },
2182 { "tx_fragment", RMON_T_FRAG },
2183 { "tx_jabber", RMON_T_JAB },
2184 { "tx_collision", RMON_T_COL },
2185 { "tx_64byte", RMON_T_P64 },
2186 { "tx_65to127byte", RMON_T_P65TO127 },
2187 { "tx_128to255byte", RMON_T_P128TO255 },
2188 { "tx_256to511byte", RMON_T_P256TO511 },
2189 { "tx_512to1023byte", RMON_T_P512TO1023 },
2190 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2191 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2192 { "tx_octets", RMON_T_OCTETS },
2193
2194 /* IEEE TX */
2195 { "IEEE_tx_drop", IEEE_T_DROP },
2196 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2197 { "IEEE_tx_1col", IEEE_T_1COL },
2198 { "IEEE_tx_mcol", IEEE_T_MCOL },
2199 { "IEEE_tx_def", IEEE_T_DEF },
2200 { "IEEE_tx_lcol", IEEE_T_LCOL },
2201 { "IEEE_tx_excol", IEEE_T_EXCOL },
2202 { "IEEE_tx_macerr", IEEE_T_MACERR },
2203 { "IEEE_tx_cserr", IEEE_T_CSERR },
2204 { "IEEE_tx_sqe", IEEE_T_SQE },
2205 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2206 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2207
2208 /* RMON RX */
2209 { "rx_packets", RMON_R_PACKETS },
2210 { "rx_broadcast", RMON_R_BC_PKT },
2211 { "rx_multicast", RMON_R_MC_PKT },
2212 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2213 { "rx_undersize", RMON_R_UNDERSIZE },
2214 { "rx_oversize", RMON_R_OVERSIZE },
2215 { "rx_fragment", RMON_R_FRAG },
2216 { "rx_jabber", RMON_R_JAB },
2217 { "rx_64byte", RMON_R_P64 },
2218 { "rx_65to127byte", RMON_R_P65TO127 },
2219 { "rx_128to255byte", RMON_R_P128TO255 },
2220 { "rx_256to511byte", RMON_R_P256TO511 },
2221 { "rx_512to1023byte", RMON_R_P512TO1023 },
2222 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2223 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2224 { "rx_octets", RMON_R_OCTETS },
2225
2226 /* IEEE RX */
2227 { "IEEE_rx_drop", IEEE_R_DROP },
2228 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2229 { "IEEE_rx_crc", IEEE_R_CRC },
2230 { "IEEE_rx_align", IEEE_R_ALIGN },
2231 { "IEEE_rx_macerr", IEEE_R_MACERR },
2232 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2233 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2234};
2235
2236static void fec_enet_get_ethtool_stats(struct net_device *dev,
2237 struct ethtool_stats *stats, u64 *data)
2238{
2239 struct fec_enet_private *fep = netdev_priv(dev);
2240 int i;
2241
2242 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2243 data[i] = readl(fep->hwp + fec_stats[i].offset);
2244}
2245
2246static void fec_enet_get_strings(struct net_device *netdev,
2247 u32 stringset, u8 *data)
2248{
2249 int i;
2250 switch (stringset) {
2251 case ETH_SS_STATS:
2252 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2253 memcpy(data + i * ETH_GSTRING_LEN,
2254 fec_stats[i].name, ETH_GSTRING_LEN);
2255 break;
2256 }
2257}
2258
2259static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2260{
2261 switch (sset) {
2262 case ETH_SS_STATS:
2263 return ARRAY_SIZE(fec_stats);
2264 default:
2265 return -EOPNOTSUPP;
2266 }
2267}
Guenter Roeckd1391932013-06-18 10:04:59 -07002268#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002269
Chris Healy32bc9b42013-06-17 07:25:06 -07002270static int fec_enet_nway_reset(struct net_device *dev)
2271{
2272 struct fec_enet_private *fep = netdev_priv(dev);
2273 struct phy_device *phydev = fep->phy_dev;
2274
2275 if (!phydev)
2276 return -ENODEV;
2277
2278 return genphy_restart_aneg(phydev);
2279}
2280
Fugang Duand851b472014-09-17 05:18:52 +08002281/* ITR clock source is enet system clock (clk_ahb).
2282 * TCTT unit is cycle_ns * 64 cycle
2283 * So, the ICTT value = X us / (cycle_ns * 64)
2284 */
2285static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2286{
2287 struct fec_enet_private *fep = netdev_priv(ndev);
2288
2289 return us * (fep->itr_clk_rate / 64000) / 1000;
2290}
2291
2292/* Set threshold for interrupt coalescing */
2293static void fec_enet_itr_coal_set(struct net_device *ndev)
2294{
2295 struct fec_enet_private *fep = netdev_priv(ndev);
2296 const struct platform_device_id *id_entry =
2297 platform_get_device_id(fep->pdev);
2298 int rx_itr, tx_itr;
2299
2300 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2301 return;
2302
2303 /* Must be greater than zero to avoid unpredictable behavior */
2304 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2305 !fep->tx_time_itr || !fep->tx_pkts_itr)
2306 return;
2307
2308 /* Select enet system clock as Interrupt Coalescing
2309 * timer Clock Source
2310 */
2311 rx_itr = FEC_ITR_CLK_SEL;
2312 tx_itr = FEC_ITR_CLK_SEL;
2313
2314 /* set ICFT and ICTT */
2315 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2316 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2317 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2318 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2319
2320 rx_itr |= FEC_ITR_EN;
2321 tx_itr |= FEC_ITR_EN;
2322
2323 writel(tx_itr, fep->hwp + FEC_TXIC0);
2324 writel(rx_itr, fep->hwp + FEC_RXIC0);
2325 writel(tx_itr, fep->hwp + FEC_TXIC1);
2326 writel(rx_itr, fep->hwp + FEC_RXIC1);
2327 writel(tx_itr, fep->hwp + FEC_TXIC2);
2328 writel(rx_itr, fep->hwp + FEC_RXIC2);
2329}
2330
2331static int
2332fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2333{
2334 struct fec_enet_private *fep = netdev_priv(ndev);
2335 const struct platform_device_id *id_entry =
2336 platform_get_device_id(fep->pdev);
2337
2338 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2339 return -EOPNOTSUPP;
2340
2341 ec->rx_coalesce_usecs = fep->rx_time_itr;
2342 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2343
2344 ec->tx_coalesce_usecs = fep->tx_time_itr;
2345 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2346
2347 return 0;
2348}
2349
2350static int
2351fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2352{
2353 struct fec_enet_private *fep = netdev_priv(ndev);
2354 const struct platform_device_id *id_entry =
2355 platform_get_device_id(fep->pdev);
2356
2357 unsigned int cycle;
2358
2359 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2360 return -EOPNOTSUPP;
2361
2362 if (ec->rx_max_coalesced_frames > 255) {
2363 pr_err("Rx coalesced frames exceed hardware limiation");
2364 return -EINVAL;
2365 }
2366
2367 if (ec->tx_max_coalesced_frames > 255) {
2368 pr_err("Tx coalesced frame exceed hardware limiation");
2369 return -EINVAL;
2370 }
2371
2372 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2373 if (cycle > 0xFFFF) {
2374 pr_err("Rx coalesed usec exceeed hardware limiation");
2375 return -EINVAL;
2376 }
2377
2378 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2379 if (cycle > 0xFFFF) {
2380 pr_err("Rx coalesed usec exceeed hardware limiation");
2381 return -EINVAL;
2382 }
2383
2384 fep->rx_time_itr = ec->rx_coalesce_usecs;
2385 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2386
2387 fep->tx_time_itr = ec->tx_coalesce_usecs;
2388 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2389
2390 fec_enet_itr_coal_set(ndev);
2391
2392 return 0;
2393}
2394
2395static void fec_enet_itr_coal_init(struct net_device *ndev)
2396{
2397 struct ethtool_coalesce ec;
2398
2399 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2400 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2401
2402 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2403 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2404
2405 fec_enet_set_coalesce(ndev, &ec);
2406}
2407
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002408static int fec_enet_get_tunable(struct net_device *netdev,
2409 const struct ethtool_tunable *tuna,
2410 void *data)
2411{
2412 struct fec_enet_private *fep = netdev_priv(netdev);
2413 int ret = 0;
2414
2415 switch (tuna->id) {
2416 case ETHTOOL_RX_COPYBREAK:
2417 *(u32 *)data = fep->rx_copybreak;
2418 break;
2419 default:
2420 ret = -EINVAL;
2421 break;
2422 }
2423
2424 return ret;
2425}
2426
2427static int fec_enet_set_tunable(struct net_device *netdev,
2428 const struct ethtool_tunable *tuna,
2429 const void *data)
2430{
2431 struct fec_enet_private *fep = netdev_priv(netdev);
2432 int ret = 0;
2433
2434 switch (tuna->id) {
2435 case ETHTOOL_RX_COPYBREAK:
2436 fep->rx_copybreak = *(u32 *)data;
2437 break;
2438 default:
2439 ret = -EINVAL;
2440 break;
2441 }
2442
2443 return ret;
2444}
2445
stephen hemminger9b07be42012-01-04 12:59:49 +00002446static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002447 .get_settings = fec_enet_get_settings,
2448 .set_settings = fec_enet_set_settings,
2449 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002450 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002451 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002452 .get_coalesce = fec_enet_get_coalesce,
2453 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002454#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002455 .get_pauseparam = fec_enet_get_pauseparam,
2456 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002457 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002458 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002459 .get_sset_count = fec_enet_get_sset_count,
2460#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002461 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002462 .get_tunable = fec_enet_get_tunable,
2463 .set_tunable = fec_enet_set_tunable,
Bryan Wue6b043d2010-03-31 02:10:44 +00002464};
2465
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002466static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002467{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002468 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002469 struct phy_device *phydev = fep->phy_dev;
2470
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002471 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002472 return -EINVAL;
2473
2474 if (!phydev)
2475 return -ENODEV;
2476
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002477 if (fep->bufdesc_ex) {
2478 if (cmd == SIOCSHWTSTAMP)
2479 return fec_ptp_set(ndev, rq);
2480 if (cmd == SIOCGHWTSTAMP)
2481 return fec_ptp_get(ndev, rq);
2482 }
Frank Liff43da82013-01-03 16:04:23 +00002483
Richard Cochran28b04112010-07-17 08:48:55 +00002484 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002487static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002488{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002489 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002490 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002491 struct sk_buff *skb;
2492 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002493 struct fec_enet_priv_tx_q *txq;
2494 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002495 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002496
Frank Li59d0f7462014-09-13 05:00:50 +08002497 for (q = 0; q < fep->num_rx_queues; q++) {
2498 rxq = fep->rx_queue[q];
2499 bdp = rxq->rx_bd_base;
2500 for (i = 0; i < rxq->rx_ring_size; i++) {
2501 skb = rxq->rx_skbuff[i];
2502 rxq->rx_skbuff[i] = NULL;
2503 if (skb) {
2504 dma_unmap_single(&fep->pdev->dev,
2505 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002506 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002507 DMA_FROM_DEVICE);
2508 dev_kfree_skb(skb);
2509 }
2510 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002511 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002512 }
2513
Frank Li59d0f7462014-09-13 05:00:50 +08002514 for (q = 0; q < fep->num_tx_queues; q++) {
2515 txq = fep->tx_queue[q];
2516 bdp = txq->tx_bd_base;
2517 for (i = 0; i < txq->tx_ring_size; i++) {
2518 kfree(txq->tx_bounce[i]);
2519 txq->tx_bounce[i] = NULL;
2520 skb = txq->tx_skbuff[i];
2521 txq->tx_skbuff[i] = NULL;
2522 dev_kfree_skb(skb);
2523 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002524 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002525}
2526
Frank Li59d0f7462014-09-13 05:00:50 +08002527static void fec_enet_free_queue(struct net_device *ndev)
2528{
2529 struct fec_enet_private *fep = netdev_priv(ndev);
2530 int i;
2531 struct fec_enet_priv_tx_q *txq;
2532
2533 for (i = 0; i < fep->num_tx_queues; i++)
2534 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2535 txq = fep->tx_queue[i];
2536 dma_free_coherent(NULL,
2537 txq->tx_ring_size * TSO_HEADER_SIZE,
2538 txq->tso_hdrs,
2539 txq->tso_hdrs_dma);
2540 }
2541
2542 for (i = 0; i < fep->num_rx_queues; i++)
2543 if (fep->rx_queue[i])
2544 kfree(fep->rx_queue[i]);
2545
2546 for (i = 0; i < fep->num_tx_queues; i++)
2547 if (fep->tx_queue[i])
2548 kfree(fep->tx_queue[i]);
2549}
2550
2551static int fec_enet_alloc_queue(struct net_device *ndev)
2552{
2553 struct fec_enet_private *fep = netdev_priv(ndev);
2554 int i;
2555 int ret = 0;
2556 struct fec_enet_priv_tx_q *txq;
2557
2558 for (i = 0; i < fep->num_tx_queues; i++) {
2559 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2560 if (!txq) {
2561 ret = -ENOMEM;
2562 goto alloc_failed;
2563 }
2564
2565 fep->tx_queue[i] = txq;
2566 txq->tx_ring_size = TX_RING_SIZE;
2567 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2568
2569 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2570 txq->tx_wake_threshold =
2571 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2572
2573 txq->tso_hdrs = dma_alloc_coherent(NULL,
2574 txq->tx_ring_size * TSO_HEADER_SIZE,
2575 &txq->tso_hdrs_dma,
2576 GFP_KERNEL);
2577 if (!txq->tso_hdrs) {
2578 ret = -ENOMEM;
2579 goto alloc_failed;
2580 }
2581 }
2582
2583 for (i = 0; i < fep->num_rx_queues; i++) {
2584 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2585 GFP_KERNEL);
2586 if (!fep->rx_queue[i]) {
2587 ret = -ENOMEM;
2588 goto alloc_failed;
2589 }
2590
2591 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2592 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2593 }
2594 return ret;
2595
2596alloc_failed:
2597 fec_enet_free_queue(ndev);
2598 return ret;
2599}
2600
2601static int
2602fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002603{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002604 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002605 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002606 struct sk_buff *skb;
2607 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002608 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002609
Frank Li59d0f7462014-09-13 05:00:50 +08002610 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002611 bdp = rxq->rx_bd_base;
2612 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002613 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002614 if (!skb)
2615 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002616
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002617 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002618 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002619 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002620 }
Russell King730ee362014-07-08 00:23:14 +01002621
Fugang Duan4d494cd2014-09-13 05:00:48 +08002622 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002623 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002624
2625 if (fep->bufdesc_ex) {
2626 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2627 ebdp->cbd_esc = BD_ENET_RX_INT;
2628 }
2629
Frank Li59d0f7462014-09-13 05:00:50 +08002630 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002631 }
2632
2633 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002634 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002635 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002636 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002637
Frank Li59d0f7462014-09-13 05:00:50 +08002638 err_alloc:
2639 fec_enet_free_buffers(ndev);
2640 return -ENOMEM;
2641}
2642
2643static int
2644fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2645{
2646 struct fec_enet_private *fep = netdev_priv(ndev);
2647 unsigned int i;
2648 struct bufdesc *bdp;
2649 struct fec_enet_priv_tx_q *txq;
2650
2651 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002652 bdp = txq->tx_bd_base;
2653 for (i = 0; i < txq->tx_ring_size; i++) {
2654 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2655 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002656 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002657
2658 bdp->cbd_sc = 0;
2659 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002660
Frank Liff43da82013-01-03 16:04:23 +00002661 if (fep->bufdesc_ex) {
2662 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002663 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002664 }
2665
Frank Li59d0f7462014-09-13 05:00:50 +08002666 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002667 }
2668
2669 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002670 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002671 bdp->cbd_sc |= BD_SC_WRAP;
2672
2673 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002674
2675 err_alloc:
2676 fec_enet_free_buffers(ndev);
2677 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002678}
2679
Frank Li59d0f7462014-09-13 05:00:50 +08002680static int fec_enet_alloc_buffers(struct net_device *ndev)
2681{
2682 struct fec_enet_private *fep = netdev_priv(ndev);
2683 unsigned int i;
2684
2685 for (i = 0; i < fep->num_rx_queues; i++)
2686 if (fec_enet_alloc_rxq_buffers(ndev, i))
2687 return -ENOMEM;
2688
2689 for (i = 0; i < fep->num_tx_queues; i++)
2690 if (fec_enet_alloc_txq_buffers(ndev, i))
2691 return -ENOMEM;
2692 return 0;
2693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002696fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002698 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002699 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002701 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002702 ret = fec_enet_clk_enable(ndev, true);
2703 if (ret)
2704 return ret;
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* I should reset the ring buffers here, but I don't yet know
2707 * a simple way to do that.
2708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002710 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002711 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002712 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002713
Bryan Wu418bd0d2010-05-28 03:40:39 -07002714 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002715 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002716 if (ret)
2717 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002718
Russell Kingef833372014-07-08 12:40:43 +01002719 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002720 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002721 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002722 netif_tx_start_all_queues(ndev);
2723
Sascha Hauer22f6b862009-04-15 01:32:18 +00002724 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002725
2726err_enet_mii_probe:
2727 fec_enet_free_buffers(ndev);
2728err_enet_alloc:
2729 fec_enet_clk_enable(ndev, false);
2730 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2731 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
2734static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002735fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002737 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Russell Kingd76cfae2014-07-08 00:23:04 +01002739 phy_stop(fep->phy_dev);
2740
Russell King31a6de32014-07-08 12:40:23 +01002741 if (netif_device_present(ndev)) {
2742 napi_disable(&fep->napi);
2743 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002744 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Russell King635cf172014-07-08 00:22:54 +01002747 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002748 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002749
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002750 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002751 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002752 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return 0;
2755}
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757/* Set or clear the multicast filter for this adaptor.
2758 * Skeleton taken from sunlance driver.
2759 * The CPM Ethernet implementation allows Multicast as well as individual
2760 * MAC address filtering. Some of the drivers check to make sure it is
2761 * a group multicast address, and discard those that are not. I guess I
2762 * will do the same for now, but just remove the test if you want
2763 * individual filtering as well (do the upper net layers want or support
2764 * this kind of feature?).
2765 */
2766
2767#define HASH_BITS 6 /* #bits in hash */
2768#define CRC32_POLY 0xEDB88320
2769
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002770static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002772 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002773 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002774 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 unsigned char hash;
2776
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002777 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002778 tmp = readl(fep->hwp + FEC_R_CNTRL);
2779 tmp |= 0x8;
2780 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002781 return;
2782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Sascha Hauer4e831832009-04-15 01:32:19 +00002784 tmp = readl(fep->hwp + FEC_R_CNTRL);
2785 tmp &= ~0x8;
2786 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002787
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002788 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002789 /* Catch all multicast addresses, so set the
2790 * filter to all 1's
2791 */
2792 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2793 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Sascha Hauer4e831832009-04-15 01:32:19 +00002795 return;
2796 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002797
Sascha Hauer4e831832009-04-15 01:32:19 +00002798 /* Clear filter and add the addresses in hash register
2799 */
2800 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2801 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002803 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002804 /* calculate crc32 value of mac address */
2805 crc = 0xffffffff;
2806
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002807 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002808 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002809 for (bit = 0; bit < 8; bit++, data >>= 1) {
2810 crc = (crc >> 1) ^
2811 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002814
2815 /* only upper 6 bits (HASH_BITS) are used
2816 * which point to specific bit in he hash registers
2817 */
2818 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2819
2820 if (hash > 31) {
2821 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2822 tmp |= 1 << (hash - 32);
2823 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2824 } else {
2825 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2826 tmp |= 1 << hash;
2827 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830}
2831
Sascha Hauer22f6b862009-04-15 01:32:18 +00002832/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002833static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002834fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002836 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002837 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Lucas Stach44934fa2014-03-30 21:32:08 +02002839 if (addr) {
2840 if (!is_valid_ether_addr(addr->sa_data))
2841 return -EADDRNOTAVAIL;
2842 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2843 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002844
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002845 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2846 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002847 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002848 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002849 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002853#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002854/**
2855 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002856 * @dev: The FEC network adapter
2857 *
2858 * Polled functionality used by netconsole and others in non interrupt mode
2859 *
2860 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002861static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002862{
2863 int i;
2864 struct fec_enet_private *fep = netdev_priv(dev);
2865
2866 for (i = 0; i < FEC_IRQ_NUM; i++) {
2867 if (fep->irq[i] > 0) {
2868 disable_irq(fep->irq[i]);
2869 fec_enet_interrupt(fep->irq[i], dev);
2870 enable_irq(fep->irq[i]);
2871 }
2872 }
2873}
2874#endif
2875
Russell King8506fa12014-07-08 12:40:33 +01002876#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
Nimrod Andy5bc26722014-10-13 10:53:48 +08002877static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00002878 netdev_features_t features)
2879{
2880 struct fec_enet_private *fep = netdev_priv(netdev);
2881 netdev_features_t changed = features ^ netdev->features;
2882
2883 netdev->features = features;
2884
2885 /* Receive checksum has been changed */
2886 if (changed & NETIF_F_RXCSUM) {
2887 if (features & NETIF_F_RXCSUM)
2888 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2889 else
2890 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002891 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08002892}
Jim Baxter4c09eed2013-04-19 08:10:49 +00002893
Nimrod Andy5bc26722014-10-13 10:53:48 +08002894static int fec_set_features(struct net_device *netdev,
2895 netdev_features_t features)
2896{
2897 struct fec_enet_private *fep = netdev_priv(netdev);
2898 netdev_features_t changed = features ^ netdev->features;
2899
Russell King8506fa12014-07-08 12:40:33 +01002900 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08002901 napi_disable(&fep->napi);
2902 netif_tx_lock_bh(netdev);
2903 fec_stop(netdev);
2904 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01002905 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002906 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002907 netif_tx_unlock_bh(netdev);
2908 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08002909 } else {
2910 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002911 }
2912
2913 return 0;
2914}
2915
Sascha Hauer009fda82009-04-15 01:32:23 +00002916static const struct net_device_ops fec_netdev_ops = {
2917 .ndo_open = fec_enet_open,
2918 .ndo_stop = fec_enet_close,
2919 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002920 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002921 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002922 .ndo_validate_addr = eth_validate_addr,
2923 .ndo_tx_timeout = fec_timeout,
2924 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002925 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002926#ifdef CONFIG_NET_POLL_CONTROLLER
2927 .ndo_poll_controller = fec_poll_controller,
2928#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002929 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002930};
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 /*
2933 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002934 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002936static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002938 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002939 const struct platform_device_id *id_entry =
2940 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002941 struct fec_enet_priv_tx_q *txq;
2942 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002943 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002944 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002945 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002946 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002947
Fugang Duan41ef84c2014-09-13 05:00:54 +08002948#if defined(CONFIG_ARM)
2949 fep->rx_align = 0xf;
2950 fep->tx_align = 0xf;
2951#else
2952 fep->rx_align = 0x3;
2953 fep->tx_align = 0x3;
2954#endif
2955
Frank Li59d0f7462014-09-13 05:00:50 +08002956 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002957
Nimrod Andy55d02182014-06-12 08:16:21 +08002958 if (fep->bufdesc_ex)
2959 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2960 else
2961 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002962 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002963 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002965 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002966 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002967 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002968 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002969 return -ENOMEM;
2970 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002971
Fugang Duan4d494cd2014-09-13 05:00:48 +08002972 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Shawn Guo49da97d2011-01-05 21:13:11 +00002974 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002975 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002976 /* make sure MAC we just acquired is programmed into the hw */
2977 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002979 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08002980 for (i = 0; i < fep->num_rx_queues; i++) {
2981 rxq = fep->rx_queue[i];
2982 rxq->index = i;
2983 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2984 rxq->bd_dma = bd_dma;
2985 if (fep->bufdesc_ex) {
2986 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2987 cbd_base = (struct bufdesc *)
2988 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2989 } else {
2990 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2991 cbd_base += rxq->rx_ring_size;
2992 }
2993 }
2994
2995 for (i = 0; i < fep->num_tx_queues; i++) {
2996 txq = fep->tx_queue[i];
2997 txq->index = i;
2998 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2999 txq->bd_dma = bd_dma;
3000 if (fep->bufdesc_ex) {
3001 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3002 cbd_base = (struct bufdesc *)
3003 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3004 } else {
3005 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3006 cbd_base += txq->tx_ring_size;
3007 }
3008 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Sascha Hauer22f6b862009-04-15 01:32:18 +00003011 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003012 ndev->watchdog_timeo = TX_TIMEOUT;
3013 ndev->netdev_ops = &fec_netdev_ops;
3014 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003015
Frank Lidc975382013-01-28 18:31:42 +00003016 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003017 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003018
Nimrod Andy09d1e542014-06-12 08:16:20 +08003019 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003020 /* enable hw VLAN support */
3021 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003022
Shawn Guo48496252013-05-08 21:08:22 +00003023 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003024 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3025
Shawn Guo48496252013-05-08 21:08:22 +00003026 /* enable hw accelerator */
3027 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003028 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003029 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3030 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003031
Fugang Duan41ef84c2014-09-13 05:00:54 +08003032 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3033 fep->tx_align = 0;
3034 fep->rx_align = 0x3f;
3035 }
3036
Nimrod Andy09d1e542014-06-12 08:16:20 +08003037 ndev->hw_features = ndev->features;
3038
Russell Kingef833372014-07-08 12:40:43 +01003039 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return 0;
3042}
3043
Shawn Guoca2cc332011-06-25 02:04:35 +08003044#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003045static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003046{
3047 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003048 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003049 struct device_node *np = pdev->dev.of_node;
3050
3051 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003052 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003053
Shawn Guoa3caad02012-06-27 03:45:24 +00003054 of_property_read_u32(np, "phy-reset-duration", &msec);
3055 /* A sane reset duration should not be longer than 1s */
3056 if (msec > 1000)
3057 msec = 1;
3058
Shawn Guoca2cc332011-06-25 02:04:35 +08003059 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003060 if (!gpio_is_valid(phy_reset))
3061 return;
3062
Shawn Guo119fc002012-06-27 03:45:22 +00003063 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3064 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003065 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003066 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
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 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003070 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003071}
3072#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003073static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003074{
3075 /*
3076 * In case of platform probe, the reset has been done
3077 * by machine code.
3078 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003079}
3080#endif /* CONFIG_OF */
3081
Fugang Duan9fc095f2014-09-13 05:00:49 +08003082static void
3083fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3084{
3085 struct device_node *np = pdev->dev.of_node;
3086 int err;
3087
3088 *num_tx = *num_rx = 1;
3089
3090 if (!np || !of_device_is_available(np))
3091 return;
3092
3093 /* parse the num of tx and rx queues */
3094 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003095 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003096 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003097
3098 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3099 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003100 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003101
3102 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003103 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3104 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003105 *num_tx = 1;
3106 return;
3107 }
3108
3109 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003110 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3111 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003112 *num_rx = 1;
3113 return;
3114 }
3115
3116}
3117
Bill Pemberton33897cc2012-12-03 09:23:58 -05003118static int
Sascha Haueread73182009-01-28 23:03:11 +00003119fec_probe(struct platform_device *pdev)
3120{
3121 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003122 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003123 struct net_device *ndev;
3124 int i, irq, ret = 0;
3125 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003126 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003127 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003128 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003129 int num_tx_qs;
3130 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003131
3132 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3133 if (of_id)
3134 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003135
Fugang Duan9fc095f2014-09-13 05:00:49 +08003136 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3137
Sascha Haueread73182009-01-28 23:03:11 +00003138 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003139 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3140 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003141 if (!ndev)
3142 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003143
3144 SET_NETDEV_DEV(ndev, &pdev->dev);
3145
3146 /* setup board info structure */
3147 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003148
Fugang Duan9fc095f2014-09-13 05:00:49 +08003149 fep->num_rx_queues = num_rx_qs;
3150 fep->num_tx_queues = num_tx_qs;
3151
Guenter Roeckd1391932013-06-18 10:04:59 -07003152#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003153 /* default enable pause frame auto negotiation */
3154 if (pdev->id_entry &&
3155 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3156 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003157#endif
Frank Libaa70a52013-01-16 16:55:58 +00003158
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003159 /* Select default pin state */
3160 pinctrl_pm_select_default_state(&pdev->dev);
3161
Fabio Estevam399db752013-07-21 13:25:03 -03003162 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303163 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3164 if (IS_ERR(fep->hwp)) {
3165 ret = PTR_ERR(fep->hwp);
3166 goto failed_ioremap;
3167 }
3168
Bryan Wue6b043d2010-03-31 02:10:44 +00003169 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003170 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003171
Frank Liff43da82013-01-03 16:04:23 +00003172 fep->bufdesc_ex = 0;
3173
Sascha Haueread73182009-01-28 23:03:11 +00003174 platform_set_drvdata(pdev, ndev);
3175
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003176 phy_node = of_parse_phandle(np, "phy-handle", 0);
3177 if (!phy_node && of_phy_is_fixed_link(np)) {
3178 ret = of_phy_register_fixed_link(np);
3179 if (ret < 0) {
3180 dev_err(&pdev->dev,
3181 "broken fixed-link specification\n");
3182 goto failed_phy;
3183 }
3184 phy_node = of_node_get(np);
3185 }
3186 fep->phy_node = phy_node;
3187
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003188 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003189 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003190 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003191 if (pdata)
3192 fep->phy_interface = pdata->phy;
3193 else
3194 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3195 } else {
3196 fep->phy_interface = ret;
3197 }
3198
Fabio Estevame2f8d552013-02-22 06:40:45 +00003199 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3200 if (IS_ERR(fep->clk_ipg)) {
3201 ret = PTR_ERR(fep->clk_ipg);
3202 goto failed_clk;
3203 }
3204
3205 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3206 if (IS_ERR(fep->clk_ahb)) {
3207 ret = PTR_ERR(fep->clk_ahb);
3208 goto failed_clk;
3209 }
3210
Fugang Duand851b472014-09-17 05:18:52 +08003211 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3212
Linus Torvalds38f56f32013-05-07 11:06:17 -07003213 /* enet_out is optional, depends on board */
3214 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3215 if (IS_ERR(fep->clk_enet_out))
3216 fep->clk_enet_out = NULL;
3217
Nimrod Andy91c0d982014-08-21 17:09:38 +08003218 fep->ptp_clk_on = false;
3219 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003220
3221 /* clk_ref is optional, depends on board */
3222 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3223 if (IS_ERR(fep->clk_ref))
3224 fep->clk_ref = NULL;
3225
Fabio Estevame2f8d552013-02-22 06:40:45 +00003226 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003227 fep->bufdesc_ex =
3228 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003229 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003230 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003231 fep->bufdesc_ex = 0;
3232 }
3233
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003234 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003235 if (ret)
3236 goto failed_clk;
3237
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003238 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3239 if (!IS_ERR(fep->reg_phy)) {
3240 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003241 if (ret) {
3242 dev_err(&pdev->dev,
3243 "Failed to enable phy regulator: %d\n", ret);
3244 goto failed_regulator;
3245 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003246 } else {
3247 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003248 }
3249
3250 fec_reset_phy(pdev);
3251
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003252 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003253 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003254
3255 ret = fec_enet_init(ndev);
3256 if (ret)
3257 goto failed_init;
3258
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003259 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003260 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003261 if (irq < 0) {
3262 if (i)
3263 break;
3264 ret = irq;
3265 goto failed_irq;
3266 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003267 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003268 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003269 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003270 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003271 }
3272
Fugang Duanb4d39b52014-09-13 05:00:55 +08003273 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003274 ret = fec_enet_mii_init(pdev);
3275 if (ret)
3276 goto failed_mii_init;
3277
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003278 /* Carrier starts down, phylib will bring it up */
3279 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003280 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003281 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003282
Sascha Haueread73182009-01-28 23:03:11 +00003283 ret = register_netdev(ndev);
3284 if (ret)
3285 goto failed_register;
3286
Fabio Estevameb1d0642013-04-13 07:25:36 +00003287 if (fep->bufdesc_ex && fep->ptp_clock)
3288 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3289
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003290 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003291 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003292 return 0;
3293
3294failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003295 fec_enet_mii_remove(fep);
3296failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003297failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003298failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003299 if (fep->reg_phy)
3300 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003301failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003302 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003303failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003304failed_phy:
3305 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003306failed_ioremap:
3307 free_netdev(ndev);
3308
3309 return ret;
3310}
3311
Bill Pemberton33897cc2012-12-03 09:23:58 -05003312static int
Sascha Haueread73182009-01-28 23:03:11 +00003313fec_drv_remove(struct platform_device *pdev)
3314{
3315 struct net_device *ndev = platform_get_drvdata(pdev);
3316 struct fec_enet_private *fep = netdev_priv(ndev);
3317
Nimrod Andy91c0d982014-08-21 17:09:38 +08003318 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003319 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003320 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003321 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003322 if (fep->reg_phy)
3323 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003324 if (fep->ptp_clock)
3325 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003326 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003327 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003328 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003329
Sascha Haueread73182009-01-28 23:03:11 +00003330 return 0;
3331}
3332
Fabio Estevamdd66d382014-07-24 18:24:01 -03003333static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003334{
Eric Benard87cad5c2010-06-18 04:19:54 +00003335 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003336 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003337
Russell Kingda1774e2014-07-08 12:39:57 +01003338 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003339 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003340 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003341 napi_disable(&fep->napi);
3342 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003343 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003344 netif_tx_unlock_bh(ndev);
3345 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003346 }
Russell Kingda1774e2014-07-08 12:39:57 +01003347 rtnl_unlock();
3348
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003349 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003350 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003351
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003352 if (fep->reg_phy)
3353 regulator_disable(fep->reg_phy);
3354
Sascha Haueread73182009-01-28 23:03:11 +00003355 return 0;
3356}
3357
Fabio Estevamdd66d382014-07-24 18:24:01 -03003358static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003359{
Eric Benard87cad5c2010-06-18 04:19:54 +00003360 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003361 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003362 int ret;
3363
3364 if (fep->reg_phy) {
3365 ret = regulator_enable(fep->reg_phy);
3366 if (ret)
3367 return ret;
3368 }
Sascha Haueread73182009-01-28 23:03:11 +00003369
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003370 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003371 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003372 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003373 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003374
Russell Kingda1774e2014-07-08 12:39:57 +01003375 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003376 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003377 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003378 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003379 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003380 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003381 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003382 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003383 }
Russell Kingda1774e2014-07-08 12:39:57 +01003384 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003385
Sascha Haueread73182009-01-28 23:03:11 +00003386 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003387
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003388failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003389 if (fep->reg_phy)
3390 regulator_disable(fep->reg_phy);
3391 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003392}
3393
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003394static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003395
Sascha Haueread73182009-01-28 23:03:11 +00003396static struct platform_driver fec_driver = {
3397 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003398 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003399 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003400 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003401 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003402 },
Shawn Guob5680e02011-01-05 21:13:13 +00003403 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003404 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003405 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003406};
3407
Fabio Estevamaaca2372012-01-23 16:44:50 +00003408module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003410MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411MODULE_LICENSE("GPL");