blob: 3d12a841ad1500cdafb1ac5571a96c6108fcd8f3 [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 Andy79f33912014-06-12 08:16:23 +0800239#define TSO_HEADER_SIZE 128
240/* Max number of allowed TCP segments for software TSO */
241#define FEC_MAX_TSO_SEGS 100
242#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
243
244#define IS_TSO_HEADER(txq, addr) \
245 ((addr >= txq->tso_hdrs_dma) && \
246 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
247
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000248static int mii_cnt;
249
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800250static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800251struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
252 struct fec_enet_private *fep,
253 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000254{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800255 struct bufdesc *new_bd = bdp + 1;
256 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800257 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
258 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800259 struct bufdesc_ex *ex_base;
260 struct bufdesc *base;
261 int ring_size;
262
Fugang Duan4d494cd2014-09-13 05:00:48 +0800263 if (bdp >= txq->tx_bd_base) {
264 base = txq->tx_bd_base;
265 ring_size = txq->tx_ring_size;
266 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800267 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800268 base = rxq->rx_bd_base;
269 ring_size = rxq->rx_ring_size;
270 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800271 }
272
273 if (fep->bufdesc_ex)
274 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
275 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000276 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800277 return (new_bd >= (base + ring_size)) ?
278 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000279}
280
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800281static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800282struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
283 struct fec_enet_private *fep,
284 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000285{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800286 struct bufdesc *new_bd = bdp - 1;
287 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800288 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
289 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800290 struct bufdesc_ex *ex_base;
291 struct bufdesc *base;
292 int ring_size;
293
Fugang Duan4d494cd2014-09-13 05:00:48 +0800294 if (bdp >= txq->tx_bd_base) {
295 base = txq->tx_bd_base;
296 ring_size = txq->tx_ring_size;
297 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800298 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800299 base = rxq->rx_bd_base;
300 ring_size = rxq->rx_ring_size;
301 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800302 }
303
304 if (fep->bufdesc_ex)
305 return (struct bufdesc *)((ex_new_bd < ex_base) ?
306 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000307 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800308 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000309}
310
Nimrod Andy61a44272014-06-12 08:16:18 +0800311static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
312 struct fec_enet_private *fep)
313{
314 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
315}
316
Fugang Duan4d494cd2014-09-13 05:00:48 +0800317static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
318 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800319{
320 int entries;
321
Fugang Duan4d494cd2014-09-13 05:00:48 +0800322 entries = ((const char *)txq->dirty_tx -
323 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800324
Fugang Duan4d494cd2014-09-13 05:00:48 +0800325 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800326}
327
Shawn Guob5680e02011-01-05 21:13:13 +0000328static void *swap_buffer(void *bufaddr, int len)
329{
330 int i;
331 unsigned int *buf = bufaddr;
332
Fabio Estevamffed61e2013-05-21 05:44:26 +0000333 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000334 *buf = cpu_to_be32(*buf);
335
336 return bufaddr;
337}
338
Russell King344756f2014-07-08 13:01:59 +0100339static void fec_dump(struct net_device *ndev)
340{
341 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800342 struct bufdesc *bdp;
343 struct fec_enet_priv_tx_q *txq;
344 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100345
346 netdev_info(ndev, "TX ring dump\n");
347 pr_info("Nr SC addr len SKB\n");
348
Fugang Duan4d494cd2014-09-13 05:00:48 +0800349 txq = fep->tx_queue[0];
350 bdp = txq->tx_bd_base;
351
Russell King344756f2014-07-08 13:01:59 +0100352 do {
353 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
354 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800355 bdp == txq->cur_tx ? 'S' : ' ',
356 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100357 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800358 txq->tx_skbuff[index]);
359 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100360 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800361 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100362}
363
Fugang Duan62a02c92014-06-18 08:33:52 +0800364static inline bool is_ipv4_pkt(struct sk_buff *skb)
365{
366 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
367}
368
Jim Baxter4c09eed2013-04-19 08:10:49 +0000369static int
370fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
371{
372 /* Only run for packets requiring a checksum. */
373 if (skb->ip_summed != CHECKSUM_PARTIAL)
374 return 0;
375
376 if (unlikely(skb_cow_head(skb, 0)))
377 return -1;
378
Fugang Duan62a02c92014-06-18 08:33:52 +0800379 if (is_ipv4_pkt(skb))
380 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000381 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
382
383 return 0;
384}
385
Nimrod Andy6e909282014-06-12 08:16:22 +0800386static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800387fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
388 struct sk_buff *skb,
389 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800390{
391 struct fec_enet_private *fep = netdev_priv(ndev);
392 const struct platform_device_id *id_entry =
393 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800394 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800395 struct bufdesc_ex *ebdp;
396 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800397 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800398 int frag, frag_len;
399 unsigned short status;
400 unsigned int estatus = 0;
401 skb_frag_t *this_frag;
402 unsigned int index;
403 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100404 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800405 int i;
406
407 for (frag = 0; frag < nr_frags; frag++) {
408 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800409 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800410 ebdp = (struct bufdesc_ex *)bdp;
411
412 status = bdp->cbd_sc;
413 status &= ~BD_ENET_TX_STATS;
414 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
415 frag_len = skb_shinfo(skb)->frags[frag].size;
416
417 /* Handle the last BD specially */
418 if (frag == nr_frags - 1) {
419 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
420 if (fep->bufdesc_ex) {
421 estatus |= BD_ENET_TX_INT;
422 if (unlikely(skb_shinfo(skb)->tx_flags &
423 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
424 estatus |= BD_ENET_TX_TS;
425 }
426 }
427
428 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800429 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
430 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800431 if (skb->ip_summed == CHECKSUM_PARTIAL)
432 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
433 ebdp->cbd_bdu = 0;
434 ebdp->cbd_esc = estatus;
435 }
436
437 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
438
Fugang Duan4d494cd2014-09-13 05:00:48 +0800439 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800440 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800441 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800442 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
443 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800444
445 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
446 swap_buffer(bufaddr, frag_len);
447 }
448
Russell Kingd6bf3142014-07-08 00:23:19 +0100449 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
450 DMA_TO_DEVICE);
451 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800452 dev_kfree_skb_any(skb);
453 if (net_ratelimit())
454 netdev_err(ndev, "Tx DMA memory map failed\n");
455 goto dma_mapping_error;
456 }
457
Russell Kingd6bf3142014-07-08 00:23:19 +0100458 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800459 bdp->cbd_datlen = frag_len;
460 bdp->cbd_sc = status;
461 }
462
Fugang Duan4d494cd2014-09-13 05:00:48 +0800463 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800464
465 return 0;
466
467dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800468 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800469 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800470 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800471 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
472 bdp->cbd_datlen, DMA_TO_DEVICE);
473 }
474 return NETDEV_TX_OK;
475}
476
Fugang Duan4d494cd2014-09-13 05:00:48 +0800477static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
478 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800479{
480 struct fec_enet_private *fep = netdev_priv(ndev);
481 const struct platform_device_id *id_entry =
482 platform_get_device_id(fep->pdev);
483 int nr_frags = skb_shinfo(skb)->nr_frags;
484 struct bufdesc *bdp, *last_bdp;
485 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100486 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800487 unsigned short status;
488 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800489 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800490 unsigned int estatus = 0;
491 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800492 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800493 int ret;
494
Fugang Duan4d494cd2014-09-13 05:00:48 +0800495 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800496 if (entries_free < MAX_SKB_FRAGS + 1) {
497 dev_kfree_skb_any(skb);
498 if (net_ratelimit())
499 netdev_err(ndev, "NOT enough BD for SG!\n");
500 return NETDEV_TX_OK;
501 }
502
Nimrod Andy6e909282014-06-12 08:16:22 +0800503 /* Protocol checksum off-load for TCP and UDP. */
504 if (fec_enet_clear_csum(skb, ndev)) {
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507 }
508
509 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800510 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800511 status = bdp->cbd_sc;
512 status &= ~BD_ENET_TX_STATS;
513
514 /* Set buffer length and buffer pointer */
515 bufaddr = skb->data;
516 buflen = skb_headlen(skb);
517
Fugang Duan4d494cd2014-09-13 05:00:48 +0800518 queue = skb_get_queue_mapping(skb);
519 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800520 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800521 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800522 memcpy(txq->tx_bounce[index], skb->data, buflen);
523 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800524
525 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
526 swap_buffer(bufaddr, buflen);
527 }
528
Russell Kingd6bf3142014-07-08 00:23:19 +0100529 /* Push the data cache so the CPM does not get stale memory data. */
530 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
531 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800532 dev_kfree_skb_any(skb);
533 if (net_ratelimit())
534 netdev_err(ndev, "Tx DMA memory map failed\n");
535 return NETDEV_TX_OK;
536 }
537
538 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800539 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800540 if (ret)
541 return ret;
542 } else {
543 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
544 if (fep->bufdesc_ex) {
545 estatus = BD_ENET_TX_INT;
546 if (unlikely(skb_shinfo(skb)->tx_flags &
547 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
548 estatus |= BD_ENET_TX_TS;
549 }
550 }
551
552 if (fep->bufdesc_ex) {
553
554 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
555
556 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
557 fep->hwts_tx_en))
558 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
559
Nimrod Andybefe8212014-09-23 15:40:55 +0800560 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
561 estatus |= FEC_TX_BD_FTYPE(queue);
562
Nimrod Andy6e909282014-06-12 08:16:22 +0800563 if (skb->ip_summed == CHECKSUM_PARTIAL)
564 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
565
566 ebdp->cbd_bdu = 0;
567 ebdp->cbd_esc = estatus;
568 }
569
Fugang Duan4d494cd2014-09-13 05:00:48 +0800570 last_bdp = txq->cur_tx;
571 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800572 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800573 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800574
575 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100576 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800577
578 /* Send it on its way. Tell FEC it's ready, interrupt when done,
579 * it's the last BD of the frame, and to put the CRC on the end.
580 */
581 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
582 bdp->cbd_sc = status;
583
Sascha Hauer22f6b862009-04-15 01:32:18 +0000584 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800585 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800587 skb_tx_timestamp(skb);
588
Fugang Duan4d494cd2014-09-13 05:00:48 +0800589 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Frank Lide5fb0a2013-03-03 17:34:25 +0000591 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800592 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000593
Nimrod Andy6e909282014-06-12 08:16:22 +0800594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Nimrod Andy79f33912014-06-12 08:16:23 +0800597static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800598fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
599 struct net_device *ndev,
600 struct bufdesc *bdp, int index, char *data,
601 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800602{
603 struct fec_enet_private *fep = netdev_priv(ndev);
604 const struct platform_device_id *id_entry =
605 platform_get_device_id(fep->pdev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200606 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800607 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800608 unsigned short status;
609 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100610 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800611
612 status = bdp->cbd_sc;
613 status &= ~BD_ENET_TX_STATS;
614
615 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800616
Fugang Duan41ef84c2014-09-13 05:00:54 +0800617 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800618 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800619 memcpy(txq->tx_bounce[index], data, size);
620 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800621
622 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
623 swap_buffer(data, size);
624 }
625
Russell Kingd6bf3142014-07-08 00:23:19 +0100626 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
627 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800628 dev_kfree_skb_any(skb);
629 if (net_ratelimit())
630 netdev_err(ndev, "Tx DMA memory map failed\n");
631 return NETDEV_TX_BUSY;
632 }
633
Russell Kingd6bf3142014-07-08 00:23:19 +0100634 bdp->cbd_datlen = size;
635 bdp->cbd_bufaddr = addr;
636
Nimrod Andy79f33912014-06-12 08:16:23 +0800637 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800638 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
639 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800640 if (skb->ip_summed == CHECKSUM_PARTIAL)
641 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
642 ebdp->cbd_bdu = 0;
643 ebdp->cbd_esc = estatus;
644 }
645
646 /* Handle the last BD specially */
647 if (last_tcp)
648 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
649 if (is_last) {
650 status |= BD_ENET_TX_INTR;
651 if (fep->bufdesc_ex)
652 ebdp->cbd_esc |= BD_ENET_TX_INT;
653 }
654
655 bdp->cbd_sc = status;
656
657 return 0;
658}
659
660static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800661fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
662 struct sk_buff *skb, struct net_device *ndev,
663 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800664{
665 struct fec_enet_private *fep = netdev_priv(ndev);
666 const struct platform_device_id *id_entry =
667 platform_get_device_id(fep->pdev);
668 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200669 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andybefe8212014-09-23 15:40:55 +0800670 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800671 void *bufaddr;
672 unsigned long dmabuf;
673 unsigned short status;
674 unsigned int estatus = 0;
675
676 status = bdp->cbd_sc;
677 status &= ~BD_ENET_TX_STATS;
678 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
679
Fugang Duan4d494cd2014-09-13 05:00:48 +0800680 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
681 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800682 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800683 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800684 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
685 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800686
687 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
688 swap_buffer(bufaddr, hdr_len);
689
690 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
691 hdr_len, DMA_TO_DEVICE);
692 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
693 dev_kfree_skb_any(skb);
694 if (net_ratelimit())
695 netdev_err(ndev, "Tx DMA memory map failed\n");
696 return NETDEV_TX_BUSY;
697 }
698 }
699
700 bdp->cbd_bufaddr = dmabuf;
701 bdp->cbd_datlen = hdr_len;
702
703 if (fep->bufdesc_ex) {
Nimrod Andybefe8212014-09-23 15:40:55 +0800704 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
705 estatus |= FEC_TX_BD_FTYPE(queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800706 if (skb->ip_summed == CHECKSUM_PARTIAL)
707 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
708 ebdp->cbd_bdu = 0;
709 ebdp->cbd_esc = estatus;
710 }
711
712 bdp->cbd_sc = status;
713
714 return 0;
715}
716
Fugang Duan4d494cd2014-09-13 05:00:48 +0800717static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
718 struct sk_buff *skb,
719 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800720{
721 struct fec_enet_private *fep = netdev_priv(ndev);
722 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
723 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800724 struct bufdesc *bdp = txq->cur_tx;
725 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800726 struct tso_t tso;
727 unsigned int index = 0;
728 int ret;
Fugang Duan37d60172014-09-17 05:18:54 +0800729 const struct platform_device_id *id_entry =
730 platform_get_device_id(fep->pdev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800731
Fugang Duan4d494cd2014-09-13 05:00:48 +0800732 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800733 dev_kfree_skb_any(skb);
734 if (net_ratelimit())
735 netdev_err(ndev, "NOT enough BD for TSO!\n");
736 return NETDEV_TX_OK;
737 }
738
739 /* Protocol checksum off-load for TCP and UDP. */
740 if (fec_enet_clear_csum(skb, ndev)) {
741 dev_kfree_skb_any(skb);
742 return NETDEV_TX_OK;
743 }
744
745 /* Initialize the TSO handler, and prepare the first payload */
746 tso_start(skb, &tso);
747
748 total_len = skb->len - hdr_len;
749 while (total_len > 0) {
750 char *hdr;
751
Fugang Duan4d494cd2014-09-13 05:00:48 +0800752 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800753 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
754 total_len -= data_left;
755
756 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800757 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800758 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800759 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800760 if (ret)
761 goto err_release;
762
763 while (data_left > 0) {
764 int size;
765
766 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800767 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
768 index = fec_enet_get_bd_index(txq->tx_bd_base,
769 bdp, fep);
770 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
771 bdp, index,
772 tso.data, size,
773 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800774 total_len == 0);
775 if (ret)
776 goto err_release;
777
778 data_left -= size;
779 tso_build_data(skb, &tso, size);
780 }
781
Fugang Duan4d494cd2014-09-13 05:00:48 +0800782 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800783 }
784
785 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800786 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800787
Nimrod Andy79f33912014-06-12 08:16:23 +0800788 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800789 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800790
791 /* Trigger transmission start */
Fugang Duan37d60172014-09-17 05:18:54 +0800792 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
793 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
794 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
795 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
796 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
797 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800798
799 return 0;
800
801err_release:
802 /* TODO: Release all used data descriptors for TSO */
803 return ret;
804}
805
Nimrod Andy61a44272014-06-12 08:16:18 +0800806static netdev_tx_t
807fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
808{
809 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800810 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800811 unsigned short queue;
812 struct fec_enet_priv_tx_q *txq;
813 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800814 int ret;
815
Fugang Duan4d494cd2014-09-13 05:00:48 +0800816 queue = skb_get_queue_mapping(skb);
817 txq = fep->tx_queue[queue];
818 nq = netdev_get_tx_queue(ndev, queue);
819
Nimrod Andy79f33912014-06-12 08:16:23 +0800820 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800821 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800822 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800823 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800824 if (ret)
825 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800826
Fugang Duan4d494cd2014-09-13 05:00:48 +0800827 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
828 if (entries_free <= txq->tx_stop_threshold)
829 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800830
831 return NETDEV_TX_OK;
832}
833
Frank Li14109a52013-03-26 16:12:03 +0000834/* Init RX & TX buffer descriptors
835 */
836static void fec_enet_bd_init(struct net_device *dev)
837{
838 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800839 struct fec_enet_priv_tx_q *txq;
840 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000841 struct bufdesc *bdp;
842 unsigned int i;
Frank Li59d0f742014-09-13 05:00:50 +0800843 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000844
Frank Li59d0f742014-09-13 05:00:50 +0800845 for (q = 0; q < fep->num_rx_queues; q++) {
846 /* Initialize the receive buffer descriptors. */
847 rxq = fep->rx_queue[q];
848 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800849
Frank Li59d0f742014-09-13 05:00:50 +0800850 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000851
Frank Li59d0f742014-09-13 05:00:50 +0800852 /* Initialize the BD for every fragment in the page. */
853 if (bdp->cbd_bufaddr)
854 bdp->cbd_sc = BD_ENET_RX_EMPTY;
855 else
856 bdp->cbd_sc = 0;
857 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000858 }
Frank Li59d0f742014-09-13 05:00:50 +0800859
860 /* Set the last buffer to wrap */
861 bdp = fec_enet_get_prevdesc(bdp, fep, q);
862 bdp->cbd_sc |= BD_SC_WRAP;
863
864 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000865 }
866
Frank Li59d0f742014-09-13 05:00:50 +0800867 for (q = 0; q < fep->num_tx_queues; q++) {
868 /* ...and the same for transmit */
869 txq = fep->tx_queue[q];
870 bdp = txq->tx_bd_base;
871 txq->cur_tx = bdp;
872
873 for (i = 0; i < txq->tx_ring_size; i++) {
874 /* Initialize the BD for every fragment in the page. */
875 bdp->cbd_sc = 0;
876 if (txq->tx_skbuff[i]) {
877 dev_kfree_skb_any(txq->tx_skbuff[i]);
878 txq->tx_skbuff[i] = NULL;
879 }
880 bdp->cbd_bufaddr = 0;
881 bdp = fec_enet_get_nextdesc(bdp, fep, q);
882 }
883
884 /* Set the last buffer to wrap */
885 bdp = fec_enet_get_prevdesc(bdp, fep, q);
886 bdp->cbd_sc |= BD_SC_WRAP;
887 txq->dirty_tx = bdp;
888 }
889}
890
Frank Lice99d0d2014-09-13 05:00:52 +0800891static void fec_enet_active_rxring(struct net_device *ndev)
892{
893 struct fec_enet_private *fep = netdev_priv(ndev);
894 int i;
895
896 for (i = 0; i < fep->num_rx_queues; i++)
897 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
898}
899
Frank Li59d0f742014-09-13 05:00:50 +0800900static void fec_enet_enable_ring(struct net_device *ndev)
901{
902 struct fec_enet_private *fep = netdev_priv(ndev);
903 struct fec_enet_priv_tx_q *txq;
904 struct fec_enet_priv_rx_q *rxq;
905 int i;
906
907 for (i = 0; i < fep->num_rx_queues; i++) {
908 rxq = fep->rx_queue[i];
909 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
910
911 /* enable DMA1/2 */
912 if (i)
913 writel(RCMR_MATCHEN | RCMR_CMP(i),
914 fep->hwp + FEC_RCMR(i));
915 }
916
917 for (i = 0; i < fep->num_tx_queues; i++) {
918 txq = fep->tx_queue[i];
919 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
920
921 /* enable DMA1/2 */
922 if (i)
923 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
924 fep->hwp + FEC_DMA_CFG(i));
925 }
926}
927
928static void fec_enet_reset_skb(struct net_device *ndev)
929{
930 struct fec_enet_private *fep = netdev_priv(ndev);
931 struct fec_enet_priv_tx_q *txq;
932 int i, j;
933
934 for (i = 0; i < fep->num_tx_queues; i++) {
935 txq = fep->tx_queue[i];
936
937 for (j = 0; j < txq->tx_ring_size; j++) {
938 if (txq->tx_skbuff[j]) {
939 dev_kfree_skb_any(txq->tx_skbuff[j]);
940 txq->tx_skbuff[j] = NULL;
941 }
942 }
943 }
Frank Li14109a52013-03-26 16:12:03 +0000944}
945
Russell Kingdbc64a82014-07-08 12:40:12 +0100946/*
947 * This function is called to start or restart the FEC during a link
948 * change, transmit timeout, or to reconfigure the FEC. The network
949 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100950 */
951static void
Russell Kingef833372014-07-08 12:40:43 +0100952fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100953{
954 struct fec_enet_private *fep = netdev_priv(ndev);
955 const struct platform_device_id *id_entry =
956 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000957 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100958 u32 temp_mac[2];
959 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000960 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100961
Fugang Duan106c3142014-09-13 05:00:51 +0800962 /* Whack a reset. We should wait for this.
963 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
964 * instead of reset MAC itself.
965 */
966 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
967 writel(0, fep->hwp + FEC_ECNTRL);
968 } else {
969 writel(1, fep->hwp + FEC_ECNTRL);
970 udelay(10);
971 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100972
973 /*
974 * enet-mac reset will reset mac address registers too,
975 * so need to reconfigure it.
976 */
977 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
978 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
979 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
980 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
981 }
982
983 /* Clear any outstanding interrupt. */
984 writel(0xffc00000, fep->hwp + FEC_IEVENT);
985
Uwe Kleine-König45993652011-01-19 20:47:04 +0100986 /* Set maximum receive buffer size. */
987 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
988
Frank Li14109a52013-03-26 16:12:03 +0000989 fec_enet_bd_init(ndev);
990
Frank Li59d0f742014-09-13 05:00:50 +0800991 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100992
Frank Li59d0f742014-09-13 05:00:50 +0800993 /* Reset tx SKB buffers. */
994 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100995
996 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100997 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100998 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100999 writel(0x04, fep->hwp + FEC_X_CNTRL);
1000 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001001 /* No Rcv on Xmit */
1002 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001003 writel(0x0, fep->hwp + FEC_X_CNTRL);
1004 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001005
Uwe Kleine-König45993652011-01-19 20:47:04 +01001006 /* Set MII speed */
1007 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1008
Guenter Roeckd1391932013-06-18 10:04:59 -07001009#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +00001010 /* set RX checksum */
1011 val = readl(fep->hwp + FEC_RACC);
1012 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1013 val |= FEC_RACC_OPTIONS;
1014 else
1015 val &= ~FEC_RACC_OPTIONS;
1016 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -07001017#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001018
Uwe Kleine-König45993652011-01-19 20:47:04 +01001019 /*
1020 * The phy interface and speed need to get configured
1021 * differently on enet-mac.
1022 */
1023 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001024 /* Enable flow control and length check */
1025 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001026
Shawn Guo230dec62011-09-23 02:12:48 +00001027 /* RGMII, RMII or MII */
1028 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1029 rcntl |= (1 << 6);
1030 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001031 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001032 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001033 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001034
Shawn Guo230dec62011-09-23 02:12:48 +00001035 /* 1G, 100M or 10M */
1036 if (fep->phy_dev) {
1037 if (fep->phy_dev->speed == SPEED_1000)
1038 ecntl |= (1 << 5);
1039 else if (fep->phy_dev->speed == SPEED_100)
1040 rcntl &= ~(1 << 9);
1041 else
1042 rcntl |= (1 << 9);
1043 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001044 } else {
1045#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001046 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001047 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001048 /* disable the gasket and wait */
1049 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1050 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1051 udelay(1);
1052
1053 /*
1054 * configure the gasket:
1055 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001056 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001057 */
Eric Benard8d82f212012-01-12 06:10:28 +00001058 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1059 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1060 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1061 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1062 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001063
1064 /* re-enable the gasket */
1065 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1066 }
1067#endif
1068 }
Frank Libaa70a52013-01-16 16:55:58 +00001069
Guenter Roeckd1391932013-06-18 10:04:59 -07001070#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001071 /* enable pause frame*/
1072 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1073 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1074 fep->phy_dev && fep->phy_dev->pause)) {
1075 rcntl |= FEC_ENET_FCE;
1076
Jim Baxter4c09eed2013-04-19 08:10:49 +00001077 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001078 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1079 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1080 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1081 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1082
1083 /* OPD */
1084 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1085 } else {
1086 rcntl &= ~FEC_ENET_FCE;
1087 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001088#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001089
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001090 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001091
Stefan Wahren84fe6182014-03-12 11:28:19 +01001092 /* Setup multicast filter. */
1093 set_multicast_list(ndev);
1094#ifndef CONFIG_M5272
1095 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1096 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1097#endif
1098
Shawn Guo230dec62011-09-23 02:12:48 +00001099 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1100 /* enable ENET endian swap */
1101 ecntl |= (1 << 8);
1102 /* enable ENET store and forward mode */
1103 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1104 }
1105
Frank Liff43da82013-01-03 16:04:23 +00001106 if (fep->bufdesc_ex)
1107 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001108
Chris Healy38ae92d2013-06-25 23:18:52 -07001109#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001110 /* Enable the MIB statistic event counters */
1111 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001112#endif
1113
Uwe Kleine-König45993652011-01-19 20:47:04 +01001114 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001115 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001116 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001117
Frank Liff43da82013-01-03 16:04:23 +00001118 if (fep->bufdesc_ex)
1119 fec_ptp_start_cyclecounter(ndev);
1120
Uwe Kleine-König45993652011-01-19 20:47:04 +01001121 /* Enable interrupts we wish to service */
1122 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001123
1124 /* Init the interrupt coalescing */
1125 fec_enet_itr_coal_init(ndev);
1126
Uwe Kleine-König45993652011-01-19 20:47:04 +01001127}
1128
1129static void
1130fec_stop(struct net_device *ndev)
1131{
1132 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001133 const struct platform_device_id *id_entry =
1134 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001135 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001136
1137 /* We cannot expect a graceful transmit stop without link !!! */
1138 if (fep->link) {
1139 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1140 udelay(10);
1141 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001142 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001143 }
1144
Fugang Duan106c3142014-09-13 05:00:51 +08001145 /* Whack a reset. We should wait for this.
1146 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1147 * instead of reset MAC itself.
1148 */
1149 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1150 writel(0, fep->hwp + FEC_ECNTRL);
1151 } else {
1152 writel(1, fep->hwp + FEC_ECNTRL);
1153 udelay(10);
1154 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001155 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1156 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001157
1158 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001159 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001160 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001161 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1162 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001163}
1164
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001167fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001169 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Russell King344756f2014-07-08 13:01:59 +01001171 fec_dump(ndev);
1172
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001173 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Russell King36cdc742014-07-08 13:01:44 +01001175 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001176}
1177
Russell King36cdc742014-07-08 13:01:44 +01001178static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001179{
1180 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001181 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001182 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001183
Russell King36cdc742014-07-08 13:01:44 +01001184 rtnl_lock();
1185 if (netif_device_present(ndev) || netif_running(ndev)) {
1186 napi_disable(&fep->napi);
1187 netif_tx_lock_bh(ndev);
1188 fec_restart(ndev);
1189 netif_wake_queue(ndev);
1190 netif_tx_unlock_bh(ndev);
1191 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001192 }
Russell King36cdc742014-07-08 13:01:44 +01001193 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001197fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1198 struct skb_shared_hwtstamps *hwtstamps)
1199{
1200 unsigned long flags;
1201 u64 ns;
1202
1203 spin_lock_irqsave(&fep->tmreg_lock, flags);
1204 ns = timecounter_cyc2time(&fep->tc, ts);
1205 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1206
1207 memset(hwtstamps, 0, sizeof(*hwtstamps));
1208 hwtstamps->hwtstamp = ns_to_ktime(ns);
1209}
1210
1211static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001212fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001215 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001216 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001218 struct fec_enet_priv_tx_q *txq;
1219 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001220 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001221 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001223 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001224
1225 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1226
1227 txq = fep->tx_queue[queue_id];
1228 /* get next bdp of dirty_tx */
1229 nq = netdev_get_tx_queue(ndev, queue_id);
1230 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Frank Lide5fb0a2013-03-03 17:34:25 +00001232 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001233 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001234
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001235 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001236
1237 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001238 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001239 break;
1240
Fugang Duan4d494cd2014-09-13 05:00:48 +08001241 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001242
Fugang Duan4d494cd2014-09-13 05:00:48 +08001243 skb = txq->tx_skbuff[index];
1244 txq->tx_skbuff[index] = NULL;
1245 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001246 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1247 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001248 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001249 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001250 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001251 continue;
1252 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001255 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 BD_ENET_TX_RL | BD_ENET_TX_UN |
1257 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001258 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001259 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001260 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001261 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001262 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001263 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001264 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001265 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001266 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001267 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001268 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001270 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001271 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Frank Liff43da82013-01-03 16:04:23 +00001274 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1275 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001276 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001277 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001278
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001279 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001280 skb_tstamp_tx(skb, &shhwtstamps);
1281 }
Frank Liff43da82013-01-03 16:04:23 +00001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* Deferred means some collisions occurred during transmit,
1284 * but we eventually sent the packet OK.
1285 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001286 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001287 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001288
Sascha Hauer22f6b862009-04-15 01:32:18 +00001289 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001291
Fugang Duan4d494cd2014-09-13 05:00:48 +08001292 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001293
Sascha Hauer22f6b862009-04-15 01:32:18 +00001294 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001295 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001296
Sascha Hauer22f6b862009-04-15 01:32:18 +00001297 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001299 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001300 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1301 if (entries_free >= txq->tx_wake_threshold)
1302 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Russell Kingccea2962014-07-08 13:01:38 +01001305
1306 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001307 if (bdp != txq->cur_tx &&
1308 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1309 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1310}
1311
1312static void
1313fec_enet_tx(struct net_device *ndev)
1314{
1315 struct fec_enet_private *fep = netdev_priv(ndev);
1316 u16 queue_id;
1317 /* First process class A queue, then Class B and Best Effort queue */
1318 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1319 clear_bit(queue_id, &fep->work_tx);
1320 fec_enet_tx_queue(ndev, queue_id);
1321 }
1322 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/* During a receive, the cur_rx points to the current incoming buffer.
1326 * When we update through the ring, if the next incoming buffer has
1327 * not been given to the system, we just set the empty indicator,
1328 * effectively tossing the packet.
1329 */
Frank Lidc975382013-01-28 18:31:42 +00001330static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001331fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001333 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001334 const struct platform_device_id *id_entry =
1335 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001336 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001337 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001338 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct sk_buff *skb;
1340 ushort pkt_len;
1341 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001342 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001343 struct bufdesc_ex *ebdp = NULL;
1344 bool vlan_packet_rcvd = false;
1345 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001346 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001347
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001348#ifdef CONFIG_M532x
1349 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001350#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001351 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1352 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* First, grab all of the stats for the incoming packet.
1355 * These get messed up if we get called due to a busy condition.
1356 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001357 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Sascha Hauer22f6b862009-04-15 01:32:18 +00001359 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Frank Lidc975382013-01-28 18:31:42 +00001361 if (pkt_received >= budget)
1362 break;
1363 pkt_received++;
1364
Sascha Hauer22f6b862009-04-15 01:32:18 +00001365 /* Since we have allocated space to hold a complete frame,
1366 * the last indicator should be set.
1367 */
1368 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001369 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Russell Kingdb3421c2014-07-08 13:01:49 +01001371
Sascha Hauer22f6b862009-04-15 01:32:18 +00001372 /* Check for errors. */
1373 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001375 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001376 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1377 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001378 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001379 }
1380 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001381 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001382 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001383 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001384 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001385 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001387
1388 /* Report late collisions as a frame error.
1389 * On this error, the BD is closed, but we don't know what we
1390 * have in the buffer. So, just drop this frame on the floor.
1391 */
1392 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001393 ndev->stats.rx_errors++;
1394 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001395 goto rx_processing_done;
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Sascha Hauer22f6b862009-04-15 01:32:18 +00001398 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001399 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001400 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001401 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Fugang Duan4d494cd2014-09-13 05:00:48 +08001403 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1404 data = rxq->rx_skbuff[index]->data;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001405 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08001406 FEC_ENET_RX_FRSIZE - fep->rx_align,
1407 DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001408
Shawn Guob5680e02011-01-05 21:13:13 +00001409 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1410 swap_buffer(data, pkt_len);
1411
Jim Baxtercdffcf12013-07-02 22:52:56 +01001412 /* Extract the enhanced buffer descriptor */
1413 ebdp = NULL;
1414 if (fep->bufdesc_ex)
1415 ebdp = (struct bufdesc_ex *)bdp;
1416
1417 /* If this is a VLAN packet remove the VLAN Tag */
1418 vlan_packet_rcvd = false;
1419 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001420 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001421 /* Push and remove the vlan tag */
1422 struct vlan_hdr *vlan_header =
1423 (struct vlan_hdr *) (data + ETH_HLEN);
1424 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1425 pkt_len -= VLAN_HLEN;
1426
1427 vlan_packet_rcvd = true;
1428 }
1429
Sascha Hauer22f6b862009-04-15 01:32:18 +00001430 /* This does 16 byte alignment, exactly what we need.
1431 * The packet length includes FCS, but we don't want to
1432 * include that when passing upstream as it messes up
1433 * bridging applications.
1434 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001435 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Sascha Hauer85498892009-04-15 01:32:21 +00001437 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001438 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001439 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001440 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001441 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001442 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001443
1444 /* Extract the frame data without the VLAN header. */
1445 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1446 if (vlan_packet_rcvd)
1447 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
Nimrod Andyb749fc92014-09-19 14:26:03 +08001448 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
Jim Baxtercdffcf12013-07-02 22:52:56 +01001449 data + payload_offset,
1450 pkt_len - 4 - (2 * ETH_ALEN));
1451
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001452 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001453
Frank Li6605b732012-10-30 18:25:31 +00001454 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001455 if (fep->hwts_rx_en && fep->bufdesc_ex)
1456 fec_enet_hwtstamp(fep, ebdp->ts,
1457 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001458
Jim Baxter4c09eed2013-04-19 08:10:49 +00001459 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001460 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001461 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1462 /* don't check it */
1463 skb->ip_summed = CHECKSUM_UNNECESSARY;
1464 } else {
1465 skb_checksum_none_assert(skb);
1466 }
1467 }
1468
Jim Baxtercdffcf12013-07-02 22:52:56 +01001469 /* Handle received VLAN packets */
1470 if (vlan_packet_rcvd)
1471 __vlan_hwaccel_put_tag(skb,
1472 htons(ETH_P_8021Q),
1473 vlan_tag);
1474
Richard Cochran0affdf32013-08-30 20:28:10 +02001475 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001476 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001477
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001478 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08001479 FEC_ENET_RX_FRSIZE - fep->rx_align,
1480 DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001481rx_processing_done:
1482 /* Clear the status flags for this buffer */
1483 status &= ~BD_ENET_RX_STATS;
1484
1485 /* Mark the buffer empty */
1486 status |= BD_ENET_RX_EMPTY;
1487 bdp->cbd_sc = status;
1488
Frank Liff43da82013-01-03 16:04:23 +00001489 if (fep->bufdesc_ex) {
1490 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1491
1492 ebdp->cbd_esc = BD_ENET_RX_INT;
1493 ebdp->cbd_prot = 0;
1494 ebdp->cbd_bdu = 0;
1495 }
Frank Li6605b732012-10-30 18:25:31 +00001496
Sascha Hauer22f6b862009-04-15 01:32:18 +00001497 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001498 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001499
Sascha Hauer22f6b862009-04-15 01:32:18 +00001500 /* Doing this here will keep the FEC running while we process
1501 * incoming frames. On a heavily loaded network, we should be
1502 * able to keep up at the expense of system resources.
1503 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001504 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001506 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001507 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Fugang Duan4d494cd2014-09-13 05:00:48 +08001510static int
1511fec_enet_rx(struct net_device *ndev, int budget)
1512{
1513 int pkt_received = 0;
1514 u16 queue_id;
1515 struct fec_enet_private *fep = netdev_priv(ndev);
1516
1517 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1518 clear_bit(queue_id, &fep->work_rx);
1519 pkt_received += fec_enet_rx_queue(ndev,
1520 budget - pkt_received, queue_id);
1521 }
1522 return pkt_received;
1523}
1524
1525static bool
1526fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1527{
1528 if (int_events == 0)
1529 return false;
1530
1531 if (int_events & FEC_ENET_RXF)
1532 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001533 if (int_events & FEC_ENET_RXF_1)
1534 fep->work_rx |= (1 << 0);
1535 if (int_events & FEC_ENET_RXF_2)
1536 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001537
1538 if (int_events & FEC_ENET_TXF)
1539 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001540 if (int_events & FEC_ENET_TXF_1)
1541 fep->work_tx |= (1 << 0);
1542 if (int_events & FEC_ENET_TXF_2)
1543 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001544
1545 return true;
1546}
1547
Uwe Kleine-König45993652011-01-19 20:47:04 +01001548static irqreturn_t
1549fec_enet_interrupt(int irq, void *dev_id)
1550{
1551 struct net_device *ndev = dev_id;
1552 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001553 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001554 uint int_events;
1555 irqreturn_t ret = IRQ_NONE;
1556
Russell King7a168072014-07-08 00:22:44 +01001557 int_events = readl(fep->hwp + FEC_IEVENT);
1558 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001559 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001560
Russell King7a168072014-07-08 00:22:44 +01001561 if (int_events & napi_mask) {
1562 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001563
Russell King7a168072014-07-08 00:22:44 +01001564 /* Disable the NAPI interrupts */
1565 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1566 napi_schedule(&fep->napi);
1567 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001568
Russell King7a168072014-07-08 00:22:44 +01001569 if (int_events & FEC_ENET_MII) {
1570 ret = IRQ_HANDLED;
1571 complete(&fep->mdio_done);
1572 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001573
1574 return ret;
1575}
1576
Frank Lidc975382013-01-28 18:31:42 +00001577static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1578{
1579 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001580 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001581 int pkts;
1582
1583 /*
1584 * Clear any pending transmit or receive interrupts before
1585 * processing the rings to avoid racing with the hardware.
1586 */
1587 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1588
1589 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001590
Frank Lide5fb0a2013-03-03 17:34:25 +00001591 fec_enet_tx(ndev);
1592
Frank Lidc975382013-01-28 18:31:42 +00001593 if (pkts < budget) {
1594 napi_complete(napi);
1595 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1596 }
1597 return pkts;
1598}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001601static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001603 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001604 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001605 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Shawn Guo49da97d2011-01-05 21:13:11 +00001607 /*
1608 * try to get mac address in following order:
1609 *
1610 * 1) module parameter via kernel command line in form
1611 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1612 */
1613 iap = macaddr;
1614
1615 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001616 * 2) from device tree data
1617 */
1618 if (!is_valid_ether_addr(iap)) {
1619 struct device_node *np = fep->pdev->dev.of_node;
1620 if (np) {
1621 const char *mac = of_get_mac_address(np);
1622 if (mac)
1623 iap = (unsigned char *) mac;
1624 }
1625 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001626
1627 /*
1628 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001629 */
1630 if (!is_valid_ether_addr(iap)) {
1631#ifdef CONFIG_M5272
1632 if (FEC_FLASHMAC)
1633 iap = (unsigned char *)FEC_FLASHMAC;
1634#else
1635 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001636 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001637#endif
1638 }
1639
1640 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001641 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001642 */
1643 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001644 *((__be32 *) &tmpaddr[0]) =
1645 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1646 *((__be16 *) &tmpaddr[4]) =
1647 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 iap = &tmpaddr[0];
1649 }
1650
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001651 /*
1652 * 5) random mac address
1653 */
1654 if (!is_valid_ether_addr(iap)) {
1655 /* Report it and use a random ethernet address instead */
1656 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1657 eth_hw_addr_random(ndev);
1658 netdev_info(ndev, "Using random MAC address: %pM\n",
1659 ndev->dev_addr);
1660 return;
1661 }
1662
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001663 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Shawn Guo49da97d2011-01-05 21:13:11 +00001665 /* Adjust MAC if using macaddr */
1666 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001667 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670/* ------------------------------------------------------------------------- */
1671
Bryan Wue6b043d2010-03-31 02:10:44 +00001672/*
1673 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001675static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001677 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001678 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001679 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Bryan Wue6b043d2010-03-31 02:10:44 +00001681 /* Prevent a state halted on mii error */
1682 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1683 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001686
Russell King8ce56242014-07-08 12:40:07 +01001687 /*
1688 * If the netdev is down, or is going down, we're not interested
1689 * in link state events, so just mark our idea of the link as down
1690 * and ignore the event.
1691 */
1692 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1693 fep->link = 0;
1694 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001695 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001696 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001697 status_change = 1;
1698 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001699
Russell Kingef833372014-07-08 12:40:43 +01001700 if (fep->full_duplex != phy_dev->duplex) {
1701 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001702 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001703 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001704
1705 if (phy_dev->speed != fep->speed) {
1706 fep->speed = phy_dev->speed;
1707 status_change = 1;
1708 }
1709
1710 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001711 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001712 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001713 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001714 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001715 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001716 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001717 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001718 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001719 } else {
1720 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001721 napi_disable(&fep->napi);
1722 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001723 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001724 netif_tx_unlock_bh(ndev);
1725 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001726 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001727 status_change = 1;
1728 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001729 }
1730
Bryan Wue6b043d2010-03-31 02:10:44 +00001731 if (status_change)
1732 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Bryan Wue6b043d2010-03-31 02:10:44 +00001735static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Bryan Wue6b043d2010-03-31 02:10:44 +00001737 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001738 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001739
1740 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001741 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001742
1743 /* start a read op */
1744 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1745 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1746 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1747
1748 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001749 time_left = wait_for_completion_timeout(&fep->mdio_done,
1750 usecs_to_jiffies(FEC_MII_TIMEOUT));
1751 if (time_left == 0) {
1752 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001753 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001754 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001755 }
1756
1757 /* return value */
1758 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1759}
1760
1761static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1762 u16 value)
1763{
1764 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001765 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001766
1767 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001768 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001769
Shawn Guo862f0982011-01-05 21:13:09 +00001770 /* start a write op */
1771 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001772 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1773 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1774 fep->hwp + FEC_MII_DATA);
1775
1776 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001777 time_left = wait_for_completion_timeout(&fep->mdio_done,
1778 usecs_to_jiffies(FEC_MII_TIMEOUT));
1779 if (time_left == 0) {
1780 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001781 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001782 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001783 }
1784
1785 return 0;
1786}
1787
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001788static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1789{
1790 struct fec_enet_private *fep = netdev_priv(ndev);
1791 int ret;
1792
1793 if (enable) {
1794 ret = clk_prepare_enable(fep->clk_ahb);
1795 if (ret)
1796 return ret;
1797 ret = clk_prepare_enable(fep->clk_ipg);
1798 if (ret)
1799 goto failed_clk_ipg;
1800 if (fep->clk_enet_out) {
1801 ret = clk_prepare_enable(fep->clk_enet_out);
1802 if (ret)
1803 goto failed_clk_enet_out;
1804 }
1805 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001806 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001807 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001808 if (ret) {
1809 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001810 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001811 } else {
1812 fep->ptp_clk_on = true;
1813 }
1814 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001815 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001816 if (fep->clk_ref) {
1817 ret = clk_prepare_enable(fep->clk_ref);
1818 if (ret)
1819 goto failed_clk_ref;
1820 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001821 } else {
1822 clk_disable_unprepare(fep->clk_ahb);
1823 clk_disable_unprepare(fep->clk_ipg);
1824 if (fep->clk_enet_out)
1825 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001826 if (fep->clk_ptp) {
1827 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001828 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001829 fep->ptp_clk_on = false;
1830 mutex_unlock(&fep->ptp_clk_mutex);
1831 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001832 if (fep->clk_ref)
1833 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001834 }
1835
1836 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001837
1838failed_clk_ref:
1839 if (fep->clk_ref)
1840 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001841failed_clk_ptp:
1842 if (fep->clk_enet_out)
1843 clk_disable_unprepare(fep->clk_enet_out);
1844failed_clk_enet_out:
1845 clk_disable_unprepare(fep->clk_ipg);
1846failed_clk_ipg:
1847 clk_disable_unprepare(fep->clk_ahb);
1848
1849 return ret;
1850}
1851
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001852static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001853{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001854 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001855 const struct platform_device_id *id_entry =
1856 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001857 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001858 char mdio_bus_id[MII_BUS_ID_SIZE];
1859 char phy_name[MII_BUS_ID_SIZE + 3];
1860 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001861 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001862
Bryan Wu418bd0d2010-05-28 03:40:39 -07001863 fep->phy_dev = NULL;
1864
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001865 if (fep->phy_node) {
1866 phy_dev = of_phy_connect(ndev, fep->phy_node,
1867 &fec_enet_adjust_link, 0,
1868 fep->phy_interface);
1869 } else {
1870 /* check for attached phy */
1871 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1872 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1873 continue;
1874 if (fep->mii_bus->phy_map[phy_id] == NULL)
1875 continue;
1876 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1877 continue;
1878 if (dev_id--)
1879 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001880 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001881 break;
1882 }
1883
1884 if (phy_id >= PHY_MAX_ADDR) {
1885 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001886 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001887 phy_id = 0;
1888 }
1889
1890 snprintf(phy_name, sizeof(phy_name),
1891 PHY_ID_FMT, mdio_bus_id, phy_id);
1892 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1893 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001894 }
1895
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001896 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001897 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001898 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001899 }
1900
1901 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001902 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001903 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001904 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001905#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001906 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001907#endif
Frank Libaa70a52013-01-16 16:55:58 +00001908 }
Shawn Guo230dec62011-09-23 02:12:48 +00001909 else
1910 phy_dev->supported &= PHY_BASIC_FEATURES;
1911
Bryan Wue6b043d2010-03-31 02:10:44 +00001912 phy_dev->advertising = phy_dev->supported;
1913
1914 fep->phy_dev = phy_dev;
1915 fep->link = 0;
1916 fep->full_duplex = 0;
1917
Joe Perches31b77202013-04-13 19:03:17 +00001918 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1919 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1920 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001921
Bryan Wue6b043d2010-03-31 02:10:44 +00001922 return 0;
1923}
1924
1925static int fec_enet_mii_init(struct platform_device *pdev)
1926{
Shawn Guob5680e02011-01-05 21:13:13 +00001927 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001928 struct net_device *ndev = platform_get_drvdata(pdev);
1929 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001930 const struct platform_device_id *id_entry =
1931 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001932 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001933 int err = -ENXIO, i;
1934
Shawn Guob5680e02011-01-05 21:13:13 +00001935 /*
1936 * The dual fec interfaces are not equivalent with enet-mac.
1937 * Here are the differences:
1938 *
1939 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1940 * - fec0 acts as the 1588 time master while fec1 is slave
1941 * - external phys can only be configured by fec0
1942 *
1943 * That is to say fec1 can not work independently. It only works
1944 * when fec0 is working. The reason behind this design is that the
1945 * second interface is added primarily for Switch mode.
1946 *
1947 * Because of the last point above, both phys are attached on fec0
1948 * mdio interface in board design, and need to be configured by
1949 * fec0 mii_bus.
1950 */
Shawn Guo43af9402011-12-05 05:01:15 +00001951 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001952 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001953 if (mii_cnt && fec0_mii_bus) {
1954 fep->mii_bus = fec0_mii_bus;
1955 mii_cnt++;
1956 return 0;
1957 }
1958 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001959 }
1960
Bryan Wue6b043d2010-03-31 02:10:44 +00001961 fep->mii_timeout = 0;
1962
1963 /*
1964 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001965 *
1966 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1967 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1968 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1969 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001970 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001971 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001972 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1973 fep->phy_speed--;
1974 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001975 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1976
1977 fep->mii_bus = mdiobus_alloc();
1978 if (fep->mii_bus == NULL) {
1979 err = -ENOMEM;
1980 goto err_out;
1981 }
1982
1983 fep->mii_bus->name = "fec_enet_mii_bus";
1984 fep->mii_bus->read = fec_enet_mdio_read;
1985 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001986 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1987 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001988 fep->mii_bus->priv = fep;
1989 fep->mii_bus->parent = &pdev->dev;
1990
1991 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1992 if (!fep->mii_bus->irq) {
1993 err = -ENOMEM;
1994 goto err_out_free_mdiobus;
1995 }
1996
1997 for (i = 0; i < PHY_MAX_ADDR; i++)
1998 fep->mii_bus->irq[i] = PHY_POLL;
1999
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002000 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2001 if (node) {
2002 err = of_mdiobus_register(fep->mii_bus, node);
2003 of_node_put(node);
2004 } else {
2005 err = mdiobus_register(fep->mii_bus);
2006 }
2007
2008 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00002009 goto err_out_free_mdio_irq;
2010
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002011 mii_cnt++;
2012
Shawn Guob5680e02011-01-05 21:13:13 +00002013 /* save fec0 mii_bus */
2014 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2015 fec0_mii_bus = fep->mii_bus;
2016
Bryan Wue6b043d2010-03-31 02:10:44 +00002017 return 0;
2018
Bryan Wue6b043d2010-03-31 02:10:44 +00002019err_out_free_mdio_irq:
2020 kfree(fep->mii_bus->irq);
2021err_out_free_mdiobus:
2022 mdiobus_free(fep->mii_bus);
2023err_out:
2024 return err;
2025}
2026
2027static void fec_enet_mii_remove(struct fec_enet_private *fep)
2028{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002029 if (--mii_cnt == 0) {
2030 mdiobus_unregister(fep->mii_bus);
2031 kfree(fep->mii_bus->irq);
2032 mdiobus_free(fep->mii_bus);
2033 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002034}
2035
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002036static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002037 struct ethtool_cmd *cmd)
2038{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002039 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002040 struct phy_device *phydev = fep->phy_dev;
2041
2042 if (!phydev)
2043 return -ENODEV;
2044
2045 return phy_ethtool_gset(phydev, cmd);
2046}
2047
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002048static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002049 struct ethtool_cmd *cmd)
2050{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002051 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002052 struct phy_device *phydev = fep->phy_dev;
2053
2054 if (!phydev)
2055 return -ENODEV;
2056
2057 return phy_ethtool_sset(phydev, cmd);
2058}
2059
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002060static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002061 struct ethtool_drvinfo *info)
2062{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002063 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Jiri Pirko7826d432013-01-06 00:44:26 +00002065 strlcpy(info->driver, fep->pdev->dev.driver->name,
2066 sizeof(info->driver));
2067 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2068 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
Bryan Wue6b043d2010-03-31 02:10:44 +00002070
Frank Li5ebae4892013-01-06 16:25:07 +00002071static int fec_enet_get_ts_info(struct net_device *ndev,
2072 struct ethtool_ts_info *info)
2073{
2074 struct fec_enet_private *fep = netdev_priv(ndev);
2075
2076 if (fep->bufdesc_ex) {
2077
2078 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2079 SOF_TIMESTAMPING_RX_SOFTWARE |
2080 SOF_TIMESTAMPING_SOFTWARE |
2081 SOF_TIMESTAMPING_TX_HARDWARE |
2082 SOF_TIMESTAMPING_RX_HARDWARE |
2083 SOF_TIMESTAMPING_RAW_HARDWARE;
2084 if (fep->ptp_clock)
2085 info->phc_index = ptp_clock_index(fep->ptp_clock);
2086 else
2087 info->phc_index = -1;
2088
2089 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2090 (1 << HWTSTAMP_TX_ON);
2091
2092 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2093 (1 << HWTSTAMP_FILTER_ALL);
2094 return 0;
2095 } else {
2096 return ethtool_op_get_ts_info(ndev, info);
2097 }
2098}
2099
Guenter Roeckd1391932013-06-18 10:04:59 -07002100#if !defined(CONFIG_M5272)
2101
Frank Libaa70a52013-01-16 16:55:58 +00002102static void fec_enet_get_pauseparam(struct net_device *ndev,
2103 struct ethtool_pauseparam *pause)
2104{
2105 struct fec_enet_private *fep = netdev_priv(ndev);
2106
2107 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2108 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2109 pause->rx_pause = pause->tx_pause;
2110}
2111
2112static int fec_enet_set_pauseparam(struct net_device *ndev,
2113 struct ethtool_pauseparam *pause)
2114{
2115 struct fec_enet_private *fep = netdev_priv(ndev);
2116
Russell King0b146ca2014-07-08 00:22:59 +01002117 if (!fep->phy_dev)
2118 return -ENODEV;
2119
Frank Libaa70a52013-01-16 16:55:58 +00002120 if (pause->tx_pause != pause->rx_pause) {
2121 netdev_info(ndev,
2122 "hardware only support enable/disable both tx and rx");
2123 return -EINVAL;
2124 }
2125
2126 fep->pause_flag = 0;
2127
2128 /* tx pause must be same as rx pause */
2129 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2130 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2131
2132 if (pause->rx_pause || pause->autoneg) {
2133 fep->phy_dev->supported |= ADVERTISED_Pause;
2134 fep->phy_dev->advertising |= ADVERTISED_Pause;
2135 } else {
2136 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2137 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2138 }
2139
2140 if (pause->autoneg) {
2141 if (netif_running(ndev))
2142 fec_stop(ndev);
2143 phy_start_aneg(fep->phy_dev);
2144 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002145 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002146 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002147 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002148 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002149 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002150 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002151 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002152 }
Frank Libaa70a52013-01-16 16:55:58 +00002153
2154 return 0;
2155}
2156
Chris Healy38ae92d2013-06-25 23:18:52 -07002157static const struct fec_stat {
2158 char name[ETH_GSTRING_LEN];
2159 u16 offset;
2160} fec_stats[] = {
2161 /* RMON TX */
2162 { "tx_dropped", RMON_T_DROP },
2163 { "tx_packets", RMON_T_PACKETS },
2164 { "tx_broadcast", RMON_T_BC_PKT },
2165 { "tx_multicast", RMON_T_MC_PKT },
2166 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2167 { "tx_undersize", RMON_T_UNDERSIZE },
2168 { "tx_oversize", RMON_T_OVERSIZE },
2169 { "tx_fragment", RMON_T_FRAG },
2170 { "tx_jabber", RMON_T_JAB },
2171 { "tx_collision", RMON_T_COL },
2172 { "tx_64byte", RMON_T_P64 },
2173 { "tx_65to127byte", RMON_T_P65TO127 },
2174 { "tx_128to255byte", RMON_T_P128TO255 },
2175 { "tx_256to511byte", RMON_T_P256TO511 },
2176 { "tx_512to1023byte", RMON_T_P512TO1023 },
2177 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2178 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2179 { "tx_octets", RMON_T_OCTETS },
2180
2181 /* IEEE TX */
2182 { "IEEE_tx_drop", IEEE_T_DROP },
2183 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2184 { "IEEE_tx_1col", IEEE_T_1COL },
2185 { "IEEE_tx_mcol", IEEE_T_MCOL },
2186 { "IEEE_tx_def", IEEE_T_DEF },
2187 { "IEEE_tx_lcol", IEEE_T_LCOL },
2188 { "IEEE_tx_excol", IEEE_T_EXCOL },
2189 { "IEEE_tx_macerr", IEEE_T_MACERR },
2190 { "IEEE_tx_cserr", IEEE_T_CSERR },
2191 { "IEEE_tx_sqe", IEEE_T_SQE },
2192 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2193 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2194
2195 /* RMON RX */
2196 { "rx_packets", RMON_R_PACKETS },
2197 { "rx_broadcast", RMON_R_BC_PKT },
2198 { "rx_multicast", RMON_R_MC_PKT },
2199 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2200 { "rx_undersize", RMON_R_UNDERSIZE },
2201 { "rx_oversize", RMON_R_OVERSIZE },
2202 { "rx_fragment", RMON_R_FRAG },
2203 { "rx_jabber", RMON_R_JAB },
2204 { "rx_64byte", RMON_R_P64 },
2205 { "rx_65to127byte", RMON_R_P65TO127 },
2206 { "rx_128to255byte", RMON_R_P128TO255 },
2207 { "rx_256to511byte", RMON_R_P256TO511 },
2208 { "rx_512to1023byte", RMON_R_P512TO1023 },
2209 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2210 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2211 { "rx_octets", RMON_R_OCTETS },
2212
2213 /* IEEE RX */
2214 { "IEEE_rx_drop", IEEE_R_DROP },
2215 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2216 { "IEEE_rx_crc", IEEE_R_CRC },
2217 { "IEEE_rx_align", IEEE_R_ALIGN },
2218 { "IEEE_rx_macerr", IEEE_R_MACERR },
2219 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2220 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2221};
2222
2223static void fec_enet_get_ethtool_stats(struct net_device *dev,
2224 struct ethtool_stats *stats, u64 *data)
2225{
2226 struct fec_enet_private *fep = netdev_priv(dev);
2227 int i;
2228
2229 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2230 data[i] = readl(fep->hwp + fec_stats[i].offset);
2231}
2232
2233static void fec_enet_get_strings(struct net_device *netdev,
2234 u32 stringset, u8 *data)
2235{
2236 int i;
2237 switch (stringset) {
2238 case ETH_SS_STATS:
2239 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2240 memcpy(data + i * ETH_GSTRING_LEN,
2241 fec_stats[i].name, ETH_GSTRING_LEN);
2242 break;
2243 }
2244}
2245
2246static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2247{
2248 switch (sset) {
2249 case ETH_SS_STATS:
2250 return ARRAY_SIZE(fec_stats);
2251 default:
2252 return -EOPNOTSUPP;
2253 }
2254}
Guenter Roeckd1391932013-06-18 10:04:59 -07002255#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002256
Chris Healy32bc9b42013-06-17 07:25:06 -07002257static int fec_enet_nway_reset(struct net_device *dev)
2258{
2259 struct fec_enet_private *fep = netdev_priv(dev);
2260 struct phy_device *phydev = fep->phy_dev;
2261
2262 if (!phydev)
2263 return -ENODEV;
2264
2265 return genphy_restart_aneg(phydev);
2266}
2267
Fugang Duand851b472014-09-17 05:18:52 +08002268/* ITR clock source is enet system clock (clk_ahb).
2269 * TCTT unit is cycle_ns * 64 cycle
2270 * So, the ICTT value = X us / (cycle_ns * 64)
2271 */
2272static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2273{
2274 struct fec_enet_private *fep = netdev_priv(ndev);
2275
2276 return us * (fep->itr_clk_rate / 64000) / 1000;
2277}
2278
2279/* Set threshold for interrupt coalescing */
2280static void fec_enet_itr_coal_set(struct net_device *ndev)
2281{
2282 struct fec_enet_private *fep = netdev_priv(ndev);
2283 const struct platform_device_id *id_entry =
2284 platform_get_device_id(fep->pdev);
2285 int rx_itr, tx_itr;
2286
2287 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2288 return;
2289
2290 /* Must be greater than zero to avoid unpredictable behavior */
2291 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2292 !fep->tx_time_itr || !fep->tx_pkts_itr)
2293 return;
2294
2295 /* Select enet system clock as Interrupt Coalescing
2296 * timer Clock Source
2297 */
2298 rx_itr = FEC_ITR_CLK_SEL;
2299 tx_itr = FEC_ITR_CLK_SEL;
2300
2301 /* set ICFT and ICTT */
2302 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2303 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2304 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2305 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2306
2307 rx_itr |= FEC_ITR_EN;
2308 tx_itr |= FEC_ITR_EN;
2309
2310 writel(tx_itr, fep->hwp + FEC_TXIC0);
2311 writel(rx_itr, fep->hwp + FEC_RXIC0);
2312 writel(tx_itr, fep->hwp + FEC_TXIC1);
2313 writel(rx_itr, fep->hwp + FEC_RXIC1);
2314 writel(tx_itr, fep->hwp + FEC_TXIC2);
2315 writel(rx_itr, fep->hwp + FEC_RXIC2);
2316}
2317
2318static int
2319fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2320{
2321 struct fec_enet_private *fep = netdev_priv(ndev);
2322 const struct platform_device_id *id_entry =
2323 platform_get_device_id(fep->pdev);
2324
2325 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2326 return -EOPNOTSUPP;
2327
2328 ec->rx_coalesce_usecs = fep->rx_time_itr;
2329 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2330
2331 ec->tx_coalesce_usecs = fep->tx_time_itr;
2332 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2333
2334 return 0;
2335}
2336
2337static int
2338fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2339{
2340 struct fec_enet_private *fep = netdev_priv(ndev);
2341 const struct platform_device_id *id_entry =
2342 platform_get_device_id(fep->pdev);
2343
2344 unsigned int cycle;
2345
2346 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2347 return -EOPNOTSUPP;
2348
2349 if (ec->rx_max_coalesced_frames > 255) {
2350 pr_err("Rx coalesced frames exceed hardware limiation");
2351 return -EINVAL;
2352 }
2353
2354 if (ec->tx_max_coalesced_frames > 255) {
2355 pr_err("Tx coalesced frame exceed hardware limiation");
2356 return -EINVAL;
2357 }
2358
2359 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2360 if (cycle > 0xFFFF) {
2361 pr_err("Rx coalesed usec exceeed hardware limiation");
2362 return -EINVAL;
2363 }
2364
2365 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2366 if (cycle > 0xFFFF) {
2367 pr_err("Rx coalesed usec exceeed hardware limiation");
2368 return -EINVAL;
2369 }
2370
2371 fep->rx_time_itr = ec->rx_coalesce_usecs;
2372 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2373
2374 fep->tx_time_itr = ec->tx_coalesce_usecs;
2375 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2376
2377 fec_enet_itr_coal_set(ndev);
2378
2379 return 0;
2380}
2381
2382static void fec_enet_itr_coal_init(struct net_device *ndev)
2383{
2384 struct ethtool_coalesce ec;
2385
2386 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2387 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2388
2389 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2390 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2391
2392 fec_enet_set_coalesce(ndev, &ec);
2393}
2394
stephen hemminger9b07be42012-01-04 12:59:49 +00002395static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002396 .get_settings = fec_enet_get_settings,
2397 .set_settings = fec_enet_set_settings,
2398 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002399 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002400 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002401 .get_coalesce = fec_enet_get_coalesce,
2402 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002403#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002404 .get_pauseparam = fec_enet_get_pauseparam,
2405 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002406 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002407 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002408 .get_sset_count = fec_enet_get_sset_count,
2409#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002410 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002411};
2412
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002413static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002414{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002415 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002416 struct phy_device *phydev = fep->phy_dev;
2417
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002418 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002419 return -EINVAL;
2420
2421 if (!phydev)
2422 return -ENODEV;
2423
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002424 if (fep->bufdesc_ex) {
2425 if (cmd == SIOCSHWTSTAMP)
2426 return fec_ptp_set(ndev, rq);
2427 if (cmd == SIOCGHWTSTAMP)
2428 return fec_ptp_get(ndev, rq);
2429 }
Frank Liff43da82013-01-03 16:04:23 +00002430
Richard Cochran28b04112010-07-17 08:48:55 +00002431 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002432}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002434static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002435{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002436 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002437 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002438 struct sk_buff *skb;
2439 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002440 struct fec_enet_priv_tx_q *txq;
2441 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f742014-09-13 05:00:50 +08002442 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002443
Frank Li59d0f742014-09-13 05:00:50 +08002444 for (q = 0; q < fep->num_rx_queues; q++) {
2445 rxq = fep->rx_queue[q];
2446 bdp = rxq->rx_bd_base;
2447 for (i = 0; i < rxq->rx_ring_size; i++) {
2448 skb = rxq->rx_skbuff[i];
2449 rxq->rx_skbuff[i] = NULL;
2450 if (skb) {
2451 dma_unmap_single(&fep->pdev->dev,
2452 bdp->cbd_bufaddr,
Nimrod Andyb64bf4b2014-09-23 15:40:57 +08002453 FEC_ENET_RX_FRSIZE - fep->rx_align,
Frank Li59d0f742014-09-13 05:00:50 +08002454 DMA_FROM_DEVICE);
2455 dev_kfree_skb(skb);
2456 }
2457 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002458 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002459 }
2460
Frank Li59d0f742014-09-13 05:00:50 +08002461 for (q = 0; q < fep->num_tx_queues; q++) {
2462 txq = fep->tx_queue[q];
2463 bdp = txq->tx_bd_base;
2464 for (i = 0; i < txq->tx_ring_size; i++) {
2465 kfree(txq->tx_bounce[i]);
2466 txq->tx_bounce[i] = NULL;
2467 skb = txq->tx_skbuff[i];
2468 txq->tx_skbuff[i] = NULL;
2469 dev_kfree_skb(skb);
2470 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002471 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002472}
2473
Frank Li59d0f742014-09-13 05:00:50 +08002474static void fec_enet_free_queue(struct net_device *ndev)
2475{
2476 struct fec_enet_private *fep = netdev_priv(ndev);
2477 int i;
2478 struct fec_enet_priv_tx_q *txq;
2479
2480 for (i = 0; i < fep->num_tx_queues; i++)
2481 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2482 txq = fep->tx_queue[i];
2483 dma_free_coherent(NULL,
2484 txq->tx_ring_size * TSO_HEADER_SIZE,
2485 txq->tso_hdrs,
2486 txq->tso_hdrs_dma);
2487 }
2488
2489 for (i = 0; i < fep->num_rx_queues; i++)
2490 if (fep->rx_queue[i])
2491 kfree(fep->rx_queue[i]);
2492
2493 for (i = 0; i < fep->num_tx_queues; i++)
2494 if (fep->tx_queue[i])
2495 kfree(fep->tx_queue[i]);
2496}
2497
2498static int fec_enet_alloc_queue(struct net_device *ndev)
2499{
2500 struct fec_enet_private *fep = netdev_priv(ndev);
2501 int i;
2502 int ret = 0;
2503 struct fec_enet_priv_tx_q *txq;
2504
2505 for (i = 0; i < fep->num_tx_queues; i++) {
2506 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2507 if (!txq) {
2508 ret = -ENOMEM;
2509 goto alloc_failed;
2510 }
2511
2512 fep->tx_queue[i] = txq;
2513 txq->tx_ring_size = TX_RING_SIZE;
2514 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2515
2516 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2517 txq->tx_wake_threshold =
2518 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2519
2520 txq->tso_hdrs = dma_alloc_coherent(NULL,
2521 txq->tx_ring_size * TSO_HEADER_SIZE,
2522 &txq->tso_hdrs_dma,
2523 GFP_KERNEL);
2524 if (!txq->tso_hdrs) {
2525 ret = -ENOMEM;
2526 goto alloc_failed;
2527 }
2528 }
2529
2530 for (i = 0; i < fep->num_rx_queues; i++) {
2531 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2532 GFP_KERNEL);
2533 if (!fep->rx_queue[i]) {
2534 ret = -ENOMEM;
2535 goto alloc_failed;
2536 }
2537
2538 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2539 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2540 }
2541 return ret;
2542
2543alloc_failed:
2544 fec_enet_free_queue(ndev);
2545 return ret;
2546}
2547
2548static int
2549fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002550{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002551 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002552 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002553 struct sk_buff *skb;
2554 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002555 struct fec_enet_priv_rx_q *rxq;
Fugang Duan41ef84c2014-09-13 05:00:54 +08002556 unsigned int off;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002557
Frank Li59d0f742014-09-13 05:00:50 +08002558 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002559 bdp = rxq->rx_bd_base;
2560 for (i = 0; i < rxq->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002561 dma_addr_t addr;
2562
Fabio Estevamb72061a2012-02-07 08:27:31 +00002563 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002564 if (!skb)
2565 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002566
Fugang Duan41ef84c2014-09-13 05:00:54 +08002567 off = ((unsigned long)skb->data) & fep->rx_align;
2568 if (off)
2569 skb_reserve(skb, fep->rx_align + 1 - off);
2570
Russell King730ee362014-07-08 00:23:14 +01002571 addr = dma_map_single(&fep->pdev->dev, skb->data,
Fugang Duan41ef84c2014-09-13 05:00:54 +08002572 FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE);
2573
Russell King730ee362014-07-08 00:23:14 +01002574 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2575 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002576 if (net_ratelimit())
2577 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002578 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002579 }
Russell King730ee362014-07-08 00:23:14 +01002580
Fugang Duan4d494cd2014-09-13 05:00:48 +08002581 rxq->rx_skbuff[i] = skb;
Russell King730ee362014-07-08 00:23:14 +01002582 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002583 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002584
2585 if (fep->bufdesc_ex) {
2586 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2587 ebdp->cbd_esc = BD_ENET_RX_INT;
2588 }
2589
Frank Li59d0f742014-09-13 05:00:50 +08002590 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002591 }
2592
2593 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002594 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002595 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f742014-09-13 05:00:50 +08002596 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002597
Frank Li59d0f742014-09-13 05:00:50 +08002598 err_alloc:
2599 fec_enet_free_buffers(ndev);
2600 return -ENOMEM;
2601}
2602
2603static int
2604fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2605{
2606 struct fec_enet_private *fep = netdev_priv(ndev);
2607 unsigned int i;
2608 struct bufdesc *bdp;
2609 struct fec_enet_priv_tx_q *txq;
2610
2611 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002612 bdp = txq->tx_bd_base;
2613 for (i = 0; i < txq->tx_ring_size; i++) {
2614 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2615 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002616 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002617
2618 bdp->cbd_sc = 0;
2619 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002620
Frank Liff43da82013-01-03 16:04:23 +00002621 if (fep->bufdesc_ex) {
2622 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002623 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002624 }
2625
Frank Li59d0f742014-09-13 05:00:50 +08002626 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002627 }
2628
2629 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002630 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002631 bdp->cbd_sc |= BD_SC_WRAP;
2632
2633 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002634
2635 err_alloc:
2636 fec_enet_free_buffers(ndev);
2637 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002638}
2639
Frank Li59d0f742014-09-13 05:00:50 +08002640static int fec_enet_alloc_buffers(struct net_device *ndev)
2641{
2642 struct fec_enet_private *fep = netdev_priv(ndev);
2643 unsigned int i;
2644
2645 for (i = 0; i < fep->num_rx_queues; i++)
2646 if (fec_enet_alloc_rxq_buffers(ndev, i))
2647 return -ENOMEM;
2648
2649 for (i = 0; i < fep->num_tx_queues; i++)
2650 if (fec_enet_alloc_txq_buffers(ndev, i))
2651 return -ENOMEM;
2652 return 0;
2653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002656fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002658 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002659 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002661 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002662 ret = fec_enet_clk_enable(ndev, true);
2663 if (ret)
2664 return ret;
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 /* I should reset the ring buffers here, but I don't yet know
2667 * a simple way to do that.
2668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002670 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002671 if (ret)
2672 return ret;
2673
Bryan Wu418bd0d2010-05-28 03:40:39 -07002674 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002675 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002676 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002677 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002678 return ret;
2679 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002680
Russell Kingef833372014-07-08 12:40:43 +01002681 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002682 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002683 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002684 netif_tx_start_all_queues(ndev);
2685
Sascha Hauer22f6b862009-04-15 01:32:18 +00002686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
2689static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002690fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002692 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Russell Kingd76cfae2014-07-08 00:23:04 +01002694 phy_stop(fep->phy_dev);
2695
Russell King31a6de32014-07-08 12:40:23 +01002696 if (netif_device_present(ndev)) {
2697 napi_disable(&fep->napi);
2698 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002699 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Russell King635cf172014-07-08 00:22:54 +01002702 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002703 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002704
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002705 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002706 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002707 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return 0;
2710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712/* Set or clear the multicast filter for this adaptor.
2713 * Skeleton taken from sunlance driver.
2714 * The CPM Ethernet implementation allows Multicast as well as individual
2715 * MAC address filtering. Some of the drivers check to make sure it is
2716 * a group multicast address, and discard those that are not. I guess I
2717 * will do the same for now, but just remove the test if you want
2718 * individual filtering as well (do the upper net layers want or support
2719 * this kind of feature?).
2720 */
2721
2722#define HASH_BITS 6 /* #bits in hash */
2723#define CRC32_POLY 0xEDB88320
2724
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002725static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002727 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002728 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002729 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 unsigned char hash;
2731
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002732 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002733 tmp = readl(fep->hwp + FEC_R_CNTRL);
2734 tmp |= 0x8;
2735 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002736 return;
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Sascha Hauer4e831832009-04-15 01:32:19 +00002739 tmp = readl(fep->hwp + FEC_R_CNTRL);
2740 tmp &= ~0x8;
2741 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002742
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002743 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002744 /* Catch all multicast addresses, so set the
2745 * filter to all 1's
2746 */
2747 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2748 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Sascha Hauer4e831832009-04-15 01:32:19 +00002750 return;
2751 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002752
Sascha Hauer4e831832009-04-15 01:32:19 +00002753 /* Clear filter and add the addresses in hash register
2754 */
2755 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2756 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002758 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002759 /* calculate crc32 value of mac address */
2760 crc = 0xffffffff;
2761
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002762 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002763 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002764 for (bit = 0; bit < 8; bit++, data >>= 1) {
2765 crc = (crc >> 1) ^
2766 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002769
2770 /* only upper 6 bits (HASH_BITS) are used
2771 * which point to specific bit in he hash registers
2772 */
2773 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2774
2775 if (hash > 31) {
2776 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2777 tmp |= 1 << (hash - 32);
2778 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2779 } else {
2780 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2781 tmp |= 1 << hash;
2782 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785}
2786
Sascha Hauer22f6b862009-04-15 01:32:18 +00002787/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002788static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002789fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002791 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002792 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Lucas Stach44934fa2014-03-30 21:32:08 +02002794 if (addr) {
2795 if (!is_valid_ether_addr(addr->sa_data))
2796 return -EADDRNOTAVAIL;
2797 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2798 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002799
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002800 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2801 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002802 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002803 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002804 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002808#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002809/**
2810 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002811 * @dev: The FEC network adapter
2812 *
2813 * Polled functionality used by netconsole and others in non interrupt mode
2814 *
2815 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002816static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002817{
2818 int i;
2819 struct fec_enet_private *fep = netdev_priv(dev);
2820
2821 for (i = 0; i < FEC_IRQ_NUM; i++) {
2822 if (fep->irq[i] > 0) {
2823 disable_irq(fep->irq[i]);
2824 fec_enet_interrupt(fep->irq[i], dev);
2825 enable_irq(fep->irq[i]);
2826 }
2827 }
2828}
2829#endif
2830
Russell King8506fa12014-07-08 12:40:33 +01002831#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2832
Jim Baxter4c09eed2013-04-19 08:10:49 +00002833static int fec_set_features(struct net_device *netdev,
2834 netdev_features_t features)
2835{
2836 struct fec_enet_private *fep = netdev_priv(netdev);
2837 netdev_features_t changed = features ^ netdev->features;
2838
Russell King8506fa12014-07-08 12:40:33 +01002839 /* Quiesce the device if necessary */
2840 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2841 napi_disable(&fep->napi);
2842 netif_tx_lock_bh(netdev);
2843 fec_stop(netdev);
2844 }
2845
Jim Baxter4c09eed2013-04-19 08:10:49 +00002846 netdev->features = features;
2847
2848 /* Receive checksum has been changed */
2849 if (changed & NETIF_F_RXCSUM) {
2850 if (features & NETIF_F_RXCSUM)
2851 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2852 else
2853 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002854 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002855
Russell King8506fa12014-07-08 12:40:33 +01002856 /* Resume the device after updates */
2857 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002858 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002859 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002860 netif_tx_unlock_bh(netdev);
2861 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002862 }
2863
2864 return 0;
2865}
2866
Sascha Hauer009fda82009-04-15 01:32:23 +00002867static const struct net_device_ops fec_netdev_ops = {
2868 .ndo_open = fec_enet_open,
2869 .ndo_stop = fec_enet_close,
2870 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002871 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002872 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002873 .ndo_validate_addr = eth_validate_addr,
2874 .ndo_tx_timeout = fec_timeout,
2875 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002876 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002877#ifdef CONFIG_NET_POLL_CONTROLLER
2878 .ndo_poll_controller = fec_poll_controller,
2879#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002880 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002881};
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /*
2884 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002885 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002887static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002889 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002890 const struct platform_device_id *id_entry =
2891 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002892 struct fec_enet_priv_tx_q *txq;
2893 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002894 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002895 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002896 int bd_size;
Frank Li59d0f742014-09-13 05:00:50 +08002897 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002898
Fugang Duan41ef84c2014-09-13 05:00:54 +08002899#if defined(CONFIG_ARM)
2900 fep->rx_align = 0xf;
2901 fep->tx_align = 0xf;
2902#else
2903 fep->rx_align = 0x3;
2904 fep->tx_align = 0x3;
2905#endif
2906
Frank Li59d0f742014-09-13 05:00:50 +08002907 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002908
Nimrod Andy55d02182014-06-12 08:16:21 +08002909 if (fep->bufdesc_ex)
2910 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2911 else
2912 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002913 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002914 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002916 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002917 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002918 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002919 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002920 return -ENOMEM;
2921 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002922
Fugang Duan4d494cd2014-09-13 05:00:48 +08002923 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Shawn Guo49da97d2011-01-05 21:13:11 +00002925 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002926 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002927 /* make sure MAC we just acquired is programmed into the hw */
2928 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002930 /* Set receive and transmit descriptor base. */
Frank Li59d0f742014-09-13 05:00:50 +08002931 for (i = 0; i < fep->num_rx_queues; i++) {
2932 rxq = fep->rx_queue[i];
2933 rxq->index = i;
2934 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2935 rxq->bd_dma = bd_dma;
2936 if (fep->bufdesc_ex) {
2937 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2938 cbd_base = (struct bufdesc *)
2939 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2940 } else {
2941 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2942 cbd_base += rxq->rx_ring_size;
2943 }
2944 }
2945
2946 for (i = 0; i < fep->num_tx_queues; i++) {
2947 txq = fep->tx_queue[i];
2948 txq->index = i;
2949 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2950 txq->bd_dma = bd_dma;
2951 if (fep->bufdesc_ex) {
2952 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
2953 cbd_base = (struct bufdesc *)
2954 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
2955 } else {
2956 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
2957 cbd_base += txq->tx_ring_size;
2958 }
2959 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Sascha Hauer22f6b862009-04-15 01:32:18 +00002962 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002963 ndev->watchdog_timeo = TX_TIMEOUT;
2964 ndev->netdev_ops = &fec_netdev_ops;
2965 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002966
Frank Lidc975382013-01-28 18:31:42 +00002967 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002968 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002969
Nimrod Andy09d1e542014-06-12 08:16:20 +08002970 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002971 /* enable hw VLAN support */
2972 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002973
Shawn Guo48496252013-05-08 21:08:22 +00002974 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002975 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2976
Shawn Guo48496252013-05-08 21:08:22 +00002977 /* enable hw accelerator */
2978 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002979 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002980 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2981 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002982
Fugang Duan41ef84c2014-09-13 05:00:54 +08002983 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
2984 fep->tx_align = 0;
2985 fep->rx_align = 0x3f;
2986 }
2987
Nimrod Andy09d1e542014-06-12 08:16:20 +08002988 ndev->hw_features = ndev->features;
2989
Russell Kingef833372014-07-08 12:40:43 +01002990 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return 0;
2993}
2994
Shawn Guoca2cc332011-06-25 02:04:35 +08002995#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002996static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002997{
2998 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002999 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08003000 struct device_node *np = pdev->dev.of_node;
3001
3002 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003003 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003004
Shawn Guoa3caad02012-06-27 03:45:24 +00003005 of_property_read_u32(np, "phy-reset-duration", &msec);
3006 /* A sane reset duration should not be longer than 1s */
3007 if (msec > 1000)
3008 msec = 1;
3009
Shawn Guoca2cc332011-06-25 02:04:35 +08003010 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003011 if (!gpio_is_valid(phy_reset))
3012 return;
3013
Shawn Guo119fc002012-06-27 03:45:22 +00003014 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3015 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003016 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003017 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003018 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003019 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003020 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003021 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003022}
3023#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003024static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003025{
3026 /*
3027 * In case of platform probe, the reset has been done
3028 * by machine code.
3029 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003030}
3031#endif /* CONFIG_OF */
3032
Fugang Duan9fc095f2014-09-13 05:00:49 +08003033static void
3034fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3035{
3036 struct device_node *np = pdev->dev.of_node;
3037 int err;
3038
3039 *num_tx = *num_rx = 1;
3040
3041 if (!np || !of_device_is_available(np))
3042 return;
3043
3044 /* parse the num of tx and rx queues */
3045 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003046 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003047 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003048
3049 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3050 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003051 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003052
3053 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003054 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3055 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003056 *num_tx = 1;
3057 return;
3058 }
3059
3060 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003061 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3062 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003063 *num_rx = 1;
3064 return;
3065 }
3066
3067}
3068
Bill Pemberton33897cc2012-12-03 09:23:58 -05003069static int
Sascha Haueread73182009-01-28 23:03:11 +00003070fec_probe(struct platform_device *pdev)
3071{
3072 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003073 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003074 struct net_device *ndev;
3075 int i, irq, ret = 0;
3076 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003077 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003078 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003079 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003080 int num_tx_qs;
3081 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003082
3083 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3084 if (of_id)
3085 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003086
Fugang Duan9fc095f2014-09-13 05:00:49 +08003087 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3088
Sascha Haueread73182009-01-28 23:03:11 +00003089 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003090 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3091 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003092 if (!ndev)
3093 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003094
3095 SET_NETDEV_DEV(ndev, &pdev->dev);
3096
3097 /* setup board info structure */
3098 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003099
Fugang Duan9fc095f2014-09-13 05:00:49 +08003100 fep->num_rx_queues = num_rx_qs;
3101 fep->num_tx_queues = num_tx_qs;
3102
Guenter Roeckd1391932013-06-18 10:04:59 -07003103#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003104 /* default enable pause frame auto negotiation */
3105 if (pdev->id_entry &&
3106 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3107 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003108#endif
Frank Libaa70a52013-01-16 16:55:58 +00003109
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003110 /* Select default pin state */
3111 pinctrl_pm_select_default_state(&pdev->dev);
3112
Fabio Estevam399db752013-07-21 13:25:03 -03003113 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303114 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3115 if (IS_ERR(fep->hwp)) {
3116 ret = PTR_ERR(fep->hwp);
3117 goto failed_ioremap;
3118 }
3119
Bryan Wue6b043d2010-03-31 02:10:44 +00003120 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003121 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003122
Frank Liff43da82013-01-03 16:04:23 +00003123 fep->bufdesc_ex = 0;
3124
Sascha Haueread73182009-01-28 23:03:11 +00003125 platform_set_drvdata(pdev, ndev);
3126
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003127 phy_node = of_parse_phandle(np, "phy-handle", 0);
3128 if (!phy_node && of_phy_is_fixed_link(np)) {
3129 ret = of_phy_register_fixed_link(np);
3130 if (ret < 0) {
3131 dev_err(&pdev->dev,
3132 "broken fixed-link specification\n");
3133 goto failed_phy;
3134 }
3135 phy_node = of_node_get(np);
3136 }
3137 fep->phy_node = phy_node;
3138
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003139 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003140 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003141 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003142 if (pdata)
3143 fep->phy_interface = pdata->phy;
3144 else
3145 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3146 } else {
3147 fep->phy_interface = ret;
3148 }
3149
Fabio Estevame2f8d552013-02-22 06:40:45 +00003150 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3151 if (IS_ERR(fep->clk_ipg)) {
3152 ret = PTR_ERR(fep->clk_ipg);
3153 goto failed_clk;
3154 }
3155
3156 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3157 if (IS_ERR(fep->clk_ahb)) {
3158 ret = PTR_ERR(fep->clk_ahb);
3159 goto failed_clk;
3160 }
3161
Fugang Duand851b472014-09-17 05:18:52 +08003162 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3163
Linus Torvalds38f56f32013-05-07 11:06:17 -07003164 /* enet_out is optional, depends on board */
3165 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3166 if (IS_ERR(fep->clk_enet_out))
3167 fep->clk_enet_out = NULL;
3168
Nimrod Andy91c0d982014-08-21 17:09:38 +08003169 fep->ptp_clk_on = false;
3170 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003171
3172 /* clk_ref is optional, depends on board */
3173 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3174 if (IS_ERR(fep->clk_ref))
3175 fep->clk_ref = NULL;
3176
Fabio Estevame2f8d552013-02-22 06:40:45 +00003177 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003178 fep->bufdesc_ex =
3179 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003180 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003181 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003182 fep->bufdesc_ex = 0;
3183 }
3184
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003185 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003186 if (ret)
3187 goto failed_clk;
3188
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003189 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3190 if (!IS_ERR(fep->reg_phy)) {
3191 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003192 if (ret) {
3193 dev_err(&pdev->dev,
3194 "Failed to enable phy regulator: %d\n", ret);
3195 goto failed_regulator;
3196 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003197 } else {
3198 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003199 }
3200
3201 fec_reset_phy(pdev);
3202
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003203 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003204 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003205
3206 ret = fec_enet_init(ndev);
3207 if (ret)
3208 goto failed_init;
3209
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003210 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003211 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003212 if (irq < 0) {
3213 if (i)
3214 break;
3215 ret = irq;
3216 goto failed_irq;
3217 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003218 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003219 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003220 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003221 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003222 }
3223
Fugang Duanb4d39b52014-09-13 05:00:55 +08003224 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003225 ret = fec_enet_mii_init(pdev);
3226 if (ret)
3227 goto failed_mii_init;
3228
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003229 /* Carrier starts down, phylib will bring it up */
3230 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003231 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003232 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003233
Sascha Haueread73182009-01-28 23:03:11 +00003234 ret = register_netdev(ndev);
3235 if (ret)
3236 goto failed_register;
3237
Fabio Estevameb1d0642013-04-13 07:25:36 +00003238 if (fep->bufdesc_ex && fep->ptp_clock)
3239 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3240
Russell King36cdc742014-07-08 13:01:44 +01003241 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003242 return 0;
3243
3244failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003245 fec_enet_mii_remove(fep);
3246failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003247failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003248failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003249 if (fep->reg_phy)
3250 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003251failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003252 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003253failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003254failed_phy:
3255 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003256failed_ioremap:
3257 free_netdev(ndev);
3258
3259 return ret;
3260}
3261
Bill Pemberton33897cc2012-12-03 09:23:58 -05003262static int
Sascha Haueread73182009-01-28 23:03:11 +00003263fec_drv_remove(struct platform_device *pdev)
3264{
3265 struct net_device *ndev = platform_get_drvdata(pdev);
3266 struct fec_enet_private *fep = netdev_priv(ndev);
3267
Nimrod Andy91c0d982014-08-21 17:09:38 +08003268 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003269 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003270 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003271 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003272 if (fep->reg_phy)
3273 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003274 if (fep->ptp_clock)
3275 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003276 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003277 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003278 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003279
Sascha Haueread73182009-01-28 23:03:11 +00003280 return 0;
3281}
3282
Fabio Estevamdd66d382014-07-24 18:24:01 -03003283static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003284{
Eric Benard87cad5c2010-06-18 04:19:54 +00003285 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003286 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003287
Russell Kingda1774e2014-07-08 12:39:57 +01003288 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003289 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003290 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003291 napi_disable(&fep->napi);
3292 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003293 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003294 netif_tx_unlock_bh(ndev);
3295 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003296 }
Russell Kingda1774e2014-07-08 12:39:57 +01003297 rtnl_unlock();
3298
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003299 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003300 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003301
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003302 if (fep->reg_phy)
3303 regulator_disable(fep->reg_phy);
3304
Sascha Haueread73182009-01-28 23:03:11 +00003305 return 0;
3306}
3307
Fabio Estevamdd66d382014-07-24 18:24:01 -03003308static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003309{
Eric Benard87cad5c2010-06-18 04:19:54 +00003310 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003311 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003312 int ret;
3313
3314 if (fep->reg_phy) {
3315 ret = regulator_enable(fep->reg_phy);
3316 if (ret)
3317 return ret;
3318 }
Sascha Haueread73182009-01-28 23:03:11 +00003319
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003320 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003321 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003322 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003323 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003324
Russell Kingda1774e2014-07-08 12:39:57 +01003325 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003326 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003327 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003328 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003329 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003330 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003331 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003332 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003333 }
Russell Kingda1774e2014-07-08 12:39:57 +01003334 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003335
Sascha Haueread73182009-01-28 23:03:11 +00003336 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003337
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003338failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003339 if (fep->reg_phy)
3340 regulator_disable(fep->reg_phy);
3341 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003342}
3343
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003344static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003345
Sascha Haueread73182009-01-28 23:03:11 +00003346static struct platform_driver fec_driver = {
3347 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003348 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003349 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003350 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003351 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003352 },
Shawn Guob5680e02011-01-05 21:13:13 +00003353 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003354 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003355 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003356};
3357
Fabio Estevamaaca2372012-01-23 16:44:50 +00003358module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003360MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361MODULE_LICENSE("GPL");