blob: 8f8e55ea7f858052c3cb3112dc27090af2d58342 [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/delay.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000036#include <linux/in.h>
37#include <linux/ip.h>
38#include <net/ip.h>
Nimrod Andy79f33912014-06-12 08:16:23 +080039#include <net/tso.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000040#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>
Uwe Kleine-König407066f2014-08-11 17:35:33 +020055#include <linux/of_mdio.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080056#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000057#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010058#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030059#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Greg Ungerer080853a2007-07-30 16:28:46 +100061#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Christoph Müllner772e42b2013-06-27 21:18:23 +020065static void set_multicast_list(struct net_device *ndev);
66
Shawn Guob5680e02011-01-05 21:13:13 +000067#define DRIVER_NAME "fec"
68
Fugang Duan4d494cd2014-09-13 05:00:48 +080069#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
70
Frank Libaa70a52013-01-16 16:55:58 +000071/* Pause frame feild and FIFO threshold */
72#define FEC_ENET_FCE (1 << 5)
73#define FEC_ENET_RSEM_V 0x84
74#define FEC_ENET_RSFL_V 16
75#define FEC_ENET_RAEM_V 0x8
76#define FEC_ENET_RAFL_V 0x8
77#define FEC_ENET_OPD_V 0xFFF0
78
Shawn Guob5680e02011-01-05 21:13:13 +000079/* Controller is ENET-MAC */
80#define FEC_QUIRK_ENET_MAC (1 << 0)
81/* Controller needs driver to swap frame */
82#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080083/* Controller uses gasket */
84#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000085/* Controller has GBIT support */
86#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000087/* Controller has extend desc buffer */
88#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000089/* Controller has hardware checksum support */
90#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010091/* Controller has hardware vlan support */
92#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080093/* ENET IP errata ERR006358
94 *
95 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
96 * detected as not set during a prior frame transmission, then the
97 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
98 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +080099 * frames not being transmitted until there is a 0-to-1 transition on
100 * ENET_TDAR[TDAR].
101 */
102#define FEC_QUIRK_ERR006358 (1 << 7)
Fugang Duan95a77472014-09-13 05:00:47 +0800103/* ENET IP hw AVB
104 *
105 * i.MX6SX ENET IP add Audio Video Bridging (AVB) feature support.
106 * - Two class indicators on receive with configurable priority
107 * - Two class indicators and line speed timer on transmit allowing
108 * implementation class credit based shapers externally
109 * - Additional DMA registers provisioned to allow managing up to 3
110 * independent rings
111 */
112#define FEC_QUIRK_HAS_AVB (1 << 8)
Shawn Guob5680e02011-01-05 21:13:13 +0000113
114static struct platform_device_id fec_devtype[] = {
115 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800116 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000117 .name = DRIVER_NAME,
118 .driver_data = 0,
119 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800120 .name = "imx25-fec",
121 .driver_data = FEC_QUIRK_USE_GASKET,
122 }, {
123 .name = "imx27-fec",
124 .driver_data = 0,
125 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000126 .name = "imx28-fec",
127 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800128 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000129 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000130 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100131 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800132 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000133 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000134 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000135 .driver_data = FEC_QUIRK_ENET_MAC,
136 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800137 .name = "imx6sx-fec",
138 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
139 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
140 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
141 FEC_QUIRK_HAS_AVB,
142 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800143 /* sentinel */
144 }
Shawn Guob5680e02011-01-05 21:13:13 +0000145};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800146MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000147
Shawn Guoca2cc332011-06-25 02:04:35 +0800148enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000149 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800150 IMX27_FEC, /* runs on i.mx27/35/51 */
151 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000152 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000153 MVF600_FEC,
Fugang Duanba593e02014-09-13 05:00:53 +0800154 IMX6SX_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800155};
156
157static const struct of_device_id fec_dt_ids[] = {
158 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
159 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
160 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000161 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000162 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Fugang Duanba593e02014-09-13 05:00:53 +0800163 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800164 { /* sentinel */ }
165};
166MODULE_DEVICE_TABLE(of, fec_dt_ids);
167
Shawn Guo49da97d2011-01-05 21:13:11 +0000168static unsigned char macaddr[ETH_ALEN];
169module_param_array(macaddr, byte, NULL, 0);
170MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
171
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000172#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * Some hardware gets it MAC address out of local flash memory.
175 * if this is non-zero then assume it is the address to get MAC from.
176 */
177#if defined(CONFIG_NETtel)
178#define FEC_FLASHMAC 0xf0006006
179#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
180#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#elif defined(CONFIG_CANCam)
182#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000183#elif defined (CONFIG_M5272C3)
184#define FEC_FLASHMAC (0xffe04000 + 4)
185#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000186#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#else
188#define FEC_FLASHMAC 0
189#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800190#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000191
Jim Baxtercdffcf12013-07-02 22:52:56 +0100192/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100194#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100196#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jim Baxter4c09eed2013-04-19 08:10:49 +0000198/* FEC receive acceleration */
199#define FEC_RACC_IPDIS (1 << 1)
200#define FEC_RACC_PRODIS (1 << 2)
201#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Matt Waddel6b265292006-06-27 13:10:56 +1000204 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * size bits. Other FEC hardware does not, so we need to take that into
206 * account when setting it.
207 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000208#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100209 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
211#else
212#define OPT_FRAME_SIZE 0
213#endif
214
Bryan Wue6b043d2010-03-31 02:10:44 +0000215/* FEC MII MMFR bits definition */
216#define FEC_MMFR_ST (1 << 30)
217#define FEC_MMFR_OP_READ (2 << 28)
218#define FEC_MMFR_OP_WRITE (1 << 28)
219#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
220#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
221#define FEC_MMFR_TA (2 << 16)
222#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500224#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Sascha Hauer22f6b862009-04-15 01:32:18 +0000226/* Transmitter timeout */
227#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Frank Libaa70a52013-01-16 16:55:58 +0000229#define FEC_PAUSE_FLAG_AUTONEG 0x1
230#define FEC_PAUSE_FLAG_ENABLE 0x2
231
Nimrod Andy79f33912014-06-12 08:16:23 +0800232#define TSO_HEADER_SIZE 128
233/* Max number of allowed TCP segments for software TSO */
234#define FEC_MAX_TSO_SEGS 100
235#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
236
237#define IS_TSO_HEADER(txq, addr) \
238 ((addr >= txq->tso_hdrs_dma) && \
239 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
240
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000241static int mii_cnt;
242
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800243static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800244struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
245 struct fec_enet_private *fep,
246 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000247{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800248 struct bufdesc *new_bd = bdp + 1;
249 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800250 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
251 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800252 struct bufdesc_ex *ex_base;
253 struct bufdesc *base;
254 int ring_size;
255
Fugang Duan4d494cd2014-09-13 05:00:48 +0800256 if (bdp >= txq->tx_bd_base) {
257 base = txq->tx_bd_base;
258 ring_size = txq->tx_ring_size;
259 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800260 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800261 base = rxq->rx_bd_base;
262 ring_size = rxq->rx_ring_size;
263 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800264 }
265
266 if (fep->bufdesc_ex)
267 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
268 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000269 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800270 return (new_bd >= (base + ring_size)) ?
271 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000272}
273
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800274static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800275struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
276 struct fec_enet_private *fep,
277 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000278{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800279 struct bufdesc *new_bd = bdp - 1;
280 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800281 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
282 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800283 struct bufdesc_ex *ex_base;
284 struct bufdesc *base;
285 int ring_size;
286
Fugang Duan4d494cd2014-09-13 05:00:48 +0800287 if (bdp >= txq->tx_bd_base) {
288 base = txq->tx_bd_base;
289 ring_size = txq->tx_ring_size;
290 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800291 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800292 base = rxq->rx_bd_base;
293 ring_size = rxq->rx_ring_size;
294 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800295 }
296
297 if (fep->bufdesc_ex)
298 return (struct bufdesc *)((ex_new_bd < ex_base) ?
299 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000300 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800301 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000302}
303
Nimrod Andy61a44272014-06-12 08:16:18 +0800304static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
305 struct fec_enet_private *fep)
306{
307 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
308}
309
Fugang Duan4d494cd2014-09-13 05:00:48 +0800310static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
311 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800312{
313 int entries;
314
Fugang Duan4d494cd2014-09-13 05:00:48 +0800315 entries = ((const char *)txq->dirty_tx -
316 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800317
Fugang Duan4d494cd2014-09-13 05:00:48 +0800318 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800319}
320
Shawn Guob5680e02011-01-05 21:13:13 +0000321static void *swap_buffer(void *bufaddr, int len)
322{
323 int i;
324 unsigned int *buf = bufaddr;
325
Fabio Estevamffed61e2013-05-21 05:44:26 +0000326 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000327 *buf = cpu_to_be32(*buf);
328
329 return bufaddr;
330}
331
Russell King344756f2014-07-08 13:01:59 +0100332static void fec_dump(struct net_device *ndev)
333{
334 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800335 struct bufdesc *bdp;
336 struct fec_enet_priv_tx_q *txq;
337 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100338
339 netdev_info(ndev, "TX ring dump\n");
340 pr_info("Nr SC addr len SKB\n");
341
Fugang Duan4d494cd2014-09-13 05:00:48 +0800342 txq = fep->tx_queue[0];
343 bdp = txq->tx_bd_base;
344
Russell King344756f2014-07-08 13:01:59 +0100345 do {
346 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
347 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800348 bdp == txq->cur_tx ? 'S' : ' ',
349 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100350 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800351 txq->tx_skbuff[index]);
352 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100353 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800354 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100355}
356
Fugang Duan62a02c92014-06-18 08:33:52 +0800357static inline bool is_ipv4_pkt(struct sk_buff *skb)
358{
359 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
360}
361
Jim Baxter4c09eed2013-04-19 08:10:49 +0000362static int
363fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
364{
365 /* Only run for packets requiring a checksum. */
366 if (skb->ip_summed != CHECKSUM_PARTIAL)
367 return 0;
368
369 if (unlikely(skb_cow_head(skb, 0)))
370 return -1;
371
Fugang Duan62a02c92014-06-18 08:33:52 +0800372 if (is_ipv4_pkt(skb))
373 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000374 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
375
376 return 0;
377}
378
Nimrod Andy6e909282014-06-12 08:16:22 +0800379static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800380fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
381 struct sk_buff *skb,
382 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800383{
384 struct fec_enet_private *fep = netdev_priv(ndev);
385 const struct platform_device_id *id_entry =
386 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800387 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800388 struct bufdesc_ex *ebdp;
389 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800390 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800391 int frag, frag_len;
392 unsigned short status;
393 unsigned int estatus = 0;
394 skb_frag_t *this_frag;
395 unsigned int index;
396 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100397 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800398 int i;
399
400 for (frag = 0; frag < nr_frags; frag++) {
401 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800402 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800403 ebdp = (struct bufdesc_ex *)bdp;
404
405 status = bdp->cbd_sc;
406 status &= ~BD_ENET_TX_STATS;
407 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
408 frag_len = skb_shinfo(skb)->frags[frag].size;
409
410 /* Handle the last BD specially */
411 if (frag == nr_frags - 1) {
412 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
413 if (fep->bufdesc_ex) {
414 estatus |= BD_ENET_TX_INT;
415 if (unlikely(skb_shinfo(skb)->tx_flags &
416 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
417 estatus |= BD_ENET_TX_TS;
418 }
419 }
420
421 if (fep->bufdesc_ex) {
422 if (skb->ip_summed == CHECKSUM_PARTIAL)
423 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
424 ebdp->cbd_bdu = 0;
425 ebdp->cbd_esc = estatus;
426 }
427
428 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
429
Fugang Duan4d494cd2014-09-13 05:00:48 +0800430 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800431 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800432 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800433 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
434 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800435
436 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
437 swap_buffer(bufaddr, frag_len);
438 }
439
Russell Kingd6bf3142014-07-08 00:23:19 +0100440 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
441 DMA_TO_DEVICE);
442 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800443 dev_kfree_skb_any(skb);
444 if (net_ratelimit())
445 netdev_err(ndev, "Tx DMA memory map failed\n");
446 goto dma_mapping_error;
447 }
448
Russell Kingd6bf3142014-07-08 00:23:19 +0100449 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800450 bdp->cbd_datlen = frag_len;
451 bdp->cbd_sc = status;
452 }
453
Fugang Duan4d494cd2014-09-13 05:00:48 +0800454 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800455
456 return 0;
457
458dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800459 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800460 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800461 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800462 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
463 bdp->cbd_datlen, DMA_TO_DEVICE);
464 }
465 return NETDEV_TX_OK;
466}
467
Fugang Duan4d494cd2014-09-13 05:00:48 +0800468static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
469 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800470{
471 struct fec_enet_private *fep = netdev_priv(ndev);
472 const struct platform_device_id *id_entry =
473 platform_get_device_id(fep->pdev);
474 int nr_frags = skb_shinfo(skb)->nr_frags;
475 struct bufdesc *bdp, *last_bdp;
476 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100477 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800478 unsigned short status;
479 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800480 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800481 unsigned int estatus = 0;
482 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800483 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800484 int ret;
485
Fugang Duan4d494cd2014-09-13 05:00:48 +0800486 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800487 if (entries_free < MAX_SKB_FRAGS + 1) {
488 dev_kfree_skb_any(skb);
489 if (net_ratelimit())
490 netdev_err(ndev, "NOT enough BD for SG!\n");
491 return NETDEV_TX_OK;
492 }
493
Nimrod Andy6e909282014-06-12 08:16:22 +0800494 /* Protocol checksum off-load for TCP and UDP. */
495 if (fec_enet_clear_csum(skb, ndev)) {
496 dev_kfree_skb_any(skb);
497 return NETDEV_TX_OK;
498 }
499
500 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800501 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800502 status = bdp->cbd_sc;
503 status &= ~BD_ENET_TX_STATS;
504
505 /* Set buffer length and buffer pointer */
506 bufaddr = skb->data;
507 buflen = skb_headlen(skb);
508
Fugang Duan4d494cd2014-09-13 05:00:48 +0800509 queue = skb_get_queue_mapping(skb);
510 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Fugang Duan41ef84c2014-09-13 05:00:54 +0800511 if (((unsigned long) bufaddr) & fep->tx_align ||
Nimrod Andy6e909282014-06-12 08:16:22 +0800512 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800513 memcpy(txq->tx_bounce[index], skb->data, buflen);
514 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800515
516 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
517 swap_buffer(bufaddr, buflen);
518 }
519
Russell Kingd6bf3142014-07-08 00:23:19 +0100520 /* Push the data cache so the CPM does not get stale memory data. */
521 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
522 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800523 dev_kfree_skb_any(skb);
524 if (net_ratelimit())
525 netdev_err(ndev, "Tx DMA memory map failed\n");
526 return NETDEV_TX_OK;
527 }
528
529 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800530 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800531 if (ret)
532 return ret;
533 } else {
534 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
535 if (fep->bufdesc_ex) {
536 estatus = BD_ENET_TX_INT;
537 if (unlikely(skb_shinfo(skb)->tx_flags &
538 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
539 estatus |= BD_ENET_TX_TS;
540 }
541 }
542
543 if (fep->bufdesc_ex) {
544
545 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
546
547 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
548 fep->hwts_tx_en))
549 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
550
551 if (skb->ip_summed == CHECKSUM_PARTIAL)
552 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
553
554 ebdp->cbd_bdu = 0;
555 ebdp->cbd_esc = estatus;
556 }
557
Fugang Duan4d494cd2014-09-13 05:00:48 +0800558 last_bdp = txq->cur_tx;
559 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800560 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800561 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800562
563 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100564 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800565
566 /* Send it on its way. Tell FEC it's ready, interrupt when done,
567 * it's the last BD of the frame, and to put the CRC on the end.
568 */
569 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
570 bdp->cbd_sc = status;
571
Sascha Hauer22f6b862009-04-15 01:32:18 +0000572 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800573 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800575 skb_tx_timestamp(skb);
576
Fugang Duan4d494cd2014-09-13 05:00:48 +0800577 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Frank Lide5fb0a2013-03-03 17:34:25 +0000579 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800580 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000581
Nimrod Andy6e909282014-06-12 08:16:22 +0800582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Nimrod Andy79f33912014-06-12 08:16:23 +0800585static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800586fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
587 struct net_device *ndev,
588 struct bufdesc *bdp, int index, char *data,
589 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800590{
591 struct fec_enet_private *fep = netdev_priv(ndev);
592 const struct platform_device_id *id_entry =
593 platform_get_device_id(fep->pdev);
594 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
595 unsigned short status;
596 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100597 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800598
599 status = bdp->cbd_sc;
600 status &= ~BD_ENET_TX_STATS;
601
602 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800603
Fugang Duan41ef84c2014-09-13 05:00:54 +0800604 if (((unsigned long) data) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800605 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800606 memcpy(txq->tx_bounce[index], data, size);
607 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800608
609 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
610 swap_buffer(data, size);
611 }
612
Russell Kingd6bf3142014-07-08 00:23:19 +0100613 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
614 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800615 dev_kfree_skb_any(skb);
616 if (net_ratelimit())
617 netdev_err(ndev, "Tx DMA memory map failed\n");
618 return NETDEV_TX_BUSY;
619 }
620
Russell Kingd6bf3142014-07-08 00:23:19 +0100621 bdp->cbd_datlen = size;
622 bdp->cbd_bufaddr = addr;
623
Nimrod Andy79f33912014-06-12 08:16:23 +0800624 if (fep->bufdesc_ex) {
625 if (skb->ip_summed == CHECKSUM_PARTIAL)
626 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
627 ebdp->cbd_bdu = 0;
628 ebdp->cbd_esc = estatus;
629 }
630
631 /* Handle the last BD specially */
632 if (last_tcp)
633 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
634 if (is_last) {
635 status |= BD_ENET_TX_INTR;
636 if (fep->bufdesc_ex)
637 ebdp->cbd_esc |= BD_ENET_TX_INT;
638 }
639
640 bdp->cbd_sc = status;
641
642 return 0;
643}
644
645static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800646fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
647 struct sk_buff *skb, struct net_device *ndev,
648 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800649{
650 struct fec_enet_private *fep = netdev_priv(ndev);
651 const struct platform_device_id *id_entry =
652 platform_get_device_id(fep->pdev);
653 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
654 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
655 void *bufaddr;
656 unsigned long dmabuf;
657 unsigned short status;
658 unsigned int estatus = 0;
659
660 status = bdp->cbd_sc;
661 status &= ~BD_ENET_TX_STATS;
662 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
663
Fugang Duan4d494cd2014-09-13 05:00:48 +0800664 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
665 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Fugang Duan41ef84c2014-09-13 05:00:54 +0800666 if (((unsigned long)bufaddr) & fep->tx_align ||
Nimrod Andy79f33912014-06-12 08:16:23 +0800667 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800668 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
669 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800670
671 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
672 swap_buffer(bufaddr, hdr_len);
673
674 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
675 hdr_len, DMA_TO_DEVICE);
676 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
677 dev_kfree_skb_any(skb);
678 if (net_ratelimit())
679 netdev_err(ndev, "Tx DMA memory map failed\n");
680 return NETDEV_TX_BUSY;
681 }
682 }
683
684 bdp->cbd_bufaddr = dmabuf;
685 bdp->cbd_datlen = hdr_len;
686
687 if (fep->bufdesc_ex) {
688 if (skb->ip_summed == CHECKSUM_PARTIAL)
689 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
690 ebdp->cbd_bdu = 0;
691 ebdp->cbd_esc = estatus;
692 }
693
694 bdp->cbd_sc = status;
695
696 return 0;
697}
698
Fugang Duan4d494cd2014-09-13 05:00:48 +0800699static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
700 struct sk_buff *skb,
701 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800702{
703 struct fec_enet_private *fep = netdev_priv(ndev);
704 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
705 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800706 struct bufdesc *bdp = txq->cur_tx;
707 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800708 struct tso_t tso;
709 unsigned int index = 0;
710 int ret;
711
Fugang Duan4d494cd2014-09-13 05:00:48 +0800712 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800713 dev_kfree_skb_any(skb);
714 if (net_ratelimit())
715 netdev_err(ndev, "NOT enough BD for TSO!\n");
716 return NETDEV_TX_OK;
717 }
718
719 /* Protocol checksum off-load for TCP and UDP. */
720 if (fec_enet_clear_csum(skb, ndev)) {
721 dev_kfree_skb_any(skb);
722 return NETDEV_TX_OK;
723 }
724
725 /* Initialize the TSO handler, and prepare the first payload */
726 tso_start(skb, &tso);
727
728 total_len = skb->len - hdr_len;
729 while (total_len > 0) {
730 char *hdr;
731
Fugang Duan4d494cd2014-09-13 05:00:48 +0800732 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800733 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
734 total_len -= data_left;
735
736 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800737 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800738 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800739 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800740 if (ret)
741 goto err_release;
742
743 while (data_left > 0) {
744 int size;
745
746 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800747 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
748 index = fec_enet_get_bd_index(txq->tx_bd_base,
749 bdp, fep);
750 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
751 bdp, index,
752 tso.data, size,
753 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800754 total_len == 0);
755 if (ret)
756 goto err_release;
757
758 data_left -= size;
759 tso_build_data(skb, &tso, size);
760 }
761
Fugang Duan4d494cd2014-09-13 05:00:48 +0800762 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800763 }
764
765 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800766 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800767
Nimrod Andy79f33912014-06-12 08:16:23 +0800768 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800769 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800770
771 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800772 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800773
774 return 0;
775
776err_release:
777 /* TODO: Release all used data descriptors for TSO */
778 return ret;
779}
780
Nimrod Andy61a44272014-06-12 08:16:18 +0800781static netdev_tx_t
782fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
783{
784 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800785 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800786 unsigned short queue;
787 struct fec_enet_priv_tx_q *txq;
788 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800789 int ret;
790
Fugang Duan4d494cd2014-09-13 05:00:48 +0800791 queue = skb_get_queue_mapping(skb);
792 txq = fep->tx_queue[queue];
793 nq = netdev_get_tx_queue(ndev, queue);
794
Nimrod Andy79f33912014-06-12 08:16:23 +0800795 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800796 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800797 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800798 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800799 if (ret)
800 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800801
Fugang Duan4d494cd2014-09-13 05:00:48 +0800802 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
803 if (entries_free <= txq->tx_stop_threshold)
804 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800805
806 return NETDEV_TX_OK;
807}
808
Frank Li14109a52013-03-26 16:12:03 +0000809/* Init RX & TX buffer descriptors
810 */
811static void fec_enet_bd_init(struct net_device *dev)
812{
813 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800814 struct fec_enet_priv_tx_q *txq;
815 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000816 struct bufdesc *bdp;
817 unsigned int i;
Frank Li59d0f742014-09-13 05:00:50 +0800818 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000819
Frank Li59d0f742014-09-13 05:00:50 +0800820 for (q = 0; q < fep->num_rx_queues; q++) {
821 /* Initialize the receive buffer descriptors. */
822 rxq = fep->rx_queue[q];
823 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800824
Frank Li59d0f742014-09-13 05:00:50 +0800825 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000826
Frank Li59d0f742014-09-13 05:00:50 +0800827 /* Initialize the BD for every fragment in the page. */
828 if (bdp->cbd_bufaddr)
829 bdp->cbd_sc = BD_ENET_RX_EMPTY;
830 else
831 bdp->cbd_sc = 0;
832 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000833 }
Frank Li59d0f742014-09-13 05:00:50 +0800834
835 /* Set the last buffer to wrap */
836 bdp = fec_enet_get_prevdesc(bdp, fep, q);
837 bdp->cbd_sc |= BD_SC_WRAP;
838
839 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000840 }
841
Frank Li59d0f742014-09-13 05:00:50 +0800842 for (q = 0; q < fep->num_tx_queues; q++) {
843 /* ...and the same for transmit */
844 txq = fep->tx_queue[q];
845 bdp = txq->tx_bd_base;
846 txq->cur_tx = bdp;
847
848 for (i = 0; i < txq->tx_ring_size; i++) {
849 /* Initialize the BD for every fragment in the page. */
850 bdp->cbd_sc = 0;
851 if (txq->tx_skbuff[i]) {
852 dev_kfree_skb_any(txq->tx_skbuff[i]);
853 txq->tx_skbuff[i] = NULL;
854 }
855 bdp->cbd_bufaddr = 0;
856 bdp = fec_enet_get_nextdesc(bdp, fep, q);
857 }
858
859 /* Set the last buffer to wrap */
860 bdp = fec_enet_get_prevdesc(bdp, fep, q);
861 bdp->cbd_sc |= BD_SC_WRAP;
862 txq->dirty_tx = bdp;
863 }
864}
865
Frank Lice99d0d2014-09-13 05:00:52 +0800866static void fec_enet_active_rxring(struct net_device *ndev)
867{
868 struct fec_enet_private *fep = netdev_priv(ndev);
869 int i;
870
871 for (i = 0; i < fep->num_rx_queues; i++)
872 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
873}
874
Frank Li59d0f742014-09-13 05:00:50 +0800875static void fec_enet_enable_ring(struct net_device *ndev)
876{
877 struct fec_enet_private *fep = netdev_priv(ndev);
878 struct fec_enet_priv_tx_q *txq;
879 struct fec_enet_priv_rx_q *rxq;
880 int i;
881
882 for (i = 0; i < fep->num_rx_queues; i++) {
883 rxq = fep->rx_queue[i];
884 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
885
886 /* enable DMA1/2 */
887 if (i)
888 writel(RCMR_MATCHEN | RCMR_CMP(i),
889 fep->hwp + FEC_RCMR(i));
890 }
891
892 for (i = 0; i < fep->num_tx_queues; i++) {
893 txq = fep->tx_queue[i];
894 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
895
896 /* enable DMA1/2 */
897 if (i)
898 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
899 fep->hwp + FEC_DMA_CFG(i));
900 }
901}
902
903static void fec_enet_reset_skb(struct net_device *ndev)
904{
905 struct fec_enet_private *fep = netdev_priv(ndev);
906 struct fec_enet_priv_tx_q *txq;
907 int i, j;
908
909 for (i = 0; i < fep->num_tx_queues; i++) {
910 txq = fep->tx_queue[i];
911
912 for (j = 0; j < txq->tx_ring_size; j++) {
913 if (txq->tx_skbuff[j]) {
914 dev_kfree_skb_any(txq->tx_skbuff[j]);
915 txq->tx_skbuff[j] = NULL;
916 }
917 }
918 }
Frank Li14109a52013-03-26 16:12:03 +0000919}
920
Russell Kingdbc64a82014-07-08 12:40:12 +0100921/*
922 * This function is called to start or restart the FEC during a link
923 * change, transmit timeout, or to reconfigure the FEC. The network
924 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100925 */
926static void
Russell Kingef833372014-07-08 12:40:43 +0100927fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100928{
929 struct fec_enet_private *fep = netdev_priv(ndev);
930 const struct platform_device_id *id_entry =
931 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000932 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100933 u32 temp_mac[2];
934 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000935 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100936
Fugang Duan106c3142014-09-13 05:00:51 +0800937 /* Whack a reset. We should wait for this.
938 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
939 * instead of reset MAC itself.
940 */
941 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
942 writel(0, fep->hwp + FEC_ECNTRL);
943 } else {
944 writel(1, fep->hwp + FEC_ECNTRL);
945 udelay(10);
946 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100947
948 /*
949 * enet-mac reset will reset mac address registers too,
950 * so need to reconfigure it.
951 */
952 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
953 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
954 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
955 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
956 }
957
958 /* Clear any outstanding interrupt. */
959 writel(0xffc00000, fep->hwp + FEC_IEVENT);
960
Uwe Kleine-König45993652011-01-19 20:47:04 +0100961 /* Set maximum receive buffer size. */
962 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
963
Frank Li14109a52013-03-26 16:12:03 +0000964 fec_enet_bd_init(ndev);
965
Frank Li59d0f742014-09-13 05:00:50 +0800966 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100967
Frank Li59d0f742014-09-13 05:00:50 +0800968 /* Reset tx SKB buffers. */
969 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100970
971 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100972 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100973 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100974 writel(0x04, fep->hwp + FEC_X_CNTRL);
975 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100976 /* No Rcv on Xmit */
977 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100978 writel(0x0, fep->hwp + FEC_X_CNTRL);
979 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100980
Uwe Kleine-König45993652011-01-19 20:47:04 +0100981 /* Set MII speed */
982 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
983
Guenter Roeckd1391932013-06-18 10:04:59 -0700984#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000985 /* set RX checksum */
986 val = readl(fep->hwp + FEC_RACC);
987 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
988 val |= FEC_RACC_OPTIONS;
989 else
990 val &= ~FEC_RACC_OPTIONS;
991 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700992#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000993
Uwe Kleine-König45993652011-01-19 20:47:04 +0100994 /*
995 * The phy interface and speed need to get configured
996 * differently on enet-mac.
997 */
998 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100999 /* Enable flow control and length check */
1000 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001001
Shawn Guo230dec62011-09-23 02:12:48 +00001002 /* RGMII, RMII or MII */
1003 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1004 rcntl |= (1 << 6);
1005 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001006 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001007 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001008 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001009
Shawn Guo230dec62011-09-23 02:12:48 +00001010 /* 1G, 100M or 10M */
1011 if (fep->phy_dev) {
1012 if (fep->phy_dev->speed == SPEED_1000)
1013 ecntl |= (1 << 5);
1014 else if (fep->phy_dev->speed == SPEED_100)
1015 rcntl &= ~(1 << 9);
1016 else
1017 rcntl |= (1 << 9);
1018 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001019 } else {
1020#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001021 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001022 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001023 /* disable the gasket and wait */
1024 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1025 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1026 udelay(1);
1027
1028 /*
1029 * configure the gasket:
1030 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001031 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001032 */
Eric Benard8d82f212012-01-12 06:10:28 +00001033 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1034 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1035 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1036 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1037 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001038
1039 /* re-enable the gasket */
1040 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1041 }
1042#endif
1043 }
Frank Libaa70a52013-01-16 16:55:58 +00001044
Guenter Roeckd1391932013-06-18 10:04:59 -07001045#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001046 /* enable pause frame*/
1047 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1048 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1049 fep->phy_dev && fep->phy_dev->pause)) {
1050 rcntl |= FEC_ENET_FCE;
1051
Jim Baxter4c09eed2013-04-19 08:10:49 +00001052 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001053 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1054 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1055 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1056 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1057
1058 /* OPD */
1059 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1060 } else {
1061 rcntl &= ~FEC_ENET_FCE;
1062 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001063#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001064
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001065 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001066
Stefan Wahren84fe6182014-03-12 11:28:19 +01001067 /* Setup multicast filter. */
1068 set_multicast_list(ndev);
1069#ifndef CONFIG_M5272
1070 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1071 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1072#endif
1073
Shawn Guo230dec62011-09-23 02:12:48 +00001074 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1075 /* enable ENET endian swap */
1076 ecntl |= (1 << 8);
1077 /* enable ENET store and forward mode */
1078 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1079 }
1080
Frank Liff43da82013-01-03 16:04:23 +00001081 if (fep->bufdesc_ex)
1082 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001083
Chris Healy38ae92d2013-06-25 23:18:52 -07001084#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001085 /* Enable the MIB statistic event counters */
1086 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001087#endif
1088
Uwe Kleine-König45993652011-01-19 20:47:04 +01001089 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001090 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001091 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001092
Frank Liff43da82013-01-03 16:04:23 +00001093 if (fep->bufdesc_ex)
1094 fec_ptp_start_cyclecounter(ndev);
1095
Uwe Kleine-König45993652011-01-19 20:47:04 +01001096 /* Enable interrupts we wish to service */
1097 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1098}
1099
1100static void
1101fec_stop(struct net_device *ndev)
1102{
1103 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001104 const struct platform_device_id *id_entry =
1105 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001106 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001107
1108 /* We cannot expect a graceful transmit stop without link !!! */
1109 if (fep->link) {
1110 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1111 udelay(10);
1112 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001113 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001114 }
1115
Fugang Duan106c3142014-09-13 05:00:51 +08001116 /* Whack a reset. We should wait for this.
1117 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1118 * instead of reset MAC itself.
1119 */
1120 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1121 writel(0, fep->hwp + FEC_ECNTRL);
1122 } else {
1123 writel(1, fep->hwp + FEC_ECNTRL);
1124 udelay(10);
1125 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001126 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1127 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001128
1129 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001130 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001131 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001132 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1133 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001134}
1135
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001138fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001140 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Russell King344756f2014-07-08 13:01:59 +01001142 fec_dump(ndev);
1143
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001144 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Russell King36cdc742014-07-08 13:01:44 +01001146 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001147}
1148
Russell King36cdc742014-07-08 13:01:44 +01001149static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001150{
1151 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001152 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001153 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001154
Russell King36cdc742014-07-08 13:01:44 +01001155 rtnl_lock();
1156 if (netif_device_present(ndev) || netif_running(ndev)) {
1157 napi_disable(&fep->napi);
1158 netif_tx_lock_bh(ndev);
1159 fec_restart(ndev);
1160 netif_wake_queue(ndev);
1161 netif_tx_unlock_bh(ndev);
1162 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001163 }
Russell King36cdc742014-07-08 13:01:44 +01001164 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001168fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1169 struct skb_shared_hwtstamps *hwtstamps)
1170{
1171 unsigned long flags;
1172 u64 ns;
1173
1174 spin_lock_irqsave(&fep->tmreg_lock, flags);
1175 ns = timecounter_cyc2time(&fep->tc, ts);
1176 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1177
1178 memset(hwtstamps, 0, sizeof(*hwtstamps));
1179 hwtstamps->hwtstamp = ns_to_ktime(ns);
1180}
1181
1182static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001183fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001186 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001187 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001189 struct fec_enet_priv_tx_q *txq;
1190 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001191 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001192 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001194 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001195
1196 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1197
1198 txq = fep->tx_queue[queue_id];
1199 /* get next bdp of dirty_tx */
1200 nq = netdev_get_tx_queue(ndev, queue_id);
1201 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Frank Lide5fb0a2013-03-03 17:34:25 +00001203 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001204 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001205
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001206 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001207
1208 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001209 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001210 break;
1211
Fugang Duan4d494cd2014-09-13 05:00:48 +08001212 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001213
Fugang Duan4d494cd2014-09-13 05:00:48 +08001214 skb = txq->tx_skbuff[index];
1215 txq->tx_skbuff[index] = NULL;
1216 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001217 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1218 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001219 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001220 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001221 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001222 continue;
1223 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001226 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 BD_ENET_TX_RL | BD_ENET_TX_UN |
1228 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001229 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001230 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001231 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001232 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001233 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001234 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001235 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001236 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001237 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001238 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001239 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001242 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
Frank Liff43da82013-01-03 16:04:23 +00001245 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1246 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001247 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001248 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001249
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001250 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001251 skb_tstamp_tx(skb, &shhwtstamps);
1252 }
Frank Liff43da82013-01-03 16:04:23 +00001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Deferred means some collisions occurred during transmit,
1255 * but we eventually sent the packet OK.
1256 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001257 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001258 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001259
Sascha Hauer22f6b862009-04-15 01:32:18 +00001260 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001262
Fugang Duan4d494cd2014-09-13 05:00:48 +08001263 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001264
Sascha Hauer22f6b862009-04-15 01:32:18 +00001265 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001266 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001267
Sascha Hauer22f6b862009-04-15 01:32:18 +00001268 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001270 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001271 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1272 if (entries_free >= txq->tx_wake_threshold)
1273 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Russell Kingccea2962014-07-08 13:01:38 +01001276
1277 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001278 if (bdp != txq->cur_tx &&
1279 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1280 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1281}
1282
1283static void
1284fec_enet_tx(struct net_device *ndev)
1285{
1286 struct fec_enet_private *fep = netdev_priv(ndev);
1287 u16 queue_id;
1288 /* First process class A queue, then Class B and Best Effort queue */
1289 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1290 clear_bit(queue_id, &fep->work_tx);
1291 fec_enet_tx_queue(ndev, queue_id);
1292 }
1293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/* During a receive, the cur_rx points to the current incoming buffer.
1297 * When we update through the ring, if the next incoming buffer has
1298 * not been given to the system, we just set the empty indicator,
1299 * effectively tossing the packet.
1300 */
Frank Lidc975382013-01-28 18:31:42 +00001301static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001302fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001304 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001305 const struct platform_device_id *id_entry =
1306 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001307 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001308 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001309 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct sk_buff *skb;
1311 ushort pkt_len;
1312 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001313 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001314 struct bufdesc_ex *ebdp = NULL;
1315 bool vlan_packet_rcvd = false;
1316 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001317 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001318
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001319#ifdef CONFIG_M532x
1320 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001321#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001322 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1323 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* First, grab all of the stats for the incoming packet.
1326 * These get messed up if we get called due to a busy condition.
1327 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001328 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Sascha Hauer22f6b862009-04-15 01:32:18 +00001330 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Frank Lidc975382013-01-28 18:31:42 +00001332 if (pkt_received >= budget)
1333 break;
1334 pkt_received++;
1335
Sascha Hauer22f6b862009-04-15 01:32:18 +00001336 /* Since we have allocated space to hold a complete frame,
1337 * the last indicator should be set.
1338 */
1339 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001340 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Russell Kingdb3421c2014-07-08 13:01:49 +01001342
Sascha Hauer22f6b862009-04-15 01:32:18 +00001343 /* Check for errors. */
1344 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001346 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001347 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1348 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001349 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001350 }
1351 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001352 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001353 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001354 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001355 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001356 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001358
1359 /* Report late collisions as a frame error.
1360 * On this error, the BD is closed, but we don't know what we
1361 * have in the buffer. So, just drop this frame on the floor.
1362 */
1363 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001364 ndev->stats.rx_errors++;
1365 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001366 goto rx_processing_done;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Sascha Hauer22f6b862009-04-15 01:32:18 +00001369 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001370 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001371 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001372 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Fugang Duan4d494cd2014-09-13 05:00:48 +08001374 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1375 data = rxq->rx_skbuff[index]->data;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001376 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1377 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001378
Shawn Guob5680e02011-01-05 21:13:13 +00001379 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1380 swap_buffer(data, pkt_len);
1381
Jim Baxtercdffcf12013-07-02 22:52:56 +01001382 /* Extract the enhanced buffer descriptor */
1383 ebdp = NULL;
1384 if (fep->bufdesc_ex)
1385 ebdp = (struct bufdesc_ex *)bdp;
1386
1387 /* If this is a VLAN packet remove the VLAN Tag */
1388 vlan_packet_rcvd = false;
1389 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001390 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001391 /* Push and remove the vlan tag */
1392 struct vlan_hdr *vlan_header =
1393 (struct vlan_hdr *) (data + ETH_HLEN);
1394 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1395 pkt_len -= VLAN_HLEN;
1396
1397 vlan_packet_rcvd = true;
1398 }
1399
Sascha Hauer22f6b862009-04-15 01:32:18 +00001400 /* This does 16 byte alignment, exactly what we need.
1401 * The packet length includes FCS, but we don't want to
1402 * include that when passing upstream as it messes up
1403 * bridging applications.
1404 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001405 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Sascha Hauer85498892009-04-15 01:32:21 +00001407 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001408 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001409 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001410 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001411 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001412 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001413
1414 /* Extract the frame data without the VLAN header. */
1415 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1416 if (vlan_packet_rcvd)
1417 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001418 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
Jim Baxtercdffcf12013-07-02 22:52:56 +01001419 data + payload_offset,
1420 pkt_len - 4 - (2 * ETH_ALEN));
1421
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001422 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001423
Frank Li6605b732012-10-30 18:25:31 +00001424 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001425 if (fep->hwts_rx_en && fep->bufdesc_ex)
1426 fec_enet_hwtstamp(fep, ebdp->ts,
1427 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001428
Jim Baxter4c09eed2013-04-19 08:10:49 +00001429 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001430 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001431 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1432 /* don't check it */
1433 skb->ip_summed = CHECKSUM_UNNECESSARY;
1434 } else {
1435 skb_checksum_none_assert(skb);
1436 }
1437 }
1438
Jim Baxtercdffcf12013-07-02 22:52:56 +01001439 /* Handle received VLAN packets */
1440 if (vlan_packet_rcvd)
1441 __vlan_hwaccel_put_tag(skb,
1442 htons(ETH_P_8021Q),
1443 vlan_tag);
1444
Richard Cochran0affdf32013-08-30 20:28:10 +02001445 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001446 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001447
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001448 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1449 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001450rx_processing_done:
1451 /* Clear the status flags for this buffer */
1452 status &= ~BD_ENET_RX_STATS;
1453
1454 /* Mark the buffer empty */
1455 status |= BD_ENET_RX_EMPTY;
1456 bdp->cbd_sc = status;
1457
Frank Liff43da82013-01-03 16:04:23 +00001458 if (fep->bufdesc_ex) {
1459 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1460
1461 ebdp->cbd_esc = BD_ENET_RX_INT;
1462 ebdp->cbd_prot = 0;
1463 ebdp->cbd_bdu = 0;
1464 }
Frank Li6605b732012-10-30 18:25:31 +00001465
Sascha Hauer22f6b862009-04-15 01:32:18 +00001466 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001467 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001468
Sascha Hauer22f6b862009-04-15 01:32:18 +00001469 /* Doing this here will keep the FEC running while we process
1470 * incoming frames. On a heavily loaded network, we should be
1471 * able to keep up at the expense of system resources.
1472 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001473 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001475 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001476 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Fugang Duan4d494cd2014-09-13 05:00:48 +08001479static int
1480fec_enet_rx(struct net_device *ndev, int budget)
1481{
1482 int pkt_received = 0;
1483 u16 queue_id;
1484 struct fec_enet_private *fep = netdev_priv(ndev);
1485
1486 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1487 clear_bit(queue_id, &fep->work_rx);
1488 pkt_received += fec_enet_rx_queue(ndev,
1489 budget - pkt_received, queue_id);
1490 }
1491 return pkt_received;
1492}
1493
1494static bool
1495fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1496{
1497 if (int_events == 0)
1498 return false;
1499
1500 if (int_events & FEC_ENET_RXF)
1501 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001502 if (int_events & FEC_ENET_RXF_1)
1503 fep->work_rx |= (1 << 0);
1504 if (int_events & FEC_ENET_RXF_2)
1505 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001506
1507 if (int_events & FEC_ENET_TXF)
1508 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001509 if (int_events & FEC_ENET_TXF_1)
1510 fep->work_tx |= (1 << 0);
1511 if (int_events & FEC_ENET_TXF_2)
1512 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001513
1514 return true;
1515}
1516
Uwe Kleine-König45993652011-01-19 20:47:04 +01001517static irqreturn_t
1518fec_enet_interrupt(int irq, void *dev_id)
1519{
1520 struct net_device *ndev = dev_id;
1521 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001522 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001523 uint int_events;
1524 irqreturn_t ret = IRQ_NONE;
1525
Russell King7a168072014-07-08 00:22:44 +01001526 int_events = readl(fep->hwp + FEC_IEVENT);
1527 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001528 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001529
Russell King7a168072014-07-08 00:22:44 +01001530 if (int_events & napi_mask) {
1531 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001532
Russell King7a168072014-07-08 00:22:44 +01001533 /* Disable the NAPI interrupts */
1534 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1535 napi_schedule(&fep->napi);
1536 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001537
Russell King7a168072014-07-08 00:22:44 +01001538 if (int_events & FEC_ENET_MII) {
1539 ret = IRQ_HANDLED;
1540 complete(&fep->mdio_done);
1541 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001542
1543 return ret;
1544}
1545
Frank Lidc975382013-01-28 18:31:42 +00001546static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1547{
1548 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001549 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001550 int pkts;
1551
1552 /*
1553 * Clear any pending transmit or receive interrupts before
1554 * processing the rings to avoid racing with the hardware.
1555 */
1556 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1557
1558 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001559
Frank Lide5fb0a2013-03-03 17:34:25 +00001560 fec_enet_tx(ndev);
1561
Frank Lidc975382013-01-28 18:31:42 +00001562 if (pkts < budget) {
1563 napi_complete(napi);
1564 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1565 }
1566 return pkts;
1567}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001570static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001572 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001573 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001574 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Shawn Guo49da97d2011-01-05 21:13:11 +00001576 /*
1577 * try to get mac address in following order:
1578 *
1579 * 1) module parameter via kernel command line in form
1580 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1581 */
1582 iap = macaddr;
1583
1584 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001585 * 2) from device tree data
1586 */
1587 if (!is_valid_ether_addr(iap)) {
1588 struct device_node *np = fep->pdev->dev.of_node;
1589 if (np) {
1590 const char *mac = of_get_mac_address(np);
1591 if (mac)
1592 iap = (unsigned char *) mac;
1593 }
1594 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001595
1596 /*
1597 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001598 */
1599 if (!is_valid_ether_addr(iap)) {
1600#ifdef CONFIG_M5272
1601 if (FEC_FLASHMAC)
1602 iap = (unsigned char *)FEC_FLASHMAC;
1603#else
1604 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001605 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001606#endif
1607 }
1608
1609 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001610 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001611 */
1612 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001613 *((__be32 *) &tmpaddr[0]) =
1614 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1615 *((__be16 *) &tmpaddr[4]) =
1616 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 iap = &tmpaddr[0];
1618 }
1619
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001620 /*
1621 * 5) random mac address
1622 */
1623 if (!is_valid_ether_addr(iap)) {
1624 /* Report it and use a random ethernet address instead */
1625 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1626 eth_hw_addr_random(ndev);
1627 netdev_info(ndev, "Using random MAC address: %pM\n",
1628 ndev->dev_addr);
1629 return;
1630 }
1631
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001632 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Shawn Guo49da97d2011-01-05 21:13:11 +00001634 /* Adjust MAC if using macaddr */
1635 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001636 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639/* ------------------------------------------------------------------------- */
1640
Bryan Wue6b043d2010-03-31 02:10:44 +00001641/*
1642 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001644static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001646 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001647 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001648 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Bryan Wue6b043d2010-03-31 02:10:44 +00001650 /* Prevent a state halted on mii error */
1651 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1652 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001653 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001655
Russell King8ce56242014-07-08 12:40:07 +01001656 /*
1657 * If the netdev is down, or is going down, we're not interested
1658 * in link state events, so just mark our idea of the link as down
1659 * and ignore the event.
1660 */
1661 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1662 fep->link = 0;
1663 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001664 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001665 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001666 status_change = 1;
1667 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001668
Russell Kingef833372014-07-08 12:40:43 +01001669 if (fep->full_duplex != phy_dev->duplex) {
1670 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001671 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001672 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001673
1674 if (phy_dev->speed != fep->speed) {
1675 fep->speed = phy_dev->speed;
1676 status_change = 1;
1677 }
1678
1679 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001680 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001681 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001682 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001683 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001684 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001685 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001686 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001687 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001688 } else {
1689 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001690 napi_disable(&fep->napi);
1691 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001692 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001693 netif_tx_unlock_bh(ndev);
1694 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001695 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001696 status_change = 1;
1697 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001698 }
1699
Bryan Wue6b043d2010-03-31 02:10:44 +00001700 if (status_change)
1701 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Bryan Wue6b043d2010-03-31 02:10:44 +00001704static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Bryan Wue6b043d2010-03-31 02:10:44 +00001706 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001707 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001708
1709 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001710 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001711
1712 /* start a read op */
1713 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1714 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1715 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1716
1717 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001718 time_left = wait_for_completion_timeout(&fep->mdio_done,
1719 usecs_to_jiffies(FEC_MII_TIMEOUT));
1720 if (time_left == 0) {
1721 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001722 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001723 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001724 }
1725
1726 /* return value */
1727 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1728}
1729
1730static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1731 u16 value)
1732{
1733 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001734 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001735
1736 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001737 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001738
Shawn Guo862f0982011-01-05 21:13:09 +00001739 /* start a write op */
1740 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001741 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1742 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1743 fep->hwp + FEC_MII_DATA);
1744
1745 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001746 time_left = wait_for_completion_timeout(&fep->mdio_done,
1747 usecs_to_jiffies(FEC_MII_TIMEOUT));
1748 if (time_left == 0) {
1749 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001750 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001751 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001752 }
1753
1754 return 0;
1755}
1756
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001757static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1758{
1759 struct fec_enet_private *fep = netdev_priv(ndev);
1760 int ret;
1761
1762 if (enable) {
1763 ret = clk_prepare_enable(fep->clk_ahb);
1764 if (ret)
1765 return ret;
1766 ret = clk_prepare_enable(fep->clk_ipg);
1767 if (ret)
1768 goto failed_clk_ipg;
1769 if (fep->clk_enet_out) {
1770 ret = clk_prepare_enable(fep->clk_enet_out);
1771 if (ret)
1772 goto failed_clk_enet_out;
1773 }
1774 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001775 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001776 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001777 if (ret) {
1778 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001779 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001780 } else {
1781 fep->ptp_clk_on = true;
1782 }
1783 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001784 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001785 if (fep->clk_ref) {
1786 ret = clk_prepare_enable(fep->clk_ref);
1787 if (ret)
1788 goto failed_clk_ref;
1789 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001790 } else {
1791 clk_disable_unprepare(fep->clk_ahb);
1792 clk_disable_unprepare(fep->clk_ipg);
1793 if (fep->clk_enet_out)
1794 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001795 if (fep->clk_ptp) {
1796 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001797 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001798 fep->ptp_clk_on = false;
1799 mutex_unlock(&fep->ptp_clk_mutex);
1800 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001801 if (fep->clk_ref)
1802 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001803 }
1804
1805 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001806
1807failed_clk_ref:
1808 if (fep->clk_ref)
1809 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001810failed_clk_ptp:
1811 if (fep->clk_enet_out)
1812 clk_disable_unprepare(fep->clk_enet_out);
1813failed_clk_enet_out:
1814 clk_disable_unprepare(fep->clk_ipg);
1815failed_clk_ipg:
1816 clk_disable_unprepare(fep->clk_ahb);
1817
1818 return ret;
1819}
1820
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001821static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001822{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001823 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001824 const struct platform_device_id *id_entry =
1825 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001826 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001827 char mdio_bus_id[MII_BUS_ID_SIZE];
1828 char phy_name[MII_BUS_ID_SIZE + 3];
1829 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001830 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001831
Bryan Wu418bd0d2010-05-28 03:40:39 -07001832 fep->phy_dev = NULL;
1833
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001834 if (fep->phy_node) {
1835 phy_dev = of_phy_connect(ndev, fep->phy_node,
1836 &fec_enet_adjust_link, 0,
1837 fep->phy_interface);
1838 } else {
1839 /* check for attached phy */
1840 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1841 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1842 continue;
1843 if (fep->mii_bus->phy_map[phy_id] == NULL)
1844 continue;
1845 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1846 continue;
1847 if (dev_id--)
1848 continue;
1849 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1850 break;
1851 }
1852
1853 if (phy_id >= PHY_MAX_ADDR) {
1854 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1855 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1856 phy_id = 0;
1857 }
1858
1859 snprintf(phy_name, sizeof(phy_name),
1860 PHY_ID_FMT, mdio_bus_id, phy_id);
1861 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1862 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001863 }
1864
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001865 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001866 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001867 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001868 }
1869
1870 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001871 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001872 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001873 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001874#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001875 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001876#endif
Frank Libaa70a52013-01-16 16:55:58 +00001877 }
Shawn Guo230dec62011-09-23 02:12:48 +00001878 else
1879 phy_dev->supported &= PHY_BASIC_FEATURES;
1880
Bryan Wue6b043d2010-03-31 02:10:44 +00001881 phy_dev->advertising = phy_dev->supported;
1882
1883 fep->phy_dev = phy_dev;
1884 fep->link = 0;
1885 fep->full_duplex = 0;
1886
Joe Perches31b77202013-04-13 19:03:17 +00001887 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1888 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1889 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001890
Bryan Wue6b043d2010-03-31 02:10:44 +00001891 return 0;
1892}
1893
1894static int fec_enet_mii_init(struct platform_device *pdev)
1895{
Shawn Guob5680e02011-01-05 21:13:13 +00001896 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001897 struct net_device *ndev = platform_get_drvdata(pdev);
1898 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001899 const struct platform_device_id *id_entry =
1900 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001901 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001902 int err = -ENXIO, i;
1903
Shawn Guob5680e02011-01-05 21:13:13 +00001904 /*
1905 * The dual fec interfaces are not equivalent with enet-mac.
1906 * Here are the differences:
1907 *
1908 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1909 * - fec0 acts as the 1588 time master while fec1 is slave
1910 * - external phys can only be configured by fec0
1911 *
1912 * That is to say fec1 can not work independently. It only works
1913 * when fec0 is working. The reason behind this design is that the
1914 * second interface is added primarily for Switch mode.
1915 *
1916 * Because of the last point above, both phys are attached on fec0
1917 * mdio interface in board design, and need to be configured by
1918 * fec0 mii_bus.
1919 */
Shawn Guo43af9402011-12-05 05:01:15 +00001920 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001921 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001922 if (mii_cnt && fec0_mii_bus) {
1923 fep->mii_bus = fec0_mii_bus;
1924 mii_cnt++;
1925 return 0;
1926 }
1927 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001928 }
1929
Bryan Wue6b043d2010-03-31 02:10:44 +00001930 fep->mii_timeout = 0;
1931
1932 /*
1933 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001934 *
1935 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1936 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1937 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1938 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001939 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001940 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001941 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1942 fep->phy_speed--;
1943 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001944 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1945
1946 fep->mii_bus = mdiobus_alloc();
1947 if (fep->mii_bus == NULL) {
1948 err = -ENOMEM;
1949 goto err_out;
1950 }
1951
1952 fep->mii_bus->name = "fec_enet_mii_bus";
1953 fep->mii_bus->read = fec_enet_mdio_read;
1954 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001955 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1956 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001957 fep->mii_bus->priv = fep;
1958 fep->mii_bus->parent = &pdev->dev;
1959
1960 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1961 if (!fep->mii_bus->irq) {
1962 err = -ENOMEM;
1963 goto err_out_free_mdiobus;
1964 }
1965
1966 for (i = 0; i < PHY_MAX_ADDR; i++)
1967 fep->mii_bus->irq[i] = PHY_POLL;
1968
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001969 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
1970 if (node) {
1971 err = of_mdiobus_register(fep->mii_bus, node);
1972 of_node_put(node);
1973 } else {
1974 err = mdiobus_register(fep->mii_bus);
1975 }
1976
1977 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00001978 goto err_out_free_mdio_irq;
1979
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001980 mii_cnt++;
1981
Shawn Guob5680e02011-01-05 21:13:13 +00001982 /* save fec0 mii_bus */
1983 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1984 fec0_mii_bus = fep->mii_bus;
1985
Bryan Wue6b043d2010-03-31 02:10:44 +00001986 return 0;
1987
Bryan Wue6b043d2010-03-31 02:10:44 +00001988err_out_free_mdio_irq:
1989 kfree(fep->mii_bus->irq);
1990err_out_free_mdiobus:
1991 mdiobus_free(fep->mii_bus);
1992err_out:
1993 return err;
1994}
1995
1996static void fec_enet_mii_remove(struct fec_enet_private *fep)
1997{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001998 if (--mii_cnt == 0) {
1999 mdiobus_unregister(fep->mii_bus);
2000 kfree(fep->mii_bus->irq);
2001 mdiobus_free(fep->mii_bus);
2002 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002003}
2004
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002005static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002006 struct ethtool_cmd *cmd)
2007{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002008 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002009 struct phy_device *phydev = fep->phy_dev;
2010
2011 if (!phydev)
2012 return -ENODEV;
2013
2014 return phy_ethtool_gset(phydev, cmd);
2015}
2016
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002017static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002018 struct ethtool_cmd *cmd)
2019{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002020 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002021 struct phy_device *phydev = fep->phy_dev;
2022
2023 if (!phydev)
2024 return -ENODEV;
2025
2026 return phy_ethtool_sset(phydev, cmd);
2027}
2028
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002029static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002030 struct ethtool_drvinfo *info)
2031{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002032 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Jiri Pirko7826d432013-01-06 00:44:26 +00002034 strlcpy(info->driver, fep->pdev->dev.driver->name,
2035 sizeof(info->driver));
2036 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2037 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
Bryan Wue6b043d2010-03-31 02:10:44 +00002039
Frank Li5ebae4892013-01-06 16:25:07 +00002040static int fec_enet_get_ts_info(struct net_device *ndev,
2041 struct ethtool_ts_info *info)
2042{
2043 struct fec_enet_private *fep = netdev_priv(ndev);
2044
2045 if (fep->bufdesc_ex) {
2046
2047 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2048 SOF_TIMESTAMPING_RX_SOFTWARE |
2049 SOF_TIMESTAMPING_SOFTWARE |
2050 SOF_TIMESTAMPING_TX_HARDWARE |
2051 SOF_TIMESTAMPING_RX_HARDWARE |
2052 SOF_TIMESTAMPING_RAW_HARDWARE;
2053 if (fep->ptp_clock)
2054 info->phc_index = ptp_clock_index(fep->ptp_clock);
2055 else
2056 info->phc_index = -1;
2057
2058 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2059 (1 << HWTSTAMP_TX_ON);
2060
2061 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2062 (1 << HWTSTAMP_FILTER_ALL);
2063 return 0;
2064 } else {
2065 return ethtool_op_get_ts_info(ndev, info);
2066 }
2067}
2068
Guenter Roeckd1391932013-06-18 10:04:59 -07002069#if !defined(CONFIG_M5272)
2070
Frank Libaa70a52013-01-16 16:55:58 +00002071static void fec_enet_get_pauseparam(struct net_device *ndev,
2072 struct ethtool_pauseparam *pause)
2073{
2074 struct fec_enet_private *fep = netdev_priv(ndev);
2075
2076 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2077 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2078 pause->rx_pause = pause->tx_pause;
2079}
2080
2081static int fec_enet_set_pauseparam(struct net_device *ndev,
2082 struct ethtool_pauseparam *pause)
2083{
2084 struct fec_enet_private *fep = netdev_priv(ndev);
2085
Russell King0b146ca2014-07-08 00:22:59 +01002086 if (!fep->phy_dev)
2087 return -ENODEV;
2088
Frank Libaa70a52013-01-16 16:55:58 +00002089 if (pause->tx_pause != pause->rx_pause) {
2090 netdev_info(ndev,
2091 "hardware only support enable/disable both tx and rx");
2092 return -EINVAL;
2093 }
2094
2095 fep->pause_flag = 0;
2096
2097 /* tx pause must be same as rx pause */
2098 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2099 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2100
2101 if (pause->rx_pause || pause->autoneg) {
2102 fep->phy_dev->supported |= ADVERTISED_Pause;
2103 fep->phy_dev->advertising |= ADVERTISED_Pause;
2104 } else {
2105 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2106 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2107 }
2108
2109 if (pause->autoneg) {
2110 if (netif_running(ndev))
2111 fec_stop(ndev);
2112 phy_start_aneg(fep->phy_dev);
2113 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002114 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002115 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002116 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002117 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002118 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002119 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002120 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002121 }
Frank Libaa70a52013-01-16 16:55:58 +00002122
2123 return 0;
2124}
2125
Chris Healy38ae92d2013-06-25 23:18:52 -07002126static const struct fec_stat {
2127 char name[ETH_GSTRING_LEN];
2128 u16 offset;
2129} fec_stats[] = {
2130 /* RMON TX */
2131 { "tx_dropped", RMON_T_DROP },
2132 { "tx_packets", RMON_T_PACKETS },
2133 { "tx_broadcast", RMON_T_BC_PKT },
2134 { "tx_multicast", RMON_T_MC_PKT },
2135 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2136 { "tx_undersize", RMON_T_UNDERSIZE },
2137 { "tx_oversize", RMON_T_OVERSIZE },
2138 { "tx_fragment", RMON_T_FRAG },
2139 { "tx_jabber", RMON_T_JAB },
2140 { "tx_collision", RMON_T_COL },
2141 { "tx_64byte", RMON_T_P64 },
2142 { "tx_65to127byte", RMON_T_P65TO127 },
2143 { "tx_128to255byte", RMON_T_P128TO255 },
2144 { "tx_256to511byte", RMON_T_P256TO511 },
2145 { "tx_512to1023byte", RMON_T_P512TO1023 },
2146 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2147 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2148 { "tx_octets", RMON_T_OCTETS },
2149
2150 /* IEEE TX */
2151 { "IEEE_tx_drop", IEEE_T_DROP },
2152 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2153 { "IEEE_tx_1col", IEEE_T_1COL },
2154 { "IEEE_tx_mcol", IEEE_T_MCOL },
2155 { "IEEE_tx_def", IEEE_T_DEF },
2156 { "IEEE_tx_lcol", IEEE_T_LCOL },
2157 { "IEEE_tx_excol", IEEE_T_EXCOL },
2158 { "IEEE_tx_macerr", IEEE_T_MACERR },
2159 { "IEEE_tx_cserr", IEEE_T_CSERR },
2160 { "IEEE_tx_sqe", IEEE_T_SQE },
2161 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2162 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2163
2164 /* RMON RX */
2165 { "rx_packets", RMON_R_PACKETS },
2166 { "rx_broadcast", RMON_R_BC_PKT },
2167 { "rx_multicast", RMON_R_MC_PKT },
2168 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2169 { "rx_undersize", RMON_R_UNDERSIZE },
2170 { "rx_oversize", RMON_R_OVERSIZE },
2171 { "rx_fragment", RMON_R_FRAG },
2172 { "rx_jabber", RMON_R_JAB },
2173 { "rx_64byte", RMON_R_P64 },
2174 { "rx_65to127byte", RMON_R_P65TO127 },
2175 { "rx_128to255byte", RMON_R_P128TO255 },
2176 { "rx_256to511byte", RMON_R_P256TO511 },
2177 { "rx_512to1023byte", RMON_R_P512TO1023 },
2178 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2179 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2180 { "rx_octets", RMON_R_OCTETS },
2181
2182 /* IEEE RX */
2183 { "IEEE_rx_drop", IEEE_R_DROP },
2184 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2185 { "IEEE_rx_crc", IEEE_R_CRC },
2186 { "IEEE_rx_align", IEEE_R_ALIGN },
2187 { "IEEE_rx_macerr", IEEE_R_MACERR },
2188 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2189 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2190};
2191
2192static void fec_enet_get_ethtool_stats(struct net_device *dev,
2193 struct ethtool_stats *stats, u64 *data)
2194{
2195 struct fec_enet_private *fep = netdev_priv(dev);
2196 int i;
2197
2198 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2199 data[i] = readl(fep->hwp + fec_stats[i].offset);
2200}
2201
2202static void fec_enet_get_strings(struct net_device *netdev,
2203 u32 stringset, u8 *data)
2204{
2205 int i;
2206 switch (stringset) {
2207 case ETH_SS_STATS:
2208 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2209 memcpy(data + i * ETH_GSTRING_LEN,
2210 fec_stats[i].name, ETH_GSTRING_LEN);
2211 break;
2212 }
2213}
2214
2215static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2216{
2217 switch (sset) {
2218 case ETH_SS_STATS:
2219 return ARRAY_SIZE(fec_stats);
2220 default:
2221 return -EOPNOTSUPP;
2222 }
2223}
Guenter Roeckd1391932013-06-18 10:04:59 -07002224#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002225
Chris Healy32bc9b42013-06-17 07:25:06 -07002226static int fec_enet_nway_reset(struct net_device *dev)
2227{
2228 struct fec_enet_private *fep = netdev_priv(dev);
2229 struct phy_device *phydev = fep->phy_dev;
2230
2231 if (!phydev)
2232 return -ENODEV;
2233
2234 return genphy_restart_aneg(phydev);
2235}
2236
stephen hemminger9b07be42012-01-04 12:59:49 +00002237static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002238 .get_settings = fec_enet_get_settings,
2239 .set_settings = fec_enet_set_settings,
2240 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002241 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002242 .get_link = ethtool_op_get_link,
Chris Healy38ae92d2013-06-25 23:18:52 -07002243#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002244 .get_pauseparam = fec_enet_get_pauseparam,
2245 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002246 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002247 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002248 .get_sset_count = fec_enet_get_sset_count,
2249#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002250 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002251};
2252
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002253static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002254{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002255 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002256 struct phy_device *phydev = fep->phy_dev;
2257
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002258 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002259 return -EINVAL;
2260
2261 if (!phydev)
2262 return -ENODEV;
2263
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002264 if (fep->bufdesc_ex) {
2265 if (cmd == SIOCSHWTSTAMP)
2266 return fec_ptp_set(ndev, rq);
2267 if (cmd == SIOCGHWTSTAMP)
2268 return fec_ptp_get(ndev, rq);
2269 }
Frank Liff43da82013-01-03 16:04:23 +00002270
Richard Cochran28b04112010-07-17 08:48:55 +00002271 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002274static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002275{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002276 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002277 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002278 struct sk_buff *skb;
2279 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002280 struct fec_enet_priv_tx_q *txq;
2281 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f742014-09-13 05:00:50 +08002282 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002283
Frank Li59d0f742014-09-13 05:00:50 +08002284 for (q = 0; q < fep->num_rx_queues; q++) {
2285 rxq = fep->rx_queue[q];
2286 bdp = rxq->rx_bd_base;
2287 for (i = 0; i < rxq->rx_ring_size; i++) {
2288 skb = rxq->rx_skbuff[i];
2289 rxq->rx_skbuff[i] = NULL;
2290 if (skb) {
2291 dma_unmap_single(&fep->pdev->dev,
2292 bdp->cbd_bufaddr,
2293 FEC_ENET_RX_FRSIZE,
2294 DMA_FROM_DEVICE);
2295 dev_kfree_skb(skb);
2296 }
2297 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002298 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002299 }
2300
Frank Li59d0f742014-09-13 05:00:50 +08002301 for (q = 0; q < fep->num_tx_queues; q++) {
2302 txq = fep->tx_queue[q];
2303 bdp = txq->tx_bd_base;
2304 for (i = 0; i < txq->tx_ring_size; i++) {
2305 kfree(txq->tx_bounce[i]);
2306 txq->tx_bounce[i] = NULL;
2307 skb = txq->tx_skbuff[i];
2308 txq->tx_skbuff[i] = NULL;
2309 dev_kfree_skb(skb);
2310 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002311 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002312}
2313
Frank Li59d0f742014-09-13 05:00:50 +08002314static void fec_enet_free_queue(struct net_device *ndev)
2315{
2316 struct fec_enet_private *fep = netdev_priv(ndev);
2317 int i;
2318 struct fec_enet_priv_tx_q *txq;
2319
2320 for (i = 0; i < fep->num_tx_queues; i++)
2321 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2322 txq = fep->tx_queue[i];
2323 dma_free_coherent(NULL,
2324 txq->tx_ring_size * TSO_HEADER_SIZE,
2325 txq->tso_hdrs,
2326 txq->tso_hdrs_dma);
2327 }
2328
2329 for (i = 0; i < fep->num_rx_queues; i++)
2330 if (fep->rx_queue[i])
2331 kfree(fep->rx_queue[i]);
2332
2333 for (i = 0; i < fep->num_tx_queues; i++)
2334 if (fep->tx_queue[i])
2335 kfree(fep->tx_queue[i]);
2336}
2337
2338static int fec_enet_alloc_queue(struct net_device *ndev)
2339{
2340 struct fec_enet_private *fep = netdev_priv(ndev);
2341 int i;
2342 int ret = 0;
2343 struct fec_enet_priv_tx_q *txq;
2344
2345 for (i = 0; i < fep->num_tx_queues; i++) {
2346 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2347 if (!txq) {
2348 ret = -ENOMEM;
2349 goto alloc_failed;
2350 }
2351
2352 fep->tx_queue[i] = txq;
2353 txq->tx_ring_size = TX_RING_SIZE;
2354 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2355
2356 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2357 txq->tx_wake_threshold =
2358 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2359
2360 txq->tso_hdrs = dma_alloc_coherent(NULL,
2361 txq->tx_ring_size * TSO_HEADER_SIZE,
2362 &txq->tso_hdrs_dma,
2363 GFP_KERNEL);
2364 if (!txq->tso_hdrs) {
2365 ret = -ENOMEM;
2366 goto alloc_failed;
2367 }
2368 }
2369
2370 for (i = 0; i < fep->num_rx_queues; i++) {
2371 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2372 GFP_KERNEL);
2373 if (!fep->rx_queue[i]) {
2374 ret = -ENOMEM;
2375 goto alloc_failed;
2376 }
2377
2378 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2379 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2380 }
2381 return ret;
2382
2383alloc_failed:
2384 fec_enet_free_queue(ndev);
2385 return ret;
2386}
2387
2388static int
2389fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002390{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002391 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002392 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002393 struct sk_buff *skb;
2394 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002395 struct fec_enet_priv_rx_q *rxq;
Fugang Duan41ef84c2014-09-13 05:00:54 +08002396 unsigned int off;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002397
Frank Li59d0f742014-09-13 05:00:50 +08002398 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002399 bdp = rxq->rx_bd_base;
2400 for (i = 0; i < rxq->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002401 dma_addr_t addr;
2402
Fabio Estevamb72061a2012-02-07 08:27:31 +00002403 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002404 if (!skb)
2405 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002406
Fugang Duan41ef84c2014-09-13 05:00:54 +08002407 off = ((unsigned long)skb->data) & fep->rx_align;
2408 if (off)
2409 skb_reserve(skb, fep->rx_align + 1 - off);
2410
Russell King730ee362014-07-08 00:23:14 +01002411 addr = dma_map_single(&fep->pdev->dev, skb->data,
Fugang Duan41ef84c2014-09-13 05:00:54 +08002412 FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE);
2413
Russell King730ee362014-07-08 00:23:14 +01002414 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2415 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002416 if (net_ratelimit())
2417 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002418 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002419 }
Russell King730ee362014-07-08 00:23:14 +01002420
Fugang Duan4d494cd2014-09-13 05:00:48 +08002421 rxq->rx_skbuff[i] = skb;
Russell King730ee362014-07-08 00:23:14 +01002422 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002423 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002424
2425 if (fep->bufdesc_ex) {
2426 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2427 ebdp->cbd_esc = BD_ENET_RX_INT;
2428 }
2429
Frank Li59d0f742014-09-13 05:00:50 +08002430 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002431 }
2432
2433 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002434 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002435 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f742014-09-13 05:00:50 +08002436 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002437
Frank Li59d0f742014-09-13 05:00:50 +08002438 err_alloc:
2439 fec_enet_free_buffers(ndev);
2440 return -ENOMEM;
2441}
2442
2443static int
2444fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2445{
2446 struct fec_enet_private *fep = netdev_priv(ndev);
2447 unsigned int i;
2448 struct bufdesc *bdp;
2449 struct fec_enet_priv_tx_q *txq;
2450
2451 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002452 bdp = txq->tx_bd_base;
2453 for (i = 0; i < txq->tx_ring_size; i++) {
2454 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2455 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002456 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002457
2458 bdp->cbd_sc = 0;
2459 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002460
Frank Liff43da82013-01-03 16:04:23 +00002461 if (fep->bufdesc_ex) {
2462 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002463 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002464 }
2465
Frank Li59d0f742014-09-13 05:00:50 +08002466 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002467 }
2468
2469 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002470 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002471 bdp->cbd_sc |= BD_SC_WRAP;
2472
2473 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002474
2475 err_alloc:
2476 fec_enet_free_buffers(ndev);
2477 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002478}
2479
Frank Li59d0f742014-09-13 05:00:50 +08002480static int fec_enet_alloc_buffers(struct net_device *ndev)
2481{
2482 struct fec_enet_private *fep = netdev_priv(ndev);
2483 unsigned int i;
2484
2485 for (i = 0; i < fep->num_rx_queues; i++)
2486 if (fec_enet_alloc_rxq_buffers(ndev, i))
2487 return -ENOMEM;
2488
2489 for (i = 0; i < fep->num_tx_queues; i++)
2490 if (fec_enet_alloc_txq_buffers(ndev, i))
2491 return -ENOMEM;
2492 return 0;
2493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002496fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002498 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002499 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002501 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002502 ret = fec_enet_clk_enable(ndev, true);
2503 if (ret)
2504 return ret;
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 /* I should reset the ring buffers here, but I don't yet know
2507 * a simple way to do that.
2508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002510 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002511 if (ret)
2512 return ret;
2513
Bryan Wu418bd0d2010-05-28 03:40:39 -07002514 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002515 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002516 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002517 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002518 return ret;
2519 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002520
Russell Kingef833372014-07-08 12:40:43 +01002521 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002522 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002523 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002524 netif_tx_start_all_queues(ndev);
2525
Sascha Hauer22f6b862009-04-15 01:32:18 +00002526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002530fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002532 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Russell Kingd76cfae2014-07-08 00:23:04 +01002534 phy_stop(fep->phy_dev);
2535
Russell King31a6de32014-07-08 12:40:23 +01002536 if (netif_device_present(ndev)) {
2537 napi_disable(&fep->napi);
2538 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002539 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Russell King635cf172014-07-08 00:22:54 +01002542 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002543 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002544
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002545 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002546 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002547 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return 0;
2550}
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552/* Set or clear the multicast filter for this adaptor.
2553 * Skeleton taken from sunlance driver.
2554 * The CPM Ethernet implementation allows Multicast as well as individual
2555 * MAC address filtering. Some of the drivers check to make sure it is
2556 * a group multicast address, and discard those that are not. I guess I
2557 * will do the same for now, but just remove the test if you want
2558 * individual filtering as well (do the upper net layers want or support
2559 * this kind of feature?).
2560 */
2561
2562#define HASH_BITS 6 /* #bits in hash */
2563#define CRC32_POLY 0xEDB88320
2564
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002565static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002567 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002568 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002569 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned char hash;
2571
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002572 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002573 tmp = readl(fep->hwp + FEC_R_CNTRL);
2574 tmp |= 0x8;
2575 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002576 return;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Sascha Hauer4e831832009-04-15 01:32:19 +00002579 tmp = readl(fep->hwp + FEC_R_CNTRL);
2580 tmp &= ~0x8;
2581 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002582
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002583 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002584 /* Catch all multicast addresses, so set the
2585 * filter to all 1's
2586 */
2587 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2588 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Sascha Hauer4e831832009-04-15 01:32:19 +00002590 return;
2591 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002592
Sascha Hauer4e831832009-04-15 01:32:19 +00002593 /* Clear filter and add the addresses in hash register
2594 */
2595 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2596 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002598 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002599 /* calculate crc32 value of mac address */
2600 crc = 0xffffffff;
2601
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002602 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002603 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002604 for (bit = 0; bit < 8; bit++, data >>= 1) {
2605 crc = (crc >> 1) ^
2606 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002609
2610 /* only upper 6 bits (HASH_BITS) are used
2611 * which point to specific bit in he hash registers
2612 */
2613 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2614
2615 if (hash > 31) {
2616 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2617 tmp |= 1 << (hash - 32);
2618 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2619 } else {
2620 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2621 tmp |= 1 << hash;
2622 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
2625}
2626
Sascha Hauer22f6b862009-04-15 01:32:18 +00002627/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002628static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002629fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002631 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002632 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Lucas Stach44934fa2014-03-30 21:32:08 +02002634 if (addr) {
2635 if (!is_valid_ether_addr(addr->sa_data))
2636 return -EADDRNOTAVAIL;
2637 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2638 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002639
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002640 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2641 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002642 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002643 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002644 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002648#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002649/**
2650 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002651 * @dev: The FEC network adapter
2652 *
2653 * Polled functionality used by netconsole and others in non interrupt mode
2654 *
2655 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002656static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002657{
2658 int i;
2659 struct fec_enet_private *fep = netdev_priv(dev);
2660
2661 for (i = 0; i < FEC_IRQ_NUM; i++) {
2662 if (fep->irq[i] > 0) {
2663 disable_irq(fep->irq[i]);
2664 fec_enet_interrupt(fep->irq[i], dev);
2665 enable_irq(fep->irq[i]);
2666 }
2667 }
2668}
2669#endif
2670
Russell King8506fa12014-07-08 12:40:33 +01002671#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2672
Jim Baxter4c09eed2013-04-19 08:10:49 +00002673static int fec_set_features(struct net_device *netdev,
2674 netdev_features_t features)
2675{
2676 struct fec_enet_private *fep = netdev_priv(netdev);
2677 netdev_features_t changed = features ^ netdev->features;
2678
Russell King8506fa12014-07-08 12:40:33 +01002679 /* Quiesce the device if necessary */
2680 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2681 napi_disable(&fep->napi);
2682 netif_tx_lock_bh(netdev);
2683 fec_stop(netdev);
2684 }
2685
Jim Baxter4c09eed2013-04-19 08:10:49 +00002686 netdev->features = features;
2687
2688 /* Receive checksum has been changed */
2689 if (changed & NETIF_F_RXCSUM) {
2690 if (features & NETIF_F_RXCSUM)
2691 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2692 else
2693 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002694 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002695
Russell King8506fa12014-07-08 12:40:33 +01002696 /* Resume the device after updates */
2697 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002698 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002699 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002700 netif_tx_unlock_bh(netdev);
2701 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002702 }
2703
2704 return 0;
2705}
2706
Fugang Duan4d494cd2014-09-13 05:00:48 +08002707u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
2708 void *accel_priv, select_queue_fallback_t fallback)
2709{
2710 return skb_tx_hash(ndev, skb);
2711}
2712
Sascha Hauer009fda82009-04-15 01:32:23 +00002713static const struct net_device_ops fec_netdev_ops = {
2714 .ndo_open = fec_enet_open,
2715 .ndo_stop = fec_enet_close,
2716 .ndo_start_xmit = fec_enet_start_xmit,
Fugang Duan4d494cd2014-09-13 05:00:48 +08002717 .ndo_select_queue = fec_enet_select_queue,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002718 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002719 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002720 .ndo_validate_addr = eth_validate_addr,
2721 .ndo_tx_timeout = fec_timeout,
2722 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002723 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002724#ifdef CONFIG_NET_POLL_CONTROLLER
2725 .ndo_poll_controller = fec_poll_controller,
2726#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002727 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002728};
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /*
2731 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002734static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002736 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002737 const struct platform_device_id *id_entry =
2738 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002739 struct fec_enet_priv_tx_q *txq;
2740 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002741 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002742 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002743 int bd_size;
Frank Li59d0f742014-09-13 05:00:50 +08002744 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002745
Fugang Duan41ef84c2014-09-13 05:00:54 +08002746#if defined(CONFIG_ARM)
2747 fep->rx_align = 0xf;
2748 fep->tx_align = 0xf;
2749#else
2750 fep->rx_align = 0x3;
2751 fep->tx_align = 0x3;
2752#endif
2753
Frank Li59d0f742014-09-13 05:00:50 +08002754 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002755
Nimrod Andy55d02182014-06-12 08:16:21 +08002756 if (fep->bufdesc_ex)
2757 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2758 else
2759 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002760 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002761 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002763 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002764 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002765 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002766 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002767 return -ENOMEM;
2768 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002769
Fugang Duan4d494cd2014-09-13 05:00:48 +08002770 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Shawn Guo49da97d2011-01-05 21:13:11 +00002772 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002773 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002774 /* make sure MAC we just acquired is programmed into the hw */
2775 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002777 /* Set receive and transmit descriptor base. */
Frank Li59d0f742014-09-13 05:00:50 +08002778 for (i = 0; i < fep->num_rx_queues; i++) {
2779 rxq = fep->rx_queue[i];
2780 rxq->index = i;
2781 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2782 rxq->bd_dma = bd_dma;
2783 if (fep->bufdesc_ex) {
2784 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2785 cbd_base = (struct bufdesc *)
2786 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2787 } else {
2788 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2789 cbd_base += rxq->rx_ring_size;
2790 }
2791 }
2792
2793 for (i = 0; i < fep->num_tx_queues; i++) {
2794 txq = fep->tx_queue[i];
2795 txq->index = i;
2796 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2797 txq->bd_dma = bd_dma;
2798 if (fep->bufdesc_ex) {
2799 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
2800 cbd_base = (struct bufdesc *)
2801 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
2802 } else {
2803 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
2804 cbd_base += txq->tx_ring_size;
2805 }
2806 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Sascha Hauer22f6b862009-04-15 01:32:18 +00002809 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002810 ndev->watchdog_timeo = TX_TIMEOUT;
2811 ndev->netdev_ops = &fec_netdev_ops;
2812 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002813
Frank Lidc975382013-01-28 18:31:42 +00002814 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002815 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002816
Nimrod Andy09d1e542014-06-12 08:16:20 +08002817 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002818 /* enable hw VLAN support */
2819 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002820
Shawn Guo48496252013-05-08 21:08:22 +00002821 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002822 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2823
Shawn Guo48496252013-05-08 21:08:22 +00002824 /* enable hw accelerator */
2825 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002826 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002827 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2828 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002829
Fugang Duan41ef84c2014-09-13 05:00:54 +08002830 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
2831 fep->tx_align = 0;
2832 fep->rx_align = 0x3f;
2833 }
2834
Nimrod Andy09d1e542014-06-12 08:16:20 +08002835 ndev->hw_features = ndev->features;
2836
Russell Kingef833372014-07-08 12:40:43 +01002837 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
2840}
2841
Shawn Guoca2cc332011-06-25 02:04:35 +08002842#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002843static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002844{
2845 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002846 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002847 struct device_node *np = pdev->dev.of_node;
2848
2849 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002850 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002851
Shawn Guoa3caad02012-06-27 03:45:24 +00002852 of_property_read_u32(np, "phy-reset-duration", &msec);
2853 /* A sane reset duration should not be longer than 1s */
2854 if (msec > 1000)
2855 msec = 1;
2856
Shawn Guoca2cc332011-06-25 02:04:35 +08002857 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002858 if (!gpio_is_valid(phy_reset))
2859 return;
2860
Shawn Guo119fc002012-06-27 03:45:22 +00002861 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2862 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002863 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002864 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002865 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002866 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002867 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002868 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002869}
2870#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002871static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002872{
2873 /*
2874 * In case of platform probe, the reset has been done
2875 * by machine code.
2876 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002877}
2878#endif /* CONFIG_OF */
2879
Fugang Duan9fc095f2014-09-13 05:00:49 +08002880static void
2881fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
2882{
2883 struct device_node *np = pdev->dev.of_node;
2884 int err;
2885
2886 *num_tx = *num_rx = 1;
2887
2888 if (!np || !of_device_is_available(np))
2889 return;
2890
2891 /* parse the num of tx and rx queues */
2892 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
2893 err |= of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
2894 if (err) {
2895 *num_tx = 1;
2896 *num_rx = 1;
2897 return;
2898 }
2899
2900 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
2901 dev_err(&pdev->dev, "Invalidate num_tx(=%d), fail back to 1\n",
2902 *num_tx);
2903 *num_tx = 1;
2904 return;
2905 }
2906
2907 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
2908 dev_err(&pdev->dev, "Invalidate num_rx(=%d), fail back to 1\n",
2909 *num_rx);
2910 *num_rx = 1;
2911 return;
2912 }
2913
2914}
2915
Bill Pemberton33897cc2012-12-03 09:23:58 -05002916static int
Sascha Haueread73182009-01-28 23:03:11 +00002917fec_probe(struct platform_device *pdev)
2918{
2919 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002920 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002921 struct net_device *ndev;
2922 int i, irq, ret = 0;
2923 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002924 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002925 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002926 struct device_node *np = pdev->dev.of_node, *phy_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08002927 int num_tx_qs = 1;
2928 int num_rx_qs = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002929
2930 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2931 if (of_id)
2932 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002933
Fugang Duan9fc095f2014-09-13 05:00:49 +08002934 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
2935
Sascha Haueread73182009-01-28 23:03:11 +00002936 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08002937 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
2938 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00002939 if (!ndev)
2940 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002941
2942 SET_NETDEV_DEV(ndev, &pdev->dev);
2943
2944 /* setup board info structure */
2945 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002946
Fugang Duan9fc095f2014-09-13 05:00:49 +08002947 fep->num_rx_queues = num_rx_qs;
2948 fep->num_tx_queues = num_tx_qs;
2949
Guenter Roeckd1391932013-06-18 10:04:59 -07002950#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002951 /* default enable pause frame auto negotiation */
2952 if (pdev->id_entry &&
2953 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2954 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002955#endif
Frank Libaa70a52013-01-16 16:55:58 +00002956
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002957 /* Select default pin state */
2958 pinctrl_pm_select_default_state(&pdev->dev);
2959
Fabio Estevam399db752013-07-21 13:25:03 -03002960 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302961 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2962 if (IS_ERR(fep->hwp)) {
2963 ret = PTR_ERR(fep->hwp);
2964 goto failed_ioremap;
2965 }
2966
Bryan Wue6b043d2010-03-31 02:10:44 +00002967 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002968 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002969
Frank Liff43da82013-01-03 16:04:23 +00002970 fep->bufdesc_ex = 0;
2971
Sascha Haueread73182009-01-28 23:03:11 +00002972 platform_set_drvdata(pdev, ndev);
2973
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002974 phy_node = of_parse_phandle(np, "phy-handle", 0);
2975 if (!phy_node && of_phy_is_fixed_link(np)) {
2976 ret = of_phy_register_fixed_link(np);
2977 if (ret < 0) {
2978 dev_err(&pdev->dev,
2979 "broken fixed-link specification\n");
2980 goto failed_phy;
2981 }
2982 phy_node = of_node_get(np);
2983 }
2984 fep->phy_node = phy_node;
2985
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002986 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002987 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002988 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002989 if (pdata)
2990 fep->phy_interface = pdata->phy;
2991 else
2992 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2993 } else {
2994 fep->phy_interface = ret;
2995 }
2996
Fabio Estevame2f8d552013-02-22 06:40:45 +00002997 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2998 if (IS_ERR(fep->clk_ipg)) {
2999 ret = PTR_ERR(fep->clk_ipg);
3000 goto failed_clk;
3001 }
3002
3003 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3004 if (IS_ERR(fep->clk_ahb)) {
3005 ret = PTR_ERR(fep->clk_ahb);
3006 goto failed_clk;
3007 }
3008
Linus Torvalds38f56f32013-05-07 11:06:17 -07003009 /* enet_out is optional, depends on board */
3010 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3011 if (IS_ERR(fep->clk_enet_out))
3012 fep->clk_enet_out = NULL;
3013
Nimrod Andy91c0d982014-08-21 17:09:38 +08003014 fep->ptp_clk_on = false;
3015 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003016
3017 /* clk_ref is optional, depends on board */
3018 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3019 if (IS_ERR(fep->clk_ref))
3020 fep->clk_ref = NULL;
3021
Fabio Estevame2f8d552013-02-22 06:40:45 +00003022 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003023 fep->bufdesc_ex =
3024 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003025 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003026 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003027 fep->bufdesc_ex = 0;
3028 }
3029
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003030 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003031 if (ret)
3032 goto failed_clk;
3033
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003034 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3035 if (!IS_ERR(fep->reg_phy)) {
3036 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003037 if (ret) {
3038 dev_err(&pdev->dev,
3039 "Failed to enable phy regulator: %d\n", ret);
3040 goto failed_regulator;
3041 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003042 } else {
3043 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003044 }
3045
3046 fec_reset_phy(pdev);
3047
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003048 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003049 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003050
3051 ret = fec_enet_init(ndev);
3052 if (ret)
3053 goto failed_init;
3054
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003055 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003056 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003057 if (irq < 0) {
3058 if (i)
3059 break;
3060 ret = irq;
3061 goto failed_irq;
3062 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003063 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003064 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003065 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003066 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003067 }
3068
Fugang Duanb4d39b52014-09-13 05:00:55 +08003069 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00003070 ret = fec_enet_mii_init(pdev);
3071 if (ret)
3072 goto failed_mii_init;
3073
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003074 /* Carrier starts down, phylib will bring it up */
3075 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003076 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003077 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003078
Sascha Haueread73182009-01-28 23:03:11 +00003079 ret = register_netdev(ndev);
3080 if (ret)
3081 goto failed_register;
3082
Fabio Estevameb1d0642013-04-13 07:25:36 +00003083 if (fep->bufdesc_ex && fep->ptp_clock)
3084 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3085
Russell King36cdc742014-07-08 13:01:44 +01003086 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003087 return 0;
3088
3089failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003090 fec_enet_mii_remove(fep);
3091failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003092failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003093failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003094 if (fep->reg_phy)
3095 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003096failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003097 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003098failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003099failed_phy:
3100 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003101failed_ioremap:
3102 free_netdev(ndev);
3103
3104 return ret;
3105}
3106
Bill Pemberton33897cc2012-12-03 09:23:58 -05003107static int
Sascha Haueread73182009-01-28 23:03:11 +00003108fec_drv_remove(struct platform_device *pdev)
3109{
3110 struct net_device *ndev = platform_get_drvdata(pdev);
3111 struct fec_enet_private *fep = netdev_priv(ndev);
3112
Nimrod Andy91c0d982014-08-21 17:09:38 +08003113 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003114 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003115 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003116 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003117 if (fep->reg_phy)
3118 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003119 if (fep->ptp_clock)
3120 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003121 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003122 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003123 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003124
Sascha Haueread73182009-01-28 23:03:11 +00003125 return 0;
3126}
3127
Fabio Estevamdd66d382014-07-24 18:24:01 -03003128static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003129{
Eric Benard87cad5c2010-06-18 04:19:54 +00003130 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003131 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003132
Russell Kingda1774e2014-07-08 12:39:57 +01003133 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003134 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003135 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003136 napi_disable(&fep->napi);
3137 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003138 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003139 netif_tx_unlock_bh(ndev);
3140 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003141 }
Russell Kingda1774e2014-07-08 12:39:57 +01003142 rtnl_unlock();
3143
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003144 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003145 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003146
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003147 if (fep->reg_phy)
3148 regulator_disable(fep->reg_phy);
3149
Sascha Haueread73182009-01-28 23:03:11 +00003150 return 0;
3151}
3152
Fabio Estevamdd66d382014-07-24 18:24:01 -03003153static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003154{
Eric Benard87cad5c2010-06-18 04:19:54 +00003155 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003156 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003157 int ret;
3158
3159 if (fep->reg_phy) {
3160 ret = regulator_enable(fep->reg_phy);
3161 if (ret)
3162 return ret;
3163 }
Sascha Haueread73182009-01-28 23:03:11 +00003164
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003165 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003166 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003167 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003168 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003169
Russell Kingda1774e2014-07-08 12:39:57 +01003170 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003171 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003172 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003173 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003174 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003175 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003176 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003177 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003178 }
Russell Kingda1774e2014-07-08 12:39:57 +01003179 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003180
Sascha Haueread73182009-01-28 23:03:11 +00003181 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003182
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003183failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003184 if (fep->reg_phy)
3185 regulator_disable(fep->reg_phy);
3186 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003187}
3188
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003189static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003190
Sascha Haueread73182009-01-28 23:03:11 +00003191static struct platform_driver fec_driver = {
3192 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003193 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003194 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003195 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003196 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003197 },
Shawn Guob5680e02011-01-05 21:13:13 +00003198 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003199 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003200 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003201};
3202
Fabio Estevamaaca2372012-01-23 16:44:50 +00003203module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003205MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206MODULE_LICENSE("GPL");