blob: 1f7ff2268bd08a93a150d76bc652cd83708773f3 [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/init.h>
33#include <linux/delay.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000037#include <linux/in.h>
38#include <linux/ip.h>
39#include <net/ip.h>
40#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>
55#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000056#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Greg Ungerer080853a2007-07-30 16:28:46 +100058#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Christoph Müllner772e42b2013-06-27 21:18:23 +020062static void set_multicast_list(struct net_device *ndev);
63
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010064#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000065#define FEC_ALIGNMENT 0xf
66#else
67#define FEC_ALIGNMENT 0x3
68#endif
69
Shawn Guob5680e02011-01-05 21:13:13 +000070#define DRIVER_NAME "fec"
Frank Lidc975382013-01-28 18:31:42 +000071#define FEC_NAPI_WEIGHT 64
Shawn Guob5680e02011-01-05 21:13:13 +000072
Frank Libaa70a52013-01-16 16:55:58 +000073/* Pause frame feild and FIFO threshold */
74#define FEC_ENET_FCE (1 << 5)
75#define FEC_ENET_RSEM_V 0x84
76#define FEC_ENET_RSFL_V 16
77#define FEC_ENET_RAEM_V 0x8
78#define FEC_ENET_RAFL_V 0x8
79#define FEC_ENET_OPD_V 0xFFF0
80
Shawn Guob5680e02011-01-05 21:13:13 +000081/* Controller is ENET-MAC */
82#define FEC_QUIRK_ENET_MAC (1 << 0)
83/* Controller needs driver to swap frame */
84#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080085/* Controller uses gasket */
86#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000087/* Controller has GBIT support */
88#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000089/* Controller has extend desc buffer */
90#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000091/* Controller has hardware checksum support */
92#define FEC_QUIRK_HAS_CSUM (1 << 5)
Shawn Guob5680e02011-01-05 21:13:13 +000093
94static struct platform_device_id fec_devtype[] = {
95 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080096 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000097 .name = DRIVER_NAME,
98 .driver_data = 0,
99 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800100 .name = "imx25-fec",
101 .driver_data = FEC_QUIRK_USE_GASKET,
102 }, {
103 .name = "imx27-fec",
104 .driver_data = 0,
105 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000106 .name = "imx28-fec",
107 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800108 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000109 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000110 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Shawn Guo48496252013-05-08 21:08:22 +0000111 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM,
Shawn Guo230dec62011-09-23 02:12:48 +0000112 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000113 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000114 .driver_data = FEC_QUIRK_ENET_MAC,
115 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800116 /* sentinel */
117 }
Shawn Guob5680e02011-01-05 21:13:13 +0000118};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800119MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000120
Shawn Guoca2cc332011-06-25 02:04:35 +0800121enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000122 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800123 IMX27_FEC, /* runs on i.mx27/35/51 */
124 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000125 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000126 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800127};
128
129static const struct of_device_id fec_dt_ids[] = {
130 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
131 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
132 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000133 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000134 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800135 { /* sentinel */ }
136};
137MODULE_DEVICE_TABLE(of, fec_dt_ids);
138
Shawn Guo49da97d2011-01-05 21:13:11 +0000139static unsigned char macaddr[ETH_ALEN];
140module_param_array(macaddr, byte, NULL, 0);
141MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
142
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000143#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Some hardware gets it MAC address out of local flash memory.
146 * if this is non-zero then assume it is the address to get MAC from.
147 */
148#if defined(CONFIG_NETtel)
149#define FEC_FLASHMAC 0xf0006006
150#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
151#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#elif defined(CONFIG_CANCam)
153#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000154#elif defined (CONFIG_M5272C3)
155#define FEC_FLASHMAC (0xffe04000 + 4)
156#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000157#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#else
159#define FEC_FLASHMAC 0
160#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800161#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000162
Frank Liff43da82013-01-03 16:04:23 +0000163#if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000164#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000165#endif
166
Sascha Hauer22f6b862009-04-15 01:32:18 +0000167/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
169#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
170#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
171#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
172#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
173#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
174#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
175#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
176#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
177#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
178
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000179#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000180#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* The FEC stores dest/src/type, data, and checksum for receive packets.
183 */
184#define PKT_MAXBUF_SIZE 1518
185#define PKT_MINBUF_SIZE 64
186#define PKT_MAXBLR_SIZE 1520
187
Jim Baxter4c09eed2013-04-19 08:10:49 +0000188/* FEC receive acceleration */
189#define FEC_RACC_IPDIS (1 << 1)
190#define FEC_RACC_PRODIS (1 << 2)
191#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
Matt Waddel6b265292006-06-27 13:10:56 +1000194 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * size bits. Other FEC hardware does not, so we need to take that into
196 * account when setting it.
197 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000198#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100199 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
201#else
202#define OPT_FRAME_SIZE 0
203#endif
204
Bryan Wue6b043d2010-03-31 02:10:44 +0000205/* FEC MII MMFR bits definition */
206#define FEC_MMFR_ST (1 << 30)
207#define FEC_MMFR_OP_READ (2 << 28)
208#define FEC_MMFR_OP_WRITE (1 << 28)
209#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
210#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
211#define FEC_MMFR_TA (2 << 16)
212#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500214#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Sascha Hauer22f6b862009-04-15 01:32:18 +0000216/* Transmitter timeout */
217#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Frank Libaa70a52013-01-16 16:55:58 +0000219#define FEC_PAUSE_FLAG_AUTONEG 0x1
220#define FEC_PAUSE_FLAG_ENABLE 0x2
221
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000222static int mii_cnt;
223
Frank Liff43da82013-01-03 16:04:23 +0000224static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex)
225{
226 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
227 if (is_ex)
228 return (struct bufdesc *)(ex + 1);
229 else
230 return bdp + 1;
231}
232
233static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex)
234{
235 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
236 if (is_ex)
237 return (struct bufdesc *)(ex - 1);
238 else
239 return bdp - 1;
240}
241
Shawn Guob5680e02011-01-05 21:13:13 +0000242static void *swap_buffer(void *bufaddr, int len)
243{
244 int i;
245 unsigned int *buf = bufaddr;
246
Fabio Estevamffed61e2013-05-21 05:44:26 +0000247 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000248 *buf = cpu_to_be32(*buf);
249
250 return bufaddr;
251}
252
Jim Baxter4c09eed2013-04-19 08:10:49 +0000253static int
254fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
255{
256 /* Only run for packets requiring a checksum. */
257 if (skb->ip_summed != CHECKSUM_PARTIAL)
258 return 0;
259
260 if (unlikely(skb_cow_head(skb, 0)))
261 return -1;
262
263 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
264
265 return 0;
266}
267
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000268static netdev_tx_t
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100269fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100271 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000272 const struct platform_device_id *id_entry =
273 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000274 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000275 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000276 unsigned short status;
Frank Lide5fb0a2013-03-03 17:34:25 +0000277 unsigned int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!fep->link) {
Jim Baxter4c09eed2013-04-19 08:10:49 +0000280 /* Link is down or auto-negotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000281 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 /* Fill in a Tx ring entry */
285 bdp = fep->cur_tx;
286
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000287 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000288
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000289 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* Ooops. All transmit buffers are full. Bail out.
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100291 * This should not happen, since ndev->tbusy should be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Joe Perches31b77202013-04-13 19:03:17 +0000293 netdev_err(ndev, "tx queue full!\n");
Patrick McHardy5b548142009-06-12 06:22:29 +0000294 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jim Baxter4c09eed2013-04-19 08:10:49 +0000297 /* Protocol checksum off-load for TCP and UDP. */
298 if (fec_enet_clear_csum(skb, ndev)) {
299 kfree_skb(skb);
300 return NETDEV_TX_OK;
301 }
302
Sascha Hauer22f6b862009-04-15 01:32:18 +0000303 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000304 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Sascha Hauer22f6b862009-04-15 01:32:18 +0000306 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000307 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 bdp->cbd_datlen = skb->len;
309
310 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000311 * On some FEC implementations data must be aligned on
312 * 4-byte boundaries. Use bounce buffers to copy data
313 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000315 if (fep->bufdesc_ex)
316 index = (struct bufdesc_ex *)bdp -
317 (struct bufdesc_ex *)fep->tx_bd_base;
318 else
319 index = bdp - fep->tx_bd_base;
320
Greg Ungerer9555b312009-08-06 17:58:18 +0000321 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Uwe Kleine-König8a73b0b2011-01-13 21:34:31 +0100322 memcpy(fep->tx_bounce[index], skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000323 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Shawn Guob5680e02011-01-05 21:13:13 +0000326 /*
327 * Some design made an incorrect assumption on endian mode of
328 * the system that it's running on. As the result, driver has to
329 * swap every frame going to and coming from the controller.
330 */
331 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
332 swap_buffer(bufaddr, skb->len);
333
Sascha Hauer22f6b862009-04-15 01:32:18 +0000334 /* Save skb pointer */
Frank Lide5fb0a2013-03-03 17:34:25 +0000335 fep->tx_skbuff[index] = skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Push the data cache so the CPM does not get stale memory
338 * data.
339 */
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100340 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000341 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000343 /* Send it on its way. Tell FEC it's ready, interrupt when done,
344 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000346 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000348 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Frank Liff43da82013-01-03 16:04:23 +0000350 if (fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000351
Frank Liff43da82013-01-03 16:04:23 +0000352 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
353 ebdp->cbd_bdu = 0;
354 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
355 fep->hwts_tx_en)) {
356 ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
357 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
358 } else {
Frank Liff43da82013-01-03 16:04:23 +0000359 ebdp->cbd_esc = BD_ENET_TX_INT;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000360
361 /* Enable protocol checksum flags
362 * We do not bother with the IP Checksum bits as they
363 * are done by the kernel
364 */
365 if (skb->ip_summed == CHECKSUM_PARTIAL)
366 ebdp->cbd_esc |= BD_ENET_TX_PINS;
Frank Liff43da82013-01-03 16:04:23 +0000367 }
Frank Li6605b732012-10-30 18:25:31 +0000368 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000369 /* If this was the last BD in the ring, start at the beginning again. */
370 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000372 else
Frank Liff43da82013-01-03 16:04:23 +0000373 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Sascha Hauer2e285322009-04-15 01:32:16 +0000375 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Frank Lide5fb0a2013-03-03 17:34:25 +0000377 if (fep->cur_tx == fep->dirty_tx)
378 netif_stop_queue(ndev);
Richard Cochran18a03b92011-06-12 02:18:59 +0000379
Frank Lide5fb0a2013-03-03 17:34:25 +0000380 /* Trigger transmission start */
381 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
382
383 skb_tx_timestamp(skb);
Richard Cochrana0087a32011-06-19 03:31:40 +0000384
Patrick McHardy6ed10652009-06-23 06:03:08 +0000385 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Frank Li14109a52013-03-26 16:12:03 +0000388/* Init RX & TX buffer descriptors
389 */
390static void fec_enet_bd_init(struct net_device *dev)
391{
392 struct fec_enet_private *fep = netdev_priv(dev);
393 struct bufdesc *bdp;
394 unsigned int i;
395
396 /* Initialize the receive buffer descriptors. */
397 bdp = fep->rx_bd_base;
398 for (i = 0; i < RX_RING_SIZE; i++) {
399
400 /* Initialize the BD for every fragment in the page. */
401 if (bdp->cbd_bufaddr)
402 bdp->cbd_sc = BD_ENET_RX_EMPTY;
403 else
404 bdp->cbd_sc = 0;
405 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
406 }
407
408 /* Set the last buffer to wrap */
409 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
410 bdp->cbd_sc |= BD_SC_WRAP;
411
412 fep->cur_rx = fep->rx_bd_base;
413
414 /* ...and the same for transmit */
415 bdp = fep->tx_bd_base;
416 fep->cur_tx = bdp;
417 for (i = 0; i < TX_RING_SIZE; i++) {
418
419 /* Initialize the BD for every fragment in the page. */
420 bdp->cbd_sc = 0;
421 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
422 dev_kfree_skb_any(fep->tx_skbuff[i]);
423 fep->tx_skbuff[i] = NULL;
424 }
425 bdp->cbd_bufaddr = 0;
426 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
427 }
428
429 /* Set the last buffer to wrap */
430 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
431 bdp->cbd_sc |= BD_SC_WRAP;
432 fep->dirty_tx = bdp;
433}
434
Uwe Kleine-König45993652011-01-19 20:47:04 +0100435/* This function is called to start or restart the FEC during a link
436 * change. This only happens when switching between half and full
437 * duplex.
438 */
439static void
440fec_restart(struct net_device *ndev, int duplex)
441{
442 struct fec_enet_private *fep = netdev_priv(ndev);
443 const struct platform_device_id *id_entry =
444 platform_get_device_id(fep->pdev);
445 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000446 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100447 u32 temp_mac[2];
448 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000449 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100450
Frank Li54309fa2013-05-07 14:08:44 +0000451 if (netif_running(ndev)) {
452 netif_device_detach(ndev);
453 napi_disable(&fep->napi);
454 netif_stop_queue(ndev);
Fabio Estevam31691342013-05-15 07:06:26 +0000455 netif_tx_lock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000456 }
457
Uwe Kleine-König45993652011-01-19 20:47:04 +0100458 /* Whack a reset. We should wait for this. */
459 writel(1, fep->hwp + FEC_ECNTRL);
460 udelay(10);
461
462 /*
463 * enet-mac reset will reset mac address registers too,
464 * so need to reconfigure it.
465 */
466 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
467 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
468 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
469 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
470 }
471
472 /* Clear any outstanding interrupt. */
473 writel(0xffc00000, fep->hwp + FEC_IEVENT);
474
Christoph Müllner772e42b2013-06-27 21:18:23 +0200475 /* Setup multicast filter. */
476 set_multicast_list(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100477#ifndef CONFIG_M5272
478 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
479 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
480#endif
481
482 /* Set maximum receive buffer size. */
483 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
484
Frank Li14109a52013-03-26 16:12:03 +0000485 fec_enet_bd_init(ndev);
486
Uwe Kleine-König45993652011-01-19 20:47:04 +0100487 /* Set receive and transmit descriptor base. */
488 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000489 if (fep->bufdesc_ex)
490 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
491 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
492 else
493 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
494 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100495
Uwe Kleine-König45993652011-01-19 20:47:04 +0100496
Uwe Kleine-König45993652011-01-19 20:47:04 +0100497 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
498 if (fep->tx_skbuff[i]) {
499 dev_kfree_skb_any(fep->tx_skbuff[i]);
500 fep->tx_skbuff[i] = NULL;
501 }
502 }
503
504 /* Enable MII mode */
505 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100506 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100507 writel(0x04, fep->hwp + FEC_X_CNTRL);
508 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100509 /* No Rcv on Xmit */
510 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100511 writel(0x0, fep->hwp + FEC_X_CNTRL);
512 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100513
Uwe Kleine-König45993652011-01-19 20:47:04 +0100514 fep->full_duplex = duplex;
515
516 /* Set MII speed */
517 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
518
Guenter Roeckd1391932013-06-18 10:04:59 -0700519#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000520 /* set RX checksum */
521 val = readl(fep->hwp + FEC_RACC);
522 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
523 val |= FEC_RACC_OPTIONS;
524 else
525 val &= ~FEC_RACC_OPTIONS;
526 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700527#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000528
Uwe Kleine-König45993652011-01-19 20:47:04 +0100529 /*
530 * The phy interface and speed need to get configured
531 * differently on enet-mac.
532 */
533 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100534 /* Enable flow control and length check */
535 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100536
Shawn Guo230dec62011-09-23 02:12:48 +0000537 /* RGMII, RMII or MII */
538 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
539 rcntl |= (1 << 6);
540 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100541 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100542 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100543 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100544
Shawn Guo230dec62011-09-23 02:12:48 +0000545 /* 1G, 100M or 10M */
546 if (fep->phy_dev) {
547 if (fep->phy_dev->speed == SPEED_1000)
548 ecntl |= (1 << 5);
549 else if (fep->phy_dev->speed == SPEED_100)
550 rcntl &= ~(1 << 9);
551 else
552 rcntl |= (1 << 9);
553 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100554 } else {
555#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800556 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000557 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100558 /* disable the gasket and wait */
559 writel(0, fep->hwp + FEC_MIIGSK_ENR);
560 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
561 udelay(1);
562
563 /*
564 * configure the gasket:
565 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800566 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100567 */
Eric Benard8d82f212012-01-12 06:10:28 +0000568 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
569 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
570 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
571 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
572 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100573
574 /* re-enable the gasket */
575 writel(2, fep->hwp + FEC_MIIGSK_ENR);
576 }
577#endif
578 }
Frank Libaa70a52013-01-16 16:55:58 +0000579
Guenter Roeckd1391932013-06-18 10:04:59 -0700580#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000581 /* enable pause frame*/
582 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
583 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
584 fep->phy_dev && fep->phy_dev->pause)) {
585 rcntl |= FEC_ENET_FCE;
586
Jim Baxter4c09eed2013-04-19 08:10:49 +0000587 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000588 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
589 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
590 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
591 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
592
593 /* OPD */
594 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
595 } else {
596 rcntl &= ~FEC_ENET_FCE;
597 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700598#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000599
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100600 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100601
Shawn Guo230dec62011-09-23 02:12:48 +0000602 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
603 /* enable ENET endian swap */
604 ecntl |= (1 << 8);
605 /* enable ENET store and forward mode */
606 writel(1 << 8, fep->hwp + FEC_X_WMRK);
607 }
608
Frank Liff43da82013-01-03 16:04:23 +0000609 if (fep->bufdesc_ex)
610 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000611
Chris Healy38ae92d2013-06-25 23:18:52 -0700612#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +0100613 /* Enable the MIB statistic event counters */
614 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -0700615#endif
616
Uwe Kleine-König45993652011-01-19 20:47:04 +0100617 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000618 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100619 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
620
Frank Liff43da82013-01-03 16:04:23 +0000621 if (fep->bufdesc_ex)
622 fec_ptp_start_cyclecounter(ndev);
623
Uwe Kleine-König45993652011-01-19 20:47:04 +0100624 /* Enable interrupts we wish to service */
625 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Frank Li54309fa2013-05-07 14:08:44 +0000626
627 if (netif_running(ndev)) {
Fabio Estevam31691342013-05-15 07:06:26 +0000628 netif_tx_unlock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000629 netif_wake_queue(ndev);
Fabio Estevam1ed0d562013-05-15 07:06:27 +0000630 napi_enable(&fep->napi);
631 netif_device_attach(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000632 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100633}
634
635static void
636fec_stop(struct net_device *ndev)
637{
638 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000639 const struct platform_device_id *id_entry =
640 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000641 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100642
643 /* We cannot expect a graceful transmit stop without link !!! */
644 if (fep->link) {
645 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
646 udelay(10);
647 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000648 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +0100649 }
650
651 /* Whack a reset. We should wait for this. */
652 writel(1, fep->hwp + FEC_ECNTRL);
653 udelay(10);
654 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
655 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000656
657 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000658 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000659 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000660 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
661 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100662}
663
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100666fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100668 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100670 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Frank Li54309fa2013-05-07 14:08:44 +0000672 fep->delay_work.timeout = true;
673 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
674}
675
676static void fec_enet_work(struct work_struct *work)
677{
678 struct fec_enet_private *fep =
679 container_of(work,
680 struct fec_enet_private,
681 delay_work.delay_work.work);
682
683 if (fep->delay_work.timeout) {
684 fep->delay_work.timeout = false;
685 fec_restart(fep->netdev, fep->full_duplex);
686 netif_wake_queue(fep->netdev);
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100691fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000694 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000695 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +0000697 int index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100699 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 bdp = fep->dirty_tx;
701
Frank Lide5fb0a2013-03-03 17:34:25 +0000702 /* get next bdp of dirty_tx */
703 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
704 bdp = fep->tx_bd_base;
705 else
706 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
707
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000708 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +0000709
710 /* current queue is empty */
711 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000712 break;
713
Frank Lide5fb0a2013-03-03 17:34:25 +0000714 if (fep->bufdesc_ex)
715 index = (struct bufdesc_ex *)bdp -
716 (struct bufdesc_ex *)fep->tx_bd_base;
717 else
718 index = bdp - fep->tx_bd_base;
719
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100720 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
721 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000722 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Frank Lide5fb0a2013-03-03 17:34:25 +0000724 skb = fep->tx_skbuff[index];
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000727 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 BD_ENET_TX_RL | BD_ENET_TX_UN |
729 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100730 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000731 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100732 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000733 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100734 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000735 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100736 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000737 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100738 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000739 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100740 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100742 ndev->stats.tx_packets++;
Jim Baxter06efce72013-06-27 19:25:08 +0100743 ndev->stats.tx_bytes += bdp->cbd_datlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Frank Liff43da82013-01-03 16:04:23 +0000746 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
747 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000748 struct skb_shared_hwtstamps shhwtstamps;
749 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000750 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000751
752 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
753 spin_lock_irqsave(&fep->tmreg_lock, flags);
754 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000755 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000756 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
757 skb_tstamp_tx(skb, &shhwtstamps);
758 }
Frank Liff43da82013-01-03 16:04:23 +0000759
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000760 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +0000761 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* Deferred means some collisions occurred during transmit,
764 * but we eventually sent the packet OK.
765 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000766 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100767 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400768
Sascha Hauer22f6b862009-04-15 01:32:18 +0000769 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +0000771 fep->tx_skbuff[index] = NULL;
772
773 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400774
Sascha Hauer22f6b862009-04-15 01:32:18 +0000775 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000776 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 bdp = fep->tx_bd_base;
778 else
Frank Liff43da82013-01-03 16:04:23 +0000779 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400780
Sascha Hauer22f6b862009-04-15 01:32:18 +0000781 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000783 if (fep->dirty_tx != fep->cur_tx) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100784 if (netif_queue_stopped(ndev))
785 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 }
Frank Lide5fb0a2013-03-03 17:34:25 +0000788 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791
792/* During a receive, the cur_rx points to the current incoming buffer.
793 * When we update through the ring, if the next incoming buffer has
794 * not been given to the system, we just set the empty indicator,
795 * effectively tossing the packet.
796 */
Frank Lidc975382013-01-28 18:31:42 +0000797static int
798fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100800 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000801 const struct platform_device_id *id_entry =
802 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000803 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000804 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct sk_buff *skb;
806 ushort pkt_len;
807 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +0000808 int pkt_received = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400809
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000810#ifdef CONFIG_M532x
811 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400812#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* First, grab all of the stats for the incoming packet.
815 * These get messed up if we get called due to a busy condition.
816 */
817 bdp = fep->cur_rx;
818
Sascha Hauer22f6b862009-04-15 01:32:18 +0000819 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Frank Lidc975382013-01-28 18:31:42 +0000821 if (pkt_received >= budget)
822 break;
823 pkt_received++;
824
Sascha Hauer22f6b862009-04-15 01:32:18 +0000825 /* Since we have allocated space to hold a complete frame,
826 * the last indicator should be set.
827 */
828 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +0000829 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Sascha Hauer22f6b862009-04-15 01:32:18 +0000831 if (!fep->opened)
832 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Sascha Hauer22f6b862009-04-15 01:32:18 +0000834 /* Check for errors. */
835 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100837 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000838 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
839 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100840 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000841 }
842 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100843 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000844 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100845 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000846 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100847 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000849
850 /* Report late collisions as a frame error.
851 * On this error, the BD is closed, but we don't know what we
852 * have in the buffer. So, just drop this frame on the floor.
853 */
854 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100855 ndev->stats.rx_errors++;
856 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000857 goto rx_processing_done;
858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Sascha Hauer22f6b862009-04-15 01:32:18 +0000860 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100861 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000862 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100863 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000864 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100866 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
867 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000868
Shawn Guob5680e02011-01-05 21:13:13 +0000869 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
870 swap_buffer(data, pkt_len);
871
Sascha Hauer22f6b862009-04-15 01:32:18 +0000872 /* This does 16 byte alignment, exactly what we need.
873 * The packet length includes FCS, but we don't want to
874 * include that when passing upstream as it messes up
875 * bridging applications.
876 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000877 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Sascha Hauer85498892009-04-15 01:32:21 +0000879 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100880 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000881 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000882 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000883 skb_put(skb, pkt_len - 4); /* Make room */
884 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100885 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +0000886
Frank Li6605b732012-10-30 18:25:31 +0000887 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +0000888 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000889 struct skb_shared_hwtstamps *shhwtstamps =
890 skb_hwtstamps(skb);
891 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000892 struct bufdesc_ex *ebdp =
893 (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000894
895 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
896
897 spin_lock_irqsave(&fep->tmreg_lock, flags);
898 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000899 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000900 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
901 }
Frank Liff43da82013-01-03 16:04:23 +0000902
Jim Baxter4c09eed2013-04-19 08:10:49 +0000903 if (fep->bufdesc_ex &&
904 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
905 struct bufdesc_ex *ebdp =
906 (struct bufdesc_ex *)bdp;
907 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
908 /* don't check it */
909 skb->ip_summed = CHECKSUM_UNNECESSARY;
910 } else {
911 skb_checksum_none_assert(skb);
912 }
913 }
914
Richard Cochran18a03b92011-06-12 02:18:59 +0000915 if (!skb_defer_rx_timestamp(skb))
Frank Lidc975382013-01-28 18:31:42 +0000916 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000917 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000918
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100919 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
920 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000921rx_processing_done:
922 /* Clear the status flags for this buffer */
923 status &= ~BD_ENET_RX_STATS;
924
925 /* Mark the buffer empty */
926 status |= BD_ENET_RX_EMPTY;
927 bdp->cbd_sc = status;
928
Frank Liff43da82013-01-03 16:04:23 +0000929 if (fep->bufdesc_ex) {
930 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
931
932 ebdp->cbd_esc = BD_ENET_RX_INT;
933 ebdp->cbd_prot = 0;
934 ebdp->cbd_bdu = 0;
935 }
Frank Li6605b732012-10-30 18:25:31 +0000936
Sascha Hauer22f6b862009-04-15 01:32:18 +0000937 /* Update BD pointer to next entry */
938 if (status & BD_ENET_RX_WRAP)
939 bdp = fep->rx_bd_base;
940 else
Frank Liff43da82013-01-03 16:04:23 +0000941 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000942 /* Doing this here will keep the FEC running while we process
943 * incoming frames. On a heavily loaded network, we should be
944 * able to keep up at the expense of system resources.
945 */
946 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000948 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Frank Lidc975382013-01-28 18:31:42 +0000950 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Uwe Kleine-König45993652011-01-19 20:47:04 +0100953static irqreturn_t
954fec_enet_interrupt(int irq, void *dev_id)
955{
956 struct net_device *ndev = dev_id;
957 struct fec_enet_private *fep = netdev_priv(ndev);
958 uint int_events;
959 irqreturn_t ret = IRQ_NONE;
960
961 do {
962 int_events = readl(fep->hwp + FEC_IEVENT);
963 writel(int_events, fep->hwp + FEC_IEVENT);
964
Frank Lide5fb0a2013-03-03 17:34:25 +0000965 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100966 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +0000967
968 /* Disable the RX interrupt */
969 if (napi_schedule_prep(&fep->napi)) {
970 writel(FEC_RX_DISABLED_IMASK,
971 fep->hwp + FEC_IMASK);
972 __napi_schedule(&fep->napi);
973 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100974 }
975
Uwe Kleine-König45993652011-01-19 20:47:04 +0100976 if (int_events & FEC_ENET_MII) {
977 ret = IRQ_HANDLED;
978 complete(&fep->mdio_done);
979 }
980 } while (int_events);
981
982 return ret;
983}
984
Frank Lidc975382013-01-28 18:31:42 +0000985static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
986{
987 struct net_device *ndev = napi->dev;
988 int pkts = fec_enet_rx(ndev, budget);
989 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100990
Frank Lide5fb0a2013-03-03 17:34:25 +0000991 fec_enet_tx(ndev);
992
Frank Lidc975382013-01-28 18:31:42 +0000993 if (pkts < budget) {
994 napi_complete(napi);
995 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
996 }
997 return pkts;
998}
Uwe Kleine-König45993652011-01-19 20:47:04 +0100999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001001static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001003 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +00001004 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001005 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Shawn Guo49da97d2011-01-05 21:13:11 +00001007 /*
1008 * try to get mac address in following order:
1009 *
1010 * 1) module parameter via kernel command line in form
1011 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1012 */
1013 iap = macaddr;
1014
1015 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001016 * 2) from device tree data
1017 */
1018 if (!is_valid_ether_addr(iap)) {
1019 struct device_node *np = fep->pdev->dev.of_node;
1020 if (np) {
1021 const char *mac = of_get_mac_address(np);
1022 if (mac)
1023 iap = (unsigned char *) mac;
1024 }
1025 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001026
1027 /*
1028 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001029 */
1030 if (!is_valid_ether_addr(iap)) {
1031#ifdef CONFIG_M5272
1032 if (FEC_FLASHMAC)
1033 iap = (unsigned char *)FEC_FLASHMAC;
1034#else
1035 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001036 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001037#endif
1038 }
1039
1040 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001041 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001042 */
1043 if (!is_valid_ether_addr(iap)) {
1044 *((unsigned long *) &tmpaddr[0]) =
1045 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
1046 *((unsigned short *) &tmpaddr[4]) =
1047 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 iap = &tmpaddr[0];
1049 }
1050
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001051 /*
1052 * 5) random mac address
1053 */
1054 if (!is_valid_ether_addr(iap)) {
1055 /* Report it and use a random ethernet address instead */
1056 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1057 eth_hw_addr_random(ndev);
1058 netdev_info(ndev, "Using random MAC address: %pM\n",
1059 ndev->dev_addr);
1060 return;
1061 }
1062
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001063 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Shawn Guo49da97d2011-01-05 21:13:11 +00001065 /* Adjust MAC if using macaddr */
1066 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001067 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070/* ------------------------------------------------------------------------- */
1071
Bryan Wue6b043d2010-03-31 02:10:44 +00001072/*
1073 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001075static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001077 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001078 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001079 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Bryan Wue6b043d2010-03-31 02:10:44 +00001081 /* Prevent a state halted on mii error */
1082 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1083 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001084 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001086
Bryan Wue6b043d2010-03-31 02:10:44 +00001087 if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001088 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001089 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001090 status_change = 1;
1091 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001092
Lucas Stachd97e7492013-03-14 05:12:01 +00001093 if (fep->full_duplex != phy_dev->duplex)
1094 status_change = 1;
1095
1096 if (phy_dev->speed != fep->speed) {
1097 fep->speed = phy_dev->speed;
1098 status_change = 1;
1099 }
1100
1101 /* if any of the above changed restart the FEC */
1102 if (status_change)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001103 fec_restart(ndev, phy_dev->duplex);
Lucas Stachd97e7492013-03-14 05:12:01 +00001104 } else {
1105 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001106 fec_stop(ndev);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001107 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001108 status_change = 1;
1109 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001110 }
1111
Bryan Wue6b043d2010-03-31 02:10:44 +00001112 if (status_change)
1113 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Bryan Wue6b043d2010-03-31 02:10:44 +00001116static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Bryan Wue6b043d2010-03-31 02:10:44 +00001118 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001119 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001120
1121 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001122 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001123
1124 /* start a read op */
1125 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1126 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1127 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1128
1129 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001130 time_left = wait_for_completion_timeout(&fep->mdio_done,
1131 usecs_to_jiffies(FEC_MII_TIMEOUT));
1132 if (time_left == 0) {
1133 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001134 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001135 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001136 }
1137
1138 /* return value */
1139 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1140}
1141
1142static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1143 u16 value)
1144{
1145 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001146 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001147
1148 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001149 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001150
Shawn Guo862f0982011-01-05 21:13:09 +00001151 /* start a write op */
1152 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001153 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1154 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1155 fep->hwp + FEC_MII_DATA);
1156
1157 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001158 time_left = wait_for_completion_timeout(&fep->mdio_done,
1159 usecs_to_jiffies(FEC_MII_TIMEOUT));
1160 if (time_left == 0) {
1161 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001162 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001163 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001164 }
1165
1166 return 0;
1167}
1168
1169static int fec_enet_mdio_reset(struct mii_bus *bus)
1170{
1171 return 0;
1172}
1173
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001174static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001175{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001176 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001177 const struct platform_device_id *id_entry =
1178 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001179 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001180 char mdio_bus_id[MII_BUS_ID_SIZE];
1181 char phy_name[MII_BUS_ID_SIZE + 3];
1182 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001183 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001184
Bryan Wu418bd0d2010-05-28 03:40:39 -07001185 fep->phy_dev = NULL;
1186
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001187 /* check for attached phy */
1188 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1189 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1190 continue;
1191 if (fep->mii_bus->phy_map[phy_id] == NULL)
1192 continue;
1193 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1194 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001195 if (dev_id--)
1196 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001197 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1198 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001199 }
1200
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001201 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001202 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001203 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001204 phy_id = 0;
1205 }
1206
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001207 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001208 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001209 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001210 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001211 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001212 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001213 }
1214
1215 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001216 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001217 phy_dev->supported &= PHY_GBIT_FEATURES;
Guenter Roeckd1391932013-06-18 10:04:59 -07001218#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001219 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001220#endif
Frank Libaa70a52013-01-16 16:55:58 +00001221 }
Shawn Guo230dec62011-09-23 02:12:48 +00001222 else
1223 phy_dev->supported &= PHY_BASIC_FEATURES;
1224
Bryan Wue6b043d2010-03-31 02:10:44 +00001225 phy_dev->advertising = phy_dev->supported;
1226
1227 fep->phy_dev = phy_dev;
1228 fep->link = 0;
1229 fep->full_duplex = 0;
1230
Joe Perches31b77202013-04-13 19:03:17 +00001231 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1232 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1233 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001234
Bryan Wue6b043d2010-03-31 02:10:44 +00001235 return 0;
1236}
1237
1238static int fec_enet_mii_init(struct platform_device *pdev)
1239{
Shawn Guob5680e02011-01-05 21:13:13 +00001240 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 struct net_device *ndev = platform_get_drvdata(pdev);
1242 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001243 const struct platform_device_id *id_entry =
1244 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001245 int err = -ENXIO, i;
1246
Shawn Guob5680e02011-01-05 21:13:13 +00001247 /*
1248 * The dual fec interfaces are not equivalent with enet-mac.
1249 * Here are the differences:
1250 *
1251 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1252 * - fec0 acts as the 1588 time master while fec1 is slave
1253 * - external phys can only be configured by fec0
1254 *
1255 * That is to say fec1 can not work independently. It only works
1256 * when fec0 is working. The reason behind this design is that the
1257 * second interface is added primarily for Switch mode.
1258 *
1259 * Because of the last point above, both phys are attached on fec0
1260 * mdio interface in board design, and need to be configured by
1261 * fec0 mii_bus.
1262 */
Shawn Guo43af9402011-12-05 05:01:15 +00001263 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001264 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001265 if (mii_cnt && fec0_mii_bus) {
1266 fep->mii_bus = fec0_mii_bus;
1267 mii_cnt++;
1268 return 0;
1269 }
1270 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001271 }
1272
Bryan Wue6b043d2010-03-31 02:10:44 +00001273 fep->mii_timeout = 0;
1274
1275 /*
1276 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001277 *
1278 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1279 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1280 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1281 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001282 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001283 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001284 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1285 fep->phy_speed--;
1286 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001287 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1288
1289 fep->mii_bus = mdiobus_alloc();
1290 if (fep->mii_bus == NULL) {
1291 err = -ENOMEM;
1292 goto err_out;
1293 }
1294
1295 fep->mii_bus->name = "fec_enet_mii_bus";
1296 fep->mii_bus->read = fec_enet_mdio_read;
1297 fep->mii_bus->write = fec_enet_mdio_write;
1298 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001299 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1300 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001301 fep->mii_bus->priv = fep;
1302 fep->mii_bus->parent = &pdev->dev;
1303
1304 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1305 if (!fep->mii_bus->irq) {
1306 err = -ENOMEM;
1307 goto err_out_free_mdiobus;
1308 }
1309
1310 for (i = 0; i < PHY_MAX_ADDR; i++)
1311 fep->mii_bus->irq[i] = PHY_POLL;
1312
Bryan Wue6b043d2010-03-31 02:10:44 +00001313 if (mdiobus_register(fep->mii_bus))
1314 goto err_out_free_mdio_irq;
1315
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001316 mii_cnt++;
1317
Shawn Guob5680e02011-01-05 21:13:13 +00001318 /* save fec0 mii_bus */
1319 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1320 fec0_mii_bus = fep->mii_bus;
1321
Bryan Wue6b043d2010-03-31 02:10:44 +00001322 return 0;
1323
Bryan Wue6b043d2010-03-31 02:10:44 +00001324err_out_free_mdio_irq:
1325 kfree(fep->mii_bus->irq);
1326err_out_free_mdiobus:
1327 mdiobus_free(fep->mii_bus);
1328err_out:
1329 return err;
1330}
1331
1332static void fec_enet_mii_remove(struct fec_enet_private *fep)
1333{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001334 if (--mii_cnt == 0) {
1335 mdiobus_unregister(fep->mii_bus);
1336 kfree(fep->mii_bus->irq);
1337 mdiobus_free(fep->mii_bus);
1338 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001339}
1340
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001341static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001342 struct ethtool_cmd *cmd)
1343{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001344 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001345 struct phy_device *phydev = fep->phy_dev;
1346
1347 if (!phydev)
1348 return -ENODEV;
1349
1350 return phy_ethtool_gset(phydev, cmd);
1351}
1352
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001353static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001354 struct ethtool_cmd *cmd)
1355{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001356 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001357 struct phy_device *phydev = fep->phy_dev;
1358
1359 if (!phydev)
1360 return -ENODEV;
1361
1362 return phy_ethtool_sset(phydev, cmd);
1363}
1364
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001365static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001366 struct ethtool_drvinfo *info)
1367{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001368 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Jiri Pirko7826d432013-01-06 00:44:26 +00001370 strlcpy(info->driver, fep->pdev->dev.driver->name,
1371 sizeof(info->driver));
1372 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1373 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
Bryan Wue6b043d2010-03-31 02:10:44 +00001375
Frank Li5ebae4892013-01-06 16:25:07 +00001376static int fec_enet_get_ts_info(struct net_device *ndev,
1377 struct ethtool_ts_info *info)
1378{
1379 struct fec_enet_private *fep = netdev_priv(ndev);
1380
1381 if (fep->bufdesc_ex) {
1382
1383 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1384 SOF_TIMESTAMPING_RX_SOFTWARE |
1385 SOF_TIMESTAMPING_SOFTWARE |
1386 SOF_TIMESTAMPING_TX_HARDWARE |
1387 SOF_TIMESTAMPING_RX_HARDWARE |
1388 SOF_TIMESTAMPING_RAW_HARDWARE;
1389 if (fep->ptp_clock)
1390 info->phc_index = ptp_clock_index(fep->ptp_clock);
1391 else
1392 info->phc_index = -1;
1393
1394 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1395 (1 << HWTSTAMP_TX_ON);
1396
1397 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1398 (1 << HWTSTAMP_FILTER_ALL);
1399 return 0;
1400 } else {
1401 return ethtool_op_get_ts_info(ndev, info);
1402 }
1403}
1404
Guenter Roeckd1391932013-06-18 10:04:59 -07001405#if !defined(CONFIG_M5272)
1406
Frank Libaa70a52013-01-16 16:55:58 +00001407static void fec_enet_get_pauseparam(struct net_device *ndev,
1408 struct ethtool_pauseparam *pause)
1409{
1410 struct fec_enet_private *fep = netdev_priv(ndev);
1411
1412 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1413 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1414 pause->rx_pause = pause->tx_pause;
1415}
1416
1417static int fec_enet_set_pauseparam(struct net_device *ndev,
1418 struct ethtool_pauseparam *pause)
1419{
1420 struct fec_enet_private *fep = netdev_priv(ndev);
1421
1422 if (pause->tx_pause != pause->rx_pause) {
1423 netdev_info(ndev,
1424 "hardware only support enable/disable both tx and rx");
1425 return -EINVAL;
1426 }
1427
1428 fep->pause_flag = 0;
1429
1430 /* tx pause must be same as rx pause */
1431 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1432 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1433
1434 if (pause->rx_pause || pause->autoneg) {
1435 fep->phy_dev->supported |= ADVERTISED_Pause;
1436 fep->phy_dev->advertising |= ADVERTISED_Pause;
1437 } else {
1438 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1439 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1440 }
1441
1442 if (pause->autoneg) {
1443 if (netif_running(ndev))
1444 fec_stop(ndev);
1445 phy_start_aneg(fep->phy_dev);
1446 }
1447 if (netif_running(ndev))
1448 fec_restart(ndev, 0);
1449
1450 return 0;
1451}
1452
Chris Healy38ae92d2013-06-25 23:18:52 -07001453static const struct fec_stat {
1454 char name[ETH_GSTRING_LEN];
1455 u16 offset;
1456} fec_stats[] = {
1457 /* RMON TX */
1458 { "tx_dropped", RMON_T_DROP },
1459 { "tx_packets", RMON_T_PACKETS },
1460 { "tx_broadcast", RMON_T_BC_PKT },
1461 { "tx_multicast", RMON_T_MC_PKT },
1462 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1463 { "tx_undersize", RMON_T_UNDERSIZE },
1464 { "tx_oversize", RMON_T_OVERSIZE },
1465 { "tx_fragment", RMON_T_FRAG },
1466 { "tx_jabber", RMON_T_JAB },
1467 { "tx_collision", RMON_T_COL },
1468 { "tx_64byte", RMON_T_P64 },
1469 { "tx_65to127byte", RMON_T_P65TO127 },
1470 { "tx_128to255byte", RMON_T_P128TO255 },
1471 { "tx_256to511byte", RMON_T_P256TO511 },
1472 { "tx_512to1023byte", RMON_T_P512TO1023 },
1473 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1474 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1475 { "tx_octets", RMON_T_OCTETS },
1476
1477 /* IEEE TX */
1478 { "IEEE_tx_drop", IEEE_T_DROP },
1479 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1480 { "IEEE_tx_1col", IEEE_T_1COL },
1481 { "IEEE_tx_mcol", IEEE_T_MCOL },
1482 { "IEEE_tx_def", IEEE_T_DEF },
1483 { "IEEE_tx_lcol", IEEE_T_LCOL },
1484 { "IEEE_tx_excol", IEEE_T_EXCOL },
1485 { "IEEE_tx_macerr", IEEE_T_MACERR },
1486 { "IEEE_tx_cserr", IEEE_T_CSERR },
1487 { "IEEE_tx_sqe", IEEE_T_SQE },
1488 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1489 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1490
1491 /* RMON RX */
1492 { "rx_packets", RMON_R_PACKETS },
1493 { "rx_broadcast", RMON_R_BC_PKT },
1494 { "rx_multicast", RMON_R_MC_PKT },
1495 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1496 { "rx_undersize", RMON_R_UNDERSIZE },
1497 { "rx_oversize", RMON_R_OVERSIZE },
1498 { "rx_fragment", RMON_R_FRAG },
1499 { "rx_jabber", RMON_R_JAB },
1500 { "rx_64byte", RMON_R_P64 },
1501 { "rx_65to127byte", RMON_R_P65TO127 },
1502 { "rx_128to255byte", RMON_R_P128TO255 },
1503 { "rx_256to511byte", RMON_R_P256TO511 },
1504 { "rx_512to1023byte", RMON_R_P512TO1023 },
1505 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1506 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1507 { "rx_octets", RMON_R_OCTETS },
1508
1509 /* IEEE RX */
1510 { "IEEE_rx_drop", IEEE_R_DROP },
1511 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1512 { "IEEE_rx_crc", IEEE_R_CRC },
1513 { "IEEE_rx_align", IEEE_R_ALIGN },
1514 { "IEEE_rx_macerr", IEEE_R_MACERR },
1515 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1516 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1517};
1518
1519static void fec_enet_get_ethtool_stats(struct net_device *dev,
1520 struct ethtool_stats *stats, u64 *data)
1521{
1522 struct fec_enet_private *fep = netdev_priv(dev);
1523 int i;
1524
1525 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1526 data[i] = readl(fep->hwp + fec_stats[i].offset);
1527}
1528
1529static void fec_enet_get_strings(struct net_device *netdev,
1530 u32 stringset, u8 *data)
1531{
1532 int i;
1533 switch (stringset) {
1534 case ETH_SS_STATS:
1535 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1536 memcpy(data + i * ETH_GSTRING_LEN,
1537 fec_stats[i].name, ETH_GSTRING_LEN);
1538 break;
1539 }
1540}
1541
1542static int fec_enet_get_sset_count(struct net_device *dev, int sset)
1543{
1544 switch (sset) {
1545 case ETH_SS_STATS:
1546 return ARRAY_SIZE(fec_stats);
1547 default:
1548 return -EOPNOTSUPP;
1549 }
1550}
Guenter Roeckd1391932013-06-18 10:04:59 -07001551#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07001552
Chris Healy32bc9b42013-06-17 07:25:06 -07001553static int fec_enet_nway_reset(struct net_device *dev)
1554{
1555 struct fec_enet_private *fep = netdev_priv(dev);
1556 struct phy_device *phydev = fep->phy_dev;
1557
1558 if (!phydev)
1559 return -ENODEV;
1560
1561 return genphy_restart_aneg(phydev);
1562}
1563
stephen hemminger9b07be42012-01-04 12:59:49 +00001564static const struct ethtool_ops fec_enet_ethtool_ops = {
Guenter Roeckd1391932013-06-18 10:04:59 -07001565#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001566 .get_pauseparam = fec_enet_get_pauseparam,
1567 .set_pauseparam = fec_enet_set_pauseparam,
Guenter Roeckd1391932013-06-18 10:04:59 -07001568#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00001569 .get_settings = fec_enet_get_settings,
1570 .set_settings = fec_enet_set_settings,
1571 .get_drvinfo = fec_enet_get_drvinfo,
1572 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00001573 .get_ts_info = fec_enet_get_ts_info,
Chris Healy32bc9b42013-06-17 07:25:06 -07001574 .nway_reset = fec_enet_nway_reset,
Chris Healy38ae92d2013-06-25 23:18:52 -07001575#ifndef CONFIG_M5272
1576 .get_ethtool_stats = fec_enet_get_ethtool_stats,
1577 .get_strings = fec_enet_get_strings,
1578 .get_sset_count = fec_enet_get_sset_count,
1579#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00001580};
1581
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001582static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001583{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001584 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001585 struct phy_device *phydev = fep->phy_dev;
1586
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001587 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001588 return -EINVAL;
1589
1590 if (!phydev)
1591 return -ENODEV;
1592
Frank Liff43da82013-01-03 16:04:23 +00001593 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
Frank Li6605b732012-10-30 18:25:31 +00001594 return fec_ptp_ioctl(ndev, rq, cmd);
Frank Liff43da82013-01-03 16:04:23 +00001595
Richard Cochran28b04112010-07-17 08:48:55 +00001596 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001599static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001600{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001601 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001602 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001603 struct sk_buff *skb;
1604 struct bufdesc *bdp;
1605
1606 bdp = fep->rx_bd_base;
1607 for (i = 0; i < RX_RING_SIZE; i++) {
1608 skb = fep->rx_skbuff[i];
1609
1610 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001611 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001612 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1613 if (skb)
1614 dev_kfree_skb(skb);
Frank Liff43da82013-01-03 16:04:23 +00001615 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001616 }
1617
1618 bdp = fep->tx_bd_base;
1619 for (i = 0; i < TX_RING_SIZE; i++)
1620 kfree(fep->tx_bounce[i]);
1621}
1622
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001623static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001624{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001625 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001626 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001627 struct sk_buff *skb;
1628 struct bufdesc *bdp;
1629
1630 bdp = fep->rx_bd_base;
1631 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001632 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001633 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001634 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001635 return -ENOMEM;
1636 }
1637 fep->rx_skbuff[i] = skb;
1638
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001639 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001640 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1641 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00001642
1643 if (fep->bufdesc_ex) {
1644 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1645 ebdp->cbd_esc = BD_ENET_RX_INT;
1646 }
1647
1648 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001649 }
1650
1651 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001652 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001653 bdp->cbd_sc |= BD_SC_WRAP;
1654
1655 bdp = fep->tx_bd_base;
1656 for (i = 0; i < TX_RING_SIZE; i++) {
1657 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1658
1659 bdp->cbd_sc = 0;
1660 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001661
Frank Liff43da82013-01-03 16:04:23 +00001662 if (fep->bufdesc_ex) {
1663 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00001664 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00001665 }
1666
1667 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001668 }
1669
1670 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001671 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001672 bdp->cbd_sc |= BD_SC_WRAP;
1673
1674 return 0;
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001678fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001680 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001681 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Frank Lidc975382013-01-28 18:31:42 +00001683 napi_enable(&fep->napi);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* I should reset the ring buffers here, but I don't yet know
1686 * a simple way to do that.
1687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001689 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001690 if (ret)
1691 return ret;
1692
Bryan Wu418bd0d2010-05-28 03:40:39 -07001693 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001694 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001695 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001696 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001697 return ret;
1698 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001699 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001700 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001706fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001708 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Sascha Hauer22f6b862009-04-15 01:32:18 +00001710 /* Don't know what to do yet. */
Georg Hofmann3f104c32013-03-14 06:54:09 +00001711 napi_disable(&fep->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001713 netif_stop_queue(ndev);
1714 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001716 if (fep->phy_dev) {
1717 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001718 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001719 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001720
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001721 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/* Set or clear the multicast filter for this adaptor.
1727 * Skeleton taken from sunlance driver.
1728 * The CPM Ethernet implementation allows Multicast as well as individual
1729 * MAC address filtering. Some of the drivers check to make sure it is
1730 * a group multicast address, and discard those that are not. I guess I
1731 * will do the same for now, but just remove the test if you want
1732 * individual filtering as well (do the upper net layers want or support
1733 * this kind of feature?).
1734 */
1735
1736#define HASH_BITS 6 /* #bits in hash */
1737#define CRC32_POLY 0xEDB88320
1738
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001739static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001741 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001742 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001743 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 unsigned char hash;
1745
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001746 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001747 tmp = readl(fep->hwp + FEC_R_CNTRL);
1748 tmp |= 0x8;
1749 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001750 return;
1751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Sascha Hauer4e831832009-04-15 01:32:19 +00001753 tmp = readl(fep->hwp + FEC_R_CNTRL);
1754 tmp &= ~0x8;
1755 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001756
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001757 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001758 /* Catch all multicast addresses, so set the
1759 * filter to all 1's
1760 */
1761 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1762 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Sascha Hauer4e831832009-04-15 01:32:19 +00001764 return;
1765 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001766
Sascha Hauer4e831832009-04-15 01:32:19 +00001767 /* Clear filter and add the addresses in hash register
1768 */
1769 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1770 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001772 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001773 /* calculate crc32 value of mac address */
1774 crc = 0xffffffff;
1775
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001776 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001777 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001778 for (bit = 0; bit < 8; bit++, data >>= 1) {
1779 crc = (crc >> 1) ^
1780 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001783
1784 /* only upper 6 bits (HASH_BITS) are used
1785 * which point to specific bit in he hash registers
1786 */
1787 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1788
1789 if (hash > 31) {
1790 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1791 tmp |= 1 << (hash - 32);
1792 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1793 } else {
1794 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1795 tmp |= 1 << hash;
1796 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799}
1800
Sascha Hauer22f6b862009-04-15 01:32:18 +00001801/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001802static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001803fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001805 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001806 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Sascha Hauer009fda82009-04-15 01:32:23 +00001808 if (!is_valid_ether_addr(addr->sa_data))
1809 return -EADDRNOTAVAIL;
1810
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001811 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001812
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001813 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1814 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001815 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001816 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001817 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001821#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001822/**
1823 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001824 * @dev: The FEC network adapter
1825 *
1826 * Polled functionality used by netconsole and others in non interrupt mode
1827 *
1828 */
Wei Yongjun47a52472013-03-20 05:06:11 +00001829static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001830{
1831 int i;
1832 struct fec_enet_private *fep = netdev_priv(dev);
1833
1834 for (i = 0; i < FEC_IRQ_NUM; i++) {
1835 if (fep->irq[i] > 0) {
1836 disable_irq(fep->irq[i]);
1837 fec_enet_interrupt(fep->irq[i], dev);
1838 enable_irq(fep->irq[i]);
1839 }
1840 }
1841}
1842#endif
1843
Jim Baxter4c09eed2013-04-19 08:10:49 +00001844static int fec_set_features(struct net_device *netdev,
1845 netdev_features_t features)
1846{
1847 struct fec_enet_private *fep = netdev_priv(netdev);
1848 netdev_features_t changed = features ^ netdev->features;
1849
1850 netdev->features = features;
1851
1852 /* Receive checksum has been changed */
1853 if (changed & NETIF_F_RXCSUM) {
1854 if (features & NETIF_F_RXCSUM)
1855 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1856 else
1857 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
1858
1859 if (netif_running(netdev)) {
1860 fec_stop(netdev);
1861 fec_restart(netdev, fep->phy_dev->duplex);
1862 netif_wake_queue(netdev);
1863 } else {
1864 fec_restart(netdev, fep->phy_dev->duplex);
1865 }
1866 }
1867
1868 return 0;
1869}
1870
Sascha Hauer009fda82009-04-15 01:32:23 +00001871static const struct net_device_ops fec_netdev_ops = {
1872 .ndo_open = fec_enet_open,
1873 .ndo_stop = fec_enet_close,
1874 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001875 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001876 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001877 .ndo_validate_addr = eth_validate_addr,
1878 .ndo_tx_timeout = fec_timeout,
1879 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001880 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001881#ifdef CONFIG_NET_POLL_CONTROLLER
1882 .ndo_poll_controller = fec_poll_controller,
1883#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001884 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00001885};
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /*
1888 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001889 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001891static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001893 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00001894 const struct platform_device_id *id_entry =
1895 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001896 struct bufdesc *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001898 /* Allocate memory for buffer descriptors. */
1899 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001900 GFP_KERNEL);
1901 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001902 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10001903
Frank Li14109a52013-03-26 16:12:03 +00001904 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001905
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001906 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Shawn Guo49da97d2011-01-05 21:13:11 +00001908 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001909 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001911 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 fep->rx_bd_base = cbd_base;
Frank Liff43da82013-01-03 16:04:23 +00001913 if (fep->bufdesc_ex)
1914 fep->tx_bd_base = (struct bufdesc *)
1915 (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1916 else
1917 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Sascha Hauer22f6b862009-04-15 01:32:18 +00001919 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001920 ndev->watchdog_timeo = TX_TIMEOUT;
1921 ndev->netdev_ops = &fec_netdev_ops;
1922 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001923
Frank Lidc975382013-01-28 18:31:42 +00001924 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1925 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1926
Shawn Guo48496252013-05-08 21:08:22 +00001927 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
1928 /* enable hw accelerator */
1929 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1930 | NETIF_F_RXCSUM);
1931 ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1932 | NETIF_F_RXCSUM);
1933 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1934 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00001935
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001936 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 0;
1939}
1940
Shawn Guoca2cc332011-06-25 02:04:35 +08001941#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05001942static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001943{
1944 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001945 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001946 struct device_node *np = pdev->dev.of_node;
1947
1948 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001949 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001950
Shawn Guoa3caad02012-06-27 03:45:24 +00001951 of_property_read_u32(np, "phy-reset-duration", &msec);
1952 /* A sane reset duration should not be longer than 1s */
1953 if (msec > 1000)
1954 msec = 1;
1955
Shawn Guoca2cc332011-06-25 02:04:35 +08001956 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001957 if (!gpio_is_valid(phy_reset))
1958 return;
1959
Shawn Guo119fc002012-06-27 03:45:22 +00001960 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1961 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001962 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001963 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001964 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001965 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001966 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001967 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001968}
1969#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001970static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001971{
1972 /*
1973 * In case of platform probe, the reset has been done
1974 * by machine code.
1975 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001976}
1977#endif /* CONFIG_OF */
1978
Bill Pemberton33897cc2012-12-03 09:23:58 -05001979static int
Sascha Haueread73182009-01-28 23:03:11 +00001980fec_probe(struct platform_device *pdev)
1981{
1982 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001983 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001984 struct net_device *ndev;
1985 int i, irq, ret = 0;
1986 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001987 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001988 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08001989
1990 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1991 if (of_id)
1992 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001993
1994 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1995 if (!r)
1996 return -ENXIO;
1997
Sascha Haueread73182009-01-28 23:03:11 +00001998 /* Init network device */
1999 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002000 if (!ndev)
2001 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002002
2003 SET_NETDEV_DEV(ndev, &pdev->dev);
2004
2005 /* setup board info structure */
2006 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002007
Guenter Roeckd1391932013-06-18 10:04:59 -07002008#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002009 /* default enable pause frame auto negotiation */
2010 if (pdev->id_entry &&
2011 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2012 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002013#endif
Frank Libaa70a52013-01-16 16:55:58 +00002014
Tushar Behera941e1732013-06-10 17:05:05 +05302015 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2016 if (IS_ERR(fep->hwp)) {
2017 ret = PTR_ERR(fep->hwp);
2018 goto failed_ioremap;
2019 }
2020
Bryan Wue6b043d2010-03-31 02:10:44 +00002021 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002022 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002023
Frank Liff43da82013-01-03 16:04:23 +00002024 fep->bufdesc_ex = 0;
2025
Sascha Haueread73182009-01-28 23:03:11 +00002026 platform_set_drvdata(pdev, ndev);
2027
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002028 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002029 if (ret < 0) {
2030 pdata = pdev->dev.platform_data;
2031 if (pdata)
2032 fep->phy_interface = pdata->phy;
2033 else
2034 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2035 } else {
2036 fep->phy_interface = ret;
2037 }
2038
Fabio Estevame2f8d552013-02-22 06:40:45 +00002039 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2040 if (IS_ERR(fep->clk_ipg)) {
2041 ret = PTR_ERR(fep->clk_ipg);
2042 goto failed_clk;
2043 }
2044
2045 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2046 if (IS_ERR(fep->clk_ahb)) {
2047 ret = PTR_ERR(fep->clk_ahb);
2048 goto failed_clk;
2049 }
2050
Linus Torvalds38f56f32013-05-07 11:06:17 -07002051 /* enet_out is optional, depends on board */
2052 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2053 if (IS_ERR(fep->clk_enet_out))
2054 fep->clk_enet_out = NULL;
2055
Fabio Estevame2f8d552013-02-22 06:40:45 +00002056 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002057 fep->bufdesc_ex =
2058 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002059 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002060 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002061 fep->bufdesc_ex = 0;
2062 }
2063
2064 clk_prepare_enable(fep->clk_ahb);
2065 clk_prepare_enable(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002066 clk_prepare_enable(fep->clk_enet_out);
2067 clk_prepare_enable(fep->clk_ptp);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002068
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002069 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2070 if (!IS_ERR(fep->reg_phy)) {
2071 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002072 if (ret) {
2073 dev_err(&pdev->dev,
2074 "Failed to enable phy regulator: %d\n", ret);
2075 goto failed_regulator;
2076 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002077 } else {
2078 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002079 }
2080
2081 fec_reset_phy(pdev);
2082
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002083 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002084 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002085
2086 ret = fec_enet_init(ndev);
2087 if (ret)
2088 goto failed_init;
2089
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002090 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002091 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002092 if (irq < 0) {
2093 if (i)
2094 break;
2095 ret = irq;
2096 goto failed_irq;
2097 }
Sascha Haueread73182009-01-28 23:03:11 +00002098 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
2099 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01002100 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00002101 irq = platform_get_irq(pdev, i);
2102 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002103 }
2104 goto failed_irq;
2105 }
2106 }
2107
Bryan Wue6b043d2010-03-31 02:10:44 +00002108 ret = fec_enet_mii_init(pdev);
2109 if (ret)
2110 goto failed_mii_init;
2111
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002112 /* Carrier starts down, phylib will bring it up */
2113 netif_carrier_off(ndev);
2114
Sascha Haueread73182009-01-28 23:03:11 +00002115 ret = register_netdev(ndev);
2116 if (ret)
2117 goto failed_register;
2118
Fabio Estevameb1d0642013-04-13 07:25:36 +00002119 if (fep->bufdesc_ex && fep->ptp_clock)
2120 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2121
Frank Li54309fa2013-05-07 14:08:44 +00002122 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00002123 return 0;
2124
2125failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002126 fec_enet_mii_remove(fep);
2127failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002128failed_irq:
Fabio Estevame2f8d552013-02-22 06:40:45 +00002129 for (i = 0; i < FEC_IRQ_NUM; i++) {
2130 irq = platform_get_irq(pdev, i);
2131 if (irq > 0)
2132 free_irq(irq, ndev);
2133 }
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002134failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002135 if (fep->reg_phy)
2136 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002137failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002138 clk_disable_unprepare(fep->clk_ahb);
2139 clk_disable_unprepare(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002140 clk_disable_unprepare(fep->clk_enet_out);
2141 clk_disable_unprepare(fep->clk_ptp);
Sascha Haueread73182009-01-28 23:03:11 +00002142failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002143failed_ioremap:
2144 free_netdev(ndev);
2145
2146 return ret;
2147}
2148
Bill Pemberton33897cc2012-12-03 09:23:58 -05002149static int
Sascha Haueread73182009-01-28 23:03:11 +00002150fec_drv_remove(struct platform_device *pdev)
2151{
2152 struct net_device *ndev = platform_get_drvdata(pdev);
2153 struct fec_enet_private *fep = netdev_priv(ndev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002154 int i;
Sascha Haueread73182009-01-28 23:03:11 +00002155
Frank Li54309fa2013-05-07 14:08:44 +00002156 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002157 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002158 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002159 del_timer_sync(&fep->time_keep);
Fabio Estevamc55284e2013-05-27 03:48:32 +00002160 for (i = 0; i < FEC_IRQ_NUM; i++) {
2161 int irq = platform_get_irq(pdev, i);
2162 if (irq > 0)
2163 free_irq(irq, ndev);
2164 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002165 if (fep->reg_phy)
2166 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002167 clk_disable_unprepare(fep->clk_ptp);
2168 if (fep->ptp_clock)
2169 ptp_clock_unregister(fep->ptp_clock);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002170 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002171 clk_disable_unprepare(fep->clk_ahb);
2172 clk_disable_unprepare(fep->clk_ipg);
Sascha Haueread73182009-01-28 23:03:11 +00002173 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002174
Sascha Haueread73182009-01-28 23:03:11 +00002175 return 0;
2176}
2177
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002178#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002179static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002180fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002181{
Eric Benard87cad5c2010-06-18 04:19:54 +00002182 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002183 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002184
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002185 if (netif_running(ndev)) {
2186 fec_stop(ndev);
2187 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002188 }
Linus Torvalds38f56f32013-05-07 11:06:17 -07002189 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002190 clk_disable_unprepare(fep->clk_ahb);
2191 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002192
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002193 if (fep->reg_phy)
2194 regulator_disable(fep->reg_phy);
2195
Sascha Haueread73182009-01-28 23:03:11 +00002196 return 0;
2197}
2198
2199static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002200fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002201{
Eric Benard87cad5c2010-06-18 04:19:54 +00002202 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002203 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002204 int ret;
2205
2206 if (fep->reg_phy) {
2207 ret = regulator_enable(fep->reg_phy);
2208 if (ret)
2209 return ret;
2210 }
Sascha Haueread73182009-01-28 23:03:11 +00002211
Linus Torvalds38f56f32013-05-07 11:06:17 -07002212 clk_prepare_enable(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002213 clk_prepare_enable(fep->clk_ahb);
2214 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002215 if (netif_running(ndev)) {
2216 fec_restart(ndev, fep->full_duplex);
2217 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002218 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002219
Sascha Haueread73182009-01-28 23:03:11 +00002220 return 0;
2221}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002222#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002223
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002224static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002225
Sascha Haueread73182009-01-28 23:03:11 +00002226static struct platform_driver fec_driver = {
2227 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002228 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002229 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002230 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002231 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002232 },
Shawn Guob5680e02011-01-05 21:13:13 +00002233 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002234 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002235 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002236};
2237
Fabio Estevamaaca2372012-01-23 16:44:50 +00002238module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240MODULE_LICENSE("GPL");