blob: 87975b5dda94674e93e5c3297a88da21beda5588 [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 +000081/* Controller is ENET-MAC */
82#define FEC_QUIRK_ENET_MAC (1 << 0)
83/* Controller needs driver to swap frame */
84#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080085/* Controller uses gasket */
86#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000087/* Controller has GBIT support */
88#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000089/* Controller has extend desc buffer */
90#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000091/* Controller has hardware checksum support */
92#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010093/* Controller has hardware vlan support */
94#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080095/* ENET IP errata ERR006358
96 *
97 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
98 * detected as not set during a prior frame transmission, then the
99 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
100 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +0800101 * frames not being transmitted until there is a 0-to-1 transition on
102 * ENET_TDAR[TDAR].
103 */
104#define FEC_QUIRK_ERR006358 (1 << 7)
Fugang Duan95a77472014-09-13 05:00:47 +0800105/* ENET IP hw AVB
106 *
107 * i.MX6SX ENET IP add Audio Video Bridging (AVB) feature support.
108 * - Two class indicators on receive with configurable priority
109 * - Two class indicators and line speed timer on transmit allowing
110 * implementation class credit based shapers externally
111 * - Additional DMA registers provisioned to allow managing up to 3
112 * independent rings
113 */
114#define FEC_QUIRK_HAS_AVB (1 << 8)
Fugang Duan37d60172014-09-17 05:18:54 +0800115/* There is a TDAR race condition for mutliQ when the software sets TDAR
116 * and the UDMA clears TDAR simultaneously or in a small window (2-4 cycles).
117 * This will cause the udma_tx and udma_tx_arbiter state machines to hang.
118 * The issue exist at i.MX6SX enet IP.
119 */
120#define FEC_QUIRK_ERR007885 (1 << 9)
Shawn Guob5680e02011-01-05 21:13:13 +0000121
122static struct platform_device_id fec_devtype[] = {
123 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800124 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000125 .name = DRIVER_NAME,
126 .driver_data = 0,
127 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800128 .name = "imx25-fec",
129 .driver_data = FEC_QUIRK_USE_GASKET,
130 }, {
131 .name = "imx27-fec",
132 .driver_data = 0,
133 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000134 .name = "imx28-fec",
135 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800136 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000137 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000138 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100139 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800140 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000141 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000142 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000143 .driver_data = FEC_QUIRK_ENET_MAC,
144 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800145 .name = "imx6sx-fec",
146 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
147 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800148 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
149 FEC_QUIRK_ERR007885,
Fugang Duan95a77472014-09-13 05:00:47 +0800150 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800151 /* sentinel */
152 }
Shawn Guob5680e02011-01-05 21:13:13 +0000153};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800154MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000155
Shawn Guoca2cc332011-06-25 02:04:35 +0800156enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000157 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800158 IMX27_FEC, /* runs on i.mx27/35/51 */
159 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000160 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000161 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800162 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800163};
164
165static const struct of_device_id fec_dt_ids[] = {
166 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
167 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
168 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000169 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000170 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800171 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800172 { /* sentinel */ }
173};
174MODULE_DEVICE_TABLE(of, fec_dt_ids);
175
Shawn Guo49da97d2011-01-05 21:13:11 +0000176static unsigned char macaddr[ETH_ALEN];
177module_param_array(macaddr, byte, NULL, 0);
178MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
179
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000180#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * Some hardware gets it MAC address out of local flash memory.
183 * if this is non-zero then assume it is the address to get MAC from.
184 */
185#if defined(CONFIG_NETtel)
186#define FEC_FLASHMAC 0xf0006006
187#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
188#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#elif defined(CONFIG_CANCam)
190#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000191#elif defined (CONFIG_M5272C3)
192#define FEC_FLASHMAC (0xffe04000 + 4)
193#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000194#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#else
196#define FEC_FLASHMAC 0
197#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800198#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000199
Jim Baxtercdffcf12013-07-02 22:52:56 +0100200/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100202#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100204#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jim Baxter4c09eed2013-04-19 08:10:49 +0000206/* FEC receive acceleration */
207#define FEC_RACC_IPDIS (1 << 1)
208#define FEC_RACC_PRODIS (1 << 2)
209#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Matt Waddel6b265292006-06-27 13:10:56 +1000212 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * size bits. Other FEC hardware does not, so we need to take that into
214 * account when setting it.
215 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000216#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100217 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
219#else
220#define OPT_FRAME_SIZE 0
221#endif
222
Bryan Wue6b043d2010-03-31 02:10:44 +0000223/* FEC MII MMFR bits definition */
224#define FEC_MMFR_ST (1 << 30)
225#define FEC_MMFR_OP_READ (2 << 28)
226#define FEC_MMFR_OP_WRITE (1 << 28)
227#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
228#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
229#define FEC_MMFR_TA (2 << 16)
230#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500232#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Sascha Hauer22f6b862009-04-15 01:32:18 +0000234/* Transmitter timeout */
235#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Frank Libaa70a52013-01-16 16:55:58 +0000237#define FEC_PAUSE_FLAG_AUTONEG 0x1
238#define FEC_PAUSE_FLAG_ENABLE 0x2
239
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800240#define COPYBREAK_DEFAULT 256
241
Nimrod Andy79f33912014-06-12 08:16:23 +0800242#define TSO_HEADER_SIZE 128
243/* Max number of allowed TCP segments for software TSO */
244#define FEC_MAX_TSO_SEGS 100
245#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
246
247#define IS_TSO_HEADER(txq, addr) \
248 ((addr >= txq->tso_hdrs_dma) && \
249 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
250
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000251static int mii_cnt;
252
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800253static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800254struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
255 struct fec_enet_private *fep,
256 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000257{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800258 struct bufdesc *new_bd = bdp + 1;
259 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800260 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
261 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800262 struct bufdesc_ex *ex_base;
263 struct bufdesc *base;
264 int ring_size;
265
Fugang Duan4d494cd2014-09-13 05:00:48 +0800266 if (bdp >= txq->tx_bd_base) {
267 base = txq->tx_bd_base;
268 ring_size = txq->tx_ring_size;
269 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800270 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800271 base = rxq->rx_bd_base;
272 ring_size = rxq->rx_ring_size;
273 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800274 }
275
276 if (fep->bufdesc_ex)
277 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
278 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000279 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800280 return (new_bd >= (base + ring_size)) ?
281 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000282}
283
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800284static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800285struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
286 struct fec_enet_private *fep,
287 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000288{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800289 struct bufdesc *new_bd = bdp - 1;
290 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800291 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
292 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800293 struct bufdesc_ex *ex_base;
294 struct bufdesc *base;
295 int ring_size;
296
Fugang Duan4d494cd2014-09-13 05:00:48 +0800297 if (bdp >= txq->tx_bd_base) {
298 base = txq->tx_bd_base;
299 ring_size = txq->tx_ring_size;
300 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800301 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800302 base = rxq->rx_bd_base;
303 ring_size = rxq->rx_ring_size;
304 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800305 }
306
307 if (fep->bufdesc_ex)
308 return (struct bufdesc *)((ex_new_bd < ex_base) ?
309 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000310 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800311 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000312}
313
Nimrod Andy61a44272014-06-12 08:16:18 +0800314static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
315 struct fec_enet_private *fep)
316{
317 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
318}
319
Fugang Duan4d494cd2014-09-13 05:00:48 +0800320static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
321 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800322{
323 int entries;
324
Fugang Duan4d494cd2014-09-13 05:00:48 +0800325 entries = ((const char *)txq->dirty_tx -
326 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800327
Fugang Duan4d494cd2014-09-13 05:00:48 +0800328 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800329}
330
Shawn Guob5680e02011-01-05 21:13:13 +0000331static void *swap_buffer(void *bufaddr, int len)
332{
333 int i;
334 unsigned int *buf = bufaddr;
335
Fabio Estevamffed61e2013-05-21 05:44:26 +0000336 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000337 *buf = cpu_to_be32(*buf);
338
339 return bufaddr;
340}
341
Russell King344756f2014-07-08 13:01:59 +0100342static void fec_dump(struct net_device *ndev)
343{
344 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800345 struct bufdesc *bdp;
346 struct fec_enet_priv_tx_q *txq;
347 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100348
349 netdev_info(ndev, "TX ring dump\n");
350 pr_info("Nr SC addr len SKB\n");
351
Fugang Duan4d494cd2014-09-13 05:00:48 +0800352 txq = fep->tx_queue[0];
353 bdp = txq->tx_bd_base;
354
Russell King344756f2014-07-08 13:01:59 +0100355 do {
356 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
357 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800358 bdp == txq->cur_tx ? 'S' : ' ',
359 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100360 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800361 txq->tx_skbuff[index]);
362 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100363 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800364 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100365}
366
Fugang Duan62a02c92014-06-18 08:33:52 +0800367static inline bool is_ipv4_pkt(struct sk_buff *skb)
368{
369 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
370}
371
Jim Baxter4c09eed2013-04-19 08:10:49 +0000372static int
373fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
374{
375 /* Only run for packets requiring a checksum. */
376 if (skb->ip_summed != CHECKSUM_PARTIAL)
377 return 0;
378
379 if (unlikely(skb_cow_head(skb, 0)))
380 return -1;
381
Fugang Duan62a02c92014-06-18 08:33:52 +0800382 if (is_ipv4_pkt(skb))
383 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000384 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
385
386 return 0;
387}
388
Nimrod Andy6e909282014-06-12 08:16:22 +0800389static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800390fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
391 struct sk_buff *skb,
392 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800393{
394 struct fec_enet_private *fep = netdev_priv(ndev);
395 const struct platform_device_id *id_entry =
396 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800397 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800398 struct bufdesc_ex *ebdp;
399 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800400 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800401 int frag, frag_len;
402 unsigned short status;
403 unsigned int estatus = 0;
404 skb_frag_t *this_frag;
405 unsigned int index;
406 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100407 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800408 int i;
409
410 for (frag = 0; frag < nr_frags; frag++) {
411 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800412 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800413 ebdp = (struct bufdesc_ex *)bdp;
414
415 status = bdp->cbd_sc;
416 status &= ~BD_ENET_TX_STATS;
417 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
418 frag_len = skb_shinfo(skb)->frags[frag].size;
419
420 /* Handle the last BD specially */
421 if (frag == nr_frags - 1) {
422 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
423 if (fep->bufdesc_ex) {
424 estatus |= BD_ENET_TX_INT;
425 if (unlikely(skb_shinfo(skb)->tx_flags &
426 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
427 estatus |= BD_ENET_TX_TS;
428 }
429 }
430
431 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800432 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
433 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800434 if (skb->ip_summed == CHECKSUM_PARTIAL)
435 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
436 ebdp->cbd_bdu = 0;
437 ebdp->cbd_esc = estatus;
438 }
439
440 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
441
Fugang Duan4d494cd2014-09-13 05:00:48 +0800442 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800443 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800444 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800445 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
446 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800447
448 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
449 swap_buffer(bufaddr, frag_len);
450 }
451
Russell Kingd6bf3142014-07-08 00:23:19 +0100452 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
453 DMA_TO_DEVICE);
454 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800455 dev_kfree_skb_any(skb);
456 if (net_ratelimit())
457 netdev_err(ndev, "Tx DMA memory map failed\n");
458 goto dma_mapping_error;
459 }
460
Russell Kingd6bf3142014-07-08 00:23:19 +0100461 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800462 bdp->cbd_datlen = frag_len;
463 bdp->cbd_sc = status;
464 }
465
Fugang Duan4d494cd2014-09-13 05:00:48 +0800466 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800467
468 return 0;
469
470dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800471 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800472 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800473 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800474 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
475 bdp->cbd_datlen, DMA_TO_DEVICE);
476 }
477 return NETDEV_TX_OK;
478}
479
Fugang Duan4d494cd2014-09-13 05:00:48 +0800480static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
481 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800482{
483 struct fec_enet_private *fep = netdev_priv(ndev);
484 const struct platform_device_id *id_entry =
485 platform_get_device_id(fep->pdev);
486 int nr_frags = skb_shinfo(skb)->nr_frags;
487 struct bufdesc *bdp, *last_bdp;
488 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100489 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800490 unsigned short status;
491 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800492 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800493 unsigned int estatus = 0;
494 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800495 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800496 int ret;
497
Fugang Duan4d494cd2014-09-13 05:00:48 +0800498 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800499 if (entries_free < MAX_SKB_FRAGS + 1) {
500 dev_kfree_skb_any(skb);
501 if (net_ratelimit())
502 netdev_err(ndev, "NOT enough BD for SG!\n");
503 return NETDEV_TX_OK;
504 }
505
Nimrod Andy6e909282014-06-12 08:16:22 +0800506 /* Protocol checksum off-load for TCP and UDP. */
507 if (fec_enet_clear_csum(skb, ndev)) {
508 dev_kfree_skb_any(skb);
509 return NETDEV_TX_OK;
510 }
511
512 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800513 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800514 status = bdp->cbd_sc;
515 status &= ~BD_ENET_TX_STATS;
516
517 /* Set buffer length and buffer pointer */
518 bufaddr = skb->data;
519 buflen = skb_headlen(skb);
520
Fugang Duan4d494cd2014-09-13 05:00:48 +0800521 queue = skb_get_queue_mapping(skb);
522 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800523 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800524 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800525 memcpy(txq->tx_bounce[index], skb->data, buflen);
526 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800527
528 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
529 swap_buffer(bufaddr, buflen);
530 }
531
Russell Kingd6bf3142014-07-08 00:23:19 +0100532 /* Push the data cache so the CPM does not get stale memory data. */
533 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
534 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800535 dev_kfree_skb_any(skb);
536 if (net_ratelimit())
537 netdev_err(ndev, "Tx DMA memory map failed\n");
538 return NETDEV_TX_OK;
539 }
540
541 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800542 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800543 if (ret)
544 return ret;
545 } else {
546 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
547 if (fep->bufdesc_ex) {
548 estatus = BD_ENET_TX_INT;
549 if (unlikely(skb_shinfo(skb)->tx_flags &
550 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
551 estatus |= BD_ENET_TX_TS;
552 }
553 }
554
555 if (fep->bufdesc_ex) {
556
557 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
558
559 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
560 fep->hwts_tx_en))
561 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
562
Nimrod Andybefe8212014-09-23 15:40:55 +0800563 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
564 estatus |= FEC_TX_BD_FTYPE(queue);
565
Nimrod Andy6e909282014-06-12 08:16:22 +0800566 if (skb->ip_summed == CHECKSUM_PARTIAL)
567 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
568
569 ebdp->cbd_bdu = 0;
570 ebdp->cbd_esc = estatus;
571 }
572
Fugang Duan4d494cd2014-09-13 05:00:48 +0800573 last_bdp = txq->cur_tx;
574 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800575 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800576 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800577
578 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100579 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800580
581 /* Send it on its way. Tell FEC it's ready, interrupt when done,
582 * it's the last BD of the frame, and to put the CRC on the end.
583 */
584 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
585 bdp->cbd_sc = status;
586
Sascha Hauer22f6b862009-04-15 01:32:18 +0000587 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800588 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800590 skb_tx_timestamp(skb);
591
Fugang Duan4d494cd2014-09-13 05:00:48 +0800592 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Frank Lide5fb0a2013-03-03 17:34:25 +0000594 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800595 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000596
Nimrod Andy6e909282014-06-12 08:16:22 +0800597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Nimrod Andy79f33912014-06-12 08:16:23 +0800600static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800601fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
602 struct net_device *ndev,
603 struct bufdesc *bdp, int index, char *data,
604 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800605{
606 struct fec_enet_private *fep = netdev_priv(ndev);
607 const struct platform_device_id *id_entry =
608 platform_get_device_id(fep->pdev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200609 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800610 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800611 unsigned short status;
612 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100613 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800614
615 status = bdp->cbd_sc;
616 status &= ~BD_ENET_TX_STATS;
617
618 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800619
Fugang Duan41ef84c2014-09-13 05:00:54 +0800620 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800621 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800622 memcpy(txq->tx_bounce[index], data, size);
623 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800624
625 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
626 swap_buffer(data, size);
627 }
628
Russell Kingd6bf3142014-07-08 00:23:19 +0100629 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
630 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800631 dev_kfree_skb_any(skb);
632 if (net_ratelimit())
633 netdev_err(ndev, "Tx DMA memory map failed\n");
634 return NETDEV_TX_BUSY;
635 }
636
Russell Kingd6bf3142014-07-08 00:23:19 +0100637 bdp->cbd_datlen = size;
638 bdp->cbd_bufaddr = addr;
639
Nimrod Andy79f33912014-06-12 08:16:23 +0800640 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800641 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
642 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800643 if (skb->ip_summed == CHECKSUM_PARTIAL)
644 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
645 ebdp->cbd_bdu = 0;
646 ebdp->cbd_esc = estatus;
647 }
648
649 /* Handle the last BD specially */
650 if (last_tcp)
651 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
652 if (is_last) {
653 status |= BD_ENET_TX_INTR;
654 if (fep->bufdesc_ex)
655 ebdp->cbd_esc |= BD_ENET_TX_INT;
656 }
657
658 bdp->cbd_sc = status;
659
660 return 0;
661}
662
663static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800664fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
665 struct sk_buff *skb, struct net_device *ndev,
666 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800667{
668 struct fec_enet_private *fep = netdev_priv(ndev);
669 const struct platform_device_id *id_entry =
670 platform_get_device_id(fep->pdev);
671 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200672 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800673 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800674 void *bufaddr;
675 unsigned long dmabuf;
676 unsigned short status;
677 unsigned int estatus = 0;
678
679 status = bdp->cbd_sc;
680 status &= ~BD_ENET_TX_STATS;
681 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
682
Fugang Duan4d494cd2014-09-13 05:00:48 +0800683 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
684 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800685 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800686 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800687 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
688 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800689
690 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
691 swap_buffer(bufaddr, hdr_len);
692
693 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
694 hdr_len, DMA_TO_DEVICE);
695 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
696 dev_kfree_skb_any(skb);
697 if (net_ratelimit())
698 netdev_err(ndev, "Tx DMA memory map failed\n");
699 return NETDEV_TX_BUSY;
700 }
701 }
702
703 bdp->cbd_bufaddr = dmabuf;
704 bdp->cbd_datlen = hdr_len;
705
706 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800707 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
708 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800709 if (skb->ip_summed == CHECKSUM_PARTIAL)
710 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
711 ebdp->cbd_bdu = 0;
712 ebdp->cbd_esc = estatus;
713 }
714
715 bdp->cbd_sc = status;
716
717 return 0;
718}
719
Fugang Duan4d494cd2014-09-13 05:00:48 +0800720static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
721 struct sk_buff *skb,
722 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800723{
724 struct fec_enet_private *fep = netdev_priv(ndev);
725 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
726 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800727 struct bufdesc *bdp = txq->cur_tx;
728 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800729 struct tso_t tso;
730 unsigned int index = 0;
731 int ret;
Fugang Duan37d60172014-09-17 05:18:54 +0800732 const struct platform_device_id *id_entry =
733 platform_get_device_id(fep->pdev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800734
Fugang Duan4d494cd2014-09-13 05:00:48 +0800735 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800736 dev_kfree_skb_any(skb);
737 if (net_ratelimit())
738 netdev_err(ndev, "NOT enough BD for TSO!\n");
739 return NETDEV_TX_OK;
740 }
741
742 /* Protocol checksum off-load for TCP and UDP. */
743 if (fec_enet_clear_csum(skb, ndev)) {
744 dev_kfree_skb_any(skb);
745 return NETDEV_TX_OK;
746 }
747
748 /* Initialize the TSO handler, and prepare the first payload */
749 tso_start(skb, &tso);
750
751 total_len = skb->len - hdr_len;
752 while (total_len > 0) {
753 char *hdr;
754
Fugang Duan4d494cd2014-09-13 05:00:48 +0800755 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800756 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
757 total_len -= data_left;
758
759 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800760 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800761 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800762 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800763 if (ret)
764 goto err_release;
765
766 while (data_left > 0) {
767 int size;
768
769 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800770 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
771 index = fec_enet_get_bd_index(txq->tx_bd_base,
772 bdp, fep);
773 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
774 bdp, index,
775 tso.data, size,
776 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800777 total_len == 0);
778 if (ret)
779 goto err_release;
780
781 data_left -= size;
782 tso_build_data(skb, &tso, size);
783 }
784
Fugang Duan4d494cd2014-09-13 05:00:48 +0800785 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800786 }
787
788 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800789 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800790
Nimrod Andy79f33912014-06-12 08:16:23 +0800791 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800792 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800793
794 /* Trigger transmission start */
Fugang Duan37d60172014-09-17 05:18:54 +0800795 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
796 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
797 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
798 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
799 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
800 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800801
802 return 0;
803
804err_release:
805 /* TODO: Release all used data descriptors for TSO */
806 return ret;
807}
808
Nimrod Andy61a44272014-06-12 08:16:18 +0800809static netdev_tx_t
810fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
811{
812 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800813 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800814 unsigned short queue;
815 struct fec_enet_priv_tx_q *txq;
816 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800817 int ret;
818
Fugang Duan4d494cd2014-09-13 05:00:48 +0800819 queue = skb_get_queue_mapping(skb);
820 txq = fep->tx_queue[queue];
821 nq = netdev_get_tx_queue(ndev, queue);
822
Nimrod Andy79f33912014-06-12 08:16:23 +0800823 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800824 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800825 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800826 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800827 if (ret)
828 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800829
Fugang Duan4d494cd2014-09-13 05:00:48 +0800830 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
831 if (entries_free <= txq->tx_stop_threshold)
832 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800833
834 return NETDEV_TX_OK;
835}
836
Frank Li14109a52013-03-26 16:12:03 +0000837/* Init RX & TX buffer descriptors
838 */
839static void fec_enet_bd_init(struct net_device *dev)
840{
841 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800842 struct fec_enet_priv_tx_q *txq;
843 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000844 struct bufdesc *bdp;
845 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800846 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000847
Frank Li59d0f7462014-09-13 05:00:50 +0800848 for (q = 0; q < fep->num_rx_queues; q++) {
849 /* Initialize the receive buffer descriptors. */
850 rxq = fep->rx_queue[q];
851 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800852
Frank Li59d0f7462014-09-13 05:00:50 +0800853 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000854
Frank Li59d0f7462014-09-13 05:00:50 +0800855 /* Initialize the BD for every fragment in the page. */
856 if (bdp->cbd_bufaddr)
857 bdp->cbd_sc = BD_ENET_RX_EMPTY;
858 else
859 bdp->cbd_sc = 0;
860 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000861 }
Frank Li59d0f7462014-09-13 05:00:50 +0800862
863 /* Set the last buffer to wrap */
864 bdp = fec_enet_get_prevdesc(bdp, fep, q);
865 bdp->cbd_sc |= BD_SC_WRAP;
866
867 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000868 }
869
Frank Li59d0f7462014-09-13 05:00:50 +0800870 for (q = 0; q < fep->num_tx_queues; q++) {
871 /* ...and the same for transmit */
872 txq = fep->tx_queue[q];
873 bdp = txq->tx_bd_base;
874 txq->cur_tx = bdp;
875
876 for (i = 0; i < txq->tx_ring_size; i++) {
877 /* Initialize the BD for every fragment in the page. */
878 bdp->cbd_sc = 0;
879 if (txq->tx_skbuff[i]) {
880 dev_kfree_skb_any(txq->tx_skbuff[i]);
881 txq->tx_skbuff[i] = NULL;
882 }
883 bdp->cbd_bufaddr = 0;
884 bdp = fec_enet_get_nextdesc(bdp, fep, q);
885 }
886
887 /* Set the last buffer to wrap */
888 bdp = fec_enet_get_prevdesc(bdp, fep, q);
889 bdp->cbd_sc |= BD_SC_WRAP;
890 txq->dirty_tx = bdp;
891 }
892}
893
Frank Lice99d0d2014-09-13 05:00:52 +0800894static void fec_enet_active_rxring(struct net_device *ndev)
895{
896 struct fec_enet_private *fep = netdev_priv(ndev);
897 int i;
898
899 for (i = 0; i < fep->num_rx_queues; i++)
900 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
901}
902
Frank Li59d0f7462014-09-13 05:00:50 +0800903static void fec_enet_enable_ring(struct net_device *ndev)
904{
905 struct fec_enet_private *fep = netdev_priv(ndev);
906 struct fec_enet_priv_tx_q *txq;
907 struct fec_enet_priv_rx_q *rxq;
908 int i;
909
910 for (i = 0; i < fep->num_rx_queues; i++) {
911 rxq = fep->rx_queue[i];
912 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
913
914 /* enable DMA1/2 */
915 if (i)
916 writel(RCMR_MATCHEN | RCMR_CMP(i),
917 fep->hwp + FEC_RCMR(i));
918 }
919
920 for (i = 0; i < fep->num_tx_queues; i++) {
921 txq = fep->tx_queue[i];
922 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
923
924 /* enable DMA1/2 */
925 if (i)
926 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
927 fep->hwp + FEC_DMA_CFG(i));
928 }
929}
930
931static void fec_enet_reset_skb(struct net_device *ndev)
932{
933 struct fec_enet_private *fep = netdev_priv(ndev);
934 struct fec_enet_priv_tx_q *txq;
935 int i, j;
936
937 for (i = 0; i < fep->num_tx_queues; i++) {
938 txq = fep->tx_queue[i];
939
940 for (j = 0; j < txq->tx_ring_size; j++) {
941 if (txq->tx_skbuff[j]) {
942 dev_kfree_skb_any(txq->tx_skbuff[j]);
943 txq->tx_skbuff[j] = NULL;
944 }
945 }
946 }
Frank Li14109a52013-03-26 16:12:03 +0000947}
948
Russell Kingdbc64a82014-07-08 12:40:12 +0100949/*
950 * This function is called to start or restart the FEC during a link
951 * change, transmit timeout, or to reconfigure the FEC. The network
952 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100953 */
954static void
Russell Kingef833372014-07-08 12:40:43 +0100955fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100956{
957 struct fec_enet_private *fep = netdev_priv(ndev);
958 const struct platform_device_id *id_entry =
959 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000960 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100961 u32 temp_mac[2];
962 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000963 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100964
Fugang Duan106c3142014-09-13 05:00:51 +0800965 /* Whack a reset. We should wait for this.
966 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
967 * instead of reset MAC itself.
968 */
969 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
970 writel(0, fep->hwp + FEC_ECNTRL);
971 } else {
972 writel(1, fep->hwp + FEC_ECNTRL);
973 udelay(10);
974 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975
976 /*
977 * enet-mac reset will reset mac address registers too,
978 * so need to reconfigure it.
979 */
980 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
981 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
982 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
983 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
984 }
985
986 /* Clear any outstanding interrupt. */
987 writel(0xffc00000, fep->hwp + FEC_IEVENT);
988
Uwe Kleine-König45993652011-01-19 20:47:04 +0100989 /* Set maximum receive buffer size. */
990 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
991
Frank Li14109a52013-03-26 16:12:03 +0000992 fec_enet_bd_init(ndev);
993
Frank Li59d0f7462014-09-13 05:00:50 +0800994 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100995
Frank Li59d0f7462014-09-13 05:00:50 +0800996 /* Reset tx SKB buffers. */
997 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100998
999 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +01001000 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001001 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +01001002 writel(0x04, fep->hwp + FEC_X_CNTRL);
1003 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001004 /* No Rcv on Xmit */
1005 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001006 writel(0x0, fep->hwp + FEC_X_CNTRL);
1007 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001008
Uwe Kleine-König45993652011-01-19 20:47:04 +01001009 /* Set MII speed */
1010 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1011
Guenter Roeckd1391932013-06-18 10:04:59 -07001012#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +00001013 /* set RX checksum */
1014 val = readl(fep->hwp + FEC_RACC);
1015 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1016 val |= FEC_RACC_OPTIONS;
1017 else
1018 val &= ~FEC_RACC_OPTIONS;
1019 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -07001020#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001021
Uwe Kleine-König45993652011-01-19 20:47:04 +01001022 /*
1023 * The phy interface and speed need to get configured
1024 * differently on enet-mac.
1025 */
1026 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001027 /* Enable flow control and length check */
1028 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001029
Shawn Guo230dec62011-09-23 02:12:48 +00001030 /* RGMII, RMII or MII */
1031 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1032 rcntl |= (1 << 6);
1033 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001034 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001035 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001036 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001037
Shawn Guo230dec62011-09-23 02:12:48 +00001038 /* 1G, 100M or 10M */
1039 if (fep->phy_dev) {
1040 if (fep->phy_dev->speed == SPEED_1000)
1041 ecntl |= (1 << 5);
1042 else if (fep->phy_dev->speed == SPEED_100)
1043 rcntl &= ~(1 << 9);
1044 else
1045 rcntl |= (1 << 9);
1046 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001047 } else {
1048#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001049 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001050 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001051 /* disable the gasket and wait */
1052 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1053 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1054 udelay(1);
1055
1056 /*
1057 * configure the gasket:
1058 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001059 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001060 */
Eric Benard8d82f212012-01-12 06:10:28 +00001061 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1062 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1063 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1064 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1065 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001066
1067 /* re-enable the gasket */
1068 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1069 }
1070#endif
1071 }
Frank Libaa70a52013-01-16 16:55:58 +00001072
Guenter Roeckd1391932013-06-18 10:04:59 -07001073#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001074 /* enable pause frame*/
1075 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1076 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1077 fep->phy_dev && fep->phy_dev->pause)) {
1078 rcntl |= FEC_ENET_FCE;
1079
Jim Baxter4c09eed2013-04-19 08:10:49 +00001080 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001081 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1082 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1083 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1084 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1085
1086 /* OPD */
1087 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1088 } else {
1089 rcntl &= ~FEC_ENET_FCE;
1090 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001091#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001092
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001093 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001094
Stefan Wahren84fe6182014-03-12 11:28:19 +01001095 /* Setup multicast filter. */
1096 set_multicast_list(ndev);
1097#ifndef CONFIG_M5272
1098 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1099 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1100#endif
1101
Shawn Guo230dec62011-09-23 02:12:48 +00001102 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1103 /* enable ENET endian swap */
1104 ecntl |= (1 << 8);
1105 /* enable ENET store and forward mode */
1106 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1107 }
1108
Frank Liff43da82013-01-03 16:04:23 +00001109 if (fep->bufdesc_ex)
1110 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001111
Chris Healy38ae92d2013-06-25 23:18:52 -07001112#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001113 /* Enable the MIB statistic event counters */
1114 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001115#endif
1116
Uwe Kleine-König45993652011-01-19 20:47:04 +01001117 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001118 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001119 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001120
Frank Liff43da82013-01-03 16:04:23 +00001121 if (fep->bufdesc_ex)
1122 fec_ptp_start_cyclecounter(ndev);
1123
Uwe Kleine-König45993652011-01-19 20:47:04 +01001124 /* Enable interrupts we wish to service */
1125 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001126
1127 /* Init the interrupt coalescing */
1128 fec_enet_itr_coal_init(ndev);
1129
Uwe Kleine-König45993652011-01-19 20:47:04 +01001130}
1131
1132static void
1133fec_stop(struct net_device *ndev)
1134{
1135 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001136 const struct platform_device_id *id_entry =
1137 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001138 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001139
1140 /* We cannot expect a graceful transmit stop without link !!! */
1141 if (fep->link) {
1142 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1143 udelay(10);
1144 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001145 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001146 }
1147
Fugang Duan106c3142014-09-13 05:00:51 +08001148 /* Whack a reset. We should wait for this.
1149 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1150 * instead of reset MAC itself.
1151 */
1152 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1153 writel(0, fep->hwp + FEC_ECNTRL);
1154 } else {
1155 writel(1, fep->hwp + FEC_ECNTRL);
1156 udelay(10);
1157 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001158 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1159 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001160
1161 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001162 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001163 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001164 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1165 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001166}
1167
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001170fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001172 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Russell King344756f2014-07-08 13:01:59 +01001174 fec_dump(ndev);
1175
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001176 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Russell King36cdc742014-07-08 13:01:44 +01001178 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001179}
1180
Russell King36cdc742014-07-08 13:01:44 +01001181static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001182{
1183 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001184 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001185 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001186
Russell King36cdc742014-07-08 13:01:44 +01001187 rtnl_lock();
1188 if (netif_device_present(ndev) || netif_running(ndev)) {
1189 napi_disable(&fep->napi);
1190 netif_tx_lock_bh(ndev);
1191 fec_restart(ndev);
1192 netif_wake_queue(ndev);
1193 netif_tx_unlock_bh(ndev);
1194 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001195 }
Russell King36cdc742014-07-08 13:01:44 +01001196 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001200fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1201 struct skb_shared_hwtstamps *hwtstamps)
1202{
1203 unsigned long flags;
1204 u64 ns;
1205
1206 spin_lock_irqsave(&fep->tmreg_lock, flags);
1207 ns = timecounter_cyc2time(&fep->tc, ts);
1208 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1209
1210 memset(hwtstamps, 0, sizeof(*hwtstamps));
1211 hwtstamps->hwtstamp = ns_to_ktime(ns);
1212}
1213
1214static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001215fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001218 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001219 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001221 struct fec_enet_priv_tx_q *txq;
1222 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001223 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001224 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001226 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001227
1228 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1229
1230 txq = fep->tx_queue[queue_id];
1231 /* get next bdp of dirty_tx */
1232 nq = netdev_get_tx_queue(ndev, queue_id);
1233 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Frank Lide5fb0a2013-03-03 17:34:25 +00001235 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001236 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001237
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001238 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001239
1240 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001241 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001242 break;
1243
Fugang Duan4d494cd2014-09-13 05:00:48 +08001244 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001245
Fugang Duan4d494cd2014-09-13 05:00:48 +08001246 skb = txq->tx_skbuff[index];
1247 txq->tx_skbuff[index] = NULL;
1248 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001249 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1250 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001251 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001252 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001253 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001254 continue;
1255 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001258 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 BD_ENET_TX_RL | BD_ENET_TX_UN |
1260 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001261 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001262 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001263 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001264 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001265 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001266 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001267 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001268 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001269 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001270 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001271 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001273 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001274 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
Frank Liff43da82013-01-03 16:04:23 +00001277 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1278 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001279 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001280 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001281
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001282 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001283 skb_tstamp_tx(skb, &shhwtstamps);
1284 }
Frank Liff43da82013-01-03 16:04:23 +00001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* Deferred means some collisions occurred during transmit,
1287 * but we eventually sent the packet OK.
1288 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001289 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001290 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001291
Sascha Hauer22f6b862009-04-15 01:32:18 +00001292 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001294
Fugang Duan4d494cd2014-09-13 05:00:48 +08001295 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001296
Sascha Hauer22f6b862009-04-15 01:32:18 +00001297 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001298 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001299
Sascha Hauer22f6b862009-04-15 01:32:18 +00001300 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001302 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001303 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1304 if (entries_free >= txq->tx_wake_threshold)
1305 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Russell Kingccea2962014-07-08 13:01:38 +01001308
1309 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001310 if (bdp != txq->cur_tx &&
1311 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1312 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1313}
1314
1315static void
1316fec_enet_tx(struct net_device *ndev)
1317{
1318 struct fec_enet_private *fep = netdev_priv(ndev);
1319 u16 queue_id;
1320 /* First process class A queue, then Class B and Best Effort queue */
1321 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1322 clear_bit(queue_id, &fep->work_tx);
1323 fec_enet_tx_queue(ndev, queue_id);
1324 }
1325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001328static int
1329fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1330{
1331 struct fec_enet_private *fep = netdev_priv(ndev);
1332 int off;
1333
1334 off = ((unsigned long)skb->data) & fep->rx_align;
1335 if (off)
1336 skb_reserve(skb, fep->rx_align + 1 - off);
1337
1338 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1339 FEC_ENET_RX_FRSIZE - fep->rx_align,
1340 DMA_FROM_DEVICE);
1341 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1342 if (net_ratelimit())
1343 netdev_err(ndev, "Rx DMA memory map failed\n");
1344 return -ENOMEM;
1345 }
1346
1347 return 0;
1348}
1349
1350static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1351 struct bufdesc *bdp, u32 length)
1352{
1353 struct fec_enet_private *fep = netdev_priv(ndev);
1354 struct sk_buff *new_skb;
1355
1356 if (length > fep->rx_copybreak)
1357 return false;
1358
1359 new_skb = netdev_alloc_skb(ndev, length);
1360 if (!new_skb)
1361 return false;
1362
1363 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1364 FEC_ENET_RX_FRSIZE - fep->rx_align,
1365 DMA_FROM_DEVICE);
1366 memcpy(new_skb->data, (*skb)->data, length);
1367 *skb = new_skb;
1368
1369 return true;
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/* During a receive, the cur_rx points to the current incoming buffer.
1373 * When we update through the ring, if the next incoming buffer has
1374 * not been given to the system, we just set the empty indicator,
1375 * effectively tossing the packet.
1376 */
Frank Lidc975382013-01-28 18:31:42 +00001377static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001378fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001380 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001381 const struct platform_device_id *id_entry =
1382 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001383 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001384 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001385 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001386 struct sk_buff *skb_new = NULL;
1387 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ushort pkt_len;
1389 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001390 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001391 struct bufdesc_ex *ebdp = NULL;
1392 bool vlan_packet_rcvd = false;
1393 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001394 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001395 bool is_copybreak;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001396
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001397#ifdef CONFIG_M532x
1398 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001399#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001400 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1401 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* First, grab all of the stats for the incoming packet.
1404 * These get messed up if we get called due to a busy condition.
1405 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001406 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Sascha Hauer22f6b862009-04-15 01:32:18 +00001408 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Frank Lidc975382013-01-28 18:31:42 +00001410 if (pkt_received >= budget)
1411 break;
1412 pkt_received++;
1413
Sascha Hauer22f6b862009-04-15 01:32:18 +00001414 /* Since we have allocated space to hold a complete frame,
1415 * the last indicator should be set.
1416 */
1417 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001418 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Russell Kingdb3421c2014-07-08 13:01:49 +01001420
Sascha Hauer22f6b862009-04-15 01:32:18 +00001421 /* Check for errors. */
1422 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001424 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001425 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1426 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001427 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001428 }
1429 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001430 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001431 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001432 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001433 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001434 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001436
1437 /* Report late collisions as a frame error.
1438 * On this error, the BD is closed, but we don't know what we
1439 * have in the buffer. So, just drop this frame on the floor.
1440 */
1441 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001442 ndev->stats.rx_errors++;
1443 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001444 goto rx_processing_done;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Sascha Hauer22f6b862009-04-15 01:32:18 +00001447 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001448 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001449 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001450 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Fugang Duan4d494cd2014-09-13 05:00:48 +08001452 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001453 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001454
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001455 /* The packet length includes FCS, but we don't want to
1456 * include that when passing upstream as it messes up
1457 * bridging applications.
1458 */
1459 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4);
1460 if (!is_copybreak) {
1461 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1462 if (unlikely(!skb_new)) {
1463 ndev->stats.rx_dropped++;
1464 goto rx_processing_done;
1465 }
1466 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1467 FEC_ENET_RX_FRSIZE - fep->rx_align,
1468 DMA_FROM_DEVICE);
1469 }
1470
1471 prefetch(skb->data - NET_IP_ALIGN);
1472 skb_put(skb, pkt_len - 4);
1473 data = skb->data;
Shawn Guob5680e02011-01-05 21:13:13 +00001474 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1475 swap_buffer(data, pkt_len);
1476
Jim Baxtercdffcf12013-07-02 22:52:56 +01001477 /* Extract the enhanced buffer descriptor */
1478 ebdp = NULL;
1479 if (fep->bufdesc_ex)
1480 ebdp = (struct bufdesc_ex *)bdp;
1481
1482 /* If this is a VLAN packet remove the VLAN Tag */
1483 vlan_packet_rcvd = false;
1484 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001485 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001486 /* Push and remove the vlan tag */
1487 struct vlan_hdr *vlan_header =
1488 (struct vlan_hdr *) (data + ETH_HLEN);
1489 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001490
1491 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001492
1493 skb_copy_to_linear_data_offset(skb, VLAN_HLEN,
1494 data, (2 * ETH_ALEN));
1495 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001496 }
1497
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001498 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001500 /* Get receive timestamp from the skb */
1501 if (fep->hwts_rx_en && fep->bufdesc_ex)
1502 fec_enet_hwtstamp(fep, ebdp->ts,
1503 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001504
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001505 if (fep->bufdesc_ex &&
1506 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1507 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1508 /* don't check it */
1509 skb->ip_summed = CHECKSUM_UNNECESSARY;
1510 } else {
1511 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001512 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001513 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001514
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001515 /* Handle received VLAN packets */
1516 if (vlan_packet_rcvd)
1517 __vlan_hwaccel_put_tag(skb,
1518 htons(ETH_P_8021Q),
1519 vlan_tag);
1520
1521 napi_gro_receive(&fep->napi, skb);
1522
1523 if (is_copybreak) {
1524 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1525 FEC_ENET_RX_FRSIZE - fep->rx_align,
1526 DMA_FROM_DEVICE);
1527 } else {
1528 rxq->rx_skbuff[index] = skb_new;
1529 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1530 }
1531
Sascha Hauer22f6b862009-04-15 01:32:18 +00001532rx_processing_done:
1533 /* Clear the status flags for this buffer */
1534 status &= ~BD_ENET_RX_STATS;
1535
1536 /* Mark the buffer empty */
1537 status |= BD_ENET_RX_EMPTY;
1538 bdp->cbd_sc = status;
1539
Frank Liff43da82013-01-03 16:04:23 +00001540 if (fep->bufdesc_ex) {
1541 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1542
1543 ebdp->cbd_esc = BD_ENET_RX_INT;
1544 ebdp->cbd_prot = 0;
1545 ebdp->cbd_bdu = 0;
1546 }
Frank Li6605b732012-10-30 18:25:31 +00001547
Sascha Hauer22f6b862009-04-15 01:32:18 +00001548 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001549 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001550
Sascha Hauer22f6b862009-04-15 01:32:18 +00001551 /* Doing this here will keep the FEC running while we process
1552 * incoming frames. On a heavily loaded network, we should be
1553 * able to keep up at the expense of system resources.
1554 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001555 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001557 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001558 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Fugang Duan4d494cd2014-09-13 05:00:48 +08001561static int
1562fec_enet_rx(struct net_device *ndev, int budget)
1563{
1564 int pkt_received = 0;
1565 u16 queue_id;
1566 struct fec_enet_private *fep = netdev_priv(ndev);
1567
1568 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1569 clear_bit(queue_id, &fep->work_rx);
1570 pkt_received += fec_enet_rx_queue(ndev,
1571 budget - pkt_received, queue_id);
1572 }
1573 return pkt_received;
1574}
1575
1576static bool
1577fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1578{
1579 if (int_events == 0)
1580 return false;
1581
1582 if (int_events & FEC_ENET_RXF)
1583 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001584 if (int_events & FEC_ENET_RXF_1)
1585 fep->work_rx |= (1 << 0);
1586 if (int_events & FEC_ENET_RXF_2)
1587 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001588
1589 if (int_events & FEC_ENET_TXF)
1590 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001591 if (int_events & FEC_ENET_TXF_1)
1592 fep->work_tx |= (1 << 0);
1593 if (int_events & FEC_ENET_TXF_2)
1594 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001595
1596 return true;
1597}
1598
Uwe Kleine-König45993652011-01-19 20:47:04 +01001599static irqreturn_t
1600fec_enet_interrupt(int irq, void *dev_id)
1601{
1602 struct net_device *ndev = dev_id;
1603 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001604 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001605 uint int_events;
1606 irqreturn_t ret = IRQ_NONE;
1607
Russell King7a168072014-07-08 00:22:44 +01001608 int_events = readl(fep->hwp + FEC_IEVENT);
1609 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001610 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001611
Russell King7a168072014-07-08 00:22:44 +01001612 if (int_events & napi_mask) {
1613 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001614
Russell King7a168072014-07-08 00:22:44 +01001615 /* Disable the NAPI interrupts */
1616 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1617 napi_schedule(&fep->napi);
1618 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001619
Russell King7a168072014-07-08 00:22:44 +01001620 if (int_events & FEC_ENET_MII) {
1621 ret = IRQ_HANDLED;
1622 complete(&fep->mdio_done);
1623 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001624
1625 return ret;
1626}
1627
Frank Lidc975382013-01-28 18:31:42 +00001628static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1629{
1630 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001631 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001632 int pkts;
1633
1634 /*
1635 * Clear any pending transmit or receive interrupts before
1636 * processing the rings to avoid racing with the hardware.
1637 */
1638 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1639
1640 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001641
Frank Lide5fb0a2013-03-03 17:34:25 +00001642 fec_enet_tx(ndev);
1643
Frank Lidc975382013-01-28 18:31:42 +00001644 if (pkts < budget) {
1645 napi_complete(napi);
1646 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1647 }
1648 return pkts;
1649}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001652static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001654 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001655 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001656 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Shawn Guo49da97d2011-01-05 21:13:11 +00001658 /*
1659 * try to get mac address in following order:
1660 *
1661 * 1) module parameter via kernel command line in form
1662 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1663 */
1664 iap = macaddr;
1665
1666 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001667 * 2) from device tree data
1668 */
1669 if (!is_valid_ether_addr(iap)) {
1670 struct device_node *np = fep->pdev->dev.of_node;
1671 if (np) {
1672 const char *mac = of_get_mac_address(np);
1673 if (mac)
1674 iap = (unsigned char *) mac;
1675 }
1676 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001677
1678 /*
1679 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001680 */
1681 if (!is_valid_ether_addr(iap)) {
1682#ifdef CONFIG_M5272
1683 if (FEC_FLASHMAC)
1684 iap = (unsigned char *)FEC_FLASHMAC;
1685#else
1686 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001687 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001688#endif
1689 }
1690
1691 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001692 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001693 */
1694 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001695 *((__be32 *) &tmpaddr[0]) =
1696 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1697 *((__be16 *) &tmpaddr[4]) =
1698 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 iap = &tmpaddr[0];
1700 }
1701
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001702 /*
1703 * 5) random mac address
1704 */
1705 if (!is_valid_ether_addr(iap)) {
1706 /* Report it and use a random ethernet address instead */
1707 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1708 eth_hw_addr_random(ndev);
1709 netdev_info(ndev, "Using random MAC address: %pM\n",
1710 ndev->dev_addr);
1711 return;
1712 }
1713
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001714 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Shawn Guo49da97d2011-01-05 21:13:11 +00001716 /* Adjust MAC if using macaddr */
1717 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001718 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721/* ------------------------------------------------------------------------- */
1722
Bryan Wue6b043d2010-03-31 02:10:44 +00001723/*
1724 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001726static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001728 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001729 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001730 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Bryan Wue6b043d2010-03-31 02:10:44 +00001732 /* Prevent a state halted on mii error */
1733 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1734 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001735 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001737
Russell King8ce56242014-07-08 12:40:07 +01001738 /*
1739 * If the netdev is down, or is going down, we're not interested
1740 * in link state events, so just mark our idea of the link as down
1741 * and ignore the event.
1742 */
1743 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1744 fep->link = 0;
1745 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001746 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001747 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001748 status_change = 1;
1749 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001750
Russell Kingef833372014-07-08 12:40:43 +01001751 if (fep->full_duplex != phy_dev->duplex) {
1752 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001753 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001754 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001755
1756 if (phy_dev->speed != fep->speed) {
1757 fep->speed = phy_dev->speed;
1758 status_change = 1;
1759 }
1760
1761 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001762 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001763 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001764 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001765 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001766 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001767 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001768 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001769 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001770 } else {
1771 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001772 napi_disable(&fep->napi);
1773 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001774 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001775 netif_tx_unlock_bh(ndev);
1776 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001777 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001778 status_change = 1;
1779 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001780 }
1781
Bryan Wue6b043d2010-03-31 02:10:44 +00001782 if (status_change)
1783 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Bryan Wue6b043d2010-03-31 02:10:44 +00001786static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Bryan Wue6b043d2010-03-31 02:10:44 +00001788 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001789 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001790
1791 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001792 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001793
1794 /* start a read op */
1795 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1796 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1797 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1798
1799 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001800 time_left = wait_for_completion_timeout(&fep->mdio_done,
1801 usecs_to_jiffies(FEC_MII_TIMEOUT));
1802 if (time_left == 0) {
1803 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001804 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001805 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001806 }
1807
1808 /* return value */
1809 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1810}
1811
1812static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1813 u16 value)
1814{
1815 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001816 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001817
1818 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001819 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001820
Shawn Guo862f0982011-01-05 21:13:09 +00001821 /* start a write op */
1822 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001823 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1824 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1825 fep->hwp + FEC_MII_DATA);
1826
1827 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001828 time_left = wait_for_completion_timeout(&fep->mdio_done,
1829 usecs_to_jiffies(FEC_MII_TIMEOUT));
1830 if (time_left == 0) {
1831 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001832 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001833 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001834 }
1835
1836 return 0;
1837}
1838
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001839static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1840{
1841 struct fec_enet_private *fep = netdev_priv(ndev);
1842 int ret;
1843
1844 if (enable) {
1845 ret = clk_prepare_enable(fep->clk_ahb);
1846 if (ret)
1847 return ret;
1848 ret = clk_prepare_enable(fep->clk_ipg);
1849 if (ret)
1850 goto failed_clk_ipg;
1851 if (fep->clk_enet_out) {
1852 ret = clk_prepare_enable(fep->clk_enet_out);
1853 if (ret)
1854 goto failed_clk_enet_out;
1855 }
1856 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001857 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001858 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001859 if (ret) {
1860 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001861 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001862 } else {
1863 fep->ptp_clk_on = true;
1864 }
1865 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001866 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001867 if (fep->clk_ref) {
1868 ret = clk_prepare_enable(fep->clk_ref);
1869 if (ret)
1870 goto failed_clk_ref;
1871 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001872 } else {
1873 clk_disable_unprepare(fep->clk_ahb);
1874 clk_disable_unprepare(fep->clk_ipg);
1875 if (fep->clk_enet_out)
1876 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001877 if (fep->clk_ptp) {
1878 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001879 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001880 fep->ptp_clk_on = false;
1881 mutex_unlock(&fep->ptp_clk_mutex);
1882 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001883 if (fep->clk_ref)
1884 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001885 }
1886
1887 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001888
1889failed_clk_ref:
1890 if (fep->clk_ref)
1891 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001892failed_clk_ptp:
1893 if (fep->clk_enet_out)
1894 clk_disable_unprepare(fep->clk_enet_out);
1895failed_clk_enet_out:
1896 clk_disable_unprepare(fep->clk_ipg);
1897failed_clk_ipg:
1898 clk_disable_unprepare(fep->clk_ahb);
1899
1900 return ret;
1901}
1902
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001903static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001904{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001905 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001906 const struct platform_device_id *id_entry =
1907 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001908 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001909 char mdio_bus_id[MII_BUS_ID_SIZE];
1910 char phy_name[MII_BUS_ID_SIZE + 3];
1911 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001912 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001913
Bryan Wu418bd0d2010-05-28 03:40:39 -07001914 fep->phy_dev = NULL;
1915
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001916 if (fep->phy_node) {
1917 phy_dev = of_phy_connect(ndev, fep->phy_node,
1918 &fec_enet_adjust_link, 0,
1919 fep->phy_interface);
1920 } else {
1921 /* check for attached phy */
1922 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1923 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1924 continue;
1925 if (fep->mii_bus->phy_map[phy_id] == NULL)
1926 continue;
1927 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1928 continue;
1929 if (dev_id--)
1930 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001931 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001932 break;
1933 }
1934
1935 if (phy_id >= PHY_MAX_ADDR) {
1936 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001937 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001938 phy_id = 0;
1939 }
1940
1941 snprintf(phy_name, sizeof(phy_name),
1942 PHY_ID_FMT, mdio_bus_id, phy_id);
1943 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1944 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001945 }
1946
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001947 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001948 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001949 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001950 }
1951
1952 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001953 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001954 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001955 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001956#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001957 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001958#endif
Frank Libaa70a52013-01-16 16:55:58 +00001959 }
Shawn Guo230dec62011-09-23 02:12:48 +00001960 else
1961 phy_dev->supported &= PHY_BASIC_FEATURES;
1962
Bryan Wue6b043d2010-03-31 02:10:44 +00001963 phy_dev->advertising = phy_dev->supported;
1964
1965 fep->phy_dev = phy_dev;
1966 fep->link = 0;
1967 fep->full_duplex = 0;
1968
Joe Perches31b77202013-04-13 19:03:17 +00001969 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1970 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1971 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001972
Bryan Wue6b043d2010-03-31 02:10:44 +00001973 return 0;
1974}
1975
1976static int fec_enet_mii_init(struct platform_device *pdev)
1977{
Shawn Guob5680e02011-01-05 21:13:13 +00001978 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001979 struct net_device *ndev = platform_get_drvdata(pdev);
1980 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001981 const struct platform_device_id *id_entry =
1982 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001983 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001984 int err = -ENXIO, i;
1985
Shawn Guob5680e02011-01-05 21:13:13 +00001986 /*
1987 * The dual fec interfaces are not equivalent with enet-mac.
1988 * Here are the differences:
1989 *
1990 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1991 * - fec0 acts as the 1588 time master while fec1 is slave
1992 * - external phys can only be configured by fec0
1993 *
1994 * That is to say fec1 can not work independently. It only works
1995 * when fec0 is working. The reason behind this design is that the
1996 * second interface is added primarily for Switch mode.
1997 *
1998 * Because of the last point above, both phys are attached on fec0
1999 * mdio interface in board design, and need to be configured by
2000 * fec0 mii_bus.
2001 */
Shawn Guo43af9402011-12-05 05:01:15 +00002002 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00002003 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002004 if (mii_cnt && fec0_mii_bus) {
2005 fep->mii_bus = fec0_mii_bus;
2006 mii_cnt++;
2007 return 0;
2008 }
2009 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00002010 }
2011
Bryan Wue6b043d2010-03-31 02:10:44 +00002012 fep->mii_timeout = 0;
2013
2014 /*
2015 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002016 *
2017 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2018 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2019 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2020 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002021 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08002022 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00002023 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2024 fep->phy_speed--;
2025 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00002026 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2027
2028 fep->mii_bus = mdiobus_alloc();
2029 if (fep->mii_bus == NULL) {
2030 err = -ENOMEM;
2031 goto err_out;
2032 }
2033
2034 fep->mii_bus->name = "fec_enet_mii_bus";
2035 fep->mii_bus->read = fec_enet_mdio_read;
2036 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002037 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2038 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002039 fep->mii_bus->priv = fep;
2040 fep->mii_bus->parent = &pdev->dev;
2041
2042 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2043 if (!fep->mii_bus->irq) {
2044 err = -ENOMEM;
2045 goto err_out_free_mdiobus;
2046 }
2047
2048 for (i = 0; i < PHY_MAX_ADDR; i++)
2049 fep->mii_bus->irq[i] = PHY_POLL;
2050
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002051 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2052 if (node) {
2053 err = of_mdiobus_register(fep->mii_bus, node);
2054 of_node_put(node);
2055 } else {
2056 err = mdiobus_register(fep->mii_bus);
2057 }
2058
2059 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002060 goto err_out_free_mdio_irq;
2061
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002062 mii_cnt++;
2063
Shawn Guob5680e02011-01-05 21:13:13 +00002064 /* save fec0 mii_bus */
2065 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2066 fec0_mii_bus = fep->mii_bus;
2067
Bryan Wue6b043d2010-03-31 02:10:44 +00002068 return 0;
2069
Bryan Wue6b043d2010-03-31 02:10:44 +00002070err_out_free_mdio_irq:
2071 kfree(fep->mii_bus->irq);
2072err_out_free_mdiobus:
2073 mdiobus_free(fep->mii_bus);
2074err_out:
2075 return err;
2076}
2077
2078static void fec_enet_mii_remove(struct fec_enet_private *fep)
2079{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002080 if (--mii_cnt == 0) {
2081 mdiobus_unregister(fep->mii_bus);
2082 kfree(fep->mii_bus->irq);
2083 mdiobus_free(fep->mii_bus);
2084 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002085}
2086
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002087static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002088 struct ethtool_cmd *cmd)
2089{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002090 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002091 struct phy_device *phydev = fep->phy_dev;
2092
2093 if (!phydev)
2094 return -ENODEV;
2095
2096 return phy_ethtool_gset(phydev, cmd);
2097}
2098
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002099static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002100 struct ethtool_cmd *cmd)
2101{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002102 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002103 struct phy_device *phydev = fep->phy_dev;
2104
2105 if (!phydev)
2106 return -ENODEV;
2107
2108 return phy_ethtool_sset(phydev, cmd);
2109}
2110
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002111static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002112 struct ethtool_drvinfo *info)
2113{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002114 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Jiri Pirko7826d432013-01-06 00:44:26 +00002116 strlcpy(info->driver, fep->pdev->dev.driver->name,
2117 sizeof(info->driver));
2118 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2119 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
Bryan Wue6b043d2010-03-31 02:10:44 +00002121
Frank Li5ebae4892013-01-06 16:25:07 +00002122static int fec_enet_get_ts_info(struct net_device *ndev,
2123 struct ethtool_ts_info *info)
2124{
2125 struct fec_enet_private *fep = netdev_priv(ndev);
2126
2127 if (fep->bufdesc_ex) {
2128
2129 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2130 SOF_TIMESTAMPING_RX_SOFTWARE |
2131 SOF_TIMESTAMPING_SOFTWARE |
2132 SOF_TIMESTAMPING_TX_HARDWARE |
2133 SOF_TIMESTAMPING_RX_HARDWARE |
2134 SOF_TIMESTAMPING_RAW_HARDWARE;
2135 if (fep->ptp_clock)
2136 info->phc_index = ptp_clock_index(fep->ptp_clock);
2137 else
2138 info->phc_index = -1;
2139
2140 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2141 (1 << HWTSTAMP_TX_ON);
2142
2143 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2144 (1 << HWTSTAMP_FILTER_ALL);
2145 return 0;
2146 } else {
2147 return ethtool_op_get_ts_info(ndev, info);
2148 }
2149}
2150
Guenter Roeckd1391932013-06-18 10:04:59 -07002151#if !defined(CONFIG_M5272)
2152
Frank Libaa70a52013-01-16 16:55:58 +00002153static void fec_enet_get_pauseparam(struct net_device *ndev,
2154 struct ethtool_pauseparam *pause)
2155{
2156 struct fec_enet_private *fep = netdev_priv(ndev);
2157
2158 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2159 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2160 pause->rx_pause = pause->tx_pause;
2161}
2162
2163static int fec_enet_set_pauseparam(struct net_device *ndev,
2164 struct ethtool_pauseparam *pause)
2165{
2166 struct fec_enet_private *fep = netdev_priv(ndev);
2167
Russell King0b146ca2014-07-08 00:22:59 +01002168 if (!fep->phy_dev)
2169 return -ENODEV;
2170
Frank Libaa70a52013-01-16 16:55:58 +00002171 if (pause->tx_pause != pause->rx_pause) {
2172 netdev_info(ndev,
2173 "hardware only support enable/disable both tx and rx");
2174 return -EINVAL;
2175 }
2176
2177 fep->pause_flag = 0;
2178
2179 /* tx pause must be same as rx pause */
2180 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2181 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2182
2183 if (pause->rx_pause || pause->autoneg) {
2184 fep->phy_dev->supported |= ADVERTISED_Pause;
2185 fep->phy_dev->advertising |= ADVERTISED_Pause;
2186 } else {
2187 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2188 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2189 }
2190
2191 if (pause->autoneg) {
2192 if (netif_running(ndev))
2193 fec_stop(ndev);
2194 phy_start_aneg(fep->phy_dev);
2195 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002196 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002197 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002198 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002199 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002200 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002201 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002202 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002203 }
Frank Libaa70a52013-01-16 16:55:58 +00002204
2205 return 0;
2206}
2207
Chris Healy38ae92d2013-06-25 23:18:52 -07002208static const struct fec_stat {
2209 char name[ETH_GSTRING_LEN];
2210 u16 offset;
2211} fec_stats[] = {
2212 /* RMON TX */
2213 { "tx_dropped", RMON_T_DROP },
2214 { "tx_packets", RMON_T_PACKETS },
2215 { "tx_broadcast", RMON_T_BC_PKT },
2216 { "tx_multicast", RMON_T_MC_PKT },
2217 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2218 { "tx_undersize", RMON_T_UNDERSIZE },
2219 { "tx_oversize", RMON_T_OVERSIZE },
2220 { "tx_fragment", RMON_T_FRAG },
2221 { "tx_jabber", RMON_T_JAB },
2222 { "tx_collision", RMON_T_COL },
2223 { "tx_64byte", RMON_T_P64 },
2224 { "tx_65to127byte", RMON_T_P65TO127 },
2225 { "tx_128to255byte", RMON_T_P128TO255 },
2226 { "tx_256to511byte", RMON_T_P256TO511 },
2227 { "tx_512to1023byte", RMON_T_P512TO1023 },
2228 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2229 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2230 { "tx_octets", RMON_T_OCTETS },
2231
2232 /* IEEE TX */
2233 { "IEEE_tx_drop", IEEE_T_DROP },
2234 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2235 { "IEEE_tx_1col", IEEE_T_1COL },
2236 { "IEEE_tx_mcol", IEEE_T_MCOL },
2237 { "IEEE_tx_def", IEEE_T_DEF },
2238 { "IEEE_tx_lcol", IEEE_T_LCOL },
2239 { "IEEE_tx_excol", IEEE_T_EXCOL },
2240 { "IEEE_tx_macerr", IEEE_T_MACERR },
2241 { "IEEE_tx_cserr", IEEE_T_CSERR },
2242 { "IEEE_tx_sqe", IEEE_T_SQE },
2243 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2244 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2245
2246 /* RMON RX */
2247 { "rx_packets", RMON_R_PACKETS },
2248 { "rx_broadcast", RMON_R_BC_PKT },
2249 { "rx_multicast", RMON_R_MC_PKT },
2250 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2251 { "rx_undersize", RMON_R_UNDERSIZE },
2252 { "rx_oversize", RMON_R_OVERSIZE },
2253 { "rx_fragment", RMON_R_FRAG },
2254 { "rx_jabber", RMON_R_JAB },
2255 { "rx_64byte", RMON_R_P64 },
2256 { "rx_65to127byte", RMON_R_P65TO127 },
2257 { "rx_128to255byte", RMON_R_P128TO255 },
2258 { "rx_256to511byte", RMON_R_P256TO511 },
2259 { "rx_512to1023byte", RMON_R_P512TO1023 },
2260 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2261 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2262 { "rx_octets", RMON_R_OCTETS },
2263
2264 /* IEEE RX */
2265 { "IEEE_rx_drop", IEEE_R_DROP },
2266 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2267 { "IEEE_rx_crc", IEEE_R_CRC },
2268 { "IEEE_rx_align", IEEE_R_ALIGN },
2269 { "IEEE_rx_macerr", IEEE_R_MACERR },
2270 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2271 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2272};
2273
2274static void fec_enet_get_ethtool_stats(struct net_device *dev,
2275 struct ethtool_stats *stats, u64 *data)
2276{
2277 struct fec_enet_private *fep = netdev_priv(dev);
2278 int i;
2279
2280 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2281 data[i] = readl(fep->hwp + fec_stats[i].offset);
2282}
2283
2284static void fec_enet_get_strings(struct net_device *netdev,
2285 u32 stringset, u8 *data)
2286{
2287 int i;
2288 switch (stringset) {
2289 case ETH_SS_STATS:
2290 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2291 memcpy(data + i * ETH_GSTRING_LEN,
2292 fec_stats[i].name, ETH_GSTRING_LEN);
2293 break;
2294 }
2295}
2296
2297static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2298{
2299 switch (sset) {
2300 case ETH_SS_STATS:
2301 return ARRAY_SIZE(fec_stats);
2302 default:
2303 return -EOPNOTSUPP;
2304 }
2305}
Guenter Roeckd1391932013-06-18 10:04:59 -07002306#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002307
Chris Healy32bc9b42013-06-17 07:25:06 -07002308static int fec_enet_nway_reset(struct net_device *dev)
2309{
2310 struct fec_enet_private *fep = netdev_priv(dev);
2311 struct phy_device *phydev = fep->phy_dev;
2312
2313 if (!phydev)
2314 return -ENODEV;
2315
2316 return genphy_restart_aneg(phydev);
2317}
2318
Fugang Duand851b472014-09-17 05:18:52 +08002319/* ITR clock source is enet system clock (clk_ahb).
2320 * TCTT unit is cycle_ns * 64 cycle
2321 * So, the ICTT value = X us / (cycle_ns * 64)
2322 */
2323static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2324{
2325 struct fec_enet_private *fep = netdev_priv(ndev);
2326
2327 return us * (fep->itr_clk_rate / 64000) / 1000;
2328}
2329
2330/* Set threshold for interrupt coalescing */
2331static void fec_enet_itr_coal_set(struct net_device *ndev)
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 int rx_itr, tx_itr;
2337
2338 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2339 return;
2340
2341 /* Must be greater than zero to avoid unpredictable behavior */
2342 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2343 !fep->tx_time_itr || !fep->tx_pkts_itr)
2344 return;
2345
2346 /* Select enet system clock as Interrupt Coalescing
2347 * timer Clock Source
2348 */
2349 rx_itr = FEC_ITR_CLK_SEL;
2350 tx_itr = FEC_ITR_CLK_SEL;
2351
2352 /* set ICFT and ICTT */
2353 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2354 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2355 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2356 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2357
2358 rx_itr |= FEC_ITR_EN;
2359 tx_itr |= FEC_ITR_EN;
2360
2361 writel(tx_itr, fep->hwp + FEC_TXIC0);
2362 writel(rx_itr, fep->hwp + FEC_RXIC0);
2363 writel(tx_itr, fep->hwp + FEC_TXIC1);
2364 writel(rx_itr, fep->hwp + FEC_RXIC1);
2365 writel(tx_itr, fep->hwp + FEC_TXIC2);
2366 writel(rx_itr, fep->hwp + FEC_RXIC2);
2367}
2368
2369static int
2370fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2371{
2372 struct fec_enet_private *fep = netdev_priv(ndev);
2373 const struct platform_device_id *id_entry =
2374 platform_get_device_id(fep->pdev);
2375
2376 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2377 return -EOPNOTSUPP;
2378
2379 ec->rx_coalesce_usecs = fep->rx_time_itr;
2380 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2381
2382 ec->tx_coalesce_usecs = fep->tx_time_itr;
2383 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2384
2385 return 0;
2386}
2387
2388static int
2389fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2390{
2391 struct fec_enet_private *fep = netdev_priv(ndev);
2392 const struct platform_device_id *id_entry =
2393 platform_get_device_id(fep->pdev);
2394
2395 unsigned int cycle;
2396
2397 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2398 return -EOPNOTSUPP;
2399
2400 if (ec->rx_max_coalesced_frames > 255) {
2401 pr_err("Rx coalesced frames exceed hardware limiation");
2402 return -EINVAL;
2403 }
2404
2405 if (ec->tx_max_coalesced_frames > 255) {
2406 pr_err("Tx coalesced frame exceed hardware limiation");
2407 return -EINVAL;
2408 }
2409
2410 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2411 if (cycle > 0xFFFF) {
2412 pr_err("Rx coalesed usec exceeed hardware limiation");
2413 return -EINVAL;
2414 }
2415
2416 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2417 if (cycle > 0xFFFF) {
2418 pr_err("Rx coalesed usec exceeed hardware limiation");
2419 return -EINVAL;
2420 }
2421
2422 fep->rx_time_itr = ec->rx_coalesce_usecs;
2423 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2424
2425 fep->tx_time_itr = ec->tx_coalesce_usecs;
2426 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2427
2428 fec_enet_itr_coal_set(ndev);
2429
2430 return 0;
2431}
2432
2433static void fec_enet_itr_coal_init(struct net_device *ndev)
2434{
2435 struct ethtool_coalesce ec;
2436
2437 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2438 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2439
2440 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2441 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2442
2443 fec_enet_set_coalesce(ndev, &ec);
2444}
2445
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002446static int fec_enet_get_tunable(struct net_device *netdev,
2447 const struct ethtool_tunable *tuna,
2448 void *data)
2449{
2450 struct fec_enet_private *fep = netdev_priv(netdev);
2451 int ret = 0;
2452
2453 switch (tuna->id) {
2454 case ETHTOOL_RX_COPYBREAK:
2455 *(u32 *)data = fep->rx_copybreak;
2456 break;
2457 default:
2458 ret = -EINVAL;
2459 break;
2460 }
2461
2462 return ret;
2463}
2464
2465static int fec_enet_set_tunable(struct net_device *netdev,
2466 const struct ethtool_tunable *tuna,
2467 const void *data)
2468{
2469 struct fec_enet_private *fep = netdev_priv(netdev);
2470 int ret = 0;
2471
2472 switch (tuna->id) {
2473 case ETHTOOL_RX_COPYBREAK:
2474 fep->rx_copybreak = *(u32 *)data;
2475 break;
2476 default:
2477 ret = -EINVAL;
2478 break;
2479 }
2480
2481 return ret;
2482}
2483
stephen hemminger9b07be42012-01-04 12:59:49 +00002484static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002485 .get_settings = fec_enet_get_settings,
2486 .set_settings = fec_enet_set_settings,
2487 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002488 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002489 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002490 .get_coalesce = fec_enet_get_coalesce,
2491 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002492#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002493 .get_pauseparam = fec_enet_get_pauseparam,
2494 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002495 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002496 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002497 .get_sset_count = fec_enet_get_sset_count,
2498#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002499 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002500 .get_tunable = fec_enet_get_tunable,
2501 .set_tunable = fec_enet_set_tunable,
Bryan Wue6b043d2010-03-31 02:10:44 +00002502};
2503
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002504static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002505{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002506 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002507 struct phy_device *phydev = fep->phy_dev;
2508
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002509 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002510 return -EINVAL;
2511
2512 if (!phydev)
2513 return -ENODEV;
2514
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002515 if (fep->bufdesc_ex) {
2516 if (cmd == SIOCSHWTSTAMP)
2517 return fec_ptp_set(ndev, rq);
2518 if (cmd == SIOCGHWTSTAMP)
2519 return fec_ptp_get(ndev, rq);
2520 }
Frank Liff43da82013-01-03 16:04:23 +00002521
Richard Cochran28b04112010-07-17 08:48:55 +00002522 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002525static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002526{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002527 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002528 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002529 struct sk_buff *skb;
2530 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002531 struct fec_enet_priv_tx_q *txq;
2532 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002533 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002534
Frank Li59d0f7462014-09-13 05:00:50 +08002535 for (q = 0; q < fep->num_rx_queues; q++) {
2536 rxq = fep->rx_queue[q];
2537 bdp = rxq->rx_bd_base;
2538 for (i = 0; i < rxq->rx_ring_size; i++) {
2539 skb = rxq->rx_skbuff[i];
2540 rxq->rx_skbuff[i] = NULL;
2541 if (skb) {
2542 dma_unmap_single(&fep->pdev->dev,
2543 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002544 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002545 DMA_FROM_DEVICE);
2546 dev_kfree_skb(skb);
2547 }
2548 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002549 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002550 }
2551
Frank Li59d0f7462014-09-13 05:00:50 +08002552 for (q = 0; q < fep->num_tx_queues; q++) {
2553 txq = fep->tx_queue[q];
2554 bdp = txq->tx_bd_base;
2555 for (i = 0; i < txq->tx_ring_size; i++) {
2556 kfree(txq->tx_bounce[i]);
2557 txq->tx_bounce[i] = NULL;
2558 skb = txq->tx_skbuff[i];
2559 txq->tx_skbuff[i] = NULL;
2560 dev_kfree_skb(skb);
2561 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002562 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002563}
2564
Frank Li59d0f7462014-09-13 05:00:50 +08002565static void fec_enet_free_queue(struct net_device *ndev)
2566{
2567 struct fec_enet_private *fep = netdev_priv(ndev);
2568 int i;
2569 struct fec_enet_priv_tx_q *txq;
2570
2571 for (i = 0; i < fep->num_tx_queues; i++)
2572 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2573 txq = fep->tx_queue[i];
2574 dma_free_coherent(NULL,
2575 txq->tx_ring_size * TSO_HEADER_SIZE,
2576 txq->tso_hdrs,
2577 txq->tso_hdrs_dma);
2578 }
2579
2580 for (i = 0; i < fep->num_rx_queues; i++)
2581 if (fep->rx_queue[i])
2582 kfree(fep->rx_queue[i]);
2583
2584 for (i = 0; i < fep->num_tx_queues; i++)
2585 if (fep->tx_queue[i])
2586 kfree(fep->tx_queue[i]);
2587}
2588
2589static int fec_enet_alloc_queue(struct net_device *ndev)
2590{
2591 struct fec_enet_private *fep = netdev_priv(ndev);
2592 int i;
2593 int ret = 0;
2594 struct fec_enet_priv_tx_q *txq;
2595
2596 for (i = 0; i < fep->num_tx_queues; i++) {
2597 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2598 if (!txq) {
2599 ret = -ENOMEM;
2600 goto alloc_failed;
2601 }
2602
2603 fep->tx_queue[i] = txq;
2604 txq->tx_ring_size = TX_RING_SIZE;
2605 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2606
2607 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2608 txq->tx_wake_threshold =
2609 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2610
2611 txq->tso_hdrs = dma_alloc_coherent(NULL,
2612 txq->tx_ring_size * TSO_HEADER_SIZE,
2613 &txq->tso_hdrs_dma,
2614 GFP_KERNEL);
2615 if (!txq->tso_hdrs) {
2616 ret = -ENOMEM;
2617 goto alloc_failed;
2618 }
2619 }
2620
2621 for (i = 0; i < fep->num_rx_queues; i++) {
2622 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2623 GFP_KERNEL);
2624 if (!fep->rx_queue[i]) {
2625 ret = -ENOMEM;
2626 goto alloc_failed;
2627 }
2628
2629 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2630 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2631 }
2632 return ret;
2633
2634alloc_failed:
2635 fec_enet_free_queue(ndev);
2636 return ret;
2637}
2638
2639static int
2640fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002641{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002642 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002643 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002644 struct sk_buff *skb;
2645 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002646 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002647
Frank Li59d0f7462014-09-13 05:00:50 +08002648 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002649 bdp = rxq->rx_bd_base;
2650 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002651 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002652 if (!skb)
2653 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002654
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002655 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002656 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002657 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002658 }
Russell King730ee362014-07-08 00:23:14 +01002659
Fugang Duan4d494cd2014-09-13 05:00:48 +08002660 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002661 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002662
2663 if (fep->bufdesc_ex) {
2664 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2665 ebdp->cbd_esc = BD_ENET_RX_INT;
2666 }
2667
Frank Li59d0f7462014-09-13 05:00:50 +08002668 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002669 }
2670
2671 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002672 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002673 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002674 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002675
Frank Li59d0f7462014-09-13 05:00:50 +08002676 err_alloc:
2677 fec_enet_free_buffers(ndev);
2678 return -ENOMEM;
2679}
2680
2681static int
2682fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2683{
2684 struct fec_enet_private *fep = netdev_priv(ndev);
2685 unsigned int i;
2686 struct bufdesc *bdp;
2687 struct fec_enet_priv_tx_q *txq;
2688
2689 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002690 bdp = txq->tx_bd_base;
2691 for (i = 0; i < txq->tx_ring_size; i++) {
2692 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2693 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002694 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002695
2696 bdp->cbd_sc = 0;
2697 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002698
Frank Liff43da82013-01-03 16:04:23 +00002699 if (fep->bufdesc_ex) {
2700 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002701 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002702 }
2703
Frank Li59d0f7462014-09-13 05:00:50 +08002704 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002705 }
2706
2707 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002708 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002709 bdp->cbd_sc |= BD_SC_WRAP;
2710
2711 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002712
2713 err_alloc:
2714 fec_enet_free_buffers(ndev);
2715 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002716}
2717
Frank Li59d0f7462014-09-13 05:00:50 +08002718static int fec_enet_alloc_buffers(struct net_device *ndev)
2719{
2720 struct fec_enet_private *fep = netdev_priv(ndev);
2721 unsigned int i;
2722
2723 for (i = 0; i < fep->num_rx_queues; i++)
2724 if (fec_enet_alloc_rxq_buffers(ndev, i))
2725 return -ENOMEM;
2726
2727 for (i = 0; i < fep->num_tx_queues; i++)
2728 if (fec_enet_alloc_txq_buffers(ndev, i))
2729 return -ENOMEM;
2730 return 0;
2731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002734fec_enet_open(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);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002737 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002739 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002740 ret = fec_enet_clk_enable(ndev, true);
2741 if (ret)
2742 return ret;
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 /* I should reset the ring buffers here, but I don't yet know
2745 * a simple way to do that.
2746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002748 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002749 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002750 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002751
Bryan Wu418bd0d2010-05-28 03:40:39 -07002752 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002753 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002754 if (ret)
2755 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002756
Russell Kingef833372014-07-08 12:40:43 +01002757 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002758 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002759 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002760 netif_tx_start_all_queues(ndev);
2761
Sascha Hauer22f6b862009-04-15 01:32:18 +00002762 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002763
2764err_enet_mii_probe:
2765 fec_enet_free_buffers(ndev);
2766err_enet_alloc:
2767 fec_enet_clk_enable(ndev, false);
2768 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
2772static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002773fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002775 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Russell Kingd76cfae2014-07-08 00:23:04 +01002777 phy_stop(fep->phy_dev);
2778
Russell King31a6de32014-07-08 12:40:23 +01002779 if (netif_device_present(ndev)) {
2780 napi_disable(&fep->napi);
2781 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002782 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Russell King635cf172014-07-08 00:22:54 +01002785 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002786 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002787
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002788 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002789 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002790 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
2793}
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795/* Set or clear the multicast filter for this adaptor.
2796 * Skeleton taken from sunlance driver.
2797 * The CPM Ethernet implementation allows Multicast as well as individual
2798 * MAC address filtering. Some of the drivers check to make sure it is
2799 * a group multicast address, and discard those that are not. I guess I
2800 * will do the same for now, but just remove the test if you want
2801 * individual filtering as well (do the upper net layers want or support
2802 * this kind of feature?).
2803 */
2804
2805#define HASH_BITS 6 /* #bits in hash */
2806#define CRC32_POLY 0xEDB88320
2807
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002808static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002810 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002811 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002812 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 unsigned char hash;
2814
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002815 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002816 tmp = readl(fep->hwp + FEC_R_CNTRL);
2817 tmp |= 0x8;
2818 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002819 return;
2820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Sascha Hauer4e831832009-04-15 01:32:19 +00002822 tmp = readl(fep->hwp + FEC_R_CNTRL);
2823 tmp &= ~0x8;
2824 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002825
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002826 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002827 /* Catch all multicast addresses, so set the
2828 * filter to all 1's
2829 */
2830 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2831 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Sascha Hauer4e831832009-04-15 01:32:19 +00002833 return;
2834 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002835
Sascha Hauer4e831832009-04-15 01:32:19 +00002836 /* Clear filter and add the addresses in hash register
2837 */
2838 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2839 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002841 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002842 /* calculate crc32 value of mac address */
2843 crc = 0xffffffff;
2844
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002845 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002846 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002847 for (bit = 0; bit < 8; bit++, data >>= 1) {
2848 crc = (crc >> 1) ^
2849 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002852
2853 /* only upper 6 bits (HASH_BITS) are used
2854 * which point to specific bit in he hash registers
2855 */
2856 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2857
2858 if (hash > 31) {
2859 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2860 tmp |= 1 << (hash - 32);
2861 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2862 } else {
2863 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2864 tmp |= 1 << hash;
2865 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868}
2869
Sascha Hauer22f6b862009-04-15 01:32:18 +00002870/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002871static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002872fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002874 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002875 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Lucas Stach44934fa2014-03-30 21:32:08 +02002877 if (addr) {
2878 if (!is_valid_ether_addr(addr->sa_data))
2879 return -EADDRNOTAVAIL;
2880 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2881 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002882
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002883 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2884 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002885 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002886 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002887 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002891#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002892/**
2893 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002894 * @dev: The FEC network adapter
2895 *
2896 * Polled functionality used by netconsole and others in non interrupt mode
2897 *
2898 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002899static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002900{
2901 int i;
2902 struct fec_enet_private *fep = netdev_priv(dev);
2903
2904 for (i = 0; i < FEC_IRQ_NUM; i++) {
2905 if (fep->irq[i] > 0) {
2906 disable_irq(fep->irq[i]);
2907 fec_enet_interrupt(fep->irq[i], dev);
2908 enable_irq(fep->irq[i]);
2909 }
2910 }
2911}
2912#endif
2913
Russell King8506fa12014-07-08 12:40:33 +01002914#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2915
Jim Baxter4c09eed2013-04-19 08:10:49 +00002916static int fec_set_features(struct net_device *netdev,
2917 netdev_features_t features)
2918{
2919 struct fec_enet_private *fep = netdev_priv(netdev);
2920 netdev_features_t changed = features ^ netdev->features;
2921
Russell King8506fa12014-07-08 12:40:33 +01002922 /* Quiesce the device if necessary */
2923 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2924 napi_disable(&fep->napi);
2925 netif_tx_lock_bh(netdev);
2926 fec_stop(netdev);
2927 }
2928
Jim Baxter4c09eed2013-04-19 08:10:49 +00002929 netdev->features = features;
2930
2931 /* Receive checksum has been changed */
2932 if (changed & NETIF_F_RXCSUM) {
2933 if (features & NETIF_F_RXCSUM)
2934 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2935 else
2936 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002937 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002938
Russell King8506fa12014-07-08 12:40:33 +01002939 /* Resume the device after updates */
2940 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002941 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002942 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002943 netif_tx_unlock_bh(netdev);
2944 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002945 }
2946
2947 return 0;
2948}
2949
Sascha Hauer009fda82009-04-15 01:32:23 +00002950static const struct net_device_ops fec_netdev_ops = {
2951 .ndo_open = fec_enet_open,
2952 .ndo_stop = fec_enet_close,
2953 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002954 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002955 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002956 .ndo_validate_addr = eth_validate_addr,
2957 .ndo_tx_timeout = fec_timeout,
2958 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002959 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002960#ifdef CONFIG_NET_POLL_CONTROLLER
2961 .ndo_poll_controller = fec_poll_controller,
2962#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002963 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002964};
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 /*
2967 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002968 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002970static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002972 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002973 const struct platform_device_id *id_entry =
2974 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002975 struct fec_enet_priv_tx_q *txq;
2976 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002977 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002978 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002979 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002980 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002981
Fugang Duan41ef84c2014-09-13 05:00:54 +08002982#if defined(CONFIG_ARM)
2983 fep->rx_align = 0xf;
2984 fep->tx_align = 0xf;
2985#else
2986 fep->rx_align = 0x3;
2987 fep->tx_align = 0x3;
2988#endif
2989
Frank Li59d0f7462014-09-13 05:00:50 +08002990 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002991
Nimrod Andy55d02182014-06-12 08:16:21 +08002992 if (fep->bufdesc_ex)
2993 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2994 else
2995 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002996 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002997 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002999 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08003000 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00003001 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003002 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003003 return -ENOMEM;
3004 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003005
Fugang Duan4d494cd2014-09-13 05:00:48 +08003006 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Shawn Guo49da97d2011-01-05 21:13:11 +00003008 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003009 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003010 /* make sure MAC we just acquired is programmed into the hw */
3011 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003013 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003014 for (i = 0; i < fep->num_rx_queues; i++) {
3015 rxq = fep->rx_queue[i];
3016 rxq->index = i;
3017 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3018 rxq->bd_dma = bd_dma;
3019 if (fep->bufdesc_ex) {
3020 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3021 cbd_base = (struct bufdesc *)
3022 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3023 } else {
3024 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3025 cbd_base += rxq->rx_ring_size;
3026 }
3027 }
3028
3029 for (i = 0; i < fep->num_tx_queues; i++) {
3030 txq = fep->tx_queue[i];
3031 txq->index = i;
3032 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3033 txq->bd_dma = bd_dma;
3034 if (fep->bufdesc_ex) {
3035 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3036 cbd_base = (struct bufdesc *)
3037 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3038 } else {
3039 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3040 cbd_base += txq->tx_ring_size;
3041 }
3042 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Sascha Hauer22f6b862009-04-15 01:32:18 +00003045 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003046 ndev->watchdog_timeo = TX_TIMEOUT;
3047 ndev->netdev_ops = &fec_netdev_ops;
3048 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003049
Frank Lidc975382013-01-28 18:31:42 +00003050 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003051 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003052
Nimrod Andy09d1e542014-06-12 08:16:20 +08003053 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003054 /* enable hw VLAN support */
3055 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003056
Shawn Guo48496252013-05-08 21:08:22 +00003057 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003058 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3059
Shawn Guo48496252013-05-08 21:08:22 +00003060 /* enable hw accelerator */
3061 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003062 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003063 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3064 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003065
Fugang Duan41ef84c2014-09-13 05:00:54 +08003066 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3067 fep->tx_align = 0;
3068 fep->rx_align = 0x3f;
3069 }
3070
Nimrod Andy09d1e542014-06-12 08:16:20 +08003071 ndev->hw_features = ndev->features;
3072
Russell Kingef833372014-07-08 12:40:43 +01003073 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return 0;
3076}
3077
Shawn Guoca2cc332011-06-25 02:04:35 +08003078#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003079static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003080{
3081 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003082 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003083 struct device_node *np = pdev->dev.of_node;
3084
3085 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003086 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003087
Shawn Guoa3caad02012-06-27 03:45:24 +00003088 of_property_read_u32(np, "phy-reset-duration", &msec);
3089 /* A sane reset duration should not be longer than 1s */
3090 if (msec > 1000)
3091 msec = 1;
3092
Shawn Guoca2cc332011-06-25 02:04:35 +08003093 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003094 if (!gpio_is_valid(phy_reset))
3095 return;
3096
Shawn Guo119fc002012-06-27 03:45:22 +00003097 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3098 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003099 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003100 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003101 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003102 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003103 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003104 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003105}
3106#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003107static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003108{
3109 /*
3110 * In case of platform probe, the reset has been done
3111 * by machine code.
3112 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003113}
3114#endif /* CONFIG_OF */
3115
Fugang Duan9fc095f2014-09-13 05:00:49 +08003116static void
3117fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3118{
3119 struct device_node *np = pdev->dev.of_node;
3120 int err;
3121
3122 *num_tx = *num_rx = 1;
3123
3124 if (!np || !of_device_is_available(np))
3125 return;
3126
3127 /* parse the num of tx and rx queues */
3128 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003129 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003130 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003131
3132 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3133 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003134 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003135
3136 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003137 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3138 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003139 *num_tx = 1;
3140 return;
3141 }
3142
3143 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003144 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3145 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003146 *num_rx = 1;
3147 return;
3148 }
3149
3150}
3151
Bill Pemberton33897cc2012-12-03 09:23:58 -05003152static int
Sascha Haueread73182009-01-28 23:03:11 +00003153fec_probe(struct platform_device *pdev)
3154{
3155 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003156 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003157 struct net_device *ndev;
3158 int i, irq, ret = 0;
3159 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003160 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003161 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003162 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003163 int num_tx_qs;
3164 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003165
3166 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3167 if (of_id)
3168 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003169
Fugang Duan9fc095f2014-09-13 05:00:49 +08003170 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3171
Sascha Haueread73182009-01-28 23:03:11 +00003172 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003173 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3174 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003175 if (!ndev)
3176 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003177
3178 SET_NETDEV_DEV(ndev, &pdev->dev);
3179
3180 /* setup board info structure */
3181 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003182
Fugang Duan9fc095f2014-09-13 05:00:49 +08003183 fep->num_rx_queues = num_rx_qs;
3184 fep->num_tx_queues = num_tx_qs;
3185
Guenter Roeckd1391932013-06-18 10:04:59 -07003186#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003187 /* default enable pause frame auto negotiation */
3188 if (pdev->id_entry &&
3189 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3190 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003191#endif
Frank Libaa70a52013-01-16 16:55:58 +00003192
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003193 /* Select default pin state */
3194 pinctrl_pm_select_default_state(&pdev->dev);
3195
Fabio Estevam399db752013-07-21 13:25:03 -03003196 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303197 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3198 if (IS_ERR(fep->hwp)) {
3199 ret = PTR_ERR(fep->hwp);
3200 goto failed_ioremap;
3201 }
3202
Bryan Wue6b043d2010-03-31 02:10:44 +00003203 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003204 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003205
Frank Liff43da82013-01-03 16:04:23 +00003206 fep->bufdesc_ex = 0;
3207
Sascha Haueread73182009-01-28 23:03:11 +00003208 platform_set_drvdata(pdev, ndev);
3209
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003210 phy_node = of_parse_phandle(np, "phy-handle", 0);
3211 if (!phy_node && of_phy_is_fixed_link(np)) {
3212 ret = of_phy_register_fixed_link(np);
3213 if (ret < 0) {
3214 dev_err(&pdev->dev,
3215 "broken fixed-link specification\n");
3216 goto failed_phy;
3217 }
3218 phy_node = of_node_get(np);
3219 }
3220 fep->phy_node = phy_node;
3221
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003222 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003223 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003224 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003225 if (pdata)
3226 fep->phy_interface = pdata->phy;
3227 else
3228 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3229 } else {
3230 fep->phy_interface = ret;
3231 }
3232
Fabio Estevame2f8d552013-02-22 06:40:45 +00003233 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3234 if (IS_ERR(fep->clk_ipg)) {
3235 ret = PTR_ERR(fep->clk_ipg);
3236 goto failed_clk;
3237 }
3238
3239 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3240 if (IS_ERR(fep->clk_ahb)) {
3241 ret = PTR_ERR(fep->clk_ahb);
3242 goto failed_clk;
3243 }
3244
Fugang Duand851b472014-09-17 05:18:52 +08003245 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3246
Linus Torvalds38f56f32013-05-07 11:06:17 -07003247 /* enet_out is optional, depends on board */
3248 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3249 if (IS_ERR(fep->clk_enet_out))
3250 fep->clk_enet_out = NULL;
3251
Nimrod Andy91c0d982014-08-21 17:09:38 +08003252 fep->ptp_clk_on = false;
3253 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003254
3255 /* clk_ref is optional, depends on board */
3256 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3257 if (IS_ERR(fep->clk_ref))
3258 fep->clk_ref = NULL;
3259
Fabio Estevame2f8d552013-02-22 06:40:45 +00003260 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003261 fep->bufdesc_ex =
3262 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003263 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003264 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003265 fep->bufdesc_ex = 0;
3266 }
3267
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003268 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003269 if (ret)
3270 goto failed_clk;
3271
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003272 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3273 if (!IS_ERR(fep->reg_phy)) {
3274 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003275 if (ret) {
3276 dev_err(&pdev->dev,
3277 "Failed to enable phy regulator: %d\n", ret);
3278 goto failed_regulator;
3279 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003280 } else {
3281 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003282 }
3283
3284 fec_reset_phy(pdev);
3285
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003286 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003287 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003288
3289 ret = fec_enet_init(ndev);
3290 if (ret)
3291 goto failed_init;
3292
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003293 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003294 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003295 if (irq < 0) {
3296 if (i)
3297 break;
3298 ret = irq;
3299 goto failed_irq;
3300 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003301 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003302 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003303 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003304 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003305 }
3306
Fugang Duanb4d39b52014-09-13 05:00:55 +08003307 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003308 ret = fec_enet_mii_init(pdev);
3309 if (ret)
3310 goto failed_mii_init;
3311
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003312 /* Carrier starts down, phylib will bring it up */
3313 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003314 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003315 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003316
Sascha Haueread73182009-01-28 23:03:11 +00003317 ret = register_netdev(ndev);
3318 if (ret)
3319 goto failed_register;
3320
Fabio Estevameb1d0642013-04-13 07:25:36 +00003321 if (fep->bufdesc_ex && fep->ptp_clock)
3322 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3323
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003324 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003325 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003326 return 0;
3327
3328failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003329 fec_enet_mii_remove(fep);
3330failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003331failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003332failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003333 if (fep->reg_phy)
3334 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003335failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003336 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003337failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003338failed_phy:
3339 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003340failed_ioremap:
3341 free_netdev(ndev);
3342
3343 return ret;
3344}
3345
Bill Pemberton33897cc2012-12-03 09:23:58 -05003346static int
Sascha Haueread73182009-01-28 23:03:11 +00003347fec_drv_remove(struct platform_device *pdev)
3348{
3349 struct net_device *ndev = platform_get_drvdata(pdev);
3350 struct fec_enet_private *fep = netdev_priv(ndev);
3351
Nimrod Andy91c0d982014-08-21 17:09:38 +08003352 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003353 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003354 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003355 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003356 if (fep->reg_phy)
3357 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003358 if (fep->ptp_clock)
3359 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003360 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003361 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003362 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003363
Sascha Haueread73182009-01-28 23:03:11 +00003364 return 0;
3365}
3366
Fabio Estevamdd66d382014-07-24 18:24:01 -03003367static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003368{
Eric Benard87cad5c2010-06-18 04:19:54 +00003369 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003370 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003371
Russell Kingda1774e2014-07-08 12:39:57 +01003372 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003373 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003374 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003375 napi_disable(&fep->napi);
3376 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003377 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003378 netif_tx_unlock_bh(ndev);
3379 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003380 }
Russell Kingda1774e2014-07-08 12:39:57 +01003381 rtnl_unlock();
3382
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003383 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003384 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003385
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003386 if (fep->reg_phy)
3387 regulator_disable(fep->reg_phy);
3388
Sascha Haueread73182009-01-28 23:03:11 +00003389 return 0;
3390}
3391
Fabio Estevamdd66d382014-07-24 18:24:01 -03003392static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003393{
Eric Benard87cad5c2010-06-18 04:19:54 +00003394 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003395 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003396 int ret;
3397
3398 if (fep->reg_phy) {
3399 ret = regulator_enable(fep->reg_phy);
3400 if (ret)
3401 return ret;
3402 }
Sascha Haueread73182009-01-28 23:03:11 +00003403
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003404 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003405 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003406 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003407 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003408
Russell Kingda1774e2014-07-08 12:39:57 +01003409 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003410 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003411 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003412 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003413 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003414 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003415 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003416 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003417 }
Russell Kingda1774e2014-07-08 12:39:57 +01003418 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003419
Sascha Haueread73182009-01-28 23:03:11 +00003420 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003421
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003422failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003423 if (fep->reg_phy)
3424 regulator_disable(fep->reg_phy);
3425 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003426}
3427
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003428static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003429
Sascha Haueread73182009-01-28 23:03:11 +00003430static struct platform_driver fec_driver = {
3431 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003432 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003433 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003434 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003435 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003436 },
Shawn Guob5680e02011-01-05 21:13:13 +00003437 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003438 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003439 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003440};
3441
Fabio Estevamaaca2372012-01-23 16:44:50 +00003442module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003444MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445MODULE_LICENSE("GPL");