blob: 866b92220c151ccbe7a9f681ab8bef93fa4a8136 [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
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010062#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000063#define FEC_ALIGNMENT 0xf
64#else
65#define FEC_ALIGNMENT 0x3
66#endif
67
Shawn Guob5680e02011-01-05 21:13:13 +000068#define DRIVER_NAME "fec"
Frank Lidc975382013-01-28 18:31:42 +000069#define FEC_NAPI_WEIGHT 64
Shawn Guob5680e02011-01-05 21:13:13 +000070
Frank Libaa70a52013-01-16 16:55:58 +000071/* Pause frame feild and FIFO threshold */
72#define FEC_ENET_FCE (1 << 5)
73#define FEC_ENET_RSEM_V 0x84
74#define FEC_ENET_RSFL_V 16
75#define FEC_ENET_RAEM_V 0x8
76#define FEC_ENET_RAFL_V 0x8
77#define FEC_ENET_OPD_V 0xFFF0
78
Shawn Guob5680e02011-01-05 21:13:13 +000079/* Controller is ENET-MAC */
80#define FEC_QUIRK_ENET_MAC (1 << 0)
81/* Controller needs driver to swap frame */
82#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080083/* Controller uses gasket */
84#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000085/* Controller has GBIT support */
86#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000087/* Controller has extend desc buffer */
88#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000089/* Controller has hardware checksum support */
90#define FEC_QUIRK_HAS_CSUM (1 << 5)
Shawn Guob5680e02011-01-05 21:13:13 +000091
92static struct platform_device_id fec_devtype[] = {
93 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080094 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000095 .name = DRIVER_NAME,
96 .driver_data = 0,
97 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080098 .name = "imx25-fec",
99 .driver_data = FEC_QUIRK_USE_GASKET,
100 }, {
101 .name = "imx27-fec",
102 .driver_data = 0,
103 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000104 .name = "imx28-fec",
105 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800106 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000107 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000108 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Shawn Guo48496252013-05-08 21:08:22 +0000109 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM,
Shawn Guo230dec62011-09-23 02:12:48 +0000110 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000111 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000112 .driver_data = FEC_QUIRK_ENET_MAC,
113 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800114 /* sentinel */
115 }
Shawn Guob5680e02011-01-05 21:13:13 +0000116};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800117MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000118
Shawn Guoca2cc332011-06-25 02:04:35 +0800119enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000120 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800121 IMX27_FEC, /* runs on i.mx27/35/51 */
122 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000123 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000124 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800125};
126
127static const struct of_device_id fec_dt_ids[] = {
128 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
129 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
130 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000131 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000132 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800133 { /* sentinel */ }
134};
135MODULE_DEVICE_TABLE(of, fec_dt_ids);
136
Shawn Guo49da97d2011-01-05 21:13:11 +0000137static unsigned char macaddr[ETH_ALEN];
138module_param_array(macaddr, byte, NULL, 0);
139MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
140
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000141#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Some hardware gets it MAC address out of local flash memory.
144 * if this is non-zero then assume it is the address to get MAC from.
145 */
146#if defined(CONFIG_NETtel)
147#define FEC_FLASHMAC 0xf0006006
148#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
149#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#elif defined(CONFIG_CANCam)
151#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000152#elif defined (CONFIG_M5272C3)
153#define FEC_FLASHMAC (0xffe04000 + 4)
154#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000155#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#else
157#define FEC_FLASHMAC 0
158#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800159#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000160
Frank Liff43da82013-01-03 16:04:23 +0000161#if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000162#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000163#endif
164
Sascha Hauer22f6b862009-04-15 01:32:18 +0000165/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
167#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
168#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
169#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
170#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
171#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
172#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
173#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
174#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
175#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
176
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000177#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000178#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* The FEC stores dest/src/type, data, and checksum for receive packets.
181 */
182#define PKT_MAXBUF_SIZE 1518
183#define PKT_MINBUF_SIZE 64
184#define PKT_MAXBLR_SIZE 1520
185
Jim Baxter4c09eed2013-04-19 08:10:49 +0000186/* FEC receive acceleration */
187#define FEC_RACC_IPDIS (1 << 1)
188#define FEC_RACC_PRODIS (1 << 2)
189#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Matt Waddel6b265292006-06-27 13:10:56 +1000192 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * size bits. Other FEC hardware does not, so we need to take that into
194 * account when setting it.
195 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000196#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100197 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
199#else
200#define OPT_FRAME_SIZE 0
201#endif
202
Bryan Wue6b043d2010-03-31 02:10:44 +0000203/* FEC MII MMFR bits definition */
204#define FEC_MMFR_ST (1 << 30)
205#define FEC_MMFR_OP_READ (2 << 28)
206#define FEC_MMFR_OP_WRITE (1 << 28)
207#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
208#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
209#define FEC_MMFR_TA (2 << 16)
210#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500212#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Sascha Hauer22f6b862009-04-15 01:32:18 +0000214/* Transmitter timeout */
215#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Frank Libaa70a52013-01-16 16:55:58 +0000217#define FEC_PAUSE_FLAG_AUTONEG 0x1
218#define FEC_PAUSE_FLAG_ENABLE 0x2
219
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000220static int mii_cnt;
221
Frank Liff43da82013-01-03 16:04:23 +0000222static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex)
223{
224 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
225 if (is_ex)
226 return (struct bufdesc *)(ex + 1);
227 else
228 return bdp + 1;
229}
230
231static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex)
232{
233 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
234 if (is_ex)
235 return (struct bufdesc *)(ex - 1);
236 else
237 return bdp - 1;
238}
239
Shawn Guob5680e02011-01-05 21:13:13 +0000240static void *swap_buffer(void *bufaddr, int len)
241{
242 int i;
243 unsigned int *buf = bufaddr;
244
Fabio Estevamffed61e2013-05-21 05:44:26 +0000245 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000246 *buf = cpu_to_be32(*buf);
247
248 return bufaddr;
249}
250
Jim Baxter4c09eed2013-04-19 08:10:49 +0000251static int
252fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
253{
254 /* Only run for packets requiring a checksum. */
255 if (skb->ip_summed != CHECKSUM_PARTIAL)
256 return 0;
257
258 if (unlikely(skb_cow_head(skb, 0)))
259 return -1;
260
261 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
262
263 return 0;
264}
265
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000266static netdev_tx_t
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100267fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100269 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000270 const struct platform_device_id *id_entry =
271 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000272 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000273 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000274 unsigned short status;
Frank Lide5fb0a2013-03-03 17:34:25 +0000275 unsigned int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (!fep->link) {
Jim Baxter4c09eed2013-04-19 08:10:49 +0000278 /* Link is down or auto-negotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000279 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
282 /* Fill in a Tx ring entry */
283 bdp = fep->cur_tx;
284
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000285 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000286
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000287 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Ooops. All transmit buffers are full. Bail out.
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100289 * This should not happen, since ndev->tbusy should be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Joe Perches31b77202013-04-13 19:03:17 +0000291 netdev_err(ndev, "tx queue full!\n");
Patrick McHardy5b548142009-06-12 06:22:29 +0000292 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Jim Baxter4c09eed2013-04-19 08:10:49 +0000295 /* Protocol checksum off-load for TCP and UDP. */
296 if (fec_enet_clear_csum(skb, ndev)) {
297 kfree_skb(skb);
298 return NETDEV_TX_OK;
299 }
300
Sascha Hauer22f6b862009-04-15 01:32:18 +0000301 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000302 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Sascha Hauer22f6b862009-04-15 01:32:18 +0000304 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000305 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 bdp->cbd_datlen = skb->len;
307
308 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000309 * On some FEC implementations data must be aligned on
310 * 4-byte boundaries. Use bounce buffers to copy data
311 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000313 if (fep->bufdesc_ex)
314 index = (struct bufdesc_ex *)bdp -
315 (struct bufdesc_ex *)fep->tx_bd_base;
316 else
317 index = bdp - fep->tx_bd_base;
318
Greg Ungerer9555b312009-08-06 17:58:18 +0000319 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Uwe Kleine-König8a73b0b2011-01-13 21:34:31 +0100320 memcpy(fep->tx_bounce[index], skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000321 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Shawn Guob5680e02011-01-05 21:13:13 +0000324 /*
325 * Some design made an incorrect assumption on endian mode of
326 * the system that it's running on. As the result, driver has to
327 * swap every frame going to and coming from the controller.
328 */
329 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
330 swap_buffer(bufaddr, skb->len);
331
Sascha Hauer22f6b862009-04-15 01:32:18 +0000332 /* Save skb pointer */
Frank Lide5fb0a2013-03-03 17:34:25 +0000333 fep->tx_skbuff[index] = skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Push the data cache so the CPM does not get stale memory
336 * data.
337 */
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100338 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000339 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000341 /* Send it on its way. Tell FEC it's ready, interrupt when done,
342 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000344 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000346 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Frank Liff43da82013-01-03 16:04:23 +0000348 if (fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000349
Frank Liff43da82013-01-03 16:04:23 +0000350 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
351 ebdp->cbd_bdu = 0;
352 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
353 fep->hwts_tx_en)) {
354 ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
355 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
356 } else {
Frank Liff43da82013-01-03 16:04:23 +0000357 ebdp->cbd_esc = BD_ENET_TX_INT;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000358
359 /* Enable protocol checksum flags
360 * We do not bother with the IP Checksum bits as they
361 * are done by the kernel
362 */
363 if (skb->ip_summed == CHECKSUM_PARTIAL)
364 ebdp->cbd_esc |= BD_ENET_TX_PINS;
Frank Liff43da82013-01-03 16:04:23 +0000365 }
Frank Li6605b732012-10-30 18:25:31 +0000366 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000367 /* If this was the last BD in the ring, start at the beginning again. */
368 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000370 else
Frank Liff43da82013-01-03 16:04:23 +0000371 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Sascha Hauer2e285322009-04-15 01:32:16 +0000373 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Frank Lide5fb0a2013-03-03 17:34:25 +0000375 if (fep->cur_tx == fep->dirty_tx)
376 netif_stop_queue(ndev);
Richard Cochran18a03b92011-06-12 02:18:59 +0000377
Frank Lide5fb0a2013-03-03 17:34:25 +0000378 /* Trigger transmission start */
379 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
380
381 skb_tx_timestamp(skb);
Richard Cochrana0087a32011-06-19 03:31:40 +0000382
Patrick McHardy6ed10652009-06-23 06:03:08 +0000383 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Frank Li14109a52013-03-26 16:12:03 +0000386/* Init RX & TX buffer descriptors
387 */
388static void fec_enet_bd_init(struct net_device *dev)
389{
390 struct fec_enet_private *fep = netdev_priv(dev);
391 struct bufdesc *bdp;
392 unsigned int i;
393
394 /* Initialize the receive buffer descriptors. */
395 bdp = fep->rx_bd_base;
396 for (i = 0; i < RX_RING_SIZE; i++) {
397
398 /* Initialize the BD for every fragment in the page. */
399 if (bdp->cbd_bufaddr)
400 bdp->cbd_sc = BD_ENET_RX_EMPTY;
401 else
402 bdp->cbd_sc = 0;
403 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
404 }
405
406 /* Set the last buffer to wrap */
407 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
408 bdp->cbd_sc |= BD_SC_WRAP;
409
410 fep->cur_rx = fep->rx_bd_base;
411
412 /* ...and the same for transmit */
413 bdp = fep->tx_bd_base;
414 fep->cur_tx = bdp;
415 for (i = 0; i < TX_RING_SIZE; i++) {
416
417 /* Initialize the BD for every fragment in the page. */
418 bdp->cbd_sc = 0;
419 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
420 dev_kfree_skb_any(fep->tx_skbuff[i]);
421 fep->tx_skbuff[i] = NULL;
422 }
423 bdp->cbd_bufaddr = 0;
424 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
425 }
426
427 /* Set the last buffer to wrap */
428 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
429 bdp->cbd_sc |= BD_SC_WRAP;
430 fep->dirty_tx = bdp;
431}
432
Uwe Kleine-König45993652011-01-19 20:47:04 +0100433/* This function is called to start or restart the FEC during a link
434 * change. This only happens when switching between half and full
435 * duplex.
436 */
437static void
438fec_restart(struct net_device *ndev, int duplex)
439{
440 struct fec_enet_private *fep = netdev_priv(ndev);
441 const struct platform_device_id *id_entry =
442 platform_get_device_id(fep->pdev);
443 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000444 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100445 u32 temp_mac[2];
446 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000447 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100448
Frank Li54309fa2013-05-07 14:08:44 +0000449 if (netif_running(ndev)) {
450 netif_device_detach(ndev);
451 napi_disable(&fep->napi);
452 netif_stop_queue(ndev);
Fabio Estevam31691342013-05-15 07:06:26 +0000453 netif_tx_lock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000454 }
455
Uwe Kleine-König45993652011-01-19 20:47:04 +0100456 /* Whack a reset. We should wait for this. */
457 writel(1, fep->hwp + FEC_ECNTRL);
458 udelay(10);
459
460 /*
461 * enet-mac reset will reset mac address registers too,
462 * so need to reconfigure it.
463 */
464 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
465 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
466 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
467 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
468 }
469
470 /* Clear any outstanding interrupt. */
471 writel(0xffc00000, fep->hwp + FEC_IEVENT);
472
473 /* Reset all multicast. */
474 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
475 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
476#ifndef CONFIG_M5272
477 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
478 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
479#endif
480
481 /* Set maximum receive buffer size. */
482 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
483
Frank Li14109a52013-03-26 16:12:03 +0000484 fec_enet_bd_init(ndev);
485
Uwe Kleine-König45993652011-01-19 20:47:04 +0100486 /* Set receive and transmit descriptor base. */
487 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000488 if (fep->bufdesc_ex)
489 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
490 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
491 else
492 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
493 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100494
Uwe Kleine-König45993652011-01-19 20:47:04 +0100495
Uwe Kleine-König45993652011-01-19 20:47:04 +0100496 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
497 if (fep->tx_skbuff[i]) {
498 dev_kfree_skb_any(fep->tx_skbuff[i]);
499 fep->tx_skbuff[i] = NULL;
500 }
501 }
502
503 /* Enable MII mode */
504 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100505 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100506 writel(0x04, fep->hwp + FEC_X_CNTRL);
507 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100508 /* No Rcv on Xmit */
509 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100510 writel(0x0, fep->hwp + FEC_X_CNTRL);
511 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100512
Uwe Kleine-König45993652011-01-19 20:47:04 +0100513 fep->full_duplex = duplex;
514
515 /* Set MII speed */
516 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
517
Jim Baxter4c09eed2013-04-19 08:10:49 +0000518 /* set RX checksum */
519 val = readl(fep->hwp + FEC_RACC);
520 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
521 val |= FEC_RACC_OPTIONS;
522 else
523 val &= ~FEC_RACC_OPTIONS;
524 writel(val, fep->hwp + FEC_RACC);
525
Uwe Kleine-König45993652011-01-19 20:47:04 +0100526 /*
527 * The phy interface and speed need to get configured
528 * differently on enet-mac.
529 */
530 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100531 /* Enable flow control and length check */
532 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100533
Shawn Guo230dec62011-09-23 02:12:48 +0000534 /* RGMII, RMII or MII */
535 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
536 rcntl |= (1 << 6);
537 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100538 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100539 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100540 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100541
Shawn Guo230dec62011-09-23 02:12:48 +0000542 /* 1G, 100M or 10M */
543 if (fep->phy_dev) {
544 if (fep->phy_dev->speed == SPEED_1000)
545 ecntl |= (1 << 5);
546 else if (fep->phy_dev->speed == SPEED_100)
547 rcntl &= ~(1 << 9);
548 else
549 rcntl |= (1 << 9);
550 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100551 } else {
552#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800553 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000554 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100555 /* disable the gasket and wait */
556 writel(0, fep->hwp + FEC_MIIGSK_ENR);
557 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
558 udelay(1);
559
560 /*
561 * configure the gasket:
562 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800563 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100564 */
Eric Benard8d82f212012-01-12 06:10:28 +0000565 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
566 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
567 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
568 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
569 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100570
571 /* re-enable the gasket */
572 writel(2, fep->hwp + FEC_MIIGSK_ENR);
573 }
574#endif
575 }
Frank Libaa70a52013-01-16 16:55:58 +0000576
577 /* enable pause frame*/
578 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
579 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
580 fep->phy_dev && fep->phy_dev->pause)) {
581 rcntl |= FEC_ENET_FCE;
582
Jim Baxter4c09eed2013-04-19 08:10:49 +0000583 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000584 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
585 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
586 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
587 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
588
589 /* OPD */
590 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
591 } else {
592 rcntl &= ~FEC_ENET_FCE;
593 }
594
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100595 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100596
Shawn Guo230dec62011-09-23 02:12:48 +0000597 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
598 /* enable ENET endian swap */
599 ecntl |= (1 << 8);
600 /* enable ENET store and forward mode */
601 writel(1 << 8, fep->hwp + FEC_X_WMRK);
602 }
603
Frank Liff43da82013-01-03 16:04:23 +0000604 if (fep->bufdesc_ex)
605 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000606
Uwe Kleine-König45993652011-01-19 20:47:04 +0100607 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000608 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100609 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
610
Frank Liff43da82013-01-03 16:04:23 +0000611 if (fep->bufdesc_ex)
612 fec_ptp_start_cyclecounter(ndev);
613
Uwe Kleine-König45993652011-01-19 20:47:04 +0100614 /* Enable interrupts we wish to service */
615 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Frank Li54309fa2013-05-07 14:08:44 +0000616
617 if (netif_running(ndev)) {
Fabio Estevam31691342013-05-15 07:06:26 +0000618 netif_tx_unlock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000619 netif_wake_queue(ndev);
Fabio Estevam1ed0d562013-05-15 07:06:27 +0000620 napi_enable(&fep->napi);
621 netif_device_attach(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000622 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100623}
624
625static void
626fec_stop(struct net_device *ndev)
627{
628 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000629 const struct platform_device_id *id_entry =
630 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000631 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100632
633 /* We cannot expect a graceful transmit stop without link !!! */
634 if (fep->link) {
635 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
636 udelay(10);
637 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000638 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +0100639 }
640
641 /* Whack a reset. We should wait for this. */
642 writel(1, fep->hwp + FEC_ECNTRL);
643 udelay(10);
644 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
645 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000646
647 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000648 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000649 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000650 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
651 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100652}
653
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100656fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100658 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100660 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Frank Li54309fa2013-05-07 14:08:44 +0000662 fep->delay_work.timeout = true;
663 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
664}
665
666static void fec_enet_work(struct work_struct *work)
667{
668 struct fec_enet_private *fep =
669 container_of(work,
670 struct fec_enet_private,
671 delay_work.delay_work.work);
672
673 if (fep->delay_work.timeout) {
674 fep->delay_work.timeout = false;
675 fec_restart(fep->netdev, fep->full_duplex);
676 netif_wake_queue(fep->netdev);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100681fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000684 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000685 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +0000687 int index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100689 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 bdp = fep->dirty_tx;
691
Frank Lide5fb0a2013-03-03 17:34:25 +0000692 /* get next bdp of dirty_tx */
693 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
694 bdp = fep->tx_bd_base;
695 else
696 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
697
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000698 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +0000699
700 /* current queue is empty */
701 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000702 break;
703
Frank Lide5fb0a2013-03-03 17:34:25 +0000704 if (fep->bufdesc_ex)
705 index = (struct bufdesc_ex *)bdp -
706 (struct bufdesc_ex *)fep->tx_bd_base;
707 else
708 index = bdp - fep->tx_bd_base;
709
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100710 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
711 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000712 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Frank Lide5fb0a2013-03-03 17:34:25 +0000714 skb = fep->tx_skbuff[index];
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000717 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 BD_ENET_TX_RL | BD_ENET_TX_UN |
719 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100720 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000721 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100722 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000723 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100724 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000725 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100726 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000727 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100728 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000729 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100730 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100732 ndev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Frank Liff43da82013-01-03 16:04:23 +0000735 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
736 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000737 struct skb_shared_hwtstamps shhwtstamps;
738 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000739 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000740
741 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
742 spin_lock_irqsave(&fep->tmreg_lock, flags);
743 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000744 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000745 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
746 skb_tstamp_tx(skb, &shhwtstamps);
747 }
Frank Liff43da82013-01-03 16:04:23 +0000748
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000749 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +0000750 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Deferred means some collisions occurred during transmit,
753 * but we eventually sent the packet OK.
754 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000755 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100756 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400757
Sascha Hauer22f6b862009-04-15 01:32:18 +0000758 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +0000760 fep->tx_skbuff[index] = NULL;
761
762 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400763
Sascha Hauer22f6b862009-04-15 01:32:18 +0000764 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000765 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 bdp = fep->tx_bd_base;
767 else
Frank Liff43da82013-01-03 16:04:23 +0000768 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400769
Sascha Hauer22f6b862009-04-15 01:32:18 +0000770 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000772 if (fep->dirty_tx != fep->cur_tx) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100773 if (netif_queue_stopped(ndev))
774 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
Frank Lide5fb0a2013-03-03 17:34:25 +0000777 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780
781/* During a receive, the cur_rx points to the current incoming buffer.
782 * When we update through the ring, if the next incoming buffer has
783 * not been given to the system, we just set the empty indicator,
784 * effectively tossing the packet.
785 */
Frank Lidc975382013-01-28 18:31:42 +0000786static int
787fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100789 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000790 const struct platform_device_id *id_entry =
791 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000792 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000793 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct sk_buff *skb;
795 ushort pkt_len;
796 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +0000797 int pkt_received = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400798
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000799#ifdef CONFIG_M532x
800 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* First, grab all of the stats for the incoming packet.
804 * These get messed up if we get called due to a busy condition.
805 */
806 bdp = fep->cur_rx;
807
Sascha Hauer22f6b862009-04-15 01:32:18 +0000808 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Frank Lidc975382013-01-28 18:31:42 +0000810 if (pkt_received >= budget)
811 break;
812 pkt_received++;
813
Sascha Hauer22f6b862009-04-15 01:32:18 +0000814 /* Since we have allocated space to hold a complete frame,
815 * the last indicator should be set.
816 */
817 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +0000818 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Sascha Hauer22f6b862009-04-15 01:32:18 +0000820 if (!fep->opened)
821 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Sascha Hauer22f6b862009-04-15 01:32:18 +0000823 /* Check for errors. */
824 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100826 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000827 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
828 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100829 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000830 }
831 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100832 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000833 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100834 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000835 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100836 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000838
839 /* Report late collisions as a frame error.
840 * On this error, the BD is closed, but we don't know what we
841 * have in the buffer. So, just drop this frame on the floor.
842 */
843 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100844 ndev->stats.rx_errors++;
845 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000846 goto rx_processing_done;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Sascha Hauer22f6b862009-04-15 01:32:18 +0000849 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100850 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000851 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100852 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000853 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100855 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
856 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000857
Shawn Guob5680e02011-01-05 21:13:13 +0000858 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
859 swap_buffer(data, pkt_len);
860
Sascha Hauer22f6b862009-04-15 01:32:18 +0000861 /* This does 16 byte alignment, exactly what we need.
862 * The packet length includes FCS, but we don't want to
863 * include that when passing upstream as it messes up
864 * bridging applications.
865 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000866 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Sascha Hauer85498892009-04-15 01:32:21 +0000868 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100869 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000870 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000871 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000872 skb_put(skb, pkt_len - 4); /* Make room */
873 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100874 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +0000875
Frank Li6605b732012-10-30 18:25:31 +0000876 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +0000877 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000878 struct skb_shared_hwtstamps *shhwtstamps =
879 skb_hwtstamps(skb);
880 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000881 struct bufdesc_ex *ebdp =
882 (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000883
884 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
885
886 spin_lock_irqsave(&fep->tmreg_lock, flags);
887 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000888 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000889 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
890 }
Frank Liff43da82013-01-03 16:04:23 +0000891
Jim Baxter4c09eed2013-04-19 08:10:49 +0000892 if (fep->bufdesc_ex &&
893 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
894 struct bufdesc_ex *ebdp =
895 (struct bufdesc_ex *)bdp;
896 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
897 /* don't check it */
898 skb->ip_summed = CHECKSUM_UNNECESSARY;
899 } else {
900 skb_checksum_none_assert(skb);
901 }
902 }
903
Richard Cochran18a03b92011-06-12 02:18:59 +0000904 if (!skb_defer_rx_timestamp(skb))
Frank Lidc975382013-01-28 18:31:42 +0000905 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000906 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000907
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100908 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
909 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000910rx_processing_done:
911 /* Clear the status flags for this buffer */
912 status &= ~BD_ENET_RX_STATS;
913
914 /* Mark the buffer empty */
915 status |= BD_ENET_RX_EMPTY;
916 bdp->cbd_sc = status;
917
Frank Liff43da82013-01-03 16:04:23 +0000918 if (fep->bufdesc_ex) {
919 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
920
921 ebdp->cbd_esc = BD_ENET_RX_INT;
922 ebdp->cbd_prot = 0;
923 ebdp->cbd_bdu = 0;
924 }
Frank Li6605b732012-10-30 18:25:31 +0000925
Sascha Hauer22f6b862009-04-15 01:32:18 +0000926 /* Update BD pointer to next entry */
927 if (status & BD_ENET_RX_WRAP)
928 bdp = fep->rx_bd_base;
929 else
Frank Liff43da82013-01-03 16:04:23 +0000930 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000931 /* Doing this here will keep the FEC running while we process
932 * incoming frames. On a heavily loaded network, we should be
933 * able to keep up at the expense of system resources.
934 */
935 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000937 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Frank Lidc975382013-01-28 18:31:42 +0000939 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Uwe Kleine-König45993652011-01-19 20:47:04 +0100942static irqreturn_t
943fec_enet_interrupt(int irq, void *dev_id)
944{
945 struct net_device *ndev = dev_id;
946 struct fec_enet_private *fep = netdev_priv(ndev);
947 uint int_events;
948 irqreturn_t ret = IRQ_NONE;
949
950 do {
951 int_events = readl(fep->hwp + FEC_IEVENT);
952 writel(int_events, fep->hwp + FEC_IEVENT);
953
Frank Lide5fb0a2013-03-03 17:34:25 +0000954 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100955 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +0000956
957 /* Disable the RX interrupt */
958 if (napi_schedule_prep(&fep->napi)) {
959 writel(FEC_RX_DISABLED_IMASK,
960 fep->hwp + FEC_IMASK);
961 __napi_schedule(&fep->napi);
962 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100963 }
964
Uwe Kleine-König45993652011-01-19 20:47:04 +0100965 if (int_events & FEC_ENET_MII) {
966 ret = IRQ_HANDLED;
967 complete(&fep->mdio_done);
968 }
969 } while (int_events);
970
971 return ret;
972}
973
Frank Lidc975382013-01-28 18:31:42 +0000974static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
975{
976 struct net_device *ndev = napi->dev;
977 int pkts = fec_enet_rx(ndev, budget);
978 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100979
Frank Lide5fb0a2013-03-03 17:34:25 +0000980 fec_enet_tx(ndev);
981
Frank Lidc975382013-01-28 18:31:42 +0000982 if (pkts < budget) {
983 napi_complete(napi);
984 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
985 }
986 return pkts;
987}
Uwe Kleine-König45993652011-01-19 20:47:04 +0100988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +0000990static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100992 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +0000993 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000994 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Shawn Guo49da97d2011-01-05 21:13:11 +0000996 /*
997 * try to get mac address in following order:
998 *
999 * 1) module parameter via kernel command line in form
1000 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1001 */
1002 iap = macaddr;
1003
1004 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001005 * 2) from device tree data
1006 */
1007 if (!is_valid_ether_addr(iap)) {
1008 struct device_node *np = fep->pdev->dev.of_node;
1009 if (np) {
1010 const char *mac = of_get_mac_address(np);
1011 if (mac)
1012 iap = (unsigned char *) mac;
1013 }
1014 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001015
1016 /*
1017 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001018 */
1019 if (!is_valid_ether_addr(iap)) {
1020#ifdef CONFIG_M5272
1021 if (FEC_FLASHMAC)
1022 iap = (unsigned char *)FEC_FLASHMAC;
1023#else
1024 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001025 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001026#endif
1027 }
1028
1029 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001030 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001031 */
1032 if (!is_valid_ether_addr(iap)) {
1033 *((unsigned long *) &tmpaddr[0]) =
1034 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
1035 *((unsigned short *) &tmpaddr[4]) =
1036 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 iap = &tmpaddr[0];
1038 }
1039
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001040 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Shawn Guo49da97d2011-01-05 21:13:11 +00001042 /* Adjust MAC if using macaddr */
1043 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001044 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047/* ------------------------------------------------------------------------- */
1048
Bryan Wue6b043d2010-03-31 02:10:44 +00001049/*
1050 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001052static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001054 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001055 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001056 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Bryan Wue6b043d2010-03-31 02:10:44 +00001058 /* Prevent a state halted on mii error */
1059 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1060 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001063
Bryan Wue6b043d2010-03-31 02:10:44 +00001064 if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001065 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001066 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001067 status_change = 1;
1068 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001069
Lucas Stachd97e7492013-03-14 05:12:01 +00001070 if (fep->full_duplex != phy_dev->duplex)
1071 status_change = 1;
1072
1073 if (phy_dev->speed != fep->speed) {
1074 fep->speed = phy_dev->speed;
1075 status_change = 1;
1076 }
1077
1078 /* if any of the above changed restart the FEC */
1079 if (status_change)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001080 fec_restart(ndev, phy_dev->duplex);
Lucas Stachd97e7492013-03-14 05:12:01 +00001081 } else {
1082 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001083 fec_stop(ndev);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001084 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001085 status_change = 1;
1086 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001087 }
1088
Bryan Wue6b043d2010-03-31 02:10:44 +00001089 if (status_change)
1090 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Bryan Wue6b043d2010-03-31 02:10:44 +00001093static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Bryan Wue6b043d2010-03-31 02:10:44 +00001095 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001096 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001097
1098 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001099 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001100
1101 /* start a read op */
1102 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1103 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1104 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1105
1106 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001107 time_left = wait_for_completion_timeout(&fep->mdio_done,
1108 usecs_to_jiffies(FEC_MII_TIMEOUT));
1109 if (time_left == 0) {
1110 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001111 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001112 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001113 }
1114
1115 /* return value */
1116 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1117}
1118
1119static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1120 u16 value)
1121{
1122 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001123 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001124
1125 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001126 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001127
Shawn Guo862f0982011-01-05 21:13:09 +00001128 /* start a write op */
1129 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001130 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1131 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1132 fep->hwp + FEC_MII_DATA);
1133
1134 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001135 time_left = wait_for_completion_timeout(&fep->mdio_done,
1136 usecs_to_jiffies(FEC_MII_TIMEOUT));
1137 if (time_left == 0) {
1138 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001139 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001140 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001141 }
1142
1143 return 0;
1144}
1145
1146static int fec_enet_mdio_reset(struct mii_bus *bus)
1147{
1148 return 0;
1149}
1150
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001151static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001152{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001153 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001154 const struct platform_device_id *id_entry =
1155 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001156 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001157 char mdio_bus_id[MII_BUS_ID_SIZE];
1158 char phy_name[MII_BUS_ID_SIZE + 3];
1159 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001160 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001161
Bryan Wu418bd0d2010-05-28 03:40:39 -07001162 fep->phy_dev = NULL;
1163
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001164 /* check for attached phy */
1165 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1166 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1167 continue;
1168 if (fep->mii_bus->phy_map[phy_id] == NULL)
1169 continue;
1170 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1171 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001172 if (dev_id--)
1173 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001174 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1175 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001176 }
1177
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001178 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001179 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001180 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001181 phy_id = 0;
1182 }
1183
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001184 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001185 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001186 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001187 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001188 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001189 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001190 }
1191
1192 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001193 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001194 phy_dev->supported &= PHY_GBIT_FEATURES;
Frank Libaa70a52013-01-16 16:55:58 +00001195 phy_dev->supported |= SUPPORTED_Pause;
1196 }
Shawn Guo230dec62011-09-23 02:12:48 +00001197 else
1198 phy_dev->supported &= PHY_BASIC_FEATURES;
1199
Bryan Wue6b043d2010-03-31 02:10:44 +00001200 phy_dev->advertising = phy_dev->supported;
1201
1202 fep->phy_dev = phy_dev;
1203 fep->link = 0;
1204 fep->full_duplex = 0;
1205
Joe Perches31b77202013-04-13 19:03:17 +00001206 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1207 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1208 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001209
Bryan Wue6b043d2010-03-31 02:10:44 +00001210 return 0;
1211}
1212
1213static int fec_enet_mii_init(struct platform_device *pdev)
1214{
Shawn Guob5680e02011-01-05 21:13:13 +00001215 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001216 struct net_device *ndev = platform_get_drvdata(pdev);
1217 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001218 const struct platform_device_id *id_entry =
1219 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001220 int err = -ENXIO, i;
1221
Shawn Guob5680e02011-01-05 21:13:13 +00001222 /*
1223 * The dual fec interfaces are not equivalent with enet-mac.
1224 * Here are the differences:
1225 *
1226 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1227 * - fec0 acts as the 1588 time master while fec1 is slave
1228 * - external phys can only be configured by fec0
1229 *
1230 * That is to say fec1 can not work independently. It only works
1231 * when fec0 is working. The reason behind this design is that the
1232 * second interface is added primarily for Switch mode.
1233 *
1234 * Because of the last point above, both phys are attached on fec0
1235 * mdio interface in board design, and need to be configured by
1236 * fec0 mii_bus.
1237 */
Shawn Guo43af9402011-12-05 05:01:15 +00001238 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001239 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001240 if (mii_cnt && fec0_mii_bus) {
1241 fep->mii_bus = fec0_mii_bus;
1242 mii_cnt++;
1243 return 0;
1244 }
1245 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001246 }
1247
Bryan Wue6b043d2010-03-31 02:10:44 +00001248 fep->mii_timeout = 0;
1249
1250 /*
1251 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001252 *
1253 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1254 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1255 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1256 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001257 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001258 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001259 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1260 fep->phy_speed--;
1261 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001262 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1263
1264 fep->mii_bus = mdiobus_alloc();
1265 if (fep->mii_bus == NULL) {
1266 err = -ENOMEM;
1267 goto err_out;
1268 }
1269
1270 fep->mii_bus->name = "fec_enet_mii_bus";
1271 fep->mii_bus->read = fec_enet_mdio_read;
1272 fep->mii_bus->write = fec_enet_mdio_write;
1273 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001274 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1275 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001276 fep->mii_bus->priv = fep;
1277 fep->mii_bus->parent = &pdev->dev;
1278
1279 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1280 if (!fep->mii_bus->irq) {
1281 err = -ENOMEM;
1282 goto err_out_free_mdiobus;
1283 }
1284
1285 for (i = 0; i < PHY_MAX_ADDR; i++)
1286 fep->mii_bus->irq[i] = PHY_POLL;
1287
Bryan Wue6b043d2010-03-31 02:10:44 +00001288 if (mdiobus_register(fep->mii_bus))
1289 goto err_out_free_mdio_irq;
1290
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001291 mii_cnt++;
1292
Shawn Guob5680e02011-01-05 21:13:13 +00001293 /* save fec0 mii_bus */
1294 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1295 fec0_mii_bus = fep->mii_bus;
1296
Bryan Wue6b043d2010-03-31 02:10:44 +00001297 return 0;
1298
Bryan Wue6b043d2010-03-31 02:10:44 +00001299err_out_free_mdio_irq:
1300 kfree(fep->mii_bus->irq);
1301err_out_free_mdiobus:
1302 mdiobus_free(fep->mii_bus);
1303err_out:
1304 return err;
1305}
1306
1307static void fec_enet_mii_remove(struct fec_enet_private *fep)
1308{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001309 if (--mii_cnt == 0) {
1310 mdiobus_unregister(fep->mii_bus);
1311 kfree(fep->mii_bus->irq);
1312 mdiobus_free(fep->mii_bus);
1313 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001314}
1315
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001316static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001317 struct ethtool_cmd *cmd)
1318{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001319 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001320 struct phy_device *phydev = fep->phy_dev;
1321
1322 if (!phydev)
1323 return -ENODEV;
1324
1325 return phy_ethtool_gset(phydev, cmd);
1326}
1327
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001328static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001329 struct ethtool_cmd *cmd)
1330{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001331 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001332 struct phy_device *phydev = fep->phy_dev;
1333
1334 if (!phydev)
1335 return -ENODEV;
1336
1337 return phy_ethtool_sset(phydev, cmd);
1338}
1339
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001340static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001341 struct ethtool_drvinfo *info)
1342{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001343 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Jiri Pirko7826d432013-01-06 00:44:26 +00001345 strlcpy(info->driver, fep->pdev->dev.driver->name,
1346 sizeof(info->driver));
1347 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1348 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
Bryan Wue6b043d2010-03-31 02:10:44 +00001350
Frank Li5ebae4892013-01-06 16:25:07 +00001351static int fec_enet_get_ts_info(struct net_device *ndev,
1352 struct ethtool_ts_info *info)
1353{
1354 struct fec_enet_private *fep = netdev_priv(ndev);
1355
1356 if (fep->bufdesc_ex) {
1357
1358 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1359 SOF_TIMESTAMPING_RX_SOFTWARE |
1360 SOF_TIMESTAMPING_SOFTWARE |
1361 SOF_TIMESTAMPING_TX_HARDWARE |
1362 SOF_TIMESTAMPING_RX_HARDWARE |
1363 SOF_TIMESTAMPING_RAW_HARDWARE;
1364 if (fep->ptp_clock)
1365 info->phc_index = ptp_clock_index(fep->ptp_clock);
1366 else
1367 info->phc_index = -1;
1368
1369 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1370 (1 << HWTSTAMP_TX_ON);
1371
1372 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1373 (1 << HWTSTAMP_FILTER_ALL);
1374 return 0;
1375 } else {
1376 return ethtool_op_get_ts_info(ndev, info);
1377 }
1378}
1379
Frank Libaa70a52013-01-16 16:55:58 +00001380static void fec_enet_get_pauseparam(struct net_device *ndev,
1381 struct ethtool_pauseparam *pause)
1382{
1383 struct fec_enet_private *fep = netdev_priv(ndev);
1384
1385 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1386 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1387 pause->rx_pause = pause->tx_pause;
1388}
1389
1390static int fec_enet_set_pauseparam(struct net_device *ndev,
1391 struct ethtool_pauseparam *pause)
1392{
1393 struct fec_enet_private *fep = netdev_priv(ndev);
1394
1395 if (pause->tx_pause != pause->rx_pause) {
1396 netdev_info(ndev,
1397 "hardware only support enable/disable both tx and rx");
1398 return -EINVAL;
1399 }
1400
1401 fep->pause_flag = 0;
1402
1403 /* tx pause must be same as rx pause */
1404 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1405 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1406
1407 if (pause->rx_pause || pause->autoneg) {
1408 fep->phy_dev->supported |= ADVERTISED_Pause;
1409 fep->phy_dev->advertising |= ADVERTISED_Pause;
1410 } else {
1411 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1412 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1413 }
1414
1415 if (pause->autoneg) {
1416 if (netif_running(ndev))
1417 fec_stop(ndev);
1418 phy_start_aneg(fep->phy_dev);
1419 }
1420 if (netif_running(ndev))
1421 fec_restart(ndev, 0);
1422
1423 return 0;
1424}
1425
stephen hemminger9b07be42012-01-04 12:59:49 +00001426static const struct ethtool_ops fec_enet_ethtool_ops = {
Frank Libaa70a52013-01-16 16:55:58 +00001427 .get_pauseparam = fec_enet_get_pauseparam,
1428 .set_pauseparam = fec_enet_set_pauseparam,
Bryan Wue6b043d2010-03-31 02:10:44 +00001429 .get_settings = fec_enet_get_settings,
1430 .set_settings = fec_enet_set_settings,
1431 .get_drvinfo = fec_enet_get_drvinfo,
1432 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00001433 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00001434};
1435
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001436static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001437{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001438 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001439 struct phy_device *phydev = fep->phy_dev;
1440
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001441 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001442 return -EINVAL;
1443
1444 if (!phydev)
1445 return -ENODEV;
1446
Frank Liff43da82013-01-03 16:04:23 +00001447 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
Frank Li6605b732012-10-30 18:25:31 +00001448 return fec_ptp_ioctl(ndev, rq, cmd);
Frank Liff43da82013-01-03 16:04:23 +00001449
Richard Cochran28b04112010-07-17 08:48:55 +00001450 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001453static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001454{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001455 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001456 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001457 struct sk_buff *skb;
1458 struct bufdesc *bdp;
1459
1460 bdp = fep->rx_bd_base;
1461 for (i = 0; i < RX_RING_SIZE; i++) {
1462 skb = fep->rx_skbuff[i];
1463
1464 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001465 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001466 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1467 if (skb)
1468 dev_kfree_skb(skb);
Frank Liff43da82013-01-03 16:04:23 +00001469 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001470 }
1471
1472 bdp = fep->tx_bd_base;
1473 for (i = 0; i < TX_RING_SIZE; i++)
1474 kfree(fep->tx_bounce[i]);
1475}
1476
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001477static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001478{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001479 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001480 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001481 struct sk_buff *skb;
1482 struct bufdesc *bdp;
1483
1484 bdp = fep->rx_bd_base;
1485 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001486 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001487 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001488 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001489 return -ENOMEM;
1490 }
1491 fep->rx_skbuff[i] = skb;
1492
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001493 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001494 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1495 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00001496
1497 if (fep->bufdesc_ex) {
1498 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1499 ebdp->cbd_esc = BD_ENET_RX_INT;
1500 }
1501
1502 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001503 }
1504
1505 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001506 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001507 bdp->cbd_sc |= BD_SC_WRAP;
1508
1509 bdp = fep->tx_bd_base;
1510 for (i = 0; i < TX_RING_SIZE; i++) {
1511 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1512
1513 bdp->cbd_sc = 0;
1514 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001515
Frank Liff43da82013-01-03 16:04:23 +00001516 if (fep->bufdesc_ex) {
1517 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00001518 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00001519 }
1520
1521 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001522 }
1523
1524 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001525 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001526 bdp->cbd_sc |= BD_SC_WRAP;
1527
1528 return 0;
1529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001532fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001534 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001535 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Frank Lidc975382013-01-28 18:31:42 +00001537 napi_enable(&fep->napi);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* I should reset the ring buffers here, but I don't yet know
1540 * a simple way to do that.
1541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001543 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001544 if (ret)
1545 return ret;
1546
Bryan Wu418bd0d2010-05-28 03:40:39 -07001547 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001548 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001549 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001550 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001551 return ret;
1552 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001553 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001554 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001560fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001562 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Sascha Hauer22f6b862009-04-15 01:32:18 +00001564 /* Don't know what to do yet. */
Georg Hofmann3f104c32013-03-14 06:54:09 +00001565 napi_disable(&fep->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001567 netif_stop_queue(ndev);
1568 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001570 if (fep->phy_dev) {
1571 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001572 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001573 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001574
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001575 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return 0;
1578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/* Set or clear the multicast filter for this adaptor.
1581 * Skeleton taken from sunlance driver.
1582 * The CPM Ethernet implementation allows Multicast as well as individual
1583 * MAC address filtering. Some of the drivers check to make sure it is
1584 * a group multicast address, and discard those that are not. I guess I
1585 * will do the same for now, but just remove the test if you want
1586 * individual filtering as well (do the upper net layers want or support
1587 * this kind of feature?).
1588 */
1589
1590#define HASH_BITS 6 /* #bits in hash */
1591#define CRC32_POLY 0xEDB88320
1592
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001593static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001595 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001596 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001597 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 unsigned char hash;
1599
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001600 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001601 tmp = readl(fep->hwp + FEC_R_CNTRL);
1602 tmp |= 0x8;
1603 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001604 return;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Sascha Hauer4e831832009-04-15 01:32:19 +00001607 tmp = readl(fep->hwp + FEC_R_CNTRL);
1608 tmp &= ~0x8;
1609 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001610
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001611 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001612 /* Catch all multicast addresses, so set the
1613 * filter to all 1's
1614 */
1615 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1616 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Sascha Hauer4e831832009-04-15 01:32:19 +00001618 return;
1619 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001620
Sascha Hauer4e831832009-04-15 01:32:19 +00001621 /* Clear filter and add the addresses in hash register
1622 */
1623 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1624 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001626 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001627 /* calculate crc32 value of mac address */
1628 crc = 0xffffffff;
1629
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001630 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001631 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001632 for (bit = 0; bit < 8; bit++, data >>= 1) {
1633 crc = (crc >> 1) ^
1634 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001637
1638 /* only upper 6 bits (HASH_BITS) are used
1639 * which point to specific bit in he hash registers
1640 */
1641 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1642
1643 if (hash > 31) {
1644 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1645 tmp |= 1 << (hash - 32);
1646 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1647 } else {
1648 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1649 tmp |= 1 << hash;
1650 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653}
1654
Sascha Hauer22f6b862009-04-15 01:32:18 +00001655/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001656static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001657fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001659 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001660 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Sascha Hauer009fda82009-04-15 01:32:23 +00001662 if (!is_valid_ether_addr(addr->sa_data))
1663 return -EADDRNOTAVAIL;
1664
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001665 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001666
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001667 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1668 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001669 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001670 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001671 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001675#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001676/**
1677 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001678 * @dev: The FEC network adapter
1679 *
1680 * Polled functionality used by netconsole and others in non interrupt mode
1681 *
1682 */
Wei Yongjun47a52472013-03-20 05:06:11 +00001683static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001684{
1685 int i;
1686 struct fec_enet_private *fep = netdev_priv(dev);
1687
1688 for (i = 0; i < FEC_IRQ_NUM; i++) {
1689 if (fep->irq[i] > 0) {
1690 disable_irq(fep->irq[i]);
1691 fec_enet_interrupt(fep->irq[i], dev);
1692 enable_irq(fep->irq[i]);
1693 }
1694 }
1695}
1696#endif
1697
Jim Baxter4c09eed2013-04-19 08:10:49 +00001698static int fec_set_features(struct net_device *netdev,
1699 netdev_features_t features)
1700{
1701 struct fec_enet_private *fep = netdev_priv(netdev);
1702 netdev_features_t changed = features ^ netdev->features;
1703
1704 netdev->features = features;
1705
1706 /* Receive checksum has been changed */
1707 if (changed & NETIF_F_RXCSUM) {
1708 if (features & NETIF_F_RXCSUM)
1709 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1710 else
1711 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
1712
1713 if (netif_running(netdev)) {
1714 fec_stop(netdev);
1715 fec_restart(netdev, fep->phy_dev->duplex);
1716 netif_wake_queue(netdev);
1717 } else {
1718 fec_restart(netdev, fep->phy_dev->duplex);
1719 }
1720 }
1721
1722 return 0;
1723}
1724
Sascha Hauer009fda82009-04-15 01:32:23 +00001725static const struct net_device_ops fec_netdev_ops = {
1726 .ndo_open = fec_enet_open,
1727 .ndo_stop = fec_enet_close,
1728 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001729 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001730 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001731 .ndo_validate_addr = eth_validate_addr,
1732 .ndo_tx_timeout = fec_timeout,
1733 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001734 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001735#ifdef CONFIG_NET_POLL_CONTROLLER
1736 .ndo_poll_controller = fec_poll_controller,
1737#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001738 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00001739};
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
1742 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001743 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001745static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001747 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00001748 const struct platform_device_id *id_entry =
1749 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001750 struct bufdesc *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001752 /* Allocate memory for buffer descriptors. */
1753 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001754 GFP_KERNEL);
1755 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001756 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10001757
Frank Li14109a52013-03-26 16:12:03 +00001758 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001759
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001760 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Shawn Guo49da97d2011-01-05 21:13:11 +00001762 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001763 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001765 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 fep->rx_bd_base = cbd_base;
Frank Liff43da82013-01-03 16:04:23 +00001767 if (fep->bufdesc_ex)
1768 fep->tx_bd_base = (struct bufdesc *)
1769 (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1770 else
1771 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Sascha Hauer22f6b862009-04-15 01:32:18 +00001773 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001774 ndev->watchdog_timeo = TX_TIMEOUT;
1775 ndev->netdev_ops = &fec_netdev_ops;
1776 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001777
Frank Lidc975382013-01-28 18:31:42 +00001778 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1779 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1780
Shawn Guo48496252013-05-08 21:08:22 +00001781 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
1782 /* enable hw accelerator */
1783 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1784 | NETIF_F_RXCSUM);
1785 ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1786 | NETIF_F_RXCSUM);
1787 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1788 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00001789
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001790 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return 0;
1793}
1794
Shawn Guoca2cc332011-06-25 02:04:35 +08001795#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05001796static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001797{
1798 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001799 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001800 struct device_node *np = pdev->dev.of_node;
1801
1802 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001803 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001804
Shawn Guoa3caad02012-06-27 03:45:24 +00001805 of_property_read_u32(np, "phy-reset-duration", &msec);
1806 /* A sane reset duration should not be longer than 1s */
1807 if (msec > 1000)
1808 msec = 1;
1809
Shawn Guoca2cc332011-06-25 02:04:35 +08001810 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001811 if (!gpio_is_valid(phy_reset))
1812 return;
1813
Shawn Guo119fc002012-06-27 03:45:22 +00001814 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1815 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001816 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001817 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001818 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001819 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001820 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001821 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001822}
1823#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001824static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001825{
1826 /*
1827 * In case of platform probe, the reset has been done
1828 * by machine code.
1829 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001830}
1831#endif /* CONFIG_OF */
1832
Bill Pemberton33897cc2012-12-03 09:23:58 -05001833static int
Sascha Haueread73182009-01-28 23:03:11 +00001834fec_probe(struct platform_device *pdev)
1835{
1836 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001837 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001838 struct net_device *ndev;
1839 int i, irq, ret = 0;
1840 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001841 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001842 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08001843
1844 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1845 if (of_id)
1846 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001847
1848 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1849 if (!r)
1850 return -ENXIO;
1851
Sascha Haueread73182009-01-28 23:03:11 +00001852 /* Init network device */
1853 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00001854 if (!ndev)
1855 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00001856
1857 SET_NETDEV_DEV(ndev, &pdev->dev);
1858
1859 /* setup board info structure */
1860 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001861
Frank Libaa70a52013-01-16 16:55:58 +00001862 /* default enable pause frame auto negotiation */
1863 if (pdev->id_entry &&
1864 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
1865 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
1866
Fabio Estevam83e519b2013-03-11 07:32:55 +00001867 fep->hwp = devm_request_and_ioremap(&pdev->dev, r);
Bryan Wue6b043d2010-03-31 02:10:44 +00001868 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00001869 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00001870
Frank Liff43da82013-01-03 16:04:23 +00001871 fep->bufdesc_ex = 0;
1872
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001873 if (!fep->hwp) {
Sascha Haueread73182009-01-28 23:03:11 +00001874 ret = -ENOMEM;
1875 goto failed_ioremap;
1876 }
1877
1878 platform_set_drvdata(pdev, ndev);
1879
Guenter Roeck6c5f7802013-04-02 09:35:10 +00001880 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08001881 if (ret < 0) {
1882 pdata = pdev->dev.platform_data;
1883 if (pdata)
1884 fep->phy_interface = pdata->phy;
1885 else
1886 fep->phy_interface = PHY_INTERFACE_MODE_MII;
1887 } else {
1888 fep->phy_interface = ret;
1889 }
1890
Fabio Estevame2f8d552013-02-22 06:40:45 +00001891 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1892 if (IS_ERR(fep->clk_ipg)) {
1893 ret = PTR_ERR(fep->clk_ipg);
1894 goto failed_clk;
1895 }
1896
1897 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1898 if (IS_ERR(fep->clk_ahb)) {
1899 ret = PTR_ERR(fep->clk_ahb);
1900 goto failed_clk;
1901 }
1902
Linus Torvalds38f56f32013-05-07 11:06:17 -07001903 /* enet_out is optional, depends on board */
1904 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
1905 if (IS_ERR(fep->clk_enet_out))
1906 fep->clk_enet_out = NULL;
1907
Fabio Estevame2f8d552013-02-22 06:40:45 +00001908 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001909 fep->bufdesc_ex =
1910 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00001911 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07001912 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00001913 fep->bufdesc_ex = 0;
1914 }
1915
1916 clk_prepare_enable(fep->clk_ahb);
1917 clk_prepare_enable(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07001918 clk_prepare_enable(fep->clk_enet_out);
1919 clk_prepare_enable(fep->clk_ptp);
Fabio Estevame2f8d552013-02-22 06:40:45 +00001920
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00001921 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
1922 if (!IS_ERR(fep->reg_phy)) {
1923 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00001924 if (ret) {
1925 dev_err(&pdev->dev,
1926 "Failed to enable phy regulator: %d\n", ret);
1927 goto failed_regulator;
1928 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00001929 } else {
1930 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00001931 }
1932
1933 fec_reset_phy(pdev);
1934
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001935 if (fep->bufdesc_ex)
1936 fec_ptp_init(ndev, pdev);
1937
1938 ret = fec_enet_init(ndev);
1939 if (ret)
1940 goto failed_init;
1941
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001942 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001943 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00001944 if (irq < 0) {
1945 if (i)
1946 break;
1947 ret = irq;
1948 goto failed_irq;
1949 }
Sascha Haueread73182009-01-28 23:03:11 +00001950 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1951 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01001952 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00001953 irq = platform_get_irq(pdev, i);
1954 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001955 }
1956 goto failed_irq;
1957 }
1958 }
1959
Bryan Wue6b043d2010-03-31 02:10:44 +00001960 ret = fec_enet_mii_init(pdev);
1961 if (ret)
1962 goto failed_mii_init;
1963
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001964 /* Carrier starts down, phylib will bring it up */
1965 netif_carrier_off(ndev);
1966
Sascha Haueread73182009-01-28 23:03:11 +00001967 ret = register_netdev(ndev);
1968 if (ret)
1969 goto failed_register;
1970
Fabio Estevameb1d0642013-04-13 07:25:36 +00001971 if (fep->bufdesc_ex && fep->ptp_clock)
1972 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
1973
Frank Li54309fa2013-05-07 14:08:44 +00001974 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00001975 return 0;
1976
1977failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001978 fec_enet_mii_remove(fep);
1979failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00001980failed_irq:
Fabio Estevame2f8d552013-02-22 06:40:45 +00001981 for (i = 0; i < FEC_IRQ_NUM; i++) {
1982 irq = platform_get_irq(pdev, i);
1983 if (irq > 0)
1984 free_irq(irq, ndev);
1985 }
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00001986failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00001987 if (fep->reg_phy)
1988 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001989failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001990 clk_disable_unprepare(fep->clk_ahb);
1991 clk_disable_unprepare(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07001992 clk_disable_unprepare(fep->clk_enet_out);
1993 clk_disable_unprepare(fep->clk_ptp);
Sascha Haueread73182009-01-28 23:03:11 +00001994failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00001995failed_ioremap:
1996 free_netdev(ndev);
1997
1998 return ret;
1999}
2000
Bill Pemberton33897cc2012-12-03 09:23:58 -05002001static int
Sascha Haueread73182009-01-28 23:03:11 +00002002fec_drv_remove(struct platform_device *pdev)
2003{
2004 struct net_device *ndev = platform_get_drvdata(pdev);
2005 struct fec_enet_private *fep = netdev_priv(ndev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002006 int i;
Sascha Haueread73182009-01-28 23:03:11 +00002007
Frank Li54309fa2013-05-07 14:08:44 +00002008 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002009 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002010 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002011 del_timer_sync(&fep->time_keep);
Fabio Estevamc55284e2013-05-27 03:48:32 +00002012 for (i = 0; i < FEC_IRQ_NUM; i++) {
2013 int irq = platform_get_irq(pdev, i);
2014 if (irq > 0)
2015 free_irq(irq, ndev);
2016 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002017 if (fep->reg_phy)
2018 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002019 clk_disable_unprepare(fep->clk_ptp);
2020 if (fep->ptp_clock)
2021 ptp_clock_unregister(fep->ptp_clock);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002022 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002023 clk_disable_unprepare(fep->clk_ahb);
2024 clk_disable_unprepare(fep->clk_ipg);
Sascha Haueread73182009-01-28 23:03:11 +00002025 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002026
Uwe Kleine-Königb3cde362011-01-25 18:03:37 +01002027 platform_set_drvdata(pdev, NULL);
2028
Sascha Haueread73182009-01-28 23:03:11 +00002029 return 0;
2030}
2031
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002032#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002033static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002034fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002035{
Eric Benard87cad5c2010-06-18 04:19:54 +00002036 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002037 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002038
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002039 if (netif_running(ndev)) {
2040 fec_stop(ndev);
2041 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002042 }
Linus Torvalds38f56f32013-05-07 11:06:17 -07002043 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002044 clk_disable_unprepare(fep->clk_ahb);
2045 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002046
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002047 if (fep->reg_phy)
2048 regulator_disable(fep->reg_phy);
2049
Sascha Haueread73182009-01-28 23:03:11 +00002050 return 0;
2051}
2052
2053static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002054fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002055{
Eric Benard87cad5c2010-06-18 04:19:54 +00002056 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002057 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002058 int ret;
2059
2060 if (fep->reg_phy) {
2061 ret = regulator_enable(fep->reg_phy);
2062 if (ret)
2063 return ret;
2064 }
Sascha Haueread73182009-01-28 23:03:11 +00002065
Linus Torvalds38f56f32013-05-07 11:06:17 -07002066 clk_prepare_enable(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002067 clk_prepare_enable(fep->clk_ahb);
2068 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002069 if (netif_running(ndev)) {
2070 fec_restart(ndev, fep->full_duplex);
2071 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002072 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002073
Sascha Haueread73182009-01-28 23:03:11 +00002074 return 0;
2075}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002076#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002077
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002078static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002079
Sascha Haueread73182009-01-28 23:03:11 +00002080static struct platform_driver fec_driver = {
2081 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002082 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002083 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002084 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002085 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002086 },
Shawn Guob5680e02011-01-05 21:13:13 +00002087 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002088 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002089 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002090};
2091
Fabio Estevamaaca2372012-01-23 16:44:50 +00002092module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094MODULE_LICENSE("GPL");