blob: d0e1b331e8e6591628fc0669ccdca3d149778e3e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Trigger transmission start */
Sascha Hauerf44d6302009-04-15 03:11:30 +0000284 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Sascha Hauer22f6b862009-04-15 01:32:18 +0000286 /* If this was the last BD in the ring, start at the beginning again. */
287 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000289 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 bdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (bdp == fep->dirty_tx) {
293 fep->tx_full = 1;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100294 netif_stop_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Sascha Hauer2e285322009-04-15 01:32:16 +0000297 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Richard Cochran18a03b92011-06-12 02:18:59 +0000299 skb_tx_timestamp(skb);
300
Richard Cochrana0087a32011-06-19 03:31:40 +0000301 spin_unlock_irqrestore(&fep->hw_lock, flags);
302
Patrick McHardy6ed10652009-06-23 06:03:08 +0000303 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Uwe Kleine-König45993652011-01-19 20:47:04 +0100306/* This function is called to start or restart the FEC during a link
307 * change. This only happens when switching between half and full
308 * duplex.
309 */
310static void
311fec_restart(struct net_device *ndev, int duplex)
312{
313 struct fec_enet_private *fep = netdev_priv(ndev);
314 const struct platform_device_id *id_entry =
315 platform_get_device_id(fep->pdev);
316 int i;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100317 u32 temp_mac[2];
318 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000319 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100320
321 /* Whack a reset. We should wait for this. */
322 writel(1, fep->hwp + FEC_ECNTRL);
323 udelay(10);
324
325 /*
326 * enet-mac reset will reset mac address registers too,
327 * so need to reconfigure it.
328 */
329 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
330 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
331 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
332 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
333 }
334
335 /* Clear any outstanding interrupt. */
336 writel(0xffc00000, fep->hwp + FEC_IEVENT);
337
338 /* Reset all multicast. */
339 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
340 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
341#ifndef CONFIG_M5272
342 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
343 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
344#endif
345
346 /* Set maximum receive buffer size. */
347 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
348
349 /* Set receive and transmit descriptor base. */
350 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
351 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
352 fep->hwp + FEC_X_DES_START);
353
354 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
355 fep->cur_rx = fep->rx_bd_base;
356
357 /* Reset SKB transmit buffers. */
358 fep->skb_cur = fep->skb_dirty = 0;
359 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
360 if (fep->tx_skbuff[i]) {
361 dev_kfree_skb_any(fep->tx_skbuff[i]);
362 fep->tx_skbuff[i] = NULL;
363 }
364 }
365
366 /* Enable MII mode */
367 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100368 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100369 writel(0x04, fep->hwp + FEC_X_CNTRL);
370 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100371 /* No Rcv on Xmit */
372 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100373 writel(0x0, fep->hwp + FEC_X_CNTRL);
374 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100375
Uwe Kleine-König45993652011-01-19 20:47:04 +0100376 fep->full_duplex = duplex;
377
378 /* Set MII speed */
379 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
380
381 /*
382 * The phy interface and speed need to get configured
383 * differently on enet-mac.
384 */
385 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100386 /* Enable flow control and length check */
387 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100388
Shawn Guo230dec62011-09-23 02:12:48 +0000389 /* RGMII, RMII or MII */
390 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
391 rcntl |= (1 << 6);
392 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100393 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100394 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100395 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100396
Shawn Guo230dec62011-09-23 02:12:48 +0000397 /* 1G, 100M or 10M */
398 if (fep->phy_dev) {
399 if (fep->phy_dev->speed == SPEED_1000)
400 ecntl |= (1 << 5);
401 else if (fep->phy_dev->speed == SPEED_100)
402 rcntl &= ~(1 << 9);
403 else
404 rcntl |= (1 << 9);
405 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100406 } else {
407#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800408 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000409 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100410 /* disable the gasket and wait */
411 writel(0, fep->hwp + FEC_MIIGSK_ENR);
412 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
413 udelay(1);
414
415 /*
416 * configure the gasket:
417 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800418 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100419 */
Eric Benard8d82f212012-01-12 06:10:28 +0000420 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
421 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
422 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
423 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
424 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100425
426 /* re-enable the gasket */
427 writel(2, fep->hwp + FEC_MIIGSK_ENR);
428 }
429#endif
430 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100431 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100432
Shawn Guo230dec62011-09-23 02:12:48 +0000433 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
434 /* enable ENET endian swap */
435 ecntl |= (1 << 8);
436 /* enable ENET store and forward mode */
437 writel(1 << 8, fep->hwp + FEC_X_WMRK);
438 }
439
Uwe Kleine-König45993652011-01-19 20:47:04 +0100440 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000441 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100442 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
443
444 /* Enable interrupts we wish to service */
445 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
446}
447
448static void
449fec_stop(struct net_device *ndev)
450{
451 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000452 const struct platform_device_id *id_entry =
453 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000454 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100455
456 /* We cannot expect a graceful transmit stop without link !!! */
457 if (fep->link) {
458 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
459 udelay(10);
460 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
461 printk("fec_stop : Graceful transmit stop did not complete !\n");
462 }
463
464 /* Whack a reset. We should wait for this. */
465 writel(1, fep->hwp + FEC_ECNTRL);
466 udelay(10);
467 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
468 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000469
470 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000471 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000472 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000473 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
474 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100475}
476
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100479fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100481 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100483 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100485 fec_restart(ndev, fep->full_duplex);
486 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100490fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000493 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000494 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct sk_buff *skb;
496
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100497 fep = netdev_priv(ndev);
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000498 spin_lock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bdp = fep->dirty_tx;
500
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000501 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000502 if (bdp == fep->cur_tx && fep->tx_full == 0)
503 break;
504
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100505 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
506 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000507 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 skb = fep->tx_skbuff[fep->skb_dirty];
510 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000511 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 BD_ENET_TX_RL | BD_ENET_TX_UN |
513 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100514 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000515 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100516 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000517 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100518 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000519 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100520 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000521 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100522 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000523 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100524 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100526 ndev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000529 if (status & BD_ENET_TX_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 printk("HEY! Enet xmit interrupt and TX_READY.\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Deferred means some collisions occurred during transmit,
533 * but we eventually sent the packet OK.
534 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000535 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100536 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400537
Sascha Hauer22f6b862009-04-15 01:32:18 +0000538 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 dev_kfree_skb_any(skb);
540 fep->tx_skbuff[fep->skb_dirty] = NULL;
541 fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400542
Sascha Hauer22f6b862009-04-15 01:32:18 +0000543 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000544 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 bdp = fep->tx_bd_base;
546 else
547 bdp++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400548
Sascha Hauer22f6b862009-04-15 01:32:18 +0000549 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
551 if (fep->tx_full) {
552 fep->tx_full = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100553 if (netif_queue_stopped(ndev))
554 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000557 fep->dirty_tx = bdp;
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000558 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561
562/* During a receive, the cur_rx points to the current incoming buffer.
563 * When we update through the ring, if the next incoming buffer has
564 * not been given to the system, we just set the empty indicator,
565 * effectively tossing the packet.
566 */
567static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100568fec_enet_rx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100570 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000571 const struct platform_device_id *id_entry =
572 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000573 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000574 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct sk_buff *skb;
576 ushort pkt_len;
577 __u8 *data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400578
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000579#ifdef CONFIG_M532x
580 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000583 spin_lock(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +1000584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* First, grab all of the stats for the incoming packet.
586 * These get messed up if we get called due to a busy condition.
587 */
588 bdp = fep->cur_rx;
589
Sascha Hauer22f6b862009-04-15 01:32:18 +0000590 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Sascha Hauer22f6b862009-04-15 01:32:18 +0000592 /* Since we have allocated space to hold a complete frame,
593 * the last indicator should be set.
594 */
595 if ((status & BD_ENET_RX_LAST) == 0)
596 printk("FEC ENET: rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Sascha Hauer22f6b862009-04-15 01:32:18 +0000598 if (!fep->opened)
599 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Sascha Hauer22f6b862009-04-15 01:32:18 +0000601 /* Check for errors. */
602 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100604 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000605 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
606 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100607 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000608 }
609 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100610 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000611 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100612 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000613 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100614 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000616
617 /* Report late collisions as a frame error.
618 * On this error, the BD is closed, but we don't know what we
619 * have in the buffer. So, just drop this frame on the floor.
620 */
621 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100622 ndev->stats.rx_errors++;
623 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000624 goto rx_processing_done;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Sascha Hauer22f6b862009-04-15 01:32:18 +0000627 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100628 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000629 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100630 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000631 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100633 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
634 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000635
Shawn Guob5680e02011-01-05 21:13:13 +0000636 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
637 swap_buffer(data, pkt_len);
638
Sascha Hauer22f6b862009-04-15 01:32:18 +0000639 /* This does 16 byte alignment, exactly what we need.
640 * The packet length includes FCS, but we don't want to
641 * include that when passing upstream as it messes up
642 * bridging applications.
643 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000644 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Sascha Hauer85498892009-04-15 01:32:21 +0000646 if (unlikely(!skb)) {
Sascha Hauer22f6b862009-04-15 01:32:18 +0000647 printk("%s: Memory squeeze, dropping packet.\n",
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100648 ndev->name);
649 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000650 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000651 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000652 skb_put(skb, pkt_len - 4); /* Make room */
653 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100654 skb->protocol = eth_type_trans(skb, ndev);
Richard Cochran18a03b92011-06-12 02:18:59 +0000655 if (!skb_defer_rx_timestamp(skb))
656 netif_rx(skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000657 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000658
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100659 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
660 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000661rx_processing_done:
662 /* Clear the status flags for this buffer */
663 status &= ~BD_ENET_RX_STATS;
664
665 /* Mark the buffer empty */
666 status |= BD_ENET_RX_EMPTY;
667 bdp->cbd_sc = status;
668
669 /* Update BD pointer to next entry */
670 if (status & BD_ENET_RX_WRAP)
671 bdp = fep->rx_bd_base;
672 else
673 bdp++;
674 /* Doing this here will keep the FEC running while we process
675 * incoming frames. On a heavily loaded network, we should be
676 * able to keep up at the expense of system resources.
677 */
678 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000680 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Uwe Kleine-König81538e72009-09-01 23:14:16 +0000682 spin_unlock(&fep->hw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Uwe Kleine-König45993652011-01-19 20:47:04 +0100685static irqreturn_t
686fec_enet_interrupt(int irq, void *dev_id)
687{
688 struct net_device *ndev = dev_id;
689 struct fec_enet_private *fep = netdev_priv(ndev);
690 uint int_events;
691 irqreturn_t ret = IRQ_NONE;
692
693 do {
694 int_events = readl(fep->hwp + FEC_IEVENT);
695 writel(int_events, fep->hwp + FEC_IEVENT);
696
697 if (int_events & FEC_ENET_RXF) {
698 ret = IRQ_HANDLED;
699 fec_enet_rx(ndev);
700 }
701
702 /* Transmit OK, or non-fatal error. Update the buffer
703 * descriptors. FEC handles all errors, we just discover
704 * them as part of the transmit process.
705 */
706 if (int_events & FEC_ENET_TXF) {
707 ret = IRQ_HANDLED;
708 fec_enet_tx(ndev);
709 }
710
711 if (int_events & FEC_ENET_MII) {
712 ret = IRQ_HANDLED;
713 complete(&fep->mdio_done);
714 }
715 } while (int_events);
716
717 return ret;
718}
719
720
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/* ------------------------------------------------------------------------- */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100723static void __inline__ fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100725 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +0000726 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000727 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Shawn Guo49da97d2011-01-05 21:13:11 +0000729 /*
730 * try to get mac address in following order:
731 *
732 * 1) module parameter via kernel command line in form
733 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
734 */
735 iap = macaddr;
736
Shawn Guoca2cc332011-06-25 02:04:35 +0800737#ifdef CONFIG_OF
Shawn Guo49da97d2011-01-05 21:13:11 +0000738 /*
Shawn Guoca2cc332011-06-25 02:04:35 +0800739 * 2) from device tree data
740 */
741 if (!is_valid_ether_addr(iap)) {
742 struct device_node *np = fep->pdev->dev.of_node;
743 if (np) {
744 const char *mac = of_get_mac_address(np);
745 if (mac)
746 iap = (unsigned char *) mac;
747 }
748 }
749#endif
750
751 /*
752 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +0000753 */
754 if (!is_valid_ether_addr(iap)) {
755#ifdef CONFIG_M5272
756 if (FEC_FLASHMAC)
757 iap = (unsigned char *)FEC_FLASHMAC;
758#else
759 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +0000760 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +0000761#endif
762 }
763
764 /*
Shawn Guoca2cc332011-06-25 02:04:35 +0800765 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +0000766 */
767 if (!is_valid_ether_addr(iap)) {
768 *((unsigned long *) &tmpaddr[0]) =
769 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
770 *((unsigned short *) &tmpaddr[4]) =
771 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 iap = &tmpaddr[0];
773 }
774
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100775 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Shawn Guo49da97d2011-01-05 21:13:11 +0000777 /* Adjust MAC if using macaddr */
778 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +0000779 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/* ------------------------------------------------------------------------- */
783
Bryan Wue6b043d2010-03-31 02:10:44 +0000784/*
785 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100787static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100789 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000790 struct phy_device *phy_dev = fep->phy_dev;
791 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Bryan Wue6b043d2010-03-31 02:10:44 +0000793 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Bryan Wue6b043d2010-03-31 02:10:44 +0000795 spin_lock_irqsave(&fep->hw_lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400796
Bryan Wue6b043d2010-03-31 02:10:44 +0000797 /* Prevent a state halted on mii error */
798 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
799 phy_dev->state = PHY_RESUMING;
800 goto spin_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Bryan Wue6b043d2010-03-31 02:10:44 +0000802
803 /* Duplex link change */
804 if (phy_dev->link) {
805 if (fep->full_duplex != phy_dev->duplex) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100806 fec_restart(ndev, phy_dev->duplex);
Lothar Waßmann6ea07222011-12-07 21:59:27 +0000807 /* prevent unnecessary second fec_restart() below */
808 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +0000809 status_change = 1;
810 }
811 }
812
813 /* Link on or off change */
814 if (phy_dev->link != fep->link) {
815 fep->link = phy_dev->link;
816 if (phy_dev->link)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100817 fec_restart(ndev, phy_dev->duplex);
Bryan Wue6b043d2010-03-31 02:10:44 +0000818 else
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100819 fec_stop(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000820 status_change = 1;
821 }
822
823spin_unlock:
824 spin_unlock_irqrestore(&fep->hw_lock, flags);
825
826 if (status_change)
827 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Bryan Wue6b043d2010-03-31 02:10:44 +0000830static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Bryan Wue6b043d2010-03-31 02:10:44 +0000832 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000833 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000834
835 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000836 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000837
838 /* start a read op */
839 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
840 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
841 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
842
843 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000844 time_left = wait_for_completion_timeout(&fep->mdio_done,
845 usecs_to_jiffies(FEC_MII_TIMEOUT));
846 if (time_left == 0) {
847 fep->mii_timeout = 1;
848 printk(KERN_ERR "FEC: MDIO read timeout\n");
849 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000850 }
851
852 /* return value */
853 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
854}
855
856static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
857 u16 value)
858{
859 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +0000860 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +0000861
862 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +0000863 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +0000864
Shawn Guo862f0982011-01-05 21:13:09 +0000865 /* start a write op */
866 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +0000867 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
868 FEC_MMFR_TA | FEC_MMFR_DATA(value),
869 fep->hwp + FEC_MII_DATA);
870
871 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +0000872 time_left = wait_for_completion_timeout(&fep->mdio_done,
873 usecs_to_jiffies(FEC_MII_TIMEOUT));
874 if (time_left == 0) {
875 fep->mii_timeout = 1;
876 printk(KERN_ERR "FEC: MDIO write timeout\n");
877 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +0000878 }
879
880 return 0;
881}
882
883static int fec_enet_mdio_reset(struct mii_bus *bus)
884{
885 return 0;
886}
887
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100888static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +0000889{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100890 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000891 const struct platform_device_id *id_entry =
892 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000893 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000894 char mdio_bus_id[MII_BUS_ID_SIZE];
895 char phy_name[MII_BUS_ID_SIZE + 3];
896 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +0000897 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +0000898
Bryan Wu418bd0d2010-05-28 03:40:39 -0700899 fep->phy_dev = NULL;
900
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000901 /* check for attached phy */
902 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
903 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
904 continue;
905 if (fep->mii_bus->phy_map[phy_id] == NULL)
906 continue;
907 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
908 continue;
Shawn Guob5680e02011-01-05 21:13:13 +0000909 if (dev_id--)
910 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000911 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
912 break;
Bryan Wue6b043d2010-03-31 02:10:44 +0000913 }
914
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000915 if (phy_id >= PHY_MAX_ADDR) {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000916 printk(KERN_INFO
917 "%s: no PHY, assuming direct connection to switch\n",
918 ndev->name);
Florian Fainelliea51ade92012-02-13 01:23:22 +0000919 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000920 phy_id = 0;
921 }
922
Richard Zhaoa7ed07d2012-01-29 22:08:12 +0000923 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100924 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 0,
Shawn Guo230dec62011-09-23 02:12:48 +0000925 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000926 if (IS_ERR(phy_dev)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100927 printk(KERN_ERR "%s: could not attach to PHY\n", ndev->name);
Greg Ungerer6fcc0402010-10-11 21:03:05 +0000928 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000929 }
930
931 /* mask with MAC supported features */
Shawn Guo230dec62011-09-23 02:12:48 +0000932 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT)
933 phy_dev->supported &= PHY_GBIT_FEATURES;
934 else
935 phy_dev->supported &= PHY_BASIC_FEATURES;
936
Bryan Wue6b043d2010-03-31 02:10:44 +0000937 phy_dev->advertising = phy_dev->supported;
938
939 fep->phy_dev = phy_dev;
940 fep->link = 0;
941 fep->full_duplex = 0;
942
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000943 printk(KERN_INFO
944 "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
945 ndev->name,
Bryan Wu418bd0d2010-05-28 03:40:39 -0700946 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
947 fep->phy_dev->irq);
948
Bryan Wue6b043d2010-03-31 02:10:44 +0000949 return 0;
950}
951
952static int fec_enet_mii_init(struct platform_device *pdev)
953{
Shawn Guob5680e02011-01-05 21:13:13 +0000954 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100955 struct net_device *ndev = platform_get_drvdata(pdev);
956 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000957 const struct platform_device_id *id_entry =
958 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +0000959 int err = -ENXIO, i;
960
Shawn Guob5680e02011-01-05 21:13:13 +0000961 /*
962 * The dual fec interfaces are not equivalent with enet-mac.
963 * Here are the differences:
964 *
965 * - fec0 supports MII & RMII modes while fec1 only supports RMII
966 * - fec0 acts as the 1588 time master while fec1 is slave
967 * - external phys can only be configured by fec0
968 *
969 * That is to say fec1 can not work independently. It only works
970 * when fec0 is working. The reason behind this design is that the
971 * second interface is added primarily for Switch mode.
972 *
973 * Because of the last point above, both phys are attached on fec0
974 * mdio interface in board design, and need to be configured by
975 * fec0 mii_bus.
976 */
Shawn Guo43af9402011-12-05 05:01:15 +0000977 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +0000978 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000979 if (mii_cnt && fec0_mii_bus) {
980 fep->mii_bus = fec0_mii_bus;
981 mii_cnt++;
982 return 0;
983 }
984 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +0000985 }
986
Bryan Wue6b043d2010-03-31 02:10:44 +0000987 fep->mii_timeout = 0;
988
989 /*
990 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +0000991 *
992 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
993 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
994 * Reference Manual has an error on this, and gets fixed on i.MX6Q
995 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +0000996 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +0100997 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +0000998 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
999 fep->phy_speed--;
1000 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001001 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1002
1003 fep->mii_bus = mdiobus_alloc();
1004 if (fep->mii_bus == NULL) {
1005 err = -ENOMEM;
1006 goto err_out;
1007 }
1008
1009 fep->mii_bus->name = "fec_enet_mii_bus";
1010 fep->mii_bus->read = fec_enet_mdio_read;
1011 fep->mii_bus->write = fec_enet_mdio_write;
1012 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001013 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1014 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001015 fep->mii_bus->priv = fep;
1016 fep->mii_bus->parent = &pdev->dev;
1017
1018 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1019 if (!fep->mii_bus->irq) {
1020 err = -ENOMEM;
1021 goto err_out_free_mdiobus;
1022 }
1023
1024 for (i = 0; i < PHY_MAX_ADDR; i++)
1025 fep->mii_bus->irq[i] = PHY_POLL;
1026
Bryan Wue6b043d2010-03-31 02:10:44 +00001027 if (mdiobus_register(fep->mii_bus))
1028 goto err_out_free_mdio_irq;
1029
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001030 mii_cnt++;
1031
Shawn Guob5680e02011-01-05 21:13:13 +00001032 /* save fec0 mii_bus */
1033 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1034 fec0_mii_bus = fep->mii_bus;
1035
Bryan Wue6b043d2010-03-31 02:10:44 +00001036 return 0;
1037
Bryan Wue6b043d2010-03-31 02:10:44 +00001038err_out_free_mdio_irq:
1039 kfree(fep->mii_bus->irq);
1040err_out_free_mdiobus:
1041 mdiobus_free(fep->mii_bus);
1042err_out:
1043 return err;
1044}
1045
1046static void fec_enet_mii_remove(struct fec_enet_private *fep)
1047{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001048 if (--mii_cnt == 0) {
1049 mdiobus_unregister(fep->mii_bus);
1050 kfree(fep->mii_bus->irq);
1051 mdiobus_free(fep->mii_bus);
1052 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001053}
1054
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001055static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001056 struct ethtool_cmd *cmd)
1057{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001058 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001059 struct phy_device *phydev = fep->phy_dev;
1060
1061 if (!phydev)
1062 return -ENODEV;
1063
1064 return phy_ethtool_gset(phydev, cmd);
1065}
1066
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001067static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001068 struct ethtool_cmd *cmd)
1069{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001070 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001071 struct phy_device *phydev = fep->phy_dev;
1072
1073 if (!phydev)
1074 return -ENODEV;
1075
1076 return phy_ethtool_sset(phydev, cmd);
1077}
1078
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001079static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001080 struct ethtool_drvinfo *info)
1081{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001082 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Bryan Wue6b043d2010-03-31 02:10:44 +00001084 strcpy(info->driver, fep->pdev->dev.driver->name);
1085 strcpy(info->version, "Revision: 1.0");
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001086 strcpy(info->bus_info, dev_name(&ndev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
Bryan Wue6b043d2010-03-31 02:10:44 +00001088
stephen hemminger9b07be42012-01-04 12:59:49 +00001089static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00001090 .get_settings = fec_enet_get_settings,
1091 .set_settings = fec_enet_set_settings,
1092 .get_drvinfo = fec_enet_get_drvinfo,
1093 .get_link = ethtool_op_get_link,
Richard Cochranec567bc2012-04-03 22:59:28 +00001094 .get_ts_info = ethtool_op_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00001095};
1096
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001097static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001098{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001099 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001100 struct phy_device *phydev = fep->phy_dev;
1101
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001102 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001103 return -EINVAL;
1104
1105 if (!phydev)
1106 return -ENODEV;
1107
Richard Cochran28b04112010-07-17 08:48:55 +00001108 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001111static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001112{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001113 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001114 int i;
1115 struct sk_buff *skb;
1116 struct bufdesc *bdp;
1117
1118 bdp = fep->rx_bd_base;
1119 for (i = 0; i < RX_RING_SIZE; i++) {
1120 skb = fep->rx_skbuff[i];
1121
1122 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001123 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001124 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1125 if (skb)
1126 dev_kfree_skb(skb);
1127 bdp++;
1128 }
1129
1130 bdp = fep->tx_bd_base;
1131 for (i = 0; i < TX_RING_SIZE; i++)
1132 kfree(fep->tx_bounce[i]);
1133}
1134
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001135static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001136{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001137 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001138 int i;
1139 struct sk_buff *skb;
1140 struct bufdesc *bdp;
1141
1142 bdp = fep->rx_bd_base;
1143 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001144 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001145 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001146 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001147 return -ENOMEM;
1148 }
1149 fep->rx_skbuff[i] = skb;
1150
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001151 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001152 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1153 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1154 bdp++;
1155 }
1156
1157 /* Set the last buffer to wrap. */
1158 bdp--;
1159 bdp->cbd_sc |= BD_SC_WRAP;
1160
1161 bdp = fep->tx_bd_base;
1162 for (i = 0; i < TX_RING_SIZE; i++) {
1163 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1164
1165 bdp->cbd_sc = 0;
1166 bdp->cbd_bufaddr = 0;
1167 bdp++;
1168 }
1169
1170 /* Set the last buffer to wrap. */
1171 bdp--;
1172 bdp->cbd_sc |= BD_SC_WRAP;
1173
1174 return 0;
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001178fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001180 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001181 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* I should reset the ring buffers here, but I don't yet know
1184 * a simple way to do that.
1185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001187 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001188 if (ret)
1189 return ret;
1190
Bryan Wu418bd0d2010-05-28 03:40:39 -07001191 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001192 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001193 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001194 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001195 return ret;
1196 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001197 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001198 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001204fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001206 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Sascha Hauer22f6b862009-04-15 01:32:18 +00001208 /* Don't know what to do yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001210 netif_stop_queue(ndev);
1211 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001213 if (fep->phy_dev) {
1214 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001215 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001216 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001217
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001218 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return 0;
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/* Set or clear the multicast filter for this adaptor.
1224 * Skeleton taken from sunlance driver.
1225 * The CPM Ethernet implementation allows Multicast as well as individual
1226 * MAC address filtering. Some of the drivers check to make sure it is
1227 * a group multicast address, and discard those that are not. I guess I
1228 * will do the same for now, but just remove the test if you want
1229 * individual filtering as well (do the upper net layers want or support
1230 * this kind of feature?).
1231 */
1232
1233#define HASH_BITS 6 /* #bits in hash */
1234#define CRC32_POLY 0xEDB88320
1235
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001236static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001238 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001239 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001240 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned char hash;
1242
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001243 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001244 tmp = readl(fep->hwp + FEC_R_CNTRL);
1245 tmp |= 0x8;
1246 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001247 return;
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Sascha Hauer4e831832009-04-15 01:32:19 +00001250 tmp = readl(fep->hwp + FEC_R_CNTRL);
1251 tmp &= ~0x8;
1252 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001253
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001254 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001255 /* Catch all multicast addresses, so set the
1256 * filter to all 1's
1257 */
1258 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1259 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Sascha Hauer4e831832009-04-15 01:32:19 +00001261 return;
1262 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001263
Sascha Hauer4e831832009-04-15 01:32:19 +00001264 /* Clear filter and add the addresses in hash register
1265 */
1266 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1267 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001269 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001270 /* calculate crc32 value of mac address */
1271 crc = 0xffffffff;
1272
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001273 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001274 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001275 for (bit = 0; bit < 8; bit++, data >>= 1) {
1276 crc = (crc >> 1) ^
1277 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001280
1281 /* only upper 6 bits (HASH_BITS) are used
1282 * which point to specific bit in he hash registers
1283 */
1284 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1285
1286 if (hash > 31) {
1287 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1288 tmp |= 1 << (hash - 32);
1289 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1290 } else {
1291 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1292 tmp |= 1 << hash;
1293 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296}
1297
Sascha Hauer22f6b862009-04-15 01:32:18 +00001298/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001299static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001300fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001302 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001303 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Sascha Hauer009fda82009-04-15 01:32:23 +00001305 if (!is_valid_ether_addr(addr->sa_data))
1306 return -EADDRNOTAVAIL;
1307
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001308 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001309
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001310 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1311 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001312 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001313 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001314 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001318#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001319/**
1320 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001321 * @dev: The FEC network adapter
1322 *
1323 * Polled functionality used by netconsole and others in non interrupt mode
1324 *
1325 */
1326void fec_poll_controller(struct net_device *dev)
1327{
1328 int i;
1329 struct fec_enet_private *fep = netdev_priv(dev);
1330
1331 for (i = 0; i < FEC_IRQ_NUM; i++) {
1332 if (fep->irq[i] > 0) {
1333 disable_irq(fep->irq[i]);
1334 fec_enet_interrupt(fep->irq[i], dev);
1335 enable_irq(fep->irq[i]);
1336 }
1337 }
1338}
1339#endif
1340
Sascha Hauer009fda82009-04-15 01:32:23 +00001341static const struct net_device_ops fec_netdev_ops = {
1342 .ndo_open = fec_enet_open,
1343 .ndo_stop = fec_enet_close,
1344 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001345 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001346 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001347 .ndo_validate_addr = eth_validate_addr,
1348 .ndo_tx_timeout = fec_timeout,
1349 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001350 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001351#ifdef CONFIG_NET_POLL_CONTROLLER
1352 .ndo_poll_controller = fec_poll_controller,
1353#endif
Sascha Hauer009fda82009-04-15 01:32:23 +00001354};
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001360static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001362 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001363 struct bufdesc *cbd_base;
Rob Herring633e7532010-02-05 08:56:20 +00001364 struct bufdesc *bdp;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001365 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001367 /* Allocate memory for buffer descriptors. */
1368 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1369 GFP_KERNEL);
1370 if (!cbd_base) {
Greg Ungerer562d2f82005-11-07 14:09:50 +10001371 printk("FEC: allocate descriptor memory failed?\n");
1372 return -ENOMEM;
1373 }
1374
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001375 spin_lock_init(&fep->hw_lock);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001376
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001377 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Shawn Guo49da97d2011-01-05 21:13:11 +00001379 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001380 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001382 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 fep->rx_bd_base = cbd_base;
1384 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1385
Sascha Hauer22f6b862009-04-15 01:32:18 +00001386 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001387 ndev->watchdog_timeo = TX_TIMEOUT;
1388 ndev->netdev_ops = &fec_netdev_ops;
1389 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001390
1391 /* Initialize the receive buffer descriptors. */
1392 bdp = fep->rx_bd_base;
1393 for (i = 0; i < RX_RING_SIZE; i++) {
1394
1395 /* Initialize the BD for every fragment in the page. */
1396 bdp->cbd_sc = 0;
1397 bdp++;
1398 }
1399
1400 /* Set the last buffer to wrap */
1401 bdp--;
1402 bdp->cbd_sc |= BD_SC_WRAP;
1403
1404 /* ...and the same for transmit */
1405 bdp = fep->tx_bd_base;
1406 for (i = 0; i < TX_RING_SIZE; i++) {
1407
1408 /* Initialize the BD for every fragment in the page. */
1409 bdp->cbd_sc = 0;
1410 bdp->cbd_bufaddr = 0;
1411 bdp++;
1412 }
1413
1414 /* Set the last buffer to wrap */
1415 bdp--;
1416 bdp->cbd_sc |= BD_SC_WRAP;
1417
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001418 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
1422
Shawn Guoca2cc332011-06-25 02:04:35 +08001423#ifdef CONFIG_OF
1424static int __devinit fec_get_phy_mode_dt(struct platform_device *pdev)
1425{
1426 struct device_node *np = pdev->dev.of_node;
1427
1428 if (np)
1429 return of_get_phy_mode(np);
1430
1431 return -ENODEV;
1432}
1433
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001434static void __devinit fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001435{
1436 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001437 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001438 struct device_node *np = pdev->dev.of_node;
1439
1440 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001441 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001442
Shawn Guoa3caad02012-06-27 03:45:24 +00001443 of_property_read_u32(np, "phy-reset-duration", &msec);
1444 /* A sane reset duration should not be longer than 1s */
1445 if (msec > 1000)
1446 msec = 1;
1447
Shawn Guoca2cc332011-06-25 02:04:35 +08001448 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Shawn Guo119fc002012-06-27 03:45:22 +00001449 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1450 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001451 if (err) {
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001452 pr_debug("FEC: failed to get gpio phy-reset: %d\n", err);
1453 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001454 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001455 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001456 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001457}
1458#else /* CONFIG_OF */
1459static inline int fec_get_phy_mode_dt(struct platform_device *pdev)
1460{
1461 return -ENODEV;
1462}
1463
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001464static inline void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001465{
1466 /*
1467 * In case of platform probe, the reset has been done
1468 * by machine code.
1469 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001470}
1471#endif /* CONFIG_OF */
1472
Sascha Haueread73182009-01-28 23:03:11 +00001473static int __devinit
1474fec_probe(struct platform_device *pdev)
1475{
1476 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001477 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001478 struct net_device *ndev;
1479 int i, irq, ret = 0;
1480 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001481 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001482 static int dev_id;
Shawn Guob2bccee2012-05-06 20:24:04 +08001483 struct pinctrl *pinctrl;
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001484 struct regulator *reg_phy;
Shawn Guoca2cc332011-06-25 02:04:35 +08001485
1486 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1487 if (of_id)
1488 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001489
1490 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1491 if (!r)
1492 return -ENXIO;
1493
1494 r = request_mem_region(r->start, resource_size(r), pdev->name);
1495 if (!r)
1496 return -EBUSY;
1497
1498 /* Init network device */
1499 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001500 if (!ndev) {
1501 ret = -ENOMEM;
1502 goto failed_alloc_etherdev;
1503 }
Sascha Haueread73182009-01-28 23:03:11 +00001504
1505 SET_NETDEV_DEV(ndev, &pdev->dev);
1506
1507 /* setup board info structure */
1508 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001509
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001510 fep->hwp = ioremap(r->start, resource_size(r));
Bryan Wue6b043d2010-03-31 02:10:44 +00001511 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00001512 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00001513
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001514 if (!fep->hwp) {
Sascha Haueread73182009-01-28 23:03:11 +00001515 ret = -ENOMEM;
1516 goto failed_ioremap;
1517 }
1518
1519 platform_set_drvdata(pdev, ndev);
1520
Shawn Guoca2cc332011-06-25 02:04:35 +08001521 ret = fec_get_phy_mode_dt(pdev);
1522 if (ret < 0) {
1523 pdata = pdev->dev.platform_data;
1524 if (pdata)
1525 fep->phy_interface = pdata->phy;
1526 else
1527 fep->phy_interface = PHY_INTERFACE_MODE_MII;
1528 } else {
1529 fep->phy_interface = ret;
1530 }
1531
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001532 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001533 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00001534 if (irq < 0) {
1535 if (i)
1536 break;
1537 ret = irq;
1538 goto failed_irq;
1539 }
Sascha Haueread73182009-01-28 23:03:11 +00001540 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1541 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01001542 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00001543 irq = platform_get_irq(pdev, i);
1544 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001545 }
1546 goto failed_irq;
1547 }
1548 }
1549
Shawn Guob2bccee2012-05-06 20:24:04 +08001550 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1551 if (IS_ERR(pinctrl)) {
1552 ret = PTR_ERR(pinctrl);
1553 goto failed_pin;
1554 }
1555
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001556 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1557 if (IS_ERR(fep->clk_ipg)) {
1558 ret = PTR_ERR(fep->clk_ipg);
Sascha Haueread73182009-01-28 23:03:11 +00001559 goto failed_clk;
1560 }
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001561
1562 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1563 if (IS_ERR(fep->clk_ahb)) {
1564 ret = PTR_ERR(fep->clk_ahb);
1565 goto failed_clk;
1566 }
1567
1568 clk_prepare_enable(fep->clk_ahb);
1569 clk_prepare_enable(fep->clk_ipg);
Sascha Haueread73182009-01-28 23:03:11 +00001570
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001571 reg_phy = devm_regulator_get(&pdev->dev, "phy");
1572 if (!IS_ERR(reg_phy)) {
1573 ret = regulator_enable(reg_phy);
1574 if (ret) {
1575 dev_err(&pdev->dev,
1576 "Failed to enable phy regulator: %d\n", ret);
1577 goto failed_regulator;
1578 }
1579 }
1580
Shawn Guo2ca9b2a2012-06-27 03:45:20 +00001581 fec_reset_phy(pdev);
1582
Shawn Guo8649a232011-01-05 21:13:10 +00001583 ret = fec_enet_init(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001584 if (ret)
1585 goto failed_init;
1586
Bryan Wue6b043d2010-03-31 02:10:44 +00001587 ret = fec_enet_mii_init(pdev);
1588 if (ret)
1589 goto failed_mii_init;
1590
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001591 /* Carrier starts down, phylib will bring it up */
1592 netif_carrier_off(ndev);
1593
Sascha Haueread73182009-01-28 23:03:11 +00001594 ret = register_netdev(ndev);
1595 if (ret)
1596 goto failed_register;
1597
1598 return 0;
1599
1600failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001601 fec_enet_mii_remove(fep);
1602failed_mii_init:
Sascha Haueread73182009-01-28 23:03:11 +00001603failed_init:
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001604failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001605 clk_disable_unprepare(fep->clk_ahb);
1606 clk_disable_unprepare(fep->clk_ipg);
Shawn Guob2bccee2012-05-06 20:24:04 +08001607failed_pin:
Sascha Haueread73182009-01-28 23:03:11 +00001608failed_clk:
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001609 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001610 irq = platform_get_irq(pdev, i);
1611 if (irq > 0)
1612 free_irq(irq, ndev);
1613 }
1614failed_irq:
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001615 iounmap(fep->hwp);
Sascha Haueread73182009-01-28 23:03:11 +00001616failed_ioremap:
1617 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001618failed_alloc_etherdev:
1619 release_mem_region(r->start, resource_size(r));
Sascha Haueread73182009-01-28 23:03:11 +00001620
1621 return ret;
1622}
1623
1624static int __devexit
1625fec_drv_remove(struct platform_device *pdev)
1626{
1627 struct net_device *ndev = platform_get_drvdata(pdev);
1628 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001629 struct resource *r;
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001630 int i;
Sascha Haueread73182009-01-28 23:03:11 +00001631
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001632 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001633 fec_enet_mii_remove(fep);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001634 for (i = 0; i < FEC_IRQ_NUM; i++) {
1635 int irq = platform_get_irq(pdev, i);
1636 if (irq > 0)
1637 free_irq(irq, ndev);
1638 }
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001639 clk_disable_unprepare(fep->clk_ahb);
1640 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001641 iounmap(fep->hwp);
Sascha Haueread73182009-01-28 23:03:11 +00001642 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01001643
1644 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1645 BUG_ON(!r);
1646 release_mem_region(r->start, resource_size(r));
1647
Uwe Kleine-Königb3cde362011-01-25 18:03:37 +01001648 platform_set_drvdata(pdev, NULL);
1649
Sascha Haueread73182009-01-28 23:03:11 +00001650 return 0;
1651}
1652
Denis Kirjanov59d42892010-06-02 09:27:04 +00001653#ifdef CONFIG_PM
Sascha Haueread73182009-01-28 23:03:11 +00001654static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001655fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001656{
Eric Benard87cad5c2010-06-18 04:19:54 +00001657 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001658 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001659
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001660 if (netif_running(ndev)) {
1661 fec_stop(ndev);
1662 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001663 }
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001664 clk_disable_unprepare(fep->clk_ahb);
1665 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001666
Sascha Haueread73182009-01-28 23:03:11 +00001667 return 0;
1668}
1669
1670static int
Eric Benard87cad5c2010-06-18 04:19:54 +00001671fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00001672{
Eric Benard87cad5c2010-06-18 04:19:54 +00001673 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001674 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001675
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001676 clk_prepare_enable(fep->clk_ahb);
1677 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001678 if (netif_running(ndev)) {
1679 fec_restart(ndev, fep->full_duplex);
1680 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001681 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01001682
Sascha Haueread73182009-01-28 23:03:11 +00001683 return 0;
1684}
1685
Denis Kirjanov59d42892010-06-02 09:27:04 +00001686static const struct dev_pm_ops fec_pm_ops = {
1687 .suspend = fec_suspend,
1688 .resume = fec_resume,
1689 .freeze = fec_suspend,
1690 .thaw = fec_resume,
1691 .poweroff = fec_suspend,
1692 .restore = fec_resume,
1693};
Eric Benard87cad5c2010-06-18 04:19:54 +00001694#endif
Denis Kirjanov59d42892010-06-02 09:27:04 +00001695
Sascha Haueread73182009-01-28 23:03:11 +00001696static struct platform_driver fec_driver = {
1697 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00001698 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00001699 .owner = THIS_MODULE,
1700#ifdef CONFIG_PM
1701 .pm = &fec_pm_ops,
1702#endif
Shawn Guoca2cc332011-06-25 02:04:35 +08001703 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00001704 },
Shawn Guob5680e02011-01-05 21:13:13 +00001705 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00001706 .probe = fec_probe,
1707 .remove = __devexit_p(fec_drv_remove),
Sascha Haueread73182009-01-28 23:03:11 +00001708};
1709
Fabio Estevamaaca2372012-01-23 16:44:50 +00001710module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712MODULE_LICENSE("GPL");