blob: 1f07db891ee8deec2a78b7d3c3a619559f90a52e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Greg Ungerer080853a2007-07-30 16:28:46 +100061#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Christoph Müllner772e42b2013-06-27 21:18:23 +020065static void set_multicast_list(struct net_device *ndev);
Fugang Duand851b472014-09-17 05:18:52 +080066static void fec_enet_itr_coal_init(struct net_device *ndev);
Christoph Müllner772e42b2013-06-27 21:18:23 +020067
Shawn Guob5680e02011-01-05 21:13:13 +000068#define DRIVER_NAME "fec"
69
Fugang Duan4d494cd2014-09-13 05:00:48 +080070#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
71
Frank Libaa70a52013-01-16 16:55:58 +000072/* Pause frame feild and FIFO threshold */
73#define FEC_ENET_FCE (1 << 5)
74#define FEC_ENET_RSEM_V 0x84
75#define FEC_ENET_RSFL_V 16
76#define FEC_ENET_RAEM_V 0x8
77#define FEC_ENET_RAFL_V 0x8
78#define FEC_ENET_OPD_V 0xFFF0
79
Shawn Guob5680e02011-01-05 21:13:13 +000080/* Controller is ENET-MAC */
81#define FEC_QUIRK_ENET_MAC (1 << 0)
82/* Controller needs driver to swap frame */
83#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080084/* Controller uses gasket */
85#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000086/* Controller has GBIT support */
87#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000088/* Controller has extend desc buffer */
89#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000090/* Controller has hardware checksum support */
91#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010092/* Controller has hardware vlan support */
93#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080094/* ENET IP errata ERR006358
95 *
96 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
97 * detected as not set during a prior frame transmission, then the
98 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
99 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +0800100 * frames not being transmitted until there is a 0-to-1 transition on
101 * ENET_TDAR[TDAR].
102 */
103#define FEC_QUIRK_ERR006358 (1 << 7)
Fugang Duan95a77472014-09-13 05:00:47 +0800104/* ENET IP hw AVB
105 *
106 * i.MX6SX ENET IP add Audio Video Bridging (AVB) feature support.
107 * - Two class indicators on receive with configurable priority
108 * - Two class indicators and line speed timer on transmit allowing
109 * implementation class credit based shapers externally
110 * - Additional DMA registers provisioned to allow managing up to 3
111 * independent rings
112 */
113#define FEC_QUIRK_HAS_AVB (1 << 8)
Fugang Duan37d60172014-09-17 05:18:54 +0800114/* There is a TDAR race condition for mutliQ when the software sets TDAR
115 * and the UDMA clears TDAR simultaneously or in a small window (2-4 cycles).
116 * This will cause the udma_tx and udma_tx_arbiter state machines to hang.
117 * The issue exist at i.MX6SX enet IP.
118 */
119#define FEC_QUIRK_ERR007885 (1 << 9)
Shawn Guob5680e02011-01-05 21:13:13 +0000120
121static struct platform_device_id fec_devtype[] = {
122 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800123 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000124 .name = DRIVER_NAME,
125 .driver_data = 0,
126 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800127 .name = "imx25-fec",
128 .driver_data = FEC_QUIRK_USE_GASKET,
129 }, {
130 .name = "imx27-fec",
131 .driver_data = 0,
132 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000133 .name = "imx28-fec",
134 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800135 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000136 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000137 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100138 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800139 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000140 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000141 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000142 .driver_data = FEC_QUIRK_ENET_MAC,
143 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800144 .name = "imx6sx-fec",
145 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
146 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Nimrod Andyf88c7ed2014-09-23 15:40:56 +0800147 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
148 FEC_QUIRK_ERR007885,
Fugang Duan95a77472014-09-13 05:00:47 +0800149 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800150 /* sentinel */
151 }
Shawn Guob5680e02011-01-05 21:13:13 +0000152};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800153MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000154
Shawn Guoca2cc332011-06-25 02:04:35 +0800155enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000156 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800157 IMX27_FEC, /* runs on i.mx27/35/51 */
158 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000159 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000160 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800161 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800162};
163
164static const struct of_device_id fec_dt_ids[] = {
165 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
166 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
167 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000168 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000169 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800170 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800171 { /* sentinel */ }
172};
173MODULE_DEVICE_TABLE(of, fec_dt_ids);
174
Shawn Guo49da97d2011-01-05 21:13:11 +0000175static unsigned char macaddr[ETH_ALEN];
176module_param_array(macaddr, byte, NULL, 0);
177MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
178
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000179#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * Some hardware gets it MAC address out of local flash memory.
182 * if this is non-zero then assume it is the address to get MAC from.
183 */
184#if defined(CONFIG_NETtel)
185#define FEC_FLASHMAC 0xf0006006
186#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
187#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#elif defined(CONFIG_CANCam)
189#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000190#elif defined (CONFIG_M5272C3)
191#define FEC_FLASHMAC (0xffe04000 + 4)
192#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000193#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#else
195#define FEC_FLASHMAC 0
196#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800197#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000198
Jim Baxtercdffcf12013-07-02 22:52:56 +0100199/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100201#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100203#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jim Baxter4c09eed2013-04-19 08:10:49 +0000205/* FEC receive acceleration */
206#define FEC_RACC_IPDIS (1 << 1)
207#define FEC_RACC_PRODIS (1 << 2)
208#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Matt Waddel6b265292006-06-27 13:10:56 +1000211 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * size bits. Other FEC hardware does not, so we need to take that into
213 * account when setting it.
214 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000215#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100216 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
218#else
219#define OPT_FRAME_SIZE 0
220#endif
221
Bryan Wue6b043d2010-03-31 02:10:44 +0000222/* FEC MII MMFR bits definition */
223#define FEC_MMFR_ST (1 << 30)
224#define FEC_MMFR_OP_READ (2 << 28)
225#define FEC_MMFR_OP_WRITE (1 << 28)
226#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
227#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
228#define FEC_MMFR_TA (2 << 16)
229#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500231#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Sascha Hauer22f6b862009-04-15 01:32:18 +0000233/* Transmitter timeout */
234#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Frank Libaa70a52013-01-16 16:55:58 +0000236#define FEC_PAUSE_FLAG_AUTONEG 0x1
237#define FEC_PAUSE_FLAG_ENABLE 0x2
238
Nimrod Andy1b7bde62014-09-30 09:28:05 +0800239#define COPYBREAK_DEFAULT 256
240
Nimrod Andy79f33912014-06-12 08:16:23 +0800241#define TSO_HEADER_SIZE 128
242/* Max number of allowed TCP segments for software TSO */
243#define FEC_MAX_TSO_SEGS 100
244#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
245
246#define IS_TSO_HEADER(txq, addr) \
247 ((addr >= txq->tso_hdrs_dma) && \
248 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
249
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000250static int mii_cnt;
251
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800252static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800253struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
254 struct fec_enet_private *fep,
255 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000256{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800257 struct bufdesc *new_bd = bdp + 1;
258 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800259 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
260 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800261 struct bufdesc_ex *ex_base;
262 struct bufdesc *base;
263 int ring_size;
264
Fugang Duan4d494cd2014-09-13 05:00:48 +0800265 if (bdp >= txq->tx_bd_base) {
266 base = txq->tx_bd_base;
267 ring_size = txq->tx_ring_size;
268 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800269 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800270 base = rxq->rx_bd_base;
271 ring_size = rxq->rx_ring_size;
272 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800273 }
274
275 if (fep->bufdesc_ex)
276 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
277 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000278 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800279 return (new_bd >= (base + ring_size)) ?
280 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000281}
282
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800283static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800284struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
285 struct fec_enet_private *fep,
286 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000287{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800288 struct bufdesc *new_bd = bdp - 1;
289 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800290 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
291 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800292 struct bufdesc_ex *ex_base;
293 struct bufdesc *base;
294 int ring_size;
295
Fugang Duan4d494cd2014-09-13 05:00:48 +0800296 if (bdp >= txq->tx_bd_base) {
297 base = txq->tx_bd_base;
298 ring_size = txq->tx_ring_size;
299 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800300 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800301 base = rxq->rx_bd_base;
302 ring_size = rxq->rx_ring_size;
303 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800304 }
305
306 if (fep->bufdesc_ex)
307 return (struct bufdesc *)((ex_new_bd < ex_base) ?
308 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000309 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800310 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000311}
312
Nimrod Andy61a44272014-06-12 08:16:18 +0800313static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
314 struct fec_enet_private *fep)
315{
316 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
317}
318
Fugang Duan4d494cd2014-09-13 05:00:48 +0800319static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
320 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800321{
322 int entries;
323
Fugang Duan4d494cd2014-09-13 05:00:48 +0800324 entries = ((const char *)txq->dirty_tx -
325 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800326
Fugang Duan4d494cd2014-09-13 05:00:48 +0800327 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800328}
329
Shawn Guob5680e02011-01-05 21:13:13 +0000330static void *swap_buffer(void *bufaddr, int len)
331{
332 int i;
333 unsigned int *buf = bufaddr;
334
Fabio Estevamffed61e2013-05-21 05:44:26 +0000335 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000336 *buf = cpu_to_be32(*buf);
337
338 return bufaddr;
339}
340
Russell King344756f2014-07-08 13:01:59 +0100341static void fec_dump(struct net_device *ndev)
342{
343 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800344 struct bufdesc *bdp;
345 struct fec_enet_priv_tx_q *txq;
346 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100347
348 netdev_info(ndev, "TX ring dump\n");
349 pr_info("Nr SC addr len SKB\n");
350
Fugang Duan4d494cd2014-09-13 05:00:48 +0800351 txq = fep->tx_queue[0];
352 bdp = txq->tx_bd_base;
353
Russell King344756f2014-07-08 13:01:59 +0100354 do {
355 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
356 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800357 bdp == txq->cur_tx ? 'S' : ' ',
358 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100359 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800360 txq->tx_skbuff[index]);
361 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100362 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800363 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100364}
365
Fugang Duan62a02c92014-06-18 08:33:52 +0800366static inline bool is_ipv4_pkt(struct sk_buff *skb)
367{
368 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
369}
370
Jim Baxter4c09eed2013-04-19 08:10:49 +0000371static int
372fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
373{
374 /* Only run for packets requiring a checksum. */
375 if (skb->ip_summed != CHECKSUM_PARTIAL)
376 return 0;
377
378 if (unlikely(skb_cow_head(skb, 0)))
379 return -1;
380
Fugang Duan62a02c92014-06-18 08:33:52 +0800381 if (is_ipv4_pkt(skb))
382 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000383 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
384
385 return 0;
386}
387
Nimrod Andy6e909282014-06-12 08:16:22 +0800388static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800389fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
390 struct sk_buff *skb,
391 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800392{
393 struct fec_enet_private *fep = netdev_priv(ndev);
394 const struct platform_device_id *id_entry =
395 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800396 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800397 struct bufdesc_ex *ebdp;
398 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800399 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800400 int frag, frag_len;
401 unsigned short status;
402 unsigned int estatus = 0;
403 skb_frag_t *this_frag;
404 unsigned int index;
405 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100406 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800407 int i;
408
409 for (frag = 0; frag < nr_frags; frag++) {
410 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800411 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800412 ebdp = (struct bufdesc_ex *)bdp;
413
414 status = bdp->cbd_sc;
415 status &= ~BD_ENET_TX_STATS;
416 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
417 frag_len = skb_shinfo(skb)->frags[frag].size;
418
419 /* Handle the last BD specially */
420 if (frag == nr_frags - 1) {
421 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
422 if (fep->bufdesc_ex) {
423 estatus |= BD_ENET_TX_INT;
424 if (unlikely(skb_shinfo(skb)->tx_flags &
425 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
426 estatus |= BD_ENET_TX_TS;
427 }
428 }
429
430 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800431 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
432 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800433 if (skb->ip_summed == CHECKSUM_PARTIAL)
434 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
435 ebdp->cbd_bdu = 0;
436 ebdp->cbd_esc = estatus;
437 }
438
439 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
440
Fugang Duan4d494cd2014-09-13 05:00:48 +0800441 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800442 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800443 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800444 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
445 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800446
447 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
448 swap_buffer(bufaddr, frag_len);
449 }
450
Russell Kingd6bf3142014-07-08 00:23:19 +0100451 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
452 DMA_TO_DEVICE);
453 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800454 dev_kfree_skb_any(skb);
455 if (net_ratelimit())
456 netdev_err(ndev, "Tx DMA memory map failed\n");
457 goto dma_mapping_error;
458 }
459
Russell Kingd6bf3142014-07-08 00:23:19 +0100460 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800461 bdp->cbd_datlen = frag_len;
462 bdp->cbd_sc = status;
463 }
464
Fugang Duan4d494cd2014-09-13 05:00:48 +0800465 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800466
467 return 0;
468
469dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800470 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800471 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800472 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800473 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
474 bdp->cbd_datlen, DMA_TO_DEVICE);
475 }
476 return NETDEV_TX_OK;
477}
478
Fugang Duan4d494cd2014-09-13 05:00:48 +0800479static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
480 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800481{
482 struct fec_enet_private *fep = netdev_priv(ndev);
483 const struct platform_device_id *id_entry =
484 platform_get_device_id(fep->pdev);
485 int nr_frags = skb_shinfo(skb)->nr_frags;
486 struct bufdesc *bdp, *last_bdp;
487 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100488 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800489 unsigned short status;
490 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800491 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800492 unsigned int estatus = 0;
493 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800494 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800495 int ret;
496
Fugang Duan4d494cd2014-09-13 05:00:48 +0800497 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800498 if (entries_free < MAX_SKB_FRAGS + 1) {
499 dev_kfree_skb_any(skb);
500 if (net_ratelimit())
501 netdev_err(ndev, "NOT enough BD for SG!\n");
502 return NETDEV_TX_OK;
503 }
504
Nimrod Andy6e909282014-06-12 08:16:22 +0800505 /* Protocol checksum off-load for TCP and UDP. */
506 if (fec_enet_clear_csum(skb, ndev)) {
507 dev_kfree_skb_any(skb);
508 return NETDEV_TX_OK;
509 }
510
511 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800512 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800513 status = bdp->cbd_sc;
514 status &= ~BD_ENET_TX_STATS;
515
516 /* Set buffer length and buffer pointer */
517 bufaddr = skb->data;
518 buflen = skb_headlen(skb);
519
Fugang Duan4d494cd2014-09-13 05:00:48 +0800520 queue = skb_get_queue_mapping(skb);
521 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800522 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800523 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800524 memcpy(txq->tx_bounce[index], skb->data, buflen);
525 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800526
527 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
528 swap_buffer(bufaddr, buflen);
529 }
530
Russell Kingd6bf3142014-07-08 00:23:19 +0100531 /* Push the data cache so the CPM does not get stale memory data. */
532 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
533 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800534 dev_kfree_skb_any(skb);
535 if (net_ratelimit())
536 netdev_err(ndev, "Tx DMA memory map failed\n");
537 return NETDEV_TX_OK;
538 }
539
540 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800541 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800542 if (ret)
543 return ret;
544 } else {
545 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
546 if (fep->bufdesc_ex) {
547 estatus = BD_ENET_TX_INT;
548 if (unlikely(skb_shinfo(skb)->tx_flags &
549 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
550 estatus |= BD_ENET_TX_TS;
551 }
552 }
553
554 if (fep->bufdesc_ex) {
555
556 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
557
558 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
559 fep->hwts_tx_en))
560 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
561
Nimrod Andybefe8212014-09-23 15:40:55 +0800562 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
563 estatus |= FEC_TX_BD_FTYPE(queue);
564
Nimrod Andy6e909282014-06-12 08:16:22 +0800565 if (skb->ip_summed == CHECKSUM_PARTIAL)
566 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
567
568 ebdp->cbd_bdu = 0;
569 ebdp->cbd_esc = estatus;
570 }
571
Fugang Duan4d494cd2014-09-13 05:00:48 +0800572 last_bdp = txq->cur_tx;
573 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800574 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800575 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800576
577 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100578 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800579
580 /* Send it on its way. Tell FEC it's ready, interrupt when done,
581 * it's the last BD of the frame, and to put the CRC on the end.
582 */
583 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
584 bdp->cbd_sc = status;
585
Sascha Hauer22f6b862009-04-15 01:32:18 +0000586 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800587 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800589 skb_tx_timestamp(skb);
590
Fugang Duan4d494cd2014-09-13 05:00:48 +0800591 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Frank Lide5fb0a2013-03-03 17:34:25 +0000593 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800594 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000595
Nimrod Andy6e909282014-06-12 08:16:22 +0800596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Nimrod Andy79f33912014-06-12 08:16:23 +0800599static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800600fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
601 struct net_device *ndev,
602 struct bufdesc *bdp, int index, char *data,
603 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800604{
605 struct fec_enet_private *fep = netdev_priv(ndev);
606 const struct platform_device_id *id_entry =
607 platform_get_device_id(fep->pdev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200608 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800609 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800610 unsigned short status;
611 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100612 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800613
614 status = bdp->cbd_sc;
615 status &= ~BD_ENET_TX_STATS;
616
617 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800618
Fugang Duan41ef84c2014-09-13 05:00:54 +0800619 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800620 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800621 memcpy(txq->tx_bounce[index], data, size);
622 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800623
624 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
625 swap_buffer(data, size);
626 }
627
Russell Kingd6bf3142014-07-08 00:23:19 +0100628 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
629 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800630 dev_kfree_skb_any(skb);
631 if (net_ratelimit())
632 netdev_err(ndev, "Tx DMA memory map failed\n");
633 return NETDEV_TX_BUSY;
634 }
635
Russell Kingd6bf3142014-07-08 00:23:19 +0100636 bdp->cbd_datlen = size;
637 bdp->cbd_bufaddr = addr;
638
Nimrod Andy79f33912014-06-12 08:16:23 +0800639 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800640 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
641 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800642 if (skb->ip_summed == CHECKSUM_PARTIAL)
643 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
644 ebdp->cbd_bdu = 0;
645 ebdp->cbd_esc = estatus;
646 }
647
648 /* Handle the last BD specially */
649 if (last_tcp)
650 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
651 if (is_last) {
652 status |= BD_ENET_TX_INTR;
653 if (fep->bufdesc_ex)
654 ebdp->cbd_esc |= BD_ENET_TX_INT;
655 }
656
657 bdp->cbd_sc = status;
658
659 return 0;
660}
661
662static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800663fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
664 struct sk_buff *skb, struct net_device *ndev,
665 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800666{
667 struct fec_enet_private *fep = netdev_priv(ndev);
668 const struct platform_device_id *id_entry =
669 platform_get_device_id(fep->pdev);
670 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200671 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800672 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800673 void *bufaddr;
674 unsigned long dmabuf;
675 unsigned short status;
676 unsigned int estatus = 0;
677
678 status = bdp->cbd_sc;
679 status &= ~BD_ENET_TX_STATS;
680 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
681
Fugang Duan4d494cd2014-09-13 05:00:48 +0800682 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
683 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800684 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800685 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800686 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
687 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800688
689 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
690 swap_buffer(bufaddr, hdr_len);
691
692 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
693 hdr_len, DMA_TO_DEVICE);
694 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
695 dev_kfree_skb_any(skb);
696 if (net_ratelimit())
697 netdev_err(ndev, "Tx DMA memory map failed\n");
698 return NETDEV_TX_BUSY;
699 }
700 }
701
702 bdp->cbd_bufaddr = dmabuf;
703 bdp->cbd_datlen = hdr_len;
704
705 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800706 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
707 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800708 if (skb->ip_summed == CHECKSUM_PARTIAL)
709 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
710 ebdp->cbd_bdu = 0;
711 ebdp->cbd_esc = estatus;
712 }
713
714 bdp->cbd_sc = status;
715
716 return 0;
717}
718
Fugang Duan4d494cd2014-09-13 05:00:48 +0800719static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
720 struct sk_buff *skb,
721 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800722{
723 struct fec_enet_private *fep = netdev_priv(ndev);
724 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
725 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800726 struct bufdesc *bdp = txq->cur_tx;
727 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800728 struct tso_t tso;
729 unsigned int index = 0;
730 int ret;
Fugang Duan37d60172014-09-17 05:18:54 +0800731 const struct platform_device_id *id_entry =
732 platform_get_device_id(fep->pdev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800733
Fugang Duan4d494cd2014-09-13 05:00:48 +0800734 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800735 dev_kfree_skb_any(skb);
736 if (net_ratelimit())
737 netdev_err(ndev, "NOT enough BD for TSO!\n");
738 return NETDEV_TX_OK;
739 }
740
741 /* Protocol checksum off-load for TCP and UDP. */
742 if (fec_enet_clear_csum(skb, ndev)) {
743 dev_kfree_skb_any(skb);
744 return NETDEV_TX_OK;
745 }
746
747 /* Initialize the TSO handler, and prepare the first payload */
748 tso_start(skb, &tso);
749
750 total_len = skb->len - hdr_len;
751 while (total_len > 0) {
752 char *hdr;
753
Fugang Duan4d494cd2014-09-13 05:00:48 +0800754 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800755 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
756 total_len -= data_left;
757
758 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800759 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800760 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800761 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800762 if (ret)
763 goto err_release;
764
765 while (data_left > 0) {
766 int size;
767
768 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800769 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
770 index = fec_enet_get_bd_index(txq->tx_bd_base,
771 bdp, fep);
772 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
773 bdp, index,
774 tso.data, size,
775 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800776 total_len == 0);
777 if (ret)
778 goto err_release;
779
780 data_left -= size;
781 tso_build_data(skb, &tso, size);
782 }
783
Fugang Duan4d494cd2014-09-13 05:00:48 +0800784 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800785 }
786
787 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800788 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800789
Nimrod Andy79f33912014-06-12 08:16:23 +0800790 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800791 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800792
793 /* Trigger transmission start */
Fugang Duan37d60172014-09-17 05:18:54 +0800794 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
795 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
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 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800800
801 return 0;
802
803err_release:
804 /* TODO: Release all used data descriptors for TSO */
805 return ret;
806}
807
Nimrod Andy61a44272014-06-12 08:16:18 +0800808static netdev_tx_t
809fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
810{
811 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800812 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800813 unsigned short queue;
814 struct fec_enet_priv_tx_q *txq;
815 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800816 int ret;
817
Fugang Duan4d494cd2014-09-13 05:00:48 +0800818 queue = skb_get_queue_mapping(skb);
819 txq = fep->tx_queue[queue];
820 nq = netdev_get_tx_queue(ndev, queue);
821
Nimrod Andy79f33912014-06-12 08:16:23 +0800822 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800823 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800824 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800825 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800826 if (ret)
827 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800828
Fugang Duan4d494cd2014-09-13 05:00:48 +0800829 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
830 if (entries_free <= txq->tx_stop_threshold)
831 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800832
833 return NETDEV_TX_OK;
834}
835
Frank Li14109a52013-03-26 16:12:03 +0000836/* Init RX & TX buffer descriptors
837 */
838static void fec_enet_bd_init(struct net_device *dev)
839{
840 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800841 struct fec_enet_priv_tx_q *txq;
842 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000843 struct bufdesc *bdp;
844 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800845 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000846
Frank Li59d0f7462014-09-13 05:00:50 +0800847 for (q = 0; q < fep->num_rx_queues; q++) {
848 /* Initialize the receive buffer descriptors. */
849 rxq = fep->rx_queue[q];
850 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800851
Frank Li59d0f7462014-09-13 05:00:50 +0800852 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000853
Frank Li59d0f7462014-09-13 05:00:50 +0800854 /* Initialize the BD for every fragment in the page. */
855 if (bdp->cbd_bufaddr)
856 bdp->cbd_sc = BD_ENET_RX_EMPTY;
857 else
858 bdp->cbd_sc = 0;
859 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000860 }
Frank Li59d0f7462014-09-13 05:00:50 +0800861
862 /* Set the last buffer to wrap */
863 bdp = fec_enet_get_prevdesc(bdp, fep, q);
864 bdp->cbd_sc |= BD_SC_WRAP;
865
866 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000867 }
868
Frank Li59d0f7462014-09-13 05:00:50 +0800869 for (q = 0; q < fep->num_tx_queues; q++) {
870 /* ...and the same for transmit */
871 txq = fep->tx_queue[q];
872 bdp = txq->tx_bd_base;
873 txq->cur_tx = bdp;
874
875 for (i = 0; i < txq->tx_ring_size; i++) {
876 /* Initialize the BD for every fragment in the page. */
877 bdp->cbd_sc = 0;
878 if (txq->tx_skbuff[i]) {
879 dev_kfree_skb_any(txq->tx_skbuff[i]);
880 txq->tx_skbuff[i] = NULL;
881 }
882 bdp->cbd_bufaddr = 0;
883 bdp = fec_enet_get_nextdesc(bdp, fep, q);
884 }
885
886 /* Set the last buffer to wrap */
887 bdp = fec_enet_get_prevdesc(bdp, fep, q);
888 bdp->cbd_sc |= BD_SC_WRAP;
889 txq->dirty_tx = bdp;
890 }
891}
892
Frank Lice99d0d2014-09-13 05:00:52 +0800893static void fec_enet_active_rxring(struct net_device *ndev)
894{
895 struct fec_enet_private *fep = netdev_priv(ndev);
896 int i;
897
898 for (i = 0; i < fep->num_rx_queues; i++)
899 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
900}
901
Frank Li59d0f7462014-09-13 05:00:50 +0800902static void fec_enet_enable_ring(struct net_device *ndev)
903{
904 struct fec_enet_private *fep = netdev_priv(ndev);
905 struct fec_enet_priv_tx_q *txq;
906 struct fec_enet_priv_rx_q *rxq;
907 int i;
908
909 for (i = 0; i < fep->num_rx_queues; i++) {
910 rxq = fep->rx_queue[i];
911 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
912
913 /* enable DMA1/2 */
914 if (i)
915 writel(RCMR_MATCHEN | RCMR_CMP(i),
916 fep->hwp + FEC_RCMR(i));
917 }
918
919 for (i = 0; i < fep->num_tx_queues; i++) {
920 txq = fep->tx_queue[i];
921 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
922
923 /* enable DMA1/2 */
924 if (i)
925 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
926 fep->hwp + FEC_DMA_CFG(i));
927 }
928}
929
930static void fec_enet_reset_skb(struct net_device *ndev)
931{
932 struct fec_enet_private *fep = netdev_priv(ndev);
933 struct fec_enet_priv_tx_q *txq;
934 int i, j;
935
936 for (i = 0; i < fep->num_tx_queues; i++) {
937 txq = fep->tx_queue[i];
938
939 for (j = 0; j < txq->tx_ring_size; j++) {
940 if (txq->tx_skbuff[j]) {
941 dev_kfree_skb_any(txq->tx_skbuff[j]);
942 txq->tx_skbuff[j] = NULL;
943 }
944 }
945 }
Frank Li14109a52013-03-26 16:12:03 +0000946}
947
Russell Kingdbc64a82014-07-08 12:40:12 +0100948/*
949 * This function is called to start or restart the FEC during a link
950 * change, transmit timeout, or to reconfigure the FEC. The network
951 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100952 */
953static void
Russell Kingef833372014-07-08 12:40:43 +0100954fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100955{
956 struct fec_enet_private *fep = netdev_priv(ndev);
957 const struct platform_device_id *id_entry =
958 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000959 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100960 u32 temp_mac[2];
961 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000962 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100963
Fugang Duan106c3142014-09-13 05:00:51 +0800964 /* Whack a reset. We should wait for this.
965 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
966 * instead of reset MAC itself.
967 */
968 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
969 writel(0, fep->hwp + FEC_ECNTRL);
970 } else {
971 writel(1, fep->hwp + FEC_ECNTRL);
972 udelay(10);
973 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100974
975 /*
976 * enet-mac reset will reset mac address registers too,
977 * so need to reconfigure it.
978 */
979 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
980 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
981 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
982 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
983 }
984
985 /* Clear any outstanding interrupt. */
986 writel(0xffc00000, fep->hwp + FEC_IEVENT);
987
Uwe Kleine-König45993652011-01-19 20:47:04 +0100988 /* Set maximum receive buffer size. */
989 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
990
Frank Li14109a52013-03-26 16:12:03 +0000991 fec_enet_bd_init(ndev);
992
Frank Li59d0f7462014-09-13 05:00:50 +0800993 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100994
Frank Li59d0f7462014-09-13 05:00:50 +0800995 /* Reset tx SKB buffers. */
996 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100997
998 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100999 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001000 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +01001001 writel(0x04, fep->hwp + FEC_X_CNTRL);
1002 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001003 /* No Rcv on Xmit */
1004 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001005 writel(0x0, fep->hwp + FEC_X_CNTRL);
1006 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001007
Uwe Kleine-König45993652011-01-19 20:47:04 +01001008 /* Set MII speed */
1009 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1010
Guenter Roeckd1391932013-06-18 10:04:59 -07001011#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +00001012 /* set RX checksum */
1013 val = readl(fep->hwp + FEC_RACC);
1014 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1015 val |= FEC_RACC_OPTIONS;
1016 else
1017 val &= ~FEC_RACC_OPTIONS;
1018 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -07001019#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001020
Uwe Kleine-König45993652011-01-19 20:47:04 +01001021 /*
1022 * The phy interface and speed need to get configured
1023 * differently on enet-mac.
1024 */
1025 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001026 /* Enable flow control and length check */
1027 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001028
Shawn Guo230dec62011-09-23 02:12:48 +00001029 /* RGMII, RMII or MII */
1030 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1031 rcntl |= (1 << 6);
1032 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001033 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001034 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001035 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001036
Shawn Guo230dec62011-09-23 02:12:48 +00001037 /* 1G, 100M or 10M */
1038 if (fep->phy_dev) {
1039 if (fep->phy_dev->speed == SPEED_1000)
1040 ecntl |= (1 << 5);
1041 else if (fep->phy_dev->speed == SPEED_100)
1042 rcntl &= ~(1 << 9);
1043 else
1044 rcntl |= (1 << 9);
1045 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001046 } else {
1047#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001048 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001049 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001050 /* disable the gasket and wait */
1051 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1052 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1053 udelay(1);
1054
1055 /*
1056 * configure the gasket:
1057 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001058 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001059 */
Eric Benard8d82f212012-01-12 06:10:28 +00001060 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1061 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1062 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1063 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1064 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001065
1066 /* re-enable the gasket */
1067 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1068 }
1069#endif
1070 }
Frank Libaa70a52013-01-16 16:55:58 +00001071
Guenter Roeckd1391932013-06-18 10:04:59 -07001072#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001073 /* enable pause frame*/
1074 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1075 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1076 fep->phy_dev && fep->phy_dev->pause)) {
1077 rcntl |= FEC_ENET_FCE;
1078
Jim Baxter4c09eed2013-04-19 08:10:49 +00001079 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001080 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1081 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1082 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1083 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1084
1085 /* OPD */
1086 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1087 } else {
1088 rcntl &= ~FEC_ENET_FCE;
1089 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001090#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001091
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001092 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001093
Stefan Wahren84fe6182014-03-12 11:28:19 +01001094 /* Setup multicast filter. */
1095 set_multicast_list(ndev);
1096#ifndef CONFIG_M5272
1097 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1098 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1099#endif
1100
Shawn Guo230dec62011-09-23 02:12:48 +00001101 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1102 /* enable ENET endian swap */
1103 ecntl |= (1 << 8);
1104 /* enable ENET store and forward mode */
1105 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1106 }
1107
Frank Liff43da82013-01-03 16:04:23 +00001108 if (fep->bufdesc_ex)
1109 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001110
Chris Healy38ae92d2013-06-25 23:18:52 -07001111#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001112 /* Enable the MIB statistic event counters */
1113 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001114#endif
1115
Uwe Kleine-König45993652011-01-19 20:47:04 +01001116 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001117 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001118 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001119
Frank Liff43da82013-01-03 16:04:23 +00001120 if (fep->bufdesc_ex)
1121 fec_ptp_start_cyclecounter(ndev);
1122
Uwe Kleine-König45993652011-01-19 20:47:04 +01001123 /* Enable interrupts we wish to service */
1124 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001125
1126 /* Init the interrupt coalescing */
1127 fec_enet_itr_coal_init(ndev);
1128
Uwe Kleine-König45993652011-01-19 20:47:04 +01001129}
1130
1131static void
1132fec_stop(struct net_device *ndev)
1133{
1134 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001135 const struct platform_device_id *id_entry =
1136 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001137 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001138
1139 /* We cannot expect a graceful transmit stop without link !!! */
1140 if (fep->link) {
1141 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1142 udelay(10);
1143 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001144 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001145 }
1146
Fugang Duan106c3142014-09-13 05:00:51 +08001147 /* Whack a reset. We should wait for this.
1148 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1149 * instead of reset MAC itself.
1150 */
1151 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1152 writel(0, fep->hwp + FEC_ECNTRL);
1153 } else {
1154 writel(1, fep->hwp + FEC_ECNTRL);
1155 udelay(10);
1156 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001157 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1158 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001159
1160 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001161 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001162 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001163 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1164 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001165}
1166
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001169fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001171 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Russell King344756f2014-07-08 13:01:59 +01001173 fec_dump(ndev);
1174
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001175 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Russell King36cdc742014-07-08 13:01:44 +01001177 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001178}
1179
Russell King36cdc742014-07-08 13:01:44 +01001180static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001181{
1182 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001183 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001184 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001185
Russell King36cdc742014-07-08 13:01:44 +01001186 rtnl_lock();
1187 if (netif_device_present(ndev) || netif_running(ndev)) {
1188 napi_disable(&fep->napi);
1189 netif_tx_lock_bh(ndev);
1190 fec_restart(ndev);
1191 netif_wake_queue(ndev);
1192 netif_tx_unlock_bh(ndev);
1193 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001194 }
Russell King36cdc742014-07-08 13:01:44 +01001195 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001199fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1200 struct skb_shared_hwtstamps *hwtstamps)
1201{
1202 unsigned long flags;
1203 u64 ns;
1204
1205 spin_lock_irqsave(&fep->tmreg_lock, flags);
1206 ns = timecounter_cyc2time(&fep->tc, ts);
1207 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1208
1209 memset(hwtstamps, 0, sizeof(*hwtstamps));
1210 hwtstamps->hwtstamp = ns_to_ktime(ns);
1211}
1212
1213static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001214fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001217 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001218 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001220 struct fec_enet_priv_tx_q *txq;
1221 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001222 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001223 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001225 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001226
1227 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1228
1229 txq = fep->tx_queue[queue_id];
1230 /* get next bdp of dirty_tx */
1231 nq = netdev_get_tx_queue(ndev, queue_id);
1232 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Frank Lide5fb0a2013-03-03 17:34:25 +00001234 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001235 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001236
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001237 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001238
1239 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001240 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001241 break;
1242
Fugang Duan4d494cd2014-09-13 05:00:48 +08001243 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001244
Fugang Duan4d494cd2014-09-13 05:00:48 +08001245 skb = txq->tx_skbuff[index];
1246 txq->tx_skbuff[index] = NULL;
1247 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001248 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1249 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001250 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001251 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001252 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001253 continue;
1254 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001257 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 BD_ENET_TX_RL | BD_ENET_TX_UN |
1259 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001260 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001261 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001262 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001263 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001264 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001265 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001266 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001267 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001268 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001269 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001270 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001272 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001273 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Frank Liff43da82013-01-03 16:04:23 +00001276 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1277 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001278 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001279 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001280
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001281 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001282 skb_tstamp_tx(skb, &shhwtstamps);
1283 }
Frank Liff43da82013-01-03 16:04:23 +00001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* Deferred means some collisions occurred during transmit,
1286 * but we eventually sent the packet OK.
1287 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001288 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001289 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001290
Sascha Hauer22f6b862009-04-15 01:32:18 +00001291 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001293
Fugang Duan4d494cd2014-09-13 05:00:48 +08001294 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001295
Sascha Hauer22f6b862009-04-15 01:32:18 +00001296 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001297 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001298
Sascha Hauer22f6b862009-04-15 01:32:18 +00001299 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001301 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001302 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1303 if (entries_free >= txq->tx_wake_threshold)
1304 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Russell Kingccea2962014-07-08 13:01:38 +01001307
1308 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001309 if (bdp != txq->cur_tx &&
1310 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1311 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1312}
1313
1314static void
1315fec_enet_tx(struct net_device *ndev)
1316{
1317 struct fec_enet_private *fep = netdev_priv(ndev);
1318 u16 queue_id;
1319 /* First process class A queue, then Class B and Best Effort queue */
1320 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1321 clear_bit(queue_id, &fep->work_tx);
1322 fec_enet_tx_queue(ndev, queue_id);
1323 }
1324 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001327static int
1328fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1329{
1330 struct fec_enet_private *fep = netdev_priv(ndev);
1331 int off;
1332
1333 off = ((unsigned long)skb->data) & fep->rx_align;
1334 if (off)
1335 skb_reserve(skb, fep->rx_align + 1 - off);
1336
1337 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1338 FEC_ENET_RX_FRSIZE - fep->rx_align,
1339 DMA_FROM_DEVICE);
1340 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1341 if (net_ratelimit())
1342 netdev_err(ndev, "Rx DMA memory map failed\n");
1343 return -ENOMEM;
1344 }
1345
1346 return 0;
1347}
1348
1349static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1350 struct bufdesc *bdp, u32 length)
1351{
1352 struct fec_enet_private *fep = netdev_priv(ndev);
1353 struct sk_buff *new_skb;
1354
1355 if (length > fep->rx_copybreak)
1356 return false;
1357
1358 new_skb = netdev_alloc_skb(ndev, length);
1359 if (!new_skb)
1360 return false;
1361
1362 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1363 FEC_ENET_RX_FRSIZE - fep->rx_align,
1364 DMA_FROM_DEVICE);
1365 memcpy(new_skb->data, (*skb)->data, length);
1366 *skb = new_skb;
1367
1368 return true;
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/* During a receive, the cur_rx points to the current incoming buffer.
1372 * When we update through the ring, if the next incoming buffer has
1373 * not been given to the system, we just set the empty indicator,
1374 * effectively tossing the packet.
1375 */
Frank Lidc975382013-01-28 18:31:42 +00001376static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001377fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001379 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001380 const struct platform_device_id *id_entry =
1381 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001382 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001383 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001384 unsigned short status;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001385 struct sk_buff *skb_new = NULL;
1386 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 ushort pkt_len;
1388 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001389 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001390 struct bufdesc_ex *ebdp = NULL;
1391 bool vlan_packet_rcvd = false;
1392 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001393 int index = 0;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001394 bool is_copybreak;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001395
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001396#ifdef CONFIG_M532x
1397 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001398#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001399 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1400 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* First, grab all of the stats for the incoming packet.
1403 * These get messed up if we get called due to a busy condition.
1404 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001405 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Sascha Hauer22f6b862009-04-15 01:32:18 +00001407 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Frank Lidc975382013-01-28 18:31:42 +00001409 if (pkt_received >= budget)
1410 break;
1411 pkt_received++;
1412
Sascha Hauer22f6b862009-04-15 01:32:18 +00001413 /* Since we have allocated space to hold a complete frame,
1414 * the last indicator should be set.
1415 */
1416 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001417 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Russell Kingdb3421c2014-07-08 13:01:49 +01001419
Sascha Hauer22f6b862009-04-15 01:32:18 +00001420 /* Check for errors. */
1421 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001423 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001424 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1425 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001426 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001427 }
1428 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001429 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001430 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001431 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001432 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001433 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001435
1436 /* Report late collisions as a frame error.
1437 * On this error, the BD is closed, but we don't know what we
1438 * have in the buffer. So, just drop this frame on the floor.
1439 */
1440 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001441 ndev->stats.rx_errors++;
1442 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001443 goto rx_processing_done;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Sascha Hauer22f6b862009-04-15 01:32:18 +00001446 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001447 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001448 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001449 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Fugang Duan4d494cd2014-09-13 05:00:48 +08001451 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001452 skb = rxq->rx_skbuff[index];
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001453
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001454 /* The packet length includes FCS, but we don't want to
1455 * include that when passing upstream as it messes up
1456 * bridging applications.
1457 */
1458 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4);
1459 if (!is_copybreak) {
1460 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1461 if (unlikely(!skb_new)) {
1462 ndev->stats.rx_dropped++;
1463 goto rx_processing_done;
1464 }
1465 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1466 FEC_ENET_RX_FRSIZE - fep->rx_align,
1467 DMA_FROM_DEVICE);
1468 }
1469
1470 prefetch(skb->data - NET_IP_ALIGN);
1471 skb_put(skb, pkt_len - 4);
1472 data = skb->data;
Shawn Guob5680e02011-01-05 21:13:13 +00001473 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1474 swap_buffer(data, pkt_len);
1475
Jim Baxtercdffcf12013-07-02 22:52:56 +01001476 /* Extract the enhanced buffer descriptor */
1477 ebdp = NULL;
1478 if (fep->bufdesc_ex)
1479 ebdp = (struct bufdesc_ex *)bdp;
1480
1481 /* If this is a VLAN packet remove the VLAN Tag */
1482 vlan_packet_rcvd = false;
1483 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001484 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001485 /* Push and remove the vlan tag */
1486 struct vlan_hdr *vlan_header =
1487 (struct vlan_hdr *) (data + ETH_HLEN);
1488 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001489
1490 vlan_packet_rcvd = true;
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001491
1492 skb_copy_to_linear_data_offset(skb, VLAN_HLEN,
1493 data, (2 * ETH_ALEN));
1494 skb_pull(skb, VLAN_HLEN);
Jim Baxtercdffcf12013-07-02 22:52:56 +01001495 }
1496
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001497 skb->protocol = eth_type_trans(skb, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001499 /* Get receive timestamp from the skb */
1500 if (fep->hwts_rx_en && fep->bufdesc_ex)
1501 fec_enet_hwtstamp(fep, ebdp->ts,
1502 skb_hwtstamps(skb));
Jim Baxtercdffcf12013-07-02 22:52:56 +01001503
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001504 if (fep->bufdesc_ex &&
1505 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1506 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1507 /* don't check it */
1508 skb->ip_summed = CHECKSUM_UNNECESSARY;
1509 } else {
1510 skb_checksum_none_assert(skb);
Jim Baxter4c09eed2013-04-19 08:10:49 +00001511 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001512 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001513
Nimrod Andy1b7bde62014-09-30 09:28:05 +08001514 /* Handle received VLAN packets */
1515 if (vlan_packet_rcvd)
1516 __vlan_hwaccel_put_tag(skb,
1517 htons(ETH_P_8021Q),
1518 vlan_tag);
1519
1520 napi_gro_receive(&fep->napi, skb);
1521
1522 if (is_copybreak) {
1523 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1524 FEC_ENET_RX_FRSIZE - fep->rx_align,
1525 DMA_FROM_DEVICE);
1526 } else {
1527 rxq->rx_skbuff[index] = skb_new;
1528 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1529 }
1530
Sascha Hauer22f6b862009-04-15 01:32:18 +00001531rx_processing_done:
1532 /* Clear the status flags for this buffer */
1533 status &= ~BD_ENET_RX_STATS;
1534
1535 /* Mark the buffer empty */
1536 status |= BD_ENET_RX_EMPTY;
1537 bdp->cbd_sc = status;
1538
Frank Liff43da82013-01-03 16:04:23 +00001539 if (fep->bufdesc_ex) {
1540 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1541
1542 ebdp->cbd_esc = BD_ENET_RX_INT;
1543 ebdp->cbd_prot = 0;
1544 ebdp->cbd_bdu = 0;
1545 }
Frank Li6605b732012-10-30 18:25:31 +00001546
Sascha Hauer22f6b862009-04-15 01:32:18 +00001547 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001548 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001549
Sascha Hauer22f6b862009-04-15 01:32:18 +00001550 /* Doing this here will keep the FEC running while we process
1551 * incoming frames. On a heavily loaded network, we should be
1552 * able to keep up at the expense of system resources.
1553 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001554 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001556 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001557 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Fugang Duan4d494cd2014-09-13 05:00:48 +08001560static int
1561fec_enet_rx(struct net_device *ndev, int budget)
1562{
1563 int pkt_received = 0;
1564 u16 queue_id;
1565 struct fec_enet_private *fep = netdev_priv(ndev);
1566
1567 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1568 clear_bit(queue_id, &fep->work_rx);
1569 pkt_received += fec_enet_rx_queue(ndev,
1570 budget - pkt_received, queue_id);
1571 }
1572 return pkt_received;
1573}
1574
1575static bool
1576fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1577{
1578 if (int_events == 0)
1579 return false;
1580
1581 if (int_events & FEC_ENET_RXF)
1582 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001583 if (int_events & FEC_ENET_RXF_1)
1584 fep->work_rx |= (1 << 0);
1585 if (int_events & FEC_ENET_RXF_2)
1586 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001587
1588 if (int_events & FEC_ENET_TXF)
1589 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001590 if (int_events & FEC_ENET_TXF_1)
1591 fep->work_tx |= (1 << 0);
1592 if (int_events & FEC_ENET_TXF_2)
1593 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001594
1595 return true;
1596}
1597
Uwe Kleine-König45993652011-01-19 20:47:04 +01001598static irqreturn_t
1599fec_enet_interrupt(int irq, void *dev_id)
1600{
1601 struct net_device *ndev = dev_id;
1602 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001603 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001604 uint int_events;
1605 irqreturn_t ret = IRQ_NONE;
1606
Russell King7a168072014-07-08 00:22:44 +01001607 int_events = readl(fep->hwp + FEC_IEVENT);
1608 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001609 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001610
Russell King7a168072014-07-08 00:22:44 +01001611 if (int_events & napi_mask) {
1612 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001613
Russell King7a168072014-07-08 00:22:44 +01001614 /* Disable the NAPI interrupts */
1615 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1616 napi_schedule(&fep->napi);
1617 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001618
Russell King7a168072014-07-08 00:22:44 +01001619 if (int_events & FEC_ENET_MII) {
1620 ret = IRQ_HANDLED;
1621 complete(&fep->mdio_done);
1622 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001623
1624 return ret;
1625}
1626
Frank Lidc975382013-01-28 18:31:42 +00001627static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1628{
1629 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001630 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001631 int pkts;
1632
1633 /*
1634 * Clear any pending transmit or receive interrupts before
1635 * processing the rings to avoid racing with the hardware.
1636 */
1637 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1638
1639 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001640
Frank Lide5fb0a2013-03-03 17:34:25 +00001641 fec_enet_tx(ndev);
1642
Frank Lidc975382013-01-28 18:31:42 +00001643 if (pkts < budget) {
1644 napi_complete(napi);
1645 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1646 }
1647 return pkts;
1648}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001651static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001653 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001654 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001655 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Shawn Guo49da97d2011-01-05 21:13:11 +00001657 /*
1658 * try to get mac address in following order:
1659 *
1660 * 1) module parameter via kernel command line in form
1661 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1662 */
1663 iap = macaddr;
1664
1665 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001666 * 2) from device tree data
1667 */
1668 if (!is_valid_ether_addr(iap)) {
1669 struct device_node *np = fep->pdev->dev.of_node;
1670 if (np) {
1671 const char *mac = of_get_mac_address(np);
1672 if (mac)
1673 iap = (unsigned char *) mac;
1674 }
1675 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001676
1677 /*
1678 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001679 */
1680 if (!is_valid_ether_addr(iap)) {
1681#ifdef CONFIG_M5272
1682 if (FEC_FLASHMAC)
1683 iap = (unsigned char *)FEC_FLASHMAC;
1684#else
1685 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001686 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001687#endif
1688 }
1689
1690 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001691 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001692 */
1693 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001694 *((__be32 *) &tmpaddr[0]) =
1695 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1696 *((__be16 *) &tmpaddr[4]) =
1697 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 iap = &tmpaddr[0];
1699 }
1700
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001701 /*
1702 * 5) random mac address
1703 */
1704 if (!is_valid_ether_addr(iap)) {
1705 /* Report it and use a random ethernet address instead */
1706 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1707 eth_hw_addr_random(ndev);
1708 netdev_info(ndev, "Using random MAC address: %pM\n",
1709 ndev->dev_addr);
1710 return;
1711 }
1712
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001713 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Shawn Guo49da97d2011-01-05 21:13:11 +00001715 /* Adjust MAC if using macaddr */
1716 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001717 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720/* ------------------------------------------------------------------------- */
1721
Bryan Wue6b043d2010-03-31 02:10:44 +00001722/*
1723 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001725static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001727 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001728 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001729 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Bryan Wue6b043d2010-03-31 02:10:44 +00001731 /* Prevent a state halted on mii error */
1732 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1733 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001734 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001736
Russell King8ce56242014-07-08 12:40:07 +01001737 /*
1738 * If the netdev is down, or is going down, we're not interested
1739 * in link state events, so just mark our idea of the link as down
1740 * and ignore the event.
1741 */
1742 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1743 fep->link = 0;
1744 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001745 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001746 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001747 status_change = 1;
1748 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001749
Russell Kingef833372014-07-08 12:40:43 +01001750 if (fep->full_duplex != phy_dev->duplex) {
1751 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001752 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001753 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001754
1755 if (phy_dev->speed != fep->speed) {
1756 fep->speed = phy_dev->speed;
1757 status_change = 1;
1758 }
1759
1760 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001761 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001762 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001763 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001764 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001765 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001766 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001767 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001768 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001769 } else {
1770 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001771 napi_disable(&fep->napi);
1772 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001773 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001774 netif_tx_unlock_bh(ndev);
1775 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001776 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001777 status_change = 1;
1778 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001779 }
1780
Bryan Wue6b043d2010-03-31 02:10:44 +00001781 if (status_change)
1782 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Bryan Wue6b043d2010-03-31 02:10:44 +00001785static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Bryan Wue6b043d2010-03-31 02:10:44 +00001787 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001788 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001789
1790 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001791 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001792
1793 /* start a read op */
1794 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1795 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1796 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1797
1798 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001799 time_left = wait_for_completion_timeout(&fep->mdio_done,
1800 usecs_to_jiffies(FEC_MII_TIMEOUT));
1801 if (time_left == 0) {
1802 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001803 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001804 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001805 }
1806
1807 /* return value */
1808 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1809}
1810
1811static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1812 u16 value)
1813{
1814 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001815 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001816
1817 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001818 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001819
Shawn Guo862f0982011-01-05 21:13:09 +00001820 /* start a write op */
1821 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001822 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1823 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1824 fep->hwp + FEC_MII_DATA);
1825
1826 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001827 time_left = wait_for_completion_timeout(&fep->mdio_done,
1828 usecs_to_jiffies(FEC_MII_TIMEOUT));
1829 if (time_left == 0) {
1830 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001831 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001832 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001833 }
1834
1835 return 0;
1836}
1837
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001838static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1839{
1840 struct fec_enet_private *fep = netdev_priv(ndev);
1841 int ret;
1842
1843 if (enable) {
1844 ret = clk_prepare_enable(fep->clk_ahb);
1845 if (ret)
1846 return ret;
1847 ret = clk_prepare_enable(fep->clk_ipg);
1848 if (ret)
1849 goto failed_clk_ipg;
1850 if (fep->clk_enet_out) {
1851 ret = clk_prepare_enable(fep->clk_enet_out);
1852 if (ret)
1853 goto failed_clk_enet_out;
1854 }
1855 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001856 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001857 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001858 if (ret) {
1859 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001860 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001861 } else {
1862 fep->ptp_clk_on = true;
1863 }
1864 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001865 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001866 if (fep->clk_ref) {
1867 ret = clk_prepare_enable(fep->clk_ref);
1868 if (ret)
1869 goto failed_clk_ref;
1870 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001871 } else {
1872 clk_disable_unprepare(fep->clk_ahb);
1873 clk_disable_unprepare(fep->clk_ipg);
1874 if (fep->clk_enet_out)
1875 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001876 if (fep->clk_ptp) {
1877 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001878 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001879 fep->ptp_clk_on = false;
1880 mutex_unlock(&fep->ptp_clk_mutex);
1881 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001882 if (fep->clk_ref)
1883 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001884 }
1885
1886 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001887
1888failed_clk_ref:
1889 if (fep->clk_ref)
1890 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001891failed_clk_ptp:
1892 if (fep->clk_enet_out)
1893 clk_disable_unprepare(fep->clk_enet_out);
1894failed_clk_enet_out:
1895 clk_disable_unprepare(fep->clk_ipg);
1896failed_clk_ipg:
1897 clk_disable_unprepare(fep->clk_ahb);
1898
1899 return ret;
1900}
1901
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001902static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001903{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001904 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001905 const struct platform_device_id *id_entry =
1906 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001907 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001908 char mdio_bus_id[MII_BUS_ID_SIZE];
1909 char phy_name[MII_BUS_ID_SIZE + 3];
1910 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001911 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001912
Bryan Wu418bd0d2010-05-28 03:40:39 -07001913 fep->phy_dev = NULL;
1914
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001915 if (fep->phy_node) {
1916 phy_dev = of_phy_connect(ndev, fep->phy_node,
1917 &fec_enet_adjust_link, 0,
1918 fep->phy_interface);
1919 } else {
1920 /* check for attached phy */
1921 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1922 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1923 continue;
1924 if (fep->mii_bus->phy_map[phy_id] == NULL)
1925 continue;
1926 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1927 continue;
1928 if (dev_id--)
1929 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001930 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001931 break;
1932 }
1933
1934 if (phy_id >= PHY_MAX_ADDR) {
1935 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001936 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001937 phy_id = 0;
1938 }
1939
1940 snprintf(phy_name, sizeof(phy_name),
1941 PHY_ID_FMT, mdio_bus_id, phy_id);
1942 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1943 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001944 }
1945
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001946 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001947 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001948 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001949 }
1950
1951 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001952 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001953 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001954 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001955#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001956 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001957#endif
Frank Libaa70a52013-01-16 16:55:58 +00001958 }
Shawn Guo230dec62011-09-23 02:12:48 +00001959 else
1960 phy_dev->supported &= PHY_BASIC_FEATURES;
1961
Bryan Wue6b043d2010-03-31 02:10:44 +00001962 phy_dev->advertising = phy_dev->supported;
1963
1964 fep->phy_dev = phy_dev;
1965 fep->link = 0;
1966 fep->full_duplex = 0;
1967
Joe Perches31b77202013-04-13 19:03:17 +00001968 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1969 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1970 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001971
Bryan Wue6b043d2010-03-31 02:10:44 +00001972 return 0;
1973}
1974
1975static int fec_enet_mii_init(struct platform_device *pdev)
1976{
Shawn Guob5680e02011-01-05 21:13:13 +00001977 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001978 struct net_device *ndev = platform_get_drvdata(pdev);
1979 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001980 const struct platform_device_id *id_entry =
1981 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001982 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001983 int err = -ENXIO, i;
1984
Shawn Guob5680e02011-01-05 21:13:13 +00001985 /*
1986 * The dual fec interfaces are not equivalent with enet-mac.
1987 * Here are the differences:
1988 *
1989 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1990 * - fec0 acts as the 1588 time master while fec1 is slave
1991 * - external phys can only be configured by fec0
1992 *
1993 * That is to say fec1 can not work independently. It only works
1994 * when fec0 is working. The reason behind this design is that the
1995 * second interface is added primarily for Switch mode.
1996 *
1997 * Because of the last point above, both phys are attached on fec0
1998 * mdio interface in board design, and need to be configured by
1999 * fec0 mii_bus.
2000 */
Shawn Guo43af9402011-12-05 05:01:15 +00002001 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00002002 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002003 if (mii_cnt && fec0_mii_bus) {
2004 fep->mii_bus = fec0_mii_bus;
2005 mii_cnt++;
2006 return 0;
2007 }
2008 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00002009 }
2010
Bryan Wue6b043d2010-03-31 02:10:44 +00002011 fep->mii_timeout = 0;
2012
2013 /*
2014 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00002015 *
2016 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2017 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2018 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2019 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00002020 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08002021 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00002022 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2023 fep->phy_speed--;
2024 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00002025 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2026
2027 fep->mii_bus = mdiobus_alloc();
2028 if (fep->mii_bus == NULL) {
2029 err = -ENOMEM;
2030 goto err_out;
2031 }
2032
2033 fep->mii_bus->name = "fec_enet_mii_bus";
2034 fep->mii_bus->read = fec_enet_mdio_read;
2035 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00002036 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2037 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00002038 fep->mii_bus->priv = fep;
2039 fep->mii_bus->parent = &pdev->dev;
2040
2041 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2042 if (!fep->mii_bus->irq) {
2043 err = -ENOMEM;
2044 goto err_out_free_mdiobus;
2045 }
2046
2047 for (i = 0; i < PHY_MAX_ADDR; i++)
2048 fep->mii_bus->irq[i] = PHY_POLL;
2049
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002050 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2051 if (node) {
2052 err = of_mdiobus_register(fep->mii_bus, node);
2053 of_node_put(node);
2054 } else {
2055 err = mdiobus_register(fep->mii_bus);
2056 }
2057
2058 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002059 goto err_out_free_mdio_irq;
2060
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002061 mii_cnt++;
2062
Shawn Guob5680e02011-01-05 21:13:13 +00002063 /* save fec0 mii_bus */
2064 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2065 fec0_mii_bus = fep->mii_bus;
2066
Bryan Wue6b043d2010-03-31 02:10:44 +00002067 return 0;
2068
Bryan Wue6b043d2010-03-31 02:10:44 +00002069err_out_free_mdio_irq:
2070 kfree(fep->mii_bus->irq);
2071err_out_free_mdiobus:
2072 mdiobus_free(fep->mii_bus);
2073err_out:
2074 return err;
2075}
2076
2077static void fec_enet_mii_remove(struct fec_enet_private *fep)
2078{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002079 if (--mii_cnt == 0) {
2080 mdiobus_unregister(fep->mii_bus);
2081 kfree(fep->mii_bus->irq);
2082 mdiobus_free(fep->mii_bus);
2083 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002084}
2085
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002086static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002087 struct ethtool_cmd *cmd)
2088{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002089 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002090 struct phy_device *phydev = fep->phy_dev;
2091
2092 if (!phydev)
2093 return -ENODEV;
2094
2095 return phy_ethtool_gset(phydev, cmd);
2096}
2097
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002098static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002099 struct ethtool_cmd *cmd)
2100{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002101 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002102 struct phy_device *phydev = fep->phy_dev;
2103
2104 if (!phydev)
2105 return -ENODEV;
2106
2107 return phy_ethtool_sset(phydev, cmd);
2108}
2109
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002110static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002111 struct ethtool_drvinfo *info)
2112{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002113 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Jiri Pirko7826d432013-01-06 00:44:26 +00002115 strlcpy(info->driver, fep->pdev->dev.driver->name,
2116 sizeof(info->driver));
2117 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2118 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
Bryan Wue6b043d2010-03-31 02:10:44 +00002120
Frank Li5ebae4892013-01-06 16:25:07 +00002121static int fec_enet_get_ts_info(struct net_device *ndev,
2122 struct ethtool_ts_info *info)
2123{
2124 struct fec_enet_private *fep = netdev_priv(ndev);
2125
2126 if (fep->bufdesc_ex) {
2127
2128 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2129 SOF_TIMESTAMPING_RX_SOFTWARE |
2130 SOF_TIMESTAMPING_SOFTWARE |
2131 SOF_TIMESTAMPING_TX_HARDWARE |
2132 SOF_TIMESTAMPING_RX_HARDWARE |
2133 SOF_TIMESTAMPING_RAW_HARDWARE;
2134 if (fep->ptp_clock)
2135 info->phc_index = ptp_clock_index(fep->ptp_clock);
2136 else
2137 info->phc_index = -1;
2138
2139 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2140 (1 << HWTSTAMP_TX_ON);
2141
2142 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2143 (1 << HWTSTAMP_FILTER_ALL);
2144 return 0;
2145 } else {
2146 return ethtool_op_get_ts_info(ndev, info);
2147 }
2148}
2149
Guenter Roeckd1391932013-06-18 10:04:59 -07002150#if !defined(CONFIG_M5272)
2151
Frank Libaa70a52013-01-16 16:55:58 +00002152static void fec_enet_get_pauseparam(struct net_device *ndev,
2153 struct ethtool_pauseparam *pause)
2154{
2155 struct fec_enet_private *fep = netdev_priv(ndev);
2156
2157 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2158 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2159 pause->rx_pause = pause->tx_pause;
2160}
2161
2162static int fec_enet_set_pauseparam(struct net_device *ndev,
2163 struct ethtool_pauseparam *pause)
2164{
2165 struct fec_enet_private *fep = netdev_priv(ndev);
2166
Russell King0b146ca2014-07-08 00:22:59 +01002167 if (!fep->phy_dev)
2168 return -ENODEV;
2169
Frank Libaa70a52013-01-16 16:55:58 +00002170 if (pause->tx_pause != pause->rx_pause) {
2171 netdev_info(ndev,
2172 "hardware only support enable/disable both tx and rx");
2173 return -EINVAL;
2174 }
2175
2176 fep->pause_flag = 0;
2177
2178 /* tx pause must be same as rx pause */
2179 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2180 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2181
2182 if (pause->rx_pause || pause->autoneg) {
2183 fep->phy_dev->supported |= ADVERTISED_Pause;
2184 fep->phy_dev->advertising |= ADVERTISED_Pause;
2185 } else {
2186 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2187 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2188 }
2189
2190 if (pause->autoneg) {
2191 if (netif_running(ndev))
2192 fec_stop(ndev);
2193 phy_start_aneg(fep->phy_dev);
2194 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002195 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002196 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002197 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002198 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002199 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002200 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002201 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002202 }
Frank Libaa70a52013-01-16 16:55:58 +00002203
2204 return 0;
2205}
2206
Chris Healy38ae92d2013-06-25 23:18:52 -07002207static const struct fec_stat {
2208 char name[ETH_GSTRING_LEN];
2209 u16 offset;
2210} fec_stats[] = {
2211 /* RMON TX */
2212 { "tx_dropped", RMON_T_DROP },
2213 { "tx_packets", RMON_T_PACKETS },
2214 { "tx_broadcast", RMON_T_BC_PKT },
2215 { "tx_multicast", RMON_T_MC_PKT },
2216 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2217 { "tx_undersize", RMON_T_UNDERSIZE },
2218 { "tx_oversize", RMON_T_OVERSIZE },
2219 { "tx_fragment", RMON_T_FRAG },
2220 { "tx_jabber", RMON_T_JAB },
2221 { "tx_collision", RMON_T_COL },
2222 { "tx_64byte", RMON_T_P64 },
2223 { "tx_65to127byte", RMON_T_P65TO127 },
2224 { "tx_128to255byte", RMON_T_P128TO255 },
2225 { "tx_256to511byte", RMON_T_P256TO511 },
2226 { "tx_512to1023byte", RMON_T_P512TO1023 },
2227 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2228 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2229 { "tx_octets", RMON_T_OCTETS },
2230
2231 /* IEEE TX */
2232 { "IEEE_tx_drop", IEEE_T_DROP },
2233 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2234 { "IEEE_tx_1col", IEEE_T_1COL },
2235 { "IEEE_tx_mcol", IEEE_T_MCOL },
2236 { "IEEE_tx_def", IEEE_T_DEF },
2237 { "IEEE_tx_lcol", IEEE_T_LCOL },
2238 { "IEEE_tx_excol", IEEE_T_EXCOL },
2239 { "IEEE_tx_macerr", IEEE_T_MACERR },
2240 { "IEEE_tx_cserr", IEEE_T_CSERR },
2241 { "IEEE_tx_sqe", IEEE_T_SQE },
2242 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2243 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2244
2245 /* RMON RX */
2246 { "rx_packets", RMON_R_PACKETS },
2247 { "rx_broadcast", RMON_R_BC_PKT },
2248 { "rx_multicast", RMON_R_MC_PKT },
2249 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2250 { "rx_undersize", RMON_R_UNDERSIZE },
2251 { "rx_oversize", RMON_R_OVERSIZE },
2252 { "rx_fragment", RMON_R_FRAG },
2253 { "rx_jabber", RMON_R_JAB },
2254 { "rx_64byte", RMON_R_P64 },
2255 { "rx_65to127byte", RMON_R_P65TO127 },
2256 { "rx_128to255byte", RMON_R_P128TO255 },
2257 { "rx_256to511byte", RMON_R_P256TO511 },
2258 { "rx_512to1023byte", RMON_R_P512TO1023 },
2259 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2260 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2261 { "rx_octets", RMON_R_OCTETS },
2262
2263 /* IEEE RX */
2264 { "IEEE_rx_drop", IEEE_R_DROP },
2265 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2266 { "IEEE_rx_crc", IEEE_R_CRC },
2267 { "IEEE_rx_align", IEEE_R_ALIGN },
2268 { "IEEE_rx_macerr", IEEE_R_MACERR },
2269 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2270 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2271};
2272
2273static void fec_enet_get_ethtool_stats(struct net_device *dev,
2274 struct ethtool_stats *stats, u64 *data)
2275{
2276 struct fec_enet_private *fep = netdev_priv(dev);
2277 int i;
2278
2279 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2280 data[i] = readl(fep->hwp + fec_stats[i].offset);
2281}
2282
2283static void fec_enet_get_strings(struct net_device *netdev,
2284 u32 stringset, u8 *data)
2285{
2286 int i;
2287 switch (stringset) {
2288 case ETH_SS_STATS:
2289 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2290 memcpy(data + i * ETH_GSTRING_LEN,
2291 fec_stats[i].name, ETH_GSTRING_LEN);
2292 break;
2293 }
2294}
2295
2296static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2297{
2298 switch (sset) {
2299 case ETH_SS_STATS:
2300 return ARRAY_SIZE(fec_stats);
2301 default:
2302 return -EOPNOTSUPP;
2303 }
2304}
Guenter Roeckd1391932013-06-18 10:04:59 -07002305#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002306
Chris Healy32bc9b42013-06-17 07:25:06 -07002307static int fec_enet_nway_reset(struct net_device *dev)
2308{
2309 struct fec_enet_private *fep = netdev_priv(dev);
2310 struct phy_device *phydev = fep->phy_dev;
2311
2312 if (!phydev)
2313 return -ENODEV;
2314
2315 return genphy_restart_aneg(phydev);
2316}
2317
Fugang Duand851b472014-09-17 05:18:52 +08002318/* ITR clock source is enet system clock (clk_ahb).
2319 * TCTT unit is cycle_ns * 64 cycle
2320 * So, the ICTT value = X us / (cycle_ns * 64)
2321 */
2322static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2323{
2324 struct fec_enet_private *fep = netdev_priv(ndev);
2325
2326 return us * (fep->itr_clk_rate / 64000) / 1000;
2327}
2328
2329/* Set threshold for interrupt coalescing */
2330static void fec_enet_itr_coal_set(struct net_device *ndev)
2331{
2332 struct fec_enet_private *fep = netdev_priv(ndev);
2333 const struct platform_device_id *id_entry =
2334 platform_get_device_id(fep->pdev);
2335 int rx_itr, tx_itr;
2336
2337 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2338 return;
2339
2340 /* Must be greater than zero to avoid unpredictable behavior */
2341 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2342 !fep->tx_time_itr || !fep->tx_pkts_itr)
2343 return;
2344
2345 /* Select enet system clock as Interrupt Coalescing
2346 * timer Clock Source
2347 */
2348 rx_itr = FEC_ITR_CLK_SEL;
2349 tx_itr = FEC_ITR_CLK_SEL;
2350
2351 /* set ICFT and ICTT */
2352 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2353 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2354 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2355 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2356
2357 rx_itr |= FEC_ITR_EN;
2358 tx_itr |= FEC_ITR_EN;
2359
2360 writel(tx_itr, fep->hwp + FEC_TXIC0);
2361 writel(rx_itr, fep->hwp + FEC_RXIC0);
2362 writel(tx_itr, fep->hwp + FEC_TXIC1);
2363 writel(rx_itr, fep->hwp + FEC_RXIC1);
2364 writel(tx_itr, fep->hwp + FEC_TXIC2);
2365 writel(rx_itr, fep->hwp + FEC_RXIC2);
2366}
2367
2368static int
2369fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2370{
2371 struct fec_enet_private *fep = netdev_priv(ndev);
2372 const struct platform_device_id *id_entry =
2373 platform_get_device_id(fep->pdev);
2374
2375 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2376 return -EOPNOTSUPP;
2377
2378 ec->rx_coalesce_usecs = fep->rx_time_itr;
2379 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2380
2381 ec->tx_coalesce_usecs = fep->tx_time_itr;
2382 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2383
2384 return 0;
2385}
2386
2387static int
2388fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2389{
2390 struct fec_enet_private *fep = netdev_priv(ndev);
2391 const struct platform_device_id *id_entry =
2392 platform_get_device_id(fep->pdev);
2393
2394 unsigned int cycle;
2395
2396 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2397 return -EOPNOTSUPP;
2398
2399 if (ec->rx_max_coalesced_frames > 255) {
2400 pr_err("Rx coalesced frames exceed hardware limiation");
2401 return -EINVAL;
2402 }
2403
2404 if (ec->tx_max_coalesced_frames > 255) {
2405 pr_err("Tx coalesced frame exceed hardware limiation");
2406 return -EINVAL;
2407 }
2408
2409 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2410 if (cycle > 0xFFFF) {
2411 pr_err("Rx coalesed usec exceeed hardware limiation");
2412 return -EINVAL;
2413 }
2414
2415 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2416 if (cycle > 0xFFFF) {
2417 pr_err("Rx coalesed usec exceeed hardware limiation");
2418 return -EINVAL;
2419 }
2420
2421 fep->rx_time_itr = ec->rx_coalesce_usecs;
2422 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2423
2424 fep->tx_time_itr = ec->tx_coalesce_usecs;
2425 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2426
2427 fec_enet_itr_coal_set(ndev);
2428
2429 return 0;
2430}
2431
2432static void fec_enet_itr_coal_init(struct net_device *ndev)
2433{
2434 struct ethtool_coalesce ec;
2435
2436 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2437 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2438
2439 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2440 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2441
2442 fec_enet_set_coalesce(ndev, &ec);
2443}
2444
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002445static int fec_enet_get_tunable(struct net_device *netdev,
2446 const struct ethtool_tunable *tuna,
2447 void *data)
2448{
2449 struct fec_enet_private *fep = netdev_priv(netdev);
2450 int ret = 0;
2451
2452 switch (tuna->id) {
2453 case ETHTOOL_RX_COPYBREAK:
2454 *(u32 *)data = fep->rx_copybreak;
2455 break;
2456 default:
2457 ret = -EINVAL;
2458 break;
2459 }
2460
2461 return ret;
2462}
2463
2464static int fec_enet_set_tunable(struct net_device *netdev,
2465 const struct ethtool_tunable *tuna,
2466 const void *data)
2467{
2468 struct fec_enet_private *fep = netdev_priv(netdev);
2469 int ret = 0;
2470
2471 switch (tuna->id) {
2472 case ETHTOOL_RX_COPYBREAK:
2473 fep->rx_copybreak = *(u32 *)data;
2474 break;
2475 default:
2476 ret = -EINVAL;
2477 break;
2478 }
2479
2480 return ret;
2481}
2482
stephen hemminger9b07be42012-01-04 12:59:49 +00002483static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002484 .get_settings = fec_enet_get_settings,
2485 .set_settings = fec_enet_set_settings,
2486 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002487 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002488 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002489 .get_coalesce = fec_enet_get_coalesce,
2490 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002491#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002492 .get_pauseparam = fec_enet_get_pauseparam,
2493 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002494 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002495 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002496 .get_sset_count = fec_enet_get_sset_count,
2497#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002498 .get_ts_info = fec_enet_get_ts_info,
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002499 .get_tunable = fec_enet_get_tunable,
2500 .set_tunable = fec_enet_set_tunable,
Bryan Wue6b043d2010-03-31 02:10:44 +00002501};
2502
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002503static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002504{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002505 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002506 struct phy_device *phydev = fep->phy_dev;
2507
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002508 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002509 return -EINVAL;
2510
2511 if (!phydev)
2512 return -ENODEV;
2513
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002514 if (fep->bufdesc_ex) {
2515 if (cmd == SIOCSHWTSTAMP)
2516 return fec_ptp_set(ndev, rq);
2517 if (cmd == SIOCGHWTSTAMP)
2518 return fec_ptp_get(ndev, rq);
2519 }
Frank Liff43da82013-01-03 16:04:23 +00002520
Richard Cochran28b04112010-07-17 08:48:55 +00002521 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002524static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002525{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002526 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002527 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002528 struct sk_buff *skb;
2529 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002530 struct fec_enet_priv_tx_q *txq;
2531 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f7462014-09-13 05:00:50 +08002532 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002533
Frank Li59d0f7462014-09-13 05:00:50 +08002534 for (q = 0; q < fep->num_rx_queues; q++) {
2535 rxq = fep->rx_queue[q];
2536 bdp = rxq->rx_bd_base;
2537 for (i = 0; i < rxq->rx_ring_size; i++) {
2538 skb = rxq->rx_skbuff[i];
2539 rxq->rx_skbuff[i] = NULL;
2540 if (skb) {
2541 dma_unmap_single(&fep->pdev->dev,
2542 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002543 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f7462014-09-13 05:00:50 +08002544 DMA_FROM_DEVICE);
2545 dev_kfree_skb(skb);
2546 }
2547 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002548 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002549 }
2550
Frank Li59d0f7462014-09-13 05:00:50 +08002551 for (q = 0; q < fep->num_tx_queues; q++) {
2552 txq = fep->tx_queue[q];
2553 bdp = txq->tx_bd_base;
2554 for (i = 0; i < txq->tx_ring_size; i++) {
2555 kfree(txq->tx_bounce[i]);
2556 txq->tx_bounce[i] = NULL;
2557 skb = txq->tx_skbuff[i];
2558 txq->tx_skbuff[i] = NULL;
2559 dev_kfree_skb(skb);
2560 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002561 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002562}
2563
Frank Li59d0f7462014-09-13 05:00:50 +08002564static void fec_enet_free_queue(struct net_device *ndev)
2565{
2566 struct fec_enet_private *fep = netdev_priv(ndev);
2567 int i;
2568 struct fec_enet_priv_tx_q *txq;
2569
2570 for (i = 0; i < fep->num_tx_queues; i++)
2571 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2572 txq = fep->tx_queue[i];
2573 dma_free_coherent(NULL,
2574 txq->tx_ring_size * TSO_HEADER_SIZE,
2575 txq->tso_hdrs,
2576 txq->tso_hdrs_dma);
2577 }
2578
2579 for (i = 0; i < fep->num_rx_queues; i++)
2580 if (fep->rx_queue[i])
2581 kfree(fep->rx_queue[i]);
2582
2583 for (i = 0; i < fep->num_tx_queues; i++)
2584 if (fep->tx_queue[i])
2585 kfree(fep->tx_queue[i]);
2586}
2587
2588static int fec_enet_alloc_queue(struct net_device *ndev)
2589{
2590 struct fec_enet_private *fep = netdev_priv(ndev);
2591 int i;
2592 int ret = 0;
2593 struct fec_enet_priv_tx_q *txq;
2594
2595 for (i = 0; i < fep->num_tx_queues; i++) {
2596 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2597 if (!txq) {
2598 ret = -ENOMEM;
2599 goto alloc_failed;
2600 }
2601
2602 fep->tx_queue[i] = txq;
2603 txq->tx_ring_size = TX_RING_SIZE;
2604 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2605
2606 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2607 txq->tx_wake_threshold =
2608 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2609
2610 txq->tso_hdrs = dma_alloc_coherent(NULL,
2611 txq->tx_ring_size * TSO_HEADER_SIZE,
2612 &txq->tso_hdrs_dma,
2613 GFP_KERNEL);
2614 if (!txq->tso_hdrs) {
2615 ret = -ENOMEM;
2616 goto alloc_failed;
2617 }
2618 }
2619
2620 for (i = 0; i < fep->num_rx_queues; i++) {
2621 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2622 GFP_KERNEL);
2623 if (!fep->rx_queue[i]) {
2624 ret = -ENOMEM;
2625 goto alloc_failed;
2626 }
2627
2628 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2629 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2630 }
2631 return ret;
2632
2633alloc_failed:
2634 fec_enet_free_queue(ndev);
2635 return ret;
2636}
2637
2638static int
2639fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002640{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002641 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002642 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002643 struct sk_buff *skb;
2644 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002645 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002646
Frank Li59d0f7462014-09-13 05:00:50 +08002647 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002648 bdp = rxq->rx_bd_base;
2649 for (i = 0; i < rxq->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00002650 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002651 if (!skb)
2652 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002653
Nimrod Andy1b7bde62014-09-30 09:28:05 +08002654 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
Russell King730ee362014-07-08 00:23:14 +01002655 dev_kfree_skb(skb);
Russell Kingffdce2c2014-07-08 00:23:30 +01002656 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002657 }
Russell King730ee362014-07-08 00:23:14 +01002658
Fugang Duan4d494cd2014-09-13 05:00:48 +08002659 rxq->rx_skbuff[i] = skb;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002660 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002661
2662 if (fep->bufdesc_ex) {
2663 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2664 ebdp->cbd_esc = BD_ENET_RX_INT;
2665 }
2666
Frank Li59d0f7462014-09-13 05:00:50 +08002667 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002668 }
2669
2670 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002671 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002672 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f7462014-09-13 05:00:50 +08002673 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002674
Frank Li59d0f7462014-09-13 05:00:50 +08002675 err_alloc:
2676 fec_enet_free_buffers(ndev);
2677 return -ENOMEM;
2678}
2679
2680static int
2681fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2682{
2683 struct fec_enet_private *fep = netdev_priv(ndev);
2684 unsigned int i;
2685 struct bufdesc *bdp;
2686 struct fec_enet_priv_tx_q *txq;
2687
2688 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002689 bdp = txq->tx_bd_base;
2690 for (i = 0; i < txq->tx_ring_size; i++) {
2691 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2692 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002693 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002694
2695 bdp->cbd_sc = 0;
2696 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002697
Frank Liff43da82013-01-03 16:04:23 +00002698 if (fep->bufdesc_ex) {
2699 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002700 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002701 }
2702
Frank Li59d0f7462014-09-13 05:00:50 +08002703 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002704 }
2705
2706 /* Set the last buffer to wrap. */
Frank Li59d0f7462014-09-13 05:00:50 +08002707 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002708 bdp->cbd_sc |= BD_SC_WRAP;
2709
2710 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002711
2712 err_alloc:
2713 fec_enet_free_buffers(ndev);
2714 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002715}
2716
Frank Li59d0f7462014-09-13 05:00:50 +08002717static int fec_enet_alloc_buffers(struct net_device *ndev)
2718{
2719 struct fec_enet_private *fep = netdev_priv(ndev);
2720 unsigned int i;
2721
2722 for (i = 0; i < fep->num_rx_queues; i++)
2723 if (fec_enet_alloc_rxq_buffers(ndev, i))
2724 return -ENOMEM;
2725
2726 for (i = 0; i < fep->num_tx_queues; i++)
2727 if (fec_enet_alloc_txq_buffers(ndev, i))
2728 return -ENOMEM;
2729 return 0;
2730}
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002733fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002735 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002736 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002738 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002739 ret = fec_enet_clk_enable(ndev, true);
2740 if (ret)
2741 return ret;
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* I should reset the ring buffers here, but I don't yet know
2744 * a simple way to do that.
2745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002747 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002748 if (ret)
2749 return ret;
2750
Bryan Wu418bd0d2010-05-28 03:40:39 -07002751 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002752 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002753 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002754 fec_enet_free_buffers(ndev);
Nimrod Andye3c96142014-09-23 15:40:58 +08002755 fec_enet_clk_enable(ndev, false);
2756 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002757 return ret;
2758 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002759
Russell Kingef833372014-07-08 12:40:43 +01002760 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002761 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002762 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002763 netif_tx_start_all_queues(ndev);
2764
Sascha Hauer22f6b862009-04-15 01:32:18 +00002765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
2768static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002769fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002771 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Russell Kingd76cfae2014-07-08 00:23:04 +01002773 phy_stop(fep->phy_dev);
2774
Russell King31a6de32014-07-08 12:40:23 +01002775 if (netif_device_present(ndev)) {
2776 napi_disable(&fep->napi);
2777 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002778 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Russell King635cf172014-07-08 00:22:54 +01002781 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002782 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002783
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002784 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002785 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002786 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return 0;
2789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791/* Set or clear the multicast filter for this adaptor.
2792 * Skeleton taken from sunlance driver.
2793 * The CPM Ethernet implementation allows Multicast as well as individual
2794 * MAC address filtering. Some of the drivers check to make sure it is
2795 * a group multicast address, and discard those that are not. I guess I
2796 * will do the same for now, but just remove the test if you want
2797 * individual filtering as well (do the upper net layers want or support
2798 * this kind of feature?).
2799 */
2800
2801#define HASH_BITS 6 /* #bits in hash */
2802#define CRC32_POLY 0xEDB88320
2803
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002804static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002806 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002807 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002808 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 unsigned char hash;
2810
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002811 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002812 tmp = readl(fep->hwp + FEC_R_CNTRL);
2813 tmp |= 0x8;
2814 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002815 return;
2816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Sascha Hauer4e831832009-04-15 01:32:19 +00002818 tmp = readl(fep->hwp + FEC_R_CNTRL);
2819 tmp &= ~0x8;
2820 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002821
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002822 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002823 /* Catch all multicast addresses, so set the
2824 * filter to all 1's
2825 */
2826 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2827 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Sascha Hauer4e831832009-04-15 01:32:19 +00002829 return;
2830 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002831
Sascha Hauer4e831832009-04-15 01:32:19 +00002832 /* Clear filter and add the addresses in hash register
2833 */
2834 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2835 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002837 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002838 /* calculate crc32 value of mac address */
2839 crc = 0xffffffff;
2840
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002841 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002842 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002843 for (bit = 0; bit < 8; bit++, data >>= 1) {
2844 crc = (crc >> 1) ^
2845 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002848
2849 /* only upper 6 bits (HASH_BITS) are used
2850 * which point to specific bit in he hash registers
2851 */
2852 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2853
2854 if (hash > 31) {
2855 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2856 tmp |= 1 << (hash - 32);
2857 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2858 } else {
2859 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2860 tmp |= 1 << hash;
2861 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864}
2865
Sascha Hauer22f6b862009-04-15 01:32:18 +00002866/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002867static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002868fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002870 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002871 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Lucas Stach44934fa2014-03-30 21:32:08 +02002873 if (addr) {
2874 if (!is_valid_ether_addr(addr->sa_data))
2875 return -EADDRNOTAVAIL;
2876 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2877 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002878
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002879 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2880 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002881 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002882 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002883 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002887#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002888/**
2889 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002890 * @dev: The FEC network adapter
2891 *
2892 * Polled functionality used by netconsole and others in non interrupt mode
2893 *
2894 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002895static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002896{
2897 int i;
2898 struct fec_enet_private *fep = netdev_priv(dev);
2899
2900 for (i = 0; i < FEC_IRQ_NUM; i++) {
2901 if (fep->irq[i] > 0) {
2902 disable_irq(fep->irq[i]);
2903 fec_enet_interrupt(fep->irq[i], dev);
2904 enable_irq(fep->irq[i]);
2905 }
2906 }
2907}
2908#endif
2909
Russell King8506fa12014-07-08 12:40:33 +01002910#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2911
Jim Baxter4c09eed2013-04-19 08:10:49 +00002912static int fec_set_features(struct net_device *netdev,
2913 netdev_features_t features)
2914{
2915 struct fec_enet_private *fep = netdev_priv(netdev);
2916 netdev_features_t changed = features ^ netdev->features;
2917
Russell King8506fa12014-07-08 12:40:33 +01002918 /* Quiesce the device if necessary */
2919 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2920 napi_disable(&fep->napi);
2921 netif_tx_lock_bh(netdev);
2922 fec_stop(netdev);
2923 }
2924
Jim Baxter4c09eed2013-04-19 08:10:49 +00002925 netdev->features = features;
2926
2927 /* Receive checksum has been changed */
2928 if (changed & NETIF_F_RXCSUM) {
2929 if (features & NETIF_F_RXCSUM)
2930 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2931 else
2932 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002933 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002934
Russell King8506fa12014-07-08 12:40:33 +01002935 /* Resume the device after updates */
2936 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002937 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002938 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002939 netif_tx_unlock_bh(netdev);
2940 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002941 }
2942
2943 return 0;
2944}
2945
Sascha Hauer009fda82009-04-15 01:32:23 +00002946static const struct net_device_ops fec_netdev_ops = {
2947 .ndo_open = fec_enet_open,
2948 .ndo_stop = fec_enet_close,
2949 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002950 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002951 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002952 .ndo_validate_addr = eth_validate_addr,
2953 .ndo_tx_timeout = fec_timeout,
2954 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002955 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002956#ifdef CONFIG_NET_POLL_CONTROLLER
2957 .ndo_poll_controller = fec_poll_controller,
2958#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002959 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002960};
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 /*
2963 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002964 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002966static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002968 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002969 const struct platform_device_id *id_entry =
2970 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002971 struct fec_enet_priv_tx_q *txq;
2972 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002973 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002974 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002975 int bd_size;
Frank Li59d0f7462014-09-13 05:00:50 +08002976 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002977
Fugang Duan41ef84c2014-09-13 05:00:54 +08002978#if defined(CONFIG_ARM)
2979 fep->rx_align = 0xf;
2980 fep->tx_align = 0xf;
2981#else
2982 fep->rx_align = 0x3;
2983 fep->tx_align = 0x3;
2984#endif
2985
Frank Li59d0f7462014-09-13 05:00:50 +08002986 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002987
Nimrod Andy55d02182014-06-12 08:16:21 +08002988 if (fep->bufdesc_ex)
2989 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2990 else
2991 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002992 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002993 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002995 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002996 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002997 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002998 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002999 return -ENOMEM;
3000 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10003001
Fugang Duan4d494cd2014-09-13 05:00:48 +08003002 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Shawn Guo49da97d2011-01-05 21:13:11 +00003004 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003005 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02003006 /* make sure MAC we just acquired is programmed into the hw */
3007 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00003009 /* Set receive and transmit descriptor base. */
Frank Li59d0f7462014-09-13 05:00:50 +08003010 for (i = 0; i < fep->num_rx_queues; i++) {
3011 rxq = fep->rx_queue[i];
3012 rxq->index = i;
3013 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3014 rxq->bd_dma = bd_dma;
3015 if (fep->bufdesc_ex) {
3016 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3017 cbd_base = (struct bufdesc *)
3018 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3019 } else {
3020 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3021 cbd_base += rxq->rx_ring_size;
3022 }
3023 }
3024
3025 for (i = 0; i < fep->num_tx_queues; i++) {
3026 txq = fep->tx_queue[i];
3027 txq->index = i;
3028 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3029 txq->bd_dma = bd_dma;
3030 if (fep->bufdesc_ex) {
3031 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3032 cbd_base = (struct bufdesc *)
3033 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3034 } else {
3035 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3036 cbd_base += txq->tx_ring_size;
3037 }
3038 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08003039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Sascha Hauer22f6b862009-04-15 01:32:18 +00003041 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01003042 ndev->watchdog_timeo = TX_TIMEOUT;
3043 ndev->netdev_ops = &fec_netdev_ops;
3044 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00003045
Frank Lidc975382013-01-28 18:31:42 +00003046 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03003047 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00003048
Nimrod Andy09d1e542014-06-12 08:16:20 +08003049 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01003050 /* enable hw VLAN support */
3051 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01003052
Shawn Guo48496252013-05-08 21:08:22 +00003053 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08003054 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3055
Shawn Guo48496252013-05-08 21:08:22 +00003056 /* enable hw accelerator */
3057 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08003058 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00003059 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3060 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00003061
Fugang Duan41ef84c2014-09-13 05:00:54 +08003062 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3063 fep->tx_align = 0;
3064 fep->rx_align = 0x3f;
3065 }
3066
Nimrod Andy09d1e542014-06-12 08:16:20 +08003067 ndev->hw_features = ndev->features;
3068
Russell Kingef833372014-07-08 12:40:43 +01003069 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return 0;
3072}
3073
Shawn Guoca2cc332011-06-25 02:04:35 +08003074#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05003075static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003076{
3077 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00003078 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003079 struct device_node *np = pdev->dev.of_node;
3080
3081 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003082 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003083
Shawn Guoa3caad02012-06-27 03:45:24 +00003084 of_property_read_u32(np, "phy-reset-duration", &msec);
3085 /* A sane reset duration should not be longer than 1s */
3086 if (msec > 1000)
3087 msec = 1;
3088
Shawn Guoca2cc332011-06-25 02:04:35 +08003089 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003090 if (!gpio_is_valid(phy_reset))
3091 return;
3092
Shawn Guo119fc002012-06-27 03:45:22 +00003093 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3094 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003095 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003096 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003097 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003098 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003099 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003100 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003101}
3102#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003103static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003104{
3105 /*
3106 * In case of platform probe, the reset has been done
3107 * by machine code.
3108 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003109}
3110#endif /* CONFIG_OF */
3111
Fugang Duan9fc095f2014-09-13 05:00:49 +08003112static void
3113fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3114{
3115 struct device_node *np = pdev->dev.of_node;
3116 int err;
3117
3118 *num_tx = *num_rx = 1;
3119
3120 if (!np || !of_device_is_available(np))
3121 return;
3122
3123 /* parse the num of tx and rx queues */
3124 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003125 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003126 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003127
3128 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3129 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003130 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003131
3132 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003133 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3134 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003135 *num_tx = 1;
3136 return;
3137 }
3138
3139 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003140 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3141 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003142 *num_rx = 1;
3143 return;
3144 }
3145
3146}
3147
Bill Pemberton33897cc2012-12-03 09:23:58 -05003148static int
Sascha Haueread73182009-01-28 23:03:11 +00003149fec_probe(struct platform_device *pdev)
3150{
3151 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003152 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003153 struct net_device *ndev;
3154 int i, irq, ret = 0;
3155 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003156 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003157 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003158 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003159 int num_tx_qs;
3160 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003161
3162 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3163 if (of_id)
3164 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003165
Fugang Duan9fc095f2014-09-13 05:00:49 +08003166 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3167
Sascha Haueread73182009-01-28 23:03:11 +00003168 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003169 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3170 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003171 if (!ndev)
3172 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003173
3174 SET_NETDEV_DEV(ndev, &pdev->dev);
3175
3176 /* setup board info structure */
3177 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003178
Fugang Duan9fc095f2014-09-13 05:00:49 +08003179 fep->num_rx_queues = num_rx_qs;
3180 fep->num_tx_queues = num_tx_qs;
3181
Guenter Roeckd1391932013-06-18 10:04:59 -07003182#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003183 /* default enable pause frame auto negotiation */
3184 if (pdev->id_entry &&
3185 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3186 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003187#endif
Frank Libaa70a52013-01-16 16:55:58 +00003188
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003189 /* Select default pin state */
3190 pinctrl_pm_select_default_state(&pdev->dev);
3191
Fabio Estevam399db752013-07-21 13:25:03 -03003192 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303193 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3194 if (IS_ERR(fep->hwp)) {
3195 ret = PTR_ERR(fep->hwp);
3196 goto failed_ioremap;
3197 }
3198
Bryan Wue6b043d2010-03-31 02:10:44 +00003199 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003200 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003201
Frank Liff43da82013-01-03 16:04:23 +00003202 fep->bufdesc_ex = 0;
3203
Sascha Haueread73182009-01-28 23:03:11 +00003204 platform_set_drvdata(pdev, ndev);
3205
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003206 phy_node = of_parse_phandle(np, "phy-handle", 0);
3207 if (!phy_node && of_phy_is_fixed_link(np)) {
3208 ret = of_phy_register_fixed_link(np);
3209 if (ret < 0) {
3210 dev_err(&pdev->dev,
3211 "broken fixed-link specification\n");
3212 goto failed_phy;
3213 }
3214 phy_node = of_node_get(np);
3215 }
3216 fep->phy_node = phy_node;
3217
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003218 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003219 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003220 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003221 if (pdata)
3222 fep->phy_interface = pdata->phy;
3223 else
3224 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3225 } else {
3226 fep->phy_interface = ret;
3227 }
3228
Fabio Estevame2f8d552013-02-22 06:40:45 +00003229 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3230 if (IS_ERR(fep->clk_ipg)) {
3231 ret = PTR_ERR(fep->clk_ipg);
3232 goto failed_clk;
3233 }
3234
3235 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3236 if (IS_ERR(fep->clk_ahb)) {
3237 ret = PTR_ERR(fep->clk_ahb);
3238 goto failed_clk;
3239 }
3240
Fugang Duand851b472014-09-17 05:18:52 +08003241 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3242
Linus Torvalds38f56f32013-05-07 11:06:17 -07003243 /* enet_out is optional, depends on board */
3244 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3245 if (IS_ERR(fep->clk_enet_out))
3246 fep->clk_enet_out = NULL;
3247
Nimrod Andy91c0d982014-08-21 17:09:38 +08003248 fep->ptp_clk_on = false;
3249 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003250
3251 /* clk_ref is optional, depends on board */
3252 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3253 if (IS_ERR(fep->clk_ref))
3254 fep->clk_ref = NULL;
3255
Fabio Estevame2f8d552013-02-22 06:40:45 +00003256 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003257 fep->bufdesc_ex =
3258 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003259 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003260 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003261 fep->bufdesc_ex = 0;
3262 }
3263
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003264 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003265 if (ret)
3266 goto failed_clk;
3267
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003268 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3269 if (!IS_ERR(fep->reg_phy)) {
3270 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003271 if (ret) {
3272 dev_err(&pdev->dev,
3273 "Failed to enable phy regulator: %d\n", ret);
3274 goto failed_regulator;
3275 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003276 } else {
3277 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003278 }
3279
3280 fec_reset_phy(pdev);
3281
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003282 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003283 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003284
3285 ret = fec_enet_init(ndev);
3286 if (ret)
3287 goto failed_init;
3288
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003289 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003290 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003291 if (irq < 0) {
3292 if (i)
3293 break;
3294 ret = irq;
3295 goto failed_irq;
3296 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003297 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003298 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003299 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003300 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003301 }
3302
Fugang Duanb4d39b52014-09-13 05:00:55 +08003303 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003304 ret = fec_enet_mii_init(pdev);
3305 if (ret)
3306 goto failed_mii_init;
3307
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003308 /* Carrier starts down, phylib will bring it up */
3309 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003310 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003311 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003312
Sascha Haueread73182009-01-28 23:03:11 +00003313 ret = register_netdev(ndev);
3314 if (ret)
3315 goto failed_register;
3316
Fabio Estevameb1d0642013-04-13 07:25:36 +00003317 if (fep->bufdesc_ex && fep->ptp_clock)
3318 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3319
Nimrod Andy1b7bde62014-09-30 09:28:05 +08003320 fep->rx_copybreak = COPYBREAK_DEFAULT;
Russell King36cdc742014-07-08 13:01:44 +01003321 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003322 return 0;
3323
3324failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003325 fec_enet_mii_remove(fep);
3326failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003327failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003328failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003329 if (fep->reg_phy)
3330 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003331failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003332 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003333failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003334failed_phy:
3335 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003336failed_ioremap:
3337 free_netdev(ndev);
3338
3339 return ret;
3340}
3341
Bill Pemberton33897cc2012-12-03 09:23:58 -05003342static int
Sascha Haueread73182009-01-28 23:03:11 +00003343fec_drv_remove(struct platform_device *pdev)
3344{
3345 struct net_device *ndev = platform_get_drvdata(pdev);
3346 struct fec_enet_private *fep = netdev_priv(ndev);
3347
Nimrod Andy91c0d982014-08-21 17:09:38 +08003348 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003349 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003350 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003351 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003352 if (fep->reg_phy)
3353 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003354 if (fep->ptp_clock)
3355 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003356 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003357 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003358 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003359
Sascha Haueread73182009-01-28 23:03:11 +00003360 return 0;
3361}
3362
Fabio Estevamdd66d382014-07-24 18:24:01 -03003363static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003364{
Eric Benard87cad5c2010-06-18 04:19:54 +00003365 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003366 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003367
Russell Kingda1774e2014-07-08 12:39:57 +01003368 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003369 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003370 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003371 napi_disable(&fep->napi);
3372 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003373 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003374 netif_tx_unlock_bh(ndev);
3375 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003376 }
Russell Kingda1774e2014-07-08 12:39:57 +01003377 rtnl_unlock();
3378
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003379 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003380 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003381
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003382 if (fep->reg_phy)
3383 regulator_disable(fep->reg_phy);
3384
Sascha Haueread73182009-01-28 23:03:11 +00003385 return 0;
3386}
3387
Fabio Estevamdd66d382014-07-24 18:24:01 -03003388static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003389{
Eric Benard87cad5c2010-06-18 04:19:54 +00003390 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003391 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003392 int ret;
3393
3394 if (fep->reg_phy) {
3395 ret = regulator_enable(fep->reg_phy);
3396 if (ret)
3397 return ret;
3398 }
Sascha Haueread73182009-01-28 23:03:11 +00003399
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003400 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003401 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003402 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003403 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003404
Russell Kingda1774e2014-07-08 12:39:57 +01003405 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003406 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003407 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003408 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003409 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003410 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003411 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003412 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003413 }
Russell Kingda1774e2014-07-08 12:39:57 +01003414 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003415
Sascha Haueread73182009-01-28 23:03:11 +00003416 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003417
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003418failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003419 if (fep->reg_phy)
3420 regulator_disable(fep->reg_phy);
3421 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003422}
3423
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003424static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003425
Sascha Haueread73182009-01-28 23:03:11 +00003426static struct platform_driver fec_driver = {
3427 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003428 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003429 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003430 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003431 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003432 },
Shawn Guob5680e02011-01-05 21:13:13 +00003433 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003434 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003435 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003436};
3437
Fabio Estevamaaca2372012-01-23 16:44:50 +00003438module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003440MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441MODULE_LICENSE("GPL");