blob: aff8a5cd17c773cb370fe4a44575a198081181b7 [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
Jim Baxter4c09eed2013-04-19 08:10:49 +0000519 /* set RX checksum */
520 val = readl(fep->hwp + FEC_RACC);
521 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
522 val |= FEC_RACC_OPTIONS;
523 else
524 val &= ~FEC_RACC_OPTIONS;
525 writel(val, fep->hwp + FEC_RACC);
526
Uwe Kleine-König45993652011-01-19 20:47:04 +0100527 /*
528 * The phy interface and speed need to get configured
529 * differently on enet-mac.
530 */
531 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100532 /* Enable flow control and length check */
533 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100534
Shawn Guo230dec62011-09-23 02:12:48 +0000535 /* RGMII, RMII or MII */
536 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
537 rcntl |= (1 << 6);
538 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100539 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100540 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100541 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100542
Shawn Guo230dec62011-09-23 02:12:48 +0000543 /* 1G, 100M or 10M */
544 if (fep->phy_dev) {
545 if (fep->phy_dev->speed == SPEED_1000)
546 ecntl |= (1 << 5);
547 else if (fep->phy_dev->speed == SPEED_100)
548 rcntl &= ~(1 << 9);
549 else
550 rcntl |= (1 << 9);
551 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100552 } else {
553#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800554 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000555 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100556 /* disable the gasket and wait */
557 writel(0, fep->hwp + FEC_MIIGSK_ENR);
558 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
559 udelay(1);
560
561 /*
562 * configure the gasket:
563 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800564 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100565 */
Eric Benard8d82f212012-01-12 06:10:28 +0000566 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
567 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
568 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
569 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
570 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100571
572 /* re-enable the gasket */
573 writel(2, fep->hwp + FEC_MIIGSK_ENR);
574 }
575#endif
576 }
Frank Libaa70a52013-01-16 16:55:58 +0000577
578 /* enable pause frame*/
579 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
580 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
581 fep->phy_dev && fep->phy_dev->pause)) {
582 rcntl |= FEC_ENET_FCE;
583
Jim Baxter4c09eed2013-04-19 08:10:49 +0000584 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000585 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
586 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
587 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
588 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
589
590 /* OPD */
591 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
592 } else {
593 rcntl &= ~FEC_ENET_FCE;
594 }
595
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100596 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100597
Shawn Guo230dec62011-09-23 02:12:48 +0000598 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
599 /* enable ENET endian swap */
600 ecntl |= (1 << 8);
601 /* enable ENET store and forward mode */
602 writel(1 << 8, fep->hwp + FEC_X_WMRK);
603 }
604
Frank Liff43da82013-01-03 16:04:23 +0000605 if (fep->bufdesc_ex)
606 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000607
Chris Healy38ae92d2013-06-25 23:18:52 -0700608#ifndef CONFIG_M5272
609 /* Disable, clear, and enable the MIB */
610 writel(1 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
611 for (i = RMON_T_DROP; i < IEEE_R_OCTETS_OK; i++)
612 writel(0, fep->hwp + i);
613 writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
614#endif
615
Uwe Kleine-König45993652011-01-19 20:47:04 +0100616 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000617 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100618 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
619
Frank Liff43da82013-01-03 16:04:23 +0000620 if (fep->bufdesc_ex)
621 fec_ptp_start_cyclecounter(ndev);
622
Uwe Kleine-König45993652011-01-19 20:47:04 +0100623 /* Enable interrupts we wish to service */
624 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Frank Li54309fa2013-05-07 14:08:44 +0000625
626 if (netif_running(ndev)) {
Fabio Estevam31691342013-05-15 07:06:26 +0000627 netif_tx_unlock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000628 netif_wake_queue(ndev);
Fabio Estevam1ed0d562013-05-15 07:06:27 +0000629 napi_enable(&fep->napi);
630 netif_device_attach(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000631 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100632}
633
634static void
635fec_stop(struct net_device *ndev)
636{
637 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000638 const struct platform_device_id *id_entry =
639 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000640 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100641
642 /* We cannot expect a graceful transmit stop without link !!! */
643 if (fep->link) {
644 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
645 udelay(10);
646 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000647 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +0100648 }
649
650 /* Whack a reset. We should wait for this. */
651 writel(1, fep->hwp + FEC_ECNTRL);
652 udelay(10);
653 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
654 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000655
656 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000657 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000658 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000659 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
660 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100661}
662
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100665fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100667 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100669 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Frank Li54309fa2013-05-07 14:08:44 +0000671 fep->delay_work.timeout = true;
672 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
673}
674
675static void fec_enet_work(struct work_struct *work)
676{
677 struct fec_enet_private *fep =
678 container_of(work,
679 struct fec_enet_private,
680 delay_work.delay_work.work);
681
682 if (fep->delay_work.timeout) {
683 fep->delay_work.timeout = false;
684 fec_restart(fep->netdev, fep->full_duplex);
685 netif_wake_queue(fep->netdev);
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100690fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000693 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000694 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +0000696 int index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100698 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 bdp = fep->dirty_tx;
700
Frank Lide5fb0a2013-03-03 17:34:25 +0000701 /* get next bdp of dirty_tx */
702 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
703 bdp = fep->tx_bd_base;
704 else
705 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
706
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000707 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +0000708
709 /* current queue is empty */
710 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000711 break;
712
Frank Lide5fb0a2013-03-03 17:34:25 +0000713 if (fep->bufdesc_ex)
714 index = (struct bufdesc_ex *)bdp -
715 (struct bufdesc_ex *)fep->tx_bd_base;
716 else
717 index = bdp - fep->tx_bd_base;
718
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100719 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
720 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000721 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Frank Lide5fb0a2013-03-03 17:34:25 +0000723 skb = fep->tx_skbuff[index];
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000726 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 BD_ENET_TX_RL | BD_ENET_TX_UN |
728 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100729 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000730 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100731 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000732 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100733 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000734 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100735 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000736 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100737 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000738 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100739 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100741 ndev->stats.tx_packets++;
Jim Baxter06efce72013-06-27 19:25:08 +0100742 ndev->stats.tx_bytes += bdp->cbd_datlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Frank Liff43da82013-01-03 16:04:23 +0000745 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
746 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000747 struct skb_shared_hwtstamps shhwtstamps;
748 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000749 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000750
751 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
752 spin_lock_irqsave(&fep->tmreg_lock, flags);
753 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000754 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000755 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
756 skb_tstamp_tx(skb, &shhwtstamps);
757 }
Frank Liff43da82013-01-03 16:04:23 +0000758
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000759 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +0000760 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Deferred means some collisions occurred during transmit,
763 * but we eventually sent the packet OK.
764 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000765 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100766 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400767
Sascha Hauer22f6b862009-04-15 01:32:18 +0000768 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +0000770 fep->tx_skbuff[index] = NULL;
771
772 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400773
Sascha Hauer22f6b862009-04-15 01:32:18 +0000774 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000775 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 bdp = fep->tx_bd_base;
777 else
Frank Liff43da82013-01-03 16:04:23 +0000778 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400779
Sascha Hauer22f6b862009-04-15 01:32:18 +0000780 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000782 if (fep->dirty_tx != fep->cur_tx) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100783 if (netif_queue_stopped(ndev))
784 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 }
Frank Lide5fb0a2013-03-03 17:34:25 +0000787 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790
791/* During a receive, the cur_rx points to the current incoming buffer.
792 * When we update through the ring, if the next incoming buffer has
793 * not been given to the system, we just set the empty indicator,
794 * effectively tossing the packet.
795 */
Frank Lidc975382013-01-28 18:31:42 +0000796static int
797fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100799 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000800 const struct platform_device_id *id_entry =
801 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000802 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000803 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct sk_buff *skb;
805 ushort pkt_len;
806 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +0000807 int pkt_received = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400808
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000809#ifdef CONFIG_M532x
810 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400811#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* First, grab all of the stats for the incoming packet.
814 * These get messed up if we get called due to a busy condition.
815 */
816 bdp = fep->cur_rx;
817
Sascha Hauer22f6b862009-04-15 01:32:18 +0000818 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Frank Lidc975382013-01-28 18:31:42 +0000820 if (pkt_received >= budget)
821 break;
822 pkt_received++;
823
Sascha Hauer22f6b862009-04-15 01:32:18 +0000824 /* Since we have allocated space to hold a complete frame,
825 * the last indicator should be set.
826 */
827 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +0000828 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Sascha Hauer22f6b862009-04-15 01:32:18 +0000830 if (!fep->opened)
831 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Sascha Hauer22f6b862009-04-15 01:32:18 +0000833 /* Check for errors. */
834 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100836 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000837 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
838 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100839 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000840 }
841 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100842 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000843 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100844 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000845 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100846 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000848
849 /* Report late collisions as a frame error.
850 * On this error, the BD is closed, but we don't know what we
851 * have in the buffer. So, just drop this frame on the floor.
852 */
853 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100854 ndev->stats.rx_errors++;
855 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000856 goto rx_processing_done;
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Sascha Hauer22f6b862009-04-15 01:32:18 +0000859 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100860 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000861 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100862 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000863 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100865 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
866 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000867
Shawn Guob5680e02011-01-05 21:13:13 +0000868 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
869 swap_buffer(data, pkt_len);
870
Sascha Hauer22f6b862009-04-15 01:32:18 +0000871 /* This does 16 byte alignment, exactly what we need.
872 * The packet length includes FCS, but we don't want to
873 * include that when passing upstream as it messes up
874 * bridging applications.
875 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000876 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Sascha Hauer85498892009-04-15 01:32:21 +0000878 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100879 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000880 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000881 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000882 skb_put(skb, pkt_len - 4); /* Make room */
883 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100884 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +0000885
Frank Li6605b732012-10-30 18:25:31 +0000886 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +0000887 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000888 struct skb_shared_hwtstamps *shhwtstamps =
889 skb_hwtstamps(skb);
890 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000891 struct bufdesc_ex *ebdp =
892 (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000893
894 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
895
896 spin_lock_irqsave(&fep->tmreg_lock, flags);
897 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000898 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000899 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
900 }
Frank Liff43da82013-01-03 16:04:23 +0000901
Jim Baxter4c09eed2013-04-19 08:10:49 +0000902 if (fep->bufdesc_ex &&
903 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
904 struct bufdesc_ex *ebdp =
905 (struct bufdesc_ex *)bdp;
906 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
907 /* don't check it */
908 skb->ip_summed = CHECKSUM_UNNECESSARY;
909 } else {
910 skb_checksum_none_assert(skb);
911 }
912 }
913
Richard Cochran18a03b92011-06-12 02:18:59 +0000914 if (!skb_defer_rx_timestamp(skb))
Frank Lidc975382013-01-28 18:31:42 +0000915 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000916 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000917
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100918 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
919 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000920rx_processing_done:
921 /* Clear the status flags for this buffer */
922 status &= ~BD_ENET_RX_STATS;
923
924 /* Mark the buffer empty */
925 status |= BD_ENET_RX_EMPTY;
926 bdp->cbd_sc = status;
927
Frank Liff43da82013-01-03 16:04:23 +0000928 if (fep->bufdesc_ex) {
929 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
930
931 ebdp->cbd_esc = BD_ENET_RX_INT;
932 ebdp->cbd_prot = 0;
933 ebdp->cbd_bdu = 0;
934 }
Frank Li6605b732012-10-30 18:25:31 +0000935
Sascha Hauer22f6b862009-04-15 01:32:18 +0000936 /* Update BD pointer to next entry */
937 if (status & BD_ENET_RX_WRAP)
938 bdp = fep->rx_bd_base;
939 else
Frank Liff43da82013-01-03 16:04:23 +0000940 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000941 /* Doing this here will keep the FEC running while we process
942 * incoming frames. On a heavily loaded network, we should be
943 * able to keep up at the expense of system resources.
944 */
945 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000947 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Frank Lidc975382013-01-28 18:31:42 +0000949 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Uwe Kleine-König45993652011-01-19 20:47:04 +0100952static irqreturn_t
953fec_enet_interrupt(int irq, void *dev_id)
954{
955 struct net_device *ndev = dev_id;
956 struct fec_enet_private *fep = netdev_priv(ndev);
957 uint int_events;
958 irqreturn_t ret = IRQ_NONE;
959
960 do {
961 int_events = readl(fep->hwp + FEC_IEVENT);
962 writel(int_events, fep->hwp + FEC_IEVENT);
963
Frank Lide5fb0a2013-03-03 17:34:25 +0000964 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100965 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +0000966
967 /* Disable the RX interrupt */
968 if (napi_schedule_prep(&fep->napi)) {
969 writel(FEC_RX_DISABLED_IMASK,
970 fep->hwp + FEC_IMASK);
971 __napi_schedule(&fep->napi);
972 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100973 }
974
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975 if (int_events & FEC_ENET_MII) {
976 ret = IRQ_HANDLED;
977 complete(&fep->mdio_done);
978 }
979 } while (int_events);
980
981 return ret;
982}
983
Frank Lidc975382013-01-28 18:31:42 +0000984static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
985{
986 struct net_device *ndev = napi->dev;
987 int pkts = fec_enet_rx(ndev, budget);
988 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100989
Frank Lide5fb0a2013-03-03 17:34:25 +0000990 fec_enet_tx(ndev);
991
Frank Lidc975382013-01-28 18:31:42 +0000992 if (pkts < budget) {
993 napi_complete(napi);
994 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
995 }
996 return pkts;
997}
Uwe Kleine-König45993652011-01-19 20:47:04 +0100998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001000static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001002 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +00001003 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001004 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Shawn Guo49da97d2011-01-05 21:13:11 +00001006 /*
1007 * try to get mac address in following order:
1008 *
1009 * 1) module parameter via kernel command line in form
1010 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1011 */
1012 iap = macaddr;
1013
1014 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001015 * 2) from device tree data
1016 */
1017 if (!is_valid_ether_addr(iap)) {
1018 struct device_node *np = fep->pdev->dev.of_node;
1019 if (np) {
1020 const char *mac = of_get_mac_address(np);
1021 if (mac)
1022 iap = (unsigned char *) mac;
1023 }
1024 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001025
1026 /*
1027 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001028 */
1029 if (!is_valid_ether_addr(iap)) {
1030#ifdef CONFIG_M5272
1031 if (FEC_FLASHMAC)
1032 iap = (unsigned char *)FEC_FLASHMAC;
1033#else
1034 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001035 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001036#endif
1037 }
1038
1039 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001040 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001041 */
1042 if (!is_valid_ether_addr(iap)) {
1043 *((unsigned long *) &tmpaddr[0]) =
1044 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
1045 *((unsigned short *) &tmpaddr[4]) =
1046 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 iap = &tmpaddr[0];
1048 }
1049
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001050 /*
1051 * 5) random mac address
1052 */
1053 if (!is_valid_ether_addr(iap)) {
1054 /* Report it and use a random ethernet address instead */
1055 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1056 eth_hw_addr_random(ndev);
1057 netdev_info(ndev, "Using random MAC address: %pM\n",
1058 ndev->dev_addr);
1059 return;
1060 }
1061
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001062 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Shawn Guo49da97d2011-01-05 21:13:11 +00001064 /* Adjust MAC if using macaddr */
1065 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001066 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069/* ------------------------------------------------------------------------- */
1070
Bryan Wue6b043d2010-03-31 02:10:44 +00001071/*
1072 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001074static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001076 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001077 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001078 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Bryan Wue6b043d2010-03-31 02:10:44 +00001080 /* Prevent a state halted on mii error */
1081 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1082 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001083 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001085
Bryan Wue6b043d2010-03-31 02:10:44 +00001086 if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001087 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001088 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001089 status_change = 1;
1090 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001091
Lucas Stachd97e7492013-03-14 05:12:01 +00001092 if (fep->full_duplex != phy_dev->duplex)
1093 status_change = 1;
1094
1095 if (phy_dev->speed != fep->speed) {
1096 fep->speed = phy_dev->speed;
1097 status_change = 1;
1098 }
1099
1100 /* if any of the above changed restart the FEC */
1101 if (status_change)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001102 fec_restart(ndev, phy_dev->duplex);
Lucas Stachd97e7492013-03-14 05:12:01 +00001103 } else {
1104 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001105 fec_stop(ndev);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001106 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001107 status_change = 1;
1108 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001109 }
1110
Bryan Wue6b043d2010-03-31 02:10:44 +00001111 if (status_change)
1112 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Bryan Wue6b043d2010-03-31 02:10:44 +00001115static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Bryan Wue6b043d2010-03-31 02:10:44 +00001117 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001118 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001119
1120 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001121 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001122
1123 /* start a read op */
1124 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1125 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1126 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1127
1128 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001129 time_left = wait_for_completion_timeout(&fep->mdio_done,
1130 usecs_to_jiffies(FEC_MII_TIMEOUT));
1131 if (time_left == 0) {
1132 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001133 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001134 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001135 }
1136
1137 /* return value */
1138 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1139}
1140
1141static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1142 u16 value)
1143{
1144 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001145 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001146
1147 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001148 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001149
Shawn Guo862f0982011-01-05 21:13:09 +00001150 /* start a write op */
1151 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001152 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1153 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1154 fep->hwp + FEC_MII_DATA);
1155
1156 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001157 time_left = wait_for_completion_timeout(&fep->mdio_done,
1158 usecs_to_jiffies(FEC_MII_TIMEOUT));
1159 if (time_left == 0) {
1160 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001161 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001162 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001163 }
1164
1165 return 0;
1166}
1167
1168static int fec_enet_mdio_reset(struct mii_bus *bus)
1169{
1170 return 0;
1171}
1172
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001173static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001174{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001175 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001176 const struct platform_device_id *id_entry =
1177 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001178 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001179 char mdio_bus_id[MII_BUS_ID_SIZE];
1180 char phy_name[MII_BUS_ID_SIZE + 3];
1181 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001182 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001183
Bryan Wu418bd0d2010-05-28 03:40:39 -07001184 fep->phy_dev = NULL;
1185
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001186 /* check for attached phy */
1187 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1188 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1189 continue;
1190 if (fep->mii_bus->phy_map[phy_id] == NULL)
1191 continue;
1192 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1193 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001194 if (dev_id--)
1195 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001196 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1197 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001198 }
1199
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001200 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001201 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001202 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001203 phy_id = 0;
1204 }
1205
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001206 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001207 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001208 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001209 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001210 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001211 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001212 }
1213
1214 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001215 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001216 phy_dev->supported &= PHY_GBIT_FEATURES;
Frank Libaa70a52013-01-16 16:55:58 +00001217 phy_dev->supported |= SUPPORTED_Pause;
1218 }
Shawn Guo230dec62011-09-23 02:12:48 +00001219 else
1220 phy_dev->supported &= PHY_BASIC_FEATURES;
1221
Bryan Wue6b043d2010-03-31 02:10:44 +00001222 phy_dev->advertising = phy_dev->supported;
1223
1224 fep->phy_dev = phy_dev;
1225 fep->link = 0;
1226 fep->full_duplex = 0;
1227
Joe Perches31b77202013-04-13 19:03:17 +00001228 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1229 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1230 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001231
Bryan Wue6b043d2010-03-31 02:10:44 +00001232 return 0;
1233}
1234
1235static int fec_enet_mii_init(struct platform_device *pdev)
1236{
Shawn Guob5680e02011-01-05 21:13:13 +00001237 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001238 struct net_device *ndev = platform_get_drvdata(pdev);
1239 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001240 const struct platform_device_id *id_entry =
1241 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001242 int err = -ENXIO, i;
1243
Shawn Guob5680e02011-01-05 21:13:13 +00001244 /*
1245 * The dual fec interfaces are not equivalent with enet-mac.
1246 * Here are the differences:
1247 *
1248 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1249 * - fec0 acts as the 1588 time master while fec1 is slave
1250 * - external phys can only be configured by fec0
1251 *
1252 * That is to say fec1 can not work independently. It only works
1253 * when fec0 is working. The reason behind this design is that the
1254 * second interface is added primarily for Switch mode.
1255 *
1256 * Because of the last point above, both phys are attached on fec0
1257 * mdio interface in board design, and need to be configured by
1258 * fec0 mii_bus.
1259 */
Shawn Guo43af9402011-12-05 05:01:15 +00001260 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001261 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001262 if (mii_cnt && fec0_mii_bus) {
1263 fep->mii_bus = fec0_mii_bus;
1264 mii_cnt++;
1265 return 0;
1266 }
1267 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001268 }
1269
Bryan Wue6b043d2010-03-31 02:10:44 +00001270 fep->mii_timeout = 0;
1271
1272 /*
1273 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001274 *
1275 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1276 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1277 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1278 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001279 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001280 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001281 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1282 fep->phy_speed--;
1283 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001284 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1285
1286 fep->mii_bus = mdiobus_alloc();
1287 if (fep->mii_bus == NULL) {
1288 err = -ENOMEM;
1289 goto err_out;
1290 }
1291
1292 fep->mii_bus->name = "fec_enet_mii_bus";
1293 fep->mii_bus->read = fec_enet_mdio_read;
1294 fep->mii_bus->write = fec_enet_mdio_write;
1295 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001296 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1297 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001298 fep->mii_bus->priv = fep;
1299 fep->mii_bus->parent = &pdev->dev;
1300
1301 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1302 if (!fep->mii_bus->irq) {
1303 err = -ENOMEM;
1304 goto err_out_free_mdiobus;
1305 }
1306
1307 for (i = 0; i < PHY_MAX_ADDR; i++)
1308 fep->mii_bus->irq[i] = PHY_POLL;
1309
Bryan Wue6b043d2010-03-31 02:10:44 +00001310 if (mdiobus_register(fep->mii_bus))
1311 goto err_out_free_mdio_irq;
1312
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001313 mii_cnt++;
1314
Shawn Guob5680e02011-01-05 21:13:13 +00001315 /* save fec0 mii_bus */
1316 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1317 fec0_mii_bus = fep->mii_bus;
1318
Bryan Wue6b043d2010-03-31 02:10:44 +00001319 return 0;
1320
Bryan Wue6b043d2010-03-31 02:10:44 +00001321err_out_free_mdio_irq:
1322 kfree(fep->mii_bus->irq);
1323err_out_free_mdiobus:
1324 mdiobus_free(fep->mii_bus);
1325err_out:
1326 return err;
1327}
1328
1329static void fec_enet_mii_remove(struct fec_enet_private *fep)
1330{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001331 if (--mii_cnt == 0) {
1332 mdiobus_unregister(fep->mii_bus);
1333 kfree(fep->mii_bus->irq);
1334 mdiobus_free(fep->mii_bus);
1335 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001336}
1337
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001338static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001339 struct ethtool_cmd *cmd)
1340{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001341 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001342 struct phy_device *phydev = fep->phy_dev;
1343
1344 if (!phydev)
1345 return -ENODEV;
1346
1347 return phy_ethtool_gset(phydev, cmd);
1348}
1349
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001350static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001351 struct ethtool_cmd *cmd)
1352{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001353 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001354 struct phy_device *phydev = fep->phy_dev;
1355
1356 if (!phydev)
1357 return -ENODEV;
1358
1359 return phy_ethtool_sset(phydev, cmd);
1360}
1361
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001362static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001363 struct ethtool_drvinfo *info)
1364{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001365 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jiri Pirko7826d432013-01-06 00:44:26 +00001367 strlcpy(info->driver, fep->pdev->dev.driver->name,
1368 sizeof(info->driver));
1369 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1370 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
Bryan Wue6b043d2010-03-31 02:10:44 +00001372
Frank Li5ebae4892013-01-06 16:25:07 +00001373static int fec_enet_get_ts_info(struct net_device *ndev,
1374 struct ethtool_ts_info *info)
1375{
1376 struct fec_enet_private *fep = netdev_priv(ndev);
1377
1378 if (fep->bufdesc_ex) {
1379
1380 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1381 SOF_TIMESTAMPING_RX_SOFTWARE |
1382 SOF_TIMESTAMPING_SOFTWARE |
1383 SOF_TIMESTAMPING_TX_HARDWARE |
1384 SOF_TIMESTAMPING_RX_HARDWARE |
1385 SOF_TIMESTAMPING_RAW_HARDWARE;
1386 if (fep->ptp_clock)
1387 info->phc_index = ptp_clock_index(fep->ptp_clock);
1388 else
1389 info->phc_index = -1;
1390
1391 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1392 (1 << HWTSTAMP_TX_ON);
1393
1394 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1395 (1 << HWTSTAMP_FILTER_ALL);
1396 return 0;
1397 } else {
1398 return ethtool_op_get_ts_info(ndev, info);
1399 }
1400}
1401
Frank Libaa70a52013-01-16 16:55:58 +00001402static void fec_enet_get_pauseparam(struct net_device *ndev,
1403 struct ethtool_pauseparam *pause)
1404{
1405 struct fec_enet_private *fep = netdev_priv(ndev);
1406
1407 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1408 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1409 pause->rx_pause = pause->tx_pause;
1410}
1411
1412static int fec_enet_set_pauseparam(struct net_device *ndev,
1413 struct ethtool_pauseparam *pause)
1414{
1415 struct fec_enet_private *fep = netdev_priv(ndev);
1416
1417 if (pause->tx_pause != pause->rx_pause) {
1418 netdev_info(ndev,
1419 "hardware only support enable/disable both tx and rx");
1420 return -EINVAL;
1421 }
1422
1423 fep->pause_flag = 0;
1424
1425 /* tx pause must be same as rx pause */
1426 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1427 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1428
1429 if (pause->rx_pause || pause->autoneg) {
1430 fep->phy_dev->supported |= ADVERTISED_Pause;
1431 fep->phy_dev->advertising |= ADVERTISED_Pause;
1432 } else {
1433 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1434 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1435 }
1436
1437 if (pause->autoneg) {
1438 if (netif_running(ndev))
1439 fec_stop(ndev);
1440 phy_start_aneg(fep->phy_dev);
1441 }
1442 if (netif_running(ndev))
1443 fec_restart(ndev, 0);
1444
1445 return 0;
1446}
1447
Chris Healy38ae92d2013-06-25 23:18:52 -07001448#ifndef CONFIG_M5272
1449static const struct fec_stat {
1450 char name[ETH_GSTRING_LEN];
1451 u16 offset;
1452} fec_stats[] = {
1453 /* RMON TX */
1454 { "tx_dropped", RMON_T_DROP },
1455 { "tx_packets", RMON_T_PACKETS },
1456 { "tx_broadcast", RMON_T_BC_PKT },
1457 { "tx_multicast", RMON_T_MC_PKT },
1458 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1459 { "tx_undersize", RMON_T_UNDERSIZE },
1460 { "tx_oversize", RMON_T_OVERSIZE },
1461 { "tx_fragment", RMON_T_FRAG },
1462 { "tx_jabber", RMON_T_JAB },
1463 { "tx_collision", RMON_T_COL },
1464 { "tx_64byte", RMON_T_P64 },
1465 { "tx_65to127byte", RMON_T_P65TO127 },
1466 { "tx_128to255byte", RMON_T_P128TO255 },
1467 { "tx_256to511byte", RMON_T_P256TO511 },
1468 { "tx_512to1023byte", RMON_T_P512TO1023 },
1469 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1470 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1471 { "tx_octets", RMON_T_OCTETS },
1472
1473 /* IEEE TX */
1474 { "IEEE_tx_drop", IEEE_T_DROP },
1475 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1476 { "IEEE_tx_1col", IEEE_T_1COL },
1477 { "IEEE_tx_mcol", IEEE_T_MCOL },
1478 { "IEEE_tx_def", IEEE_T_DEF },
1479 { "IEEE_tx_lcol", IEEE_T_LCOL },
1480 { "IEEE_tx_excol", IEEE_T_EXCOL },
1481 { "IEEE_tx_macerr", IEEE_T_MACERR },
1482 { "IEEE_tx_cserr", IEEE_T_CSERR },
1483 { "IEEE_tx_sqe", IEEE_T_SQE },
1484 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1485 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1486
1487 /* RMON RX */
1488 { "rx_packets", RMON_R_PACKETS },
1489 { "rx_broadcast", RMON_R_BC_PKT },
1490 { "rx_multicast", RMON_R_MC_PKT },
1491 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1492 { "rx_undersize", RMON_R_UNDERSIZE },
1493 { "rx_oversize", RMON_R_OVERSIZE },
1494 { "rx_fragment", RMON_R_FRAG },
1495 { "rx_jabber", RMON_R_JAB },
1496 { "rx_64byte", RMON_R_P64 },
1497 { "rx_65to127byte", RMON_R_P65TO127 },
1498 { "rx_128to255byte", RMON_R_P128TO255 },
1499 { "rx_256to511byte", RMON_R_P256TO511 },
1500 { "rx_512to1023byte", RMON_R_P512TO1023 },
1501 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1502 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1503 { "rx_octets", RMON_R_OCTETS },
1504
1505 /* IEEE RX */
1506 { "IEEE_rx_drop", IEEE_R_DROP },
1507 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1508 { "IEEE_rx_crc", IEEE_R_CRC },
1509 { "IEEE_rx_align", IEEE_R_ALIGN },
1510 { "IEEE_rx_macerr", IEEE_R_MACERR },
1511 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1512 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1513};
1514
1515static void fec_enet_get_ethtool_stats(struct net_device *dev,
1516 struct ethtool_stats *stats, u64 *data)
1517{
1518 struct fec_enet_private *fep = netdev_priv(dev);
1519 int i;
1520
1521 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1522 data[i] = readl(fep->hwp + fec_stats[i].offset);
1523}
1524
1525static void fec_enet_get_strings(struct net_device *netdev,
1526 u32 stringset, u8 *data)
1527{
1528 int i;
1529 switch (stringset) {
1530 case ETH_SS_STATS:
1531 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1532 memcpy(data + i * ETH_GSTRING_LEN,
1533 fec_stats[i].name, ETH_GSTRING_LEN);
1534 break;
1535 }
1536}
1537
1538static int fec_enet_get_sset_count(struct net_device *dev, int sset)
1539{
1540 switch (sset) {
1541 case ETH_SS_STATS:
1542 return ARRAY_SIZE(fec_stats);
1543 default:
1544 return -EOPNOTSUPP;
1545 }
1546}
1547#endif
1548
Chris Healy32bc9b42013-06-17 07:25:06 -07001549static int fec_enet_nway_reset(struct net_device *dev)
1550{
1551 struct fec_enet_private *fep = netdev_priv(dev);
1552 struct phy_device *phydev = fep->phy_dev;
1553
1554 if (!phydev)
1555 return -ENODEV;
1556
1557 return genphy_restart_aneg(phydev);
1558}
1559
stephen hemminger9b07be42012-01-04 12:59:49 +00001560static const struct ethtool_ops fec_enet_ethtool_ops = {
Frank Libaa70a52013-01-16 16:55:58 +00001561 .get_pauseparam = fec_enet_get_pauseparam,
1562 .set_pauseparam = fec_enet_set_pauseparam,
Bryan Wue6b043d2010-03-31 02:10:44 +00001563 .get_settings = fec_enet_get_settings,
1564 .set_settings = fec_enet_set_settings,
1565 .get_drvinfo = fec_enet_get_drvinfo,
1566 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00001567 .get_ts_info = fec_enet_get_ts_info,
Chris Healy32bc9b42013-06-17 07:25:06 -07001568 .nway_reset = fec_enet_nway_reset,
Chris Healy38ae92d2013-06-25 23:18:52 -07001569#ifndef CONFIG_M5272
1570 .get_ethtool_stats = fec_enet_get_ethtool_stats,
1571 .get_strings = fec_enet_get_strings,
1572 .get_sset_count = fec_enet_get_sset_count,
1573#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00001574};
1575
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001576static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001577{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001578 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001579 struct phy_device *phydev = fep->phy_dev;
1580
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001581 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001582 return -EINVAL;
1583
1584 if (!phydev)
1585 return -ENODEV;
1586
Frank Liff43da82013-01-03 16:04:23 +00001587 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
Frank Li6605b732012-10-30 18:25:31 +00001588 return fec_ptp_ioctl(ndev, rq, cmd);
Frank Liff43da82013-01-03 16:04:23 +00001589
Richard Cochran28b04112010-07-17 08:48:55 +00001590 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001593static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001594{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001595 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001596 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001597 struct sk_buff *skb;
1598 struct bufdesc *bdp;
1599
1600 bdp = fep->rx_bd_base;
1601 for (i = 0; i < RX_RING_SIZE; i++) {
1602 skb = fep->rx_skbuff[i];
1603
1604 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001605 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001606 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1607 if (skb)
1608 dev_kfree_skb(skb);
Frank Liff43da82013-01-03 16:04:23 +00001609 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001610 }
1611
1612 bdp = fep->tx_bd_base;
1613 for (i = 0; i < TX_RING_SIZE; i++)
1614 kfree(fep->tx_bounce[i]);
1615}
1616
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001617static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001618{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001619 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001620 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001621 struct sk_buff *skb;
1622 struct bufdesc *bdp;
1623
1624 bdp = fep->rx_bd_base;
1625 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001626 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001627 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001628 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001629 return -ENOMEM;
1630 }
1631 fep->rx_skbuff[i] = skb;
1632
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001633 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001634 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1635 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00001636
1637 if (fep->bufdesc_ex) {
1638 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1639 ebdp->cbd_esc = BD_ENET_RX_INT;
1640 }
1641
1642 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001643 }
1644
1645 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001646 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001647 bdp->cbd_sc |= BD_SC_WRAP;
1648
1649 bdp = fep->tx_bd_base;
1650 for (i = 0; i < TX_RING_SIZE; i++) {
1651 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1652
1653 bdp->cbd_sc = 0;
1654 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001655
Frank Liff43da82013-01-03 16:04:23 +00001656 if (fep->bufdesc_ex) {
1657 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00001658 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00001659 }
1660
1661 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001662 }
1663
1664 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001665 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001666 bdp->cbd_sc |= BD_SC_WRAP;
1667
1668 return 0;
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001672fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001674 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001675 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Frank Lidc975382013-01-28 18:31:42 +00001677 napi_enable(&fep->napi);
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* I should reset the ring buffers here, but I don't yet know
1680 * a simple way to do that.
1681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001683 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001684 if (ret)
1685 return ret;
1686
Bryan Wu418bd0d2010-05-28 03:40:39 -07001687 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001688 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001689 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001690 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001691 return ret;
1692 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001693 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001694 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001700fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001702 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Sascha Hauer22f6b862009-04-15 01:32:18 +00001704 /* Don't know what to do yet. */
Georg Hofmann3f104c32013-03-14 06:54:09 +00001705 napi_disable(&fep->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001707 netif_stop_queue(ndev);
1708 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001710 if (fep->phy_dev) {
1711 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001712 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001713 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001714
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001715 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720/* Set or clear the multicast filter for this adaptor.
1721 * Skeleton taken from sunlance driver.
1722 * The CPM Ethernet implementation allows Multicast as well as individual
1723 * MAC address filtering. Some of the drivers check to make sure it is
1724 * a group multicast address, and discard those that are not. I guess I
1725 * will do the same for now, but just remove the test if you want
1726 * individual filtering as well (do the upper net layers want or support
1727 * this kind of feature?).
1728 */
1729
1730#define HASH_BITS 6 /* #bits in hash */
1731#define CRC32_POLY 0xEDB88320
1732
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001733static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001735 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001736 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001737 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 unsigned char hash;
1739
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001740 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001741 tmp = readl(fep->hwp + FEC_R_CNTRL);
1742 tmp |= 0x8;
1743 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001744 return;
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Sascha Hauer4e831832009-04-15 01:32:19 +00001747 tmp = readl(fep->hwp + FEC_R_CNTRL);
1748 tmp &= ~0x8;
1749 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001750
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001751 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001752 /* Catch all multicast addresses, so set the
1753 * filter to all 1's
1754 */
1755 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1756 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Sascha Hauer4e831832009-04-15 01:32:19 +00001758 return;
1759 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001760
Sascha Hauer4e831832009-04-15 01:32:19 +00001761 /* Clear filter and add the addresses in hash register
1762 */
1763 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1764 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001766 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001767 /* calculate crc32 value of mac address */
1768 crc = 0xffffffff;
1769
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001770 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001771 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001772 for (bit = 0; bit < 8; bit++, data >>= 1) {
1773 crc = (crc >> 1) ^
1774 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001777
1778 /* only upper 6 bits (HASH_BITS) are used
1779 * which point to specific bit in he hash registers
1780 */
1781 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1782
1783 if (hash > 31) {
1784 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1785 tmp |= 1 << (hash - 32);
1786 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1787 } else {
1788 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1789 tmp |= 1 << hash;
1790 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793}
1794
Sascha Hauer22f6b862009-04-15 01:32:18 +00001795/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001796static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001797fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001799 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001800 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Sascha Hauer009fda82009-04-15 01:32:23 +00001802 if (!is_valid_ether_addr(addr->sa_data))
1803 return -EADDRNOTAVAIL;
1804
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001805 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001806
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001807 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1808 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001809 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001810 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001811 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001815#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001816/**
1817 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001818 * @dev: The FEC network adapter
1819 *
1820 * Polled functionality used by netconsole and others in non interrupt mode
1821 *
1822 */
Wei Yongjun47a52472013-03-20 05:06:11 +00001823static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001824{
1825 int i;
1826 struct fec_enet_private *fep = netdev_priv(dev);
1827
1828 for (i = 0; i < FEC_IRQ_NUM; i++) {
1829 if (fep->irq[i] > 0) {
1830 disable_irq(fep->irq[i]);
1831 fec_enet_interrupt(fep->irq[i], dev);
1832 enable_irq(fep->irq[i]);
1833 }
1834 }
1835}
1836#endif
1837
Jim Baxter4c09eed2013-04-19 08:10:49 +00001838static int fec_set_features(struct net_device *netdev,
1839 netdev_features_t features)
1840{
1841 struct fec_enet_private *fep = netdev_priv(netdev);
1842 netdev_features_t changed = features ^ netdev->features;
1843
1844 netdev->features = features;
1845
1846 /* Receive checksum has been changed */
1847 if (changed & NETIF_F_RXCSUM) {
1848 if (features & NETIF_F_RXCSUM)
1849 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1850 else
1851 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
1852
1853 if (netif_running(netdev)) {
1854 fec_stop(netdev);
1855 fec_restart(netdev, fep->phy_dev->duplex);
1856 netif_wake_queue(netdev);
1857 } else {
1858 fec_restart(netdev, fep->phy_dev->duplex);
1859 }
1860 }
1861
1862 return 0;
1863}
1864
Sascha Hauer009fda82009-04-15 01:32:23 +00001865static const struct net_device_ops fec_netdev_ops = {
1866 .ndo_open = fec_enet_open,
1867 .ndo_stop = fec_enet_close,
1868 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001869 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001870 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001871 .ndo_validate_addr = eth_validate_addr,
1872 .ndo_tx_timeout = fec_timeout,
1873 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001874 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001875#ifdef CONFIG_NET_POLL_CONTROLLER
1876 .ndo_poll_controller = fec_poll_controller,
1877#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001878 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00001879};
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /*
1882 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001883 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001885static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001887 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00001888 const struct platform_device_id *id_entry =
1889 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001890 struct bufdesc *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001892 /* Allocate memory for buffer descriptors. */
1893 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001894 GFP_KERNEL);
1895 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001896 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10001897
Frank Li14109a52013-03-26 16:12:03 +00001898 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001899
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001900 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Shawn Guo49da97d2011-01-05 21:13:11 +00001902 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001903 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001905 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 fep->rx_bd_base = cbd_base;
Frank Liff43da82013-01-03 16:04:23 +00001907 if (fep->bufdesc_ex)
1908 fep->tx_bd_base = (struct bufdesc *)
1909 (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1910 else
1911 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Sascha Hauer22f6b862009-04-15 01:32:18 +00001913 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001914 ndev->watchdog_timeo = TX_TIMEOUT;
1915 ndev->netdev_ops = &fec_netdev_ops;
1916 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001917
Frank Lidc975382013-01-28 18:31:42 +00001918 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1919 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1920
Shawn Guo48496252013-05-08 21:08:22 +00001921 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
1922 /* enable hw accelerator */
1923 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1924 | NETIF_F_RXCSUM);
1925 ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1926 | NETIF_F_RXCSUM);
1927 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1928 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00001929
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001930 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933}
1934
Shawn Guoca2cc332011-06-25 02:04:35 +08001935#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05001936static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001937{
1938 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001939 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001940 struct device_node *np = pdev->dev.of_node;
1941
1942 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001943 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001944
Shawn Guoa3caad02012-06-27 03:45:24 +00001945 of_property_read_u32(np, "phy-reset-duration", &msec);
1946 /* A sane reset duration should not be longer than 1s */
1947 if (msec > 1000)
1948 msec = 1;
1949
Shawn Guoca2cc332011-06-25 02:04:35 +08001950 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001951 if (!gpio_is_valid(phy_reset))
1952 return;
1953
Shawn Guo119fc002012-06-27 03:45:22 +00001954 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1955 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001956 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001957 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001958 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001959 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001960 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001961 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001962}
1963#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001964static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001965{
1966 /*
1967 * In case of platform probe, the reset has been done
1968 * by machine code.
1969 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001970}
1971#endif /* CONFIG_OF */
1972
Bill Pemberton33897cc2012-12-03 09:23:58 -05001973static int
Sascha Haueread73182009-01-28 23:03:11 +00001974fec_probe(struct platform_device *pdev)
1975{
1976 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001977 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001978 struct net_device *ndev;
1979 int i, irq, ret = 0;
1980 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001981 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001982 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08001983
1984 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1985 if (of_id)
1986 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001987
1988 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1989 if (!r)
1990 return -ENXIO;
1991
Sascha Haueread73182009-01-28 23:03:11 +00001992 /* Init network device */
1993 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00001994 if (!ndev)
1995 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00001996
1997 SET_NETDEV_DEV(ndev, &pdev->dev);
1998
1999 /* setup board info structure */
2000 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002001
Frank Libaa70a52013-01-16 16:55:58 +00002002 /* default enable pause frame auto negotiation */
2003 if (pdev->id_entry &&
2004 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2005 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
2006
Tushar Behera941e1732013-06-10 17:05:05 +05302007 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2008 if (IS_ERR(fep->hwp)) {
2009 ret = PTR_ERR(fep->hwp);
2010 goto failed_ioremap;
2011 }
2012
Bryan Wue6b043d2010-03-31 02:10:44 +00002013 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002014 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002015
Frank Liff43da82013-01-03 16:04:23 +00002016 fep->bufdesc_ex = 0;
2017
Sascha Haueread73182009-01-28 23:03:11 +00002018 platform_set_drvdata(pdev, ndev);
2019
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002020 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002021 if (ret < 0) {
2022 pdata = pdev->dev.platform_data;
2023 if (pdata)
2024 fep->phy_interface = pdata->phy;
2025 else
2026 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2027 } else {
2028 fep->phy_interface = ret;
2029 }
2030
Fabio Estevame2f8d552013-02-22 06:40:45 +00002031 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2032 if (IS_ERR(fep->clk_ipg)) {
2033 ret = PTR_ERR(fep->clk_ipg);
2034 goto failed_clk;
2035 }
2036
2037 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2038 if (IS_ERR(fep->clk_ahb)) {
2039 ret = PTR_ERR(fep->clk_ahb);
2040 goto failed_clk;
2041 }
2042
Linus Torvalds38f56f32013-05-07 11:06:17 -07002043 /* enet_out is optional, depends on board */
2044 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2045 if (IS_ERR(fep->clk_enet_out))
2046 fep->clk_enet_out = NULL;
2047
Fabio Estevame2f8d552013-02-22 06:40:45 +00002048 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002049 fep->bufdesc_ex =
2050 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002051 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002052 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002053 fep->bufdesc_ex = 0;
2054 }
2055
2056 clk_prepare_enable(fep->clk_ahb);
2057 clk_prepare_enable(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002058 clk_prepare_enable(fep->clk_enet_out);
2059 clk_prepare_enable(fep->clk_ptp);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002060
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002061 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2062 if (!IS_ERR(fep->reg_phy)) {
2063 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002064 if (ret) {
2065 dev_err(&pdev->dev,
2066 "Failed to enable phy regulator: %d\n", ret);
2067 goto failed_regulator;
2068 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002069 } else {
2070 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002071 }
2072
2073 fec_reset_phy(pdev);
2074
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002075 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002076 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002077
2078 ret = fec_enet_init(ndev);
2079 if (ret)
2080 goto failed_init;
2081
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002082 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002083 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002084 if (irq < 0) {
2085 if (i)
2086 break;
2087 ret = irq;
2088 goto failed_irq;
2089 }
Sascha Haueread73182009-01-28 23:03:11 +00002090 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
2091 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01002092 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00002093 irq = platform_get_irq(pdev, i);
2094 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002095 }
2096 goto failed_irq;
2097 }
2098 }
2099
Bryan Wue6b043d2010-03-31 02:10:44 +00002100 ret = fec_enet_mii_init(pdev);
2101 if (ret)
2102 goto failed_mii_init;
2103
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002104 /* Carrier starts down, phylib will bring it up */
2105 netif_carrier_off(ndev);
2106
Sascha Haueread73182009-01-28 23:03:11 +00002107 ret = register_netdev(ndev);
2108 if (ret)
2109 goto failed_register;
2110
Fabio Estevameb1d0642013-04-13 07:25:36 +00002111 if (fep->bufdesc_ex && fep->ptp_clock)
2112 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2113
Frank Li54309fa2013-05-07 14:08:44 +00002114 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00002115 return 0;
2116
2117failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002118 fec_enet_mii_remove(fep);
2119failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002120failed_irq:
Fabio Estevame2f8d552013-02-22 06:40:45 +00002121 for (i = 0; i < FEC_IRQ_NUM; i++) {
2122 irq = platform_get_irq(pdev, i);
2123 if (irq > 0)
2124 free_irq(irq, ndev);
2125 }
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002126failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002127 if (fep->reg_phy)
2128 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002129failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002130 clk_disable_unprepare(fep->clk_ahb);
2131 clk_disable_unprepare(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002132 clk_disable_unprepare(fep->clk_enet_out);
2133 clk_disable_unprepare(fep->clk_ptp);
Sascha Haueread73182009-01-28 23:03:11 +00002134failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002135failed_ioremap:
2136 free_netdev(ndev);
2137
2138 return ret;
2139}
2140
Bill Pemberton33897cc2012-12-03 09:23:58 -05002141static int
Sascha Haueread73182009-01-28 23:03:11 +00002142fec_drv_remove(struct platform_device *pdev)
2143{
2144 struct net_device *ndev = platform_get_drvdata(pdev);
2145 struct fec_enet_private *fep = netdev_priv(ndev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002146 int i;
Sascha Haueread73182009-01-28 23:03:11 +00002147
Frank Li54309fa2013-05-07 14:08:44 +00002148 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002149 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002150 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002151 del_timer_sync(&fep->time_keep);
Fabio Estevamc55284e2013-05-27 03:48:32 +00002152 for (i = 0; i < FEC_IRQ_NUM; i++) {
2153 int irq = platform_get_irq(pdev, i);
2154 if (irq > 0)
2155 free_irq(irq, ndev);
2156 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002157 if (fep->reg_phy)
2158 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002159 clk_disable_unprepare(fep->clk_ptp);
2160 if (fep->ptp_clock)
2161 ptp_clock_unregister(fep->ptp_clock);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002162 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002163 clk_disable_unprepare(fep->clk_ahb);
2164 clk_disable_unprepare(fep->clk_ipg);
Sascha Haueread73182009-01-28 23:03:11 +00002165 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002166
Sascha Haueread73182009-01-28 23:03:11 +00002167 return 0;
2168}
2169
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002170#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002171static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002172fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002173{
Eric Benard87cad5c2010-06-18 04:19:54 +00002174 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002175 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002176
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002177 if (netif_running(ndev)) {
2178 fec_stop(ndev);
2179 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002180 }
Linus Torvalds38f56f32013-05-07 11:06:17 -07002181 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002182 clk_disable_unprepare(fep->clk_ahb);
2183 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002184
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002185 if (fep->reg_phy)
2186 regulator_disable(fep->reg_phy);
2187
Sascha Haueread73182009-01-28 23:03:11 +00002188 return 0;
2189}
2190
2191static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002192fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002193{
Eric Benard87cad5c2010-06-18 04:19:54 +00002194 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002195 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002196 int ret;
2197
2198 if (fep->reg_phy) {
2199 ret = regulator_enable(fep->reg_phy);
2200 if (ret)
2201 return ret;
2202 }
Sascha Haueread73182009-01-28 23:03:11 +00002203
Linus Torvalds38f56f32013-05-07 11:06:17 -07002204 clk_prepare_enable(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002205 clk_prepare_enable(fep->clk_ahb);
2206 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002207 if (netif_running(ndev)) {
2208 fec_restart(ndev, fep->full_duplex);
2209 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002210 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002211
Sascha Haueread73182009-01-28 23:03:11 +00002212 return 0;
2213}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002214#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002215
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002216static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002217
Sascha Haueread73182009-01-28 23:03:11 +00002218static struct platform_driver fec_driver = {
2219 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002220 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002221 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002222 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002223 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002224 },
Shawn Guob5680e02011-01-05 21:13:13 +00002225 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002226 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002227 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002228};
2229
Fabio Estevamaaca2372012-01-23 16:44:50 +00002230module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232MODULE_LICENSE("GPL");