blob: 20890874ead74e231bb923118314a66e6ac2b673 [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>
37#include <linux/spinlock.h>
38#include <linux/workqueue.h>
39#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000040#include <linux/io.h>
41#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000042#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000043#include <linux/platform_device.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000044#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070045#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080046#include <linux/of.h>
47#include <linux/of_device.h>
48#include <linux/of_gpio.h>
49#include <linux/of_net.h>
Shawn Guob2bccee2012-05-06 20:24:04 +080050#include <linux/pinctrl/consumer.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000051#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Greg Ungerer080853a2007-07-30 16:28:46 +100053#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010057#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000058#define FEC_ALIGNMENT 0xf
59#else
60#define FEC_ALIGNMENT 0x3
61#endif
62
Shawn Guob5680e02011-01-05 21:13:13 +000063#define DRIVER_NAME "fec"
Frank Lidc975382013-01-28 18:31:42 +000064#define FEC_NAPI_WEIGHT 64
Shawn Guob5680e02011-01-05 21:13:13 +000065
Frank Libaa70a52013-01-16 16:55:58 +000066/* Pause frame feild and FIFO threshold */
67#define FEC_ENET_FCE (1 << 5)
68#define FEC_ENET_RSEM_V 0x84
69#define FEC_ENET_RSFL_V 16
70#define FEC_ENET_RAEM_V 0x8
71#define FEC_ENET_RAFL_V 0x8
72#define FEC_ENET_OPD_V 0xFFF0
73
Shawn Guob5680e02011-01-05 21:13:13 +000074/* Controller is ENET-MAC */
75#define FEC_QUIRK_ENET_MAC (1 << 0)
76/* Controller needs driver to swap frame */
77#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080078/* Controller uses gasket */
79#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000080/* Controller has GBIT support */
81#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000082/* Controller has extend desc buffer */
83#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guob5680e02011-01-05 21:13:13 +000084
85static struct platform_device_id fec_devtype[] = {
86 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080087 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000088 .name = DRIVER_NAME,
89 .driver_data = 0,
90 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080091 .name = "imx25-fec",
92 .driver_data = FEC_QUIRK_USE_GASKET,
93 }, {
94 .name = "imx27-fec",
95 .driver_data = 0,
96 }, {
Shawn Guob5680e02011-01-05 21:13:13 +000097 .name = "imx28-fec",
98 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +080099 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000100 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000101 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
102 FEC_QUIRK_HAS_BUFDESC_EX,
Shawn Guo230dec62011-09-23 02:12:48 +0000103 }, {
Jingchang Luca7c4a42013-04-11 21:12:45 +0000104 .name = "mvf-fec",
105 .driver_data = FEC_QUIRK_ENET_MAC,
106 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800107 /* sentinel */
108 }
Shawn Guob5680e02011-01-05 21:13:13 +0000109};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800110MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000111
Shawn Guoca2cc332011-06-25 02:04:35 +0800112enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000113 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800114 IMX27_FEC, /* runs on i.mx27/35/51 */
115 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000116 IMX6Q_FEC,
Jingchang Luca7c4a42013-04-11 21:12:45 +0000117 MVF_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800118};
119
120static const struct of_device_id fec_dt_ids[] = {
121 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
122 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
123 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000124 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Jingchang Luca7c4a42013-04-11 21:12:45 +0000125 { .compatible = "fsl,mvf-fec", .data = &fec_devtype[MVF_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800126 { /* sentinel */ }
127};
128MODULE_DEVICE_TABLE(of, fec_dt_ids);
129
Shawn Guo49da97d2011-01-05 21:13:11 +0000130static unsigned char macaddr[ETH_ALEN];
131module_param_array(macaddr, byte, NULL, 0);
132MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
133
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000134#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Some hardware gets it MAC address out of local flash memory.
137 * if this is non-zero then assume it is the address to get MAC from.
138 */
139#if defined(CONFIG_NETtel)
140#define FEC_FLASHMAC 0xf0006006
141#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
142#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#elif defined(CONFIG_CANCam)
144#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000145#elif defined (CONFIG_M5272C3)
146#define FEC_FLASHMAC (0xffe04000 + 4)
147#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000148#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#else
150#define FEC_FLASHMAC 0
151#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800152#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000153
Frank Liff43da82013-01-03 16:04:23 +0000154#if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000155#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000156#endif
157
Sascha Hauer22f6b862009-04-15 01:32:18 +0000158/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
160#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
161#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
162#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
163#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
164#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
165#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
166#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
167#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
168#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
169
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000170#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000171#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* The FEC stores dest/src/type, data, and checksum for receive packets.
174 */
175#define PKT_MAXBUF_SIZE 1518
176#define PKT_MINBUF_SIZE 64
177#define PKT_MAXBLR_SIZE 1520
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Matt Waddel6b265292006-06-27 13:10:56 +1000180 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * size bits. Other FEC hardware does not, so we need to take that into
182 * account when setting it.
183 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000184#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100185 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
187#else
188#define OPT_FRAME_SIZE 0
189#endif
190
Bryan Wue6b043d2010-03-31 02:10:44 +0000191/* FEC MII MMFR bits definition */
192#define FEC_MMFR_ST (1 << 30)
193#define FEC_MMFR_OP_READ (2 << 28)
194#define FEC_MMFR_OP_WRITE (1 << 28)
195#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
196#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
197#define FEC_MMFR_TA (2 << 16)
198#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500200#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Sascha Hauer22f6b862009-04-15 01:32:18 +0000202/* Transmitter timeout */
203#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Frank Libaa70a52013-01-16 16:55:58 +0000205#define FEC_PAUSE_FLAG_AUTONEG 0x1
206#define FEC_PAUSE_FLAG_ENABLE 0x2
207
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000208static int mii_cnt;
209
Frank Liff43da82013-01-03 16:04:23 +0000210static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex)
211{
212 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
213 if (is_ex)
214 return (struct bufdesc *)(ex + 1);
215 else
216 return bdp + 1;
217}
218
219static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex)
220{
221 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
222 if (is_ex)
223 return (struct bufdesc *)(ex - 1);
224 else
225 return bdp - 1;
226}
227
Shawn Guob5680e02011-01-05 21:13:13 +0000228static void *swap_buffer(void *bufaddr, int len)
229{
230 int i;
231 unsigned int *buf = bufaddr;
232
233 for (i = 0; i < (len + 3) / 4; i++, buf++)
234 *buf = cpu_to_be32(*buf);
235
236 return bufaddr;
237}
238
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000239static netdev_tx_t
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100240fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100242 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000243 const struct platform_device_id *id_entry =
244 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000245 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000246 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000247 unsigned short status;
Frank Lide5fb0a2013-03-03 17:34:25 +0000248 unsigned int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!fep->link) {
251 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000252 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 /* Fill in a Tx ring entry */
256 bdp = fep->cur_tx;
257
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000258 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000259
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000260 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Ooops. All transmit buffers are full. Bail out.
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100262 * This should not happen, since ndev->tbusy should be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Joe Perches31b77202013-04-13 19:03:17 +0000264 netdev_err(ndev, "tx queue full!\n");
Patrick McHardy5b548142009-06-12 06:22:29 +0000265 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Sascha Hauer22f6b862009-04-15 01:32:18 +0000268 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000269 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Sascha Hauer22f6b862009-04-15 01:32:18 +0000271 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000272 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 bdp->cbd_datlen = skb->len;
274
275 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000276 * On some FEC implementations data must be aligned on
277 * 4-byte boundaries. Use bounce buffers to copy data
278 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000280 if (fep->bufdesc_ex)
281 index = (struct bufdesc_ex *)bdp -
282 (struct bufdesc_ex *)fep->tx_bd_base;
283 else
284 index = bdp - fep->tx_bd_base;
285
Greg Ungerer9555b312009-08-06 17:58:18 +0000286 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Uwe Kleine-König8a73b0b2011-01-13 21:34:31 +0100287 memcpy(fep->tx_bounce[index], skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000288 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Shawn Guob5680e02011-01-05 21:13:13 +0000291 /*
292 * Some design made an incorrect assumption on endian mode of
293 * the system that it's running on. As the result, driver has to
294 * swap every frame going to and coming from the controller.
295 */
296 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
297 swap_buffer(bufaddr, skb->len);
298
Sascha Hauer22f6b862009-04-15 01:32:18 +0000299 /* Save skb pointer */
Frank Lide5fb0a2013-03-03 17:34:25 +0000300 fep->tx_skbuff[index] = skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* Push the data cache so the CPM does not get stale memory
303 * data.
304 */
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100305 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000306 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000308 /* Send it on its way. Tell FEC it's ready, interrupt when done,
309 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000311 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000313 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Frank Liff43da82013-01-03 16:04:23 +0000315 if (fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000316
Frank Liff43da82013-01-03 16:04:23 +0000317 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
318 ebdp->cbd_bdu = 0;
319 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
320 fep->hwts_tx_en)) {
321 ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
322 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
323 } else {
324
325 ebdp->cbd_esc = BD_ENET_TX_INT;
326 }
Frank Li6605b732012-10-30 18:25:31 +0000327 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000328 /* If this was the last BD in the ring, start at the beginning again. */
329 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000331 else
Frank Liff43da82013-01-03 16:04:23 +0000332 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Sascha Hauer2e285322009-04-15 01:32:16 +0000334 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Frank Lide5fb0a2013-03-03 17:34:25 +0000336 if (fep->cur_tx == fep->dirty_tx)
337 netif_stop_queue(ndev);
Richard Cochran18a03b92011-06-12 02:18:59 +0000338
Frank Lide5fb0a2013-03-03 17:34:25 +0000339 /* Trigger transmission start */
340 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
341
342 skb_tx_timestamp(skb);
Richard Cochrana0087a32011-06-19 03:31:40 +0000343
Patrick McHardy6ed10652009-06-23 06:03:08 +0000344 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Frank Li14109a52013-03-26 16:12:03 +0000347/* Init RX & TX buffer descriptors
348 */
349static void fec_enet_bd_init(struct net_device *dev)
350{
351 struct fec_enet_private *fep = netdev_priv(dev);
352 struct bufdesc *bdp;
353 unsigned int i;
354
355 /* Initialize the receive buffer descriptors. */
356 bdp = fep->rx_bd_base;
357 for (i = 0; i < RX_RING_SIZE; i++) {
358
359 /* Initialize the BD for every fragment in the page. */
360 if (bdp->cbd_bufaddr)
361 bdp->cbd_sc = BD_ENET_RX_EMPTY;
362 else
363 bdp->cbd_sc = 0;
364 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
365 }
366
367 /* Set the last buffer to wrap */
368 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
369 bdp->cbd_sc |= BD_SC_WRAP;
370
371 fep->cur_rx = fep->rx_bd_base;
372
373 /* ...and the same for transmit */
374 bdp = fep->tx_bd_base;
375 fep->cur_tx = bdp;
376 for (i = 0; i < TX_RING_SIZE; i++) {
377
378 /* Initialize the BD for every fragment in the page. */
379 bdp->cbd_sc = 0;
380 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
381 dev_kfree_skb_any(fep->tx_skbuff[i]);
382 fep->tx_skbuff[i] = NULL;
383 }
384 bdp->cbd_bufaddr = 0;
385 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
386 }
387
388 /* Set the last buffer to wrap */
389 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
390 bdp->cbd_sc |= BD_SC_WRAP;
391 fep->dirty_tx = bdp;
392}
393
Uwe Kleine-König45993652011-01-19 20:47:04 +0100394/* This function is called to start or restart the FEC during a link
395 * change. This only happens when switching between half and full
396 * duplex.
397 */
398static void
399fec_restart(struct net_device *ndev, int duplex)
400{
401 struct fec_enet_private *fep = netdev_priv(ndev);
402 const struct platform_device_id *id_entry =
403 platform_get_device_id(fep->pdev);
404 int i;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100405 u32 temp_mac[2];
406 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000407 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100408
409 /* Whack a reset. We should wait for this. */
410 writel(1, fep->hwp + FEC_ECNTRL);
411 udelay(10);
412
413 /*
414 * enet-mac reset will reset mac address registers too,
415 * so need to reconfigure it.
416 */
417 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
418 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
419 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
420 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
421 }
422
423 /* Clear any outstanding interrupt. */
424 writel(0xffc00000, fep->hwp + FEC_IEVENT);
425
426 /* Reset all multicast. */
427 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
428 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
429#ifndef CONFIG_M5272
430 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
431 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
432#endif
433
434 /* Set maximum receive buffer size. */
435 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
436
Frank Li14109a52013-03-26 16:12:03 +0000437 fec_enet_bd_init(ndev);
438
Uwe Kleine-König45993652011-01-19 20:47:04 +0100439 /* Set receive and transmit descriptor base. */
440 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000441 if (fep->bufdesc_ex)
442 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
443 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
444 else
445 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
446 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100447
Uwe Kleine-König45993652011-01-19 20:47:04 +0100448
Uwe Kleine-König45993652011-01-19 20:47:04 +0100449 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
450 if (fep->tx_skbuff[i]) {
451 dev_kfree_skb_any(fep->tx_skbuff[i]);
452 fep->tx_skbuff[i] = NULL;
453 }
454 }
455
456 /* Enable MII mode */
457 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100458 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100459 writel(0x04, fep->hwp + FEC_X_CNTRL);
460 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100461 /* No Rcv on Xmit */
462 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100463 writel(0x0, fep->hwp + FEC_X_CNTRL);
464 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100465
Uwe Kleine-König45993652011-01-19 20:47:04 +0100466 fep->full_duplex = duplex;
467
468 /* Set MII speed */
469 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
470
471 /*
472 * The phy interface and speed need to get configured
473 * differently on enet-mac.
474 */
475 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100476 /* Enable flow control and length check */
477 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100478
Shawn Guo230dec62011-09-23 02:12:48 +0000479 /* RGMII, RMII or MII */
480 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
481 rcntl |= (1 << 6);
482 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100483 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100484 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100485 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100486
Shawn Guo230dec62011-09-23 02:12:48 +0000487 /* 1G, 100M or 10M */
488 if (fep->phy_dev) {
489 if (fep->phy_dev->speed == SPEED_1000)
490 ecntl |= (1 << 5);
491 else if (fep->phy_dev->speed == SPEED_100)
492 rcntl &= ~(1 << 9);
493 else
494 rcntl |= (1 << 9);
495 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100496 } else {
497#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800498 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000499 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100500 /* disable the gasket and wait */
501 writel(0, fep->hwp + FEC_MIIGSK_ENR);
502 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
503 udelay(1);
504
505 /*
506 * configure the gasket:
507 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800508 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100509 */
Eric Benard8d82f212012-01-12 06:10:28 +0000510 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
511 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
512 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
513 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
514 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100515
516 /* re-enable the gasket */
517 writel(2, fep->hwp + FEC_MIIGSK_ENR);
518 }
519#endif
520 }
Frank Libaa70a52013-01-16 16:55:58 +0000521
522 /* enable pause frame*/
523 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
524 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
525 fep->phy_dev && fep->phy_dev->pause)) {
526 rcntl |= FEC_ENET_FCE;
527
528 /* set FIFO thresh hold parameter to reduce overrun */
529 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
530 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
531 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
532 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
533
534 /* OPD */
535 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
536 } else {
537 rcntl &= ~FEC_ENET_FCE;
538 }
539
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100540 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100541
Shawn Guo230dec62011-09-23 02:12:48 +0000542 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
543 /* enable ENET endian swap */
544 ecntl |= (1 << 8);
545 /* enable ENET store and forward mode */
546 writel(1 << 8, fep->hwp + FEC_X_WMRK);
547 }
548
Frank Liff43da82013-01-03 16:04:23 +0000549 if (fep->bufdesc_ex)
550 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000551
Uwe Kleine-König45993652011-01-19 20:47:04 +0100552 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000553 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100554 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
555
Frank Liff43da82013-01-03 16:04:23 +0000556 if (fep->bufdesc_ex)
557 fec_ptp_start_cyclecounter(ndev);
558
Uwe Kleine-König45993652011-01-19 20:47:04 +0100559 /* Enable interrupts we wish to service */
560 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
561}
562
563static void
564fec_stop(struct net_device *ndev)
565{
566 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000567 const struct platform_device_id *id_entry =
568 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000569 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100570
571 /* We cannot expect a graceful transmit stop without link !!! */
572 if (fep->link) {
573 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
574 udelay(10);
575 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000576 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +0100577 }
578
579 /* Whack a reset. We should wait for this. */
580 writel(1, fep->hwp + FEC_ECNTRL);
581 udelay(10);
582 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
583 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000584
585 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000586 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000587 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000588 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
589 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100590}
591
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100594fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100596 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100598 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100600 fec_restart(ndev, fep->full_duplex);
601 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100605fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000608 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000609 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +0000611 int index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100613 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 bdp = fep->dirty_tx;
615
Frank Lide5fb0a2013-03-03 17:34:25 +0000616 /* get next bdp of dirty_tx */
617 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
618 bdp = fep->tx_bd_base;
619 else
620 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
621
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000622 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +0000623
624 /* current queue is empty */
625 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000626 break;
627
Frank Lide5fb0a2013-03-03 17:34:25 +0000628 if (fep->bufdesc_ex)
629 index = (struct bufdesc_ex *)bdp -
630 (struct bufdesc_ex *)fep->tx_bd_base;
631 else
632 index = bdp - fep->tx_bd_base;
633
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100634 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
635 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000636 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Frank Lide5fb0a2013-03-03 17:34:25 +0000638 skb = fep->tx_skbuff[index];
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000641 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 BD_ENET_TX_RL | BD_ENET_TX_UN |
643 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100644 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000645 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100646 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000647 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100648 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000649 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100650 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000651 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100652 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000653 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100654 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100656 ndev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Frank Liff43da82013-01-03 16:04:23 +0000659 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
660 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000661 struct skb_shared_hwtstamps shhwtstamps;
662 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000663 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000664
665 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
666 spin_lock_irqsave(&fep->tmreg_lock, flags);
667 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000668 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000669 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
670 skb_tstamp_tx(skb, &shhwtstamps);
671 }
Frank Liff43da82013-01-03 16:04:23 +0000672
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000673 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +0000674 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* Deferred means some collisions occurred during transmit,
677 * but we eventually sent the packet OK.
678 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000679 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100680 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400681
Sascha Hauer22f6b862009-04-15 01:32:18 +0000682 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +0000684 fep->tx_skbuff[index] = NULL;
685
686 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400687
Sascha Hauer22f6b862009-04-15 01:32:18 +0000688 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000689 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 bdp = fep->tx_bd_base;
691 else
Frank Liff43da82013-01-03 16:04:23 +0000692 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400693
Sascha Hauer22f6b862009-04-15 01:32:18 +0000694 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000696 if (fep->dirty_tx != fep->cur_tx) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100697 if (netif_queue_stopped(ndev))
698 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 }
Frank Lide5fb0a2013-03-03 17:34:25 +0000701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704
705/* During a receive, the cur_rx points to the current incoming buffer.
706 * When we update through the ring, if the next incoming buffer has
707 * not been given to the system, we just set the empty indicator,
708 * effectively tossing the packet.
709 */
Frank Lidc975382013-01-28 18:31:42 +0000710static int
711fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100713 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000714 const struct platform_device_id *id_entry =
715 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000716 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000717 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct sk_buff *skb;
719 ushort pkt_len;
720 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +0000721 int pkt_received = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400722
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000723#ifdef CONFIG_M532x
724 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* First, grab all of the stats for the incoming packet.
728 * These get messed up if we get called due to a busy condition.
729 */
730 bdp = fep->cur_rx;
731
Sascha Hauer22f6b862009-04-15 01:32:18 +0000732 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Frank Lidc975382013-01-28 18:31:42 +0000734 if (pkt_received >= budget)
735 break;
736 pkt_received++;
737
Sascha Hauer22f6b862009-04-15 01:32:18 +0000738 /* Since we have allocated space to hold a complete frame,
739 * the last indicator should be set.
740 */
741 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +0000742 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Sascha Hauer22f6b862009-04-15 01:32:18 +0000744 if (!fep->opened)
745 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Sascha Hauer22f6b862009-04-15 01:32:18 +0000747 /* Check for errors. */
748 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100750 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000751 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
752 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100753 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000754 }
755 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100756 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000757 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100758 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000759 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100760 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000762
763 /* Report late collisions as a frame error.
764 * On this error, the BD is closed, but we don't know what we
765 * have in the buffer. So, just drop this frame on the floor.
766 */
767 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100768 ndev->stats.rx_errors++;
769 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000770 goto rx_processing_done;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Sascha Hauer22f6b862009-04-15 01:32:18 +0000773 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100774 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000775 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100776 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000777 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100779 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
780 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000781
Shawn Guob5680e02011-01-05 21:13:13 +0000782 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
783 swap_buffer(data, pkt_len);
784
Sascha Hauer22f6b862009-04-15 01:32:18 +0000785 /* This does 16 byte alignment, exactly what we need.
786 * The packet length includes FCS, but we don't want to
787 * include that when passing upstream as it messes up
788 * bridging applications.
789 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000790 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Sascha Hauer85498892009-04-15 01:32:21 +0000792 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100793 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000794 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000795 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000796 skb_put(skb, pkt_len - 4); /* Make room */
797 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100798 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +0000799
Frank Li6605b732012-10-30 18:25:31 +0000800 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +0000801 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000802 struct skb_shared_hwtstamps *shhwtstamps =
803 skb_hwtstamps(skb);
804 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000805 struct bufdesc_ex *ebdp =
806 (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000807
808 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
809
810 spin_lock_irqsave(&fep->tmreg_lock, flags);
811 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000812 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000813 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
814 }
Frank Liff43da82013-01-03 16:04:23 +0000815
Richard Cochran18a03b92011-06-12 02:18:59 +0000816 if (!skb_defer_rx_timestamp(skb))
Frank Lidc975382013-01-28 18:31:42 +0000817 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000818 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000819
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100820 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
821 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000822rx_processing_done:
823 /* Clear the status flags for this buffer */
824 status &= ~BD_ENET_RX_STATS;
825
826 /* Mark the buffer empty */
827 status |= BD_ENET_RX_EMPTY;
828 bdp->cbd_sc = status;
829
Frank Liff43da82013-01-03 16:04:23 +0000830 if (fep->bufdesc_ex) {
831 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
832
833 ebdp->cbd_esc = BD_ENET_RX_INT;
834 ebdp->cbd_prot = 0;
835 ebdp->cbd_bdu = 0;
836 }
Frank Li6605b732012-10-30 18:25:31 +0000837
Sascha Hauer22f6b862009-04-15 01:32:18 +0000838 /* Update BD pointer to next entry */
839 if (status & BD_ENET_RX_WRAP)
840 bdp = fep->rx_bd_base;
841 else
Frank Liff43da82013-01-03 16:04:23 +0000842 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000843 /* Doing this here will keep the FEC running while we process
844 * incoming frames. On a heavily loaded network, we should be
845 * able to keep up at the expense of system resources.
846 */
847 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000849 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Frank Lidc975382013-01-28 18:31:42 +0000851 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Uwe Kleine-König45993652011-01-19 20:47:04 +0100854static irqreturn_t
855fec_enet_interrupt(int irq, void *dev_id)
856{
857 struct net_device *ndev = dev_id;
858 struct fec_enet_private *fep = netdev_priv(ndev);
859 uint int_events;
860 irqreturn_t ret = IRQ_NONE;
861
862 do {
863 int_events = readl(fep->hwp + FEC_IEVENT);
864 writel(int_events, fep->hwp + FEC_IEVENT);
865
Frank Lide5fb0a2013-03-03 17:34:25 +0000866 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100867 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +0000868
869 /* Disable the RX interrupt */
870 if (napi_schedule_prep(&fep->napi)) {
871 writel(FEC_RX_DISABLED_IMASK,
872 fep->hwp + FEC_IMASK);
873 __napi_schedule(&fep->napi);
874 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100875 }
876
Uwe Kleine-König45993652011-01-19 20:47:04 +0100877 if (int_events & FEC_ENET_MII) {
878 ret = IRQ_HANDLED;
879 complete(&fep->mdio_done);
880 }
881 } while (int_events);
882
883 return ret;
884}
885
Frank Lidc975382013-01-28 18:31:42 +0000886static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
887{
888 struct net_device *ndev = napi->dev;
889 int pkts = fec_enet_rx(ndev, budget);
890 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100891
Frank Lide5fb0a2013-03-03 17:34:25 +0000892 fec_enet_tx(ndev);
893
Frank Lidc975382013-01-28 18:31:42 +0000894 if (pkts < budget) {
895 napi_complete(napi);
896 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
897 }
898 return pkts;
899}
Uwe Kleine-König45993652011-01-19 20:47:04 +0100900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +0000902static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100904 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +0000905 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000906 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Shawn Guo49da97d2011-01-05 21:13:11 +0000908 /*
909 * try to get mac address in following order:
910 *
911 * 1) module parameter via kernel command line in form
912 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
913 */
914 iap = macaddr;
915
916 /*
Shawn Guoca2cc332011-06-25 02:04:35 +0800917 * 2) from device tree data
918 */
919 if (!is_valid_ether_addr(iap)) {
920 struct device_node *np = fep->pdev->dev.of_node;
921 if (np) {
922 const char *mac = of_get_mac_address(np);
923 if (mac)
924 iap = (unsigned char *) mac;
925 }
926 }
Shawn Guoca2cc332011-06-25 02:04:35 +0800927
928 /*
929 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +0000930 */
931 if (!is_valid_ether_addr(iap)) {
932#ifdef CONFIG_M5272
933 if (FEC_FLASHMAC)
934 iap = (unsigned char *)FEC_FLASHMAC;
935#else
936 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +0000937 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +0000938#endif
939 }
940
941 /*
Shawn Guoca2cc332011-06-25 02:04:35 +0800942 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +0000943 */
944 if (!is_valid_ether_addr(iap)) {
945 *((unsigned long *) &tmpaddr[0]) =
946 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
947 *((unsigned short *) &tmpaddr[4]) =
948 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 iap = &tmpaddr[0];
950 }
951
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100952 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Shawn Guo49da97d2011-01-05 21:13:11 +0000954 /* Adjust MAC if using macaddr */
955 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +0000956 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959/* ------------------------------------------------------------------------- */
960
Bryan Wue6b043d2010-03-31 02:10:44 +0000961/*
962 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100964static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100966 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000967 struct phy_device *phy_dev = fep->phy_dev;
968 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Bryan Wue6b043d2010-03-31 02:10:44 +0000970 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Bryan Wue6b043d2010-03-31 02:10:44 +0000972 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400973
Bryan Wue6b043d2010-03-31 02:10:44 +0000974 /* Prevent a state halted on mii error */
975 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
976 phy_dev->state = PHY_RESUMING;
977 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000979
Bryan Wue6b043d2010-03-31 02:10:44 +0000980 if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +0000981 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +0000982 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +0000983 status_change = 1;
984 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000985
Lucas Stachd97e7492013-03-14 05:12:01 +0000986 if (fep->full_duplex != phy_dev->duplex)
987 status_change = 1;
988
989 if (phy_dev->speed != fep->speed) {
990 fep->speed = phy_dev->speed;
991 status_change = 1;
992 }
993
994 /* if any of the above changed restart the FEC */
995 if (status_change)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100996 fec_restart(ndev, phy_dev->duplex);
Lucas Stachd97e7492013-03-14 05:12:01 +0000997 } else {
998 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100999 fec_stop(ndev);
Lucas Stachd97e7492013-03-14 05:12:01 +00001000 status_change = 1;
1001 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001002 }
1003
1004spin_unlock:
1005 spin_unlock_irqrestore(&fep->hw_lock, flags);
1006
1007 if (status_change)
1008 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Bryan Wue6b043d2010-03-31 02:10:44 +00001011static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Bryan Wue6b043d2010-03-31 02:10:44 +00001013 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001014 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001015
1016 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001017 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001018
1019 /* start a read op */
1020 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1021 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1022 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1023
1024 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001025 time_left = wait_for_completion_timeout(&fep->mdio_done,
1026 usecs_to_jiffies(FEC_MII_TIMEOUT));
1027 if (time_left == 0) {
1028 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001029 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001030 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001031 }
1032
1033 /* return value */
1034 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1035}
1036
1037static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1038 u16 value)
1039{
1040 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001041 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001042
1043 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001044 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001045
Shawn Guo862f0982011-01-05 21:13:09 +00001046 /* start a write op */
1047 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001048 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1049 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1050 fep->hwp + FEC_MII_DATA);
1051
1052 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001053 time_left = wait_for_completion_timeout(&fep->mdio_done,
1054 usecs_to_jiffies(FEC_MII_TIMEOUT));
1055 if (time_left == 0) {
1056 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001057 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001058 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001059 }
1060
1061 return 0;
1062}
1063
1064static int fec_enet_mdio_reset(struct mii_bus *bus)
1065{
1066 return 0;
1067}
1068
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001069static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001070{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001071 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001072 const struct platform_device_id *id_entry =
1073 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001074 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001075 char mdio_bus_id[MII_BUS_ID_SIZE];
1076 char phy_name[MII_BUS_ID_SIZE + 3];
1077 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001078 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001079
Bryan Wu418bd0d2010-05-28 03:40:39 -07001080 fep->phy_dev = NULL;
1081
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001082 /* check for attached phy */
1083 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1084 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1085 continue;
1086 if (fep->mii_bus->phy_map[phy_id] == NULL)
1087 continue;
1088 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1089 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001090 if (dev_id--)
1091 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001092 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1093 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001094 }
1095
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001096 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001097 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001098 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001099 phy_id = 0;
1100 }
1101
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001102 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001103 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001104 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001105 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001106 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001107 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001108 }
1109
1110 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001111 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001112 phy_dev->supported &= PHY_GBIT_FEATURES;
Frank Libaa70a52013-01-16 16:55:58 +00001113 phy_dev->supported |= SUPPORTED_Pause;
1114 }
Shawn Guo230dec62011-09-23 02:12:48 +00001115 else
1116 phy_dev->supported &= PHY_BASIC_FEATURES;
1117
Bryan Wue6b043d2010-03-31 02:10:44 +00001118 phy_dev->advertising = phy_dev->supported;
1119
1120 fep->phy_dev = phy_dev;
1121 fep->link = 0;
1122 fep->full_duplex = 0;
1123
Joe Perches31b77202013-04-13 19:03:17 +00001124 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1125 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1126 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001127
Bryan Wue6b043d2010-03-31 02:10:44 +00001128 return 0;
1129}
1130
1131static int fec_enet_mii_init(struct platform_device *pdev)
1132{
Shawn Guob5680e02011-01-05 21:13:13 +00001133 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001134 struct net_device *ndev = platform_get_drvdata(pdev);
1135 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001136 const struct platform_device_id *id_entry =
1137 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001138 int err = -ENXIO, i;
1139
Shawn Guob5680e02011-01-05 21:13:13 +00001140 /*
1141 * The dual fec interfaces are not equivalent with enet-mac.
1142 * Here are the differences:
1143 *
1144 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1145 * - fec0 acts as the 1588 time master while fec1 is slave
1146 * - external phys can only be configured by fec0
1147 *
1148 * That is to say fec1 can not work independently. It only works
1149 * when fec0 is working. The reason behind this design is that the
1150 * second interface is added primarily for Switch mode.
1151 *
1152 * Because of the last point above, both phys are attached on fec0
1153 * mdio interface in board design, and need to be configured by
1154 * fec0 mii_bus.
1155 */
Shawn Guo43af9402011-12-05 05:01:15 +00001156 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001157 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001158 if (mii_cnt && fec0_mii_bus) {
1159 fep->mii_bus = fec0_mii_bus;
1160 mii_cnt++;
1161 return 0;
1162 }
1163 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001164 }
1165
Bryan Wue6b043d2010-03-31 02:10:44 +00001166 fep->mii_timeout = 0;
1167
1168 /*
1169 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001170 *
1171 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1172 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1173 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1174 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001175 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001176 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001177 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1178 fep->phy_speed--;
1179 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001180 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1181
1182 fep->mii_bus = mdiobus_alloc();
1183 if (fep->mii_bus == NULL) {
1184 err = -ENOMEM;
1185 goto err_out;
1186 }
1187
1188 fep->mii_bus->name = "fec_enet_mii_bus";
1189 fep->mii_bus->read = fec_enet_mdio_read;
1190 fep->mii_bus->write = fec_enet_mdio_write;
1191 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001192 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1193 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001194 fep->mii_bus->priv = fep;
1195 fep->mii_bus->parent = &pdev->dev;
1196
1197 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1198 if (!fep->mii_bus->irq) {
1199 err = -ENOMEM;
1200 goto err_out_free_mdiobus;
1201 }
1202
1203 for (i = 0; i < PHY_MAX_ADDR; i++)
1204 fep->mii_bus->irq[i] = PHY_POLL;
1205
Bryan Wue6b043d2010-03-31 02:10:44 +00001206 if (mdiobus_register(fep->mii_bus))
1207 goto err_out_free_mdio_irq;
1208
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001209 mii_cnt++;
1210
Shawn Guob5680e02011-01-05 21:13:13 +00001211 /* save fec0 mii_bus */
1212 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1213 fec0_mii_bus = fep->mii_bus;
1214
Bryan Wue6b043d2010-03-31 02:10:44 +00001215 return 0;
1216
Bryan Wue6b043d2010-03-31 02:10:44 +00001217err_out_free_mdio_irq:
1218 kfree(fep->mii_bus->irq);
1219err_out_free_mdiobus:
1220 mdiobus_free(fep->mii_bus);
1221err_out:
1222 return err;
1223}
1224
1225static void fec_enet_mii_remove(struct fec_enet_private *fep)
1226{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001227 if (--mii_cnt == 0) {
1228 mdiobus_unregister(fep->mii_bus);
1229 kfree(fep->mii_bus->irq);
1230 mdiobus_free(fep->mii_bus);
1231 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001232}
1233
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001234static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001235 struct ethtool_cmd *cmd)
1236{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001237 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001238 struct phy_device *phydev = fep->phy_dev;
1239
1240 if (!phydev)
1241 return -ENODEV;
1242
1243 return phy_ethtool_gset(phydev, cmd);
1244}
1245
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001246static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001247 struct ethtool_cmd *cmd)
1248{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001250 struct phy_device *phydev = fep->phy_dev;
1251
1252 if (!phydev)
1253 return -ENODEV;
1254
1255 return phy_ethtool_sset(phydev, cmd);
1256}
1257
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001258static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001259 struct ethtool_drvinfo *info)
1260{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001261 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jiri Pirko7826d432013-01-06 00:44:26 +00001263 strlcpy(info->driver, fep->pdev->dev.driver->name,
1264 sizeof(info->driver));
1265 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1266 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
Bryan Wue6b043d2010-03-31 02:10:44 +00001268
Frank Li5ebae4892013-01-06 16:25:07 +00001269static int fec_enet_get_ts_info(struct net_device *ndev,
1270 struct ethtool_ts_info *info)
1271{
1272 struct fec_enet_private *fep = netdev_priv(ndev);
1273
1274 if (fep->bufdesc_ex) {
1275
1276 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1277 SOF_TIMESTAMPING_RX_SOFTWARE |
1278 SOF_TIMESTAMPING_SOFTWARE |
1279 SOF_TIMESTAMPING_TX_HARDWARE |
1280 SOF_TIMESTAMPING_RX_HARDWARE |
1281 SOF_TIMESTAMPING_RAW_HARDWARE;
1282 if (fep->ptp_clock)
1283 info->phc_index = ptp_clock_index(fep->ptp_clock);
1284 else
1285 info->phc_index = -1;
1286
1287 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1288 (1 << HWTSTAMP_TX_ON);
1289
1290 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1291 (1 << HWTSTAMP_FILTER_ALL);
1292 return 0;
1293 } else {
1294 return ethtool_op_get_ts_info(ndev, info);
1295 }
1296}
1297
Frank Libaa70a52013-01-16 16:55:58 +00001298static void fec_enet_get_pauseparam(struct net_device *ndev,
1299 struct ethtool_pauseparam *pause)
1300{
1301 struct fec_enet_private *fep = netdev_priv(ndev);
1302
1303 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1304 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1305 pause->rx_pause = pause->tx_pause;
1306}
1307
1308static int fec_enet_set_pauseparam(struct net_device *ndev,
1309 struct ethtool_pauseparam *pause)
1310{
1311 struct fec_enet_private *fep = netdev_priv(ndev);
1312
1313 if (pause->tx_pause != pause->rx_pause) {
1314 netdev_info(ndev,
1315 "hardware only support enable/disable both tx and rx");
1316 return -EINVAL;
1317 }
1318
1319 fep->pause_flag = 0;
1320
1321 /* tx pause must be same as rx pause */
1322 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1323 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1324
1325 if (pause->rx_pause || pause->autoneg) {
1326 fep->phy_dev->supported |= ADVERTISED_Pause;
1327 fep->phy_dev->advertising |= ADVERTISED_Pause;
1328 } else {
1329 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1330 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1331 }
1332
1333 if (pause->autoneg) {
1334 if (netif_running(ndev))
1335 fec_stop(ndev);
1336 phy_start_aneg(fep->phy_dev);
1337 }
1338 if (netif_running(ndev))
1339 fec_restart(ndev, 0);
1340
1341 return 0;
1342}
1343
stephen hemminger9b07be42012-01-04 12:59:49 +00001344static const struct ethtool_ops fec_enet_ethtool_ops = {
Frank Libaa70a52013-01-16 16:55:58 +00001345 .get_pauseparam = fec_enet_get_pauseparam,
1346 .set_pauseparam = fec_enet_set_pauseparam,
Bryan Wue6b043d2010-03-31 02:10:44 +00001347 .get_settings = fec_enet_get_settings,
1348 .set_settings = fec_enet_set_settings,
1349 .get_drvinfo = fec_enet_get_drvinfo,
1350 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00001351 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00001352};
1353
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001354static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001355{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001356 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001357 struct phy_device *phydev = fep->phy_dev;
1358
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001359 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001360 return -EINVAL;
1361
1362 if (!phydev)
1363 return -ENODEV;
1364
Frank Liff43da82013-01-03 16:04:23 +00001365 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
Frank Li6605b732012-10-30 18:25:31 +00001366 return fec_ptp_ioctl(ndev, rq, cmd);
Frank Liff43da82013-01-03 16:04:23 +00001367
Richard Cochran28b04112010-07-17 08:48:55 +00001368 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001371static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001372{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001373 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001374 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001375 struct sk_buff *skb;
1376 struct bufdesc *bdp;
1377
1378 bdp = fep->rx_bd_base;
1379 for (i = 0; i < RX_RING_SIZE; i++) {
1380 skb = fep->rx_skbuff[i];
1381
1382 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001383 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001384 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1385 if (skb)
1386 dev_kfree_skb(skb);
Frank Liff43da82013-01-03 16:04:23 +00001387 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001388 }
1389
1390 bdp = fep->tx_bd_base;
1391 for (i = 0; i < TX_RING_SIZE; i++)
1392 kfree(fep->tx_bounce[i]);
1393}
1394
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001395static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001396{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001397 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001398 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001399 struct sk_buff *skb;
1400 struct bufdesc *bdp;
1401
1402 bdp = fep->rx_bd_base;
1403 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001404 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001405 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001406 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001407 return -ENOMEM;
1408 }
1409 fep->rx_skbuff[i] = skb;
1410
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001411 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001412 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1413 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00001414
1415 if (fep->bufdesc_ex) {
1416 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1417 ebdp->cbd_esc = BD_ENET_RX_INT;
1418 }
1419
1420 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001421 }
1422
1423 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001424 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001425 bdp->cbd_sc |= BD_SC_WRAP;
1426
1427 bdp = fep->tx_bd_base;
1428 for (i = 0; i < TX_RING_SIZE; i++) {
1429 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1430
1431 bdp->cbd_sc = 0;
1432 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001433
Frank Liff43da82013-01-03 16:04:23 +00001434 if (fep->bufdesc_ex) {
1435 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00001436 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00001437 }
1438
1439 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001440 }
1441
1442 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001443 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001444 bdp->cbd_sc |= BD_SC_WRAP;
1445
1446 return 0;
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001450fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001452 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001453 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Frank Lidc975382013-01-28 18:31:42 +00001455 napi_enable(&fep->napi);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* I should reset the ring buffers here, but I don't yet know
1458 * a simple way to do that.
1459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001461 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001462 if (ret)
1463 return ret;
1464
Bryan Wu418bd0d2010-05-28 03:40:39 -07001465 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001466 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001467 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001468 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001469 return ret;
1470 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001471 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001472 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001478fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001480 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Sascha Hauer22f6b862009-04-15 01:32:18 +00001482 /* Don't know what to do yet. */
Georg Hofmann3f104c32013-03-14 06:54:09 +00001483 napi_disable(&fep->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001485 netif_stop_queue(ndev);
1486 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001488 if (fep->phy_dev) {
1489 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001490 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001491 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001492
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001493 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/* Set or clear the multicast filter for this adaptor.
1499 * Skeleton taken from sunlance driver.
1500 * The CPM Ethernet implementation allows Multicast as well as individual
1501 * MAC address filtering. Some of the drivers check to make sure it is
1502 * a group multicast address, and discard those that are not. I guess I
1503 * will do the same for now, but just remove the test if you want
1504 * individual filtering as well (do the upper net layers want or support
1505 * this kind of feature?).
1506 */
1507
1508#define HASH_BITS 6 /* #bits in hash */
1509#define CRC32_POLY 0xEDB88320
1510
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001511static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001513 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001514 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001515 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned char hash;
1517
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001518 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001519 tmp = readl(fep->hwp + FEC_R_CNTRL);
1520 tmp |= 0x8;
1521 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001522 return;
1523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Sascha Hauer4e831832009-04-15 01:32:19 +00001525 tmp = readl(fep->hwp + FEC_R_CNTRL);
1526 tmp &= ~0x8;
1527 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001528
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001529 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001530 /* Catch all multicast addresses, so set the
1531 * filter to all 1's
1532 */
1533 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1534 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Sascha Hauer4e831832009-04-15 01:32:19 +00001536 return;
1537 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001538
Sascha Hauer4e831832009-04-15 01:32:19 +00001539 /* Clear filter and add the addresses in hash register
1540 */
1541 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1542 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001544 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001545 /* calculate crc32 value of mac address */
1546 crc = 0xffffffff;
1547
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001548 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001549 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001550 for (bit = 0; bit < 8; bit++, data >>= 1) {
1551 crc = (crc >> 1) ^
1552 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001555
1556 /* only upper 6 bits (HASH_BITS) are used
1557 * which point to specific bit in he hash registers
1558 */
1559 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1560
1561 if (hash > 31) {
1562 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1563 tmp |= 1 << (hash - 32);
1564 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1565 } else {
1566 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1567 tmp |= 1 << hash;
1568 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571}
1572
Sascha Hauer22f6b862009-04-15 01:32:18 +00001573/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001574static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001575fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001577 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001578 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Sascha Hauer009fda82009-04-15 01:32:23 +00001580 if (!is_valid_ether_addr(addr->sa_data))
1581 return -EADDRNOTAVAIL;
1582
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001583 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001584
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001585 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1586 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001587 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001588 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001589 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001593#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001594/**
1595 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001596 * @dev: The FEC network adapter
1597 *
1598 * Polled functionality used by netconsole and others in non interrupt mode
1599 *
1600 */
Wei Yongjun47a52472013-03-20 05:06:11 +00001601static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001602{
1603 int i;
1604 struct fec_enet_private *fep = netdev_priv(dev);
1605
1606 for (i = 0; i < FEC_IRQ_NUM; i++) {
1607 if (fep->irq[i] > 0) {
1608 disable_irq(fep->irq[i]);
1609 fec_enet_interrupt(fep->irq[i], dev);
1610 enable_irq(fep->irq[i]);
1611 }
1612 }
1613}
1614#endif
1615
Sascha Hauer009fda82009-04-15 01:32:23 +00001616static const struct net_device_ops fec_netdev_ops = {
1617 .ndo_open = fec_enet_open,
1618 .ndo_stop = fec_enet_close,
1619 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001620 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001621 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001622 .ndo_validate_addr = eth_validate_addr,
1623 .ndo_tx_timeout = fec_timeout,
1624 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001625 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001626#ifdef CONFIG_NET_POLL_CONTROLLER
1627 .ndo_poll_controller = fec_poll_controller,
1628#endif
Sascha Hauer009fda82009-04-15 01:32:23 +00001629};
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001633 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001635static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001637 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001638 struct bufdesc *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001640 /* Allocate memory for buffer descriptors. */
1641 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001642 GFP_KERNEL);
1643 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001644 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10001645
Frank Li14109a52013-03-26 16:12:03 +00001646 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001647 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001648
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001649 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Shawn Guo49da97d2011-01-05 21:13:11 +00001651 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001652 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001654 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 fep->rx_bd_base = cbd_base;
Frank Liff43da82013-01-03 16:04:23 +00001656 if (fep->bufdesc_ex)
1657 fep->tx_bd_base = (struct bufdesc *)
1658 (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1659 else
1660 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Sascha Hauer22f6b862009-04-15 01:32:18 +00001662 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001663 ndev->watchdog_timeo = TX_TIMEOUT;
1664 ndev->netdev_ops = &fec_netdev_ops;
1665 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001666
Frank Lidc975382013-01-28 18:31:42 +00001667 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1668 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1669
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001670 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return 0;
1673}
1674
Shawn Guoca2cc332011-06-25 02:04:35 +08001675#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05001676static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001677{
1678 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001679 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001680 struct device_node *np = pdev->dev.of_node;
1681
1682 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001683 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001684
Shawn Guoa3caad02012-06-27 03:45:24 +00001685 of_property_read_u32(np, "phy-reset-duration", &msec);
1686 /* A sane reset duration should not be longer than 1s */
1687 if (msec > 1000)
1688 msec = 1;
1689
Shawn Guoca2cc332011-06-25 02:04:35 +08001690 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001691 if (!gpio_is_valid(phy_reset))
1692 return;
1693
Shawn Guo119fc002012-06-27 03:45:22 +00001694 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1695 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001696 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001697 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001698 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001699 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001700 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001701 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001702}
1703#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001704static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001705{
1706 /*
1707 * In case of platform probe, the reset has been done
1708 * by machine code.
1709 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001710}
1711#endif /* CONFIG_OF */
1712
Bill Pemberton33897cc2012-12-03 09:23:58 -05001713static int
Sascha Haueread73182009-01-28 23:03:11 +00001714fec_probe(struct platform_device *pdev)
1715{
1716 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001717 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001718 struct net_device *ndev;
1719 int i, irq, ret = 0;
1720 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001721 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001722 static int dev_id;
Shawn Guob2bccee2012-05-06 20:24:04 +08001723 struct pinctrl *pinctrl;
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001724 struct regulator *reg_phy;
Shawn Guoca2cc332011-06-25 02:04:35 +08001725
1726 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1727 if (of_id)
1728 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001729
1730 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1731 if (!r)
1732 return -ENXIO;
1733
Sascha Haueread73182009-01-28 23:03:11 +00001734 /* Init network device */
1735 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00001736 if (!ndev)
1737 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00001738
1739 SET_NETDEV_DEV(ndev, &pdev->dev);
1740
1741 /* setup board info structure */
1742 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001743
Frank Libaa70a52013-01-16 16:55:58 +00001744 /* default enable pause frame auto negotiation */
1745 if (pdev->id_entry &&
1746 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
1747 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
1748
Fabio Estevam83e519b2013-03-11 07:32:55 +00001749 fep->hwp = devm_request_and_ioremap(&pdev->dev, r);
Bryan Wue6b043d2010-03-31 02:10:44 +00001750 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00001751 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00001752
Frank Liff43da82013-01-03 16:04:23 +00001753 fep->bufdesc_ex = 0;
1754
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001755 if (!fep->hwp) {
Sascha Haueread73182009-01-28 23:03:11 +00001756 ret = -ENOMEM;
1757 goto failed_ioremap;
1758 }
1759
1760 platform_set_drvdata(pdev, ndev);
1761
Guenter Roeck6c5f7802013-04-02 09:35:10 +00001762 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08001763 if (ret < 0) {
1764 pdata = pdev->dev.platform_data;
1765 if (pdata)
1766 fep->phy_interface = pdata->phy;
1767 else
1768 fep->phy_interface = PHY_INTERFACE_MODE_MII;
1769 } else {
1770 fep->phy_interface = ret;
1771 }
1772
Fabio Estevame2f8d552013-02-22 06:40:45 +00001773 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1774 if (IS_ERR(pinctrl)) {
1775 ret = PTR_ERR(pinctrl);
1776 goto failed_pin;
1777 }
1778
1779 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1780 if (IS_ERR(fep->clk_ipg)) {
1781 ret = PTR_ERR(fep->clk_ipg);
1782 goto failed_clk;
1783 }
1784
1785 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1786 if (IS_ERR(fep->clk_ahb)) {
1787 ret = PTR_ERR(fep->clk_ahb);
1788 goto failed_clk;
1789 }
1790
1791 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001792 fep->bufdesc_ex =
1793 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00001794 if (IS_ERR(fep->clk_ptp)) {
1795 ret = PTR_ERR(fep->clk_ptp);
1796 fep->bufdesc_ex = 0;
1797 }
1798
1799 clk_prepare_enable(fep->clk_ahb);
1800 clk_prepare_enable(fep->clk_ipg);
1801 if (!IS_ERR(fep->clk_ptp))
1802 clk_prepare_enable(fep->clk_ptp);
1803
1804 reg_phy = devm_regulator_get(&pdev->dev, "phy");
1805 if (!IS_ERR(reg_phy)) {
1806 ret = regulator_enable(reg_phy);
1807 if (ret) {
1808 dev_err(&pdev->dev,
1809 "Failed to enable phy regulator: %d\n", ret);
1810 goto failed_regulator;
1811 }
1812 }
1813
1814 fec_reset_phy(pdev);
1815
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001816 if (fep->bufdesc_ex)
1817 fec_ptp_init(ndev, pdev);
1818
1819 ret = fec_enet_init(ndev);
1820 if (ret)
1821 goto failed_init;
1822
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001823 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001824 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00001825 if (irq < 0) {
1826 if (i)
1827 break;
1828 ret = irq;
1829 goto failed_irq;
1830 }
Sascha Haueread73182009-01-28 23:03:11 +00001831 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1832 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01001833 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00001834 irq = platform_get_irq(pdev, i);
1835 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001836 }
1837 goto failed_irq;
1838 }
1839 }
1840
Bryan Wue6b043d2010-03-31 02:10:44 +00001841 ret = fec_enet_mii_init(pdev);
1842 if (ret)
1843 goto failed_mii_init;
1844
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001845 /* Carrier starts down, phylib will bring it up */
1846 netif_carrier_off(ndev);
1847
Sascha Haueread73182009-01-28 23:03:11 +00001848 ret = register_netdev(ndev);
1849 if (ret)
1850 goto failed_register;
1851
Fabio Estevameb1d0642013-04-13 07:25:36 +00001852 if (fep->bufdesc_ex && fep->ptp_clock)
1853 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
1854
Sascha Haueread73182009-01-28 23:03:11 +00001855 return 0;
1856
1857failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001858 fec_enet_mii_remove(fep);
1859failed_mii_init:
Fabio Estevame2f8d552013-02-22 06:40:45 +00001860failed_init:
1861 for (i = 0; i < FEC_IRQ_NUM; i++) {
1862 irq = platform_get_irq(pdev, i);
1863 if (irq > 0)
1864 free_irq(irq, ndev);
1865 }
1866failed_irq:
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001867failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001868 clk_disable_unprepare(fep->clk_ahb);
1869 clk_disable_unprepare(fep->clk_ipg);
Frank Liff43da82013-01-03 16:04:23 +00001870 if (!IS_ERR(fep->clk_ptp))
1871 clk_disable_unprepare(fep->clk_ptp);
Shawn Guob2bccee2012-05-06 20:24:04 +08001872failed_pin:
Sascha Haueread73182009-01-28 23:03:11 +00001873failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00001874failed_ioremap:
1875 free_netdev(ndev);
1876
1877 return ret;
1878}
1879
Bill Pemberton33897cc2012-12-03 09:23:58 -05001880static int
Sascha Haueread73182009-01-28 23:03:11 +00001881fec_drv_remove(struct platform_device *pdev)
1882{
1883 struct net_device *ndev = platform_get_drvdata(pdev);
1884 struct fec_enet_private *fep = netdev_priv(ndev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001885 int i;
Sascha Haueread73182009-01-28 23:03:11 +00001886
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001887 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001888 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00001889 del_timer_sync(&fep->time_keep);
1890 clk_disable_unprepare(fep->clk_ptp);
1891 if (fep->ptp_clock)
1892 ptp_clock_unregister(fep->ptp_clock);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001893 clk_disable_unprepare(fep->clk_ahb);
1894 clk_disable_unprepare(fep->clk_ipg);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001895 for (i = 0; i < FEC_IRQ_NUM; i++) {
1896 int irq = platform_get_irq(pdev, i);
1897 if (irq > 0)
1898 free_irq(irq, ndev);
1899 }
Sascha Haueread73182009-01-28 23:03:11 +00001900 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001901
Uwe Kleine-Königb3cde362011-01-25 18:03:37 +01001902 platform_set_drvdata(pdev, NULL);
1903
Sascha Haueread73182009-01-28 23:03:11 +00001904 return 0;
1905}
1906
Fabio Estevambf7bfd72013-04-16 08:17:46 +00001907#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00001908static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001909fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001910{
Eric Benard87cad5c2010-06-18 04:19:54 +00001911 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001912 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001913
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001914 if (netif_running(ndev)) {
1915 fec_stop(ndev);
1916 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001917 }
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001918 clk_disable_unprepare(fep->clk_ahb);
1919 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001920
Sascha Haueread73182009-01-28 23:03:11 +00001921 return 0;
1922}
1923
1924static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001925fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001926{
Eric Benard87cad5c2010-06-18 04:19:54 +00001927 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001928 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001929
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001930 clk_prepare_enable(fep->clk_ahb);
1931 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001932 if (netif_running(ndev)) {
1933 fec_restart(ndev, fep->full_duplex);
1934 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001935 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001936
Sascha Haueread73182009-01-28 23:03:11 +00001937 return 0;
1938}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00001939#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00001940
Fabio Estevambf7bfd72013-04-16 08:17:46 +00001941static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00001942
Sascha Haueread73182009-01-28 23:03:11 +00001943static struct platform_driver fec_driver = {
1944 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00001945 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00001946 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00001947 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08001948 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00001949 },
Shawn Guob5680e02011-01-05 21:13:13 +00001950 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00001951 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05001952 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00001953};
1954
Fabio Estevamaaca2372012-01-23 16:44:50 +00001955module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957MODULE_LICENSE("GPL");