blob: 7379f4bb74b06cd0d8d2827d4090a58bccd672a5 [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 |
147 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
Fugang Duan37d60172014-09-17 05:18:54 +0800148 FEC_QUIRK_HAS_AVB | 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) {
429 if (skb->ip_summed == CHECKSUM_PARTIAL)
430 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
431 ebdp->cbd_bdu = 0;
432 ebdp->cbd_esc = estatus;
433 }
434
435 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
436
Fugang Duan4d494cd2014-09-13 05:00:48 +0800437 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800438 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800439 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800440 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
441 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800442
443 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
444 swap_buffer(bufaddr, frag_len);
445 }
446
Russell Kingd6bf3142014-07-08 00:23:19 +0100447 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
448 DMA_TO_DEVICE);
449 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800450 dev_kfree_skb_any(skb);
451 if (net_ratelimit())
452 netdev_err(ndev, "Tx DMA memory map failed\n");
453 goto dma_mapping_error;
454 }
455
Russell Kingd6bf3142014-07-08 00:23:19 +0100456 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800457 bdp->cbd_datlen = frag_len;
458 bdp->cbd_sc = status;
459 }
460
Fugang Duan4d494cd2014-09-13 05:00:48 +0800461 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800462
463 return 0;
464
465dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800466 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800467 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800468 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800469 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
470 bdp->cbd_datlen, DMA_TO_DEVICE);
471 }
472 return NETDEV_TX_OK;
473}
474
Fugang Duan4d494cd2014-09-13 05:00:48 +0800475static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
476 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800477{
478 struct fec_enet_private *fep = netdev_priv(ndev);
479 const struct platform_device_id *id_entry =
480 platform_get_device_id(fep->pdev);
481 int nr_frags = skb_shinfo(skb)->nr_frags;
482 struct bufdesc *bdp, *last_bdp;
483 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100484 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800485 unsigned short status;
486 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800487 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800488 unsigned int estatus = 0;
489 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800490 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800491 int ret;
492
Fugang Duan4d494cd2014-09-13 05:00:48 +0800493 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800494 if (entries_free < MAX_SKB_FRAGS + 1) {
495 dev_kfree_skb_any(skb);
496 if (net_ratelimit())
497 netdev_err(ndev, "NOT enough BD for SG!\n");
498 return NETDEV_TX_OK;
499 }
500
Nimrod Andy6e909282014-06-12 08:16:22 +0800501 /* Protocol checksum off-load for TCP and UDP. */
502 if (fec_enet_clear_csum(skb, ndev)) {
503 dev_kfree_skb_any(skb);
504 return NETDEV_TX_OK;
505 }
506
507 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800508 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800509 status = bdp->cbd_sc;
510 status &= ~BD_ENET_TX_STATS;
511
512 /* Set buffer length and buffer pointer */
513 bufaddr = skb->data;
514 buflen = skb_headlen(skb);
515
Fugang Duan4d494cd2014-09-13 05:00:48 +0800516 queue = skb_get_queue_mapping(skb);
517 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800518 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800519 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800520 memcpy(txq->tx_bounce[index], skb->data, buflen);
521 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800522
523 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
524 swap_buffer(bufaddr, buflen);
525 }
526
Russell Kingd6bf3142014-07-08 00:23:19 +0100527 /* Push the data cache so the CPM does not get stale memory data. */
528 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
529 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800530 dev_kfree_skb_any(skb);
531 if (net_ratelimit())
532 netdev_err(ndev, "Tx DMA memory map failed\n");
533 return NETDEV_TX_OK;
534 }
535
536 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800537 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800538 if (ret)
539 return ret;
540 } else {
541 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
542 if (fep->bufdesc_ex) {
543 estatus = BD_ENET_TX_INT;
544 if (unlikely(skb_shinfo(skb)->tx_flags &
545 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
546 estatus |= BD_ENET_TX_TS;
547 }
548 }
549
550 if (fep->bufdesc_ex) {
551
552 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
553
554 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
555 fep->hwts_tx_en))
556 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
557
558 if (skb->ip_summed == CHECKSUM_PARTIAL)
559 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
560
561 ebdp->cbd_bdu = 0;
562 ebdp->cbd_esc = estatus;
563 }
564
Fugang Duan4d494cd2014-09-13 05:00:48 +0800565 last_bdp = txq->cur_tx;
566 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800567 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800568 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800569
570 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100571 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800572
573 /* Send it on its way. Tell FEC it's ready, interrupt when done,
574 * it's the last BD of the frame, and to put the CRC on the end.
575 */
576 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
577 bdp->cbd_sc = status;
578
Sascha Hauer22f6b862009-04-15 01:32:18 +0000579 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800580 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800582 skb_tx_timestamp(skb);
583
Fugang Duan4d494cd2014-09-13 05:00:48 +0800584 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Frank Lide5fb0a2013-03-03 17:34:25 +0000586 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800587 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000588
Nimrod Andy6e909282014-06-12 08:16:22 +0800589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Nimrod Andy79f33912014-06-12 08:16:23 +0800592static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800593fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
594 struct net_device *ndev,
595 struct bufdesc *bdp, int index, char *data,
596 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800597{
598 struct fec_enet_private *fep = netdev_priv(ndev);
599 const struct platform_device_id *id_entry =
600 platform_get_device_id(fep->pdev);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200601 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800602 unsigned short status;
603 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100604 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800605
606 status = bdp->cbd_sc;
607 status &= ~BD_ENET_TX_STATS;
608
609 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800610
Fugang Duan41ef84c2014-09-13 05:00:54 +0800611 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800612 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800613 memcpy(txq->tx_bounce[index], data, size);
614 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800615
616 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
617 swap_buffer(data, size);
618 }
619
Russell Kingd6bf3142014-07-08 00:23:19 +0100620 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
621 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800622 dev_kfree_skb_any(skb);
623 if (net_ratelimit())
624 netdev_err(ndev, "Tx DMA memory map failed\n");
625 return NETDEV_TX_BUSY;
626 }
627
Russell Kingd6bf3142014-07-08 00:23:19 +0100628 bdp->cbd_datlen = size;
629 bdp->cbd_bufaddr = addr;
630
Nimrod Andy79f33912014-06-12 08:16:23 +0800631 if (fep->bufdesc_ex) {
632 if (skb->ip_summed == CHECKSUM_PARTIAL)
633 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
634 ebdp->cbd_bdu = 0;
635 ebdp->cbd_esc = estatus;
636 }
637
638 /* Handle the last BD specially */
639 if (last_tcp)
640 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
641 if (is_last) {
642 status |= BD_ENET_TX_INTR;
643 if (fep->bufdesc_ex)
644 ebdp->cbd_esc |= BD_ENET_TX_INT;
645 }
646
647 bdp->cbd_sc = status;
648
649 return 0;
650}
651
652static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800653fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
654 struct sk_buff *skb, struct net_device *ndev,
655 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800656{
657 struct fec_enet_private *fep = netdev_priv(ndev);
658 const struct platform_device_id *id_entry =
659 platform_get_device_id(fep->pdev);
660 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Fabian Frederick61cd2eb2014-09-13 22:38:26 +0200661 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
Nimrod Andy79f33912014-06-12 08:16:23 +0800662 void *bufaddr;
663 unsigned long dmabuf;
664 unsigned short status;
665 unsigned int estatus = 0;
666
667 status = bdp->cbd_sc;
668 status &= ~BD_ENET_TX_STATS;
669 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
670
Fugang Duan4d494cd2014-09-13 05:00:48 +0800671 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
672 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800673 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800674 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800675 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
676 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800677
678 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
679 swap_buffer(bufaddr, hdr_len);
680
681 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
682 hdr_len, DMA_TO_DEVICE);
683 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
684 dev_kfree_skb_any(skb);
685 if (net_ratelimit())
686 netdev_err(ndev, "Tx DMA memory map failed\n");
687 return NETDEV_TX_BUSY;
688 }
689 }
690
691 bdp->cbd_bufaddr = dmabuf;
692 bdp->cbd_datlen = hdr_len;
693
694 if (fep->bufdesc_ex) {
695 if (skb->ip_summed == CHECKSUM_PARTIAL)
696 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
697 ebdp->cbd_bdu = 0;
698 ebdp->cbd_esc = estatus;
699 }
700
701 bdp->cbd_sc = status;
702
703 return 0;
704}
705
Fugang Duan4d494cd2014-09-13 05:00:48 +0800706static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
707 struct sk_buff *skb,
708 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800709{
710 struct fec_enet_private *fep = netdev_priv(ndev);
711 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
712 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800713 struct bufdesc *bdp = txq->cur_tx;
714 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800715 struct tso_t tso;
716 unsigned int index = 0;
717 int ret;
Fugang Duan37d60172014-09-17 05:18:54 +0800718 const struct platform_device_id *id_entry =
719 platform_get_device_id(fep->pdev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800720
Fugang Duan4d494cd2014-09-13 05:00:48 +0800721 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800722 dev_kfree_skb_any(skb);
723 if (net_ratelimit())
724 netdev_err(ndev, "NOT enough BD for TSO!\n");
725 return NETDEV_TX_OK;
726 }
727
728 /* Protocol checksum off-load for TCP and UDP. */
729 if (fec_enet_clear_csum(skb, ndev)) {
730 dev_kfree_skb_any(skb);
731 return NETDEV_TX_OK;
732 }
733
734 /* Initialize the TSO handler, and prepare the first payload */
735 tso_start(skb, &tso);
736
737 total_len = skb->len - hdr_len;
738 while (total_len > 0) {
739 char *hdr;
740
Fugang Duan4d494cd2014-09-13 05:00:48 +0800741 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800742 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
743 total_len -= data_left;
744
745 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800746 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800747 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800748 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800749 if (ret)
750 goto err_release;
751
752 while (data_left > 0) {
753 int size;
754
755 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800756 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
757 index = fec_enet_get_bd_index(txq->tx_bd_base,
758 bdp, fep);
759 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
760 bdp, index,
761 tso.data, size,
762 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800763 total_len == 0);
764 if (ret)
765 goto err_release;
766
767 data_left -= size;
768 tso_build_data(skb, &tso, size);
769 }
770
Fugang Duan4d494cd2014-09-13 05:00:48 +0800771 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800772 }
773
774 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800775 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800776
Nimrod Andy79f33912014-06-12 08:16:23 +0800777 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800778 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800779
780 /* Trigger transmission start */
Fugang Duan37d60172014-09-17 05:18:54 +0800781 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
782 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
783 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
784 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
785 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
786 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800787
788 return 0;
789
790err_release:
791 /* TODO: Release all used data descriptors for TSO */
792 return ret;
793}
794
Nimrod Andy61a44272014-06-12 08:16:18 +0800795static netdev_tx_t
796fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
797{
798 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800799 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800800 unsigned short queue;
801 struct fec_enet_priv_tx_q *txq;
802 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800803 int ret;
804
Fugang Duan4d494cd2014-09-13 05:00:48 +0800805 queue = skb_get_queue_mapping(skb);
806 txq = fep->tx_queue[queue];
807 nq = netdev_get_tx_queue(ndev, queue);
808
Nimrod Andy79f33912014-06-12 08:16:23 +0800809 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800810 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800811 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800812 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800813 if (ret)
814 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800815
Fugang Duan4d494cd2014-09-13 05:00:48 +0800816 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
817 if (entries_free <= txq->tx_stop_threshold)
818 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800819
820 return NETDEV_TX_OK;
821}
822
Frank Li14109a52013-03-26 16:12:03 +0000823/* Init RX & TX buffer descriptors
824 */
825static void fec_enet_bd_init(struct net_device *dev)
826{
827 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800828 struct fec_enet_priv_tx_q *txq;
829 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000830 struct bufdesc *bdp;
831 unsigned int i;
Frank Li59d0f742014-09-13 05:00:50 +0800832 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000833
Frank Li59d0f742014-09-13 05:00:50 +0800834 for (q = 0; q < fep->num_rx_queues; q++) {
835 /* Initialize the receive buffer descriptors. */
836 rxq = fep->rx_queue[q];
837 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800838
Frank Li59d0f742014-09-13 05:00:50 +0800839 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000840
Frank Li59d0f742014-09-13 05:00:50 +0800841 /* Initialize the BD for every fragment in the page. */
842 if (bdp->cbd_bufaddr)
843 bdp->cbd_sc = BD_ENET_RX_EMPTY;
844 else
845 bdp->cbd_sc = 0;
846 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000847 }
Frank Li59d0f742014-09-13 05:00:50 +0800848
849 /* Set the last buffer to wrap */
850 bdp = fec_enet_get_prevdesc(bdp, fep, q);
851 bdp->cbd_sc |= BD_SC_WRAP;
852
853 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000854 }
855
Frank Li59d0f742014-09-13 05:00:50 +0800856 for (q = 0; q < fep->num_tx_queues; q++) {
857 /* ...and the same for transmit */
858 txq = fep->tx_queue[q];
859 bdp = txq->tx_bd_base;
860 txq->cur_tx = bdp;
861
862 for (i = 0; i < txq->tx_ring_size; i++) {
863 /* Initialize the BD for every fragment in the page. */
864 bdp->cbd_sc = 0;
865 if (txq->tx_skbuff[i]) {
866 dev_kfree_skb_any(txq->tx_skbuff[i]);
867 txq->tx_skbuff[i] = NULL;
868 }
869 bdp->cbd_bufaddr = 0;
870 bdp = fec_enet_get_nextdesc(bdp, fep, q);
871 }
872
873 /* Set the last buffer to wrap */
874 bdp = fec_enet_get_prevdesc(bdp, fep, q);
875 bdp->cbd_sc |= BD_SC_WRAP;
876 txq->dirty_tx = bdp;
877 }
878}
879
Frank Lice99d0d2014-09-13 05:00:52 +0800880static void fec_enet_active_rxring(struct net_device *ndev)
881{
882 struct fec_enet_private *fep = netdev_priv(ndev);
883 int i;
884
885 for (i = 0; i < fep->num_rx_queues; i++)
886 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
887}
888
Frank Li59d0f742014-09-13 05:00:50 +0800889static void fec_enet_enable_ring(struct net_device *ndev)
890{
891 struct fec_enet_private *fep = netdev_priv(ndev);
892 struct fec_enet_priv_tx_q *txq;
893 struct fec_enet_priv_rx_q *rxq;
894 int i;
895
896 for (i = 0; i < fep->num_rx_queues; i++) {
897 rxq = fep->rx_queue[i];
898 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
899
900 /* enable DMA1/2 */
901 if (i)
902 writel(RCMR_MATCHEN | RCMR_CMP(i),
903 fep->hwp + FEC_RCMR(i));
904 }
905
906 for (i = 0; i < fep->num_tx_queues; i++) {
907 txq = fep->tx_queue[i];
908 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
909
910 /* enable DMA1/2 */
911 if (i)
912 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
913 fep->hwp + FEC_DMA_CFG(i));
914 }
915}
916
917static void fec_enet_reset_skb(struct net_device *ndev)
918{
919 struct fec_enet_private *fep = netdev_priv(ndev);
920 struct fec_enet_priv_tx_q *txq;
921 int i, j;
922
923 for (i = 0; i < fep->num_tx_queues; i++) {
924 txq = fep->tx_queue[i];
925
926 for (j = 0; j < txq->tx_ring_size; j++) {
927 if (txq->tx_skbuff[j]) {
928 dev_kfree_skb_any(txq->tx_skbuff[j]);
929 txq->tx_skbuff[j] = NULL;
930 }
931 }
932 }
Frank Li14109a52013-03-26 16:12:03 +0000933}
934
Russell Kingdbc64a82014-07-08 12:40:12 +0100935/*
936 * This function is called to start or restart the FEC during a link
937 * change, transmit timeout, or to reconfigure the FEC. The network
938 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100939 */
940static void
Russell Kingef833372014-07-08 12:40:43 +0100941fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100942{
943 struct fec_enet_private *fep = netdev_priv(ndev);
944 const struct platform_device_id *id_entry =
945 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000946 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100947 u32 temp_mac[2];
948 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000949 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100950
Fugang Duan106c3142014-09-13 05:00:51 +0800951 /* Whack a reset. We should wait for this.
952 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
953 * instead of reset MAC itself.
954 */
955 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
956 writel(0, fep->hwp + FEC_ECNTRL);
957 } else {
958 writel(1, fep->hwp + FEC_ECNTRL);
959 udelay(10);
960 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100961
962 /*
963 * enet-mac reset will reset mac address registers too,
964 * so need to reconfigure it.
965 */
966 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
967 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
968 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
969 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
970 }
971
972 /* Clear any outstanding interrupt. */
973 writel(0xffc00000, fep->hwp + FEC_IEVENT);
974
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975 /* Set maximum receive buffer size. */
976 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
977
Frank Li14109a52013-03-26 16:12:03 +0000978 fec_enet_bd_init(ndev);
979
Frank Li59d0f742014-09-13 05:00:50 +0800980 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100981
Frank Li59d0f742014-09-13 05:00:50 +0800982 /* Reset tx SKB buffers. */
983 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100984
985 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100986 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100987 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100988 writel(0x04, fep->hwp + FEC_X_CNTRL);
989 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100990 /* No Rcv on Xmit */
991 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100992 writel(0x0, fep->hwp + FEC_X_CNTRL);
993 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100994
Uwe Kleine-König45993652011-01-19 20:47:04 +0100995 /* Set MII speed */
996 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
997
Guenter Roeckd1391932013-06-18 10:04:59 -0700998#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000999 /* set RX checksum */
1000 val = readl(fep->hwp + FEC_RACC);
1001 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1002 val |= FEC_RACC_OPTIONS;
1003 else
1004 val &= ~FEC_RACC_OPTIONS;
1005 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -07001006#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001007
Uwe Kleine-König45993652011-01-19 20:47:04 +01001008 /*
1009 * The phy interface and speed need to get configured
1010 * differently on enet-mac.
1011 */
1012 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001013 /* Enable flow control and length check */
1014 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001015
Shawn Guo230dec62011-09-23 02:12:48 +00001016 /* RGMII, RMII or MII */
1017 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1018 rcntl |= (1 << 6);
1019 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001020 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001021 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001022 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001023
Shawn Guo230dec62011-09-23 02:12:48 +00001024 /* 1G, 100M or 10M */
1025 if (fep->phy_dev) {
1026 if (fep->phy_dev->speed == SPEED_1000)
1027 ecntl |= (1 << 5);
1028 else if (fep->phy_dev->speed == SPEED_100)
1029 rcntl &= ~(1 << 9);
1030 else
1031 rcntl |= (1 << 9);
1032 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001033 } else {
1034#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001035 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001036 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001037 /* disable the gasket and wait */
1038 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1039 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1040 udelay(1);
1041
1042 /*
1043 * configure the gasket:
1044 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001045 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001046 */
Eric Benard8d82f212012-01-12 06:10:28 +00001047 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1048 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1049 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1050 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1051 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001052
1053 /* re-enable the gasket */
1054 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1055 }
1056#endif
1057 }
Frank Libaa70a52013-01-16 16:55:58 +00001058
Guenter Roeckd1391932013-06-18 10:04:59 -07001059#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001060 /* enable pause frame*/
1061 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1062 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1063 fep->phy_dev && fep->phy_dev->pause)) {
1064 rcntl |= FEC_ENET_FCE;
1065
Jim Baxter4c09eed2013-04-19 08:10:49 +00001066 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001067 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1068 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1069 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1070 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1071
1072 /* OPD */
1073 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1074 } else {
1075 rcntl &= ~FEC_ENET_FCE;
1076 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001077#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001078
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001079 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001080
Stefan Wahren84fe6182014-03-12 11:28:19 +01001081 /* Setup multicast filter. */
1082 set_multicast_list(ndev);
1083#ifndef CONFIG_M5272
1084 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1085 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1086#endif
1087
Shawn Guo230dec62011-09-23 02:12:48 +00001088 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1089 /* enable ENET endian swap */
1090 ecntl |= (1 << 8);
1091 /* enable ENET store and forward mode */
1092 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1093 }
1094
Frank Liff43da82013-01-03 16:04:23 +00001095 if (fep->bufdesc_ex)
1096 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001097
Chris Healy38ae92d2013-06-25 23:18:52 -07001098#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001099 /* Enable the MIB statistic event counters */
1100 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001101#endif
1102
Uwe Kleine-König45993652011-01-19 20:47:04 +01001103 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001104 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001105 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001106
Frank Liff43da82013-01-03 16:04:23 +00001107 if (fep->bufdesc_ex)
1108 fec_ptp_start_cyclecounter(ndev);
1109
Uwe Kleine-König45993652011-01-19 20:47:04 +01001110 /* Enable interrupts we wish to service */
1111 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Fugang Duand851b472014-09-17 05:18:52 +08001112
1113 /* Init the interrupt coalescing */
1114 fec_enet_itr_coal_init(ndev);
1115
Uwe Kleine-König45993652011-01-19 20:47:04 +01001116}
1117
1118static void
1119fec_stop(struct net_device *ndev)
1120{
1121 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001122 const struct platform_device_id *id_entry =
1123 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001124 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001125
1126 /* We cannot expect a graceful transmit stop without link !!! */
1127 if (fep->link) {
1128 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1129 udelay(10);
1130 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001131 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001132 }
1133
Fugang Duan106c3142014-09-13 05:00:51 +08001134 /* Whack a reset. We should wait for this.
1135 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1136 * instead of reset MAC itself.
1137 */
1138 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1139 writel(0, fep->hwp + FEC_ECNTRL);
1140 } else {
1141 writel(1, fep->hwp + FEC_ECNTRL);
1142 udelay(10);
1143 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001144 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1145 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001146
1147 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001148 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001149 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001150 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1151 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001152}
1153
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001156fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001158 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Russell King344756f2014-07-08 13:01:59 +01001160 fec_dump(ndev);
1161
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001162 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Russell King36cdc742014-07-08 13:01:44 +01001164 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001165}
1166
Russell King36cdc742014-07-08 13:01:44 +01001167static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001168{
1169 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001170 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001171 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001172
Russell King36cdc742014-07-08 13:01:44 +01001173 rtnl_lock();
1174 if (netif_device_present(ndev) || netif_running(ndev)) {
1175 napi_disable(&fep->napi);
1176 netif_tx_lock_bh(ndev);
1177 fec_restart(ndev);
1178 netif_wake_queue(ndev);
1179 netif_tx_unlock_bh(ndev);
1180 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001181 }
Russell King36cdc742014-07-08 13:01:44 +01001182 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001186fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1187 struct skb_shared_hwtstamps *hwtstamps)
1188{
1189 unsigned long flags;
1190 u64 ns;
1191
1192 spin_lock_irqsave(&fep->tmreg_lock, flags);
1193 ns = timecounter_cyc2time(&fep->tc, ts);
1194 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1195
1196 memset(hwtstamps, 0, sizeof(*hwtstamps));
1197 hwtstamps->hwtstamp = ns_to_ktime(ns);
1198}
1199
1200static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001201fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001204 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001205 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001207 struct fec_enet_priv_tx_q *txq;
1208 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001209 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001210 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001212 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001213
1214 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1215
1216 txq = fep->tx_queue[queue_id];
1217 /* get next bdp of dirty_tx */
1218 nq = netdev_get_tx_queue(ndev, queue_id);
1219 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Frank Lide5fb0a2013-03-03 17:34:25 +00001221 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001222 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001223
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001224 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001225
1226 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001227 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001228 break;
1229
Fugang Duan4d494cd2014-09-13 05:00:48 +08001230 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001231
Fugang Duan4d494cd2014-09-13 05:00:48 +08001232 skb = txq->tx_skbuff[index];
1233 txq->tx_skbuff[index] = NULL;
1234 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001235 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1236 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001237 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001238 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001239 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001240 continue;
1241 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001244 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 BD_ENET_TX_RL | BD_ENET_TX_UN |
1246 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001248 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001250 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001251 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001252 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001253 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001254 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001255 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001256 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001257 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001259 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001260 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
Frank Liff43da82013-01-03 16:04:23 +00001263 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1264 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001265 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001266 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001267
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001268 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001269 skb_tstamp_tx(skb, &shhwtstamps);
1270 }
Frank Liff43da82013-01-03 16:04:23 +00001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /* Deferred means some collisions occurred during transmit,
1273 * but we eventually sent the packet OK.
1274 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001275 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001276 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001277
Sascha Hauer22f6b862009-04-15 01:32:18 +00001278 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001280
Fugang Duan4d494cd2014-09-13 05:00:48 +08001281 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001282
Sascha Hauer22f6b862009-04-15 01:32:18 +00001283 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001284 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001285
Sascha Hauer22f6b862009-04-15 01:32:18 +00001286 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001288 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001289 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1290 if (entries_free >= txq->tx_wake_threshold)
1291 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Russell Kingccea2962014-07-08 13:01:38 +01001294
1295 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001296 if (bdp != txq->cur_tx &&
1297 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1298 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1299}
1300
1301static void
1302fec_enet_tx(struct net_device *ndev)
1303{
1304 struct fec_enet_private *fep = netdev_priv(ndev);
1305 u16 queue_id;
1306 /* First process class A queue, then Class B and Best Effort queue */
1307 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1308 clear_bit(queue_id, &fep->work_tx);
1309 fec_enet_tx_queue(ndev, queue_id);
1310 }
1311 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/* During a receive, the cur_rx points to the current incoming buffer.
1315 * When we update through the ring, if the next incoming buffer has
1316 * not been given to the system, we just set the empty indicator,
1317 * effectively tossing the packet.
1318 */
Frank Lidc975382013-01-28 18:31:42 +00001319static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001320fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001322 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001323 const struct platform_device_id *id_entry =
1324 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001325 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001326 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001327 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct sk_buff *skb;
1329 ushort pkt_len;
1330 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001331 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001332 struct bufdesc_ex *ebdp = NULL;
1333 bool vlan_packet_rcvd = false;
1334 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001335 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001336
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001337#ifdef CONFIG_M532x
1338 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001339#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001340 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1341 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* First, grab all of the stats for the incoming packet.
1344 * These get messed up if we get called due to a busy condition.
1345 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001346 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Sascha Hauer22f6b862009-04-15 01:32:18 +00001348 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Frank Lidc975382013-01-28 18:31:42 +00001350 if (pkt_received >= budget)
1351 break;
1352 pkt_received++;
1353
Sascha Hauer22f6b862009-04-15 01:32:18 +00001354 /* Since we have allocated space to hold a complete frame,
1355 * the last indicator should be set.
1356 */
1357 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001358 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Russell Kingdb3421c2014-07-08 13:01:49 +01001360
Sascha Hauer22f6b862009-04-15 01:32:18 +00001361 /* Check for errors. */
1362 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001364 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001365 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1366 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001367 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001368 }
1369 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001370 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001371 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001372 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001373 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001374 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001376
1377 /* Report late collisions as a frame error.
1378 * On this error, the BD is closed, but we don't know what we
1379 * have in the buffer. So, just drop this frame on the floor.
1380 */
1381 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001382 ndev->stats.rx_errors++;
1383 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001384 goto rx_processing_done;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Sascha Hauer22f6b862009-04-15 01:32:18 +00001387 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001388 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001389 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001390 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Fugang Duan4d494cd2014-09-13 05:00:48 +08001392 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1393 data = rxq->rx_skbuff[index]->data;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001394 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1395 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001396
Shawn Guob5680e02011-01-05 21:13:13 +00001397 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1398 swap_buffer(data, pkt_len);
1399
Jim Baxtercdffcf12013-07-02 22:52:56 +01001400 /* Extract the enhanced buffer descriptor */
1401 ebdp = NULL;
1402 if (fep->bufdesc_ex)
1403 ebdp = (struct bufdesc_ex *)bdp;
1404
1405 /* If this is a VLAN packet remove the VLAN Tag */
1406 vlan_packet_rcvd = false;
1407 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001408 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001409 /* Push and remove the vlan tag */
1410 struct vlan_hdr *vlan_header =
1411 (struct vlan_hdr *) (data + ETH_HLEN);
1412 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1413 pkt_len -= VLAN_HLEN;
1414
1415 vlan_packet_rcvd = true;
1416 }
1417
Sascha Hauer22f6b862009-04-15 01:32:18 +00001418 /* This does 16 byte alignment, exactly what we need.
1419 * The packet length includes FCS, but we don't want to
1420 * include that when passing upstream as it messes up
1421 * bridging applications.
1422 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001423 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Sascha Hauer85498892009-04-15 01:32:21 +00001425 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001426 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001427 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001428 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001429 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001430 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001431
1432 /* Extract the frame data without the VLAN header. */
1433 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1434 if (vlan_packet_rcvd)
1435 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
Nimrod Andyb749fc92014-09-19 14:26:03 +08001436 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
Jim Baxtercdffcf12013-07-02 22:52:56 +01001437 data + payload_offset,
1438 pkt_len - 4 - (2 * ETH_ALEN));
1439
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001440 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001441
Frank Li6605b732012-10-30 18:25:31 +00001442 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001443 if (fep->hwts_rx_en && fep->bufdesc_ex)
1444 fec_enet_hwtstamp(fep, ebdp->ts,
1445 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001446
Jim Baxter4c09eed2013-04-19 08:10:49 +00001447 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001448 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001449 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1450 /* don't check it */
1451 skb->ip_summed = CHECKSUM_UNNECESSARY;
1452 } else {
1453 skb_checksum_none_assert(skb);
1454 }
1455 }
1456
Jim Baxtercdffcf12013-07-02 22:52:56 +01001457 /* Handle received VLAN packets */
1458 if (vlan_packet_rcvd)
1459 __vlan_hwaccel_put_tag(skb,
1460 htons(ETH_P_8021Q),
1461 vlan_tag);
1462
Richard Cochran0affdf32013-08-30 20:28:10 +02001463 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001464 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001465
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001466 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1467 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001468rx_processing_done:
1469 /* Clear the status flags for this buffer */
1470 status &= ~BD_ENET_RX_STATS;
1471
1472 /* Mark the buffer empty */
1473 status |= BD_ENET_RX_EMPTY;
1474 bdp->cbd_sc = status;
1475
Frank Liff43da82013-01-03 16:04:23 +00001476 if (fep->bufdesc_ex) {
1477 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1478
1479 ebdp->cbd_esc = BD_ENET_RX_INT;
1480 ebdp->cbd_prot = 0;
1481 ebdp->cbd_bdu = 0;
1482 }
Frank Li6605b732012-10-30 18:25:31 +00001483
Sascha Hauer22f6b862009-04-15 01:32:18 +00001484 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001485 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001486
Sascha Hauer22f6b862009-04-15 01:32:18 +00001487 /* Doing this here will keep the FEC running while we process
1488 * incoming frames. On a heavily loaded network, we should be
1489 * able to keep up at the expense of system resources.
1490 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001491 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001493 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001494 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Fugang Duan4d494cd2014-09-13 05:00:48 +08001497static int
1498fec_enet_rx(struct net_device *ndev, int budget)
1499{
1500 int pkt_received = 0;
1501 u16 queue_id;
1502 struct fec_enet_private *fep = netdev_priv(ndev);
1503
1504 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1505 clear_bit(queue_id, &fep->work_rx);
1506 pkt_received += fec_enet_rx_queue(ndev,
1507 budget - pkt_received, queue_id);
1508 }
1509 return pkt_received;
1510}
1511
1512static bool
1513fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1514{
1515 if (int_events == 0)
1516 return false;
1517
1518 if (int_events & FEC_ENET_RXF)
1519 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001520 if (int_events & FEC_ENET_RXF_1)
1521 fep->work_rx |= (1 << 0);
1522 if (int_events & FEC_ENET_RXF_2)
1523 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001524
1525 if (int_events & FEC_ENET_TXF)
1526 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001527 if (int_events & FEC_ENET_TXF_1)
1528 fep->work_tx |= (1 << 0);
1529 if (int_events & FEC_ENET_TXF_2)
1530 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001531
1532 return true;
1533}
1534
Uwe Kleine-König45993652011-01-19 20:47:04 +01001535static irqreturn_t
1536fec_enet_interrupt(int irq, void *dev_id)
1537{
1538 struct net_device *ndev = dev_id;
1539 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001540 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001541 uint int_events;
1542 irqreturn_t ret = IRQ_NONE;
1543
Russell King7a168072014-07-08 00:22:44 +01001544 int_events = readl(fep->hwp + FEC_IEVENT);
1545 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001546 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001547
Russell King7a168072014-07-08 00:22:44 +01001548 if (int_events & napi_mask) {
1549 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001550
Russell King7a168072014-07-08 00:22:44 +01001551 /* Disable the NAPI interrupts */
1552 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1553 napi_schedule(&fep->napi);
1554 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001555
Russell King7a168072014-07-08 00:22:44 +01001556 if (int_events & FEC_ENET_MII) {
1557 ret = IRQ_HANDLED;
1558 complete(&fep->mdio_done);
1559 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001560
1561 return ret;
1562}
1563
Frank Lidc975382013-01-28 18:31:42 +00001564static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1565{
1566 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001567 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001568 int pkts;
1569
1570 /*
1571 * Clear any pending transmit or receive interrupts before
1572 * processing the rings to avoid racing with the hardware.
1573 */
1574 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1575
1576 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001577
Frank Lide5fb0a2013-03-03 17:34:25 +00001578 fec_enet_tx(ndev);
1579
Frank Lidc975382013-01-28 18:31:42 +00001580 if (pkts < budget) {
1581 napi_complete(napi);
1582 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1583 }
1584 return pkts;
1585}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001588static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001590 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001591 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001592 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Shawn Guo49da97d2011-01-05 21:13:11 +00001594 /*
1595 * try to get mac address in following order:
1596 *
1597 * 1) module parameter via kernel command line in form
1598 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1599 */
1600 iap = macaddr;
1601
1602 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001603 * 2) from device tree data
1604 */
1605 if (!is_valid_ether_addr(iap)) {
1606 struct device_node *np = fep->pdev->dev.of_node;
1607 if (np) {
1608 const char *mac = of_get_mac_address(np);
1609 if (mac)
1610 iap = (unsigned char *) mac;
1611 }
1612 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001613
1614 /*
1615 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001616 */
1617 if (!is_valid_ether_addr(iap)) {
1618#ifdef CONFIG_M5272
1619 if (FEC_FLASHMAC)
1620 iap = (unsigned char *)FEC_FLASHMAC;
1621#else
1622 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001623 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001624#endif
1625 }
1626
1627 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001628 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001629 */
1630 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001631 *((__be32 *) &tmpaddr[0]) =
1632 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1633 *((__be16 *) &tmpaddr[4]) =
1634 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 iap = &tmpaddr[0];
1636 }
1637
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001638 /*
1639 * 5) random mac address
1640 */
1641 if (!is_valid_ether_addr(iap)) {
1642 /* Report it and use a random ethernet address instead */
1643 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1644 eth_hw_addr_random(ndev);
1645 netdev_info(ndev, "Using random MAC address: %pM\n",
1646 ndev->dev_addr);
1647 return;
1648 }
1649
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001650 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Shawn Guo49da97d2011-01-05 21:13:11 +00001652 /* Adjust MAC if using macaddr */
1653 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001654 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657/* ------------------------------------------------------------------------- */
1658
Bryan Wue6b043d2010-03-31 02:10:44 +00001659/*
1660 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001662static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001664 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001665 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001666 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Bryan Wue6b043d2010-03-31 02:10:44 +00001668 /* Prevent a state halted on mii error */
1669 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1670 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001673
Russell King8ce56242014-07-08 12:40:07 +01001674 /*
1675 * If the netdev is down, or is going down, we're not interested
1676 * in link state events, so just mark our idea of the link as down
1677 * and ignore the event.
1678 */
1679 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1680 fep->link = 0;
1681 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001682 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001683 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001684 status_change = 1;
1685 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001686
Russell Kingef833372014-07-08 12:40:43 +01001687 if (fep->full_duplex != phy_dev->duplex) {
1688 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001689 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001690 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001691
1692 if (phy_dev->speed != fep->speed) {
1693 fep->speed = phy_dev->speed;
1694 status_change = 1;
1695 }
1696
1697 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001698 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001699 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001700 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001701 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001702 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001703 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001704 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001705 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001706 } else {
1707 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001708 napi_disable(&fep->napi);
1709 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001710 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001711 netif_tx_unlock_bh(ndev);
1712 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001713 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001714 status_change = 1;
1715 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001716 }
1717
Bryan Wue6b043d2010-03-31 02:10:44 +00001718 if (status_change)
1719 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Bryan Wue6b043d2010-03-31 02:10:44 +00001722static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Bryan Wue6b043d2010-03-31 02:10:44 +00001724 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001725 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001726
1727 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001728 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001729
1730 /* start a read op */
1731 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1732 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1733 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1734
1735 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001736 time_left = wait_for_completion_timeout(&fep->mdio_done,
1737 usecs_to_jiffies(FEC_MII_TIMEOUT));
1738 if (time_left == 0) {
1739 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001740 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001741 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001742 }
1743
1744 /* return value */
1745 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1746}
1747
1748static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1749 u16 value)
1750{
1751 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001752 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001753
1754 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001755 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001756
Shawn Guo862f0982011-01-05 21:13:09 +00001757 /* start a write op */
1758 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001759 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1760 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1761 fep->hwp + FEC_MII_DATA);
1762
1763 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001764 time_left = wait_for_completion_timeout(&fep->mdio_done,
1765 usecs_to_jiffies(FEC_MII_TIMEOUT));
1766 if (time_left == 0) {
1767 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001768 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001769 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001770 }
1771
1772 return 0;
1773}
1774
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001775static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1776{
1777 struct fec_enet_private *fep = netdev_priv(ndev);
1778 int ret;
1779
1780 if (enable) {
1781 ret = clk_prepare_enable(fep->clk_ahb);
1782 if (ret)
1783 return ret;
1784 ret = clk_prepare_enable(fep->clk_ipg);
1785 if (ret)
1786 goto failed_clk_ipg;
1787 if (fep->clk_enet_out) {
1788 ret = clk_prepare_enable(fep->clk_enet_out);
1789 if (ret)
1790 goto failed_clk_enet_out;
1791 }
1792 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001793 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001794 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001795 if (ret) {
1796 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001797 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001798 } else {
1799 fep->ptp_clk_on = true;
1800 }
1801 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001802 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001803 if (fep->clk_ref) {
1804 ret = clk_prepare_enable(fep->clk_ref);
1805 if (ret)
1806 goto failed_clk_ref;
1807 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001808 } else {
1809 clk_disable_unprepare(fep->clk_ahb);
1810 clk_disable_unprepare(fep->clk_ipg);
1811 if (fep->clk_enet_out)
1812 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001813 if (fep->clk_ptp) {
1814 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001815 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001816 fep->ptp_clk_on = false;
1817 mutex_unlock(&fep->ptp_clk_mutex);
1818 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001819 if (fep->clk_ref)
1820 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001821 }
1822
1823 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001824
1825failed_clk_ref:
1826 if (fep->clk_ref)
1827 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001828failed_clk_ptp:
1829 if (fep->clk_enet_out)
1830 clk_disable_unprepare(fep->clk_enet_out);
1831failed_clk_enet_out:
1832 clk_disable_unprepare(fep->clk_ipg);
1833failed_clk_ipg:
1834 clk_disable_unprepare(fep->clk_ahb);
1835
1836 return ret;
1837}
1838
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001839static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001840{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001841 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001842 const struct platform_device_id *id_entry =
1843 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001844 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001845 char mdio_bus_id[MII_BUS_ID_SIZE];
1846 char phy_name[MII_BUS_ID_SIZE + 3];
1847 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001848 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001849
Bryan Wu418bd0d2010-05-28 03:40:39 -07001850 fep->phy_dev = NULL;
1851
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001852 if (fep->phy_node) {
1853 phy_dev = of_phy_connect(ndev, fep->phy_node,
1854 &fec_enet_adjust_link, 0,
1855 fep->phy_interface);
1856 } else {
1857 /* check for attached phy */
1858 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1859 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1860 continue;
1861 if (fep->mii_bus->phy_map[phy_id] == NULL)
1862 continue;
1863 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1864 continue;
1865 if (dev_id--)
1866 continue;
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001867 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001868 break;
1869 }
1870
1871 if (phy_id >= PHY_MAX_ADDR) {
1872 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Rickard Strandqvist949bdd22014-09-14 19:32:42 +02001873 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001874 phy_id = 0;
1875 }
1876
1877 snprintf(phy_name, sizeof(phy_name),
1878 PHY_ID_FMT, mdio_bus_id, phy_id);
1879 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1880 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001881 }
1882
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001883 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001884 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001885 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001886 }
1887
1888 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001889 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001890 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001891 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001892#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001893 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001894#endif
Frank Libaa70a52013-01-16 16:55:58 +00001895 }
Shawn Guo230dec62011-09-23 02:12:48 +00001896 else
1897 phy_dev->supported &= PHY_BASIC_FEATURES;
1898
Bryan Wue6b043d2010-03-31 02:10:44 +00001899 phy_dev->advertising = phy_dev->supported;
1900
1901 fep->phy_dev = phy_dev;
1902 fep->link = 0;
1903 fep->full_duplex = 0;
1904
Joe Perches31b77202013-04-13 19:03:17 +00001905 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1906 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1907 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001908
Bryan Wue6b043d2010-03-31 02:10:44 +00001909 return 0;
1910}
1911
1912static int fec_enet_mii_init(struct platform_device *pdev)
1913{
Shawn Guob5680e02011-01-05 21:13:13 +00001914 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001915 struct net_device *ndev = platform_get_drvdata(pdev);
1916 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001917 const struct platform_device_id *id_entry =
1918 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001919 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001920 int err = -ENXIO, i;
1921
Shawn Guob5680e02011-01-05 21:13:13 +00001922 /*
1923 * The dual fec interfaces are not equivalent with enet-mac.
1924 * Here are the differences:
1925 *
1926 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1927 * - fec0 acts as the 1588 time master while fec1 is slave
1928 * - external phys can only be configured by fec0
1929 *
1930 * That is to say fec1 can not work independently. It only works
1931 * when fec0 is working. The reason behind this design is that the
1932 * second interface is added primarily for Switch mode.
1933 *
1934 * Because of the last point above, both phys are attached on fec0
1935 * mdio interface in board design, and need to be configured by
1936 * fec0 mii_bus.
1937 */
Shawn Guo43af9402011-12-05 05:01:15 +00001938 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001939 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001940 if (mii_cnt && fec0_mii_bus) {
1941 fep->mii_bus = fec0_mii_bus;
1942 mii_cnt++;
1943 return 0;
1944 }
1945 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001946 }
1947
Bryan Wue6b043d2010-03-31 02:10:44 +00001948 fep->mii_timeout = 0;
1949
1950 /*
1951 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001952 *
1953 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1954 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1955 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1956 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001957 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001958 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001959 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1960 fep->phy_speed--;
1961 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001962 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1963
1964 fep->mii_bus = mdiobus_alloc();
1965 if (fep->mii_bus == NULL) {
1966 err = -ENOMEM;
1967 goto err_out;
1968 }
1969
1970 fep->mii_bus->name = "fec_enet_mii_bus";
1971 fep->mii_bus->read = fec_enet_mdio_read;
1972 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001973 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1974 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001975 fep->mii_bus->priv = fep;
1976 fep->mii_bus->parent = &pdev->dev;
1977
1978 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1979 if (!fep->mii_bus->irq) {
1980 err = -ENOMEM;
1981 goto err_out_free_mdiobus;
1982 }
1983
1984 for (i = 0; i < PHY_MAX_ADDR; i++)
1985 fep->mii_bus->irq[i] = PHY_POLL;
1986
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001987 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
1988 if (node) {
1989 err = of_mdiobus_register(fep->mii_bus, node);
1990 of_node_put(node);
1991 } else {
1992 err = mdiobus_register(fep->mii_bus);
1993 }
1994
1995 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00001996 goto err_out_free_mdio_irq;
1997
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001998 mii_cnt++;
1999
Shawn Guob5680e02011-01-05 21:13:13 +00002000 /* save fec0 mii_bus */
2001 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2002 fec0_mii_bus = fep->mii_bus;
2003
Bryan Wue6b043d2010-03-31 02:10:44 +00002004 return 0;
2005
Bryan Wue6b043d2010-03-31 02:10:44 +00002006err_out_free_mdio_irq:
2007 kfree(fep->mii_bus->irq);
2008err_out_free_mdiobus:
2009 mdiobus_free(fep->mii_bus);
2010err_out:
2011 return err;
2012}
2013
2014static void fec_enet_mii_remove(struct fec_enet_private *fep)
2015{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002016 if (--mii_cnt == 0) {
2017 mdiobus_unregister(fep->mii_bus);
2018 kfree(fep->mii_bus->irq);
2019 mdiobus_free(fep->mii_bus);
2020 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002021}
2022
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002023static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002024 struct ethtool_cmd *cmd)
2025{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002026 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002027 struct phy_device *phydev = fep->phy_dev;
2028
2029 if (!phydev)
2030 return -ENODEV;
2031
2032 return phy_ethtool_gset(phydev, cmd);
2033}
2034
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002035static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002036 struct ethtool_cmd *cmd)
2037{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002038 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002039 struct phy_device *phydev = fep->phy_dev;
2040
2041 if (!phydev)
2042 return -ENODEV;
2043
2044 return phy_ethtool_sset(phydev, cmd);
2045}
2046
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002047static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002048 struct ethtool_drvinfo *info)
2049{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002050 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Jiri Pirko7826d432013-01-06 00:44:26 +00002052 strlcpy(info->driver, fep->pdev->dev.driver->name,
2053 sizeof(info->driver));
2054 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2055 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
Bryan Wue6b043d2010-03-31 02:10:44 +00002057
Frank Li5ebae4892013-01-06 16:25:07 +00002058static int fec_enet_get_ts_info(struct net_device *ndev,
2059 struct ethtool_ts_info *info)
2060{
2061 struct fec_enet_private *fep = netdev_priv(ndev);
2062
2063 if (fep->bufdesc_ex) {
2064
2065 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2066 SOF_TIMESTAMPING_RX_SOFTWARE |
2067 SOF_TIMESTAMPING_SOFTWARE |
2068 SOF_TIMESTAMPING_TX_HARDWARE |
2069 SOF_TIMESTAMPING_RX_HARDWARE |
2070 SOF_TIMESTAMPING_RAW_HARDWARE;
2071 if (fep->ptp_clock)
2072 info->phc_index = ptp_clock_index(fep->ptp_clock);
2073 else
2074 info->phc_index = -1;
2075
2076 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2077 (1 << HWTSTAMP_TX_ON);
2078
2079 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2080 (1 << HWTSTAMP_FILTER_ALL);
2081 return 0;
2082 } else {
2083 return ethtool_op_get_ts_info(ndev, info);
2084 }
2085}
2086
Guenter Roeckd1391932013-06-18 10:04:59 -07002087#if !defined(CONFIG_M5272)
2088
Frank Libaa70a52013-01-16 16:55:58 +00002089static void fec_enet_get_pauseparam(struct net_device *ndev,
2090 struct ethtool_pauseparam *pause)
2091{
2092 struct fec_enet_private *fep = netdev_priv(ndev);
2093
2094 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2095 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2096 pause->rx_pause = pause->tx_pause;
2097}
2098
2099static int fec_enet_set_pauseparam(struct net_device *ndev,
2100 struct ethtool_pauseparam *pause)
2101{
2102 struct fec_enet_private *fep = netdev_priv(ndev);
2103
Russell King0b146ca2014-07-08 00:22:59 +01002104 if (!fep->phy_dev)
2105 return -ENODEV;
2106
Frank Libaa70a52013-01-16 16:55:58 +00002107 if (pause->tx_pause != pause->rx_pause) {
2108 netdev_info(ndev,
2109 "hardware only support enable/disable both tx and rx");
2110 return -EINVAL;
2111 }
2112
2113 fep->pause_flag = 0;
2114
2115 /* tx pause must be same as rx pause */
2116 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2117 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2118
2119 if (pause->rx_pause || pause->autoneg) {
2120 fep->phy_dev->supported |= ADVERTISED_Pause;
2121 fep->phy_dev->advertising |= ADVERTISED_Pause;
2122 } else {
2123 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2124 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2125 }
2126
2127 if (pause->autoneg) {
2128 if (netif_running(ndev))
2129 fec_stop(ndev);
2130 phy_start_aneg(fep->phy_dev);
2131 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002132 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002133 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002134 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002135 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002136 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002137 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002138 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002139 }
Frank Libaa70a52013-01-16 16:55:58 +00002140
2141 return 0;
2142}
2143
Chris Healy38ae92d2013-06-25 23:18:52 -07002144static const struct fec_stat {
2145 char name[ETH_GSTRING_LEN];
2146 u16 offset;
2147} fec_stats[] = {
2148 /* RMON TX */
2149 { "tx_dropped", RMON_T_DROP },
2150 { "tx_packets", RMON_T_PACKETS },
2151 { "tx_broadcast", RMON_T_BC_PKT },
2152 { "tx_multicast", RMON_T_MC_PKT },
2153 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2154 { "tx_undersize", RMON_T_UNDERSIZE },
2155 { "tx_oversize", RMON_T_OVERSIZE },
2156 { "tx_fragment", RMON_T_FRAG },
2157 { "tx_jabber", RMON_T_JAB },
2158 { "tx_collision", RMON_T_COL },
2159 { "tx_64byte", RMON_T_P64 },
2160 { "tx_65to127byte", RMON_T_P65TO127 },
2161 { "tx_128to255byte", RMON_T_P128TO255 },
2162 { "tx_256to511byte", RMON_T_P256TO511 },
2163 { "tx_512to1023byte", RMON_T_P512TO1023 },
2164 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2165 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2166 { "tx_octets", RMON_T_OCTETS },
2167
2168 /* IEEE TX */
2169 { "IEEE_tx_drop", IEEE_T_DROP },
2170 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2171 { "IEEE_tx_1col", IEEE_T_1COL },
2172 { "IEEE_tx_mcol", IEEE_T_MCOL },
2173 { "IEEE_tx_def", IEEE_T_DEF },
2174 { "IEEE_tx_lcol", IEEE_T_LCOL },
2175 { "IEEE_tx_excol", IEEE_T_EXCOL },
2176 { "IEEE_tx_macerr", IEEE_T_MACERR },
2177 { "IEEE_tx_cserr", IEEE_T_CSERR },
2178 { "IEEE_tx_sqe", IEEE_T_SQE },
2179 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2180 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2181
2182 /* RMON RX */
2183 { "rx_packets", RMON_R_PACKETS },
2184 { "rx_broadcast", RMON_R_BC_PKT },
2185 { "rx_multicast", RMON_R_MC_PKT },
2186 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2187 { "rx_undersize", RMON_R_UNDERSIZE },
2188 { "rx_oversize", RMON_R_OVERSIZE },
2189 { "rx_fragment", RMON_R_FRAG },
2190 { "rx_jabber", RMON_R_JAB },
2191 { "rx_64byte", RMON_R_P64 },
2192 { "rx_65to127byte", RMON_R_P65TO127 },
2193 { "rx_128to255byte", RMON_R_P128TO255 },
2194 { "rx_256to511byte", RMON_R_P256TO511 },
2195 { "rx_512to1023byte", RMON_R_P512TO1023 },
2196 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2197 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2198 { "rx_octets", RMON_R_OCTETS },
2199
2200 /* IEEE RX */
2201 { "IEEE_rx_drop", IEEE_R_DROP },
2202 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2203 { "IEEE_rx_crc", IEEE_R_CRC },
2204 { "IEEE_rx_align", IEEE_R_ALIGN },
2205 { "IEEE_rx_macerr", IEEE_R_MACERR },
2206 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2207 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2208};
2209
2210static void fec_enet_get_ethtool_stats(struct net_device *dev,
2211 struct ethtool_stats *stats, u64 *data)
2212{
2213 struct fec_enet_private *fep = netdev_priv(dev);
2214 int i;
2215
2216 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2217 data[i] = readl(fep->hwp + fec_stats[i].offset);
2218}
2219
2220static void fec_enet_get_strings(struct net_device *netdev,
2221 u32 stringset, u8 *data)
2222{
2223 int i;
2224 switch (stringset) {
2225 case ETH_SS_STATS:
2226 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2227 memcpy(data + i * ETH_GSTRING_LEN,
2228 fec_stats[i].name, ETH_GSTRING_LEN);
2229 break;
2230 }
2231}
2232
2233static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2234{
2235 switch (sset) {
2236 case ETH_SS_STATS:
2237 return ARRAY_SIZE(fec_stats);
2238 default:
2239 return -EOPNOTSUPP;
2240 }
2241}
Guenter Roeckd1391932013-06-18 10:04:59 -07002242#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002243
Chris Healy32bc9b42013-06-17 07:25:06 -07002244static int fec_enet_nway_reset(struct net_device *dev)
2245{
2246 struct fec_enet_private *fep = netdev_priv(dev);
2247 struct phy_device *phydev = fep->phy_dev;
2248
2249 if (!phydev)
2250 return -ENODEV;
2251
2252 return genphy_restart_aneg(phydev);
2253}
2254
Fugang Duand851b472014-09-17 05:18:52 +08002255/* ITR clock source is enet system clock (clk_ahb).
2256 * TCTT unit is cycle_ns * 64 cycle
2257 * So, the ICTT value = X us / (cycle_ns * 64)
2258 */
2259static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2260{
2261 struct fec_enet_private *fep = netdev_priv(ndev);
2262
2263 return us * (fep->itr_clk_rate / 64000) / 1000;
2264}
2265
2266/* Set threshold for interrupt coalescing */
2267static void fec_enet_itr_coal_set(struct net_device *ndev)
2268{
2269 struct fec_enet_private *fep = netdev_priv(ndev);
2270 const struct platform_device_id *id_entry =
2271 platform_get_device_id(fep->pdev);
2272 int rx_itr, tx_itr;
2273
2274 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2275 return;
2276
2277 /* Must be greater than zero to avoid unpredictable behavior */
2278 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2279 !fep->tx_time_itr || !fep->tx_pkts_itr)
2280 return;
2281
2282 /* Select enet system clock as Interrupt Coalescing
2283 * timer Clock Source
2284 */
2285 rx_itr = FEC_ITR_CLK_SEL;
2286 tx_itr = FEC_ITR_CLK_SEL;
2287
2288 /* set ICFT and ICTT */
2289 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2290 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2291 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2292 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2293
2294 rx_itr |= FEC_ITR_EN;
2295 tx_itr |= FEC_ITR_EN;
2296
2297 writel(tx_itr, fep->hwp + FEC_TXIC0);
2298 writel(rx_itr, fep->hwp + FEC_RXIC0);
2299 writel(tx_itr, fep->hwp + FEC_TXIC1);
2300 writel(rx_itr, fep->hwp + FEC_RXIC1);
2301 writel(tx_itr, fep->hwp + FEC_TXIC2);
2302 writel(rx_itr, fep->hwp + FEC_RXIC2);
2303}
2304
2305static int
2306fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2307{
2308 struct fec_enet_private *fep = netdev_priv(ndev);
2309 const struct platform_device_id *id_entry =
2310 platform_get_device_id(fep->pdev);
2311
2312 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2313 return -EOPNOTSUPP;
2314
2315 ec->rx_coalesce_usecs = fep->rx_time_itr;
2316 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2317
2318 ec->tx_coalesce_usecs = fep->tx_time_itr;
2319 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2320
2321 return 0;
2322}
2323
2324static int
2325fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2326{
2327 struct fec_enet_private *fep = netdev_priv(ndev);
2328 const struct platform_device_id *id_entry =
2329 platform_get_device_id(fep->pdev);
2330
2331 unsigned int cycle;
2332
2333 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2334 return -EOPNOTSUPP;
2335
2336 if (ec->rx_max_coalesced_frames > 255) {
2337 pr_err("Rx coalesced frames exceed hardware limiation");
2338 return -EINVAL;
2339 }
2340
2341 if (ec->tx_max_coalesced_frames > 255) {
2342 pr_err("Tx coalesced frame exceed hardware limiation");
2343 return -EINVAL;
2344 }
2345
2346 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2347 if (cycle > 0xFFFF) {
2348 pr_err("Rx coalesed usec exceeed hardware limiation");
2349 return -EINVAL;
2350 }
2351
2352 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2353 if (cycle > 0xFFFF) {
2354 pr_err("Rx coalesed usec exceeed hardware limiation");
2355 return -EINVAL;
2356 }
2357
2358 fep->rx_time_itr = ec->rx_coalesce_usecs;
2359 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2360
2361 fep->tx_time_itr = ec->tx_coalesce_usecs;
2362 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2363
2364 fec_enet_itr_coal_set(ndev);
2365
2366 return 0;
2367}
2368
2369static void fec_enet_itr_coal_init(struct net_device *ndev)
2370{
2371 struct ethtool_coalesce ec;
2372
2373 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2374 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2375
2376 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2377 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2378
2379 fec_enet_set_coalesce(ndev, &ec);
2380}
2381
stephen hemminger9b07be42012-01-04 12:59:49 +00002382static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002383 .get_settings = fec_enet_get_settings,
2384 .set_settings = fec_enet_set_settings,
2385 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002386 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002387 .get_link = ethtool_op_get_link,
Fugang Duand851b472014-09-17 05:18:52 +08002388 .get_coalesce = fec_enet_get_coalesce,
2389 .set_coalesce = fec_enet_set_coalesce,
Chris Healy38ae92d2013-06-25 23:18:52 -07002390#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002391 .get_pauseparam = fec_enet_get_pauseparam,
2392 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002393 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002394 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002395 .get_sset_count = fec_enet_get_sset_count,
2396#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002397 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002398};
2399
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002400static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002401{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002402 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002403 struct phy_device *phydev = fep->phy_dev;
2404
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002405 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002406 return -EINVAL;
2407
2408 if (!phydev)
2409 return -ENODEV;
2410
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002411 if (fep->bufdesc_ex) {
2412 if (cmd == SIOCSHWTSTAMP)
2413 return fec_ptp_set(ndev, rq);
2414 if (cmd == SIOCGHWTSTAMP)
2415 return fec_ptp_get(ndev, rq);
2416 }
Frank Liff43da82013-01-03 16:04:23 +00002417
Richard Cochran28b04112010-07-17 08:48:55 +00002418 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002421static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002422{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002423 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002424 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002425 struct sk_buff *skb;
2426 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002427 struct fec_enet_priv_tx_q *txq;
2428 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f742014-09-13 05:00:50 +08002429 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002430
Frank Li59d0f742014-09-13 05:00:50 +08002431 for (q = 0; q < fep->num_rx_queues; q++) {
2432 rxq = fep->rx_queue[q];
2433 bdp = rxq->rx_bd_base;
2434 for (i = 0; i < rxq->rx_ring_size; i++) {
2435 skb = rxq->rx_skbuff[i];
2436 rxq->rx_skbuff[i] = NULL;
2437 if (skb) {
2438 dma_unmap_single(&fep->pdev->dev,
2439 bdp->cbd_bufaddr,
2440 FEC_ENET_RX_FRSIZE,
2441 DMA_FROM_DEVICE);
2442 dev_kfree_skb(skb);
2443 }
2444 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002445 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002446 }
2447
Frank Li59d0f742014-09-13 05:00:50 +08002448 for (q = 0; q < fep->num_tx_queues; q++) {
2449 txq = fep->tx_queue[q];
2450 bdp = txq->tx_bd_base;
2451 for (i = 0; i < txq->tx_ring_size; i++) {
2452 kfree(txq->tx_bounce[i]);
2453 txq->tx_bounce[i] = NULL;
2454 skb = txq->tx_skbuff[i];
2455 txq->tx_skbuff[i] = NULL;
2456 dev_kfree_skb(skb);
2457 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002458 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002459}
2460
Frank Li59d0f742014-09-13 05:00:50 +08002461static void fec_enet_free_queue(struct net_device *ndev)
2462{
2463 struct fec_enet_private *fep = netdev_priv(ndev);
2464 int i;
2465 struct fec_enet_priv_tx_q *txq;
2466
2467 for (i = 0; i < fep->num_tx_queues; i++)
2468 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2469 txq = fep->tx_queue[i];
2470 dma_free_coherent(NULL,
2471 txq->tx_ring_size * TSO_HEADER_SIZE,
2472 txq->tso_hdrs,
2473 txq->tso_hdrs_dma);
2474 }
2475
2476 for (i = 0; i < fep->num_rx_queues; i++)
2477 if (fep->rx_queue[i])
2478 kfree(fep->rx_queue[i]);
2479
2480 for (i = 0; i < fep->num_tx_queues; i++)
2481 if (fep->tx_queue[i])
2482 kfree(fep->tx_queue[i]);
2483}
2484
2485static int fec_enet_alloc_queue(struct net_device *ndev)
2486{
2487 struct fec_enet_private *fep = netdev_priv(ndev);
2488 int i;
2489 int ret = 0;
2490 struct fec_enet_priv_tx_q *txq;
2491
2492 for (i = 0; i < fep->num_tx_queues; i++) {
2493 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2494 if (!txq) {
2495 ret = -ENOMEM;
2496 goto alloc_failed;
2497 }
2498
2499 fep->tx_queue[i] = txq;
2500 txq->tx_ring_size = TX_RING_SIZE;
2501 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2502
2503 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2504 txq->tx_wake_threshold =
2505 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2506
2507 txq->tso_hdrs = dma_alloc_coherent(NULL,
2508 txq->tx_ring_size * TSO_HEADER_SIZE,
2509 &txq->tso_hdrs_dma,
2510 GFP_KERNEL);
2511 if (!txq->tso_hdrs) {
2512 ret = -ENOMEM;
2513 goto alloc_failed;
2514 }
2515 }
2516
2517 for (i = 0; i < fep->num_rx_queues; i++) {
2518 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2519 GFP_KERNEL);
2520 if (!fep->rx_queue[i]) {
2521 ret = -ENOMEM;
2522 goto alloc_failed;
2523 }
2524
2525 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2526 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2527 }
2528 return ret;
2529
2530alloc_failed:
2531 fec_enet_free_queue(ndev);
2532 return ret;
2533}
2534
2535static int
2536fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002537{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002538 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002539 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002540 struct sk_buff *skb;
2541 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002542 struct fec_enet_priv_rx_q *rxq;
Fugang Duan41ef84c2014-09-13 05:00:54 +08002543 unsigned int off;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002544
Frank Li59d0f742014-09-13 05:00:50 +08002545 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002546 bdp = rxq->rx_bd_base;
2547 for (i = 0; i < rxq->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002548 dma_addr_t addr;
2549
Fabio Estevamb72061a2012-02-07 08:27:31 +00002550 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002551 if (!skb)
2552 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002553
Fugang Duan41ef84c2014-09-13 05:00:54 +08002554 off = ((unsigned long)skb->data) & fep->rx_align;
2555 if (off)
2556 skb_reserve(skb, fep->rx_align + 1 - off);
2557
Russell King730ee362014-07-08 00:23:14 +01002558 addr = dma_map_single(&fep->pdev->dev, skb->data,
Fugang Duan41ef84c2014-09-13 05:00:54 +08002559 FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE);
2560
Russell King730ee362014-07-08 00:23:14 +01002561 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2562 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002563 if (net_ratelimit())
2564 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002565 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002566 }
Russell King730ee362014-07-08 00:23:14 +01002567
Fugang Duan4d494cd2014-09-13 05:00:48 +08002568 rxq->rx_skbuff[i] = skb;
Russell King730ee362014-07-08 00:23:14 +01002569 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002570 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002571
2572 if (fep->bufdesc_ex) {
2573 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2574 ebdp->cbd_esc = BD_ENET_RX_INT;
2575 }
2576
Frank Li59d0f742014-09-13 05:00:50 +08002577 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002578 }
2579
2580 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002581 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002582 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f742014-09-13 05:00:50 +08002583 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002584
Frank Li59d0f742014-09-13 05:00:50 +08002585 err_alloc:
2586 fec_enet_free_buffers(ndev);
2587 return -ENOMEM;
2588}
2589
2590static int
2591fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2592{
2593 struct fec_enet_private *fep = netdev_priv(ndev);
2594 unsigned int i;
2595 struct bufdesc *bdp;
2596 struct fec_enet_priv_tx_q *txq;
2597
2598 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002599 bdp = txq->tx_bd_base;
2600 for (i = 0; i < txq->tx_ring_size; i++) {
2601 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2602 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002603 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002604
2605 bdp->cbd_sc = 0;
2606 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002607
Frank Liff43da82013-01-03 16:04:23 +00002608 if (fep->bufdesc_ex) {
2609 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002610 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002611 }
2612
Frank Li59d0f742014-09-13 05:00:50 +08002613 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002614 }
2615
2616 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002617 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002618 bdp->cbd_sc |= BD_SC_WRAP;
2619
2620 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002621
2622 err_alloc:
2623 fec_enet_free_buffers(ndev);
2624 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002625}
2626
Frank Li59d0f742014-09-13 05:00:50 +08002627static int fec_enet_alloc_buffers(struct net_device *ndev)
2628{
2629 struct fec_enet_private *fep = netdev_priv(ndev);
2630 unsigned int i;
2631
2632 for (i = 0; i < fep->num_rx_queues; i++)
2633 if (fec_enet_alloc_rxq_buffers(ndev, i))
2634 return -ENOMEM;
2635
2636 for (i = 0; i < fep->num_tx_queues; i++)
2637 if (fec_enet_alloc_txq_buffers(ndev, i))
2638 return -ENOMEM;
2639 return 0;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002643fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002645 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002646 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002648 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002649 ret = fec_enet_clk_enable(ndev, true);
2650 if (ret)
2651 return ret;
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 /* I should reset the ring buffers here, but I don't yet know
2654 * a simple way to do that.
2655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002657 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002658 if (ret)
2659 return ret;
2660
Bryan Wu418bd0d2010-05-28 03:40:39 -07002661 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002662 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002663 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002664 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002665 return ret;
2666 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002667
Russell Kingef833372014-07-08 12:40:43 +01002668 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002669 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002670 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002671 netif_tx_start_all_queues(ndev);
2672
Sascha Hauer22f6b862009-04-15 01:32:18 +00002673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
2676static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002677fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002679 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Russell Kingd76cfae2014-07-08 00:23:04 +01002681 phy_stop(fep->phy_dev);
2682
Russell King31a6de32014-07-08 12:40:23 +01002683 if (netif_device_present(ndev)) {
2684 napi_disable(&fep->napi);
2685 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002686 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Russell King635cf172014-07-08 00:22:54 +01002689 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002690 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002691
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002692 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002693 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002694 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return 0;
2697}
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699/* Set or clear the multicast filter for this adaptor.
2700 * Skeleton taken from sunlance driver.
2701 * The CPM Ethernet implementation allows Multicast as well as individual
2702 * MAC address filtering. Some of the drivers check to make sure it is
2703 * a group multicast address, and discard those that are not. I guess I
2704 * will do the same for now, but just remove the test if you want
2705 * individual filtering as well (do the upper net layers want or support
2706 * this kind of feature?).
2707 */
2708
2709#define HASH_BITS 6 /* #bits in hash */
2710#define CRC32_POLY 0xEDB88320
2711
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002712static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002714 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002715 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002716 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 unsigned char hash;
2718
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002719 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002720 tmp = readl(fep->hwp + FEC_R_CNTRL);
2721 tmp |= 0x8;
2722 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002723 return;
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Sascha Hauer4e831832009-04-15 01:32:19 +00002726 tmp = readl(fep->hwp + FEC_R_CNTRL);
2727 tmp &= ~0x8;
2728 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002729
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002730 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002731 /* Catch all multicast addresses, so set the
2732 * filter to all 1's
2733 */
2734 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2735 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Sascha Hauer4e831832009-04-15 01:32:19 +00002737 return;
2738 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002739
Sascha Hauer4e831832009-04-15 01:32:19 +00002740 /* Clear filter and add the addresses in hash register
2741 */
2742 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2743 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002745 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002746 /* calculate crc32 value of mac address */
2747 crc = 0xffffffff;
2748
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002749 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002750 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002751 for (bit = 0; bit < 8; bit++, data >>= 1) {
2752 crc = (crc >> 1) ^
2753 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
2755 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002756
2757 /* only upper 6 bits (HASH_BITS) are used
2758 * which point to specific bit in he hash registers
2759 */
2760 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2761
2762 if (hash > 31) {
2763 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2764 tmp |= 1 << (hash - 32);
2765 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2766 } else {
2767 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2768 tmp |= 1 << hash;
2769 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772}
2773
Sascha Hauer22f6b862009-04-15 01:32:18 +00002774/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002775static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002776fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002778 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002779 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Lucas Stach44934fa2014-03-30 21:32:08 +02002781 if (addr) {
2782 if (!is_valid_ether_addr(addr->sa_data))
2783 return -EADDRNOTAVAIL;
2784 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2785 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002786
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002787 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2788 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002789 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002790 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002791 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002795#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002796/**
2797 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002798 * @dev: The FEC network adapter
2799 *
2800 * Polled functionality used by netconsole and others in non interrupt mode
2801 *
2802 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002803static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002804{
2805 int i;
2806 struct fec_enet_private *fep = netdev_priv(dev);
2807
2808 for (i = 0; i < FEC_IRQ_NUM; i++) {
2809 if (fep->irq[i] > 0) {
2810 disable_irq(fep->irq[i]);
2811 fec_enet_interrupt(fep->irq[i], dev);
2812 enable_irq(fep->irq[i]);
2813 }
2814 }
2815}
2816#endif
2817
Russell King8506fa12014-07-08 12:40:33 +01002818#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2819
Jim Baxter4c09eed2013-04-19 08:10:49 +00002820static int fec_set_features(struct net_device *netdev,
2821 netdev_features_t features)
2822{
2823 struct fec_enet_private *fep = netdev_priv(netdev);
2824 netdev_features_t changed = features ^ netdev->features;
2825
Russell King8506fa12014-07-08 12:40:33 +01002826 /* Quiesce the device if necessary */
2827 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2828 napi_disable(&fep->napi);
2829 netif_tx_lock_bh(netdev);
2830 fec_stop(netdev);
2831 }
2832
Jim Baxter4c09eed2013-04-19 08:10:49 +00002833 netdev->features = features;
2834
2835 /* Receive checksum has been changed */
2836 if (changed & NETIF_F_RXCSUM) {
2837 if (features & NETIF_F_RXCSUM)
2838 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2839 else
2840 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002841 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002842
Russell King8506fa12014-07-08 12:40:33 +01002843 /* Resume the device after updates */
2844 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002845 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002846 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002847 netif_tx_unlock_bh(netdev);
2848 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002849 }
2850
2851 return 0;
2852}
2853
Sascha Hauer009fda82009-04-15 01:32:23 +00002854static const struct net_device_ops fec_netdev_ops = {
2855 .ndo_open = fec_enet_open,
2856 .ndo_stop = fec_enet_close,
2857 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002858 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002859 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002860 .ndo_validate_addr = eth_validate_addr,
2861 .ndo_tx_timeout = fec_timeout,
2862 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002863 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002864#ifdef CONFIG_NET_POLL_CONTROLLER
2865 .ndo_poll_controller = fec_poll_controller,
2866#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002867 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002868};
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /*
2871 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002874static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002876 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002877 const struct platform_device_id *id_entry =
2878 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002879 struct fec_enet_priv_tx_q *txq;
2880 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002881 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002882 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002883 int bd_size;
Frank Li59d0f742014-09-13 05:00:50 +08002884 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002885
Fugang Duan41ef84c2014-09-13 05:00:54 +08002886#if defined(CONFIG_ARM)
2887 fep->rx_align = 0xf;
2888 fep->tx_align = 0xf;
2889#else
2890 fep->rx_align = 0x3;
2891 fep->tx_align = 0x3;
2892#endif
2893
Frank Li59d0f742014-09-13 05:00:50 +08002894 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002895
Nimrod Andy55d02182014-06-12 08:16:21 +08002896 if (fep->bufdesc_ex)
2897 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2898 else
2899 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002900 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002901 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002903 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002904 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002905 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002906 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002907 return -ENOMEM;
2908 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002909
Fugang Duan4d494cd2014-09-13 05:00:48 +08002910 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Shawn Guo49da97d2011-01-05 21:13:11 +00002912 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002913 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002914 /* make sure MAC we just acquired is programmed into the hw */
2915 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002917 /* Set receive and transmit descriptor base. */
Frank Li59d0f742014-09-13 05:00:50 +08002918 for (i = 0; i < fep->num_rx_queues; i++) {
2919 rxq = fep->rx_queue[i];
2920 rxq->index = i;
2921 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2922 rxq->bd_dma = bd_dma;
2923 if (fep->bufdesc_ex) {
2924 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2925 cbd_base = (struct bufdesc *)
2926 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2927 } else {
2928 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2929 cbd_base += rxq->rx_ring_size;
2930 }
2931 }
2932
2933 for (i = 0; i < fep->num_tx_queues; i++) {
2934 txq = fep->tx_queue[i];
2935 txq->index = i;
2936 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2937 txq->bd_dma = bd_dma;
2938 if (fep->bufdesc_ex) {
2939 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
2940 cbd_base = (struct bufdesc *)
2941 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
2942 } else {
2943 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
2944 cbd_base += txq->tx_ring_size;
2945 }
2946 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Sascha Hauer22f6b862009-04-15 01:32:18 +00002949 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002950 ndev->watchdog_timeo = TX_TIMEOUT;
2951 ndev->netdev_ops = &fec_netdev_ops;
2952 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002953
Frank Lidc975382013-01-28 18:31:42 +00002954 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002955 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002956
Nimrod Andy09d1e542014-06-12 08:16:20 +08002957 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002958 /* enable hw VLAN support */
2959 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002960
Shawn Guo48496252013-05-08 21:08:22 +00002961 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002962 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2963
Shawn Guo48496252013-05-08 21:08:22 +00002964 /* enable hw accelerator */
2965 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002966 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002967 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2968 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002969
Fugang Duan41ef84c2014-09-13 05:00:54 +08002970 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
2971 fep->tx_align = 0;
2972 fep->rx_align = 0x3f;
2973 }
2974
Nimrod Andy09d1e542014-06-12 08:16:20 +08002975 ndev->hw_features = ndev->features;
2976
Russell Kingef833372014-07-08 12:40:43 +01002977 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return 0;
2980}
2981
Shawn Guoca2cc332011-06-25 02:04:35 +08002982#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002983static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002984{
2985 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002986 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002987 struct device_node *np = pdev->dev.of_node;
2988
2989 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002990 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002991
Shawn Guoa3caad02012-06-27 03:45:24 +00002992 of_property_read_u32(np, "phy-reset-duration", &msec);
2993 /* A sane reset duration should not be longer than 1s */
2994 if (msec > 1000)
2995 msec = 1;
2996
Shawn Guoca2cc332011-06-25 02:04:35 +08002997 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002998 if (!gpio_is_valid(phy_reset))
2999 return;
3000
Shawn Guo119fc002012-06-27 03:45:22 +00003001 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3002 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08003003 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00003004 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00003005 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08003006 }
Shawn Guoa3caad02012-06-27 03:45:24 +00003007 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08003008 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08003009}
3010#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00003011static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08003012{
3013 /*
3014 * In case of platform probe, the reset has been done
3015 * by machine code.
3016 */
Shawn Guoca2cc332011-06-25 02:04:35 +08003017}
3018#endif /* CONFIG_OF */
3019
Fugang Duan9fc095f2014-09-13 05:00:49 +08003020static void
3021fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3022{
3023 struct device_node *np = pdev->dev.of_node;
3024 int err;
3025
3026 *num_tx = *num_rx = 1;
3027
3028 if (!np || !of_device_is_available(np))
3029 return;
3030
3031 /* parse the num of tx and rx queues */
3032 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
Frank Lib7bd75c2014-09-17 05:18:51 +08003033 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003034 *num_tx = 1;
Frank Lib7bd75c2014-09-17 05:18:51 +08003035
3036 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3037 if (err)
Fugang Duan9fc095f2014-09-13 05:00:49 +08003038 *num_rx = 1;
Fugang Duan9fc095f2014-09-13 05:00:49 +08003039
3040 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003041 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3042 *num_tx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003043 *num_tx = 1;
3044 return;
3045 }
3046
3047 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
Frank Lib7bd75c2014-09-17 05:18:51 +08003048 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3049 *num_rx);
Fugang Duan9fc095f2014-09-13 05:00:49 +08003050 *num_rx = 1;
3051 return;
3052 }
3053
3054}
3055
Bill Pemberton33897cc2012-12-03 09:23:58 -05003056static int
Sascha Haueread73182009-01-28 23:03:11 +00003057fec_probe(struct platform_device *pdev)
3058{
3059 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07003060 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00003061 struct net_device *ndev;
3062 int i, irq, ret = 0;
3063 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08003064 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00003065 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003066 struct device_node *np = pdev->dev.of_node, *phy_node;
Frank Lib7bd75c2014-09-17 05:18:51 +08003067 int num_tx_qs;
3068 int num_rx_qs;
Shawn Guoca2cc332011-06-25 02:04:35 +08003069
3070 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3071 if (of_id)
3072 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00003073
Fugang Duan9fc095f2014-09-13 05:00:49 +08003074 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3075
Sascha Haueread73182009-01-28 23:03:11 +00003076 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08003077 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3078 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00003079 if (!ndev)
3080 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00003081
3082 SET_NETDEV_DEV(ndev, &pdev->dev);
3083
3084 /* setup board info structure */
3085 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003086
Fugang Duan9fc095f2014-09-13 05:00:49 +08003087 fep->num_rx_queues = num_rx_qs;
3088 fep->num_tx_queues = num_tx_qs;
3089
Guenter Roeckd1391932013-06-18 10:04:59 -07003090#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00003091 /* default enable pause frame auto negotiation */
3092 if (pdev->id_entry &&
3093 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3094 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07003095#endif
Frank Libaa70a52013-01-16 16:55:58 +00003096
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003097 /* Select default pin state */
3098 pinctrl_pm_select_default_state(&pdev->dev);
3099
Fabio Estevam399db752013-07-21 13:25:03 -03003100 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05303101 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3102 if (IS_ERR(fep->hwp)) {
3103 ret = PTR_ERR(fep->hwp);
3104 goto failed_ioremap;
3105 }
3106
Bryan Wue6b043d2010-03-31 02:10:44 +00003107 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00003108 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00003109
Frank Liff43da82013-01-03 16:04:23 +00003110 fep->bufdesc_ex = 0;
3111
Sascha Haueread73182009-01-28 23:03:11 +00003112 platform_set_drvdata(pdev, ndev);
3113
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003114 phy_node = of_parse_phandle(np, "phy-handle", 0);
3115 if (!phy_node && of_phy_is_fixed_link(np)) {
3116 ret = of_phy_register_fixed_link(np);
3117 if (ret < 0) {
3118 dev_err(&pdev->dev,
3119 "broken fixed-link specification\n");
3120 goto failed_phy;
3121 }
3122 phy_node = of_node_get(np);
3123 }
3124 fep->phy_node = phy_node;
3125
Guenter Roeck6c5f7802013-04-02 09:35:10 +00003126 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08003127 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09003128 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08003129 if (pdata)
3130 fep->phy_interface = pdata->phy;
3131 else
3132 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3133 } else {
3134 fep->phy_interface = ret;
3135 }
3136
Fabio Estevame2f8d552013-02-22 06:40:45 +00003137 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3138 if (IS_ERR(fep->clk_ipg)) {
3139 ret = PTR_ERR(fep->clk_ipg);
3140 goto failed_clk;
3141 }
3142
3143 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3144 if (IS_ERR(fep->clk_ahb)) {
3145 ret = PTR_ERR(fep->clk_ahb);
3146 goto failed_clk;
3147 }
3148
Fugang Duand851b472014-09-17 05:18:52 +08003149 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3150
Linus Torvalds38f56f32013-05-07 11:06:17 -07003151 /* enet_out is optional, depends on board */
3152 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3153 if (IS_ERR(fep->clk_enet_out))
3154 fep->clk_enet_out = NULL;
3155
Nimrod Andy91c0d982014-08-21 17:09:38 +08003156 fep->ptp_clk_on = false;
3157 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003158
3159 /* clk_ref is optional, depends on board */
3160 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3161 if (IS_ERR(fep->clk_ref))
3162 fep->clk_ref = NULL;
3163
Fabio Estevame2f8d552013-02-22 06:40:45 +00003164 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003165 fep->bufdesc_ex =
3166 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003167 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003168 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003169 fep->bufdesc_ex = 0;
3170 }
3171
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003172 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003173 if (ret)
3174 goto failed_clk;
3175
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003176 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3177 if (!IS_ERR(fep->reg_phy)) {
3178 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003179 if (ret) {
3180 dev_err(&pdev->dev,
3181 "Failed to enable phy regulator: %d\n", ret);
3182 goto failed_regulator;
3183 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003184 } else {
3185 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003186 }
3187
3188 fec_reset_phy(pdev);
3189
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003190 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003191 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003192
3193 ret = fec_enet_init(ndev);
3194 if (ret)
3195 goto failed_init;
3196
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003197 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003198 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003199 if (irq < 0) {
3200 if (i)
3201 break;
3202 ret = irq;
3203 goto failed_irq;
3204 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003205 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003206 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003207 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003208 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003209 }
3210
Fugang Duanb4d39b52014-09-13 05:00:55 +08003211 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003212 ret = fec_enet_mii_init(pdev);
3213 if (ret)
3214 goto failed_mii_init;
3215
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003216 /* Carrier starts down, phylib will bring it up */
3217 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003218 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003219 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003220
Sascha Haueread73182009-01-28 23:03:11 +00003221 ret = register_netdev(ndev);
3222 if (ret)
3223 goto failed_register;
3224
Fabio Estevameb1d0642013-04-13 07:25:36 +00003225 if (fep->bufdesc_ex && fep->ptp_clock)
3226 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3227
Russell King36cdc742014-07-08 13:01:44 +01003228 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003229 return 0;
3230
3231failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003232 fec_enet_mii_remove(fep);
3233failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003234failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003235failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003236 if (fep->reg_phy)
3237 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003238failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003239 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003240failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003241failed_phy:
3242 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003243failed_ioremap:
3244 free_netdev(ndev);
3245
3246 return ret;
3247}
3248
Bill Pemberton33897cc2012-12-03 09:23:58 -05003249static int
Sascha Haueread73182009-01-28 23:03:11 +00003250fec_drv_remove(struct platform_device *pdev)
3251{
3252 struct net_device *ndev = platform_get_drvdata(pdev);
3253 struct fec_enet_private *fep = netdev_priv(ndev);
3254
Nimrod Andy91c0d982014-08-21 17:09:38 +08003255 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003256 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003257 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003258 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003259 if (fep->reg_phy)
3260 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003261 if (fep->ptp_clock)
3262 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003263 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003264 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003265 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003266
Sascha Haueread73182009-01-28 23:03:11 +00003267 return 0;
3268}
3269
Fabio Estevamdd66d382014-07-24 18:24:01 -03003270static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003271{
Eric Benard87cad5c2010-06-18 04:19:54 +00003272 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003273 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003274
Russell Kingda1774e2014-07-08 12:39:57 +01003275 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003276 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003277 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003278 napi_disable(&fep->napi);
3279 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003280 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003281 netif_tx_unlock_bh(ndev);
3282 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003283 }
Russell Kingda1774e2014-07-08 12:39:57 +01003284 rtnl_unlock();
3285
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003286 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003287 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003288
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003289 if (fep->reg_phy)
3290 regulator_disable(fep->reg_phy);
3291
Sascha Haueread73182009-01-28 23:03:11 +00003292 return 0;
3293}
3294
Fabio Estevamdd66d382014-07-24 18:24:01 -03003295static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003296{
Eric Benard87cad5c2010-06-18 04:19:54 +00003297 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003298 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003299 int ret;
3300
3301 if (fep->reg_phy) {
3302 ret = regulator_enable(fep->reg_phy);
3303 if (ret)
3304 return ret;
3305 }
Sascha Haueread73182009-01-28 23:03:11 +00003306
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003307 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003308 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003309 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003310 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003311
Russell Kingda1774e2014-07-08 12:39:57 +01003312 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003313 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003314 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003315 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003316 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003317 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003318 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003319 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003320 }
Russell Kingda1774e2014-07-08 12:39:57 +01003321 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003322
Sascha Haueread73182009-01-28 23:03:11 +00003323 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003324
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003325failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003326 if (fep->reg_phy)
3327 regulator_disable(fep->reg_phy);
3328 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003329}
3330
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003331static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003332
Sascha Haueread73182009-01-28 23:03:11 +00003333static struct platform_driver fec_driver = {
3334 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003335 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003336 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003337 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003338 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003339 },
Shawn Guob5680e02011-01-05 21:13:13 +00003340 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003341 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003342 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003343};
3344
Fabio Estevamaaca2372012-01-23 16:44:50 +00003345module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003347MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348MODULE_LICENSE("GPL");