blob: 50bb71c663e20a010c9a4390c3d1c38726037c85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10005 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
10 * small packets.
11 *
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
14 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100015 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100017 *
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100019 * Copyright (c) 2004-2006 Macq Electronique SA.
Shawn Guob5680e02011-01-05 21:13:13 +000020 *
Shawn Guo230dec62011-09-23 02:12:48 +000021 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/string.h>
27#include <linux/ptrace.h>
28#include <linux/errno.h>
29#include <linux/ioport.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000037#include <linux/in.h>
38#include <linux/ip.h>
39#include <net/ip.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
44#include <linux/workqueue.h>
45#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000046#include <linux/io.h>
47#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000048#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000049#include <linux/platform_device.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000050#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070051#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080052#include <linux/of.h>
53#include <linux/of_device.h>
54#include <linux/of_gpio.h>
55#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000056#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010057#include <linux/if_vlan.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
Christoph Müllner772e42b2013-06-27 21:18:23 +020063static void set_multicast_list(struct net_device *ndev);
64
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010065#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000066#define FEC_ALIGNMENT 0xf
67#else
68#define FEC_ALIGNMENT 0x3
69#endif
70
Shawn Guob5680e02011-01-05 21:13:13 +000071#define DRIVER_NAME "fec"
72
Frank Libaa70a52013-01-16 16:55:58 +000073/* Pause frame feild and FIFO threshold */
74#define FEC_ENET_FCE (1 << 5)
75#define FEC_ENET_RSEM_V 0x84
76#define FEC_ENET_RSFL_V 16
77#define FEC_ENET_RAEM_V 0x8
78#define FEC_ENET_RAFL_V 0x8
79#define FEC_ENET_OPD_V 0xFFF0
80
Shawn Guob5680e02011-01-05 21:13:13 +000081/* Controller is ENET-MAC */
82#define FEC_QUIRK_ENET_MAC (1 << 0)
83/* Controller needs driver to swap frame */
84#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080085/* Controller uses gasket */
86#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000087/* Controller has GBIT support */
88#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000089/* Controller has extend desc buffer */
90#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000091/* Controller has hardware checksum support */
92#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010093/* Controller has hardware vlan support */
94#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080095/* ENET IP errata ERR006358
96 *
97 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
98 * detected as not set during a prior frame transmission, then the
99 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
100 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +0800101 * frames not being transmitted until there is a 0-to-1 transition on
102 * ENET_TDAR[TDAR].
103 */
104#define FEC_QUIRK_ERR006358 (1 << 7)
Shawn Guob5680e02011-01-05 21:13:13 +0000105
106static struct platform_device_id fec_devtype[] = {
107 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800108 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000109 .name = DRIVER_NAME,
110 .driver_data = 0,
111 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800112 .name = "imx25-fec",
113 .driver_data = FEC_QUIRK_USE_GASKET,
114 }, {
115 .name = "imx27-fec",
116 .driver_data = 0,
117 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000118 .name = "imx28-fec",
119 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800120 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000121 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000122 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100123 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800124 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000125 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000126 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000127 .driver_data = FEC_QUIRK_ENET_MAC,
128 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800129 /* sentinel */
130 }
Shawn Guob5680e02011-01-05 21:13:13 +0000131};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800132MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000133
Shawn Guoca2cc332011-06-25 02:04:35 +0800134enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000135 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800136 IMX27_FEC, /* runs on i.mx27/35/51 */
137 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000138 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000139 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800140};
141
142static const struct of_device_id fec_dt_ids[] = {
143 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
144 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
145 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000146 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000147 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800148 { /* sentinel */ }
149};
150MODULE_DEVICE_TABLE(of, fec_dt_ids);
151
Shawn Guo49da97d2011-01-05 21:13:11 +0000152static unsigned char macaddr[ETH_ALEN];
153module_param_array(macaddr, byte, NULL, 0);
154MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
155
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000156#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * Some hardware gets it MAC address out of local flash memory.
159 * if this is non-zero then assume it is the address to get MAC from.
160 */
161#if defined(CONFIG_NETtel)
162#define FEC_FLASHMAC 0xf0006006
163#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
164#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#elif defined(CONFIG_CANCam)
166#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000167#elif defined (CONFIG_M5272C3)
168#define FEC_FLASHMAC (0xffe04000 + 4)
169#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000170#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#else
172#define FEC_FLASHMAC 0
173#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800174#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000175
Frank Liff43da82013-01-03 16:04:23 +0000176#if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
Matt Waddel6b265292006-06-27 13:10:56 +1000177#error "FEC: descriptor ring size constants too large"
Greg Ungerer562d2f82005-11-07 14:09:50 +1000178#endif
179
Sascha Hauer22f6b862009-04-15 01:32:18 +0000180/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
182#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
183#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
184#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
185#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
186#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
187#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
188#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
189#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
190#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
191
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000192#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000193#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000194
Jim Baxtercdffcf12013-07-02 22:52:56 +0100195/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100197#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100199#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Jim Baxter4c09eed2013-04-19 08:10:49 +0000201/* FEC receive acceleration */
202#define FEC_RACC_IPDIS (1 << 1)
203#define FEC_RACC_PRODIS (1 << 2)
204#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
Matt Waddel6b265292006-06-27 13:10:56 +1000207 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * size bits. Other FEC hardware does not, so we need to take that into
209 * account when setting it.
210 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000211#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100212 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
214#else
215#define OPT_FRAME_SIZE 0
216#endif
217
Bryan Wue6b043d2010-03-31 02:10:44 +0000218/* FEC MII MMFR bits definition */
219#define FEC_MMFR_ST (1 << 30)
220#define FEC_MMFR_OP_READ (2 << 28)
221#define FEC_MMFR_OP_WRITE (1 << 28)
222#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
223#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
224#define FEC_MMFR_TA (2 << 16)
225#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500227#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Sascha Hauer22f6b862009-04-15 01:32:18 +0000229/* Transmitter timeout */
230#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Frank Libaa70a52013-01-16 16:55:58 +0000232#define FEC_PAUSE_FLAG_AUTONEG 0x1
233#define FEC_PAUSE_FLAG_ENABLE 0x2
234
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000235static int mii_cnt;
236
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800237static inline
238struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000239{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800240 struct bufdesc *new_bd = bdp + 1;
241 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
242 struct bufdesc_ex *ex_base;
243 struct bufdesc *base;
244 int ring_size;
245
246 if (bdp >= fep->tx_bd_base) {
247 base = fep->tx_bd_base;
248 ring_size = fep->tx_ring_size;
249 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
250 } else {
251 base = fep->rx_bd_base;
252 ring_size = fep->rx_ring_size;
253 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
254 }
255
256 if (fep->bufdesc_ex)
257 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
258 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000259 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800260 return (new_bd >= (base + ring_size)) ?
261 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000262}
263
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800264static inline
265struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000266{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800267 struct bufdesc *new_bd = bdp - 1;
268 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
269 struct bufdesc_ex *ex_base;
270 struct bufdesc *base;
271 int ring_size;
272
273 if (bdp >= fep->tx_bd_base) {
274 base = fep->tx_bd_base;
275 ring_size = fep->tx_ring_size;
276 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
277 } else {
278 base = fep->rx_bd_base;
279 ring_size = fep->rx_ring_size;
280 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
281 }
282
283 if (fep->bufdesc_ex)
284 return (struct bufdesc *)((ex_new_bd < ex_base) ?
285 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000286 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800287 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000288}
289
Shawn Guob5680e02011-01-05 21:13:13 +0000290static void *swap_buffer(void *bufaddr, int len)
291{
292 int i;
293 unsigned int *buf = bufaddr;
294
Fabio Estevamffed61e2013-05-21 05:44:26 +0000295 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000296 *buf = cpu_to_be32(*buf);
297
298 return bufaddr;
299}
300
Jim Baxter4c09eed2013-04-19 08:10:49 +0000301static int
302fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
303{
304 /* Only run for packets requiring a checksum. */
305 if (skb->ip_summed != CHECKSUM_PARTIAL)
306 return 0;
307
308 if (unlikely(skb_cow_head(skb, 0)))
309 return -1;
310
311 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
312
313 return 0;
314}
315
Denis Kirjanovc7621cb2010-06-02 09:15:47 +0000316static netdev_tx_t
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100317fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100319 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000320 const struct platform_device_id *id_entry =
321 platform_get_device_id(fep->pdev);
Frank Li03191652013-07-25 14:05:53 +0800322 struct bufdesc *bdp, *bdp_pre;
Greg Ungerer9555b312009-08-06 17:58:18 +0000323 void *bufaddr;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000324 unsigned short status;
Frank Lide5fb0a2013-03-03 17:34:25 +0000325 unsigned int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Fill in a Tx ring entry */
328 bdp = fep->cur_tx;
329
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000330 status = bdp->cbd_sc;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000331
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000332 if (status & BD_ENET_TX_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Ooops. All transmit buffers are full. Bail out.
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100334 * This should not happen, since ndev->tbusy should be set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Joe Perches31b77202013-04-13 19:03:17 +0000336 netdev_err(ndev, "tx queue full!\n");
Patrick McHardy5b548142009-06-12 06:22:29 +0000337 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jim Baxter4c09eed2013-04-19 08:10:49 +0000340 /* Protocol checksum off-load for TCP and UDP. */
341 if (fec_enet_clear_csum(skb, ndev)) {
342 kfree_skb(skb);
343 return NETDEV_TX_OK;
344 }
345
Sascha Hauer22f6b862009-04-15 01:32:18 +0000346 /* Clear all of the status flags */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000347 status &= ~BD_ENET_TX_STATS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Sascha Hauer22f6b862009-04-15 01:32:18 +0000349 /* Set buffer length and buffer pointer */
Greg Ungerer9555b312009-08-06 17:58:18 +0000350 bufaddr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 bdp->cbd_datlen = skb->len;
352
353 /*
Sascha Hauer22f6b862009-04-15 01:32:18 +0000354 * On some FEC implementations data must be aligned on
355 * 4-byte boundaries. Use bounce buffers to copy data
356 * and get it aligned. Ugh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000358 if (fep->bufdesc_ex)
359 index = (struct bufdesc_ex *)bdp -
360 (struct bufdesc_ex *)fep->tx_bd_base;
361 else
362 index = bdp - fep->tx_bd_base;
363
Greg Ungerer9555b312009-08-06 17:58:18 +0000364 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
Uwe Kleine-König8a73b0b2011-01-13 21:34:31 +0100365 memcpy(fep->tx_bounce[index], skb->data, skb->len);
Greg Ungerer9555b312009-08-06 17:58:18 +0000366 bufaddr = fep->tx_bounce[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Shawn Guob5680e02011-01-05 21:13:13 +0000369 /*
370 * Some design made an incorrect assumption on endian mode of
371 * the system that it's running on. As the result, driver has to
372 * swap every frame going to and coming from the controller.
373 */
374 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
375 swap_buffer(bufaddr, skb->len);
376
Sascha Hauer22f6b862009-04-15 01:32:18 +0000377 /* Save skb pointer */
Frank Lide5fb0a2013-03-03 17:34:25 +0000378 fep->tx_skbuff[index] = skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Push the data cache so the CPM does not get stale memory
381 * data.
382 */
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +0100383 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
Sebastian Siewior2488a542013-12-02 10:52:55 +0100384 skb->len, DMA_TO_DEVICE);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +0800385 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
386 bdp->cbd_bufaddr = 0;
387 fep->tx_skbuff[index] = NULL;
388 dev_kfree_skb_any(skb);
389 if (net_ratelimit())
390 netdev_err(ndev, "Tx DMA memory map failed\n");
391 return NETDEV_TX_OK;
392 }
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000393 /* Send it on its way. Tell FEC it's ready, interrupt when done,
394 * it's the last BD of the frame, and to put the CRC on the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000396 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000398 bdp->cbd_sc = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Frank Liff43da82013-01-03 16:04:23 +0000400 if (fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000401
Frank Liff43da82013-01-03 16:04:23 +0000402 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
403 ebdp->cbd_bdu = 0;
404 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
405 fep->hwts_tx_en)) {
406 ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
407 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
408 } else {
Frank Liff43da82013-01-03 16:04:23 +0000409 ebdp->cbd_esc = BD_ENET_TX_INT;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000410
411 /* Enable protocol checksum flags
412 * We do not bother with the IP Checksum bits as they
413 * are done by the kernel
414 */
415 if (skb->ip_summed == CHECKSUM_PARTIAL)
416 ebdp->cbd_esc |= BD_ENET_TX_PINS;
Frank Liff43da82013-01-03 16:04:23 +0000417 }
Frank Li6605b732012-10-30 18:25:31 +0000418 }
Frank Li03191652013-07-25 14:05:53 +0800419
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800420 bdp_pre = fec_enet_get_prevdesc(bdp, fep);
Frank Li03191652013-07-25 14:05:53 +0800421 if ((id_entry->driver_data & FEC_QUIRK_ERR006358) &&
422 !(bdp_pre->cbd_sc & BD_ENET_TX_READY)) {
423 fep->delay_work.trig_tx = true;
424 schedule_delayed_work(&(fep->delay_work.delay_work),
425 msecs_to_jiffies(1));
426 }
427
Sascha Hauer22f6b862009-04-15 01:32:18 +0000428 /* If this was the last BD in the ring, start at the beginning again. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800429 bdp = fec_enet_get_nextdesc(bdp, fep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800431 skb_tx_timestamp(skb);
432
Sascha Hauer2e285322009-04-15 01:32:16 +0000433 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Frank Lide5fb0a2013-03-03 17:34:25 +0000435 if (fep->cur_tx == fep->dirty_tx)
436 netif_stop_queue(ndev);
Richard Cochran18a03b92011-06-12 02:18:59 +0000437
Frank Lide5fb0a2013-03-03 17:34:25 +0000438 /* Trigger transmission start */
439 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
440
Patrick McHardy6ed10652009-06-23 06:03:08 +0000441 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Frank Li14109a52013-03-26 16:12:03 +0000444/* Init RX & TX buffer descriptors
445 */
446static void fec_enet_bd_init(struct net_device *dev)
447{
448 struct fec_enet_private *fep = netdev_priv(dev);
449 struct bufdesc *bdp;
450 unsigned int i;
451
452 /* Initialize the receive buffer descriptors. */
453 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800454 for (i = 0; i < fep->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000455
456 /* Initialize the BD for every fragment in the page. */
457 if (bdp->cbd_bufaddr)
458 bdp->cbd_sc = BD_ENET_RX_EMPTY;
459 else
460 bdp->cbd_sc = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800461 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000462 }
463
464 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800465 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000466 bdp->cbd_sc |= BD_SC_WRAP;
467
468 fep->cur_rx = fep->rx_bd_base;
469
470 /* ...and the same for transmit */
471 bdp = fep->tx_bd_base;
472 fep->cur_tx = bdp;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800473 for (i = 0; i < fep->tx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000474
475 /* Initialize the BD for every fragment in the page. */
476 bdp->cbd_sc = 0;
477 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
478 dev_kfree_skb_any(fep->tx_skbuff[i]);
479 fep->tx_skbuff[i] = NULL;
480 }
481 bdp->cbd_bufaddr = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800482 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000483 }
484
485 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800486 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000487 bdp->cbd_sc |= BD_SC_WRAP;
488 fep->dirty_tx = bdp;
489}
490
Uwe Kleine-König45993652011-01-19 20:47:04 +0100491/* This function is called to start or restart the FEC during a link
492 * change. This only happens when switching between half and full
493 * duplex.
494 */
495static void
496fec_restart(struct net_device *ndev, int duplex)
497{
498 struct fec_enet_private *fep = netdev_priv(ndev);
499 const struct platform_device_id *id_entry =
500 platform_get_device_id(fep->pdev);
501 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000502 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100503 u32 temp_mac[2];
504 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000505 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100506
Frank Li54309fa2013-05-07 14:08:44 +0000507 if (netif_running(ndev)) {
508 netif_device_detach(ndev);
509 napi_disable(&fep->napi);
510 netif_stop_queue(ndev);
Fabio Estevam31691342013-05-15 07:06:26 +0000511 netif_tx_lock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000512 }
513
Uwe Kleine-König45993652011-01-19 20:47:04 +0100514 /* Whack a reset. We should wait for this. */
515 writel(1, fep->hwp + FEC_ECNTRL);
516 udelay(10);
517
518 /*
519 * enet-mac reset will reset mac address registers too,
520 * so need to reconfigure it.
521 */
522 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
523 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
524 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
525 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
526 }
527
528 /* Clear any outstanding interrupt. */
529 writel(0xffc00000, fep->hwp + FEC_IEVENT);
530
Christoph Müllner772e42b2013-06-27 21:18:23 +0200531 /* Setup multicast filter. */
532 set_multicast_list(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100533#ifndef CONFIG_M5272
534 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
535 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
536#endif
537
538 /* Set maximum receive buffer size. */
539 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
540
Frank Li14109a52013-03-26 16:12:03 +0000541 fec_enet_bd_init(ndev);
542
Uwe Kleine-König45993652011-01-19 20:47:04 +0100543 /* Set receive and transmit descriptor base. */
544 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000545 if (fep->bufdesc_ex)
546 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800547 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000548 else
549 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800550 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100551
Uwe Kleine-König45993652011-01-19 20:47:04 +0100552
Uwe Kleine-König45993652011-01-19 20:47:04 +0100553 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
554 if (fep->tx_skbuff[i]) {
555 dev_kfree_skb_any(fep->tx_skbuff[i]);
556 fep->tx_skbuff[i] = NULL;
557 }
558 }
559
560 /* Enable MII mode */
561 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100562 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100563 writel(0x04, fep->hwp + FEC_X_CNTRL);
564 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100565 /* No Rcv on Xmit */
566 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100567 writel(0x0, fep->hwp + FEC_X_CNTRL);
568 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100569
Uwe Kleine-König45993652011-01-19 20:47:04 +0100570 fep->full_duplex = duplex;
571
572 /* Set MII speed */
573 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
574
Guenter Roeckd1391932013-06-18 10:04:59 -0700575#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000576 /* set RX checksum */
577 val = readl(fep->hwp + FEC_RACC);
578 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
579 val |= FEC_RACC_OPTIONS;
580 else
581 val &= ~FEC_RACC_OPTIONS;
582 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700583#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000584
Uwe Kleine-König45993652011-01-19 20:47:04 +0100585 /*
586 * The phy interface and speed need to get configured
587 * differently on enet-mac.
588 */
589 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100590 /* Enable flow control and length check */
591 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100592
Shawn Guo230dec62011-09-23 02:12:48 +0000593 /* RGMII, RMII or MII */
594 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
595 rcntl |= (1 << 6);
596 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100597 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100598 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100599 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100600
Shawn Guo230dec62011-09-23 02:12:48 +0000601 /* 1G, 100M or 10M */
602 if (fep->phy_dev) {
603 if (fep->phy_dev->speed == SPEED_1000)
604 ecntl |= (1 << 5);
605 else if (fep->phy_dev->speed == SPEED_100)
606 rcntl &= ~(1 << 9);
607 else
608 rcntl |= (1 << 9);
609 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100610 } else {
611#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800612 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000613 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100614 /* disable the gasket and wait */
615 writel(0, fep->hwp + FEC_MIIGSK_ENR);
616 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
617 udelay(1);
618
619 /*
620 * configure the gasket:
621 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800622 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100623 */
Eric Benard8d82f212012-01-12 06:10:28 +0000624 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
625 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
626 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
627 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
628 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100629
630 /* re-enable the gasket */
631 writel(2, fep->hwp + FEC_MIIGSK_ENR);
632 }
633#endif
634 }
Frank Libaa70a52013-01-16 16:55:58 +0000635
Guenter Roeckd1391932013-06-18 10:04:59 -0700636#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000637 /* enable pause frame*/
638 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
639 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
640 fep->phy_dev && fep->phy_dev->pause)) {
641 rcntl |= FEC_ENET_FCE;
642
Jim Baxter4c09eed2013-04-19 08:10:49 +0000643 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000644 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
645 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
646 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
647 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
648
649 /* OPD */
650 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
651 } else {
652 rcntl &= ~FEC_ENET_FCE;
653 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700654#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000655
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100656 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100657
Shawn Guo230dec62011-09-23 02:12:48 +0000658 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
659 /* enable ENET endian swap */
660 ecntl |= (1 << 8);
661 /* enable ENET store and forward mode */
662 writel(1 << 8, fep->hwp + FEC_X_WMRK);
663 }
664
Frank Liff43da82013-01-03 16:04:23 +0000665 if (fep->bufdesc_ex)
666 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000667
Chris Healy38ae92d2013-06-25 23:18:52 -0700668#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +0100669 /* Enable the MIB statistic event counters */
670 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -0700671#endif
672
Uwe Kleine-König45993652011-01-19 20:47:04 +0100673 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000674 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100675 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
676
Frank Liff43da82013-01-03 16:04:23 +0000677 if (fep->bufdesc_ex)
678 fec_ptp_start_cyclecounter(ndev);
679
Uwe Kleine-König45993652011-01-19 20:47:04 +0100680 /* Enable interrupts we wish to service */
681 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Frank Li54309fa2013-05-07 14:08:44 +0000682
683 if (netif_running(ndev)) {
Fabio Estevam31691342013-05-15 07:06:26 +0000684 netif_tx_unlock_bh(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000685 netif_wake_queue(ndev);
Fabio Estevam1ed0d562013-05-15 07:06:27 +0000686 napi_enable(&fep->napi);
687 netif_device_attach(ndev);
Frank Li54309fa2013-05-07 14:08:44 +0000688 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100689}
690
691static void
692fec_stop(struct net_device *ndev)
693{
694 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000695 const struct platform_device_id *id_entry =
696 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000697 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100698
699 /* We cannot expect a graceful transmit stop without link !!! */
700 if (fep->link) {
701 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
702 udelay(10);
703 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000704 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +0100705 }
706
707 /* Whack a reset. We should wait for this. */
708 writel(1, fep->hwp + FEC_ECNTRL);
709 udelay(10);
710 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
711 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +0000712
713 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000714 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +0000715 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000716 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
717 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100718}
719
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100722fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100724 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100726 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Frank Li54309fa2013-05-07 14:08:44 +0000728 fep->delay_work.timeout = true;
729 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
730}
731
732static void fec_enet_work(struct work_struct *work)
733{
734 struct fec_enet_private *fep =
735 container_of(work,
736 struct fec_enet_private,
737 delay_work.delay_work.work);
738
739 if (fep->delay_work.timeout) {
740 fep->delay_work.timeout = false;
741 fec_restart(fep->netdev, fep->full_duplex);
742 netif_wake_queue(fep->netdev);
743 }
Frank Li03191652013-07-25 14:05:53 +0800744
745 if (fep->delay_work.trig_tx) {
746 fep->delay_work.trig_tx = false;
747 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100752fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +0000755 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000756 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +0000758 int index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100760 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 bdp = fep->dirty_tx;
762
Frank Lide5fb0a2013-03-03 17:34:25 +0000763 /* get next bdp of dirty_tx */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800764 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +0000765
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000766 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +0000767
768 /* current queue is empty */
769 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +0000770 break;
771
Frank Lide5fb0a2013-03-03 17:34:25 +0000772 if (fep->bufdesc_ex)
773 index = (struct bufdesc_ex *)bdp -
774 (struct bufdesc_ex *)fep->tx_bd_base;
775 else
776 index = bdp - fep->tx_bd_base;
777
Frank Lide5fb0a2013-03-03 17:34:25 +0000778 skb = fep->tx_skbuff[index];
Sebastian Siewior2488a542013-12-02 10:52:55 +0100779 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, skb->len,
780 DMA_TO_DEVICE);
781 bdp->cbd_bufaddr = 0;
Frank Lide5fb0a2013-03-03 17:34:25 +0000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000784 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 BD_ENET_TX_RL | BD_ENET_TX_UN |
786 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100787 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000788 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100789 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000790 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100791 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000792 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100793 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000794 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100795 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000796 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100797 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100799 ndev->stats.tx_packets++;
Jim Baxter06efce72013-06-27 19:25:08 +0100800 ndev->stats.tx_bytes += bdp->cbd_datlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Frank Liff43da82013-01-03 16:04:23 +0000803 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
804 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000805 struct skb_shared_hwtstamps shhwtstamps;
806 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +0000807 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +0000808
809 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
810 spin_lock_irqsave(&fep->tmreg_lock, flags);
811 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000812 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000813 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
814 skb_tstamp_tx(skb, &shhwtstamps);
815 }
Frank Liff43da82013-01-03 16:04:23 +0000816
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000817 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +0000818 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +0000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* Deferred means some collisions occurred during transmit,
821 * but we eventually sent the packet OK.
822 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000823 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100824 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400825
Sascha Hauer22f6b862009-04-15 01:32:18 +0000826 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +0000828 fep->tx_skbuff[index] = NULL;
829
830 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400831
Sascha Hauer22f6b862009-04-15 01:32:18 +0000832 /* Update pointer to next buffer descriptor to be transmitted */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800833 bdp = fec_enet_get_nextdesc(bdp, fep);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400834
Sascha Hauer22f6b862009-04-15 01:32:18 +0000835 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Frank Lide5fb0a2013-03-03 17:34:25 +0000837 if (fep->dirty_tx != fep->cur_tx) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100838 if (netif_queue_stopped(ndev))
839 netif_wake_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 }
Frank Lide5fb0a2013-03-03 17:34:25 +0000842 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845
846/* During a receive, the cur_rx points to the current incoming buffer.
847 * When we update through the ring, if the next incoming buffer has
848 * not been given to the system, we just set the empty indicator,
849 * effectively tossing the packet.
850 */
Frank Lidc975382013-01-28 18:31:42 +0000851static int
852fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100854 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +0000855 const struct platform_device_id *id_entry =
856 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +0000857 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000858 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct sk_buff *skb;
860 ushort pkt_len;
861 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +0000862 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +0100863 struct bufdesc_ex *ebdp = NULL;
864 bool vlan_packet_rcvd = false;
865 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +0800866 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400867
Greg Ungerer0e702ab2006-06-27 13:19:33 +1000868#ifdef CONFIG_M532x
869 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400870#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* First, grab all of the stats for the incoming packet.
873 * These get messed up if we get called due to a busy condition.
874 */
875 bdp = fep->cur_rx;
876
Sascha Hauer22f6b862009-04-15 01:32:18 +0000877 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Frank Lidc975382013-01-28 18:31:42 +0000879 if (pkt_received >= budget)
880 break;
881 pkt_received++;
882
Sascha Hauer22f6b862009-04-15 01:32:18 +0000883 /* Since we have allocated space to hold a complete frame,
884 * the last indicator should be set.
885 */
886 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +0000887 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Sascha Hauer22f6b862009-04-15 01:32:18 +0000889 if (!fep->opened)
890 goto rx_processing_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Sascha Hauer22f6b862009-04-15 01:32:18 +0000892 /* Check for errors. */
893 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100895 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000896 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
897 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100898 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000899 }
900 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100901 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000902 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100903 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000904 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100905 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Sascha Hauer22f6b862009-04-15 01:32:18 +0000907
908 /* Report late collisions as a frame error.
909 * On this error, the BD is closed, but we don't know what we
910 * have in the buffer. So, just drop this frame on the floor.
911 */
912 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100913 ndev->stats.rx_errors++;
914 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000915 goto rx_processing_done;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Sascha Hauer22f6b862009-04-15 01:32:18 +0000918 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100919 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000920 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100921 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Duan Fugang-B38611d842a312013-11-14 09:57:10 +0800923 if (fep->bufdesc_ex)
924 index = (struct bufdesc_ex *)bdp -
925 (struct bufdesc_ex *)fep->rx_bd_base;
926 else
927 index = bdp - fep->rx_bd_base;
928 data = fep->rx_skbuff[index]->data;
929 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
930 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +0000931
Shawn Guob5680e02011-01-05 21:13:13 +0000932 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
933 swap_buffer(data, pkt_len);
934
Jim Baxtercdffcf12013-07-02 22:52:56 +0100935 /* Extract the enhanced buffer descriptor */
936 ebdp = NULL;
937 if (fep->bufdesc_ex)
938 ebdp = (struct bufdesc_ex *)bdp;
939
940 /* If this is a VLAN packet remove the VLAN Tag */
941 vlan_packet_rcvd = false;
942 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
943 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
944 /* Push and remove the vlan tag */
945 struct vlan_hdr *vlan_header =
946 (struct vlan_hdr *) (data + ETH_HLEN);
947 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
948 pkt_len -= VLAN_HLEN;
949
950 vlan_packet_rcvd = true;
951 }
952
Sascha Hauer22f6b862009-04-15 01:32:18 +0000953 /* This does 16 byte alignment, exactly what we need.
954 * The packet length includes FCS, but we don't want to
955 * include that when passing upstream as it messes up
956 * bridging applications.
957 */
Fabio Estevamb72061a2012-02-07 08:27:31 +0000958 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Sascha Hauer85498892009-04-15 01:32:21 +0000960 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100961 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +0000962 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +0100963 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +0000964 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +0000965 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100966
967 /* Extract the frame data without the VLAN header. */
968 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
969 if (vlan_packet_rcvd)
970 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
971 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
972 data + payload_offset,
973 pkt_len - 4 - (2 * ETH_ALEN));
974
Uwe Kleine-Königc5561672011-01-19 11:58:12 +0100975 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +0000976
Frank Li6605b732012-10-30 18:25:31 +0000977 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +0000978 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +0000979 struct skb_shared_hwtstamps *shhwtstamps =
980 skb_hwtstamps(skb);
981 unsigned long flags;
982
983 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
984
985 spin_lock_irqsave(&fep->tmreg_lock, flags);
986 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +0000987 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +0000988 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
989 }
Frank Liff43da82013-01-03 16:04:23 +0000990
Jim Baxter4c09eed2013-04-19 08:10:49 +0000991 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +0100992 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +0000993 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
994 /* don't check it */
995 skb->ip_summed = CHECKSUM_UNNECESSARY;
996 } else {
997 skb_checksum_none_assert(skb);
998 }
999 }
1000
Jim Baxtercdffcf12013-07-02 22:52:56 +01001001 /* Handle received VLAN packets */
1002 if (vlan_packet_rcvd)
1003 __vlan_hwaccel_put_tag(skb,
1004 htons(ETH_P_8021Q),
1005 vlan_tag);
1006
Richard Cochran0affdf32013-08-30 20:28:10 +02001007 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001008 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001009
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001010 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1011 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001012rx_processing_done:
1013 /* Clear the status flags for this buffer */
1014 status &= ~BD_ENET_RX_STATS;
1015
1016 /* Mark the buffer empty */
1017 status |= BD_ENET_RX_EMPTY;
1018 bdp->cbd_sc = status;
1019
Frank Liff43da82013-01-03 16:04:23 +00001020 if (fep->bufdesc_ex) {
1021 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1022
1023 ebdp->cbd_esc = BD_ENET_RX_INT;
1024 ebdp->cbd_prot = 0;
1025 ebdp->cbd_bdu = 0;
1026 }
Frank Li6605b732012-10-30 18:25:31 +00001027
Sascha Hauer22f6b862009-04-15 01:32:18 +00001028 /* Update BD pointer to next entry */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001029 bdp = fec_enet_get_nextdesc(bdp, fep);
1030
Sascha Hauer22f6b862009-04-15 01:32:18 +00001031 /* Doing this here will keep the FEC running while we process
1032 * incoming frames. On a heavily loaded network, we should be
1033 * able to keep up at the expense of system resources.
1034 */
1035 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Sascha Hauer2e285322009-04-15 01:32:16 +00001037 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Frank Lidc975382013-01-28 18:31:42 +00001039 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Uwe Kleine-König45993652011-01-19 20:47:04 +01001042static irqreturn_t
1043fec_enet_interrupt(int irq, void *dev_id)
1044{
1045 struct net_device *ndev = dev_id;
1046 struct fec_enet_private *fep = netdev_priv(ndev);
1047 uint int_events;
1048 irqreturn_t ret = IRQ_NONE;
1049
1050 do {
1051 int_events = readl(fep->hwp + FEC_IEVENT);
1052 writel(int_events, fep->hwp + FEC_IEVENT);
1053
Frank Lide5fb0a2013-03-03 17:34:25 +00001054 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
Uwe Kleine-König45993652011-01-19 20:47:04 +01001055 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001056
1057 /* Disable the RX interrupt */
1058 if (napi_schedule_prep(&fep->napi)) {
1059 writel(FEC_RX_DISABLED_IMASK,
1060 fep->hwp + FEC_IMASK);
1061 __napi_schedule(&fep->napi);
1062 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001063 }
1064
Uwe Kleine-König45993652011-01-19 20:47:04 +01001065 if (int_events & FEC_ENET_MII) {
1066 ret = IRQ_HANDLED;
1067 complete(&fep->mdio_done);
1068 }
1069 } while (int_events);
1070
1071 return ret;
1072}
1073
Frank Lidc975382013-01-28 18:31:42 +00001074static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1075{
1076 struct net_device *ndev = napi->dev;
1077 int pkts = fec_enet_rx(ndev, budget);
1078 struct fec_enet_private *fep = netdev_priv(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001079
Frank Lide5fb0a2013-03-03 17:34:25 +00001080 fec_enet_tx(ndev);
1081
Frank Lidc975382013-01-28 18:31:42 +00001082 if (pkts < budget) {
1083 napi_complete(napi);
1084 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1085 }
1086 return pkts;
1087}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001090static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001092 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001093 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001094 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Shawn Guo49da97d2011-01-05 21:13:11 +00001096 /*
1097 * try to get mac address in following order:
1098 *
1099 * 1) module parameter via kernel command line in form
1100 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1101 */
1102 iap = macaddr;
1103
1104 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001105 * 2) from device tree data
1106 */
1107 if (!is_valid_ether_addr(iap)) {
1108 struct device_node *np = fep->pdev->dev.of_node;
1109 if (np) {
1110 const char *mac = of_get_mac_address(np);
1111 if (mac)
1112 iap = (unsigned char *) mac;
1113 }
1114 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001115
1116 /*
1117 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001118 */
1119 if (!is_valid_ether_addr(iap)) {
1120#ifdef CONFIG_M5272
1121 if (FEC_FLASHMAC)
1122 iap = (unsigned char *)FEC_FLASHMAC;
1123#else
1124 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001125 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001126#endif
1127 }
1128
1129 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001130 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001131 */
1132 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001133 *((__be32 *) &tmpaddr[0]) =
1134 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1135 *((__be16 *) &tmpaddr[4]) =
1136 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 iap = &tmpaddr[0];
1138 }
1139
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001140 /*
1141 * 5) random mac address
1142 */
1143 if (!is_valid_ether_addr(iap)) {
1144 /* Report it and use a random ethernet address instead */
1145 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1146 eth_hw_addr_random(ndev);
1147 netdev_info(ndev, "Using random MAC address: %pM\n",
1148 ndev->dev_addr);
1149 return;
1150 }
1151
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001152 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Shawn Guo49da97d2011-01-05 21:13:11 +00001154 /* Adjust MAC if using macaddr */
1155 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001156 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159/* ------------------------------------------------------------------------- */
1160
Bryan Wue6b043d2010-03-31 02:10:44 +00001161/*
1162 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001164static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001166 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001167 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001168 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Bryan Wue6b043d2010-03-31 02:10:44 +00001170 /* Prevent a state halted on mii error */
1171 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1172 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001175
Bryan Wue6b043d2010-03-31 02:10:44 +00001176 if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001177 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001178 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001179 status_change = 1;
1180 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001181
Lucas Stachd97e7492013-03-14 05:12:01 +00001182 if (fep->full_duplex != phy_dev->duplex)
1183 status_change = 1;
1184
1185 if (phy_dev->speed != fep->speed) {
1186 fep->speed = phy_dev->speed;
1187 status_change = 1;
1188 }
1189
1190 /* if any of the above changed restart the FEC */
1191 if (status_change)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001192 fec_restart(ndev, phy_dev->duplex);
Lucas Stachd97e7492013-03-14 05:12:01 +00001193 } else {
1194 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001195 fec_stop(ndev);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001196 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001197 status_change = 1;
1198 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001199 }
1200
Bryan Wue6b043d2010-03-31 02:10:44 +00001201 if (status_change)
1202 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Bryan Wue6b043d2010-03-31 02:10:44 +00001205static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Bryan Wue6b043d2010-03-31 02:10:44 +00001207 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001208 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001209
1210 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001211 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001212
1213 /* start a read op */
1214 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1215 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1216 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1217
1218 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001219 time_left = wait_for_completion_timeout(&fep->mdio_done,
1220 usecs_to_jiffies(FEC_MII_TIMEOUT));
1221 if (time_left == 0) {
1222 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001223 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001224 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001225 }
1226
1227 /* return value */
1228 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1229}
1230
1231static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1232 u16 value)
1233{
1234 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001235 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001236
1237 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001238 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001239
Shawn Guo862f0982011-01-05 21:13:09 +00001240 /* start a write op */
1241 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001242 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1243 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1244 fep->hwp + FEC_MII_DATA);
1245
1246 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001247 time_left = wait_for_completion_timeout(&fep->mdio_done,
1248 usecs_to_jiffies(FEC_MII_TIMEOUT));
1249 if (time_left == 0) {
1250 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001251 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001252 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001253 }
1254
1255 return 0;
1256}
1257
1258static int fec_enet_mdio_reset(struct mii_bus *bus)
1259{
1260 return 0;
1261}
1262
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001263static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001264{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001265 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001266 const struct platform_device_id *id_entry =
1267 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001268 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001269 char mdio_bus_id[MII_BUS_ID_SIZE];
1270 char phy_name[MII_BUS_ID_SIZE + 3];
1271 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001272 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001273
Bryan Wu418bd0d2010-05-28 03:40:39 -07001274 fep->phy_dev = NULL;
1275
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001276 /* check for attached phy */
1277 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1278 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1279 continue;
1280 if (fep->mii_bus->phy_map[phy_id] == NULL)
1281 continue;
1282 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1283 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001284 if (dev_id--)
1285 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001286 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1287 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001288 }
1289
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001290 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001291 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001292 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001293 phy_id = 0;
1294 }
1295
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001296 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001297 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001298 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001299 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001300 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001301 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001302 }
1303
1304 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001305 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001306 phy_dev->supported &= PHY_GBIT_FEATURES;
Guenter Roeckd1391932013-06-18 10:04:59 -07001307#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001308 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001309#endif
Frank Libaa70a52013-01-16 16:55:58 +00001310 }
Shawn Guo230dec62011-09-23 02:12:48 +00001311 else
1312 phy_dev->supported &= PHY_BASIC_FEATURES;
1313
Bryan Wue6b043d2010-03-31 02:10:44 +00001314 phy_dev->advertising = phy_dev->supported;
1315
1316 fep->phy_dev = phy_dev;
1317 fep->link = 0;
1318 fep->full_duplex = 0;
1319
Joe Perches31b77202013-04-13 19:03:17 +00001320 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1321 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1322 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001323
Bryan Wue6b043d2010-03-31 02:10:44 +00001324 return 0;
1325}
1326
1327static int fec_enet_mii_init(struct platform_device *pdev)
1328{
Shawn Guob5680e02011-01-05 21:13:13 +00001329 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001330 struct net_device *ndev = platform_get_drvdata(pdev);
1331 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001332 const struct platform_device_id *id_entry =
1333 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001334 int err = -ENXIO, i;
1335
Shawn Guob5680e02011-01-05 21:13:13 +00001336 /*
1337 * The dual fec interfaces are not equivalent with enet-mac.
1338 * Here are the differences:
1339 *
1340 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1341 * - fec0 acts as the 1588 time master while fec1 is slave
1342 * - external phys can only be configured by fec0
1343 *
1344 * That is to say fec1 can not work independently. It only works
1345 * when fec0 is working. The reason behind this design is that the
1346 * second interface is added primarily for Switch mode.
1347 *
1348 * Because of the last point above, both phys are attached on fec0
1349 * mdio interface in board design, and need to be configured by
1350 * fec0 mii_bus.
1351 */
Shawn Guo43af9402011-12-05 05:01:15 +00001352 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001353 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001354 if (mii_cnt && fec0_mii_bus) {
1355 fep->mii_bus = fec0_mii_bus;
1356 mii_cnt++;
1357 return 0;
1358 }
1359 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001360 }
1361
Bryan Wue6b043d2010-03-31 02:10:44 +00001362 fep->mii_timeout = 0;
1363
1364 /*
1365 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001366 *
1367 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1368 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1369 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1370 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001371 */
Sascha Hauerf4d40de2012-03-07 09:30:49 +01001372 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001373 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1374 fep->phy_speed--;
1375 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001376 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1377
1378 fep->mii_bus = mdiobus_alloc();
1379 if (fep->mii_bus == NULL) {
1380 err = -ENOMEM;
1381 goto err_out;
1382 }
1383
1384 fep->mii_bus->name = "fec_enet_mii_bus";
1385 fep->mii_bus->read = fec_enet_mdio_read;
1386 fep->mii_bus->write = fec_enet_mdio_write;
1387 fep->mii_bus->reset = fec_enet_mdio_reset;
Florian Fainelli391420f2012-01-09 23:59:13 +00001388 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1389 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001390 fep->mii_bus->priv = fep;
1391 fep->mii_bus->parent = &pdev->dev;
1392
1393 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1394 if (!fep->mii_bus->irq) {
1395 err = -ENOMEM;
1396 goto err_out_free_mdiobus;
1397 }
1398
1399 for (i = 0; i < PHY_MAX_ADDR; i++)
1400 fep->mii_bus->irq[i] = PHY_POLL;
1401
Bryan Wue6b043d2010-03-31 02:10:44 +00001402 if (mdiobus_register(fep->mii_bus))
1403 goto err_out_free_mdio_irq;
1404
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001405 mii_cnt++;
1406
Shawn Guob5680e02011-01-05 21:13:13 +00001407 /* save fec0 mii_bus */
1408 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1409 fec0_mii_bus = fep->mii_bus;
1410
Bryan Wue6b043d2010-03-31 02:10:44 +00001411 return 0;
1412
Bryan Wue6b043d2010-03-31 02:10:44 +00001413err_out_free_mdio_irq:
1414 kfree(fep->mii_bus->irq);
1415err_out_free_mdiobus:
1416 mdiobus_free(fep->mii_bus);
1417err_out:
1418 return err;
1419}
1420
1421static void fec_enet_mii_remove(struct fec_enet_private *fep)
1422{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001423 if (--mii_cnt == 0) {
1424 mdiobus_unregister(fep->mii_bus);
1425 kfree(fep->mii_bus->irq);
1426 mdiobus_free(fep->mii_bus);
1427 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001428}
1429
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001430static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001431 struct ethtool_cmd *cmd)
1432{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001433 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001434 struct phy_device *phydev = fep->phy_dev;
1435
1436 if (!phydev)
1437 return -ENODEV;
1438
1439 return phy_ethtool_gset(phydev, cmd);
1440}
1441
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001442static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001443 struct ethtool_cmd *cmd)
1444{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001445 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001446 struct phy_device *phydev = fep->phy_dev;
1447
1448 if (!phydev)
1449 return -ENODEV;
1450
1451 return phy_ethtool_sset(phydev, cmd);
1452}
1453
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001454static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001455 struct ethtool_drvinfo *info)
1456{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001457 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Jiri Pirko7826d432013-01-06 00:44:26 +00001459 strlcpy(info->driver, fep->pdev->dev.driver->name,
1460 sizeof(info->driver));
1461 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1462 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
Bryan Wue6b043d2010-03-31 02:10:44 +00001464
Frank Li5ebae4892013-01-06 16:25:07 +00001465static int fec_enet_get_ts_info(struct net_device *ndev,
1466 struct ethtool_ts_info *info)
1467{
1468 struct fec_enet_private *fep = netdev_priv(ndev);
1469
1470 if (fep->bufdesc_ex) {
1471
1472 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1473 SOF_TIMESTAMPING_RX_SOFTWARE |
1474 SOF_TIMESTAMPING_SOFTWARE |
1475 SOF_TIMESTAMPING_TX_HARDWARE |
1476 SOF_TIMESTAMPING_RX_HARDWARE |
1477 SOF_TIMESTAMPING_RAW_HARDWARE;
1478 if (fep->ptp_clock)
1479 info->phc_index = ptp_clock_index(fep->ptp_clock);
1480 else
1481 info->phc_index = -1;
1482
1483 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1484 (1 << HWTSTAMP_TX_ON);
1485
1486 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1487 (1 << HWTSTAMP_FILTER_ALL);
1488 return 0;
1489 } else {
1490 return ethtool_op_get_ts_info(ndev, info);
1491 }
1492}
1493
Guenter Roeckd1391932013-06-18 10:04:59 -07001494#if !defined(CONFIG_M5272)
1495
Frank Libaa70a52013-01-16 16:55:58 +00001496static void fec_enet_get_pauseparam(struct net_device *ndev,
1497 struct ethtool_pauseparam *pause)
1498{
1499 struct fec_enet_private *fep = netdev_priv(ndev);
1500
1501 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1502 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1503 pause->rx_pause = pause->tx_pause;
1504}
1505
1506static int fec_enet_set_pauseparam(struct net_device *ndev,
1507 struct ethtool_pauseparam *pause)
1508{
1509 struct fec_enet_private *fep = netdev_priv(ndev);
1510
1511 if (pause->tx_pause != pause->rx_pause) {
1512 netdev_info(ndev,
1513 "hardware only support enable/disable both tx and rx");
1514 return -EINVAL;
1515 }
1516
1517 fep->pause_flag = 0;
1518
1519 /* tx pause must be same as rx pause */
1520 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1521 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1522
1523 if (pause->rx_pause || pause->autoneg) {
1524 fep->phy_dev->supported |= ADVERTISED_Pause;
1525 fep->phy_dev->advertising |= ADVERTISED_Pause;
1526 } else {
1527 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1528 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1529 }
1530
1531 if (pause->autoneg) {
1532 if (netif_running(ndev))
1533 fec_stop(ndev);
1534 phy_start_aneg(fep->phy_dev);
1535 }
1536 if (netif_running(ndev))
1537 fec_restart(ndev, 0);
1538
1539 return 0;
1540}
1541
Chris Healy38ae92d2013-06-25 23:18:52 -07001542static const struct fec_stat {
1543 char name[ETH_GSTRING_LEN];
1544 u16 offset;
1545} fec_stats[] = {
1546 /* RMON TX */
1547 { "tx_dropped", RMON_T_DROP },
1548 { "tx_packets", RMON_T_PACKETS },
1549 { "tx_broadcast", RMON_T_BC_PKT },
1550 { "tx_multicast", RMON_T_MC_PKT },
1551 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1552 { "tx_undersize", RMON_T_UNDERSIZE },
1553 { "tx_oversize", RMON_T_OVERSIZE },
1554 { "tx_fragment", RMON_T_FRAG },
1555 { "tx_jabber", RMON_T_JAB },
1556 { "tx_collision", RMON_T_COL },
1557 { "tx_64byte", RMON_T_P64 },
1558 { "tx_65to127byte", RMON_T_P65TO127 },
1559 { "tx_128to255byte", RMON_T_P128TO255 },
1560 { "tx_256to511byte", RMON_T_P256TO511 },
1561 { "tx_512to1023byte", RMON_T_P512TO1023 },
1562 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1563 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1564 { "tx_octets", RMON_T_OCTETS },
1565
1566 /* IEEE TX */
1567 { "IEEE_tx_drop", IEEE_T_DROP },
1568 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1569 { "IEEE_tx_1col", IEEE_T_1COL },
1570 { "IEEE_tx_mcol", IEEE_T_MCOL },
1571 { "IEEE_tx_def", IEEE_T_DEF },
1572 { "IEEE_tx_lcol", IEEE_T_LCOL },
1573 { "IEEE_tx_excol", IEEE_T_EXCOL },
1574 { "IEEE_tx_macerr", IEEE_T_MACERR },
1575 { "IEEE_tx_cserr", IEEE_T_CSERR },
1576 { "IEEE_tx_sqe", IEEE_T_SQE },
1577 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1578 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1579
1580 /* RMON RX */
1581 { "rx_packets", RMON_R_PACKETS },
1582 { "rx_broadcast", RMON_R_BC_PKT },
1583 { "rx_multicast", RMON_R_MC_PKT },
1584 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1585 { "rx_undersize", RMON_R_UNDERSIZE },
1586 { "rx_oversize", RMON_R_OVERSIZE },
1587 { "rx_fragment", RMON_R_FRAG },
1588 { "rx_jabber", RMON_R_JAB },
1589 { "rx_64byte", RMON_R_P64 },
1590 { "rx_65to127byte", RMON_R_P65TO127 },
1591 { "rx_128to255byte", RMON_R_P128TO255 },
1592 { "rx_256to511byte", RMON_R_P256TO511 },
1593 { "rx_512to1023byte", RMON_R_P512TO1023 },
1594 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1595 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1596 { "rx_octets", RMON_R_OCTETS },
1597
1598 /* IEEE RX */
1599 { "IEEE_rx_drop", IEEE_R_DROP },
1600 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1601 { "IEEE_rx_crc", IEEE_R_CRC },
1602 { "IEEE_rx_align", IEEE_R_ALIGN },
1603 { "IEEE_rx_macerr", IEEE_R_MACERR },
1604 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1605 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1606};
1607
1608static void fec_enet_get_ethtool_stats(struct net_device *dev,
1609 struct ethtool_stats *stats, u64 *data)
1610{
1611 struct fec_enet_private *fep = netdev_priv(dev);
1612 int i;
1613
1614 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1615 data[i] = readl(fep->hwp + fec_stats[i].offset);
1616}
1617
1618static void fec_enet_get_strings(struct net_device *netdev,
1619 u32 stringset, u8 *data)
1620{
1621 int i;
1622 switch (stringset) {
1623 case ETH_SS_STATS:
1624 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1625 memcpy(data + i * ETH_GSTRING_LEN,
1626 fec_stats[i].name, ETH_GSTRING_LEN);
1627 break;
1628 }
1629}
1630
1631static int fec_enet_get_sset_count(struct net_device *dev, int sset)
1632{
1633 switch (sset) {
1634 case ETH_SS_STATS:
1635 return ARRAY_SIZE(fec_stats);
1636 default:
1637 return -EOPNOTSUPP;
1638 }
1639}
Guenter Roeckd1391932013-06-18 10:04:59 -07001640#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07001641
Chris Healy32bc9b42013-06-17 07:25:06 -07001642static int fec_enet_nway_reset(struct net_device *dev)
1643{
1644 struct fec_enet_private *fep = netdev_priv(dev);
1645 struct phy_device *phydev = fep->phy_dev;
1646
1647 if (!phydev)
1648 return -ENODEV;
1649
1650 return genphy_restart_aneg(phydev);
1651}
1652
stephen hemminger9b07be42012-01-04 12:59:49 +00001653static const struct ethtool_ops fec_enet_ethtool_ops = {
Guenter Roeckd1391932013-06-18 10:04:59 -07001654#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001655 .get_pauseparam = fec_enet_get_pauseparam,
1656 .set_pauseparam = fec_enet_set_pauseparam,
Guenter Roeckd1391932013-06-18 10:04:59 -07001657#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00001658 .get_settings = fec_enet_get_settings,
1659 .set_settings = fec_enet_set_settings,
1660 .get_drvinfo = fec_enet_get_drvinfo,
1661 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00001662 .get_ts_info = fec_enet_get_ts_info,
Chris Healy32bc9b42013-06-17 07:25:06 -07001663 .nway_reset = fec_enet_nway_reset,
Chris Healy38ae92d2013-06-25 23:18:52 -07001664#ifndef CONFIG_M5272
1665 .get_ethtool_stats = fec_enet_get_ethtool_stats,
1666 .get_strings = fec_enet_get_strings,
1667 .get_sset_count = fec_enet_get_sset_count,
1668#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00001669};
1670
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001671static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00001672{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001673 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001674 struct phy_device *phydev = fep->phy_dev;
1675
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001676 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00001677 return -EINVAL;
1678
1679 if (!phydev)
1680 return -ENODEV;
1681
Frank Liff43da82013-01-03 16:04:23 +00001682 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
Frank Li6605b732012-10-30 18:25:31 +00001683 return fec_ptp_ioctl(ndev, rq, cmd);
Frank Liff43da82013-01-03 16:04:23 +00001684
Richard Cochran28b04112010-07-17 08:48:55 +00001685 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00001686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001688static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001689{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001690 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001691 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001692 struct sk_buff *skb;
1693 struct bufdesc *bdp;
1694
1695 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001696 for (i = 0; i < fep->rx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001697 skb = fep->rx_skbuff[i];
1698
1699 if (bdp->cbd_bufaddr)
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001700 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001701 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1702 if (skb)
1703 dev_kfree_skb(skb);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001704 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001705 }
1706
1707 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001708 for (i = 0; i < fep->tx_ring_size; i++)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001709 kfree(fep->tx_bounce[i]);
1710}
1711
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001712static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001713{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001714 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03001715 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001716 struct sk_buff *skb;
1717 struct bufdesc *bdp;
1718
1719 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001720 for (i = 0; i < fep->rx_ring_size; i++) {
Fabio Estevamb72061a2012-02-07 08:27:31 +00001721 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001722 if (!skb) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001723 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001724 return -ENOMEM;
1725 }
1726 fep->rx_skbuff[i] = skb;
1727
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01001728 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001729 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001730 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1731 fec_enet_free_buffers(ndev);
1732 if (net_ratelimit())
1733 netdev_err(ndev, "Rx DMA memory map failed\n");
1734 return -ENOMEM;
1735 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001736 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00001737
1738 if (fep->bufdesc_ex) {
1739 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1740 ebdp->cbd_esc = BD_ENET_RX_INT;
1741 }
1742
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001743 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001744 }
1745
1746 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001747 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001748 bdp->cbd_sc |= BD_SC_WRAP;
1749
1750 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001751 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001752 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1753
1754 bdp->cbd_sc = 0;
1755 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00001756
Frank Liff43da82013-01-03 16:04:23 +00001757 if (fep->bufdesc_ex) {
1758 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00001759 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00001760 }
1761
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001762 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001763 }
1764
1765 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001766 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001767 bdp->cbd_sc |= BD_SC_WRAP;
1768
1769 return 0;
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001773fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001775 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001776 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Frank Lidc975382013-01-28 18:31:42 +00001778 napi_enable(&fep->napi);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /* I should reset the ring buffers here, but I don't yet know
1781 * a simple way to do that.
1782 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001784 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001785 if (ret)
1786 return ret;
1787
Bryan Wu418bd0d2010-05-28 03:40:39 -07001788 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001789 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001790 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001791 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001792 return ret;
1793 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001794 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001795 netif_start_queue(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 fep->opened = 1;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001801fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001803 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Sascha Hauer22f6b862009-04-15 01:32:18 +00001805 /* Don't know what to do yet. */
Georg Hofmann3f104c32013-03-14 06:54:09 +00001806 napi_disable(&fep->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 fep->opened = 0;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001808 netif_stop_queue(ndev);
1809 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001811 if (fep->phy_dev) {
1812 phy_stop(fep->phy_dev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001813 phy_disconnect(fep->phy_dev);
Uwe Kleine-Könige497ba82011-01-17 20:04:23 +01001814 }
Bryan Wu418bd0d2010-05-28 03:40:39 -07001815
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001816 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
1819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821/* Set or clear the multicast filter for this adaptor.
1822 * Skeleton taken from sunlance driver.
1823 * The CPM Ethernet implementation allows Multicast as well as individual
1824 * MAC address filtering. Some of the drivers check to make sure it is
1825 * a group multicast address, and discard those that are not. I guess I
1826 * will do the same for now, but just remove the test if you want
1827 * individual filtering as well (do the upper net layers want or support
1828 * this kind of feature?).
1829 */
1830
1831#define HASH_BITS 6 /* #bits in hash */
1832#define CRC32_POLY 0xEDB88320
1833
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001834static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001836 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001837 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00001838 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 unsigned char hash;
1840
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001841 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00001842 tmp = readl(fep->hwp + FEC_R_CNTRL);
1843 tmp |= 0x8;
1844 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00001845 return;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Sascha Hauer4e831832009-04-15 01:32:19 +00001848 tmp = readl(fep->hwp + FEC_R_CNTRL);
1849 tmp &= ~0x8;
1850 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001851
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001852 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001853 /* Catch all multicast addresses, so set the
1854 * filter to all 1's
1855 */
1856 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1857 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Sascha Hauer4e831832009-04-15 01:32:19 +00001859 return;
1860 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001861
Sascha Hauer4e831832009-04-15 01:32:19 +00001862 /* Clear filter and add the addresses in hash register
1863 */
1864 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1865 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001867 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00001868 /* calculate crc32 value of mac address */
1869 crc = 0xffffffff;
1870
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001871 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001872 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00001873 for (bit = 0; bit < 8; bit++, data >>= 1) {
1874 crc = (crc >> 1) ^
1875 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877 }
Sascha Hauer4e831832009-04-15 01:32:19 +00001878
1879 /* only upper 6 bits (HASH_BITS) are used
1880 * which point to specific bit in he hash registers
1881 */
1882 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1883
1884 if (hash > 31) {
1885 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1886 tmp |= 1 << (hash - 32);
1887 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1888 } else {
1889 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1890 tmp |= 1 << hash;
1891 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894}
1895
Sascha Hauer22f6b862009-04-15 01:32:18 +00001896/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00001897static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001898fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001900 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00001901 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Sascha Hauer009fda82009-04-15 01:32:23 +00001903 if (!is_valid_ether_addr(addr->sa_data))
1904 return -EADDRNOTAVAIL;
1905
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001906 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Sascha Hauer009fda82009-04-15 01:32:23 +00001907
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001908 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1909 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00001910 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001911 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07001912 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00001913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001916#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001917/**
1918 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001919 * @dev: The FEC network adapter
1920 *
1921 * Polled functionality used by netconsole and others in non interrupt mode
1922 *
1923 */
Wei Yongjun47a52472013-03-20 05:06:11 +00001924static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001925{
1926 int i;
1927 struct fec_enet_private *fep = netdev_priv(dev);
1928
1929 for (i = 0; i < FEC_IRQ_NUM; i++) {
1930 if (fep->irq[i] > 0) {
1931 disable_irq(fep->irq[i]);
1932 fec_enet_interrupt(fep->irq[i], dev);
1933 enable_irq(fep->irq[i]);
1934 }
1935 }
1936}
1937#endif
1938
Jim Baxter4c09eed2013-04-19 08:10:49 +00001939static int fec_set_features(struct net_device *netdev,
1940 netdev_features_t features)
1941{
1942 struct fec_enet_private *fep = netdev_priv(netdev);
1943 netdev_features_t changed = features ^ netdev->features;
1944
1945 netdev->features = features;
1946
1947 /* Receive checksum has been changed */
1948 if (changed & NETIF_F_RXCSUM) {
1949 if (features & NETIF_F_RXCSUM)
1950 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1951 else
1952 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
1953
1954 if (netif_running(netdev)) {
1955 fec_stop(netdev);
1956 fec_restart(netdev, fep->phy_dev->duplex);
1957 netif_wake_queue(netdev);
1958 } else {
1959 fec_restart(netdev, fep->phy_dev->duplex);
1960 }
1961 }
1962
1963 return 0;
1964}
1965
Sascha Hauer009fda82009-04-15 01:32:23 +00001966static const struct net_device_ops fec_netdev_ops = {
1967 .ndo_open = fec_enet_open,
1968 .ndo_stop = fec_enet_close,
1969 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001970 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001971 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00001972 .ndo_validate_addr = eth_validate_addr,
1973 .ndo_tx_timeout = fec_timeout,
1974 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01001975 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00001976#ifdef CONFIG_NET_POLL_CONTROLLER
1977 .ndo_poll_controller = fec_poll_controller,
1978#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001979 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00001980};
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00001984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001986static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001988 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00001989 const struct platform_device_id *id_entry =
1990 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001991 struct bufdesc *cbd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00001993 /* Allocate memory for buffer descriptors. */
1994 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001995 GFP_KERNEL);
1996 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10001997 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10001998
Frank Li14109a52013-03-26 16:12:03 +00001999 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002000
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002001 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Shawn Guo49da97d2011-01-05 21:13:11 +00002003 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002004 fec_get_mac(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002006 /* init the tx & rx ring size */
2007 fep->tx_ring_size = TX_RING_SIZE;
2008 fep->rx_ring_size = RX_RING_SIZE;
2009
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002010 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 fep->rx_bd_base = cbd_base;
Frank Liff43da82013-01-03 16:04:23 +00002012 if (fep->bufdesc_ex)
2013 fep->tx_bd_base = (struct bufdesc *)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002014 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
Frank Liff43da82013-01-03 16:04:23 +00002015 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002016 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Sascha Hauer22f6b862009-04-15 01:32:18 +00002018 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002019 ndev->watchdog_timeo = TX_TIMEOUT;
2020 ndev->netdev_ops = &fec_netdev_ops;
2021 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002022
Frank Lidc975382013-01-28 18:31:42 +00002023 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002024 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002025
Jim Baxtercdffcf12013-07-02 22:52:56 +01002026 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN) {
2027 /* enable hw VLAN support */
2028 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2029 ndev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2030 }
2031
Shawn Guo48496252013-05-08 21:08:22 +00002032 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
2033 /* enable hw accelerator */
2034 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
2035 | NETIF_F_RXCSUM);
2036 ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
2037 | NETIF_F_RXCSUM);
2038 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2039 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002040
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002041 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return 0;
2044}
2045
Shawn Guoca2cc332011-06-25 02:04:35 +08002046#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002047static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002048{
2049 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002050 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002051 struct device_node *np = pdev->dev.of_node;
2052
2053 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002054 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002055
Shawn Guoa3caad02012-06-27 03:45:24 +00002056 of_property_read_u32(np, "phy-reset-duration", &msec);
2057 /* A sane reset duration should not be longer than 1s */
2058 if (msec > 1000)
2059 msec = 1;
2060
Fabio Estevam7d306222014-01-05 22:08:25 -02002061 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002062 if (!gpio_is_valid(phy_reset))
2063 return;
2064
Fabio Estevam7d306222014-01-05 22:08:25 -02002065 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2066 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002067 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002068 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002069 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002070 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002071 msleep(msec);
Fabio Estevam7d306222014-01-05 22:08:25 -02002072 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002073}
2074#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002075static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002076{
2077 /*
2078 * In case of platform probe, the reset has been done
2079 * by machine code.
2080 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002081}
2082#endif /* CONFIG_OF */
2083
Bill Pemberton33897cc2012-12-03 09:23:58 -05002084static int
Sascha Haueread73182009-01-28 23:03:11 +00002085fec_probe(struct platform_device *pdev)
2086{
2087 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002088 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002089 struct net_device *ndev;
2090 int i, irq, ret = 0;
2091 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002092 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002093 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08002094
2095 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2096 if (of_id)
2097 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002098
Sascha Haueread73182009-01-28 23:03:11 +00002099 /* Init network device */
2100 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002101 if (!ndev)
2102 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002103
2104 SET_NETDEV_DEV(ndev, &pdev->dev);
2105
2106 /* setup board info structure */
2107 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002108
Guenter Roeckd1391932013-06-18 10:04:59 -07002109#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002110 /* default enable pause frame auto negotiation */
2111 if (pdev->id_entry &&
2112 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2113 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002114#endif
Frank Libaa70a52013-01-16 16:55:58 +00002115
Fabio Estevam399db752013-07-21 13:25:03 -03002116 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302117 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2118 if (IS_ERR(fep->hwp)) {
2119 ret = PTR_ERR(fep->hwp);
2120 goto failed_ioremap;
2121 }
2122
Bryan Wue6b043d2010-03-31 02:10:44 +00002123 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002124 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002125
Frank Liff43da82013-01-03 16:04:23 +00002126 fep->bufdesc_ex = 0;
2127
Sascha Haueread73182009-01-28 23:03:11 +00002128 platform_set_drvdata(pdev, ndev);
2129
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002130 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002131 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002132 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002133 if (pdata)
2134 fep->phy_interface = pdata->phy;
2135 else
2136 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2137 } else {
2138 fep->phy_interface = ret;
2139 }
2140
Fabio Estevame2f8d552013-02-22 06:40:45 +00002141 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2142 if (IS_ERR(fep->clk_ipg)) {
2143 ret = PTR_ERR(fep->clk_ipg);
2144 goto failed_clk;
2145 }
2146
2147 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2148 if (IS_ERR(fep->clk_ahb)) {
2149 ret = PTR_ERR(fep->clk_ahb);
2150 goto failed_clk;
2151 }
2152
Linus Torvalds38f56f32013-05-07 11:06:17 -07002153 /* enet_out is optional, depends on board */
2154 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2155 if (IS_ERR(fep->clk_enet_out))
2156 fep->clk_enet_out = NULL;
2157
Fabio Estevame2f8d552013-02-22 06:40:45 +00002158 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002159 fep->bufdesc_ex =
2160 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002161 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002162 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002163 fep->bufdesc_ex = 0;
2164 }
2165
Fabio Estevam13a097b2013-07-21 13:25:02 -03002166 ret = clk_prepare_enable(fep->clk_ahb);
2167 if (ret)
2168 goto failed_clk;
2169
2170 ret = clk_prepare_enable(fep->clk_ipg);
2171 if (ret)
2172 goto failed_clk_ipg;
2173
2174 if (fep->clk_enet_out) {
2175 ret = clk_prepare_enable(fep->clk_enet_out);
2176 if (ret)
2177 goto failed_clk_enet_out;
2178 }
2179
2180 if (fep->clk_ptp) {
2181 ret = clk_prepare_enable(fep->clk_ptp);
2182 if (ret)
2183 goto failed_clk_ptp;
2184 }
Fabio Estevame2f8d552013-02-22 06:40:45 +00002185
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002186 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2187 if (!IS_ERR(fep->reg_phy)) {
2188 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002189 if (ret) {
2190 dev_err(&pdev->dev,
2191 "Failed to enable phy regulator: %d\n", ret);
2192 goto failed_regulator;
2193 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002194 } else {
2195 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002196 }
2197
2198 fec_reset_phy(pdev);
2199
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002200 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002201 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002202
2203 ret = fec_enet_init(ndev);
2204 if (ret)
2205 goto failed_init;
2206
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002207 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002208 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002209 if (irq < 0) {
2210 if (i)
2211 break;
2212 ret = irq;
2213 goto failed_irq;
2214 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002215 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02002216 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002217 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00002218 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00002219 }
2220
Bryan Wue6b043d2010-03-31 02:10:44 +00002221 ret = fec_enet_mii_init(pdev);
2222 if (ret)
2223 goto failed_mii_init;
2224
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002225 /* Carrier starts down, phylib will bring it up */
2226 netif_carrier_off(ndev);
2227
Sascha Haueread73182009-01-28 23:03:11 +00002228 ret = register_netdev(ndev);
2229 if (ret)
2230 goto failed_register;
2231
Fabio Estevameb1d0642013-04-13 07:25:36 +00002232 if (fep->bufdesc_ex && fep->ptp_clock)
2233 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2234
Frank Li54309fa2013-05-07 14:08:44 +00002235 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00002236 return 0;
2237
2238failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002239 fec_enet_mii_remove(fep);
2240failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002241failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002242failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002243 if (fep->reg_phy)
2244 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002245failed_regulator:
Fabio Estevam9514fe72013-07-21 13:24:59 -03002246 if (fep->clk_ptp)
2247 clk_disable_unprepare(fep->clk_ptp);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002248failed_clk_ptp:
Fabio Estevamd265cf42013-07-21 13:25:00 -03002249 if (fep->clk_enet_out)
2250 clk_disable_unprepare(fep->clk_enet_out);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002251failed_clk_enet_out:
Fabio Estevamd265cf42013-07-21 13:25:00 -03002252 clk_disable_unprepare(fep->clk_ipg);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002253failed_clk_ipg:
Fabio Estevamd265cf42013-07-21 13:25:00 -03002254 clk_disable_unprepare(fep->clk_ahb);
Sascha Haueread73182009-01-28 23:03:11 +00002255failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002256failed_ioremap:
2257 free_netdev(ndev);
2258
2259 return ret;
2260}
2261
Bill Pemberton33897cc2012-12-03 09:23:58 -05002262static int
Sascha Haueread73182009-01-28 23:03:11 +00002263fec_drv_remove(struct platform_device *pdev)
2264{
2265 struct net_device *ndev = platform_get_drvdata(pdev);
2266 struct fec_enet_private *fep = netdev_priv(ndev);
2267
Frank Li54309fa2013-05-07 14:08:44 +00002268 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002269 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002270 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002271 del_timer_sync(&fep->time_keep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002272 if (fep->reg_phy)
2273 regulator_disable(fep->reg_phy);
Fabio Estevam9514fe72013-07-21 13:24:59 -03002274 if (fep->clk_ptp)
2275 clk_disable_unprepare(fep->clk_ptp);
Frank Li6605b732012-10-30 18:25:31 +00002276 if (fep->ptp_clock)
2277 ptp_clock_unregister(fep->ptp_clock);
Fabio Estevam9514fe72013-07-21 13:24:59 -03002278 if (fep->clk_enet_out)
2279 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002280 clk_disable_unprepare(fep->clk_ipg);
Fabio Estevamd265cf42013-07-21 13:25:00 -03002281 clk_disable_unprepare(fep->clk_ahb);
Sascha Haueread73182009-01-28 23:03:11 +00002282 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002283
Sascha Haueread73182009-01-28 23:03:11 +00002284 return 0;
2285}
2286
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002287#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002288static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002289fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002290{
Eric Benard87cad5c2010-06-18 04:19:54 +00002291 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002292 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002293
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002294 if (netif_running(ndev)) {
2295 fec_stop(ndev);
2296 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002297 }
Fabio Estevam79820e72013-07-21 13:25:01 -03002298 if (fep->clk_ptp)
2299 clk_disable_unprepare(fep->clk_ptp);
Fabio Estevam9514fe72013-07-21 13:24:59 -03002300 if (fep->clk_enet_out)
2301 clk_disable_unprepare(fep->clk_enet_out);
Sascha Hauerf4d40de2012-03-07 09:30:49 +01002302 clk_disable_unprepare(fep->clk_ipg);
Fabio Estevamd265cf42013-07-21 13:25:00 -03002303 clk_disable_unprepare(fep->clk_ahb);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002304
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002305 if (fep->reg_phy)
2306 regulator_disable(fep->reg_phy);
2307
Sascha Haueread73182009-01-28 23:03:11 +00002308 return 0;
2309}
2310
2311static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002312fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002313{
Eric Benard87cad5c2010-06-18 04:19:54 +00002314 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002315 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002316 int ret;
2317
2318 if (fep->reg_phy) {
2319 ret = regulator_enable(fep->reg_phy);
2320 if (ret)
2321 return ret;
2322 }
Sascha Haueread73182009-01-28 23:03:11 +00002323
Fabio Estevam13a097b2013-07-21 13:25:02 -03002324 ret = clk_prepare_enable(fep->clk_ahb);
2325 if (ret)
2326 goto failed_clk_ahb;
2327
2328 ret = clk_prepare_enable(fep->clk_ipg);
2329 if (ret)
2330 goto failed_clk_ipg;
2331
2332 if (fep->clk_enet_out) {
2333 ret = clk_prepare_enable(fep->clk_enet_out);
2334 if (ret)
2335 goto failed_clk_enet_out;
2336 }
2337
2338 if (fep->clk_ptp) {
2339 ret = clk_prepare_enable(fep->clk_ptp);
2340 if (ret)
2341 goto failed_clk_ptp;
2342 }
2343
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002344 if (netif_running(ndev)) {
2345 fec_restart(ndev, fep->full_duplex);
2346 netif_device_attach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002347 }
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002348
Sascha Haueread73182009-01-28 23:03:11 +00002349 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002350
2351failed_clk_ptp:
2352 if (fep->clk_enet_out)
2353 clk_disable_unprepare(fep->clk_enet_out);
2354failed_clk_enet_out:
2355 clk_disable_unprepare(fep->clk_ipg);
2356failed_clk_ipg:
2357 clk_disable_unprepare(fep->clk_ahb);
2358failed_clk_ahb:
2359 if (fep->reg_phy)
2360 regulator_disable(fep->reg_phy);
2361 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00002362}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002363#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002364
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002365static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002366
Sascha Haueread73182009-01-28 23:03:11 +00002367static struct platform_driver fec_driver = {
2368 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002369 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002370 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002371 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002372 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002373 },
Shawn Guob5680e02011-01-05 21:13:13 +00002374 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002375 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002376 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002377};
2378
Fabio Estevamaaca2372012-01-23 16:44:50 +00002379module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03002381MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382MODULE_LICENSE("GPL");