blob: 570dfad8403aecd2290bbec9726fc73c47832773 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10005 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
10 * small packets.
11 *
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
14 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100015 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100017 *
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100019 * Copyright (c) 2004-2006 Macq Electronique SA.
Shawn Guob5680e02011-01-05 21:13:13 +000020 *
Shawn Guo230dec62011-09-23 02:12:48 +000021 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/string.h>
27#include <linux/ptrace.h>
28#include <linux/errno.h>
29#include <linux/ioport.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000037#include <linux/in.h>
38#include <linux/ip.h>
39#include <net/ip.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
44#include <linux/workqueue.h>
45#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000046#include <linux/io.h>
47#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000048#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000049#include <linux/platform_device.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000050#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070051#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080052#include <linux/of.h>
53#include <linux/of_device.h>
54#include <linux/of_gpio.h>
55#include <linux/of_net.h>
Shawn Guob2bccee2012-05-06 20:24:04 +080056#include <linux/pinctrl/consumer.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000057#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Greg Ungerer080853a2007-07-30 16:28:46 +100059#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +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"
Frank Lidc975382013-01-28 18:31:42 +000070#define FEC_NAPI_WEIGHT 64
Shawn Guob5680e02011-01-05 21:13:13 +000071
Frank Libaa70a52013-01-16 16:55:58 +000072/* Pause frame feild and FIFO threshold */
73#define FEC_ENET_FCE (1 << 5)
74#define FEC_ENET_RSEM_V 0x84
75#define FEC_ENET_RSFL_V 16
76#define FEC_ENET_RAEM_V 0x8
77#define FEC_ENET_RAFL_V 0x8
78#define FEC_ENET_OPD_V 0xFFF0
79
Shawn Guob5680e02011-01-05 21:13:13 +000080/* Controller is ENET-MAC */
81#define FEC_QUIRK_ENET_MAC (1 << 0)
82/* Controller needs driver to swap frame */
83#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080084/* Controller uses gasket */
85#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000086/* Controller has GBIT support */
87#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000088/* Controller has extend desc buffer */
89#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000090/* Controller has hardware checksum support */
91#define FEC_QUIRK_HAS_CSUM (1 << 5)
Shawn Guob5680e02011-01-05 21:13:13 +000092
93static struct platform_device_id fec_devtype[] = {
94 {
Shawn Guo0ca1e292011-07-01 18:11:22 +080095 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +000096 .name = DRIVER_NAME,
97 .driver_data = 0,
98 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +080099 .name = "imx25-fec",
100 .driver_data = FEC_QUIRK_USE_GASKET,
101 }, {
102 .name = "imx27-fec",
103 .driver_data = 0,
104 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000105 .name = "imx28-fec",
106 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800107 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000108 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000109 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Shawn Guo48496252013-05-08 21:08:22 +0000110 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM,
Shawn Guo230dec62011-09-23 02:12:48 +0000111 }, {
Jingchang Luca7c4a42013-04-11 21:12:45 +0000112 .name = "mvf-fec",
113 .driver_data = FEC_QUIRK_ENET_MAC,
114 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800115 /* sentinel */
116 }
Shawn Guob5680e02011-01-05 21:13:13 +0000117};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800118MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000119
Shawn Guoca2cc332011-06-25 02:04:35 +0800120enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000121 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800122 IMX27_FEC, /* runs on i.mx27/35/51 */
123 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000124 IMX6Q_FEC,
Jingchang Luca7c4a42013-04-11 21:12:45 +0000125 MVF_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800126};
127
128static const struct of_device_id fec_dt_ids[] = {
129 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
130 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
131 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000132 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Jingchang Luca7c4a42013-04-11 21:12:45 +0000133 { .compatible = "fsl,mvf-fec", .data = &fec_devtype[MVF_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800134 { /* sentinel */ }
135};
136MODULE_DEVICE_TABLE(of, fec_dt_ids);
137
Shawn Guo49da97d2011-01-05 21:13:11 +0000138static unsigned char macaddr[ETH_ALEN];
139module_param_array(macaddr, byte, NULL, 0);
140MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
141
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000142#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * Some hardware gets it MAC address out of local flash memory.
145 * if this is non-zero then assume it is the address to get MAC from.
146 */
147#if defined(CONFIG_NETtel)
148#define FEC_FLASHMAC 0xf0006006
149#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
150#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#elif defined(CONFIG_CANCam)
152#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000153#elif defined (CONFIG_M5272C3)
154#define FEC_FLASHMAC (0xffe04000 + 4)
155#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000156#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#else
158#define FEC_FLASHMAC 0
159#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800160#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000161
Frank Liff43da82013-01-03 16:04:23 +0000162#if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000163#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000164#endif
165
Sascha Hauer22f6b862009-04-15 01:32:18 +0000166/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
168#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
169#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
170#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
171#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
172#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
173#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
174#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
175#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
176#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
177
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000178#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000179#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* The FEC stores dest/src/type, data, and checksum for receive packets.
182 */
183#define PKT_MAXBUF_SIZE 1518
184#define PKT_MINBUF_SIZE 64
185#define PKT_MAXBLR_SIZE 1520
186
Jim Baxter4c09eed2013-04-19 08:10:49 +0000187/* FEC receive acceleration */
188#define FEC_RACC_IPDIS (1 << 1)
189#define FEC_RACC_PRODIS (1 << 2)
190#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
Matt Waddel6b265292006-06-27 13:10:56 +1000193 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * size bits. Other FEC hardware does not, so we need to take that into
195 * account when setting it.
196 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000197#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100198 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
200#else
201#define OPT_FRAME_SIZE 0
202#endif
203
Bryan Wue6b043d2010-03-31 02:10:44 +0000204/* FEC MII MMFR bits definition */
205#define FEC_MMFR_ST (1 << 30)
206#define FEC_MMFR_OP_READ (2 << 28)
207#define FEC_MMFR_OP_WRITE (1 << 28)
208#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
209#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
210#define FEC_MMFR_TA (2 << 16)
211#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500213#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Sascha Hauer22f6b862009-04-15 01:32:18 +0000215/* Transmitter timeout */
216#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Frank Libaa70a52013-01-16 16:55:58 +0000218#define FEC_PAUSE_FLAG_AUTONEG 0x1
219#define FEC_PAUSE_FLAG_ENABLE 0x2
220
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000221static int mii_cnt;
222
Frank Liff43da82013-01-03 16:04:23 +0000223static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex)
224{
225 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
226 if (is_ex)
227 return (struct bufdesc *)(ex + 1);
228 else
229 return bdp + 1;
230}
231
232static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex)
233{
234 struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
235 if (is_ex)
236 return (struct bufdesc *)(ex - 1);
237 else
238 return bdp - 1;
239}
240
Shawn Guob5680e02011-01-05 21:13:13 +0000241static void *swap_buffer(void *bufaddr, int len)
242{
243 int i;
244 unsigned int *buf = bufaddr;
245
246 for (i = 0; i < (len + 3) / 4; i++, buf++)
247 *buf = cpu_to_be32(*buf);
248
249 return bufaddr;
250}
251
Jim Baxter4c09eed2013-04-19 08:10:49 +0000252static int
253fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
254{
255 /* Only run for packets requiring a checksum. */
256 if (skb->ip_summed != CHECKSUM_PARTIAL)
257 return 0;
258
259 if (unlikely(skb_cow_head(skb, 0)))
260 return -1;
261
262 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
263
264 return 0;
265}
266
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000267static netdev_tx_t
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100268fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100270 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000271 const struct platform_device_id *id_entry =
272 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000273 struct bufdesc *bdp;
Greg Ungerer9555b312009-08-06 17:58:18 +0000274 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000275 unsigned short status;
Frank Lide5fb0a2013-03-03 17:34:25 +0000276 unsigned int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!fep->link) {
Jim Baxter4c09eed2013-04-19 08:10:49 +0000279 /* Link is down or auto-negotiation is in progress. */
Patrick McHardy5b548142009-06-12 06:22:29 +0000280 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 /* Fill in a Tx ring entry */
284 bdp = fep->cur_tx;
285
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000286 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000287
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000288 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Ooops. All transmit buffers are full. Bail out.
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100290 * This should not happen, since ndev->tbusy should be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Joe Perches31b77202013-04-13 19:03:17 +0000292 netdev_err(ndev, "tx queue full!\n");
Patrick McHardy5b548142009-06-12 06:22:29 +0000293 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jim Baxter4c09eed2013-04-19 08:10:49 +0000296 /* Protocol checksum off-load for TCP and UDP. */
297 if (fec_enet_clear_csum(skb, ndev)) {
298 kfree_skb(skb);
299 return NETDEV_TX_OK;
300 }
301
Sascha Hauer22f6b862009-04-15 01:32:18 +0000302 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000303 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Sascha Hauer22f6b862009-04-15 01:32:18 +0000305 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000306 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 bdp->cbd_datlen = skb->len;
308
309 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000310 * On some FEC implementations data must be aligned on
311 * 4-byte boundaries. Use bounce buffers to copy data
312 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000314 if (fep->bufdesc_ex)
315 index = (struct bufdesc_ex *)bdp -
316 (struct bufdesc_ex *)fep->tx_bd_base;
317 else
318 index = bdp - fep->tx_bd_base;
319
Greg Ungerer9555b312009-08-06 17:58:18 +0000320 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Uwe Kleine-König8a73b0b2011-01-13 21:34:31 +0100321 memcpy(fep->tx_bounce[index], skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000322 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Shawn Guob5680e02011-01-05 21:13:13 +0000325 /*
326 * Some design made an incorrect assumption on endian mode of
327 * the system that it's running on. As the result, driver has to
328 * swap every frame going to and coming from the controller.
329 */
330 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
331 swap_buffer(bufaddr, skb->len);
332
Sascha Hauer22f6b862009-04-15 01:32:18 +0000333 /* Save skb pointer */
Frank Lide5fb0a2013-03-03 17:34:25 +0000334 fep->tx_skbuff[index] = skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Push the data cache so the CPM does not get stale memory
337 * data.
338 */
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100339 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000340 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000342 /* Send it on its way. Tell FEC it's ready, interrupt when done,
343 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000345 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000347 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Frank Liff43da82013-01-03 16:04:23 +0000349 if (fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000350
Frank Liff43da82013-01-03 16:04:23 +0000351 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
352 ebdp->cbd_bdu = 0;
353 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
354 fep->hwts_tx_en)) {
355 ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
356 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
357 } else {
Frank Liff43da82013-01-03 16:04:23 +0000358 ebdp->cbd_esc = BD_ENET_TX_INT;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000359
360 /* Enable protocol checksum flags
361 * We do not bother with the IP Checksum bits as they
362 * are done by the kernel
363 */
364 if (skb->ip_summed == CHECKSUM_PARTIAL)
365 ebdp->cbd_esc |= BD_ENET_TX_PINS;
Frank Liff43da82013-01-03 16:04:23 +0000366 }
Frank Li6605b732012-10-30 18:25:31 +0000367 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000368 /* If this was the last BD in the ring, start at the beginning again. */
369 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 bdp = fep->tx_bd_base;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000371 else
Frank Liff43da82013-01-03 16:04:23 +0000372 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Sascha Hauer2e285322009-04-15 01:32:16 +0000374 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Frank Lide5fb0a2013-03-03 17:34:25 +0000376 if (fep->cur_tx == fep->dirty_tx)
377 netif_stop_queue(ndev);
Richard Cochran18a03b92011-06-12 02:18:59 +0000378
Frank Lide5fb0a2013-03-03 17:34:25 +0000379 /* Trigger transmission start */
380 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
381
382 skb_tx_timestamp(skb);
Richard Cochrana0087a32011-06-19 03:31:40 +0000383
Patrick McHardy6ed10652009-06-23 06:03:08 +0000384 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Frank Li14109a52013-03-26 16:12:03 +0000387/* Init RX & TX buffer descriptors
388 */
389static void fec_enet_bd_init(struct net_device *dev)
390{
391 struct fec_enet_private *fep = netdev_priv(dev);
392 struct bufdesc *bdp;
393 unsigned int i;
394
395 /* Initialize the receive buffer descriptors. */
396 bdp = fep->rx_bd_base;
397 for (i = 0; i < RX_RING_SIZE; i++) {
398
399 /* Initialize the BD for every fragment in the page. */
400 if (bdp->cbd_bufaddr)
401 bdp->cbd_sc = BD_ENET_RX_EMPTY;
402 else
403 bdp->cbd_sc = 0;
404 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
405 }
406
407 /* Set the last buffer to wrap */
408 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
409 bdp->cbd_sc |= BD_SC_WRAP;
410
411 fep->cur_rx = fep->rx_bd_base;
412
413 /* ...and the same for transmit */
414 bdp = fep->tx_bd_base;
415 fep->cur_tx = bdp;
416 for (i = 0; i < TX_RING_SIZE; i++) {
417
418 /* Initialize the BD for every fragment in the page. */
419 bdp->cbd_sc = 0;
420 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
421 dev_kfree_skb_any(fep->tx_skbuff[i]);
422 fep->tx_skbuff[i] = NULL;
423 }
424 bdp->cbd_bufaddr = 0;
425 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
426 }
427
428 /* Set the last buffer to wrap */
429 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
430 bdp->cbd_sc |= BD_SC_WRAP;
431 fep->dirty_tx = bdp;
432}
433
Uwe Kleine-König45993652011-01-19 20:47:04 +0100434/* This function is called to start or restart the FEC during a link
435 * change. This only happens when switching between half and full
436 * duplex.
437 */
438static void
439fec_restart(struct net_device *ndev, int duplex)
440{
441 struct fec_enet_private *fep = netdev_priv(ndev);
442 const struct platform_device_id *id_entry =
443 platform_get_device_id(fep->pdev);
444 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000445 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100446 u32 temp_mac[2];
447 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000448 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100449
Frank Li54309fa2013-05-07 14:08:44 +0000450 if (netif_running(ndev)) {
451 netif_device_detach(ndev);
452 napi_disable(&fep->napi);
453 netif_stop_queue(ndev);
Fabio Estevam31691342013-05-15 07:06:26 +0000454 netif_tx_lock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000455 }
456
Uwe Kleine-König45993652011-01-19 20:47:04 +0100457 /* Whack a reset. We should wait for this. */
458 writel(1, fep->hwp + FEC_ECNTRL);
459 udelay(10);
460
461 /*
462 * enet-mac reset will reset mac address registers too,
463 * so need to reconfigure it.
464 */
465 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
466 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
467 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
468 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
469 }
470
471 /* Clear any outstanding interrupt. */
472 writel(0xffc00000, fep->hwp + FEC_IEVENT);
473
474 /* Reset all multicast. */
475 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
476 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
477#ifndef CONFIG_M5272
478 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
479 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
480#endif
481
482 /* Set maximum receive buffer size. */
483 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
484
Frank Li14109a52013-03-26 16:12:03 +0000485 fec_enet_bd_init(ndev);
486
Uwe Kleine-König45993652011-01-19 20:47:04 +0100487 /* Set receive and transmit descriptor base. */
488 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000489 if (fep->bufdesc_ex)
490 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
491 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
492 else
493 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
494 * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100495
Uwe Kleine-König45993652011-01-19 20:47:04 +0100496
Uwe Kleine-König45993652011-01-19 20:47:04 +0100497 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
498 if (fep->tx_skbuff[i]) {
499 dev_kfree_skb_any(fep->tx_skbuff[i]);
500 fep->tx_skbuff[i] = NULL;
501 }
502 }
503
504 /* Enable MII mode */
505 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100506 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100507 writel(0x04, fep->hwp + FEC_X_CNTRL);
508 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100509 /* No Rcv on Xmit */
510 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100511 writel(0x0, fep->hwp + FEC_X_CNTRL);
512 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100513
Uwe Kleine-König45993652011-01-19 20:47:04 +0100514 fep->full_duplex = duplex;
515
516 /* Set MII speed */
517 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
518
Jim Baxter4c09eed2013-04-19 08:10:49 +0000519 /* set RX checksum */
520 val = readl(fep->hwp + FEC_RACC);
521 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
522 val |= FEC_RACC_OPTIONS;
523 else
524 val &= ~FEC_RACC_OPTIONS;
525 writel(val, fep->hwp + FEC_RACC);
526
Uwe Kleine-König45993652011-01-19 20:47:04 +0100527 /*
528 * The phy interface and speed need to get configured
529 * differently on enet-mac.
530 */
531 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100532 /* Enable flow control and length check */
533 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100534
Shawn Guo230dec62011-09-23 02:12:48 +0000535 /* RGMII, RMII or MII */
536 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
537 rcntl |= (1 << 6);
538 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100539 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100540 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100541 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100542
Shawn Guo230dec62011-09-23 02:12:48 +0000543 /* 1G, 100M or 10M */
544 if (fep->phy_dev) {
545 if (fep->phy_dev->speed == SPEED_1000)
546 ecntl |= (1 << 5);
547 else if (fep->phy_dev->speed == SPEED_100)
548 rcntl &= ~(1 << 9);
549 else
550 rcntl |= (1 << 9);
551 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100552 } else {
553#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800554 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000555 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100556 /* disable the gasket and wait */
557 writel(0, fep->hwp + FEC_MIIGSK_ENR);
558 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
559 udelay(1);
560
561 /*
562 * configure the gasket:
563 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800564 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100565 */
Eric Benard8d82f212012-01-12 06:10:28 +0000566 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
567 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
568 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
569 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
570 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100571
572 /* re-enable the gasket */
573 writel(2, fep->hwp + FEC_MIIGSK_ENR);
574 }
575#endif
576 }
Frank Libaa70a52013-01-16 16:55:58 +0000577
578 /* enable pause frame*/
579 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
580 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
581 fep->phy_dev && fep->phy_dev->pause)) {
582 rcntl |= FEC_ENET_FCE;
583
Jim Baxter4c09eed2013-04-19 08:10:49 +0000584 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000585 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
586 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
587 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
588 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
589
590 /* OPD */
591 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
592 } else {
593 rcntl &= ~FEC_ENET_FCE;
594 }
595
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100596 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100597
Shawn Guo230dec62011-09-23 02:12:48 +0000598 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
599 /* enable ENET endian swap */
600 ecntl |= (1 << 8);
601 /* enable ENET store and forward mode */
602 writel(1 << 8, fep->hwp + FEC_X_WMRK);
603 }
604
Frank Liff43da82013-01-03 16:04:23 +0000605 if (fep->bufdesc_ex)
606 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000607
Uwe Kleine-König45993652011-01-19 20:47:04 +0100608 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000609 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100610 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
611
Frank Liff43da82013-01-03 16:04:23 +0000612 if (fep->bufdesc_ex)
613 fec_ptp_start_cyclecounter(ndev);
614
Uwe Kleine-König45993652011-01-19 20:47:04 +0100615 /* Enable interrupts we wish to service */
616 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Frank Li54309fa2013-05-07 14:08:44 +0000617
618 if (netif_running(ndev)) {
Fabio Estevam31691342013-05-15 07:06:26 +0000619 netif_tx_unlock_bh(ndev);
Fabio Estevam1ed0d562013-05-15 07:06:27 +0000620 netif_wake_queue(ndev);
621 napi_enable(&fep->napi);
622 netif_device_attach(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000623 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100624}
625
626static void
627fec_stop(struct net_device *ndev)
628{
629 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000630 const struct platform_device_id *id_entry =
631 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000632 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100633
634 /* We cannot expect a graceful transmit stop without link !!! */
635 if (fep->link) {
636 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
637 udelay(10);
638 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000639 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +0100640 }
641
642 /* Whack a reset. We should wait for this. */
643 writel(1, fep->hwp + FEC_ECNTRL);
644 udelay(10);
645 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
646 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000647
648 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000649 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000650 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000651 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
652 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100653}
654
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100657fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100659 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100661 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Frank Li54309fa2013-05-07 14:08:44 +0000663 fep->delay_work.timeout = true;
664 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
665}
666
667static void fec_enet_work(struct work_struct *work)
668{
669 struct fec_enet_private *fep =
670 container_of(work,
671 struct fec_enet_private,
672 delay_work.delay_work.work);
673
674 if (fep->delay_work.timeout) {
675 fep->delay_work.timeout = false;
676 fec_restart(fep->netdev, fep->full_duplex);
677 netif_wake_queue(fep->netdev);
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100682fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000685 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000686 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +0000688 int index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100690 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 bdp = fep->dirty_tx;
692
Frank Lide5fb0a2013-03-03 17:34:25 +0000693 /* get next bdp of dirty_tx */
694 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
695 bdp = fep->tx_bd_base;
696 else
697 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
698
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000699 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +0000700
701 /* current queue is empty */
702 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000703 break;
704
Frank Lide5fb0a2013-03-03 17:34:25 +0000705 if (fep->bufdesc_ex)
706 index = (struct bufdesc_ex *)bdp -
707 (struct bufdesc_ex *)fep->tx_bd_base;
708 else
709 index = bdp - fep->tx_bd_base;
710
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100711 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
712 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000713 bdp->cbd_bufaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Frank Lide5fb0a2013-03-03 17:34:25 +0000715 skb = fep->tx_skbuff[index];
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000718 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 BD_ENET_TX_RL | BD_ENET_TX_UN |
720 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100721 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000722 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100723 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000724 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100725 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000726 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100727 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000728 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100729 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000730 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100731 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100733 ndev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Frank Liff43da82013-01-03 16:04:23 +0000736 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
737 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000738 struct skb_shared_hwtstamps shhwtstamps;
739 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000740 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000741
742 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
743 spin_lock_irqsave(&fep->tmreg_lock, flags);
744 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000745 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000746 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
747 skb_tstamp_tx(skb, &shhwtstamps);
748 }
Frank Liff43da82013-01-03 16:04:23 +0000749
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000750 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +0000751 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* Deferred means some collisions occurred during transmit,
754 * but we eventually sent the packet OK.
755 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000756 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100757 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400758
Sascha Hauer22f6b862009-04-15 01:32:18 +0000759 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +0000761 fep->tx_skbuff[index] = NULL;
762
763 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400764
Sascha Hauer22f6b862009-04-15 01:32:18 +0000765 /* Update pointer to next buffer descriptor to be transmitted */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000766 if (status & BD_ENET_TX_WRAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 bdp = fep->tx_bd_base;
768 else
Frank Liff43da82013-01-03 16:04:23 +0000769 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400770
Sascha Hauer22f6b862009-04-15 01:32:18 +0000771 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000773 if (fep->dirty_tx != fep->cur_tx) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100774 if (netif_queue_stopped(ndev))
775 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 }
Frank Lide5fb0a2013-03-03 17:34:25 +0000778 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781
782/* During a receive, the cur_rx points to the current incoming buffer.
783 * When we update through the ring, if the next incoming buffer has
784 * not been given to the system, we just set the empty indicator,
785 * effectively tossing the packet.
786 */
Frank Lidc975382013-01-28 18:31:42 +0000787static int
788fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100790 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000791 const struct platform_device_id *id_entry =
792 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000793 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000794 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct sk_buff *skb;
796 ushort pkt_len;
797 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +0000798 int pkt_received = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400799
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000800#ifdef CONFIG_M532x
801 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400802#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* First, grab all of the stats for the incoming packet.
805 * These get messed up if we get called due to a busy condition.
806 */
807 bdp = fep->cur_rx;
808
Sascha Hauer22f6b862009-04-15 01:32:18 +0000809 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Frank Lidc975382013-01-28 18:31:42 +0000811 if (pkt_received >= budget)
812 break;
813 pkt_received++;
814
Sascha Hauer22f6b862009-04-15 01:32:18 +0000815 /* Since we have allocated space to hold a complete frame,
816 * the last indicator should be set.
817 */
818 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +0000819 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Sascha Hauer22f6b862009-04-15 01:32:18 +0000821 if (!fep->opened)
822 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Sascha Hauer22f6b862009-04-15 01:32:18 +0000824 /* Check for errors. */
825 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100827 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000828 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
829 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100830 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000831 }
832 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100833 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000834 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100835 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000836 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100837 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000839
840 /* Report late collisions as a frame error.
841 * On this error, the BD is closed, but we don't know what we
842 * have in the buffer. So, just drop this frame on the floor.
843 */
844 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100845 ndev->stats.rx_errors++;
846 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000847 goto rx_processing_done;
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Sascha Hauer22f6b862009-04-15 01:32:18 +0000850 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100851 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000852 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100853 ndev->stats.rx_bytes += pkt_len;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000854 data = (__u8*)__va(bdp->cbd_bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100856 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
857 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000858
Shawn Guob5680e02011-01-05 21:13:13 +0000859 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
860 swap_buffer(data, pkt_len);
861
Sascha Hauer22f6b862009-04-15 01:32:18 +0000862 /* This does 16 byte alignment, exactly what we need.
863 * The packet length includes FCS, but we don't want to
864 * include that when passing upstream as it messes up
865 * bridging applications.
866 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000867 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Sascha Hauer85498892009-04-15 01:32:21 +0000869 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100870 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000871 } else {
Sascha Hauer85498892009-04-15 01:32:21 +0000872 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000873 skb_put(skb, pkt_len - 4); /* Make room */
874 skb_copy_to_linear_data(skb, data, pkt_len - 4);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100875 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +0000876
Frank Li6605b732012-10-30 18:25:31 +0000877 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +0000878 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000879 struct skb_shared_hwtstamps *shhwtstamps =
880 skb_hwtstamps(skb);
881 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000882 struct bufdesc_ex *ebdp =
883 (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000884
885 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
886
887 spin_lock_irqsave(&fep->tmreg_lock, flags);
888 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000889 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000890 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
891 }
Frank Liff43da82013-01-03 16:04:23 +0000892
Jim Baxter4c09eed2013-04-19 08:10:49 +0000893 if (fep->bufdesc_ex &&
894 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
895 struct bufdesc_ex *ebdp =
896 (struct bufdesc_ex *)bdp;
897 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
898 /* don't check it */
899 skb->ip_summed = CHECKSUM_UNNECESSARY;
900 } else {
901 skb_checksum_none_assert(skb);
902 }
903 }
904
Richard Cochran18a03b92011-06-12 02:18:59 +0000905 if (!skb_defer_rx_timestamp(skb))
Frank Lidc975382013-01-28 18:31:42 +0000906 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000907 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000908
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100909 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
910 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000911rx_processing_done:
912 /* Clear the status flags for this buffer */
913 status &= ~BD_ENET_RX_STATS;
914
915 /* Mark the buffer empty */
916 status |= BD_ENET_RX_EMPTY;
917 bdp->cbd_sc = status;
918
Frank Liff43da82013-01-03 16:04:23 +0000919 if (fep->bufdesc_ex) {
920 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
921
922 ebdp->cbd_esc = BD_ENET_RX_INT;
923 ebdp->cbd_prot = 0;
924 ebdp->cbd_bdu = 0;
925 }
Frank Li6605b732012-10-30 18:25:31 +0000926
Sascha Hauer22f6b862009-04-15 01:32:18 +0000927 /* Update BD pointer to next entry */
928 if (status & BD_ENET_RX_WRAP)
929 bdp = fep->rx_bd_base;
930 else
Frank Liff43da82013-01-03 16:04:23 +0000931 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000932 /* Doing this here will keep the FEC running while we process
933 * incoming frames. On a heavily loaded network, we should be
934 * able to keep up at the expense of system resources.
935 */
936 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Sascha Hauer2e285322009-04-15 01:32:16 +0000938 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Frank Lidc975382013-01-28 18:31:42 +0000940 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Uwe Kleine-König45993652011-01-19 20:47:04 +0100943static irqreturn_t
944fec_enet_interrupt(int irq, void *dev_id)
945{
946 struct net_device *ndev = dev_id;
947 struct fec_enet_private *fep = netdev_priv(ndev);
948 uint int_events;
949 irqreturn_t ret = IRQ_NONE;
950
951 do {
952 int_events = readl(fep->hwp + FEC_IEVENT);
953 writel(int_events, fep->hwp + FEC_IEVENT);
954
Frank Lide5fb0a2013-03-03 17:34:25 +0000955 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
Uwe Kleine-König45993652011-01-19 20:47:04 +0100956 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +0000957
958 /* Disable the RX interrupt */
959 if (napi_schedule_prep(&fep->napi)) {
960 writel(FEC_RX_DISABLED_IMASK,
961 fep->hwp + FEC_IMASK);
962 __napi_schedule(&fep->napi);
963 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100964 }
965
Uwe Kleine-König45993652011-01-19 20:47:04 +0100966 if (int_events & FEC_ENET_MII) {
967 ret = IRQ_HANDLED;
968 complete(&fep->mdio_done);
969 }
970 } while (int_events);
971
972 return ret;
973}
974
Frank Lidc975382013-01-28 18:31:42 +0000975static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
976{
977 struct net_device *ndev = napi->dev;
978 int pkts = fec_enet_rx(ndev, budget);
979 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100980
Frank Lide5fb0a2013-03-03 17:34:25 +0000981 fec_enet_tx(ndev);
982
Frank Lidc975382013-01-28 18:31:42 +0000983 if (pkts < budget) {
984 napi_complete(napi);
985 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
986 }
987 return pkts;
988}
Uwe Kleine-König45993652011-01-19 20:47:04 +0100989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +0000991static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100993 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo49da97d2011-01-05 21:13:11 +0000994 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000995 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Shawn Guo49da97d2011-01-05 21:13:11 +0000997 /*
998 * try to get mac address in following order:
999 *
1000 * 1) module parameter via kernel command line in form
1001 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1002 */
1003 iap = macaddr;
1004
1005 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001006 * 2) from device tree data
1007 */
1008 if (!is_valid_ether_addr(iap)) {
1009 struct device_node *np = fep->pdev->dev.of_node;
1010 if (np) {
1011 const char *mac = of_get_mac_address(np);
1012 if (mac)
1013 iap = (unsigned char *) mac;
1014 }
1015 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001016
1017 /*
1018 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001019 */
1020 if (!is_valid_ether_addr(iap)) {
1021#ifdef CONFIG_M5272
1022 if (FEC_FLASHMAC)
1023 iap = (unsigned char *)FEC_FLASHMAC;
1024#else
1025 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001026 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001027#endif
1028 }
1029
1030 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001031 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001032 */
1033 if (!is_valid_ether_addr(iap)) {
1034 *((unsigned long *) &tmpaddr[0]) =
1035 be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
1036 *((unsigned short *) &tmpaddr[4]) =
1037 be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 iap = &tmpaddr[0];
1039 }
1040
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001041 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Shawn Guo49da97d2011-01-05 21:13:11 +00001043 /* Adjust MAC if using macaddr */
1044 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001045 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048/* ------------------------------------------------------------------------- */
1049
Bryan Wue6b043d2010-03-31 02:10:44 +00001050/*
1051 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001053static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001055 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001056 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001057 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Bryan Wue6b043d2010-03-31 02:10:44 +00001059 /* Prevent a state halted on mii error */
1060 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1061 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001064
Bryan Wue6b043d2010-03-31 02:10:44 +00001065 if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001066 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001067 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001068 status_change = 1;
1069 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001070
Lucas Stachd97e7492013-03-14 05:12:01 +00001071 if (fep->full_duplex != phy_dev->duplex)
1072 status_change = 1;
1073
1074 if (phy_dev->speed != fep->speed) {
1075 fep->speed = phy_dev->speed;
1076 status_change = 1;
1077 }
1078
1079 /* if any of the above changed restart the FEC */
1080 if (status_change)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001081 fec_restart(ndev, phy_dev->duplex);
Lucas Stachd97e7492013-03-14 05:12:01 +00001082 } else {
1083 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001084 fec_stop(ndev);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001085 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001086 status_change = 1;
1087 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001088 }
1089
Bryan Wue6b043d2010-03-31 02:10:44 +00001090 if (status_change)
1091 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Bryan Wue6b043d2010-03-31 02:10:44 +00001094static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Bryan Wue6b043d2010-03-31 02:10:44 +00001096 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001097 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001098
1099 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001100 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001101
1102 /* start a read op */
1103 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1104 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1105 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1106
1107 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001108 time_left = wait_for_completion_timeout(&fep->mdio_done,
1109 usecs_to_jiffies(FEC_MII_TIMEOUT));
1110 if (time_left == 0) {
1111 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001112 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001113 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001114 }
1115
1116 /* return value */
1117 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1118}
1119
1120static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1121 u16 value)
1122{
1123 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001124 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001125
1126 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001127 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001128
Shawn Guo862f0982011-01-05 21:13:09 +00001129 /* start a write op */
1130 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001131 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1132 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1133 fep->hwp + FEC_MII_DATA);
1134
1135 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001136 time_left = wait_for_completion_timeout(&fep->mdio_done,
1137 usecs_to_jiffies(FEC_MII_TIMEOUT));
1138 if (time_left == 0) {
1139 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001140 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001141 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001142 }
1143
1144 return 0;
1145}
1146
1147static int fec_enet_mdio_reset(struct mii_bus *bus)
1148{
1149 return 0;
1150}
1151
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001152static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001153{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001154 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001155 const struct platform_device_id *id_entry =
1156 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001157 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001158 char mdio_bus_id[MII_BUS_ID_SIZE];
1159 char phy_name[MII_BUS_ID_SIZE + 3];
1160 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001161 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001162
Bryan Wu418bd0d2010-05-28 03:40:39 -07001163 fep->phy_dev = NULL;
1164
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001165 /* check for attached phy */
1166 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1167 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1168 continue;
1169 if (fep->mii_bus->phy_map[phy_id] == NULL)
1170 continue;
1171 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1172 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001173 if (dev_id--)
1174 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001175 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1176 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001177 }
1178
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001179 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001180 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001181 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001182 phy_id = 0;
1183 }
1184
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001185 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001186 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001187 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001188 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001189 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001190 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001191 }
1192
1193 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001194 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001195 phy_dev->supported &= PHY_GBIT_FEATURES;
Frank Libaa70a52013-01-16 16:55:58 +00001196 phy_dev->supported |= SUPPORTED_Pause;
1197 }
Shawn Guo230dec62011-09-23 02:12:48 +00001198 else
1199 phy_dev->supported &= PHY_BASIC_FEATURES;
1200
Bryan Wue6b043d2010-03-31 02:10:44 +00001201 phy_dev->advertising = phy_dev->supported;
1202
1203 fep->phy_dev = phy_dev;
1204 fep->link = 0;
1205 fep->full_duplex = 0;
1206
Joe Perches31b77202013-04-13 19:03:17 +00001207 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1208 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1209 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001210
Bryan Wue6b043d2010-03-31 02:10:44 +00001211 return 0;
1212}
1213
1214static int fec_enet_mii_init(struct platform_device *pdev)
1215{
Shawn Guob5680e02011-01-05 21:13:13 +00001216 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001217 struct net_device *ndev = platform_get_drvdata(pdev);
1218 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001219 const struct platform_device_id *id_entry =
1220 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001221 int err = -ENXIO, i;
1222
Shawn Guob5680e02011-01-05 21:13:13 +00001223 /*
1224 * The dual fec interfaces are not equivalent with enet-mac.
1225 * Here are the differences:
1226 *
1227 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1228 * - fec0 acts as the 1588 time master while fec1 is slave
1229 * - external phys can only be configured by fec0
1230 *
1231 * That is to say fec1 can not work independently. It only works
1232 * when fec0 is working. The reason behind this design is that the
1233 * second interface is added primarily for Switch mode.
1234 *
1235 * Because of the last point above, both phys are attached on fec0
1236 * mdio interface in board design, and need to be configured by
1237 * fec0 mii_bus.
1238 */
Shawn Guo43af9402011-12-05 05:01:15 +00001239 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001240 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001241 if (mii_cnt && fec0_mii_bus) {
1242 fep->mii_bus = fec0_mii_bus;
1243 mii_cnt++;
1244 return 0;
1245 }
1246 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001247 }
1248
Bryan Wue6b043d2010-03-31 02:10:44 +00001249 fep->mii_timeout = 0;
1250
1251 /*
1252 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001253 *
1254 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1255 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1256 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1257 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001258 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001259 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001260 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1261 fep->phy_speed--;
1262 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001263 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1264
1265 fep->mii_bus = mdiobus_alloc();
1266 if (fep->mii_bus == NULL) {
1267 err = -ENOMEM;
1268 goto err_out;
1269 }
1270
1271 fep->mii_bus->name = "fec_enet_mii_bus";
1272 fep->mii_bus->read = fec_enet_mdio_read;
1273 fep->mii_bus->write = fec_enet_mdio_write;
1274 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001275 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1276 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001277 fep->mii_bus->priv = fep;
1278 fep->mii_bus->parent = &pdev->dev;
1279
1280 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1281 if (!fep->mii_bus->irq) {
1282 err = -ENOMEM;
1283 goto err_out_free_mdiobus;
1284 }
1285
1286 for (i = 0; i < PHY_MAX_ADDR; i++)
1287 fep->mii_bus->irq[i] = PHY_POLL;
1288
Bryan Wue6b043d2010-03-31 02:10:44 +00001289 if (mdiobus_register(fep->mii_bus))
1290 goto err_out_free_mdio_irq;
1291
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001292 mii_cnt++;
1293
Shawn Guob5680e02011-01-05 21:13:13 +00001294 /* save fec0 mii_bus */
1295 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1296 fec0_mii_bus = fep->mii_bus;
1297
Bryan Wue6b043d2010-03-31 02:10:44 +00001298 return 0;
1299
Bryan Wue6b043d2010-03-31 02:10:44 +00001300err_out_free_mdio_irq:
1301 kfree(fep->mii_bus->irq);
1302err_out_free_mdiobus:
1303 mdiobus_free(fep->mii_bus);
1304err_out:
1305 return err;
1306}
1307
1308static void fec_enet_mii_remove(struct fec_enet_private *fep)
1309{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001310 if (--mii_cnt == 0) {
1311 mdiobus_unregister(fep->mii_bus);
1312 kfree(fep->mii_bus->irq);
1313 mdiobus_free(fep->mii_bus);
1314 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001315}
1316
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001317static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001318 struct ethtool_cmd *cmd)
1319{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001320 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001321 struct phy_device *phydev = fep->phy_dev;
1322
1323 if (!phydev)
1324 return -ENODEV;
1325
1326 return phy_ethtool_gset(phydev, cmd);
1327}
1328
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001329static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001330 struct ethtool_cmd *cmd)
1331{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001332 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001333 struct phy_device *phydev = fep->phy_dev;
1334
1335 if (!phydev)
1336 return -ENODEV;
1337
1338 return phy_ethtool_sset(phydev, cmd);
1339}
1340
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001341static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001342 struct ethtool_drvinfo *info)
1343{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001344 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Jiri Pirko7826d432013-01-06 00:44:26 +00001346 strlcpy(info->driver, fep->pdev->dev.driver->name,
1347 sizeof(info->driver));
1348 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1349 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
Bryan Wue6b043d2010-03-31 02:10:44 +00001351
Frank Li5ebae4892013-01-06 16:25:07 +00001352static int fec_enet_get_ts_info(struct net_device *ndev,
1353 struct ethtool_ts_info *info)
1354{
1355 struct fec_enet_private *fep = netdev_priv(ndev);
1356
1357 if (fep->bufdesc_ex) {
1358
1359 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1360 SOF_TIMESTAMPING_RX_SOFTWARE |
1361 SOF_TIMESTAMPING_SOFTWARE |
1362 SOF_TIMESTAMPING_TX_HARDWARE |
1363 SOF_TIMESTAMPING_RX_HARDWARE |
1364 SOF_TIMESTAMPING_RAW_HARDWARE;
1365 if (fep->ptp_clock)
1366 info->phc_index = ptp_clock_index(fep->ptp_clock);
1367 else
1368 info->phc_index = -1;
1369
1370 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1371 (1 << HWTSTAMP_TX_ON);
1372
1373 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1374 (1 << HWTSTAMP_FILTER_ALL);
1375 return 0;
1376 } else {
1377 return ethtool_op_get_ts_info(ndev, info);
1378 }
1379}
1380
Frank Libaa70a52013-01-16 16:55:58 +00001381static void fec_enet_get_pauseparam(struct net_device *ndev,
1382 struct ethtool_pauseparam *pause)
1383{
1384 struct fec_enet_private *fep = netdev_priv(ndev);
1385
1386 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1387 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1388 pause->rx_pause = pause->tx_pause;
1389}
1390
1391static int fec_enet_set_pauseparam(struct net_device *ndev,
1392 struct ethtool_pauseparam *pause)
1393{
1394 struct fec_enet_private *fep = netdev_priv(ndev);
1395
1396 if (pause->tx_pause != pause->rx_pause) {
1397 netdev_info(ndev,
1398 "hardware only support enable/disable both tx and rx");
1399 return -EINVAL;
1400 }
1401
1402 fep->pause_flag = 0;
1403
1404 /* tx pause must be same as rx pause */
1405 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1406 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1407
1408 if (pause->rx_pause || pause->autoneg) {
1409 fep->phy_dev->supported |= ADVERTISED_Pause;
1410 fep->phy_dev->advertising |= ADVERTISED_Pause;
1411 } else {
1412 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1413 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1414 }
1415
1416 if (pause->autoneg) {
1417 if (netif_running(ndev))
1418 fec_stop(ndev);
1419 phy_start_aneg(fep->phy_dev);
1420 }
1421 if (netif_running(ndev))
1422 fec_restart(ndev, 0);
1423
1424 return 0;
1425}
1426
stephen hemminger9b07be42012-01-04 12:59:49 +00001427static const struct ethtool_ops fec_enet_ethtool_ops = {
Frank Libaa70a52013-01-16 16:55:58 +00001428 .get_pauseparam = fec_enet_get_pauseparam,
1429 .set_pauseparam = fec_enet_set_pauseparam,
Bryan Wue6b043d2010-03-31 02:10:44 +00001430 .get_settings = fec_enet_get_settings,
1431 .set_settings = fec_enet_set_settings,
1432 .get_drvinfo = fec_enet_get_drvinfo,
1433 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00001434 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00001435};
1436
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001437static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001438{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001439 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001440 struct phy_device *phydev = fep->phy_dev;
1441
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001442 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001443 return -EINVAL;
1444
1445 if (!phydev)
1446 return -ENODEV;
1447
Frank Liff43da82013-01-03 16:04:23 +00001448 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
Frank Li6605b732012-10-30 18:25:31 +00001449 return fec_ptp_ioctl(ndev, rq, cmd);
Frank Liff43da82013-01-03 16:04:23 +00001450
Richard Cochran28b04112010-07-17 08:48:55 +00001451 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001454static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001455{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001456 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001457 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001458 struct sk_buff *skb;
1459 struct bufdesc *bdp;
1460
1461 bdp = fep->rx_bd_base;
1462 for (i = 0; i < RX_RING_SIZE; i++) {
1463 skb = fep->rx_skbuff[i];
1464
1465 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001466 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001467 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1468 if (skb)
1469 dev_kfree_skb(skb);
Frank Liff43da82013-01-03 16:04:23 +00001470 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001471 }
1472
1473 bdp = fep->tx_bd_base;
1474 for (i = 0; i < TX_RING_SIZE; i++)
1475 kfree(fep->tx_bounce[i]);
1476}
1477
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001478static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001479{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001480 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001481 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001482 struct sk_buff *skb;
1483 struct bufdesc *bdp;
1484
1485 bdp = fep->rx_bd_base;
1486 for (i = 0; i < RX_RING_SIZE; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001487 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001488 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001489 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001490 return -ENOMEM;
1491 }
1492 fep->rx_skbuff[i] = skb;
1493
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001494 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001495 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1496 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00001497
1498 if (fep->bufdesc_ex) {
1499 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1500 ebdp->cbd_esc = BD_ENET_RX_INT;
1501 }
1502
1503 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001504 }
1505
1506 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001507 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001508 bdp->cbd_sc |= BD_SC_WRAP;
1509
1510 bdp = fep->tx_bd_base;
1511 for (i = 0; i < TX_RING_SIZE; i++) {
1512 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1513
1514 bdp->cbd_sc = 0;
1515 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001516
Frank Liff43da82013-01-03 16:04:23 +00001517 if (fep->bufdesc_ex) {
1518 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00001519 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00001520 }
1521
1522 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001523 }
1524
1525 /* Set the last buffer to wrap. */
Frank Liff43da82013-01-03 16:04:23 +00001526 bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001527 bdp->cbd_sc |= BD_SC_WRAP;
1528
1529 return 0;
1530}
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001533fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001535 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001536 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Frank Lidc975382013-01-28 18:31:42 +00001538 napi_enable(&fep->napi);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* I should reset the ring buffers here, but I don't yet know
1541 * a simple way to do that.
1542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001544 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001545 if (ret)
1546 return ret;
1547
Bryan Wu418bd0d2010-05-28 03:40:39 -07001548 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001549 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001550 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001551 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001552 return ret;
1553 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001554 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001555 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001561fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001563 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Sascha Hauer22f6b862009-04-15 01:32:18 +00001565 /* Don't know what to do yet. */
Georg Hofmann3f104c32013-03-14 06:54:09 +00001566 napi_disable(&fep->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001568 netif_stop_queue(ndev);
1569 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001571 if (fep->phy_dev) {
1572 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001573 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001574 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001575
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001576 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/* Set or clear the multicast filter for this adaptor.
1582 * Skeleton taken from sunlance driver.
1583 * The CPM Ethernet implementation allows Multicast as well as individual
1584 * MAC address filtering. Some of the drivers check to make sure it is
1585 * a group multicast address, and discard those that are not. I guess I
1586 * will do the same for now, but just remove the test if you want
1587 * individual filtering as well (do the upper net layers want or support
1588 * this kind of feature?).
1589 */
1590
1591#define HASH_BITS 6 /* #bits in hash */
1592#define CRC32_POLY 0xEDB88320
1593
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001594static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001596 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001597 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001598 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 unsigned char hash;
1600
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001601 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001602 tmp = readl(fep->hwp + FEC_R_CNTRL);
1603 tmp |= 0x8;
1604 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001605 return;
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Sascha Hauer4e831832009-04-15 01:32:19 +00001608 tmp = readl(fep->hwp + FEC_R_CNTRL);
1609 tmp &= ~0x8;
1610 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001611
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001612 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001613 /* Catch all multicast addresses, so set the
1614 * filter to all 1's
1615 */
1616 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1617 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Sascha Hauer4e831832009-04-15 01:32:19 +00001619 return;
1620 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001621
Sascha Hauer4e831832009-04-15 01:32:19 +00001622 /* Clear filter and add the addresses in hash register
1623 */
1624 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1625 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001627 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001628 /* calculate crc32 value of mac address */
1629 crc = 0xffffffff;
1630
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001631 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001632 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001633 for (bit = 0; bit < 8; bit++, data >>= 1) {
1634 crc = (crc >> 1) ^
1635 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001638
1639 /* only upper 6 bits (HASH_BITS) are used
1640 * which point to specific bit in he hash registers
1641 */
1642 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1643
1644 if (hash > 31) {
1645 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1646 tmp |= 1 << (hash - 32);
1647 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1648 } else {
1649 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1650 tmp |= 1 << hash;
1651 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654}
1655
Sascha Hauer22f6b862009-04-15 01:32:18 +00001656/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001657static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001658fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001660 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001661 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Sascha Hauer009fda82009-04-15 01:32:23 +00001663 if (!is_valid_ether_addr(addr->sa_data))
1664 return -EADDRNOTAVAIL;
1665
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001666 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001667
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001668 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1669 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001670 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001671 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001672 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001676#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001677/**
1678 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001679 * @dev: The FEC network adapter
1680 *
1681 * Polled functionality used by netconsole and others in non interrupt mode
1682 *
1683 */
Wei Yongjun47a52472013-03-20 05:06:11 +00001684static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001685{
1686 int i;
1687 struct fec_enet_private *fep = netdev_priv(dev);
1688
1689 for (i = 0; i < FEC_IRQ_NUM; i++) {
1690 if (fep->irq[i] > 0) {
1691 disable_irq(fep->irq[i]);
1692 fec_enet_interrupt(fep->irq[i], dev);
1693 enable_irq(fep->irq[i]);
1694 }
1695 }
1696}
1697#endif
1698
Jim Baxter4c09eed2013-04-19 08:10:49 +00001699static int fec_set_features(struct net_device *netdev,
1700 netdev_features_t features)
1701{
1702 struct fec_enet_private *fep = netdev_priv(netdev);
1703 netdev_features_t changed = features ^ netdev->features;
1704
1705 netdev->features = features;
1706
1707 /* Receive checksum has been changed */
1708 if (changed & NETIF_F_RXCSUM) {
1709 if (features & NETIF_F_RXCSUM)
1710 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1711 else
1712 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
1713
1714 if (netif_running(netdev)) {
1715 fec_stop(netdev);
1716 fec_restart(netdev, fep->phy_dev->duplex);
1717 netif_wake_queue(netdev);
1718 } else {
1719 fec_restart(netdev, fep->phy_dev->duplex);
1720 }
1721 }
1722
1723 return 0;
1724}
1725
Sascha Hauer009fda82009-04-15 01:32:23 +00001726static const struct net_device_ops fec_netdev_ops = {
1727 .ndo_open = fec_enet_open,
1728 .ndo_stop = fec_enet_close,
1729 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001730 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001731 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001732 .ndo_validate_addr = eth_validate_addr,
1733 .ndo_tx_timeout = fec_timeout,
1734 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001735 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001736#ifdef CONFIG_NET_POLL_CONTROLLER
1737 .ndo_poll_controller = fec_poll_controller,
1738#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001739 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00001740};
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001746static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001748 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00001749 const struct platform_device_id *id_entry =
1750 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001751 struct bufdesc *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001753 /* Allocate memory for buffer descriptors. */
1754 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001755 GFP_KERNEL);
1756 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001757 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10001758
Frank Li14109a52013-03-26 16:12:03 +00001759 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10001760
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001761 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Shawn Guo49da97d2011-01-05 21:13:11 +00001763 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001764 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001766 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 fep->rx_bd_base = cbd_base;
Frank Liff43da82013-01-03 16:04:23 +00001768 if (fep->bufdesc_ex)
1769 fep->tx_bd_base = (struct bufdesc *)
1770 (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1771 else
1772 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Sascha Hauer22f6b862009-04-15 01:32:18 +00001774 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001775 ndev->watchdog_timeo = TX_TIMEOUT;
1776 ndev->netdev_ops = &fec_netdev_ops;
1777 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00001778
Frank Lidc975382013-01-28 18:31:42 +00001779 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1780 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1781
Shawn Guo48496252013-05-08 21:08:22 +00001782 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
1783 /* enable hw accelerator */
1784 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1785 | NETIF_F_RXCSUM);
1786 ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
1787 | NETIF_F_RXCSUM);
1788 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1789 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00001790
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001791 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
1794}
1795
Shawn Guoca2cc332011-06-25 02:04:35 +08001796#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05001797static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001798{
1799 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00001800 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08001801 struct device_node *np = pdev->dev.of_node;
1802
1803 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001804 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001805
Shawn Guoa3caad02012-06-27 03:45:24 +00001806 of_property_read_u32(np, "phy-reset-duration", &msec);
1807 /* A sane reset duration should not be longer than 1s */
1808 if (msec > 1000)
1809 msec = 1;
1810
Shawn Guoca2cc332011-06-25 02:04:35 +08001811 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001812 if (!gpio_is_valid(phy_reset))
1813 return;
1814
Shawn Guo119fc002012-06-27 03:45:22 +00001815 err = devm_gpio_request_one(&pdev->dev, phy_reset,
1816 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08001817 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00001818 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00001819 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08001820 }
Shawn Guoa3caad02012-06-27 03:45:24 +00001821 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08001822 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08001823}
1824#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001825static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08001826{
1827 /*
1828 * In case of platform probe, the reset has been done
1829 * by machine code.
1830 */
Shawn Guoca2cc332011-06-25 02:04:35 +08001831}
1832#endif /* CONFIG_OF */
1833
Bill Pemberton33897cc2012-12-03 09:23:58 -05001834static int
Sascha Haueread73182009-01-28 23:03:11 +00001835fec_probe(struct platform_device *pdev)
1836{
1837 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07001838 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00001839 struct net_device *ndev;
1840 int i, irq, ret = 0;
1841 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08001842 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001843 static int dev_id;
Shawn Guob2bccee2012-05-06 20:24:04 +08001844 struct pinctrl *pinctrl;
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001845 struct regulator *reg_phy;
Shawn Guoca2cc332011-06-25 02:04:35 +08001846
1847 of_id = of_match_device(fec_dt_ids, &pdev->dev);
1848 if (of_id)
1849 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00001850
1851 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1852 if (!r)
1853 return -ENXIO;
1854
Sascha Haueread73182009-01-28 23:03:11 +00001855 /* Init network device */
1856 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00001857 if (!ndev)
1858 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00001859
1860 SET_NETDEV_DEV(ndev, &pdev->dev);
1861
1862 /* setup board info structure */
1863 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001864
Frank Libaa70a52013-01-16 16:55:58 +00001865 /* default enable pause frame auto negotiation */
1866 if (pdev->id_entry &&
1867 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
1868 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
1869
Fabio Estevam83e519b2013-03-11 07:32:55 +00001870 fep->hwp = devm_request_and_ioremap(&pdev->dev, r);
Bryan Wue6b043d2010-03-31 02:10:44 +00001871 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00001872 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00001873
Frank Liff43da82013-01-03 16:04:23 +00001874 fep->bufdesc_ex = 0;
1875
Uwe Kleine-König24e531b2011-01-13 22:29:05 +01001876 if (!fep->hwp) {
Sascha Haueread73182009-01-28 23:03:11 +00001877 ret = -ENOMEM;
1878 goto failed_ioremap;
1879 }
1880
1881 platform_set_drvdata(pdev, ndev);
1882
Guenter Roeck6c5f7802013-04-02 09:35:10 +00001883 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08001884 if (ret < 0) {
1885 pdata = pdev->dev.platform_data;
1886 if (pdata)
1887 fep->phy_interface = pdata->phy;
1888 else
1889 fep->phy_interface = PHY_INTERFACE_MODE_MII;
1890 } else {
1891 fep->phy_interface = ret;
1892 }
1893
Fabio Estevame2f8d552013-02-22 06:40:45 +00001894 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1895 if (IS_ERR(pinctrl)) {
1896 ret = PTR_ERR(pinctrl);
1897 goto failed_pin;
1898 }
1899
1900 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1901 if (IS_ERR(fep->clk_ipg)) {
1902 ret = PTR_ERR(fep->clk_ipg);
1903 goto failed_clk;
1904 }
1905
1906 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1907 if (IS_ERR(fep->clk_ahb)) {
1908 ret = PTR_ERR(fep->clk_ahb);
1909 goto failed_clk;
1910 }
1911
Linus Torvalds38f56f32013-05-07 11:06:17 -07001912 /* enet_out is optional, depends on board */
1913 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
1914 if (IS_ERR(fep->clk_enet_out))
1915 fep->clk_enet_out = NULL;
1916
Fabio Estevame2f8d552013-02-22 06:40:45 +00001917 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001918 fep->bufdesc_ex =
1919 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00001920 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07001921 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00001922 fep->bufdesc_ex = 0;
1923 }
1924
1925 clk_prepare_enable(fep->clk_ahb);
1926 clk_prepare_enable(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07001927 clk_prepare_enable(fep->clk_enet_out);
1928 clk_prepare_enable(fep->clk_ptp);
Fabio Estevame2f8d552013-02-22 06:40:45 +00001929
1930 reg_phy = devm_regulator_get(&pdev->dev, "phy");
1931 if (!IS_ERR(reg_phy)) {
1932 ret = regulator_enable(reg_phy);
1933 if (ret) {
1934 dev_err(&pdev->dev,
1935 "Failed to enable phy regulator: %d\n", ret);
1936 goto failed_regulator;
1937 }
1938 }
1939
1940 fec_reset_phy(pdev);
1941
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00001942 if (fep->bufdesc_ex)
1943 fec_ptp_init(ndev, pdev);
1944
1945 ret = fec_enet_init(ndev);
1946 if (ret)
1947 goto failed_init;
1948
Xiao Jiangc7c83d12011-09-29 02:15:56 +00001949 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00001950 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00001951 if (irq < 0) {
1952 if (i)
1953 break;
1954 ret = irq;
1955 goto failed_irq;
1956 }
Sascha Haueread73182009-01-28 23:03:11 +00001957 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1958 if (ret) {
Uwe Kleine-Königb2b09ad2011-01-13 21:49:05 +01001959 while (--i >= 0) {
Sascha Haueread73182009-01-28 23:03:11 +00001960 irq = platform_get_irq(pdev, i);
1961 free_irq(irq, ndev);
Sascha Haueread73182009-01-28 23:03:11 +00001962 }
1963 goto failed_irq;
1964 }
1965 }
1966
Bryan Wue6b043d2010-03-31 02:10:44 +00001967 ret = fec_enet_mii_init(pdev);
1968 if (ret)
1969 goto failed_mii_init;
1970
Oskar Schirmer03c698c2010-10-07 02:30:30 +00001971 /* Carrier starts down, phylib will bring it up */
1972 netif_carrier_off(ndev);
1973
Sascha Haueread73182009-01-28 23:03:11 +00001974 ret = register_netdev(ndev);
1975 if (ret)
1976 goto failed_register;
1977
Fabio Estevameb1d0642013-04-13 07:25:36 +00001978 if (fep->bufdesc_ex && fep->ptp_clock)
1979 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
1980
Frank Li54309fa2013-05-07 14:08:44 +00001981 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00001982 return 0;
1983
1984failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00001985 fec_enet_mii_remove(fep);
1986failed_mii_init:
Fabio Estevame2f8d552013-02-22 06:40:45 +00001987failed_init:
1988 for (i = 0; i < FEC_IRQ_NUM; i++) {
1989 irq = platform_get_irq(pdev, i);
1990 if (irq > 0)
1991 free_irq(irq, ndev);
1992 }
1993failed_irq:
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00001994failed_regulator:
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001995 clk_disable_unprepare(fep->clk_ahb);
1996 clk_disable_unprepare(fep->clk_ipg);
Linus Torvalds38f56f32013-05-07 11:06:17 -07001997 clk_disable_unprepare(fep->clk_enet_out);
1998 clk_disable_unprepare(fep->clk_ptp);
Shawn Guob2bccee2012-05-06 20:24:04 +08001999failed_pin:
Sascha Haueread73182009-01-28 23:03:11 +00002000failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002001failed_ioremap:
2002 free_netdev(ndev);
2003
2004 return ret;
2005}
2006
Bill Pemberton33897cc2012-12-03 09:23:58 -05002007static int
Sascha Haueread73182009-01-28 23:03:11 +00002008fec_drv_remove(struct platform_device *pdev)
2009{
2010 struct net_device *ndev = platform_get_drvdata(pdev);
2011 struct fec_enet_private *fep = netdev_priv(ndev);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002012 int i;
Sascha Haueread73182009-01-28 23:03:11 +00002013
Frank Li54309fa2013-05-07 14:08:44 +00002014 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002015 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002016 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002017 del_timer_sync(&fep->time_keep);
2018 clk_disable_unprepare(fep->clk_ptp);
2019 if (fep->ptp_clock)
2020 ptp_clock_unregister(fep->ptp_clock);
Linus Torvalds38f56f32013-05-07 11:06:17 -07002021 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002022 clk_disable_unprepare(fep->clk_ahb);
2023 clk_disable_unprepare(fep->clk_ipg);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002024 for (i = 0; i < FEC_IRQ_NUM; i++) {
2025 int irq = platform_get_irq(pdev, i);
2026 if (irq > 0)
2027 free_irq(irq, ndev);
2028 }
Sascha Haueread73182009-01-28 23:03:11 +00002029 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002030
Uwe Kleine-Königb3cde362011-01-25 18:03:37 +01002031 platform_set_drvdata(pdev, NULL);
2032
Sascha Haueread73182009-01-28 23:03:11 +00002033 return 0;
2034}
2035
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002036#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002037static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002038fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002039{
Eric Benard87cad5c2010-06-18 04:19:54 +00002040 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002041 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002042
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002043 if (netif_running(ndev)) {
2044 fec_stop(ndev);
2045 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002046 }
Linus Torvalds38f56f32013-05-07 11:06:17 -07002047 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002048 clk_disable_unprepare(fep->clk_ahb);
2049 clk_disable_unprepare(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002050
Sascha Haueread73182009-01-28 23:03:11 +00002051 return 0;
2052}
2053
2054static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002055fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002056{
Eric Benard87cad5c2010-06-18 04:19:54 +00002057 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002058 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002059
Linus Torvalds38f56f32013-05-07 11:06:17 -07002060 clk_prepare_enable(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002061 clk_prepare_enable(fep->clk_ahb);
2062 clk_prepare_enable(fep->clk_ipg);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002063 if (netif_running(ndev)) {
2064 fec_restart(ndev, fep->full_duplex);
2065 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002066 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002067
Sascha Haueread73182009-01-28 23:03:11 +00002068 return 0;
2069}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002070#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002071
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002072static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002073
Sascha Haueread73182009-01-28 23:03:11 +00002074static struct platform_driver fec_driver = {
2075 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002076 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002077 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002078 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002079 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002080 },
Shawn Guob5680e02011-01-05 21:13:13 +00002081 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002082 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002083 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002084};
2085
Fabio Estevamaaca2372012-01-23 16:44:50 +00002086module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088MODULE_LICENSE("GPL");