blob: e364d1fc7bdc9c9fb94d9984538d0cf9895fb250 [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
Luwei Zhou278d2402014-10-10 13:15:30 +08001625 fec_ptp_check_pps_event(fep);
1626
Uwe Kleine-König45993652011-01-19 20:47:04 +01001627 return ret;
1628}
1629
Frank Lidc975382013-01-28 18:31:42 +00001630static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1631{
1632 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001633 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001634 int pkts;
1635
1636 /*
1637 * Clear any pending transmit or receive interrupts before
1638 * processing the rings to avoid racing with the hardware.
1639 */
1640 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1641
1642 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001643
Frank Lide5fb0a2013-03-03 17:34:25 +00001644 fec_enet_tx(ndev);
1645
Frank Lidc975382013-01-28 18:31:42 +00001646 if (pkts < budget) {
1647 napi_complete(napi);
1648 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1649 }
1650 return pkts;
1651}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001654static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001656 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001657 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001658 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Shawn Guo49da97d2011-01-05 21:13:11 +00001660 /*
1661 * try to get mac address in following order:
1662 *
1663 * 1) module parameter via kernel command line in form
1664 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1665 */
1666 iap = macaddr;
1667
1668 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001669 * 2) from device tree data
1670 */
1671 if (!is_valid_ether_addr(iap)) {
1672 struct device_node *np = fep->pdev->dev.of_node;
1673 if (np) {
1674 const char *mac = of_get_mac_address(np);
1675 if (mac)
1676 iap = (unsigned char *) mac;
1677 }
1678 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001679
1680 /*
1681 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001682 */
1683 if (!is_valid_ether_addr(iap)) {
1684#ifdef CONFIG_M5272
1685 if (FEC_FLASHMAC)
1686 iap = (unsigned char *)FEC_FLASHMAC;
1687#else
1688 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001689 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001690#endif
1691 }
1692
1693 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001694 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001695 */
1696 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001697 *((__be32 *) &tmpaddr[0]) =
1698 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1699 *((__be16 *) &tmpaddr[4]) =
1700 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 iap = &tmpaddr[0];
1702 }
1703
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001704 /*
1705 * 5) random mac address
1706 */
1707 if (!is_valid_ether_addr(iap)) {
1708 /* Report it and use a random ethernet address instead */
1709 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1710 eth_hw_addr_random(ndev);
1711 netdev_info(ndev, "Using random MAC address: %pM\n",
1712 ndev->dev_addr);
1713 return;
1714 }
1715
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001716 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Shawn Guo49da97d2011-01-05 21:13:11 +00001718 /* Adjust MAC if using macaddr */
1719 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001720 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723/* ------------------------------------------------------------------------- */
1724
Bryan Wue6b043d2010-03-31 02:10:44 +00001725/*
1726 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001728static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001730 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001731 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001732 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Bryan Wue6b043d2010-03-31 02:10:44 +00001734 /* Prevent a state halted on mii error */
1735 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1736 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001739
Russell King8ce56242014-07-08 12:40:07 +01001740 /*
1741 * If the netdev is down, or is going down, we're not interested
1742 * in link state events, so just mark our idea of the link as down
1743 * and ignore the event.
1744 */
1745 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1746 fep->link = 0;
1747 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001748 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001749 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001750 status_change = 1;
1751 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001752
Russell Kingef833372014-07-08 12:40:43 +01001753 if (fep->full_duplex != phy_dev->duplex) {
1754 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001755 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001756 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001757
1758 if (phy_dev->speed != fep->speed) {
1759 fep->speed = phy_dev->speed;
1760 status_change = 1;
1761 }
1762
1763 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001764 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001765 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001766 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001767 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001768 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001769 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001770 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001771 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001772 } else {
1773 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001774 napi_disable(&fep->napi);
1775 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001776 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001777 netif_tx_unlock_bh(ndev);
1778 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001779 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001780 status_change = 1;
1781 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001782 }
1783
Bryan Wue6b043d2010-03-31 02:10:44 +00001784 if (status_change)
1785 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Bryan Wue6b043d2010-03-31 02:10:44 +00001788static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Bryan Wue6b043d2010-03-31 02:10:44 +00001790 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001791 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001792
1793 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001794 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001795
1796 /* start a read op */
1797 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1798 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1799 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1800
1801 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001802 time_left = wait_for_completion_timeout(&fep->mdio_done,
1803 usecs_to_jiffies(FEC_MII_TIMEOUT));
1804 if (time_left == 0) {
1805 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001806 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001807 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001808 }
1809
1810 /* return value */
1811 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1812}
1813
1814static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1815 u16 value)
1816{
1817 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001818 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001819
1820 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001821 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001822
Shawn Guo862f0982011-01-05 21:13:09 +00001823 /* start a write op */
1824 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001825 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1826 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1827 fep->hwp + FEC_MII_DATA);
1828
1829 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001830 time_left = wait_for_completion_timeout(&fep->mdio_done,
1831 usecs_to_jiffies(FEC_MII_TIMEOUT));
1832 if (time_left == 0) {
1833 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001834 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001835 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001836 }
1837
1838 return 0;
1839}
1840
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001841static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1842{
1843 struct fec_enet_private *fep = netdev_priv(ndev);
1844 int ret;
1845
1846 if (enable) {
1847 ret = clk_prepare_enable(fep->clk_ahb);
1848 if (ret)
1849 return ret;
1850 ret = clk_prepare_enable(fep->clk_ipg);
1851 if (ret)
1852 goto failed_clk_ipg;
1853 if (fep->clk_enet_out) {
1854 ret = clk_prepare_enable(fep->clk_enet_out);
1855 if (ret)
1856 goto failed_clk_enet_out;
1857 }
1858 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001859 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001860 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001861 if (ret) {
1862 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001863 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001864 } else {
1865 fep->ptp_clk_on = true;
1866 }
1867 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001868 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001869 if (fep->clk_ref) {
1870 ret = clk_prepare_enable(fep->clk_ref);
1871 if (ret)
1872 goto failed_clk_ref;
1873 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001874 } else {
1875 clk_disable_unprepare(fep->clk_ahb);
1876 clk_disable_unprepare(fep->clk_ipg);
1877 if (fep->clk_enet_out)
1878 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001879 if (fep->clk_ptp) {
1880 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001881 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001882 fep->ptp_clk_on = false;
1883 mutex_unlock(&fep->ptp_clk_mutex);
1884 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001885 if (fep->clk_ref)
1886 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001887 }
1888
1889 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001890
1891failed_clk_ref:
1892 if (fep->clk_ref)
1893 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001894failed_clk_ptp:
1895 if (fep->clk_enet_out)
1896 clk_disable_unprepare(fep->clk_enet_out);
1897failed_clk_enet_out:
1898 clk_disable_unprepare(fep->clk_ipg);
1899failed_clk_ipg:
1900 clk_disable_unprepare(fep->clk_ahb);
1901
1902 return ret;
1903}
1904
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001905static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001906{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001907 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001908 const struct platform_device_id *id_entry =
1909 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001910 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001911 char mdio_bus_id[MII_BUS_ID_SIZE];
1912 char phy_name[MII_BUS_ID_SIZE + 3];
1913 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001914 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001915
Bryan Wu418bd0d2010-05-28 03:40:39 -07001916 fep->phy_dev = NULL;
1917
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001918 if (fep->phy_node) {
1919 phy_dev = of_phy_connect(ndev, fep->phy_node,
1920 &fec_enet_adjust_link, 0,
1921 fep->phy_interface);
1922 } else {
1923 /* check for attached phy */
1924 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1925 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1926 continue;
1927 if (fep->mii_bus->phy_map[phy_id] == NULL)
1928 continue;
1929 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1930 continue;
1931 if (dev_id--)
1932 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001933 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001934 break;
1935 }
1936
1937 if (phy_id >= PHY_MAX_ADDR) {
1938 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001939 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001940 phy_id = 0;
1941 }
1942
1943 snprintf(phy_name, sizeof(phy_name),
1944 PHY_ID_FMT, mdio_bus_id, phy_id);
1945 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1946 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001947 }
1948
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001949 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001950 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001951 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001952 }
1953
1954 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001955 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001956 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001957 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001958#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001959 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001960#endif
Frank Libaa70a52013-01-16 16:55:58 +00001961 }
Shawn Guo230dec62011-09-23 02:12:48 +00001962 else
1963 phy_dev->supported &= PHY_BASIC_FEATURES;
1964
Bryan Wue6b043d2010-03-31 02:10:44 +00001965 phy_dev->advertising = phy_dev->supported;
1966
1967 fep->phy_dev = phy_dev;
1968 fep->link = 0;
1969 fep->full_duplex = 0;
1970
Joe Perches31b77202013-04-13 19:03:17 +00001971 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1972 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1973 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001974
Bryan Wue6b043d2010-03-31 02:10:44 +00001975 return 0;
1976}
1977
1978static int fec_enet_mii_init(struct platform_device *pdev)
1979{
Shawn Guob5680e02011-01-05 21:13:13 +00001980 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001981 struct net_device *ndev = platform_get_drvdata(pdev);
1982 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001983 const struct platform_device_id *id_entry =
1984 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001985 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001986 int err = -ENXIO, i;
1987
Shawn Guob5680e02011-01-05 21:13:13 +00001988 /*
1989 * The dual fec interfaces are not equivalent with enet-mac.
1990 * Here are the differences:
1991 *
1992 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1993 * - fec0 acts as the 1588 time master while fec1 is slave
1994 * - external phys can only be configured by fec0
1995 *
1996 * That is to say fec1 can not work independently. It only works
1997 * when fec0 is working. The reason behind this design is that the
1998 * second interface is added primarily for Switch mode.
1999 *
2000 * Because of the last point above, both phys are attached on fec0
2001 * mdio interface in board design, and need to be configured by
2002 * fec0 mii_bus.
2003 */
Shawn Guo43af9402011-12-05 05:01:15 +00002004 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00002005 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002006 if (mii_cnt && fec0_mii_bus) {
2007 fep->mii_bus = fec0_mii_bus;
2008 mii_cnt++;
2009 return 0;
2010 }
2011 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00002012 }
2013
Bryan Wue6b043d2010-03-31 02:10:44 +00002014 fep->mii_timeout = 0;
2015
2016 /*
2017 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002018 *
2019 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2020 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2021 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2022 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002023 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08002024 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00002025 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2026 fep->phy_speed--;
2027 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00002028 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2029
2030 fep->mii_bus = mdiobus_alloc();
2031 if (fep->mii_bus == NULL) {
2032 err = -ENOMEM;
2033 goto err_out;
2034 }
2035
2036 fep->mii_bus->name = "fec_enet_mii_bus";
2037 fep->mii_bus->read = fec_enet_mdio_read;
2038 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002039 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2040 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002041 fep->mii_bus->priv = fep;
2042 fep->mii_bus->parent = &pdev->dev;
2043
2044 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2045 if (!fep->mii_bus->irq) {
2046 err = -ENOMEM;
2047 goto err_out_free_mdiobus;
2048 }
2049
2050 for (i = 0; i < PHY_MAX_ADDR; i++)
2051 fep->mii_bus->irq[i] = PHY_POLL;
2052
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002053 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2054 if (node) {
2055 err = of_mdiobus_register(fep->mii_bus, node);
2056 of_node_put(node);
2057 } else {
2058 err = mdiobus_register(fep->mii_bus);
2059 }
2060
2061 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002062 goto err_out_free_mdio_irq;
2063
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002064 mii_cnt++;
2065
Shawn Guob5680e02011-01-05 21:13:13 +00002066 /* save fec0 mii_bus */
2067 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2068 fec0_mii_bus = fep->mii_bus;
2069
Bryan Wue6b043d2010-03-31 02:10:44 +00002070 return 0;
2071
Bryan Wue6b043d2010-03-31 02:10:44 +00002072err_out_free_mdio_irq:
2073 kfree(fep->mii_bus->irq);
2074err_out_free_mdiobus:
2075 mdiobus_free(fep->mii_bus);
2076err_out:
2077 return err;
2078}
2079
2080static void fec_enet_mii_remove(struct fec_enet_private *fep)
2081{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002082 if (--mii_cnt == 0) {
2083 mdiobus_unregister(fep->mii_bus);
2084 kfree(fep->mii_bus->irq);
2085 mdiobus_free(fep->mii_bus);
2086 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002087}
2088
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002089static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002090 struct ethtool_cmd *cmd)
2091{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002092 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002093 struct phy_device *phydev = fep->phy_dev;
2094
2095 if (!phydev)
2096 return -ENODEV;
2097
2098 return phy_ethtool_gset(phydev, cmd);
2099}
2100
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002101static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002102 struct ethtool_cmd *cmd)
2103{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002104 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002105 struct phy_device *phydev = fep->phy_dev;
2106
2107 if (!phydev)
2108 return -ENODEV;
2109
2110 return phy_ethtool_sset(phydev, cmd);
2111}
2112
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002113static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002114 struct ethtool_drvinfo *info)
2115{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002116 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Jiri Pirko7826d432013-01-06 00:44:26 +00002118 strlcpy(info->driver, fep->pdev->dev.driver->name,
2119 sizeof(info->driver));
2120 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2121 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
Bryan Wue6b043d2010-03-31 02:10:44 +00002123
Frank Li5ebae4892013-01-06 16:25:07 +00002124static int fec_enet_get_ts_info(struct net_device *ndev,
2125 struct ethtool_ts_info *info)
2126{
2127 struct fec_enet_private *fep = netdev_priv(ndev);
2128
2129 if (fep->bufdesc_ex) {
2130
2131 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2132 SOF_TIMESTAMPING_RX_SOFTWARE |
2133 SOF_TIMESTAMPING_SOFTWARE |
2134 SOF_TIMESTAMPING_TX_HARDWARE |
2135 SOF_TIMESTAMPING_RX_HARDWARE |
2136 SOF_TIMESTAMPING_RAW_HARDWARE;
2137 if (fep->ptp_clock)
2138 info->phc_index = ptp_clock_index(fep->ptp_clock);
2139 else
2140 info->phc_index = -1;
2141
2142 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2143 (1 << HWTSTAMP_TX_ON);
2144
2145 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2146 (1 << HWTSTAMP_FILTER_ALL);
2147 return 0;
2148 } else {
2149 return ethtool_op_get_ts_info(ndev, info);
2150 }
2151}
2152
Guenter Roeckd1391932013-06-18 10:04:59 -07002153#if !defined(CONFIG_M5272)
2154
Frank Libaa70a52013-01-16 16:55:58 +00002155static void fec_enet_get_pauseparam(struct net_device *ndev,
2156 struct ethtool_pauseparam *pause)
2157{
2158 struct fec_enet_private *fep = netdev_priv(ndev);
2159
2160 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2161 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2162 pause->rx_pause = pause->tx_pause;
2163}
2164
2165static int fec_enet_set_pauseparam(struct net_device *ndev,
2166 struct ethtool_pauseparam *pause)
2167{
2168 struct fec_enet_private *fep = netdev_priv(ndev);
2169
Russell King0b146ca2014-07-08 00:22:59 +01002170 if (!fep->phy_dev)
2171 return -ENODEV;
2172
Frank Libaa70a52013-01-16 16:55:58 +00002173 if (pause->tx_pause != pause->rx_pause) {
2174 netdev_info(ndev,
2175 "hardware only support enable/disable both tx and rx");
2176 return -EINVAL;
2177 }
2178
2179 fep->pause_flag = 0;
2180
2181 /* tx pause must be same as rx pause */
2182 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2183 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2184
2185 if (pause->rx_pause || pause->autoneg) {
2186 fep->phy_dev->supported |= ADVERTISED_Pause;
2187 fep->phy_dev->advertising |= ADVERTISED_Pause;
2188 } else {
2189 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2190 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2191 }
2192
2193 if (pause->autoneg) {
2194 if (netif_running(ndev))
2195 fec_stop(ndev);
2196 phy_start_aneg(fep->phy_dev);
2197 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002198 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002199 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002200 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002201 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002202 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002203 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002204 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002205 }
Frank Libaa70a52013-01-16 16:55:58 +00002206
2207 return 0;
2208}
2209
Chris Healy38ae92d2013-06-25 23:18:52 -07002210static const struct fec_stat {
2211 char name[ETH_GSTRING_LEN];
2212 u16 offset;
2213} fec_stats[] = {
2214 /* RMON TX */
2215 { "tx_dropped", RMON_T_DROP },
2216 { "tx_packets", RMON_T_PACKETS },
2217 { "tx_broadcast", RMON_T_BC_PKT },
2218 { "tx_multicast", RMON_T_MC_PKT },
2219 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2220 { "tx_undersize", RMON_T_UNDERSIZE },
2221 { "tx_oversize", RMON_T_OVERSIZE },
2222 { "tx_fragment", RMON_T_FRAG },
2223 { "tx_jabber", RMON_T_JAB },
2224 { "tx_collision", RMON_T_COL },
2225 { "tx_64byte", RMON_T_P64 },
2226 { "tx_65to127byte", RMON_T_P65TO127 },
2227 { "tx_128to255byte", RMON_T_P128TO255 },
2228 { "tx_256to511byte", RMON_T_P256TO511 },
2229 { "tx_512to1023byte", RMON_T_P512TO1023 },
2230 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2231 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2232 { "tx_octets", RMON_T_OCTETS },
2233
2234 /* IEEE TX */
2235 { "IEEE_tx_drop", IEEE_T_DROP },
2236 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2237 { "IEEE_tx_1col", IEEE_T_1COL },
2238 { "IEEE_tx_mcol", IEEE_T_MCOL },
2239 { "IEEE_tx_def", IEEE_T_DEF },
2240 { "IEEE_tx_lcol", IEEE_T_LCOL },
2241 { "IEEE_tx_excol", IEEE_T_EXCOL },
2242 { "IEEE_tx_macerr", IEEE_T_MACERR },
2243 { "IEEE_tx_cserr", IEEE_T_CSERR },
2244 { "IEEE_tx_sqe", IEEE_T_SQE },
2245 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2246 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2247
2248 /* RMON RX */
2249 { "rx_packets", RMON_R_PACKETS },
2250 { "rx_broadcast", RMON_R_BC_PKT },
2251 { "rx_multicast", RMON_R_MC_PKT },
2252 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2253 { "rx_undersize", RMON_R_UNDERSIZE },
2254 { "rx_oversize", RMON_R_OVERSIZE },
2255 { "rx_fragment", RMON_R_FRAG },
2256 { "rx_jabber", RMON_R_JAB },
2257 { "rx_64byte", RMON_R_P64 },
2258 { "rx_65to127byte", RMON_R_P65TO127 },
2259 { "rx_128to255byte", RMON_R_P128TO255 },
2260 { "rx_256to511byte", RMON_R_P256TO511 },
2261 { "rx_512to1023byte", RMON_R_P512TO1023 },
2262 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2263 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2264 { "rx_octets", RMON_R_OCTETS },
2265
2266 /* IEEE RX */
2267 { "IEEE_rx_drop", IEEE_R_DROP },
2268 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2269 { "IEEE_rx_crc", IEEE_R_CRC },
2270 { "IEEE_rx_align", IEEE_R_ALIGN },
2271 { "IEEE_rx_macerr", IEEE_R_MACERR },
2272 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2273 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2274};
2275
2276static void fec_enet_get_ethtool_stats(struct net_device *dev,
2277 struct ethtool_stats *stats, u64 *data)
2278{
2279 struct fec_enet_private *fep = netdev_priv(dev);
2280 int i;
2281
2282 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2283 data[i] = readl(fep->hwp + fec_stats[i].offset);
2284}
2285
2286static void fec_enet_get_strings(struct net_device *netdev,
2287 u32 stringset, u8 *data)
2288{
2289 int i;
2290 switch (stringset) {
2291 case ETH_SS_STATS:
2292 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2293 memcpy(data + i * ETH_GSTRING_LEN,
2294 fec_stats[i].name, ETH_GSTRING_LEN);
2295 break;
2296 }
2297}
2298
2299static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2300{
2301 switch (sset) {
2302 case ETH_SS_STATS:
2303 return ARRAY_SIZE(fec_stats);
2304 default:
2305 return -EOPNOTSUPP;
2306 }
2307}
Guenter Roeckd1391932013-06-18 10:04:59 -07002308#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002309
Chris Healy32bc9b42013-06-17 07:25:06 -07002310static int fec_enet_nway_reset(struct net_device *dev)
2311{
2312 struct fec_enet_private *fep = netdev_priv(dev);
2313 struct phy_device *phydev = fep->phy_dev;
2314
2315 if (!phydev)
2316 return -ENODEV;
2317
2318 return genphy_restart_aneg(phydev);
2319}
2320
Fugang Duand851b472014-09-17 05:18:52 +08002321/* ITR clock source is enet system clock (clk_ahb).
2322 * TCTT unit is cycle_ns * 64 cycle
2323 * So, the ICTT value = X us / (cycle_ns * 64)
2324 */
2325static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2326{
2327 struct fec_enet_private *fep = netdev_priv(ndev);
2328
2329 return us * (fep->itr_clk_rate / 64000) / 1000;
2330}
2331
2332/* Set threshold for interrupt coalescing */
2333static void fec_enet_itr_coal_set(struct net_device *ndev)
2334{
2335 struct fec_enet_private *fep = netdev_priv(ndev);
2336 const struct platform_device_id *id_entry =
2337 platform_get_device_id(fep->pdev);
2338 int rx_itr, tx_itr;
2339
2340 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2341 return;
2342
2343 /* Must be greater than zero to avoid unpredictable behavior */
2344 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2345 !fep->tx_time_itr || !fep->tx_pkts_itr)
2346 return;
2347
2348 /* Select enet system clock as Interrupt Coalescing
2349 * timer Clock Source
2350 */
2351 rx_itr = FEC_ITR_CLK_SEL;
2352 tx_itr = FEC_ITR_CLK_SEL;
2353
2354 /* set ICFT and ICTT */
2355 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2356 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2357 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2358 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2359
2360 rx_itr |= FEC_ITR_EN;
2361 tx_itr |= FEC_ITR_EN;
2362
2363 writel(tx_itr, fep->hwp + FEC_TXIC0);
2364 writel(rx_itr, fep->hwp + FEC_RXIC0);
2365 writel(tx_itr, fep->hwp + FEC_TXIC1);
2366 writel(rx_itr, fep->hwp + FEC_RXIC1);
2367 writel(tx_itr, fep->hwp + FEC_TXIC2);
2368 writel(rx_itr, fep->hwp + FEC_RXIC2);
2369}
2370
2371static int
2372fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2373{
2374 struct fec_enet_private *fep = netdev_priv(ndev);
2375 const struct platform_device_id *id_entry =
2376 platform_get_device_id(fep->pdev);
2377
2378 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2379 return -EOPNOTSUPP;
2380
2381 ec->rx_coalesce_usecs = fep->rx_time_itr;
2382 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2383
2384 ec->tx_coalesce_usecs = fep->tx_time_itr;
2385 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2386
2387 return 0;
2388}
2389
2390static int
2391fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2392{
2393 struct fec_enet_private *fep = netdev_priv(ndev);
2394 const struct platform_device_id *id_entry =
2395 platform_get_device_id(fep->pdev);
2396
2397 unsigned int cycle;
2398
2399 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2400 return -EOPNOTSUPP;
2401
2402 if (ec->rx_max_coalesced_frames > 255) {
2403 pr_err("Rx coalesced frames exceed hardware limiation");
2404 return -EINVAL;
2405 }
2406
2407 if (ec->tx_max_coalesced_frames > 255) {
2408 pr_err("Tx coalesced frame exceed hardware limiation");
2409 return -EINVAL;
2410 }
2411
2412 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2413 if (cycle > 0xFFFF) {
2414 pr_err("Rx coalesed usec exceeed hardware limiation");
2415 return -EINVAL;
2416 }
2417
2418 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2419 if (cycle > 0xFFFF) {
2420 pr_err("Rx coalesed usec exceeed hardware limiation");
2421 return -EINVAL;
2422 }
2423
2424 fep->rx_time_itr = ec->rx_coalesce_usecs;
2425 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2426
2427 fep->tx_time_itr = ec->tx_coalesce_usecs;
2428 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2429
2430 fec_enet_itr_coal_set(ndev);
2431
2432 return 0;
2433}
2434
2435static void fec_enet_itr_coal_init(struct net_device *ndev)
2436{
2437 struct ethtool_coalesce ec;
2438
2439 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2440 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2441
2442 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2443 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2444
2445 fec_enet_set_coalesce(ndev, &ec);
2446}
2447
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002448static int fec_enet_get_tunable(struct net_device *netdev,
2449 const struct ethtool_tunable *tuna,
2450 void *data)
2451{
2452 struct fec_enet_private *fep = netdev_priv(netdev);
2453 int ret = 0;
2454
2455 switch (tuna->id) {
2456 case ETHTOOL_RX_COPYBREAK:
2457 *(u32 *)data = fep->rx_copybreak;
2458 break;
2459 default:
2460 ret = -EINVAL;
2461 break;
2462 }
2463
2464 return ret;
2465}
2466
2467static int fec_enet_set_tunable(struct net_device *netdev,
2468 const struct ethtool_tunable *tuna,
2469 const void *data)
2470{
2471 struct fec_enet_private *fep = netdev_priv(netdev);
2472 int ret = 0;
2473
2474 switch (tuna->id) {
2475 case ETHTOOL_RX_COPYBREAK:
2476 fep->rx_copybreak = *(u32 *)data;
2477 break;
2478 default:
2479 ret = -EINVAL;
2480 break;
2481 }
2482
2483 return ret;
2484}
2485
stephen hemminger9b07be42012-01-04 12:59:49 +00002486static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002487 .get_settings = fec_enet_get_settings,
2488 .set_settings = fec_enet_set_settings,
2489 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002490 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002491 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002492 .get_coalesce = fec_enet_get_coalesce,
2493 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002494#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002495 .get_pauseparam = fec_enet_get_pauseparam,
2496 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002497 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002498 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002499 .get_sset_count = fec_enet_get_sset_count,
2500#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002501 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002502 .get_tunable = fec_enet_get_tunable,
2503 .set_tunable = fec_enet_set_tunable,
Bryan Wue6b043d2010-03-31 02:10:44 +00002504};
2505
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002506static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002507{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002508 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002509 struct phy_device *phydev = fep->phy_dev;
2510
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002511 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002512 return -EINVAL;
2513
2514 if (!phydev)
2515 return -ENODEV;
2516
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002517 if (fep->bufdesc_ex) {
2518 if (cmd == SIOCSHWTSTAMP)
2519 return fec_ptp_set(ndev, rq);
2520 if (cmd == SIOCGHWTSTAMP)
2521 return fec_ptp_get(ndev, rq);
2522 }
Frank Liff43da82013-01-03 16:04:23 +00002523
Richard Cochran28b04112010-07-17 08:48:55 +00002524 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002527static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002528{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002529 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002530 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002531 struct sk_buff *skb;
2532 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002533 struct fec_enet_priv_tx_q *txq;
2534 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002535 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002536
Frank Li59d0f7462014-09-13 05:00:50 +08002537 for (q = 0; q < fep->num_rx_queues; q++) {
2538 rxq = fep->rx_queue[q];
2539 bdp = rxq->rx_bd_base;
2540 for (i = 0; i < rxq->rx_ring_size; i++) {
2541 skb = rxq->rx_skbuff[i];
2542 rxq->rx_skbuff[i] = NULL;
2543 if (skb) {
2544 dma_unmap_single(&fep->pdev->dev,
2545 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002546 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002547 DMA_FROM_DEVICE);
2548 dev_kfree_skb(skb);
2549 }
2550 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002551 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002552 }
2553
Frank Li59d0f7462014-09-13 05:00:50 +08002554 for (q = 0; q < fep->num_tx_queues; q++) {
2555 txq = fep->tx_queue[q];
2556 bdp = txq->tx_bd_base;
2557 for (i = 0; i < txq->tx_ring_size; i++) {
2558 kfree(txq->tx_bounce[i]);
2559 txq->tx_bounce[i] = NULL;
2560 skb = txq->tx_skbuff[i];
2561 txq->tx_skbuff[i] = NULL;
2562 dev_kfree_skb(skb);
2563 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002564 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002565}
2566
Frank Li59d0f7462014-09-13 05:00:50 +08002567static void fec_enet_free_queue(struct net_device *ndev)
2568{
2569 struct fec_enet_private *fep = netdev_priv(ndev);
2570 int i;
2571 struct fec_enet_priv_tx_q *txq;
2572
2573 for (i = 0; i < fep->num_tx_queues; i++)
2574 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2575 txq = fep->tx_queue[i];
2576 dma_free_coherent(NULL,
2577 txq->tx_ring_size * TSO_HEADER_SIZE,
2578 txq->tso_hdrs,
2579 txq->tso_hdrs_dma);
2580 }
2581
2582 for (i = 0; i < fep->num_rx_queues; i++)
2583 if (fep->rx_queue[i])
2584 kfree(fep->rx_queue[i]);
2585
2586 for (i = 0; i < fep->num_tx_queues; i++)
2587 if (fep->tx_queue[i])
2588 kfree(fep->tx_queue[i]);
2589}
2590
2591static int fec_enet_alloc_queue(struct net_device *ndev)
2592{
2593 struct fec_enet_private *fep = netdev_priv(ndev);
2594 int i;
2595 int ret = 0;
2596 struct fec_enet_priv_tx_q *txq;
2597
2598 for (i = 0; i < fep->num_tx_queues; i++) {
2599 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2600 if (!txq) {
2601 ret = -ENOMEM;
2602 goto alloc_failed;
2603 }
2604
2605 fep->tx_queue[i] = txq;
2606 txq->tx_ring_size = TX_RING_SIZE;
2607 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2608
2609 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2610 txq->tx_wake_threshold =
2611 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2612
2613 txq->tso_hdrs = dma_alloc_coherent(NULL,
2614 txq->tx_ring_size * TSO_HEADER_SIZE,
2615 &txq->tso_hdrs_dma,
2616 GFP_KERNEL);
2617 if (!txq->tso_hdrs) {
2618 ret = -ENOMEM;
2619 goto alloc_failed;
2620 }
2621 }
2622
2623 for (i = 0; i < fep->num_rx_queues; i++) {
2624 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2625 GFP_KERNEL);
2626 if (!fep->rx_queue[i]) {
2627 ret = -ENOMEM;
2628 goto alloc_failed;
2629 }
2630
2631 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2632 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2633 }
2634 return ret;
2635
2636alloc_failed:
2637 fec_enet_free_queue(ndev);
2638 return ret;
2639}
2640
2641static int
2642fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002643{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002644 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002645 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002646 struct sk_buff *skb;
2647 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002648 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002649
Frank Li59d0f7462014-09-13 05:00:50 +08002650 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002651 bdp = rxq->rx_bd_base;
2652 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002653 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002654 if (!skb)
2655 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002656
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002657 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002658 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002659 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002660 }
Russell King730ee362014-07-08 00:23:14 +01002661
Fugang Duan4d494cd2014-09-13 05:00:48 +08002662 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002663 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002664
2665 if (fep->bufdesc_ex) {
2666 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2667 ebdp->cbd_esc = BD_ENET_RX_INT;
2668 }
2669
Frank Li59d0f7462014-09-13 05:00:50 +08002670 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002671 }
2672
2673 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002674 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002675 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002676 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002677
Frank Li59d0f7462014-09-13 05:00:50 +08002678 err_alloc:
2679 fec_enet_free_buffers(ndev);
2680 return -ENOMEM;
2681}
2682
2683static int
2684fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2685{
2686 struct fec_enet_private *fep = netdev_priv(ndev);
2687 unsigned int i;
2688 struct bufdesc *bdp;
2689 struct fec_enet_priv_tx_q *txq;
2690
2691 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002692 bdp = txq->tx_bd_base;
2693 for (i = 0; i < txq->tx_ring_size; i++) {
2694 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2695 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002696 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002697
2698 bdp->cbd_sc = 0;
2699 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002700
Frank Liff43da82013-01-03 16:04:23 +00002701 if (fep->bufdesc_ex) {
2702 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002703 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002704 }
2705
Frank Li59d0f7462014-09-13 05:00:50 +08002706 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002707 }
2708
2709 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002710 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002711 bdp->cbd_sc |= BD_SC_WRAP;
2712
2713 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002714
2715 err_alloc:
2716 fec_enet_free_buffers(ndev);
2717 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002718}
2719
Frank Li59d0f7462014-09-13 05:00:50 +08002720static int fec_enet_alloc_buffers(struct net_device *ndev)
2721{
2722 struct fec_enet_private *fep = netdev_priv(ndev);
2723 unsigned int i;
2724
2725 for (i = 0; i < fep->num_rx_queues; i++)
2726 if (fec_enet_alloc_rxq_buffers(ndev, i))
2727 return -ENOMEM;
2728
2729 for (i = 0; i < fep->num_tx_queues; i++)
2730 if (fec_enet_alloc_txq_buffers(ndev, i))
2731 return -ENOMEM;
2732 return 0;
2733}
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002736fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002738 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002739 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002741 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002742 ret = fec_enet_clk_enable(ndev, true);
2743 if (ret)
2744 return ret;
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* I should reset the ring buffers here, but I don't yet know
2747 * a simple way to do that.
2748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002750 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002751 if (ret)
Fabio Estevam681d2422014-10-04 13:40:01 -03002752 goto err_enet_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002753
Bryan Wu418bd0d2010-05-28 03:40:39 -07002754 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002755 ret = fec_enet_mii_probe(ndev);
Fabio Estevam681d2422014-10-04 13:40:01 -03002756 if (ret)
2757 goto err_enet_mii_probe;
Russell Kingce5eaf02014-02-18 12:55:42 +00002758
Russell Kingef833372014-07-08 12:40:43 +01002759 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002760 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002761 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002762 netif_tx_start_all_queues(ndev);
2763
Sascha Hauer22f6b862009-04-15 01:32:18 +00002764 return 0;
Fabio Estevam681d2422014-10-04 13:40:01 -03002765
2766err_enet_mii_probe:
2767 fec_enet_free_buffers(ndev);
2768err_enet_alloc:
2769 fec_enet_clk_enable(ndev, false);
2770 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2771 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
2774static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002775fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002777 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Russell Kingd76cfae2014-07-08 00:23:04 +01002779 phy_stop(fep->phy_dev);
2780
Russell King31a6de32014-07-08 12:40:23 +01002781 if (netif_device_present(ndev)) {
2782 napi_disable(&fep->napi);
2783 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002784 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Russell King635cf172014-07-08 00:22:54 +01002787 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002788 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002789
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002790 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002791 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002792 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return 0;
2795}
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797/* Set or clear the multicast filter for this adaptor.
2798 * Skeleton taken from sunlance driver.
2799 * The CPM Ethernet implementation allows Multicast as well as individual
2800 * MAC address filtering. Some of the drivers check to make sure it is
2801 * a group multicast address, and discard those that are not. I guess I
2802 * will do the same for now, but just remove the test if you want
2803 * individual filtering as well (do the upper net layers want or support
2804 * this kind of feature?).
2805 */
2806
2807#define HASH_BITS 6 /* #bits in hash */
2808#define CRC32_POLY 0xEDB88320
2809
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002810static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002812 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002813 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002814 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 unsigned char hash;
2816
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002817 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002818 tmp = readl(fep->hwp + FEC_R_CNTRL);
2819 tmp |= 0x8;
2820 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002821 return;
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Sascha Hauer4e831832009-04-15 01:32:19 +00002824 tmp = readl(fep->hwp + FEC_R_CNTRL);
2825 tmp &= ~0x8;
2826 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002827
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002828 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002829 /* Catch all multicast addresses, so set the
2830 * filter to all 1's
2831 */
2832 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2833 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Sascha Hauer4e831832009-04-15 01:32:19 +00002835 return;
2836 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002837
Sascha Hauer4e831832009-04-15 01:32:19 +00002838 /* Clear filter and add the addresses in hash register
2839 */
2840 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2841 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002843 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002844 /* calculate crc32 value of mac address */
2845 crc = 0xffffffff;
2846
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002847 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002848 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002849 for (bit = 0; bit < 8; bit++, data >>= 1) {
2850 crc = (crc >> 1) ^
2851 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002854
2855 /* only upper 6 bits (HASH_BITS) are used
2856 * which point to specific bit in he hash registers
2857 */
2858 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2859
2860 if (hash > 31) {
2861 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2862 tmp |= 1 << (hash - 32);
2863 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2864 } else {
2865 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2866 tmp |= 1 << hash;
2867 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870}
2871
Sascha Hauer22f6b862009-04-15 01:32:18 +00002872/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002873static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002874fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002876 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002877 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Lucas Stach44934fa2014-03-30 21:32:08 +02002879 if (addr) {
2880 if (!is_valid_ether_addr(addr->sa_data))
2881 return -EADDRNOTAVAIL;
2882 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2883 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002884
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002885 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2886 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002887 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002888 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002889 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002893#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002894/**
2895 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002896 * @dev: The FEC network adapter
2897 *
2898 * Polled functionality used by netconsole and others in non interrupt mode
2899 *
2900 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002901static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002902{
2903 int i;
2904 struct fec_enet_private *fep = netdev_priv(dev);
2905
2906 for (i = 0; i < FEC_IRQ_NUM; i++) {
2907 if (fep->irq[i] > 0) {
2908 disable_irq(fep->irq[i]);
2909 fec_enet_interrupt(fep->irq[i], dev);
2910 enable_irq(fep->irq[i]);
2911 }
2912 }
2913}
2914#endif
2915
Russell King8506fa12014-07-08 12:40:33 +01002916#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
Nimrod Andy5bc26722014-10-13 10:53:48 +08002917static inline void fec_enet_set_netdev_features(struct net_device *netdev,
Jim Baxter4c09eed2013-04-19 08:10:49 +00002918 netdev_features_t features)
2919{
2920 struct fec_enet_private *fep = netdev_priv(netdev);
2921 netdev_features_t changed = features ^ netdev->features;
2922
2923 netdev->features = features;
2924
2925 /* Receive checksum has been changed */
2926 if (changed & NETIF_F_RXCSUM) {
2927 if (features & NETIF_F_RXCSUM)
2928 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2929 else
2930 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002931 }
Nimrod Andy5bc26722014-10-13 10:53:48 +08002932}
Jim Baxter4c09eed2013-04-19 08:10:49 +00002933
Nimrod Andy5bc26722014-10-13 10:53:48 +08002934static int fec_set_features(struct net_device *netdev,
2935 netdev_features_t features)
2936{
2937 struct fec_enet_private *fep = netdev_priv(netdev);
2938 netdev_features_t changed = features ^ netdev->features;
2939
Russell King8506fa12014-07-08 12:40:33 +01002940 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Nimrod Andy5bc26722014-10-13 10:53:48 +08002941 napi_disable(&fep->napi);
2942 netif_tx_lock_bh(netdev);
2943 fec_stop(netdev);
2944 fec_enet_set_netdev_features(netdev, features);
Russell Kingef833372014-07-08 12:40:43 +01002945 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002946 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002947 netif_tx_unlock_bh(netdev);
2948 napi_enable(&fep->napi);
Nimrod Andy5bc26722014-10-13 10:53:48 +08002949 } else {
2950 fec_enet_set_netdev_features(netdev, features);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002951 }
2952
2953 return 0;
2954}
2955
Sascha Hauer009fda82009-04-15 01:32:23 +00002956static const struct net_device_ops fec_netdev_ops = {
2957 .ndo_open = fec_enet_open,
2958 .ndo_stop = fec_enet_close,
2959 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002960 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002961 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002962 .ndo_validate_addr = eth_validate_addr,
2963 .ndo_tx_timeout = fec_timeout,
2964 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002965 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002966#ifdef CONFIG_NET_POLL_CONTROLLER
2967 .ndo_poll_controller = fec_poll_controller,
2968#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002969 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002970};
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /*
2973 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002974 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002976static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002978 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002979 const struct platform_device_id *id_entry =
2980 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002981 struct fec_enet_priv_tx_q *txq;
2982 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002983 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002984 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002985 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002986 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002987
Fugang Duan41ef84c2014-09-13 05:00:54 +08002988#if defined(CONFIG_ARM)
2989 fep->rx_align = 0xf;
2990 fep->tx_align = 0xf;
2991#else
2992 fep->rx_align = 0x3;
2993 fep->tx_align = 0x3;
2994#endif
2995
Frank Li59d0f7462014-09-13 05:00:50 +08002996 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002997
Nimrod Andy55d02182014-06-12 08:16:21 +08002998 if (fep->bufdesc_ex)
2999 fep->bufdesc_size = sizeof(struct bufdesc_ex);
3000 else
3001 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003002 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08003003 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003005 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08003006 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00003007 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08003008 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08003009 return -ENOMEM;
3010 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003011
Fugang Duan4d494cd2014-09-13 05:00:48 +08003012 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Shawn Guo49da97d2011-01-05 21:13:11 +00003014 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003015 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003016 /* make sure MAC we just acquired is programmed into the hw */
3017 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003019 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003020 for (i = 0; i < fep->num_rx_queues; i++) {
3021 rxq = fep->rx_queue[i];
3022 rxq->index = i;
3023 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3024 rxq->bd_dma = bd_dma;
3025 if (fep->bufdesc_ex) {
3026 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3027 cbd_base = (struct bufdesc *)
3028 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3029 } else {
3030 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3031 cbd_base += rxq->rx_ring_size;
3032 }
3033 }
3034
3035 for (i = 0; i < fep->num_tx_queues; i++) {
3036 txq = fep->tx_queue[i];
3037 txq->index = i;
3038 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3039 txq->bd_dma = bd_dma;
3040 if (fep->bufdesc_ex) {
3041 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3042 cbd_base = (struct bufdesc *)
3043 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3044 } else {
3045 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3046 cbd_base += txq->tx_ring_size;
3047 }
3048 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Sascha Hauer22f6b862009-04-15 01:32:18 +00003051 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003052 ndev->watchdog_timeo = TX_TIMEOUT;
3053 ndev->netdev_ops = &fec_netdev_ops;
3054 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003055
Frank Lidc975382013-01-28 18:31:42 +00003056 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003057 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003058
Nimrod Andy09d1e542014-06-12 08:16:20 +08003059 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003060 /* enable hw VLAN support */
3061 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003062
Shawn Guo48496252013-05-08 21:08:22 +00003063 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003064 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3065
Shawn Guo48496252013-05-08 21:08:22 +00003066 /* enable hw accelerator */
3067 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003068 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003069 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3070 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003071
Fugang Duan41ef84c2014-09-13 05:00:54 +08003072 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3073 fep->tx_align = 0;
3074 fep->rx_align = 0x3f;
3075 }
3076
Nimrod Andy09d1e542014-06-12 08:16:20 +08003077 ndev->hw_features = ndev->features;
3078
Russell Kingef833372014-07-08 12:40:43 +01003079 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082}
3083
Shawn Guoca2cc332011-06-25 02:04:35 +08003084#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003085static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003086{
3087 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003088 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003089 struct device_node *np = pdev->dev.of_node;
3090
3091 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003092 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003093
Shawn Guoa3caad02012-06-27 03:45:24 +00003094 of_property_read_u32(np, "phy-reset-duration", &msec);
3095 /* A sane reset duration should not be longer than 1s */
3096 if (msec > 1000)
3097 msec = 1;
3098
Shawn Guoca2cc332011-06-25 02:04:35 +08003099 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003100 if (!gpio_is_valid(phy_reset))
3101 return;
3102
Shawn Guo119fc002012-06-27 03:45:22 +00003103 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3104 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003105 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003106 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003107 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003108 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003109 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003110 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003111}
3112#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003113static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003114{
3115 /*
3116 * In case of platform probe, the reset has been done
3117 * by machine code.
3118 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003119}
3120#endif /* CONFIG_OF */
3121
Fugang Duan9fc095f2014-09-13 05:00:49 +08003122static void
3123fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3124{
3125 struct device_node *np = pdev->dev.of_node;
3126 int err;
3127
3128 *num_tx = *num_rx = 1;
3129
3130 if (!np || !of_device_is_available(np))
3131 return;
3132
3133 /* parse the num of tx and rx queues */
3134 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003135 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003136 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003137
3138 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3139 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003140 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003141
3142 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003143 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3144 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003145 *num_tx = 1;
3146 return;
3147 }
3148
3149 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003150 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3151 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003152 *num_rx = 1;
3153 return;
3154 }
3155
3156}
3157
Bill Pemberton33897cc2012-12-03 09:23:58 -05003158static int
Sascha Haueread73182009-01-28 23:03:11 +00003159fec_probe(struct platform_device *pdev)
3160{
3161 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003162 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003163 struct net_device *ndev;
3164 int i, irq, ret = 0;
3165 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003166 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003167 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003168 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003169 int num_tx_qs;
3170 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003171
3172 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3173 if (of_id)
3174 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003175
Fugang Duan9fc095f2014-09-13 05:00:49 +08003176 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3177
Sascha Haueread73182009-01-28 23:03:11 +00003178 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003179 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3180 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003181 if (!ndev)
3182 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003183
3184 SET_NETDEV_DEV(ndev, &pdev->dev);
3185
3186 /* setup board info structure */
3187 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003188
Fugang Duan9fc095f2014-09-13 05:00:49 +08003189 fep->num_rx_queues = num_rx_qs;
3190 fep->num_tx_queues = num_tx_qs;
3191
Guenter Roeckd1391932013-06-18 10:04:59 -07003192#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003193 /* default enable pause frame auto negotiation */
3194 if (pdev->id_entry &&
3195 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3196 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003197#endif
Frank Libaa70a52013-01-16 16:55:58 +00003198
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003199 /* Select default pin state */
3200 pinctrl_pm_select_default_state(&pdev->dev);
3201
Fabio Estevam399db752013-07-21 13:25:03 -03003202 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303203 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3204 if (IS_ERR(fep->hwp)) {
3205 ret = PTR_ERR(fep->hwp);
3206 goto failed_ioremap;
3207 }
3208
Bryan Wue6b043d2010-03-31 02:10:44 +00003209 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003210 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003211
Frank Liff43da82013-01-03 16:04:23 +00003212 fep->bufdesc_ex = 0;
3213
Sascha Haueread73182009-01-28 23:03:11 +00003214 platform_set_drvdata(pdev, ndev);
3215
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003216 phy_node = of_parse_phandle(np, "phy-handle", 0);
3217 if (!phy_node && of_phy_is_fixed_link(np)) {
3218 ret = of_phy_register_fixed_link(np);
3219 if (ret < 0) {
3220 dev_err(&pdev->dev,
3221 "broken fixed-link specification\n");
3222 goto failed_phy;
3223 }
3224 phy_node = of_node_get(np);
3225 }
3226 fep->phy_node = phy_node;
3227
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003228 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003229 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003230 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003231 if (pdata)
3232 fep->phy_interface = pdata->phy;
3233 else
3234 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3235 } else {
3236 fep->phy_interface = ret;
3237 }
3238
Fabio Estevame2f8d552013-02-22 06:40:45 +00003239 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3240 if (IS_ERR(fep->clk_ipg)) {
3241 ret = PTR_ERR(fep->clk_ipg);
3242 goto failed_clk;
3243 }
3244
3245 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3246 if (IS_ERR(fep->clk_ahb)) {
3247 ret = PTR_ERR(fep->clk_ahb);
3248 goto failed_clk;
3249 }
3250
Fugang Duand851b472014-09-17 05:18:52 +08003251 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3252
Linus Torvalds38f56f32013-05-07 11:06:17 -07003253 /* enet_out is optional, depends on board */
3254 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3255 if (IS_ERR(fep->clk_enet_out))
3256 fep->clk_enet_out = NULL;
3257
Nimrod Andy91c0d982014-08-21 17:09:38 +08003258 fep->ptp_clk_on = false;
3259 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003260
3261 /* clk_ref is optional, depends on board */
3262 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3263 if (IS_ERR(fep->clk_ref))
3264 fep->clk_ref = NULL;
3265
Fabio Estevame2f8d552013-02-22 06:40:45 +00003266 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003267 fep->bufdesc_ex =
3268 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003269 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003270 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003271 fep->bufdesc_ex = 0;
3272 }
3273
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003274 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003275 if (ret)
3276 goto failed_clk;
3277
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003278 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3279 if (!IS_ERR(fep->reg_phy)) {
3280 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003281 if (ret) {
3282 dev_err(&pdev->dev,
3283 "Failed to enable phy regulator: %d\n", ret);
3284 goto failed_regulator;
3285 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003286 } else {
3287 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003288 }
3289
3290 fec_reset_phy(pdev);
3291
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003292 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003293 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003294
3295 ret = fec_enet_init(ndev);
3296 if (ret)
3297 goto failed_init;
3298
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003299 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003300 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003301 if (irq < 0) {
3302 if (i)
3303 break;
3304 ret = irq;
3305 goto failed_irq;
3306 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003307 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003308 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003309 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003310 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003311 }
3312
Fugang Duanb4d39b52014-09-13 05:00:55 +08003313 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003314 ret = fec_enet_mii_init(pdev);
3315 if (ret)
3316 goto failed_mii_init;
3317
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003318 /* Carrier starts down, phylib will bring it up */
3319 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003320 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003321 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003322
Sascha Haueread73182009-01-28 23:03:11 +00003323 ret = register_netdev(ndev);
3324 if (ret)
3325 goto failed_register;
3326
Fabio Estevameb1d0642013-04-13 07:25:36 +00003327 if (fep->bufdesc_ex && fep->ptp_clock)
3328 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3329
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003330 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003331 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003332 return 0;
3333
3334failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003335 fec_enet_mii_remove(fep);
3336failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003337failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003338failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003339 if (fep->reg_phy)
3340 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003341failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003342 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003343failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003344failed_phy:
3345 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003346failed_ioremap:
3347 free_netdev(ndev);
3348
3349 return ret;
3350}
3351
Bill Pemberton33897cc2012-12-03 09:23:58 -05003352static int
Sascha Haueread73182009-01-28 23:03:11 +00003353fec_drv_remove(struct platform_device *pdev)
3354{
3355 struct net_device *ndev = platform_get_drvdata(pdev);
3356 struct fec_enet_private *fep = netdev_priv(ndev);
3357
Nimrod Andy91c0d982014-08-21 17:09:38 +08003358 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003359 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003360 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003361 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003362 if (fep->reg_phy)
3363 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003364 if (fep->ptp_clock)
3365 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003366 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003367 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003368 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003369
Sascha Haueread73182009-01-28 23:03:11 +00003370 return 0;
3371}
3372
Fabio Estevamdd66d382014-07-24 18:24:01 -03003373static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003374{
Eric Benard87cad5c2010-06-18 04:19:54 +00003375 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003376 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003377
Russell Kingda1774e2014-07-08 12:39:57 +01003378 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003379 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003380 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003381 napi_disable(&fep->napi);
3382 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003383 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003384 netif_tx_unlock_bh(ndev);
3385 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003386 }
Russell Kingda1774e2014-07-08 12:39:57 +01003387 rtnl_unlock();
3388
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003389 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003390 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003391
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003392 if (fep->reg_phy)
3393 regulator_disable(fep->reg_phy);
3394
Sascha Haueread73182009-01-28 23:03:11 +00003395 return 0;
3396}
3397
Fabio Estevamdd66d382014-07-24 18:24:01 -03003398static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003399{
Eric Benard87cad5c2010-06-18 04:19:54 +00003400 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003401 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003402 int ret;
3403
3404 if (fep->reg_phy) {
3405 ret = regulator_enable(fep->reg_phy);
3406 if (ret)
3407 return ret;
3408 }
Sascha Haueread73182009-01-28 23:03:11 +00003409
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003410 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003411 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003412 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003413 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003414
Russell Kingda1774e2014-07-08 12:39:57 +01003415 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003416 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003417 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003418 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003419 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003420 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003421 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003422 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003423 }
Russell Kingda1774e2014-07-08 12:39:57 +01003424 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003425
Sascha Haueread73182009-01-28 23:03:11 +00003426 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003427
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003428failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003429 if (fep->reg_phy)
3430 regulator_disable(fep->reg_phy);
3431 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003432}
3433
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003434static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003435
Sascha Haueread73182009-01-28 23:03:11 +00003436static struct platform_driver fec_driver = {
3437 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003438 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003439 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003440 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003441 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003442 },
Shawn Guob5680e02011-01-05 21:13:13 +00003443 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003444 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003445 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003446};
3447
Fabio Estevamaaca2372012-01-23 16:44:50 +00003448module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003450MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451MODULE_LICENSE("GPL");