blob: 0704bcab178ab64a036e6cec7e44167622c7271c [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>
32#include <linux/pci.h>
33#include <linux/init.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/skbuff.h>
38#include <linux/spinlock.h>
39#include <linux/workqueue.h>
40#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000041#include <linux/io.h>
42#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000043#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000044#include <linux/platform_device.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000045#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070046#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080047#include <linux/of.h>
48#include <linux/of_device.h>
49#include <linux/of_gpio.h>
50#include <linux/of_net.h>
Shawn Guob2bccee2012-05-06 20:24:04 +080051#include <linux/pinctrl/consumer.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000052#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Greg Ungerer080853a2007-07-30 16:28:46 +100054#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000055
Shawn Guob5680e02011-01-05 21:13:13 +000056#ifndef CONFIG_ARM
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/coldfire.h>
58#include <asm/mcfsim.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000059#endif
Sascha Hauer6f501b12009-01-28 23:03:05 +000060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010063#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000064#define FEC_ALIGNMENT 0xf
65#else
66#define FEC_ALIGNMENT 0x3
67#endif
68
Shawn Guob5680e02011-01-05 21:13:13 +000069#define DRIVER_NAME "fec"
70
71/* Controller is ENET-MAC */
72#define FEC_QUIRK_ENET_MAC (1 << 0)
73/* Controller needs driver to swap frame */
74#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080075/* Controller uses gasket */
76#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000077/* Controller has GBIT support */
78#define FEC_QUIRK_HAS_GBIT (1 << 3)
Shawn Guob5680e02011-01-05 21:13:13 +000079
80static struct platform_device_id fec_devtype[] = {
81 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080082 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000083 .name = DRIVER_NAME,
84 .driver_data = 0,
85 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080086 .name = "imx25-fec",
87 .driver_data = FEC_QUIRK_USE_GASKET,
88 }, {
89 .name = "imx27-fec",
90 .driver_data = 0,
91 }, {
Shawn Guob5680e02011-01-05 21:13:13 +000092 .name = "imx28-fec",
93 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +080094 }, {
Shawn Guo230dec62011-09-23 02:12:48 +000095 .name = "imx6q-fec",
96 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT,
97 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080098 /* sentinel */
99 }
Shawn Guob5680e02011-01-05 21:13:13 +0000100};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800101MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000102
Shawn Guoca2cc332011-06-25 02:04:35 +0800103enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000104 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800105 IMX27_FEC, /* runs on i.mx27/35/51 */
106 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000107 IMX6Q_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800108};
109
110static const struct of_device_id fec_dt_ids[] = {
111 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
112 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
113 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000114 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800115 { /* sentinel */ }
116};
117MODULE_DEVICE_TABLE(of, fec_dt_ids);
118
Shawn Guo49da97d2011-01-05 21:13:11 +0000119static unsigned char macaddr[ETH_ALEN];
120module_param_array(macaddr, byte, NULL, 0);
121MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
122
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000123#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
125 * Some hardware gets it MAC address out of local flash memory.
126 * if this is non-zero then assume it is the address to get MAC from.
127 */
128#if defined(CONFIG_NETtel)
129#define FEC_FLASHMAC 0xf0006006
130#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
131#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#elif defined(CONFIG_CANCam)
133#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000134#elif defined (CONFIG_M5272C3)
135#define FEC_FLASHMAC (0xffe04000 + 4)
136#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000137#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#else
139#define FEC_FLASHMAC 0
140#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800141#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000142
Greg Ungerer562d2f82005-11-07 14:09:50 +1000143#if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000144#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000145#endif
146
Sascha Hauer22f6b862009-04-15 01:32:18 +0000147/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
149#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
150#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
151#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
152#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
153#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
154#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
155#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
156#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
157#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
158
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000159#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* The FEC stores dest/src/type, data, and checksum for receive packets.
162 */
163#define PKT_MAXBUF_SIZE 1518
164#define PKT_MINBUF_SIZE 64
165#define PKT_MAXBLR_SIZE 1520
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Matt Waddel6b265292006-06-27 13:10:56 +1000168 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * size bits. Other FEC hardware does not, so we need to take that into
170 * account when setting it.
171 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000172#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100173 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
175#else
176#define OPT_FRAME_SIZE 0
177#endif
178
Bryan Wue6b043d2010-03-31 02:10:44 +0000179/* FEC MII MMFR bits definition */
180#define FEC_MMFR_ST (1 << 30)
181#define FEC_MMFR_OP_READ (2 << 28)
182#define FEC_MMFR_OP_WRITE (1 << 28)
183#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
184#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
185#define FEC_MMFR_TA (2 << 16)
186#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500188#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Sascha Hauer22f6b862009-04-15 01:32:18 +0000190/* Transmitter timeout */
191#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000193static int mii_cnt;
194
Shawn Guob5680e02011-01-05 21:13:13 +0000195static void *swap_buffer(void *bufaddr, int len)
196{
197 int i;
198 unsigned int *buf = bufaddr;
199
200 for (i = 0; i < (len + 3) / 4; i++, buf++)
201 *buf = cpu_to_be32(*buf);
202
203 return bufaddr;
204}
205
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000206static netdev_tx_t
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100207fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100209 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000210 const struct platform_device_id *id_entry =
211 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000212 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000213 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000214 unsigned short status;
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000215 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!fep->link) {
218 /* Link is down or autonegotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000219 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000222 spin_lock_irqsave(&fep->hw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Fill in a Tx ring entry */
224 bdp = fep->cur_tx;
225
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000226 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000227
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000228 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Ooops. All transmit buffers are full. Bail out.
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100230 * This should not happen, since ndev->tbusy should be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100232 printk("%s: tx queue full!.\n", ndev->name);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000233 spin_unlock_irqrestore(&fep->hw_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000234 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Sascha Hauer22f6b862009-04-15 01:32:18 +0000237 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000238 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Sascha Hauer22f6b862009-04-15 01:32:18 +0000240 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000241 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 bdp->cbd_datlen = skb->len;
243
244 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000245 * On some FEC implementations data must be aligned on
246 * 4-byte boundaries. Use bounce buffers to copy data
247 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Greg Ungerer9555b312009-08-06 17:58:18 +0000249 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned int index;
251 index = bdp - fep->tx_bd_base;
Uwe Kleine-König8a73b0b2011-01-13 21:34:31 +0100252 memcpy(fep->tx_bounce[index], skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000253 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
Shawn Guob5680e02011-01-05 21:13:13 +0000256 /*
257 * Some design made an incorrect assumption on endian mode of
258 * the system that it's running on. As the result, driver has to
259 * swap every frame going to and coming from the controller.
260 */
261 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
262 swap_buffer(bufaddr, skb->len);
263
Sascha Hauer22f6b862009-04-15 01:32:18 +0000264 /* Save skb pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 fep->tx_skbuff[fep->skb_cur] = skb;
266
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100267 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* Push the data cache so the CPM does not get stale memory
271 * data.
272 */
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100273 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000274 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000276 /* Send it on its way. Tell FEC it's ready, interrupt when done,
277 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000279 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000281 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Frank Li6605b732012-10-30 18:25:31 +0000283#ifdef CONFIG_FEC_PTP
284 bdp->cbd_bdu = 0;
285 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
286 fep->hwts_tx_en)) {
287 bdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
288 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
289 } else {
290
291 bdp->cbd_esc = BD_ENET_TX_INT;
292 }
293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000295 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Sascha Hauer22f6b862009-04-15 01:32:18 +0000297 /* If this was the last BD in the ring, start at the beginning again. */
298 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000300 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (bdp == fep->dirty_tx) {
304 fep->tx_full = 1;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100305 netif_stop_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Sascha Hauer2e285322009-04-15 01:32:16 +0000308 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Richard Cochran18a03b92011-06-12 02:18:59 +0000310 skb_tx_timestamp(skb);
311
Richard Cochrana0087a32011-06-19 03:31:40 +0000312 spin_unlock_irqrestore(&fep->hw_lock, flags);
313
Patrick McHardy6ed10652009-06-23 06:03:08 +0000314 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Uwe Kleine-König45993652011-01-19 20:47:04 +0100317/* This function is called to start or restart the FEC during a link
318 * change. This only happens when switching between half and full
319 * duplex.
320 */
321static void
322fec_restart(struct net_device *ndev, int duplex)
323{
324 struct fec_enet_private *fep = netdev_priv(ndev);
325 const struct platform_device_id *id_entry =
326 platform_get_device_id(fep->pdev);
327 int i;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100328 u32 temp_mac[2];
329 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000330 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100331
332 /* Whack a reset. We should wait for this. */
333 writel(1, fep->hwp + FEC_ECNTRL);
334 udelay(10);
335
336 /*
337 * enet-mac reset will reset mac address registers too,
338 * so need to reconfigure it.
339 */
340 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
341 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
342 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
343 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
344 }
345
346 /* Clear any outstanding interrupt. */
347 writel(0xffc00000, fep->hwp + FEC_IEVENT);
348
349 /* Reset all multicast. */
350 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
351 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
352#ifndef CONFIG_M5272
353 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
354 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
355#endif
356
357 /* Set maximum receive buffer size. */
358 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
359
360 /* Set receive and transmit descriptor base. */
361 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
362 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
363 fep->hwp + FEC_X_DES_START);
364
365 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
366 fep->cur_rx = fep->rx_bd_base;
367
368 /* Reset SKB transmit buffers. */
369 fep->skb_cur = fep->skb_dirty = 0;
370 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
371 if (fep->tx_skbuff[i]) {
372 dev_kfree_skb_any(fep->tx_skbuff[i]);
373 fep->tx_skbuff[i] = NULL;
374 }
375 }
376
377 /* Enable MII mode */
378 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100379 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100380 writel(0x04, fep->hwp + FEC_X_CNTRL);
381 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100382 /* No Rcv on Xmit */
383 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100384 writel(0x0, fep->hwp + FEC_X_CNTRL);
385 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100386
Uwe Kleine-König45993652011-01-19 20:47:04 +0100387 fep->full_duplex = duplex;
388
389 /* Set MII speed */
390 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
391
392 /*
393 * The phy interface and speed need to get configured
394 * differently on enet-mac.
395 */
396 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100397 /* Enable flow control and length check */
398 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100399
Shawn Guo230dec62011-09-23 02:12:48 +0000400 /* RGMII, RMII or MII */
401 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
402 rcntl |= (1 << 6);
403 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100404 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100405 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100406 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100407
Shawn Guo230dec62011-09-23 02:12:48 +0000408 /* 1G, 100M or 10M */
409 if (fep->phy_dev) {
410 if (fep->phy_dev->speed == SPEED_1000)
411 ecntl |= (1 << 5);
412 else if (fep->phy_dev->speed == SPEED_100)
413 rcntl &= ~(1 << 9);
414 else
415 rcntl |= (1 << 9);
416 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100417 } else {
418#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800419 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000420 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100421 /* disable the gasket and wait */
422 writel(0, fep->hwp + FEC_MIIGSK_ENR);
423 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
424 udelay(1);
425
426 /*
427 * configure the gasket:
428 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800429 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100430 */
Eric Benard8d82f212012-01-12 06:10:28 +0000431 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
432 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
433 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
434 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
435 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100436
437 /* re-enable the gasket */
438 writel(2, fep->hwp + FEC_MIIGSK_ENR);
439 }
440#endif
441 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100442 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100443
Shawn Guo230dec62011-09-23 02:12:48 +0000444 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
445 /* enable ENET endian swap */
446 ecntl |= (1 << 8);
447 /* enable ENET store and forward mode */
448 writel(1 << 8, fep->hwp + FEC_X_WMRK);
449 }
450
Frank Li6605b732012-10-30 18:25:31 +0000451#ifdef CONFIG_FEC_PTP
452 ecntl |= (1 << 4);
453#endif
454
Uwe Kleine-König45993652011-01-19 20:47:04 +0100455 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000456 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100457 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
458
Frank Li6605b732012-10-30 18:25:31 +0000459#ifdef CONFIG_FEC_PTP
460 fec_ptp_start_cyclecounter(ndev);
461#endif
Uwe Kleine-König45993652011-01-19 20:47:04 +0100462 /* Enable interrupts we wish to service */
463 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
464}
465
466static void
467fec_stop(struct net_device *ndev)
468{
469 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000470 const struct platform_device_id *id_entry =
471 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000472 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100473
474 /* We cannot expect a graceful transmit stop without link !!! */
475 if (fep->link) {
476 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
477 udelay(10);
478 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
479 printk("fec_stop : Graceful transmit stop did not complete !\n");
480 }
481
482 /* Whack a reset. We should wait for this. */
483 writel(1, fep->hwp + FEC_ECNTRL);
484 udelay(10);
485 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
486 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000487
488 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000489 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000490 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000491 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
492 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100493}
494
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100497fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100499 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100501 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100503 fec_restart(ndev, fep->full_duplex);
504 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100508fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000511 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000512 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct sk_buff *skb;
514
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100515 fep = netdev_priv(ndev);
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000516 spin_lock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bdp = fep->dirty_tx;
518
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000519 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000520 if (bdp == fep->cur_tx && fep->tx_full == 0)
521 break;
522
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100523 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
524 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000525 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 skb = fep->tx_skbuff[fep->skb_dirty];
528 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000529 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 BD_ENET_TX_RL | BD_ENET_TX_UN |
531 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100532 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000533 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100534 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000535 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100536 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000537 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100538 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000539 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100540 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000541 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100542 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100544 ndev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Frank Li6605b732012-10-30 18:25:31 +0000547#ifdef CONFIG_FEC_PTP
548 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
549 struct skb_shared_hwtstamps shhwtstamps;
550 unsigned long flags;
551
552 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
553 spin_lock_irqsave(&fep->tmreg_lock, flags);
554 shhwtstamps.hwtstamp = ns_to_ktime(
555 timecounter_cyc2time(&fep->tc, bdp->ts));
556 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
557 skb_tstamp_tx(skb, &shhwtstamps);
558 }
559#endif
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000560 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Deferred means some collisions occurred during transmit,
564 * but we eventually sent the packet OK.
565 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000566 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100567 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400568
Sascha Hauer22f6b862009-04-15 01:32:18 +0000569 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dev_kfree_skb_any(skb);
571 fep->tx_skbuff[fep->skb_dirty] = NULL;
572 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400573
Sascha Hauer22f6b862009-04-15 01:32:18 +0000574 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000575 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 bdp = fep->tx_bd_base;
577 else
578 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400579
Sascha Hauer22f6b862009-04-15 01:32:18 +0000580 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582 if (fep->tx_full) {
583 fep->tx_full = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100584 if (netif_queue_stopped(ndev))
585 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000588 fep->dirty_tx = bdp;
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000589 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592
593/* During a receive, the cur_rx points to the current incoming buffer.
594 * When we update through the ring, if the next incoming buffer has
595 * not been given to the system, we just set the empty indicator,
596 * effectively tossing the packet.
597 */
598static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100599fec_enet_rx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100601 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000602 const struct platform_device_id *id_entry =
603 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000604 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000605 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct sk_buff *skb;
607 ushort pkt_len;
608 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400609
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000610#ifdef CONFIG_M532x
611 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400612#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000614 spin_lock(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* First, grab all of the stats for the incoming packet.
617 * These get messed up if we get called due to a busy condition.
618 */
619 bdp = fep->cur_rx;
620
Sascha Hauer22f6b862009-04-15 01:32:18 +0000621 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Sascha Hauer22f6b862009-04-15 01:32:18 +0000623 /* Since we have allocated space to hold a complete frame,
624 * the last indicator should be set.
625 */
626 if ((status & BD_ENET_RX_LAST) == 0)
627 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Sascha Hauer22f6b862009-04-15 01:32:18 +0000629 if (!fep->opened)
630 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Sascha Hauer22f6b862009-04-15 01:32:18 +0000632 /* Check for errors. */
633 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100635 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000636 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
637 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100638 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000639 }
640 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100641 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000642 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100643 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000644 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100645 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000647
648 /* Report late collisions as a frame error.
649 * On this error, the BD is closed, but we don't know what we
650 * have in the buffer. So, just drop this frame on the floor.
651 */
652 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100653 ndev->stats.rx_errors++;
654 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000655 goto rx_processing_done;
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Sascha Hauer22f6b862009-04-15 01:32:18 +0000658 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100659 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000660 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100661 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000662 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100664 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
665 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000666
Shawn Guob5680e02011-01-05 21:13:13 +0000667 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
668 swap_buffer(data, pkt_len);
669
Sascha Hauer22f6b862009-04-15 01:32:18 +0000670 /* This does 16 byte alignment, exactly what we need.
671 * The packet length includes FCS, but we don't want to
672 * include that when passing upstream as it messes up
673 * bridging applications.
674 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000675 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Sascha Hauer85498892009-04-15 01:32:21 +0000677 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000678 printk("%s: Memory squeeze, dropping packet.\n",
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100679 ndev->name);
680 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000681 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000682 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000683 skb_put(skb, pkt_len - 4); /* Make room */
684 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100685 skb->protocol = eth_type_trans(skb, ndev);
Frank Li6605b732012-10-30 18:25:31 +0000686#ifdef CONFIG_FEC_PTP
687 /* Get receive timestamp from the skb */
688 if (fep->hwts_rx_en) {
689 struct skb_shared_hwtstamps *shhwtstamps =
690 skb_hwtstamps(skb);
691 unsigned long flags;
692
693 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
694
695 spin_lock_irqsave(&fep->tmreg_lock, flags);
696 shhwtstamps->hwtstamp = ns_to_ktime(
697 timecounter_cyc2time(&fep->tc, bdp->ts));
698 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
699 }
700#endif
Richard Cochran18a03b92011-06-12 02:18:59 +0000701 if (!skb_defer_rx_timestamp(skb))
702 netif_rx(skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000703 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000704
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100705 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
706 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000707rx_processing_done:
708 /* Clear the status flags for this buffer */
709 status &= ~BD_ENET_RX_STATS;
710
711 /* Mark the buffer empty */
712 status |= BD_ENET_RX_EMPTY;
713 bdp->cbd_sc = status;
714
Frank Li6605b732012-10-30 18:25:31 +0000715#ifdef CONFIG_FEC_PTP
716 bdp->cbd_esc = BD_ENET_RX_INT;
717 bdp->cbd_prot = 0;
718 bdp->cbd_bdu = 0;
719#endif
720
Sascha Hauer22f6b862009-04-15 01:32:18 +0000721 /* Update BD pointer to next entry */
722 if (status & BD_ENET_RX_WRAP)
723 bdp = fep->rx_bd_base;
724 else
725 bdp++;
726 /* Doing this here will keep the FEC running while we process
727 * incoming frames. On a heavily loaded network, we should be
728 * able to keep up at the expense of system resources.
729 */
730 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000732 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000734 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Uwe Kleine-König45993652011-01-19 20:47:04 +0100737static irqreturn_t
738fec_enet_interrupt(int irq, void *dev_id)
739{
740 struct net_device *ndev = dev_id;
741 struct fec_enet_private *fep = netdev_priv(ndev);
742 uint int_events;
743 irqreturn_t ret = IRQ_NONE;
744
745 do {
746 int_events = readl(fep->hwp + FEC_IEVENT);
747 writel(int_events, fep->hwp + FEC_IEVENT);
748
749 if (int_events & FEC_ENET_RXF) {
750 ret = IRQ_HANDLED;
751 fec_enet_rx(ndev);
752 }
753
754 /* Transmit OK, or non-fatal error. Update the buffer
755 * descriptors. FEC handles all errors, we just discover
756 * them as part of the transmit process.
757 */
758 if (int_events & FEC_ENET_TXF) {
759 ret = IRQ_HANDLED;
760 fec_enet_tx(ndev);
761 }
762
763 if (int_events & FEC_ENET_MII) {
764 ret = IRQ_HANDLED;
765 complete(&fep->mdio_done);
766 }
767 } while (int_events);
768
769 return ret;
770}
771
772
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/* ------------------------------------------------------------------------- */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100775static void __inline__ fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100777 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +0000778 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000779 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Shawn Guo49da97d2011-01-05 21:13:11 +0000781 /*
782 * try to get mac address in following order:
783 *
784 * 1) module parameter via kernel command line in form
785 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
786 */
787 iap = macaddr;
788
Shawn Guoca2cc332011-06-25 02:04:35 +0800789#ifdef CONFIG_OF
Shawn Guo49da97d2011-01-05 21:13:11 +0000790 /*
Shawn Guoca2cc332011-06-25 02:04:35 +0800791 * 2) from device tree data
792 */
793 if (!is_valid_ether_addr(iap)) {
794 struct device_node *np = fep->pdev->dev.of_node;
795 if (np) {
796 const char *mac = of_get_mac_address(np);
797 if (mac)
798 iap = (unsigned char *) mac;
799 }
800 }
801#endif
802
803 /*
804 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +0000805 */
806 if (!is_valid_ether_addr(iap)) {
807#ifdef CONFIG_M5272
808 if (FEC_FLASHMAC)
809 iap = (unsigned char *)FEC_FLASHMAC;
810#else
811 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +0000812 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +0000813#endif
814 }
815
816 /*
Shawn Guoca2cc332011-06-25 02:04:35 +0800817 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +0000818 */
819 if (!is_valid_ether_addr(iap)) {
820 *((unsigned long *) &tmpaddr[0]) =
821 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
822 *((unsigned short *) &tmpaddr[4]) =
823 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 iap = &tmpaddr[0];
825 }
826
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100827 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Shawn Guo49da97d2011-01-05 21:13:11 +0000829 /* Adjust MAC if using macaddr */
830 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +0000831 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834/* ------------------------------------------------------------------------- */
835
Bryan Wue6b043d2010-03-31 02:10:44 +0000836/*
837 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100839static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100841 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000842 struct phy_device *phy_dev = fep->phy_dev;
843 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Bryan Wue6b043d2010-03-31 02:10:44 +0000845 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Bryan Wue6b043d2010-03-31 02:10:44 +0000847 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400848
Bryan Wue6b043d2010-03-31 02:10:44 +0000849 /* Prevent a state halted on mii error */
850 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
851 phy_dev->state = PHY_RESUMING;
852 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000854
855 /* Duplex link change */
856 if (phy_dev->link) {
857 if (fep->full_duplex != phy_dev->duplex) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100858 fec_restart(ndev, phy_dev->duplex);
Lothar Waßmann6ea07222011-12-07 21:59:27 +0000859 /* prevent unnecessary second fec_restart() below */
860 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +0000861 status_change = 1;
862 }
863 }
864
865 /* Link on or off change */
866 if (phy_dev->link != fep->link) {
867 fep->link = phy_dev->link;
868 if (phy_dev->link)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100869 fec_restart(ndev, phy_dev->duplex);
Bryan Wue6b043d2010-03-31 02:10:44 +0000870 else
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100871 fec_stop(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000872 status_change = 1;
873 }
874
875spin_unlock:
876 spin_unlock_irqrestore(&fep->hw_lock, flags);
877
878 if (status_change)
879 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Bryan Wue6b043d2010-03-31 02:10:44 +0000882static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Bryan Wue6b043d2010-03-31 02:10:44 +0000884 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000885 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000886
887 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000888 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000889
890 /* start a read op */
891 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
892 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
893 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
894
895 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000896 time_left = wait_for_completion_timeout(&fep->mdio_done,
897 usecs_to_jiffies(FEC_MII_TIMEOUT));
898 if (time_left == 0) {
899 fep->mii_timeout = 1;
900 printk(KERN_ERR "FEC: MDIO read timeout\n");
901 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000902 }
903
904 /* return value */
905 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
906}
907
908static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
909 u16 value)
910{
911 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000912 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000913
914 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000915 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000916
Shawn Guo862f0982011-01-05 21:13:09 +0000917 /* start a write op */
918 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +0000919 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
920 FEC_MMFR_TA | FEC_MMFR_DATA(value),
921 fep->hwp + FEC_MII_DATA);
922
923 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000924 time_left = wait_for_completion_timeout(&fep->mdio_done,
925 usecs_to_jiffies(FEC_MII_TIMEOUT));
926 if (time_left == 0) {
927 fep->mii_timeout = 1;
928 printk(KERN_ERR "FEC: MDIO write timeout\n");
929 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000930 }
931
932 return 0;
933}
934
935static int fec_enet_mdio_reset(struct mii_bus *bus)
936{
937 return 0;
938}
939
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100940static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +0000941{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100942 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000943 const struct platform_device_id *id_entry =
944 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000945 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000946 char mdio_bus_id[MII_BUS_ID_SIZE];
947 char phy_name[MII_BUS_ID_SIZE + 3];
948 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +0000949 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +0000950
Bryan Wu418bd0d2010-05-28 03:40:39 -0700951 fep->phy_dev = NULL;
952
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000953 /* check for attached phy */
954 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
955 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
956 continue;
957 if (fep->mii_bus->phy_map[phy_id] == NULL)
958 continue;
959 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
960 continue;
Shawn Guob5680e02011-01-05 21:13:13 +0000961 if (dev_id--)
962 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000963 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
964 break;
Bryan Wue6b043d2010-03-31 02:10:44 +0000965 }
966
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000967 if (phy_id >= PHY_MAX_ADDR) {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000968 printk(KERN_INFO
969 "%s: no PHY, assuming direct connection to switch\n",
970 ndev->name);
Florian Fainelliea51ade92012-02-13 01:23:22 +0000971 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000972 phy_id = 0;
973 }
974
Richard Zhaoa7ed07d2012-01-29 22:08:12 +0000975 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100976 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 0,
Shawn Guo230dec62011-09-23 02:12:48 +0000977 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000978 if (IS_ERR(phy_dev)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100979 printk(KERN_ERR "%s: could not attach to PHY\n", ndev->name);
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000980 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000981 }
982
983 /* mask with MAC supported features */
Shawn Guo230dec62011-09-23 02:12:48 +0000984 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT)
985 phy_dev->supported &= PHY_GBIT_FEATURES;
986 else
987 phy_dev->supported &= PHY_BASIC_FEATURES;
988
Bryan Wue6b043d2010-03-31 02:10:44 +0000989 phy_dev->advertising = phy_dev->supported;
990
991 fep->phy_dev = phy_dev;
992 fep->link = 0;
993 fep->full_duplex = 0;
994
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000995 printk(KERN_INFO
996 "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
997 ndev->name,
Bryan Wu418bd0d2010-05-28 03:40:39 -0700998 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
999 fep->phy_dev->irq);
1000
Bryan Wue6b043d2010-03-31 02:10:44 +00001001 return 0;
1002}
1003
1004static int fec_enet_mii_init(struct platform_device *pdev)
1005{
Shawn Guob5680e02011-01-05 21:13:13 +00001006 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001007 struct net_device *ndev = platform_get_drvdata(pdev);
1008 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001009 const struct platform_device_id *id_entry =
1010 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001011 int err = -ENXIO, i;
1012
Shawn Guob5680e02011-01-05 21:13:13 +00001013 /*
1014 * The dual fec interfaces are not equivalent with enet-mac.
1015 * Here are the differences:
1016 *
1017 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1018 * - fec0 acts as the 1588 time master while fec1 is slave
1019 * - external phys can only be configured by fec0
1020 *
1021 * That is to say fec1 can not work independently. It only works
1022 * when fec0 is working. The reason behind this design is that the
1023 * second interface is added primarily for Switch mode.
1024 *
1025 * Because of the last point above, both phys are attached on fec0
1026 * mdio interface in board design, and need to be configured by
1027 * fec0 mii_bus.
1028 */
Shawn Guo43af9402011-12-05 05:01:15 +00001029 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001030 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001031 if (mii_cnt && fec0_mii_bus) {
1032 fep->mii_bus = fec0_mii_bus;
1033 mii_cnt++;
1034 return 0;
1035 }
1036 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001037 }
1038
Bryan Wue6b043d2010-03-31 02:10:44 +00001039 fep->mii_timeout = 0;
1040
1041 /*
1042 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001043 *
1044 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1045 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1046 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1047 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001048 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001049 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001050 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1051 fep->phy_speed--;
1052 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001053 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1054
1055 fep->mii_bus = mdiobus_alloc();
1056 if (fep->mii_bus == NULL) {
1057 err = -ENOMEM;
1058 goto err_out;
1059 }
1060
1061 fep->mii_bus->name = "fec_enet_mii_bus";
1062 fep->mii_bus->read = fec_enet_mdio_read;
1063 fep->mii_bus->write = fec_enet_mdio_write;
1064 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001065 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1066 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001067 fep->mii_bus->priv = fep;
1068 fep->mii_bus->parent = &pdev->dev;
1069
1070 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1071 if (!fep->mii_bus->irq) {
1072 err = -ENOMEM;
1073 goto err_out_free_mdiobus;
1074 }
1075
1076 for (i = 0; i < PHY_MAX_ADDR; i++)
1077 fep->mii_bus->irq[i] = PHY_POLL;
1078
Bryan Wue6b043d2010-03-31 02:10:44 +00001079 if (mdiobus_register(fep->mii_bus))
1080 goto err_out_free_mdio_irq;
1081
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001082 mii_cnt++;
1083
Shawn Guob5680e02011-01-05 21:13:13 +00001084 /* save fec0 mii_bus */
1085 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1086 fec0_mii_bus = fep->mii_bus;
1087
Bryan Wue6b043d2010-03-31 02:10:44 +00001088 return 0;
1089
Bryan Wue6b043d2010-03-31 02:10:44 +00001090err_out_free_mdio_irq:
1091 kfree(fep->mii_bus->irq);
1092err_out_free_mdiobus:
1093 mdiobus_free(fep->mii_bus);
1094err_out:
1095 return err;
1096}
1097
1098static void fec_enet_mii_remove(struct fec_enet_private *fep)
1099{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001100 if (--mii_cnt == 0) {
1101 mdiobus_unregister(fep->mii_bus);
1102 kfree(fep->mii_bus->irq);
1103 mdiobus_free(fep->mii_bus);
1104 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001105}
1106
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001107static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001108 struct ethtool_cmd *cmd)
1109{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001110 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001111 struct phy_device *phydev = fep->phy_dev;
1112
1113 if (!phydev)
1114 return -ENODEV;
1115
1116 return phy_ethtool_gset(phydev, cmd);
1117}
1118
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001119static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001120 struct ethtool_cmd *cmd)
1121{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001122 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001123 struct phy_device *phydev = fep->phy_dev;
1124
1125 if (!phydev)
1126 return -ENODEV;
1127
1128 return phy_ethtool_sset(phydev, cmd);
1129}
1130
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001131static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001132 struct ethtool_drvinfo *info)
1133{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001134 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Bryan Wue6b043d2010-03-31 02:10:44 +00001136 strcpy(info->driver, fep->pdev->dev.driver->name);
1137 strcpy(info->version, "Revision: 1.0");
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001138 strcpy(info->bus_info, dev_name(&ndev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
Bryan Wue6b043d2010-03-31 02:10:44 +00001140
stephen hemminger9b07be42012-01-04 12:59:49 +00001141static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00001142 .get_settings = fec_enet_get_settings,
1143 .set_settings = fec_enet_set_settings,
1144 .get_drvinfo = fec_enet_get_drvinfo,
1145 .get_link = ethtool_op_get_link,
Richard Cochranec567bc2012-04-03 22:59:28 +00001146 .get_ts_info = ethtool_op_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00001147};
1148
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001149static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001150{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001151 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001152 struct phy_device *phydev = fep->phy_dev;
1153
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001154 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001155 return -EINVAL;
1156
1157 if (!phydev)
1158 return -ENODEV;
1159
Frank Li6605b732012-10-30 18:25:31 +00001160#ifdef CONFIG_FEC_PTP
1161 if (cmd == SIOCSHWTSTAMP)
1162 return fec_ptp_ioctl(ndev, rq, cmd);
1163#endif
Richard Cochran28b04112010-07-17 08:48:55 +00001164 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001167static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001168{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001169 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001170 int i;
1171 struct sk_buff *skb;
1172 struct bufdesc *bdp;
1173
1174 bdp = fep->rx_bd_base;
1175 for (i = 0; i < RX_RING_SIZE; i++) {
1176 skb = fep->rx_skbuff[i];
1177
1178 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001179 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001180 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1181 if (skb)
1182 dev_kfree_skb(skb);
1183 bdp++;
1184 }
1185
1186 bdp = fep->tx_bd_base;
1187 for (i = 0; i < TX_RING_SIZE; i++)
1188 kfree(fep->tx_bounce[i]);
1189}
1190
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001191static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001192{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001193 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001194 int i;
1195 struct sk_buff *skb;
1196 struct bufdesc *bdp;
1197
1198 bdp = fep->rx_bd_base;
1199 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001200 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001201 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001202 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001203 return -ENOMEM;
1204 }
1205 fep->rx_skbuff[i] = skb;
1206
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001207 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001208 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1209 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Li6605b732012-10-30 18:25:31 +00001210#ifdef CONFIG_FEC_PTP
1211 bdp->cbd_esc = BD_ENET_RX_INT;
1212#endif
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001213 bdp++;
1214 }
1215
1216 /* Set the last buffer to wrap. */
1217 bdp--;
1218 bdp->cbd_sc |= BD_SC_WRAP;
1219
1220 bdp = fep->tx_bd_base;
1221 for (i = 0; i < TX_RING_SIZE; i++) {
1222 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1223
1224 bdp->cbd_sc = 0;
1225 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001226
1227#ifdef CONFIG_FEC_PTP
1228 bdp->cbd_esc = BD_ENET_RX_INT;
1229#endif
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001230 bdp++;
1231 }
1232
1233 /* Set the last buffer to wrap. */
1234 bdp--;
1235 bdp->cbd_sc |= BD_SC_WRAP;
1236
1237 return 0;
1238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001243 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001244 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* I should reset the ring buffers here, but I don't yet know
1247 * a simple way to do that.
1248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001250 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001251 if (ret)
1252 return ret;
1253
Bryan Wu418bd0d2010-05-28 03:40:39 -07001254 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001255 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001256 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001257 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001258 return ret;
1259 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001260 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001261 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001267fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001269 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Sascha Hauer22f6b862009-04-15 01:32:18 +00001271 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001273 netif_stop_queue(ndev);
1274 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001276 if (fep->phy_dev) {
1277 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001278 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001279 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001280
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001281 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/* Set or clear the multicast filter for this adaptor.
1287 * Skeleton taken from sunlance driver.
1288 * The CPM Ethernet implementation allows Multicast as well as individual
1289 * MAC address filtering. Some of the drivers check to make sure it is
1290 * a group multicast address, and discard those that are not. I guess I
1291 * will do the same for now, but just remove the test if you want
1292 * individual filtering as well (do the upper net layers want or support
1293 * this kind of feature?).
1294 */
1295
1296#define HASH_BITS 6 /* #bits in hash */
1297#define CRC32_POLY 0xEDB88320
1298
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001299static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001301 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001302 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001303 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned char hash;
1305
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001306 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001307 tmp = readl(fep->hwp + FEC_R_CNTRL);
1308 tmp |= 0x8;
1309 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001310 return;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Sascha Hauer4e831832009-04-15 01:32:19 +00001313 tmp = readl(fep->hwp + FEC_R_CNTRL);
1314 tmp &= ~0x8;
1315 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001316
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001317 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001318 /* Catch all multicast addresses, so set the
1319 * filter to all 1's
1320 */
1321 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1322 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Sascha Hauer4e831832009-04-15 01:32:19 +00001324 return;
1325 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001326
Sascha Hauer4e831832009-04-15 01:32:19 +00001327 /* Clear filter and add the addresses in hash register
1328 */
1329 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1330 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001332 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001333 /* calculate crc32 value of mac address */
1334 crc = 0xffffffff;
1335
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001336 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001337 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001338 for (bit = 0; bit < 8; bit++, data >>= 1) {
1339 crc = (crc >> 1) ^
1340 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001343
1344 /* only upper 6 bits (HASH_BITS) are used
1345 * which point to specific bit in he hash registers
1346 */
1347 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1348
1349 if (hash > 31) {
1350 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1351 tmp |= 1 << (hash - 32);
1352 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1353 } else {
1354 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1355 tmp |= 1 << hash;
1356 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359}
1360
Sascha Hauer22f6b862009-04-15 01:32:18 +00001361/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001362static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001363fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001365 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001366 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Sascha Hauer009fda82009-04-15 01:32:23 +00001368 if (!is_valid_ether_addr(addr->sa_data))
1369 return -EADDRNOTAVAIL;
1370
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001371 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001372
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001373 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1374 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001375 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001376 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001377 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001381#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001382/**
1383 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001384 * @dev: The FEC network adapter
1385 *
1386 * Polled functionality used by netconsole and others in non interrupt mode
1387 *
1388 */
1389void fec_poll_controller(struct net_device *dev)
1390{
1391 int i;
1392 struct fec_enet_private *fep = netdev_priv(dev);
1393
1394 for (i = 0; i < FEC_IRQ_NUM; i++) {
1395 if (fep->irq[i] > 0) {
1396 disable_irq(fep->irq[i]);
1397 fec_enet_interrupt(fep->irq[i], dev);
1398 enable_irq(fep->irq[i]);
1399 }
1400 }
1401}
1402#endif
1403
Sascha Hauer009fda82009-04-15 01:32:23 +00001404static const struct net_device_ops fec_netdev_ops = {
1405 .ndo_open = fec_enet_open,
1406 .ndo_stop = fec_enet_close,
1407 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001408 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001409 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001410 .ndo_validate_addr = eth_validate_addr,
1411 .ndo_tx_timeout = fec_timeout,
1412 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001413 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001414#ifdef CONFIG_NET_POLL_CONTROLLER
1415 .ndo_poll_controller = fec_poll_controller,
1416#endif
Sascha Hauer009fda82009-04-15 01:32:23 +00001417};
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /*
1420 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001423static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001425 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001426 struct bufdesc *cbd_base;
Rob Herring633e7532010-02-05 08:56:20 +00001427 struct bufdesc *bdp;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001428 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001430 /* Allocate memory for buffer descriptors. */
1431 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1432 GFP_KERNEL);
1433 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001434 printk("FEC: allocate descriptor memory failed?\n");
1435 return -ENOMEM;
1436 }
1437
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001438 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001439
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001440 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Shawn Guo49da97d2011-01-05 21:13:11 +00001442 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001443 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001445 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 fep->rx_bd_base = cbd_base;
1447 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1448
Sascha Hauer22f6b862009-04-15 01:32:18 +00001449 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001450 ndev->watchdog_timeo = TX_TIMEOUT;
1451 ndev->netdev_ops = &fec_netdev_ops;
1452 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001453
1454 /* Initialize the receive buffer descriptors. */
1455 bdp = fep->rx_bd_base;
1456 for (i = 0; i < RX_RING_SIZE; i++) {
1457
1458 /* Initialize the BD for every fragment in the page. */
1459 bdp->cbd_sc = 0;
1460 bdp++;
1461 }
1462
1463 /* Set the last buffer to wrap */
1464 bdp--;
1465 bdp->cbd_sc |= BD_SC_WRAP;
1466
1467 /* ...and the same for transmit */
1468 bdp = fep->tx_bd_base;
1469 for (i = 0; i < TX_RING_SIZE; i++) {
1470
1471 /* Initialize the BD for every fragment in the page. */
1472 bdp->cbd_sc = 0;
1473 bdp->cbd_bufaddr = 0;
1474 bdp++;
1475 }
1476
1477 /* Set the last buffer to wrap */
1478 bdp--;
1479 bdp->cbd_sc |= BD_SC_WRAP;
1480
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001481 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484}
1485
Shawn Guoca2cc332011-06-25 02:04:35 +08001486#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05001487static int fec_get_phy_mode_dt(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001488{
1489 struct device_node *np = pdev->dev.of_node;
1490
1491 if (np)
1492 return of_get_phy_mode(np);
1493
1494 return -ENODEV;
1495}
1496
Bill Pemberton33897cc2012-12-03 09:23:58 -05001497static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001498{
1499 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001500 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001501 struct device_node *np = pdev->dev.of_node;
1502
1503 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001504 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001505
Shawn Guoa3caad02012-06-27 03:45:24 +00001506 of_property_read_u32(np, "phy-reset-duration", &msec);
1507 /* A sane reset duration should not be longer than 1s */
1508 if (msec > 1000)
1509 msec = 1;
1510
Shawn Guoca2cc332011-06-25 02:04:35 +08001511 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Shawn Guo119fc002012-06-27 03:45:22 +00001512 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1513 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001514 if (err) {
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001515 pr_debug("FEC: failed to get gpio phy-reset: %d\n", err);
1516 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001517 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001518 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001519 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001520}
1521#else /* CONFIG_OF */
1522static inline int fec_get_phy_mode_dt(struct platform_device *pdev)
1523{
1524 return -ENODEV;
1525}
1526
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001527static inline void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001528{
1529 /*
1530 * In case of platform probe, the reset has been done
1531 * by machine code.
1532 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001533}
1534#endif /* CONFIG_OF */
1535
Bill Pemberton33897cc2012-12-03 09:23:58 -05001536static int
Sascha Haueread73182009-01-28 23:03:11 +00001537fec_probe(struct platform_device *pdev)
1538{
1539 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001540 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001541 struct net_device *ndev;
1542 int i, irq, ret = 0;
1543 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001544 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001545 static int dev_id;
Shawn Guob2bccee2012-05-06 20:24:04 +08001546 struct pinctrl *pinctrl;
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001547 struct regulator *reg_phy;
Shawn Guoca2cc332011-06-25 02:04:35 +08001548
1549 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1550 if (of_id)
1551 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001552
1553 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1554 if (!r)
1555 return -ENXIO;
1556
1557 r = request_mem_region(r->start, resource_size(r), pdev->name);
1558 if (!r)
1559 return -EBUSY;
1560
1561 /* Init network device */
1562 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001563 if (!ndev) {
1564 ret = -ENOMEM;
1565 goto failed_alloc_etherdev;
1566 }
Sascha Haueread73182009-01-28 23:03:11 +00001567
1568 SET_NETDEV_DEV(ndev, &pdev->dev);
1569
1570 /* setup board info structure */
1571 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001572
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001573 fep->hwp = ioremap(r->start, resource_size(r));
Bryan Wue6b043d2010-03-31 02:10:44 +00001574 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00001575 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00001576
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001577 if (!fep->hwp) {
Sascha Haueread73182009-01-28 23:03:11 +00001578 ret = -ENOMEM;
1579 goto failed_ioremap;
1580 }
1581
1582 platform_set_drvdata(pdev, ndev);
1583
Shawn Guoca2cc332011-06-25 02:04:35 +08001584 ret = fec_get_phy_mode_dt(pdev);
1585 if (ret < 0) {
1586 pdata = pdev->dev.platform_data;
1587 if (pdata)
1588 fep->phy_interface = pdata->phy;
1589 else
1590 fep->phy_interface = PHY_INTERFACE_MODE_MII;
1591 } else {
1592 fep->phy_interface = ret;
1593 }
1594
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001595 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001596 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00001597 if (irq < 0) {
1598 if (i)
1599 break;
1600 ret = irq;
1601 goto failed_irq;
1602 }
Sascha Haueread73182009-01-28 23:03:11 +00001603 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1604 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01001605 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00001606 irq = platform_get_irq(pdev, i);
1607 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001608 }
1609 goto failed_irq;
1610 }
1611 }
1612
Shawn Guob2bccee2012-05-06 20:24:04 +08001613 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1614 if (IS_ERR(pinctrl)) {
1615 ret = PTR_ERR(pinctrl);
1616 goto failed_pin;
1617 }
1618
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001619 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1620 if (IS_ERR(fep->clk_ipg)) {
1621 ret = PTR_ERR(fep->clk_ipg);
Sascha Haueread73182009-01-28 23:03:11 +00001622 goto failed_clk;
1623 }
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001624
1625 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1626 if (IS_ERR(fep->clk_ahb)) {
1627 ret = PTR_ERR(fep->clk_ahb);
1628 goto failed_clk;
1629 }
1630
Frank Li6605b732012-10-30 18:25:31 +00001631#ifdef CONFIG_FEC_PTP
1632 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
1633 if (IS_ERR(fep->clk_ptp)) {
1634 ret = PTR_ERR(fep->clk_ptp);
1635 goto failed_clk;
1636 }
1637#endif
1638
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001639 clk_prepare_enable(fep->clk_ahb);
1640 clk_prepare_enable(fep->clk_ipg);
Frank Li6605b732012-10-30 18:25:31 +00001641#ifdef CONFIG_FEC_PTP
1642 clk_prepare_enable(fep->clk_ptp);
1643#endif
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001644 reg_phy = devm_regulator_get(&pdev->dev, "phy");
1645 if (!IS_ERR(reg_phy)) {
1646 ret = regulator_enable(reg_phy);
1647 if (ret) {
1648 dev_err(&pdev->dev,
1649 "Failed to enable phy regulator: %d\n", ret);
1650 goto failed_regulator;
1651 }
1652 }
1653
Shawn Guo2ca9b2a2012-06-27 03:45:20 +00001654 fec_reset_phy(pdev);
1655
Shawn Guo8649a232011-01-05 21:13:10 +00001656 ret = fec_enet_init(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001657 if (ret)
1658 goto failed_init;
1659
Bryan Wue6b043d2010-03-31 02:10:44 +00001660 ret = fec_enet_mii_init(pdev);
1661 if (ret)
1662 goto failed_mii_init;
1663
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001664 /* Carrier starts down, phylib will bring it up */
1665 netif_carrier_off(ndev);
1666
Sascha Haueread73182009-01-28 23:03:11 +00001667 ret = register_netdev(ndev);
1668 if (ret)
1669 goto failed_register;
1670
Frank Li6605b732012-10-30 18:25:31 +00001671#ifdef CONFIG_FEC_PTP
1672 fec_ptp_init(ndev, pdev);
1673#endif
1674
Sascha Haueread73182009-01-28 23:03:11 +00001675 return 0;
1676
1677failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001678 fec_enet_mii_remove(fep);
1679failed_mii_init:
Sascha Haueread73182009-01-28 23:03:11 +00001680failed_init:
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001681failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001682 clk_disable_unprepare(fep->clk_ahb);
1683 clk_disable_unprepare(fep->clk_ipg);
Frank Li6605b732012-10-30 18:25:31 +00001684#ifdef CONFIG_FEC_PTP
1685 clk_disable_unprepare(fep->clk_ptp);
1686#endif
Shawn Guob2bccee2012-05-06 20:24:04 +08001687failed_pin:
Sascha Haueread73182009-01-28 23:03:11 +00001688failed_clk:
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001689 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001690 irq = platform_get_irq(pdev, i);
1691 if (irq > 0)
1692 free_irq(irq, ndev);
1693 }
1694failed_irq:
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001695 iounmap(fep->hwp);
Sascha Haueread73182009-01-28 23:03:11 +00001696failed_ioremap:
1697 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001698failed_alloc_etherdev:
1699 release_mem_region(r->start, resource_size(r));
Sascha Haueread73182009-01-28 23:03:11 +00001700
1701 return ret;
1702}
1703
Bill Pemberton33897cc2012-12-03 09:23:58 -05001704static int
Sascha Haueread73182009-01-28 23:03:11 +00001705fec_drv_remove(struct platform_device *pdev)
1706{
1707 struct net_device *ndev = platform_get_drvdata(pdev);
1708 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001709 struct resource *r;
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001710 int i;
Sascha Haueread73182009-01-28 23:03:11 +00001711
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001712 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001713 fec_enet_mii_remove(fep);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001714 for (i = 0; i < FEC_IRQ_NUM; i++) {
1715 int irq = platform_get_irq(pdev, i);
1716 if (irq > 0)
1717 free_irq(irq, ndev);
1718 }
Frank Li6605b732012-10-30 18:25:31 +00001719#ifdef CONFIG_FEC_PTP
1720 del_timer_sync(&fep->time_keep);
1721 clk_disable_unprepare(fep->clk_ptp);
1722 if (fep->ptp_clock)
1723 ptp_clock_unregister(fep->ptp_clock);
1724#endif
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001725 clk_disable_unprepare(fep->clk_ahb);
1726 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001727 iounmap(fep->hwp);
Sascha Haueread73182009-01-28 23:03:11 +00001728 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001729
1730 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1731 BUG_ON(!r);
1732 release_mem_region(r->start, resource_size(r));
1733
Uwe Kleine-Königb3cde362011-01-25 18:03:37 +01001734 platform_set_drvdata(pdev, NULL);
1735
Sascha Haueread73182009-01-28 23:03:11 +00001736 return 0;
1737}
1738
Denis Kirjanov59d42892010-06-02 09:27:04 +00001739#ifdef CONFIG_PM
Sascha Haueread73182009-01-28 23:03:11 +00001740static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001741fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001742{
Eric Benard87cad5c2010-06-18 04:19:54 +00001743 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001744 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001745
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001746 if (netif_running(ndev)) {
1747 fec_stop(ndev);
1748 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001749 }
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001750 clk_disable_unprepare(fep->clk_ahb);
1751 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001752
Sascha Haueread73182009-01-28 23:03:11 +00001753 return 0;
1754}
1755
1756static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001757fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001758{
Eric Benard87cad5c2010-06-18 04:19:54 +00001759 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001760 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001761
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001762 clk_prepare_enable(fep->clk_ahb);
1763 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001764 if (netif_running(ndev)) {
1765 fec_restart(ndev, fep->full_duplex);
1766 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001767 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001768
Sascha Haueread73182009-01-28 23:03:11 +00001769 return 0;
1770}
1771
Denis Kirjanov59d42892010-06-02 09:27:04 +00001772static const struct dev_pm_ops fec_pm_ops = {
1773 .suspend = fec_suspend,
1774 .resume = fec_resume,
1775 .freeze = fec_suspend,
1776 .thaw = fec_resume,
1777 .poweroff = fec_suspend,
1778 .restore = fec_resume,
1779};
Eric Benard87cad5c2010-06-18 04:19:54 +00001780#endif
Denis Kirjanov59d42892010-06-02 09:27:04 +00001781
Sascha Haueread73182009-01-28 23:03:11 +00001782static struct platform_driver fec_driver = {
1783 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00001784 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00001785 .owner = THIS_MODULE,
1786#ifdef CONFIG_PM
1787 .pm = &fec_pm_ops,
1788#endif
Shawn Guoca2cc332011-06-25 02:04:35 +08001789 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00001790 },
Shawn Guob5680e02011-01-05 21:13:13 +00001791 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00001792 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05001793 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00001794};
1795
Fabio Estevamaaca2372012-01-23 16:44:50 +00001796module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798MODULE_LICENSE("GPL");