blob: 81b96cf875740f6faa418885718dcc78c2113570 [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 Li59d0f742014-09-13 05:00:50 +0800805 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000806
Frank Li59d0f742014-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 Li59d0f742014-09-13 05:00:50 +0800812 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000813
Frank Li59d0f742014-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 Li59d0f742014-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 Li59d0f742014-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 Li59d0f742014-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 Li59d0f742014-09-13 05:00:50 +0800953 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100954
Frank Li59d0f742014-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
Luwei Zhou278d2402014-10-10 13:15:30 +08001584 fec_ptp_check_pps_event(fep);
1585
Uwe Kleine-König45993652011-01-19 20:47:04 +01001586 return ret;
1587}
1588
Frank Lidc975382013-01-28 18:31:42 +00001589static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1590{
1591 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001592 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001593 int pkts;
1594
1595 /*
1596 * Clear any pending transmit or receive interrupts before
1597 * processing the rings to avoid racing with the hardware.
1598 */
1599 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1600
1601 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001602
Frank Lide5fb0a2013-03-03 17:34:25 +00001603 fec_enet_tx(ndev);
1604
Frank Lidc975382013-01-28 18:31:42 +00001605 if (pkts < budget) {
1606 napi_complete(napi);
1607 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1608 }
1609 return pkts;
1610}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001613static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001615 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001616 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001617 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Shawn Guo49da97d2011-01-05 21:13:11 +00001619 /*
1620 * try to get mac address in following order:
1621 *
1622 * 1) module parameter via kernel command line in form
1623 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1624 */
1625 iap = macaddr;
1626
1627 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001628 * 2) from device tree data
1629 */
1630 if (!is_valid_ether_addr(iap)) {
1631 struct device_node *np = fep->pdev->dev.of_node;
1632 if (np) {
1633 const char *mac = of_get_mac_address(np);
1634 if (mac)
1635 iap = (unsigned char *) mac;
1636 }
1637 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001638
1639 /*
1640 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001641 */
1642 if (!is_valid_ether_addr(iap)) {
1643#ifdef CONFIG_M5272
1644 if (FEC_FLASHMAC)
1645 iap = (unsigned char *)FEC_FLASHMAC;
1646#else
1647 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001648 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001649#endif
1650 }
1651
1652 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001653 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001654 */
1655 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001656 *((__be32 *) &tmpaddr[0]) =
1657 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1658 *((__be16 *) &tmpaddr[4]) =
1659 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 iap = &tmpaddr[0];
1661 }
1662
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001663 /*
1664 * 5) random mac address
1665 */
1666 if (!is_valid_ether_addr(iap)) {
1667 /* Report it and use a random ethernet address instead */
1668 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1669 eth_hw_addr_random(ndev);
1670 netdev_info(ndev, "Using random MAC address: %pM\n",
1671 ndev->dev_addr);
1672 return;
1673 }
1674
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001675 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Shawn Guo49da97d2011-01-05 21:13:11 +00001677 /* Adjust MAC if using macaddr */
1678 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001679 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/* ------------------------------------------------------------------------- */
1683
Bryan Wue6b043d2010-03-31 02:10:44 +00001684/*
1685 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001687static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001689 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001690 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001691 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Bryan Wue6b043d2010-03-31 02:10:44 +00001693 /* Prevent a state halted on mii error */
1694 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1695 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001698
Russell King8ce56242014-07-08 12:40:07 +01001699 /*
1700 * If the netdev is down, or is going down, we're not interested
1701 * in link state events, so just mark our idea of the link as down
1702 * and ignore the event.
1703 */
1704 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1705 fep->link = 0;
1706 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001707 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001708 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001709 status_change = 1;
1710 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001711
Russell Kingef833372014-07-08 12:40:43 +01001712 if (fep->full_duplex != phy_dev->duplex) {
1713 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001714 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001715 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001716
1717 if (phy_dev->speed != fep->speed) {
1718 fep->speed = phy_dev->speed;
1719 status_change = 1;
1720 }
1721
1722 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001723 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001724 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001725 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001726 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001727 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001728 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001729 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001730 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001731 } else {
1732 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001733 napi_disable(&fep->napi);
1734 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001735 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001736 netif_tx_unlock_bh(ndev);
1737 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001738 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001739 status_change = 1;
1740 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001741 }
1742
Bryan Wue6b043d2010-03-31 02:10:44 +00001743 if (status_change)
1744 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Bryan Wue6b043d2010-03-31 02:10:44 +00001747static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Bryan Wue6b043d2010-03-31 02:10:44 +00001749 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001750 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001751
1752 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001753 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001754
1755 /* start a read op */
1756 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1757 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1758 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1759
1760 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001761 time_left = wait_for_completion_timeout(&fep->mdio_done,
1762 usecs_to_jiffies(FEC_MII_TIMEOUT));
1763 if (time_left == 0) {
1764 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001765 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001766 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001767 }
1768
1769 /* return value */
1770 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1771}
1772
1773static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1774 u16 value)
1775{
1776 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001777 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001778
1779 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001780 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001781
Shawn Guo862f0982011-01-05 21:13:09 +00001782 /* start a write op */
1783 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001784 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1785 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1786 fep->hwp + FEC_MII_DATA);
1787
1788 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001789 time_left = wait_for_completion_timeout(&fep->mdio_done,
1790 usecs_to_jiffies(FEC_MII_TIMEOUT));
1791 if (time_left == 0) {
1792 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001793 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001794 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001795 }
1796
1797 return 0;
1798}
1799
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001800static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1801{
1802 struct fec_enet_private *fep = netdev_priv(ndev);
1803 int ret;
1804
1805 if (enable) {
1806 ret = clk_prepare_enable(fep->clk_ahb);
1807 if (ret)
1808 return ret;
1809 ret = clk_prepare_enable(fep->clk_ipg);
1810 if (ret)
1811 goto failed_clk_ipg;
1812 if (fep->clk_enet_out) {
1813 ret = clk_prepare_enable(fep->clk_enet_out);
1814 if (ret)
1815 goto failed_clk_enet_out;
1816 }
1817 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001818 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001819 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001820 if (ret) {
1821 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001822 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001823 } else {
1824 fep->ptp_clk_on = true;
1825 }
1826 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001827 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001828 if (fep->clk_ref) {
1829 ret = clk_prepare_enable(fep->clk_ref);
1830 if (ret)
1831 goto failed_clk_ref;
1832 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001833 } else {
1834 clk_disable_unprepare(fep->clk_ahb);
1835 clk_disable_unprepare(fep->clk_ipg);
1836 if (fep->clk_enet_out)
1837 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001838 if (fep->clk_ptp) {
1839 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001840 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001841 fep->ptp_clk_on = false;
1842 mutex_unlock(&fep->ptp_clk_mutex);
1843 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001844 if (fep->clk_ref)
1845 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001846 }
1847
1848 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001849
1850failed_clk_ref:
1851 if (fep->clk_ref)
1852 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001853failed_clk_ptp:
1854 if (fep->clk_enet_out)
1855 clk_disable_unprepare(fep->clk_enet_out);
1856failed_clk_enet_out:
1857 clk_disable_unprepare(fep->clk_ipg);
1858failed_clk_ipg:
1859 clk_disable_unprepare(fep->clk_ahb);
1860
1861 return ret;
1862}
1863
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001864static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001865{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001866 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001867 const struct platform_device_id *id_entry =
1868 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001869 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001870 char mdio_bus_id[MII_BUS_ID_SIZE];
1871 char phy_name[MII_BUS_ID_SIZE + 3];
1872 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001873 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001874
Bryan Wu418bd0d2010-05-28 03:40:39 -07001875 fep->phy_dev = NULL;
1876
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001877 if (fep->phy_node) {
1878 phy_dev = of_phy_connect(ndev, fep->phy_node,
1879 &fec_enet_adjust_link, 0,
1880 fep->phy_interface);
1881 } else {
1882 /* check for attached phy */
1883 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1884 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1885 continue;
1886 if (fep->mii_bus->phy_map[phy_id] == NULL)
1887 continue;
1888 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1889 continue;
1890 if (dev_id--)
1891 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001892 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001893 break;
1894 }
1895
1896 if (phy_id >= PHY_MAX_ADDR) {
1897 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001898 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001899 phy_id = 0;
1900 }
1901
1902 snprintf(phy_name, sizeof(phy_name),
1903 PHY_ID_FMT, mdio_bus_id, phy_id);
1904 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1905 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001906 }
1907
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001908 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001909 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001910 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001911 }
1912
1913 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001914 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001915 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001916 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001917#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001918 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001919#endif
Frank Libaa70a52013-01-16 16:55:58 +00001920 }
Shawn Guo230dec62011-09-23 02:12:48 +00001921 else
1922 phy_dev->supported &= PHY_BASIC_FEATURES;
1923
Bryan Wue6b043d2010-03-31 02:10:44 +00001924 phy_dev->advertising = phy_dev->supported;
1925
1926 fep->phy_dev = phy_dev;
1927 fep->link = 0;
1928 fep->full_duplex = 0;
1929
Joe Perches31b77202013-04-13 19:03:17 +00001930 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1931 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1932 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001933
Bryan Wue6b043d2010-03-31 02:10:44 +00001934 return 0;
1935}
1936
1937static int fec_enet_mii_init(struct platform_device *pdev)
1938{
Shawn Guob5680e02011-01-05 21:13:13 +00001939 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001940 struct net_device *ndev = platform_get_drvdata(pdev);
1941 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001942 const struct platform_device_id *id_entry =
1943 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001944 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001945 int err = -ENXIO, i;
1946
Shawn Guob5680e02011-01-05 21:13:13 +00001947 /*
1948 * The dual fec interfaces are not equivalent with enet-mac.
1949 * Here are the differences:
1950 *
1951 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1952 * - fec0 acts as the 1588 time master while fec1 is slave
1953 * - external phys can only be configured by fec0
1954 *
1955 * That is to say fec1 can not work independently. It only works
1956 * when fec0 is working. The reason behind this design is that the
1957 * second interface is added primarily for Switch mode.
1958 *
1959 * Because of the last point above, both phys are attached on fec0
1960 * mdio interface in board design, and need to be configured by
1961 * fec0 mii_bus.
1962 */
Shawn Guo43af9402011-12-05 05:01:15 +00001963 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001964 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001965 if (mii_cnt && fec0_mii_bus) {
1966 fep->mii_bus = fec0_mii_bus;
1967 mii_cnt++;
1968 return 0;
1969 }
1970 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001971 }
1972
Bryan Wue6b043d2010-03-31 02:10:44 +00001973 fep->mii_timeout = 0;
1974
1975 /*
1976 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001977 *
1978 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1979 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1980 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1981 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001982 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001983 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001984 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1985 fep->phy_speed--;
1986 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001987 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1988
1989 fep->mii_bus = mdiobus_alloc();
1990 if (fep->mii_bus == NULL) {
1991 err = -ENOMEM;
1992 goto err_out;
1993 }
1994
1995 fep->mii_bus->name = "fec_enet_mii_bus";
1996 fep->mii_bus->read = fec_enet_mdio_read;
1997 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001998 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1999 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002000 fep->mii_bus->priv = fep;
2001 fep->mii_bus->parent = &pdev->dev;
2002
2003 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2004 if (!fep->mii_bus->irq) {
2005 err = -ENOMEM;
2006 goto err_out_free_mdiobus;
2007 }
2008
2009 for (i = 0; i < PHY_MAX_ADDR; i++)
2010 fep->mii_bus->irq[i] = PHY_POLL;
2011
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002012 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2013 if (node) {
2014 err = of_mdiobus_register(fep->mii_bus, node);
2015 of_node_put(node);
2016 } else {
2017 err = mdiobus_register(fep->mii_bus);
2018 }
2019
2020 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002021 goto err_out_free_mdio_irq;
2022
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002023 mii_cnt++;
2024
Shawn Guob5680e02011-01-05 21:13:13 +00002025 /* save fec0 mii_bus */
2026 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2027 fec0_mii_bus = fep->mii_bus;
2028
Bryan Wue6b043d2010-03-31 02:10:44 +00002029 return 0;
2030
Bryan Wue6b043d2010-03-31 02:10:44 +00002031err_out_free_mdio_irq:
2032 kfree(fep->mii_bus->irq);
2033err_out_free_mdiobus:
2034 mdiobus_free(fep->mii_bus);
2035err_out:
2036 return err;
2037}
2038
2039static void fec_enet_mii_remove(struct fec_enet_private *fep)
2040{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002041 if (--mii_cnt == 0) {
2042 mdiobus_unregister(fep->mii_bus);
2043 kfree(fep->mii_bus->irq);
2044 mdiobus_free(fep->mii_bus);
2045 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002046}
2047
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002048static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002049 struct ethtool_cmd *cmd)
2050{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002051 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002052 struct phy_device *phydev = fep->phy_dev;
2053
2054 if (!phydev)
2055 return -ENODEV;
2056
2057 return phy_ethtool_gset(phydev, cmd);
2058}
2059
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002060static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002061 struct ethtool_cmd *cmd)
2062{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002063 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002064 struct phy_device *phydev = fep->phy_dev;
2065
2066 if (!phydev)
2067 return -ENODEV;
2068
2069 return phy_ethtool_sset(phydev, cmd);
2070}
2071
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002072static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002073 struct ethtool_drvinfo *info)
2074{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002075 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Jiri Pirko7826d432013-01-06 00:44:26 +00002077 strlcpy(info->driver, fep->pdev->dev.driver->name,
2078 sizeof(info->driver));
2079 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2080 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
Bryan Wue6b043d2010-03-31 02:10:44 +00002082
Frank Li5ebae4892013-01-06 16:25:07 +00002083static int fec_enet_get_ts_info(struct net_device *ndev,
2084 struct ethtool_ts_info *info)
2085{
2086 struct fec_enet_private *fep = netdev_priv(ndev);
2087
2088 if (fep->bufdesc_ex) {
2089
2090 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2091 SOF_TIMESTAMPING_RX_SOFTWARE |
2092 SOF_TIMESTAMPING_SOFTWARE |
2093 SOF_TIMESTAMPING_TX_HARDWARE |
2094 SOF_TIMESTAMPING_RX_HARDWARE |
2095 SOF_TIMESTAMPING_RAW_HARDWARE;
2096 if (fep->ptp_clock)
2097 info->phc_index = ptp_clock_index(fep->ptp_clock);
2098 else
2099 info->phc_index = -1;
2100
2101 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2102 (1 << HWTSTAMP_TX_ON);
2103
2104 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2105 (1 << HWTSTAMP_FILTER_ALL);
2106 return 0;
2107 } else {
2108 return ethtool_op_get_ts_info(ndev, info);
2109 }
2110}
2111
Guenter Roeckd1391932013-06-18 10:04:59 -07002112#if !defined(CONFIG_M5272)
2113
Frank Libaa70a52013-01-16 16:55:58 +00002114static void fec_enet_get_pauseparam(struct net_device *ndev,
2115 struct ethtool_pauseparam *pause)
2116{
2117 struct fec_enet_private *fep = netdev_priv(ndev);
2118
2119 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2120 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2121 pause->rx_pause = pause->tx_pause;
2122}
2123
2124static int fec_enet_set_pauseparam(struct net_device *ndev,
2125 struct ethtool_pauseparam *pause)
2126{
2127 struct fec_enet_private *fep = netdev_priv(ndev);
2128
Russell King0b146ca2014-07-08 00:22:59 +01002129 if (!fep->phy_dev)
2130 return -ENODEV;
2131
Frank Libaa70a52013-01-16 16:55:58 +00002132 if (pause->tx_pause != pause->rx_pause) {
2133 netdev_info(ndev,
2134 "hardware only support enable/disable both tx and rx");
2135 return -EINVAL;
2136 }
2137
2138 fep->pause_flag = 0;
2139
2140 /* tx pause must be same as rx pause */
2141 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2142 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2143
2144 if (pause->rx_pause || pause->autoneg) {
2145 fep->phy_dev->supported |= ADVERTISED_Pause;
2146 fep->phy_dev->advertising |= ADVERTISED_Pause;
2147 } else {
2148 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2149 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2150 }
2151
2152 if (pause->autoneg) {
2153 if (netif_running(ndev))
2154 fec_stop(ndev);
2155 phy_start_aneg(fep->phy_dev);
2156 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002157 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002158 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002159 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002160 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002161 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002162 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002163 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002164 }
Frank Libaa70a52013-01-16 16:55:58 +00002165
2166 return 0;
2167}
2168
Chris Healy38ae92d2013-06-25 23:18:52 -07002169static const struct fec_stat {
2170 char name[ETH_GSTRING_LEN];
2171 u16 offset;
2172} fec_stats[] = {
2173 /* RMON TX */
2174 { "tx_dropped", RMON_T_DROP },
2175 { "tx_packets", RMON_T_PACKETS },
2176 { "tx_broadcast", RMON_T_BC_PKT },
2177 { "tx_multicast", RMON_T_MC_PKT },
2178 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2179 { "tx_undersize", RMON_T_UNDERSIZE },
2180 { "tx_oversize", RMON_T_OVERSIZE },
2181 { "tx_fragment", RMON_T_FRAG },
2182 { "tx_jabber", RMON_T_JAB },
2183 { "tx_collision", RMON_T_COL },
2184 { "tx_64byte", RMON_T_P64 },
2185 { "tx_65to127byte", RMON_T_P65TO127 },
2186 { "tx_128to255byte", RMON_T_P128TO255 },
2187 { "tx_256to511byte", RMON_T_P256TO511 },
2188 { "tx_512to1023byte", RMON_T_P512TO1023 },
2189 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2190 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2191 { "tx_octets", RMON_T_OCTETS },
2192
2193 /* IEEE TX */
2194 { "IEEE_tx_drop", IEEE_T_DROP },
2195 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2196 { "IEEE_tx_1col", IEEE_T_1COL },
2197 { "IEEE_tx_mcol", IEEE_T_MCOL },
2198 { "IEEE_tx_def", IEEE_T_DEF },
2199 { "IEEE_tx_lcol", IEEE_T_LCOL },
2200 { "IEEE_tx_excol", IEEE_T_EXCOL },
2201 { "IEEE_tx_macerr", IEEE_T_MACERR },
2202 { "IEEE_tx_cserr", IEEE_T_CSERR },
2203 { "IEEE_tx_sqe", IEEE_T_SQE },
2204 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2205 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2206
2207 /* RMON RX */
2208 { "rx_packets", RMON_R_PACKETS },
2209 { "rx_broadcast", RMON_R_BC_PKT },
2210 { "rx_multicast", RMON_R_MC_PKT },
2211 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2212 { "rx_undersize", RMON_R_UNDERSIZE },
2213 { "rx_oversize", RMON_R_OVERSIZE },
2214 { "rx_fragment", RMON_R_FRAG },
2215 { "rx_jabber", RMON_R_JAB },
2216 { "rx_64byte", RMON_R_P64 },
2217 { "rx_65to127byte", RMON_R_P65TO127 },
2218 { "rx_128to255byte", RMON_R_P128TO255 },
2219 { "rx_256to511byte", RMON_R_P256TO511 },
2220 { "rx_512to1023byte", RMON_R_P512TO1023 },
2221 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2222 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2223 { "rx_octets", RMON_R_OCTETS },
2224
2225 /* IEEE RX */
2226 { "IEEE_rx_drop", IEEE_R_DROP },
2227 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2228 { "IEEE_rx_crc", IEEE_R_CRC },
2229 { "IEEE_rx_align", IEEE_R_ALIGN },
2230 { "IEEE_rx_macerr", IEEE_R_MACERR },
2231 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2232 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2233};
2234
2235static void fec_enet_get_ethtool_stats(struct net_device *dev,
2236 struct ethtool_stats *stats, u64 *data)
2237{
2238 struct fec_enet_private *fep = netdev_priv(dev);
2239 int i;
2240
2241 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2242 data[i] = readl(fep->hwp + fec_stats[i].offset);
2243}
2244
2245static void fec_enet_get_strings(struct net_device *netdev,
2246 u32 stringset, u8 *data)
2247{
2248 int i;
2249 switch (stringset) {
2250 case ETH_SS_STATS:
2251 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2252 memcpy(data + i * ETH_GSTRING_LEN,
2253 fec_stats[i].name, ETH_GSTRING_LEN);
2254 break;
2255 }
2256}
2257
2258static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2259{
2260 switch (sset) {
2261 case ETH_SS_STATS:
2262 return ARRAY_SIZE(fec_stats);
2263 default:
2264 return -EOPNOTSUPP;
2265 }
2266}
Guenter Roeckd1391932013-06-18 10:04:59 -07002267#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002268
Chris Healy32bc9b42013-06-17 07:25:06 -07002269static int fec_enet_nway_reset(struct net_device *dev)
2270{
2271 struct fec_enet_private *fep = netdev_priv(dev);
2272 struct phy_device *phydev = fep->phy_dev;
2273
2274 if (!phydev)
2275 return -ENODEV;
2276
2277 return genphy_restart_aneg(phydev);
2278}
2279
Fugang Duand851b472014-09-17 05:18:52 +08002280/* ITR clock source is enet system clock (clk_ahb).
2281 * TCTT unit is cycle_ns * 64 cycle
2282 * So, the ICTT value = X us / (cycle_ns * 64)
2283 */
2284static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2285{
2286 struct fec_enet_private *fep = netdev_priv(ndev);
2287
2288 return us * (fep->itr_clk_rate / 64000) / 1000;
2289}
2290
2291/* Set threshold for interrupt coalescing */
2292static void fec_enet_itr_coal_set(struct net_device *ndev)
2293{
2294 struct fec_enet_private *fep = netdev_priv(ndev);
2295 const struct platform_device_id *id_entry =
2296 platform_get_device_id(fep->pdev);
2297 int rx_itr, tx_itr;
2298
2299 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2300 return;
2301
2302 /* Must be greater than zero to avoid unpredictable behavior */
2303 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2304 !fep->tx_time_itr || !fep->tx_pkts_itr)
2305 return;
2306
2307 /* Select enet system clock as Interrupt Coalescing
2308 * timer Clock Source
2309 */
2310 rx_itr = FEC_ITR_CLK_SEL;
2311 tx_itr = FEC_ITR_CLK_SEL;
2312
2313 /* set ICFT and ICTT */
2314 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2315 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2316 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2317 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2318
2319 rx_itr |= FEC_ITR_EN;
2320 tx_itr |= FEC_ITR_EN;
2321
2322 writel(tx_itr, fep->hwp + FEC_TXIC0);
2323 writel(rx_itr, fep->hwp + FEC_RXIC0);
2324 writel(tx_itr, fep->hwp + FEC_TXIC1);
2325 writel(rx_itr, fep->hwp + FEC_RXIC1);
2326 writel(tx_itr, fep->hwp + FEC_TXIC2);
2327 writel(rx_itr, fep->hwp + FEC_RXIC2);
2328}
2329
2330static int
2331fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2332{
2333 struct fec_enet_private *fep = netdev_priv(ndev);
2334 const struct platform_device_id *id_entry =
2335 platform_get_device_id(fep->pdev);
2336
2337 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2338 return -EOPNOTSUPP;
2339
2340 ec->rx_coalesce_usecs = fep->rx_time_itr;
2341 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2342
2343 ec->tx_coalesce_usecs = fep->tx_time_itr;
2344 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2345
2346 return 0;
2347}
2348
2349static int
2350fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2351{
2352 struct fec_enet_private *fep = netdev_priv(ndev);
2353 const struct platform_device_id *id_entry =
2354 platform_get_device_id(fep->pdev);
2355
2356 unsigned int cycle;
2357
2358 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2359 return -EOPNOTSUPP;
2360
2361 if (ec->rx_max_coalesced_frames > 255) {
2362 pr_err("Rx coalesced frames exceed hardware limiation");
2363 return -EINVAL;
2364 }
2365
2366 if (ec->tx_max_coalesced_frames > 255) {
2367 pr_err("Tx coalesced frame exceed hardware limiation");
2368 return -EINVAL;
2369 }
2370
2371 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2372 if (cycle > 0xFFFF) {
2373 pr_err("Rx coalesed usec exceeed hardware limiation");
2374 return -EINVAL;
2375 }
2376
2377 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2378 if (cycle > 0xFFFF) {
2379 pr_err("Rx coalesed usec exceeed hardware limiation");
2380 return -EINVAL;
2381 }
2382
2383 fep->rx_time_itr = ec->rx_coalesce_usecs;
2384 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2385
2386 fep->tx_time_itr = ec->tx_coalesce_usecs;
2387 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2388
2389 fec_enet_itr_coal_set(ndev);
2390
2391 return 0;
2392}
2393
2394static void fec_enet_itr_coal_init(struct net_device *ndev)
2395{
2396 struct ethtool_coalesce ec;
2397
2398 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2399 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2400
2401 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2402 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2403
2404 fec_enet_set_coalesce(ndev, &ec);
2405}
2406
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002407static int fec_enet_get_tunable(struct net_device *netdev,
2408 const struct ethtool_tunable *tuna,
2409 void *data)
2410{
2411 struct fec_enet_private *fep = netdev_priv(netdev);
2412 int ret = 0;
2413
2414 switch (tuna->id) {
2415 case ETHTOOL_RX_COPYBREAK:
2416 *(u32 *)data = fep->rx_copybreak;
2417 break;
2418 default:
2419 ret = -EINVAL;
2420 break;
2421 }
2422
2423 return ret;
2424}
2425
2426static int fec_enet_set_tunable(struct net_device *netdev,
2427 const struct ethtool_tunable *tuna,
2428 const void *data)
2429{
2430 struct fec_enet_private *fep = netdev_priv(netdev);
2431 int ret = 0;
2432
2433 switch (tuna->id) {
2434 case ETHTOOL_RX_COPYBREAK:
2435 fep->rx_copybreak = *(u32 *)data;
2436 break;
2437 default:
2438 ret = -EINVAL;
2439 break;
2440 }
2441
2442 return ret;
2443}
2444
stephen hemminger9b07be42012-01-04 12:59:49 +00002445static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002446 .get_settings = fec_enet_get_settings,
2447 .set_settings = fec_enet_set_settings,
2448 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002449 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002450 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002451 .get_coalesce = fec_enet_get_coalesce,
2452 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002453#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002454 .get_pauseparam = fec_enet_get_pauseparam,
2455 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002456 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002457 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002458 .get_sset_count = fec_enet_get_sset_count,
2459#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002460 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002461 .get_tunable = fec_enet_get_tunable,
2462 .set_tunable = fec_enet_set_tunable,
Bryan Wue6b043d2010-03-31 02:10:44 +00002463};
2464
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002465static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002466{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002467 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002468 struct phy_device *phydev = fep->phy_dev;
2469
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002470 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002471 return -EINVAL;
2472
2473 if (!phydev)
2474 return -ENODEV;
2475
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002476 if (fep->bufdesc_ex) {
2477 if (cmd == SIOCSHWTSTAMP)
2478 return fec_ptp_set(ndev, rq);
2479 if (cmd == SIOCGHWTSTAMP)
2480 return fec_ptp_get(ndev, rq);
2481 }
Frank Liff43da82013-01-03 16:04:23 +00002482
Richard Cochran28b04112010-07-17 08:48:55 +00002483 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002486static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002487{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002488 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002489 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002490 struct sk_buff *skb;
2491 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002492 struct fec_enet_priv_tx_q *txq;
2493 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f742014-09-13 05:00:50 +08002494 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002495
Frank Li59d0f742014-09-13 05:00:50 +08002496 for (q = 0; q < fep->num_rx_queues; q++) {
2497 rxq = fep->rx_queue[q];
2498 bdp = rxq->rx_bd_base;
2499 for (i = 0; i < rxq->rx_ring_size; i++) {
2500 skb = rxq->rx_skbuff[i];
2501 rxq->rx_skbuff[i] = NULL;
2502 if (skb) {
2503 dma_unmap_single(&fep->pdev->dev,
2504 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002505 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f742014-09-13 05:00:50 +08002506 DMA_FROM_DEVICE);
2507 dev_kfree_skb(skb);
2508 }
2509 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002510 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002511 }
2512
Frank Li59d0f742014-09-13 05:00:50 +08002513 for (q = 0; q < fep->num_tx_queues; q++) {
2514 txq = fep->tx_queue[q];
2515 bdp = txq->tx_bd_base;
2516 for (i = 0; i < txq->tx_ring_size; i++) {
2517 kfree(txq->tx_bounce[i]);
2518 txq->tx_bounce[i] = NULL;
2519 skb = txq->tx_skbuff[i];
2520 txq->tx_skbuff[i] = NULL;
2521 dev_kfree_skb(skb);
2522 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002523 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002524}
2525
Frank Li59d0f742014-09-13 05:00:50 +08002526static void fec_enet_free_queue(struct net_device *ndev)
2527{
2528 struct fec_enet_private *fep = netdev_priv(ndev);
2529 int i;
2530 struct fec_enet_priv_tx_q *txq;
2531
2532 for (i = 0; i < fep->num_tx_queues; i++)
2533 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2534 txq = fep->tx_queue[i];
2535 dma_free_coherent(NULL,
2536 txq->tx_ring_size * TSO_HEADER_SIZE,
2537 txq->tso_hdrs,
2538 txq->tso_hdrs_dma);
2539 }
2540
2541 for (i = 0; i < fep->num_rx_queues; i++)
2542 if (fep->rx_queue[i])
2543 kfree(fep->rx_queue[i]);
2544
2545 for (i = 0; i < fep->num_tx_queues; i++)
2546 if (fep->tx_queue[i])
2547 kfree(fep->tx_queue[i]);
2548}
2549
2550static int fec_enet_alloc_queue(struct net_device *ndev)
2551{
2552 struct fec_enet_private *fep = netdev_priv(ndev);
2553 int i;
2554 int ret = 0;
2555 struct fec_enet_priv_tx_q *txq;
2556
2557 for (i = 0; i < fep->num_tx_queues; i++) {
2558 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2559 if (!txq) {
2560 ret = -ENOMEM;
2561 goto alloc_failed;
2562 }
2563
2564 fep->tx_queue[i] = txq;
2565 txq->tx_ring_size = TX_RING_SIZE;
2566 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2567
2568 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2569 txq->tx_wake_threshold =
2570 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2571
2572 txq->tso_hdrs = dma_alloc_coherent(NULL,
2573 txq->tx_ring_size * TSO_HEADER_SIZE,
2574 &txq->tso_hdrs_dma,
2575 GFP_KERNEL);
2576 if (!txq->tso_hdrs) {
2577 ret = -ENOMEM;
2578 goto alloc_failed;
2579 }
2580 }
2581
2582 for (i = 0; i < fep->num_rx_queues; i++) {
2583 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2584 GFP_KERNEL);
2585 if (!fep->rx_queue[i]) {
2586 ret = -ENOMEM;
2587 goto alloc_failed;
2588 }
2589
2590 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2591 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2592 }
2593 return ret;
2594
2595alloc_failed:
2596 fec_enet_free_queue(ndev);
2597 return ret;
2598}
2599
2600static int
2601fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002602{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002603 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002604 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002605 struct sk_buff *skb;
2606 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002607 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002608
Frank Li59d0f742014-09-13 05:00:50 +08002609 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002610 bdp = rxq->rx_bd_base;
2611 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002612 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002613 if (!skb)
2614 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002615
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002616 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002617 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002618 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002619 }
Russell King730ee362014-07-08 00:23:14 +01002620
Fugang Duan4d494cd2014-09-13 05:00:48 +08002621 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002622 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002623
2624 if (fep->bufdesc_ex) {
2625 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2626 ebdp->cbd_esc = BD_ENET_RX_INT;
2627 }
2628
Frank Li59d0f742014-09-13 05:00:50 +08002629 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002630 }
2631
2632 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002633 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002634 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f742014-09-13 05:00:50 +08002635 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002636
Frank Li59d0f742014-09-13 05:00:50 +08002637 err_alloc:
2638 fec_enet_free_buffers(ndev);
2639 return -ENOMEM;
2640}
2641
2642static int
2643fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2644{
2645 struct fec_enet_private *fep = netdev_priv(ndev);
2646 unsigned int i;
2647 struct bufdesc *bdp;
2648 struct fec_enet_priv_tx_q *txq;
2649
2650 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002651 bdp = txq->tx_bd_base;
2652 for (i = 0; i < txq->tx_ring_size; i++) {
2653 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2654 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002655 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002656
2657 bdp->cbd_sc = 0;
2658 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002659
Frank Liff43da82013-01-03 16:04:23 +00002660 if (fep->bufdesc_ex) {
2661 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002662 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002663 }
2664
Frank Li59d0f742014-09-13 05:00:50 +08002665 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002666 }
2667
2668 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002669 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002670 bdp->cbd_sc |= BD_SC_WRAP;
2671
2672 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002673
2674 err_alloc:
2675 fec_enet_free_buffers(ndev);
2676 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002677}
2678
Frank Li59d0f742014-09-13 05:00:50 +08002679static int fec_enet_alloc_buffers(struct net_device *ndev)
2680{
2681 struct fec_enet_private *fep = netdev_priv(ndev);
2682 unsigned int i;
2683
2684 for (i = 0; i < fep->num_rx_queues; i++)
2685 if (fec_enet_alloc_rxq_buffers(ndev, i))
2686 return -ENOMEM;
2687
2688 for (i = 0; i < fep->num_tx_queues; i++)
2689 if (fec_enet_alloc_txq_buffers(ndev, i))
2690 return -ENOMEM;
2691 return 0;
2692}
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002695fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002697 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002700 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002701 ret = fec_enet_clk_enable(ndev, true);
2702 if (ret)
2703 return ret;
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* I should reset the ring buffers here, but I don't yet know
2706 * a simple way to do that.
2707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002709 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002710 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002711 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002712
Bryan Wu418bd0d2010-05-28 03:40:39 -07002713 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002714 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002715 if (ret)
2716 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002717
Russell Kingef833372014-07-08 12:40:43 +01002718 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002719 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002720 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002721 netif_tx_start_all_queues(ndev);
2722
Sascha Hauer22f6b862009-04-15 01:32:18 +00002723 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002724
2725err_enet_mii_probe:
2726 fec_enet_free_buffers(ndev);
2727err_enet_alloc:
2728 fec_enet_clk_enable(ndev, false);
2729 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
2733static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002734fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002736 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Russell Kingd76cfae2014-07-08 00:23:04 +01002738 phy_stop(fep->phy_dev);
2739
Russell King31a6de32014-07-08 12:40:23 +01002740 if (netif_device_present(ndev)) {
2741 napi_disable(&fep->napi);
2742 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002743 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Russell King635cf172014-07-08 00:22:54 +01002746 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002747 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002748
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002749 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002750 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002751 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
2754}
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756/* Set or clear the multicast filter for this adaptor.
2757 * Skeleton taken from sunlance driver.
2758 * The CPM Ethernet implementation allows Multicast as well as individual
2759 * MAC address filtering. Some of the drivers check to make sure it is
2760 * a group multicast address, and discard those that are not. I guess I
2761 * will do the same for now, but just remove the test if you want
2762 * individual filtering as well (do the upper net layers want or support
2763 * this kind of feature?).
2764 */
2765
2766#define HASH_BITS 6 /* #bits in hash */
2767#define CRC32_POLY 0xEDB88320
2768
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002769static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002771 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002772 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002773 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unsigned char hash;
2775
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002776 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002777 tmp = readl(fep->hwp + FEC_R_CNTRL);
2778 tmp |= 0x8;
2779 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002780 return;
2781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Sascha Hauer4e831832009-04-15 01:32:19 +00002783 tmp = readl(fep->hwp + FEC_R_CNTRL);
2784 tmp &= ~0x8;
2785 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002786
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002787 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002788 /* Catch all multicast addresses, so set the
2789 * filter to all 1's
2790 */
2791 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2792 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Sascha Hauer4e831832009-04-15 01:32:19 +00002794 return;
2795 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002796
Sascha Hauer4e831832009-04-15 01:32:19 +00002797 /* Clear filter and add the addresses in hash register
2798 */
2799 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2800 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002802 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002803 /* calculate crc32 value of mac address */
2804 crc = 0xffffffff;
2805
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002806 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002807 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002808 for (bit = 0; bit < 8; bit++, data >>= 1) {
2809 crc = (crc >> 1) ^
2810 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
2812 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002813
2814 /* only upper 6 bits (HASH_BITS) are used
2815 * which point to specific bit in he hash registers
2816 */
2817 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2818
2819 if (hash > 31) {
2820 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2821 tmp |= 1 << (hash - 32);
2822 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2823 } else {
2824 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2825 tmp |= 1 << hash;
2826 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829}
2830
Sascha Hauer22f6b862009-04-15 01:32:18 +00002831/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002832static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002833fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002835 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002836 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Lucas Stach44934fa2014-03-30 21:32:08 +02002838 if (addr) {
2839 if (!is_valid_ether_addr(addr->sa_data))
2840 return -EADDRNOTAVAIL;
2841 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2842 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002843
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002844 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2845 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002846 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002847 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002848 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002852#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002853/**
2854 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002855 * @dev: The FEC network adapter
2856 *
2857 * Polled functionality used by netconsole and others in non interrupt mode
2858 *
2859 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002860static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002861{
2862 int i;
2863 struct fec_enet_private *fep = netdev_priv(dev);
2864
2865 for (i = 0; i < FEC_IRQ_NUM; i++) {
2866 if (fep->irq[i] > 0) {
2867 disable_irq(fep->irq[i]);
2868 fec_enet_interrupt(fep->irq[i], dev);
2869 enable_irq(fep->irq[i]);
2870 }
2871 }
2872}
2873#endif
2874
Russell King8506fa12014-07-08 12:40:33 +01002875#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
Nimrod Andy5bc26722014-10-13 10:53:48 +08002876static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00002877 netdev_features_t features)
2878{
2879 struct fec_enet_private *fep = netdev_priv(netdev);
2880 netdev_features_t changed = features ^ netdev->features;
2881
2882 netdev->features = features;
2883
2884 /* Receive checksum has been changed */
2885 if (changed & NETIF_F_RXCSUM) {
2886 if (features & NETIF_F_RXCSUM)
2887 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2888 else
2889 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002890 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08002891}
Jim Baxter4c09eed2013-04-19 08:10:49 +00002892
Nimrod Andy5bc26722014-10-13 10:53:48 +08002893static int fec_set_features(struct net_device *netdev,
2894 netdev_features_t features)
2895{
2896 struct fec_enet_private *fep = netdev_priv(netdev);
2897 netdev_features_t changed = features ^ netdev->features;
2898
Russell King8506fa12014-07-08 12:40:33 +01002899 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08002900 napi_disable(&fep->napi);
2901 netif_tx_lock_bh(netdev);
2902 fec_stop(netdev);
2903 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01002904 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002905 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002906 netif_tx_unlock_bh(netdev);
2907 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08002908 } else {
2909 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002910 }
2911
2912 return 0;
2913}
2914
Sascha Hauer009fda82009-04-15 01:32:23 +00002915static const struct net_device_ops fec_netdev_ops = {
2916 .ndo_open = fec_enet_open,
2917 .ndo_stop = fec_enet_close,
2918 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002919 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002920 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002921 .ndo_validate_addr = eth_validate_addr,
2922 .ndo_tx_timeout = fec_timeout,
2923 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002924 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002925#ifdef CONFIG_NET_POLL_CONTROLLER
2926 .ndo_poll_controller = fec_poll_controller,
2927#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002928 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002929};
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 /*
2932 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002935static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002937 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002938 const struct platform_device_id *id_entry =
2939 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002940 struct fec_enet_priv_tx_q *txq;
2941 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002942 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002943 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002944 int bd_size;
Frank Li59d0f742014-09-13 05:00:50 +08002945 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002946
Fugang Duan41ef84c2014-09-13 05:00:54 +08002947#if defined(CONFIG_ARM)
2948 fep->rx_align = 0xf;
2949 fep->tx_align = 0xf;
2950#else
2951 fep->rx_align = 0x3;
2952 fep->tx_align = 0x3;
2953#endif
2954
Frank Li59d0f742014-09-13 05:00:50 +08002955 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002956
Nimrod Andy55d02182014-06-12 08:16:21 +08002957 if (fep->bufdesc_ex)
2958 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2959 else
2960 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002961 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002962 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002964 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002965 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002966 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002967 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002968 return -ENOMEM;
2969 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002970
Fugang Duan4d494cd2014-09-13 05:00:48 +08002971 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Shawn Guo49da97d2011-01-05 21:13:11 +00002973 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002974 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002975 /* make sure MAC we just acquired is programmed into the hw */
2976 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002978 /* Set receive and transmit descriptor base. */
Frank Li59d0f742014-09-13 05:00:50 +08002979 for (i = 0; i < fep->num_rx_queues; i++) {
2980 rxq = fep->rx_queue[i];
2981 rxq->index = i;
2982 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2983 rxq->bd_dma = bd_dma;
2984 if (fep->bufdesc_ex) {
2985 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2986 cbd_base = (struct bufdesc *)
2987 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2988 } else {
2989 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2990 cbd_base += rxq->rx_ring_size;
2991 }
2992 }
2993
2994 for (i = 0; i < fep->num_tx_queues; i++) {
2995 txq = fep->tx_queue[i];
2996 txq->index = i;
2997 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2998 txq->bd_dma = bd_dma;
2999 if (fep->bufdesc_ex) {
3000 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3001 cbd_base = (struct bufdesc *)
3002 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3003 } else {
3004 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3005 cbd_base += txq->tx_ring_size;
3006 }
3007 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Sascha Hauer22f6b862009-04-15 01:32:18 +00003010 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003011 ndev->watchdog_timeo = TX_TIMEOUT;
3012 ndev->netdev_ops = &fec_netdev_ops;
3013 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003014
Frank Lidc975382013-01-28 18:31:42 +00003015 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003016 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003017
Nimrod Andy09d1e542014-06-12 08:16:20 +08003018 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003019 /* enable hw VLAN support */
3020 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003021
Shawn Guo48496252013-05-08 21:08:22 +00003022 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003023 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3024
Shawn Guo48496252013-05-08 21:08:22 +00003025 /* enable hw accelerator */
3026 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003027 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003028 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3029 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003030
Fugang Duan41ef84c2014-09-13 05:00:54 +08003031 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3032 fep->tx_align = 0;
3033 fep->rx_align = 0x3f;
3034 }
3035
Nimrod Andy09d1e542014-06-12 08:16:20 +08003036 ndev->hw_features = ndev->features;
3037
Russell Kingef833372014-07-08 12:40:43 +01003038 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return 0;
3041}
3042
Shawn Guoca2cc332011-06-25 02:04:35 +08003043#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003044static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003045{
3046 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003047 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003048 struct device_node *np = pdev->dev.of_node;
3049
3050 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003051 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003052
Shawn Guoa3caad02012-06-27 03:45:24 +00003053 of_property_read_u32(np, "phy-reset-duration", &msec);
3054 /* A sane reset duration should not be longer than 1s */
3055 if (msec > 1000)
3056 msec = 1;
3057
Shawn Guoca2cc332011-06-25 02:04:35 +08003058 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003059 if (!gpio_is_valid(phy_reset))
3060 return;
3061
Shawn Guo119fc002012-06-27 03:45:22 +00003062 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3063 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003064 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003065 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003066 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003067 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003068 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003069 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003070}
3071#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003072static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003073{
3074 /*
3075 * In case of platform probe, the reset has been done
3076 * by machine code.
3077 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003078}
3079#endif /* CONFIG_OF */
3080
Fugang Duan9fc095f2014-09-13 05:00:49 +08003081static void
3082fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3083{
3084 struct device_node *np = pdev->dev.of_node;
3085 int err;
3086
3087 *num_tx = *num_rx = 1;
3088
3089 if (!np || !of_device_is_available(np))
3090 return;
3091
3092 /* parse the num of tx and rx queues */
3093 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003094 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003095 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003096
3097 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3098 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003099 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003100
3101 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003102 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3103 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003104 *num_tx = 1;
3105 return;
3106 }
3107
3108 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003109 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3110 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003111 *num_rx = 1;
3112 return;
3113 }
3114
3115}
3116
Bill Pemberton33897cc2012-12-03 09:23:58 -05003117static int
Sascha Haueread73182009-01-28 23:03:11 +00003118fec_probe(struct platform_device *pdev)
3119{
3120 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003121 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003122 struct net_device *ndev;
3123 int i, irq, ret = 0;
3124 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003125 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003126 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003127 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003128 int num_tx_qs;
3129 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003130
3131 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3132 if (of_id)
3133 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003134
Fugang Duan9fc095f2014-09-13 05:00:49 +08003135 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3136
Sascha Haueread73182009-01-28 23:03:11 +00003137 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003138 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3139 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003140 if (!ndev)
3141 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003142
3143 SET_NETDEV_DEV(ndev, &pdev->dev);
3144
3145 /* setup board info structure */
3146 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003147
Fugang Duan9fc095f2014-09-13 05:00:49 +08003148 fep->num_rx_queues = num_rx_qs;
3149 fep->num_tx_queues = num_tx_qs;
3150
Guenter Roeckd1391932013-06-18 10:04:59 -07003151#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003152 /* default enable pause frame auto negotiation */
3153 if (pdev->id_entry &&
3154 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3155 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003156#endif
Frank Libaa70a52013-01-16 16:55:58 +00003157
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003158 /* Select default pin state */
3159 pinctrl_pm_select_default_state(&pdev->dev);
3160
Fabio Estevam399db752013-07-21 13:25:03 -03003161 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303162 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3163 if (IS_ERR(fep->hwp)) {
3164 ret = PTR_ERR(fep->hwp);
3165 goto failed_ioremap;
3166 }
3167
Bryan Wue6b043d2010-03-31 02:10:44 +00003168 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003169 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003170
Frank Liff43da82013-01-03 16:04:23 +00003171 fep->bufdesc_ex = 0;
3172
Sascha Haueread73182009-01-28 23:03:11 +00003173 platform_set_drvdata(pdev, ndev);
3174
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003175 phy_node = of_parse_phandle(np, "phy-handle", 0);
3176 if (!phy_node && of_phy_is_fixed_link(np)) {
3177 ret = of_phy_register_fixed_link(np);
3178 if (ret < 0) {
3179 dev_err(&pdev->dev,
3180 "broken fixed-link specification\n");
3181 goto failed_phy;
3182 }
3183 phy_node = of_node_get(np);
3184 }
3185 fep->phy_node = phy_node;
3186
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003187 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003188 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003189 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003190 if (pdata)
3191 fep->phy_interface = pdata->phy;
3192 else
3193 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3194 } else {
3195 fep->phy_interface = ret;
3196 }
3197
Fabio Estevame2f8d552013-02-22 06:40:45 +00003198 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3199 if (IS_ERR(fep->clk_ipg)) {
3200 ret = PTR_ERR(fep->clk_ipg);
3201 goto failed_clk;
3202 }
3203
3204 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3205 if (IS_ERR(fep->clk_ahb)) {
3206 ret = PTR_ERR(fep->clk_ahb);
3207 goto failed_clk;
3208 }
3209
Fugang Duand851b472014-09-17 05:18:52 +08003210 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3211
Linus Torvalds38f56f32013-05-07 11:06:17 -07003212 /* enet_out is optional, depends on board */
3213 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3214 if (IS_ERR(fep->clk_enet_out))
3215 fep->clk_enet_out = NULL;
3216
Nimrod Andy91c0d982014-08-21 17:09:38 +08003217 fep->ptp_clk_on = false;
3218 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003219
3220 /* clk_ref is optional, depends on board */
3221 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3222 if (IS_ERR(fep->clk_ref))
3223 fep->clk_ref = NULL;
3224
Fabio Estevame2f8d552013-02-22 06:40:45 +00003225 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003226 fep->bufdesc_ex =
3227 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003228 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003229 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003230 fep->bufdesc_ex = 0;
3231 }
3232
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003233 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003234 if (ret)
3235 goto failed_clk;
3236
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003237 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3238 if (!IS_ERR(fep->reg_phy)) {
3239 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003240 if (ret) {
3241 dev_err(&pdev->dev,
3242 "Failed to enable phy regulator: %d\n", ret);
3243 goto failed_regulator;
3244 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003245 } else {
3246 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003247 }
3248
3249 fec_reset_phy(pdev);
3250
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003251 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003252 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003253
3254 ret = fec_enet_init(ndev);
3255 if (ret)
3256 goto failed_init;
3257
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003258 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003259 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003260 if (irq < 0) {
3261 if (i)
3262 break;
3263 ret = irq;
3264 goto failed_irq;
3265 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003266 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003267 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003268 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003269 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003270 }
3271
Fugang Duanb4d39b52014-09-13 05:00:55 +08003272 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003273 ret = fec_enet_mii_init(pdev);
3274 if (ret)
3275 goto failed_mii_init;
3276
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003277 /* Carrier starts down, phylib will bring it up */
3278 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003279 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003280 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003281
Sascha Haueread73182009-01-28 23:03:11 +00003282 ret = register_netdev(ndev);
3283 if (ret)
3284 goto failed_register;
3285
Fabio Estevameb1d0642013-04-13 07:25:36 +00003286 if (fep->bufdesc_ex && fep->ptp_clock)
3287 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3288
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003289 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003290 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003291 return 0;
3292
3293failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003294 fec_enet_mii_remove(fep);
3295failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003296failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003297failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003298 if (fep->reg_phy)
3299 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003300failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003301 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003302failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003303failed_phy:
3304 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003305failed_ioremap:
3306 free_netdev(ndev);
3307
3308 return ret;
3309}
3310
Bill Pemberton33897cc2012-12-03 09:23:58 -05003311static int
Sascha Haueread73182009-01-28 23:03:11 +00003312fec_drv_remove(struct platform_device *pdev)
3313{
3314 struct net_device *ndev = platform_get_drvdata(pdev);
3315 struct fec_enet_private *fep = netdev_priv(ndev);
3316
Nimrod Andy91c0d982014-08-21 17:09:38 +08003317 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003318 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003319 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003320 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003321 if (fep->reg_phy)
3322 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003323 if (fep->ptp_clock)
3324 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003325 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003326 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003327 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003328
Sascha Haueread73182009-01-28 23:03:11 +00003329 return 0;
3330}
3331
Fabio Estevamdd66d382014-07-24 18:24:01 -03003332static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003333{
Eric Benard87cad5c2010-06-18 04:19:54 +00003334 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003335 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003336
Russell Kingda1774e2014-07-08 12:39:57 +01003337 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003338 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003339 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003340 napi_disable(&fep->napi);
3341 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003342 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003343 netif_tx_unlock_bh(ndev);
3344 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003345 }
Russell Kingda1774e2014-07-08 12:39:57 +01003346 rtnl_unlock();
3347
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003348 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003349 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003350
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003351 if (fep->reg_phy)
3352 regulator_disable(fep->reg_phy);
3353
Sascha Haueread73182009-01-28 23:03:11 +00003354 return 0;
3355}
3356
Fabio Estevamdd66d382014-07-24 18:24:01 -03003357static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003358{
Eric Benard87cad5c2010-06-18 04:19:54 +00003359 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003360 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003361 int ret;
3362
3363 if (fep->reg_phy) {
3364 ret = regulator_enable(fep->reg_phy);
3365 if (ret)
3366 return ret;
3367 }
Sascha Haueread73182009-01-28 23:03:11 +00003368
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003369 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003370 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003371 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003372 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003373
Russell Kingda1774e2014-07-08 12:39:57 +01003374 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003375 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003376 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003377 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003378 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003379 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003380 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003381 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003382 }
Russell Kingda1774e2014-07-08 12:39:57 +01003383 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003384
Sascha Haueread73182009-01-28 23:03:11 +00003385 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003386
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003387failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003388 if (fep->reg_phy)
3389 regulator_disable(fep->reg_phy);
3390 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003391}
3392
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003393static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003394
Sascha Haueread73182009-01-28 23:03:11 +00003395static struct platform_driver fec_driver = {
3396 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003397 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003398 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003399 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003400 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003401 },
Shawn Guob5680e02011-01-05 21:13:13 +00003402 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003403 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003404 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003405};
3406
Fabio Estevamaaca2372012-01-23 16:44:50 +00003407module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003409MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410MODULE_LICENSE("GPL");