Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx. |
| 3 | * Copyright (c) 1997 Dan Malek (dmalek@jlc.net) |
| 4 | * |
Greg Ungerer | 7dd6a2a | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 5 | * Right now, I am very wasteful with the buffers. I allocate memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * 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 Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 15 | * Support for FEC controller of ColdFire processors. |
| 16 | * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com) |
Greg Ungerer | 7dd6a2a | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 17 | * |
| 18 | * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be) |
Philippe De Muyter | 677177c | 2006-06-27 13:05:33 +1000 | [diff] [blame] | 19 | * Copyright (c) 2004-2006 Macq Electronique SA. |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 20 | * |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 21 | * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/delay.h> |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <linux/etherdevice.h> |
| 35 | #include <linux/skbuff.h> |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 36 | #include <linux/in.h> |
| 37 | #include <linux/ip.h> |
| 38 | #include <net/ip.h> |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 39 | #include <net/tso.h> |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 40 | #include <linux/tcp.h> |
| 41 | #include <linux/udp.h> |
| 42 | #include <linux/icmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/spinlock.h> |
| 44 | #include <linux/workqueue.h> |
| 45 | #include <linux/bitops.h> |
Sascha Hauer | 6f501b1 | 2009-01-28 23:03:05 +0000 | [diff] [blame] | 46 | #include <linux/io.h> |
| 47 | #include <linux/irq.h> |
Sascha Hauer | 196719e | 2009-01-28 23:03:10 +0000 | [diff] [blame] | 48 | #include <linux/clk.h> |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 49 | #include <linux/platform_device.h> |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 50 | #include <linux/phy.h> |
Baruch Siach | 5eb32bd | 2010-05-24 00:36:13 -0700 | [diff] [blame] | 51 | #include <linux/fec.h> |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 52 | #include <linux/of.h> |
| 53 | #include <linux/of_device.h> |
| 54 | #include <linux/of_gpio.h> |
| 55 | #include <linux/of_net.h> |
Shawn Guo | 5fa9c0f | 2012-06-27 03:45:21 +0000 | [diff] [blame] | 56 | #include <linux/regulator/consumer.h> |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 57 | #include <linux/if_vlan.h> |
Fabio Estevam | a68ab98 | 2014-06-02 15:44:30 -0300 | [diff] [blame] | 58 | #include <linux/pinctrl/consumer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Greg Ungerer | 080853a | 2007-07-30 16:28:46 +1000 | [diff] [blame] | 60 | #include <asm/cacheflush.h> |
Sascha Hauer | 196719e | 2009-01-28 23:03:10 +0000 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #include "fec.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Christoph Müllner | 772e42b | 2013-06-27 21:18:23 +0200 | [diff] [blame] | 64 | static void set_multicast_list(struct net_device *ndev); |
| 65 | |
Uwe Kleine-König | 085e79e | 2011-01-17 09:52:18 +0100 | [diff] [blame] | 66 | #if defined(CONFIG_ARM) |
Sascha Hauer | 196719e | 2009-01-28 23:03:10 +0000 | [diff] [blame] | 67 | #define FEC_ALIGNMENT 0xf |
| 68 | #else |
| 69 | #define FEC_ALIGNMENT 0x3 |
| 70 | #endif |
| 71 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 72 | #define DRIVER_NAME "fec" |
| 73 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 74 | /* Pause frame feild and FIFO threshold */ |
| 75 | #define FEC_ENET_FCE (1 << 5) |
| 76 | #define FEC_ENET_RSEM_V 0x84 |
| 77 | #define FEC_ENET_RSFL_V 16 |
| 78 | #define FEC_ENET_RAEM_V 0x8 |
| 79 | #define FEC_ENET_RAFL_V 0x8 |
| 80 | #define FEC_ENET_OPD_V 0xFFF0 |
| 81 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 82 | /* Controller is ENET-MAC */ |
| 83 | #define FEC_QUIRK_ENET_MAC (1 << 0) |
| 84 | /* Controller needs driver to swap frame */ |
| 85 | #define FEC_QUIRK_SWAP_FRAME (1 << 1) |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 86 | /* Controller uses gasket */ |
| 87 | #define FEC_QUIRK_USE_GASKET (1 << 2) |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 88 | /* Controller has GBIT support */ |
| 89 | #define FEC_QUIRK_HAS_GBIT (1 << 3) |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 90 | /* Controller has extend desc buffer */ |
| 91 | #define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4) |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 92 | /* Controller has hardware checksum support */ |
| 93 | #define FEC_QUIRK_HAS_CSUM (1 << 5) |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 94 | /* Controller has hardware vlan support */ |
| 95 | #define FEC_QUIRK_HAS_VLAN (1 << 6) |
Frank Li | 0319165 | 2013-07-25 14:05:53 +0800 | [diff] [blame] | 96 | /* ENET IP errata ERR006358 |
| 97 | * |
| 98 | * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously |
| 99 | * detected as not set during a prior frame transmission, then the |
| 100 | * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs |
| 101 | * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in |
Frank Li | 0319165 | 2013-07-25 14:05:53 +0800 | [diff] [blame] | 102 | * frames not being transmitted until there is a 0-to-1 transition on |
| 103 | * ENET_TDAR[TDAR]. |
| 104 | */ |
| 105 | #define FEC_QUIRK_ERR006358 (1 << 7) |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 106 | |
| 107 | static struct platform_device_id fec_devtype[] = { |
| 108 | { |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 109 | /* keep it for coldfire */ |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 110 | .name = DRIVER_NAME, |
| 111 | .driver_data = 0, |
| 112 | }, { |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 113 | .name = "imx25-fec", |
| 114 | .driver_data = FEC_QUIRK_USE_GASKET, |
| 115 | }, { |
| 116 | .name = "imx27-fec", |
| 117 | .driver_data = 0, |
| 118 | }, { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 119 | .name = "imx28-fec", |
| 120 | .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME, |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 121 | }, { |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 122 | .name = "imx6q-fec", |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 123 | .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 124 | FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | |
Frank Li | 0319165 | 2013-07-25 14:05:53 +0800 | [diff] [blame] | 125 | FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 126 | }, { |
Shawn Guo | 3680354 | 2013-05-19 04:38:46 +0000 | [diff] [blame] | 127 | .name = "mvf600-fec", |
Jingchang Lu | ca7c4a4 | 2013-04-11 21:12:45 +0000 | [diff] [blame] | 128 | .driver_data = FEC_QUIRK_ENET_MAC, |
| 129 | }, { |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 130 | /* sentinel */ |
| 131 | } |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 132 | }; |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 133 | MODULE_DEVICE_TABLE(platform, fec_devtype); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 134 | |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 135 | enum imx_fec_type { |
Lothar Waßmann | a7dd321 | 2011-12-07 21:59:25 +0000 | [diff] [blame] | 136 | IMX25_FEC = 1, /* runs on i.mx25/50/53 */ |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 137 | IMX27_FEC, /* runs on i.mx27/35/51 */ |
| 138 | IMX28_FEC, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 139 | IMX6Q_FEC, |
Shawn Guo | 3680354 | 2013-05-19 04:38:46 +0000 | [diff] [blame] | 140 | MVF600_FEC, |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static const struct of_device_id fec_dt_ids[] = { |
| 144 | { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, |
| 145 | { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, |
| 146 | { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 147 | { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, |
Shawn Guo | 3680354 | 2013-05-19 04:38:46 +0000 | [diff] [blame] | 148 | { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 149 | { /* sentinel */ } |
| 150 | }; |
| 151 | MODULE_DEVICE_TABLE(of, fec_dt_ids); |
| 152 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 153 | static unsigned char macaddr[ETH_ALEN]; |
| 154 | module_param_array(macaddr, byte, NULL, 0); |
| 155 | MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); |
| 156 | |
Greg Ungerer | 87f4abb | 2008-06-06 15:55:36 +1000 | [diff] [blame] | 157 | #if defined(CONFIG_M5272) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* |
| 159 | * Some hardware gets it MAC address out of local flash memory. |
| 160 | * if this is non-zero then assume it is the address to get MAC from. |
| 161 | */ |
| 162 | #if defined(CONFIG_NETtel) |
| 163 | #define FEC_FLASHMAC 0xf0006006 |
| 164 | #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) |
| 165 | #define FEC_FLASHMAC 0xf0006000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | #elif defined(CONFIG_CANCam) |
| 167 | #define FEC_FLASHMAC 0xf0020000 |
Greg Ungerer | 7dd6a2a | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 168 | #elif defined (CONFIG_M5272C3) |
| 169 | #define FEC_FLASHMAC (0xffe04000 + 4) |
| 170 | #elif defined(CONFIG_MOD5272) |
Lothar Waßmann | a7dd321 | 2011-12-07 21:59:25 +0000 | [diff] [blame] | 171 | #define FEC_FLASHMAC 0xffc0406b |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | #else |
| 173 | #define FEC_FLASHMAC 0 |
| 174 | #endif |
Greg Ungerer | 43be636 | 2009-02-26 22:42:51 -0800 | [diff] [blame] | 175 | #endif /* CONFIG_M5272 */ |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 176 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 177 | /* Interrupt events/masks. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */ |
| 179 | #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */ |
| 180 | #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */ |
| 181 | #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */ |
| 182 | #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */ |
| 183 | #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */ |
| 184 | #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */ |
| 185 | #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */ |
| 186 | #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */ |
| 187 | #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */ |
| 188 | |
Wolfram Sang | 4bee1f9 | 2010-07-21 02:51:13 +0000 | [diff] [blame] | 189 | #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII) |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 190 | #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF)) |
Wolfram Sang | 4bee1f9 | 2010-07-21 02:51:13 +0000 | [diff] [blame] | 191 | |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 192 | /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | */ |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 194 | #define PKT_MAXBUF_SIZE 1522 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | #define PKT_MINBUF_SIZE 64 |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 196 | #define PKT_MAXBLR_SIZE 1536 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 198 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* |
Matt Waddel | 6b26529 | 2006-06-27 13:10:56 +1000 | [diff] [blame] | 204 | * The 5270/5271/5280/5282/532x RX control register also contains maximum frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * size bits. Other FEC hardware does not, so we need to take that into |
| 206 | * account when setting it. |
| 207 | */ |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 208 | #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ |
Uwe Kleine-König | 085e79e | 2011-01-17 09:52:18 +0100 | [diff] [blame] | 209 | defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) |
| 211 | #else |
| 212 | #define OPT_FRAME_SIZE 0 |
| 213 | #endif |
| 214 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 215 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Rogerio Pimentel | c3b084c | 2011-12-27 14:07:37 -0500 | [diff] [blame] | 224 | #define FEC_MII_TIMEOUT 30000 /* us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 226 | /* Transmitter timeout */ |
| 227 | #define TX_TIMEOUT (2 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 229 | #define FEC_PAUSE_FLAG_AUTONEG 0x1 |
| 230 | #define FEC_PAUSE_FLAG_ENABLE 0x2 |
| 231 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 232 | #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ßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 241 | static int mii_cnt; |
| 242 | |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 243 | static inline |
| 244 | struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep) |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 245 | { |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 246 | struct bufdesc *new_bd = bdp + 1; |
| 247 | struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1; |
| 248 | struct bufdesc_ex *ex_base; |
| 249 | struct bufdesc *base; |
| 250 | int ring_size; |
| 251 | |
| 252 | if (bdp >= fep->tx_bd_base) { |
| 253 | base = fep->tx_bd_base; |
| 254 | ring_size = fep->tx_ring_size; |
| 255 | ex_base = (struct bufdesc_ex *)fep->tx_bd_base; |
| 256 | } else { |
| 257 | base = fep->rx_bd_base; |
| 258 | ring_size = fep->rx_ring_size; |
| 259 | ex_base = (struct bufdesc_ex *)fep->rx_bd_base; |
| 260 | } |
| 261 | |
| 262 | if (fep->bufdesc_ex) |
| 263 | return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ? |
| 264 | ex_base : ex_new_bd); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 265 | else |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 266 | return (new_bd >= (base + ring_size)) ? |
| 267 | base : new_bd; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 270 | static inline |
| 271 | struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep) |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 272 | { |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 273 | struct bufdesc *new_bd = bdp - 1; |
| 274 | struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1; |
| 275 | struct bufdesc_ex *ex_base; |
| 276 | struct bufdesc *base; |
| 277 | int ring_size; |
| 278 | |
| 279 | if (bdp >= fep->tx_bd_base) { |
| 280 | base = fep->tx_bd_base; |
| 281 | ring_size = fep->tx_ring_size; |
| 282 | ex_base = (struct bufdesc_ex *)fep->tx_bd_base; |
| 283 | } else { |
| 284 | base = fep->rx_bd_base; |
| 285 | ring_size = fep->rx_ring_size; |
| 286 | ex_base = (struct bufdesc_ex *)fep->rx_bd_base; |
| 287 | } |
| 288 | |
| 289 | if (fep->bufdesc_ex) |
| 290 | return (struct bufdesc *)((ex_new_bd < ex_base) ? |
| 291 | (ex_new_bd + ring_size) : ex_new_bd); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 292 | else |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 293 | return (new_bd < base) ? (new_bd + ring_size) : new_bd; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 296 | static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp, |
| 297 | struct fec_enet_private *fep) |
| 298 | { |
| 299 | return ((const char *)bdp - (const char *)base) / fep->bufdesc_size; |
| 300 | } |
| 301 | |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 302 | static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep) |
| 303 | { |
| 304 | int entries; |
| 305 | |
| 306 | entries = ((const char *)fep->dirty_tx - |
| 307 | (const char *)fep->cur_tx) / fep->bufdesc_size - 1; |
| 308 | |
| 309 | return entries > 0 ? entries : entries + fep->tx_ring_size; |
| 310 | } |
| 311 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 312 | static void *swap_buffer(void *bufaddr, int len) |
| 313 | { |
| 314 | int i; |
| 315 | unsigned int *buf = bufaddr; |
| 316 | |
Fabio Estevam | ffed61e | 2013-05-21 05:44:26 +0000 | [diff] [blame] | 317 | for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++) |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 318 | *buf = cpu_to_be32(*buf); |
| 319 | |
| 320 | return bufaddr; |
| 321 | } |
| 322 | |
Fugang Duan | 62a02c9 | 2014-06-18 08:33:52 +0800 | [diff] [blame] | 323 | static inline bool is_ipv4_pkt(struct sk_buff *skb) |
| 324 | { |
| 325 | return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4; |
| 326 | } |
| 327 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 328 | static int |
| 329 | fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) |
| 330 | { |
| 331 | /* Only run for packets requiring a checksum. */ |
| 332 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 333 | return 0; |
| 334 | |
| 335 | if (unlikely(skb_cow_head(skb, 0))) |
| 336 | return -1; |
| 337 | |
Fugang Duan | 62a02c9 | 2014-06-18 08:33:52 +0800 | [diff] [blame] | 338 | if (is_ipv4_pkt(skb)) |
| 339 | ip_hdr(skb)->check = 0; |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 340 | *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 345 | static void |
| 346 | fec_enet_submit_work(struct bufdesc *bdp, struct fec_enet_private *fep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 348 | const struct platform_device_id *id_entry = |
| 349 | platform_get_device_id(fep->pdev); |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 350 | struct bufdesc *bdp_pre; |
Duan Fugang-B38611 | fb8ef78 | 2014-02-20 18:14:39 +0800 | [diff] [blame] | 351 | |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 352 | bdp_pre = fec_enet_get_prevdesc(bdp, fep); |
Frank Li | 0319165 | 2013-07-25 14:05:53 +0800 | [diff] [blame] | 353 | if ((id_entry->driver_data & FEC_QUIRK_ERR006358) && |
| 354 | !(bdp_pre->cbd_sc & BD_ENET_TX_READY)) { |
| 355 | fep->delay_work.trig_tx = true; |
| 356 | schedule_delayed_work(&(fep->delay_work.delay_work), |
| 357 | msecs_to_jiffies(1)); |
| 358 | } |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static int |
| 362 | fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev) |
| 363 | { |
| 364 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 365 | const struct platform_device_id *id_entry = |
| 366 | platform_get_device_id(fep->pdev); |
| 367 | struct bufdesc *bdp = fep->cur_tx; |
| 368 | struct bufdesc_ex *ebdp; |
| 369 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 370 | int frag, frag_len; |
| 371 | unsigned short status; |
| 372 | unsigned int estatus = 0; |
| 373 | skb_frag_t *this_frag; |
| 374 | unsigned int index; |
| 375 | void *bufaddr; |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 376 | dma_addr_t addr; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 377 | int i; |
| 378 | |
| 379 | for (frag = 0; frag < nr_frags; frag++) { |
| 380 | this_frag = &skb_shinfo(skb)->frags[frag]; |
| 381 | bdp = fec_enet_get_nextdesc(bdp, fep); |
| 382 | ebdp = (struct bufdesc_ex *)bdp; |
| 383 | |
| 384 | status = bdp->cbd_sc; |
| 385 | status &= ~BD_ENET_TX_STATS; |
| 386 | status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); |
| 387 | frag_len = skb_shinfo(skb)->frags[frag].size; |
| 388 | |
| 389 | /* Handle the last BD specially */ |
| 390 | if (frag == nr_frags - 1) { |
| 391 | status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); |
| 392 | if (fep->bufdesc_ex) { |
| 393 | estatus |= BD_ENET_TX_INT; |
| 394 | if (unlikely(skb_shinfo(skb)->tx_flags & |
| 395 | SKBTX_HW_TSTAMP && fep->hwts_tx_en)) |
| 396 | estatus |= BD_ENET_TX_TS; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (fep->bufdesc_ex) { |
| 401 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 402 | estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; |
| 403 | ebdp->cbd_bdu = 0; |
| 404 | ebdp->cbd_esc = estatus; |
| 405 | } |
| 406 | |
| 407 | bufaddr = page_address(this_frag->page.p) + this_frag->page_offset; |
| 408 | |
| 409 | index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep); |
| 410 | if (((unsigned long) bufaddr) & FEC_ALIGNMENT || |
| 411 | id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { |
| 412 | memcpy(fep->tx_bounce[index], bufaddr, frag_len); |
| 413 | bufaddr = fep->tx_bounce[index]; |
| 414 | |
| 415 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 416 | swap_buffer(bufaddr, frag_len); |
| 417 | } |
| 418 | |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 419 | addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len, |
| 420 | DMA_TO_DEVICE); |
| 421 | if (dma_mapping_error(&fep->pdev->dev, addr)) { |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 422 | dev_kfree_skb_any(skb); |
| 423 | if (net_ratelimit()) |
| 424 | netdev_err(ndev, "Tx DMA memory map failed\n"); |
| 425 | goto dma_mapping_error; |
| 426 | } |
| 427 | |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 428 | bdp->cbd_bufaddr = addr; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 429 | bdp->cbd_datlen = frag_len; |
| 430 | bdp->cbd_sc = status; |
| 431 | } |
| 432 | |
| 433 | fep->cur_tx = bdp; |
| 434 | |
| 435 | return 0; |
| 436 | |
| 437 | dma_mapping_error: |
| 438 | bdp = fep->cur_tx; |
| 439 | for (i = 0; i < frag; i++) { |
| 440 | bdp = fec_enet_get_nextdesc(bdp, fep); |
| 441 | dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, |
| 442 | bdp->cbd_datlen, DMA_TO_DEVICE); |
| 443 | } |
| 444 | return NETDEV_TX_OK; |
| 445 | } |
| 446 | |
| 447 | static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev) |
| 448 | { |
| 449 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 450 | const struct platform_device_id *id_entry = |
| 451 | platform_get_device_id(fep->pdev); |
| 452 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 453 | struct bufdesc *bdp, *last_bdp; |
| 454 | void *bufaddr; |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 455 | dma_addr_t addr; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 456 | unsigned short status; |
| 457 | unsigned short buflen; |
| 458 | unsigned int estatus = 0; |
| 459 | unsigned int index; |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 460 | int entries_free; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 461 | int ret; |
| 462 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 463 | entries_free = fec_enet_get_free_txdesc_num(fep); |
| 464 | if (entries_free < MAX_SKB_FRAGS + 1) { |
| 465 | dev_kfree_skb_any(skb); |
| 466 | if (net_ratelimit()) |
| 467 | netdev_err(ndev, "NOT enough BD for SG!\n"); |
| 468 | return NETDEV_TX_OK; |
| 469 | } |
| 470 | |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 471 | /* Protocol checksum off-load for TCP and UDP. */ |
| 472 | if (fec_enet_clear_csum(skb, ndev)) { |
| 473 | dev_kfree_skb_any(skb); |
| 474 | return NETDEV_TX_OK; |
| 475 | } |
| 476 | |
| 477 | /* Fill in a Tx ring entry */ |
| 478 | bdp = fep->cur_tx; |
| 479 | status = bdp->cbd_sc; |
| 480 | status &= ~BD_ENET_TX_STATS; |
| 481 | |
| 482 | /* Set buffer length and buffer pointer */ |
| 483 | bufaddr = skb->data; |
| 484 | buflen = skb_headlen(skb); |
| 485 | |
| 486 | index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep); |
| 487 | if (((unsigned long) bufaddr) & FEC_ALIGNMENT || |
| 488 | id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { |
| 489 | memcpy(fep->tx_bounce[index], skb->data, buflen); |
| 490 | bufaddr = fep->tx_bounce[index]; |
| 491 | |
| 492 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 493 | swap_buffer(bufaddr, buflen); |
| 494 | } |
| 495 | |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 496 | /* Push the data cache so the CPM does not get stale memory data. */ |
| 497 | addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE); |
| 498 | if (dma_mapping_error(&fep->pdev->dev, addr)) { |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 499 | dev_kfree_skb_any(skb); |
| 500 | if (net_ratelimit()) |
| 501 | netdev_err(ndev, "Tx DMA memory map failed\n"); |
| 502 | return NETDEV_TX_OK; |
| 503 | } |
| 504 | |
| 505 | if (nr_frags) { |
| 506 | ret = fec_enet_txq_submit_frag_skb(skb, ndev); |
| 507 | if (ret) |
| 508 | return ret; |
| 509 | } else { |
| 510 | status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); |
| 511 | if (fep->bufdesc_ex) { |
| 512 | estatus = BD_ENET_TX_INT; |
| 513 | if (unlikely(skb_shinfo(skb)->tx_flags & |
| 514 | SKBTX_HW_TSTAMP && fep->hwts_tx_en)) |
| 515 | estatus |= BD_ENET_TX_TS; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (fep->bufdesc_ex) { |
| 520 | |
| 521 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 522 | |
| 523 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && |
| 524 | fep->hwts_tx_en)) |
| 525 | skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; |
| 526 | |
| 527 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 528 | estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; |
| 529 | |
| 530 | ebdp->cbd_bdu = 0; |
| 531 | ebdp->cbd_esc = estatus; |
| 532 | } |
| 533 | |
| 534 | last_bdp = fep->cur_tx; |
| 535 | index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep); |
| 536 | /* Save skb pointer */ |
| 537 | fep->tx_skbuff[index] = skb; |
| 538 | |
| 539 | bdp->cbd_datlen = buflen; |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 540 | bdp->cbd_bufaddr = addr; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 541 | |
| 542 | /* Send it on its way. Tell FEC it's ready, interrupt when done, |
| 543 | * it's the last BD of the frame, and to put the CRC on the end. |
| 544 | */ |
| 545 | status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); |
| 546 | bdp->cbd_sc = status; |
| 547 | |
| 548 | fec_enet_submit_work(bdp, fep); |
Frank Li | 0319165 | 2013-07-25 14:05:53 +0800 | [diff] [blame] | 549 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 550 | /* If this was the last BD in the ring, start at the beginning again. */ |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 551 | bdp = fec_enet_get_nextdesc(last_bdp, fep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Eric Dumazet | 7a2a845 | 2013-12-19 10:53:02 -0800 | [diff] [blame] | 553 | skb_tx_timestamp(skb); |
| 554 | |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 555 | fep->cur_tx = bdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 557 | /* Trigger transmission start */ |
| 558 | writel(0, fep->hwp + FEC_X_DES_ACTIVE); |
| 559 | |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 560 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 563 | static int |
| 564 | fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev, |
| 565 | struct bufdesc *bdp, int index, char *data, |
| 566 | int size, bool last_tcp, bool is_last) |
| 567 | { |
| 568 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 569 | const struct platform_device_id *id_entry = |
| 570 | platform_get_device_id(fep->pdev); |
| 571 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 572 | unsigned short status; |
| 573 | unsigned int estatus = 0; |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 574 | dma_addr_t addr; |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 575 | |
| 576 | status = bdp->cbd_sc; |
| 577 | status &= ~BD_ENET_TX_STATS; |
| 578 | |
| 579 | status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 580 | |
| 581 | if (((unsigned long) data) & FEC_ALIGNMENT || |
| 582 | id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { |
| 583 | memcpy(fep->tx_bounce[index], data, size); |
| 584 | data = fep->tx_bounce[index]; |
| 585 | |
| 586 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 587 | swap_buffer(data, size); |
| 588 | } |
| 589 | |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 590 | addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE); |
| 591 | if (dma_mapping_error(&fep->pdev->dev, addr)) { |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 592 | dev_kfree_skb_any(skb); |
| 593 | if (net_ratelimit()) |
| 594 | netdev_err(ndev, "Tx DMA memory map failed\n"); |
| 595 | return NETDEV_TX_BUSY; |
| 596 | } |
| 597 | |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 598 | bdp->cbd_datlen = size; |
| 599 | bdp->cbd_bufaddr = addr; |
| 600 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 601 | if (fep->bufdesc_ex) { |
| 602 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 603 | estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; |
| 604 | ebdp->cbd_bdu = 0; |
| 605 | ebdp->cbd_esc = estatus; |
| 606 | } |
| 607 | |
| 608 | /* Handle the last BD specially */ |
| 609 | if (last_tcp) |
| 610 | status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC); |
| 611 | if (is_last) { |
| 612 | status |= BD_ENET_TX_INTR; |
| 613 | if (fep->bufdesc_ex) |
| 614 | ebdp->cbd_esc |= BD_ENET_TX_INT; |
| 615 | } |
| 616 | |
| 617 | bdp->cbd_sc = status; |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int |
| 623 | fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev, |
| 624 | struct bufdesc *bdp, int index) |
| 625 | { |
| 626 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 627 | const struct platform_device_id *id_entry = |
| 628 | platform_get_device_id(fep->pdev); |
| 629 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 630 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 631 | void *bufaddr; |
| 632 | unsigned long dmabuf; |
| 633 | unsigned short status; |
| 634 | unsigned int estatus = 0; |
| 635 | |
| 636 | status = bdp->cbd_sc; |
| 637 | status &= ~BD_ENET_TX_STATS; |
| 638 | status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); |
| 639 | |
| 640 | bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE; |
| 641 | dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE; |
| 642 | if (((unsigned long) bufaddr) & FEC_ALIGNMENT || |
| 643 | id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { |
| 644 | memcpy(fep->tx_bounce[index], skb->data, hdr_len); |
| 645 | bufaddr = fep->tx_bounce[index]; |
| 646 | |
| 647 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 648 | swap_buffer(bufaddr, hdr_len); |
| 649 | |
| 650 | dmabuf = dma_map_single(&fep->pdev->dev, bufaddr, |
| 651 | hdr_len, DMA_TO_DEVICE); |
| 652 | if (dma_mapping_error(&fep->pdev->dev, dmabuf)) { |
| 653 | dev_kfree_skb_any(skb); |
| 654 | if (net_ratelimit()) |
| 655 | netdev_err(ndev, "Tx DMA memory map failed\n"); |
| 656 | return NETDEV_TX_BUSY; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | bdp->cbd_bufaddr = dmabuf; |
| 661 | bdp->cbd_datlen = hdr_len; |
| 662 | |
| 663 | if (fep->bufdesc_ex) { |
| 664 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 665 | estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; |
| 666 | ebdp->cbd_bdu = 0; |
| 667 | ebdp->cbd_esc = estatus; |
| 668 | } |
| 669 | |
| 670 | bdp->cbd_sc = status; |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev) |
| 676 | { |
| 677 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 678 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 679 | int total_len, data_left; |
| 680 | struct bufdesc *bdp = fep->cur_tx; |
| 681 | struct tso_t tso; |
| 682 | unsigned int index = 0; |
| 683 | int ret; |
| 684 | |
| 685 | if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) { |
| 686 | dev_kfree_skb_any(skb); |
| 687 | if (net_ratelimit()) |
| 688 | netdev_err(ndev, "NOT enough BD for TSO!\n"); |
| 689 | return NETDEV_TX_OK; |
| 690 | } |
| 691 | |
| 692 | /* Protocol checksum off-load for TCP and UDP. */ |
| 693 | if (fec_enet_clear_csum(skb, ndev)) { |
| 694 | dev_kfree_skb_any(skb); |
| 695 | return NETDEV_TX_OK; |
| 696 | } |
| 697 | |
| 698 | /* Initialize the TSO handler, and prepare the first payload */ |
| 699 | tso_start(skb, &tso); |
| 700 | |
| 701 | total_len = skb->len - hdr_len; |
| 702 | while (total_len > 0) { |
| 703 | char *hdr; |
| 704 | |
| 705 | index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep); |
| 706 | data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); |
| 707 | total_len -= data_left; |
| 708 | |
| 709 | /* prepare packet headers: MAC + IP + TCP */ |
| 710 | hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE; |
| 711 | tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); |
| 712 | ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index); |
| 713 | if (ret) |
| 714 | goto err_release; |
| 715 | |
| 716 | while (data_left > 0) { |
| 717 | int size; |
| 718 | |
| 719 | size = min_t(int, tso.size, data_left); |
| 720 | bdp = fec_enet_get_nextdesc(bdp, fep); |
| 721 | index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep); |
| 722 | ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data, |
| 723 | size, size == data_left, |
| 724 | total_len == 0); |
| 725 | if (ret) |
| 726 | goto err_release; |
| 727 | |
| 728 | data_left -= size; |
| 729 | tso_build_data(skb, &tso, size); |
| 730 | } |
| 731 | |
| 732 | bdp = fec_enet_get_nextdesc(bdp, fep); |
| 733 | } |
| 734 | |
| 735 | /* Save skb pointer */ |
| 736 | fep->tx_skbuff[index] = skb; |
| 737 | |
| 738 | fec_enet_submit_work(bdp, fep); |
| 739 | |
| 740 | skb_tx_timestamp(skb); |
| 741 | fep->cur_tx = bdp; |
| 742 | |
| 743 | /* Trigger transmission start */ |
| 744 | writel(0, fep->hwp + FEC_X_DES_ACTIVE); |
| 745 | |
| 746 | return 0; |
| 747 | |
| 748 | err_release: |
| 749 | /* TODO: Release all used data descriptors for TSO */ |
| 750 | return ret; |
| 751 | } |
| 752 | |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 753 | static netdev_tx_t |
| 754 | fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 755 | { |
| 756 | struct fec_enet_private *fep = netdev_priv(ndev); |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 757 | int entries_free; |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 758 | int ret; |
| 759 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 760 | if (skb_is_gso(skb)) |
| 761 | ret = fec_enet_txq_submit_tso(skb, ndev); |
| 762 | else |
| 763 | ret = fec_enet_txq_submit_skb(skb, ndev); |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 764 | if (ret) |
| 765 | return ret; |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 766 | |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 767 | entries_free = fec_enet_get_free_txdesc_num(fep); |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 768 | if (entries_free <= fep->tx_stop_threshold) |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 769 | netif_stop_queue(ndev); |
| 770 | |
| 771 | return NETDEV_TX_OK; |
| 772 | } |
| 773 | |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 774 | /* Init RX & TX buffer descriptors |
| 775 | */ |
| 776 | static void fec_enet_bd_init(struct net_device *dev) |
| 777 | { |
| 778 | struct fec_enet_private *fep = netdev_priv(dev); |
| 779 | struct bufdesc *bdp; |
| 780 | unsigned int i; |
| 781 | |
| 782 | /* Initialize the receive buffer descriptors. */ |
| 783 | bdp = fep->rx_bd_base; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 784 | for (i = 0; i < fep->rx_ring_size; i++) { |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 785 | |
| 786 | /* Initialize the BD for every fragment in the page. */ |
| 787 | if (bdp->cbd_bufaddr) |
| 788 | bdp->cbd_sc = BD_ENET_RX_EMPTY; |
| 789 | else |
| 790 | bdp->cbd_sc = 0; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 791 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* Set the last buffer to wrap */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 795 | bdp = fec_enet_get_prevdesc(bdp, fep); |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 796 | bdp->cbd_sc |= BD_SC_WRAP; |
| 797 | |
| 798 | fep->cur_rx = fep->rx_bd_base; |
| 799 | |
| 800 | /* ...and the same for transmit */ |
| 801 | bdp = fep->tx_bd_base; |
| 802 | fep->cur_tx = bdp; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 803 | for (i = 0; i < fep->tx_ring_size; i++) { |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 804 | |
| 805 | /* Initialize the BD for every fragment in the page. */ |
| 806 | bdp->cbd_sc = 0; |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 807 | if (fep->tx_skbuff[i]) { |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 808 | dev_kfree_skb_any(fep->tx_skbuff[i]); |
| 809 | fep->tx_skbuff[i] = NULL; |
| 810 | } |
| 811 | bdp->cbd_bufaddr = 0; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 812 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* Set the last buffer to wrap */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 816 | bdp = fec_enet_get_prevdesc(bdp, fep); |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 817 | bdp->cbd_sc |= BD_SC_WRAP; |
| 818 | fep->dirty_tx = bdp; |
| 819 | } |
| 820 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 821 | /* This function is called to start or restart the FEC during a link |
| 822 | * change. This only happens when switching between half and full |
| 823 | * duplex. |
| 824 | */ |
| 825 | static void |
| 826 | fec_restart(struct net_device *ndev, int duplex) |
| 827 | { |
| 828 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 829 | const struct platform_device_id *id_entry = |
| 830 | platform_get_device_id(fep->pdev); |
| 831 | int i; |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 832 | u32 val; |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 833 | u32 temp_mac[2]; |
| 834 | u32 rcntl = OPT_FRAME_SIZE | 0x04; |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 835 | u32 ecntl = 0x2; /* ETHEREN */ |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 836 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 837 | if (netif_running(ndev)) { |
| 838 | netif_device_detach(ndev); |
| 839 | napi_disable(&fep->napi); |
Russell King | b49cd50 | 2014-07-08 00:22:49 +0100 | [diff] [blame] | 840 | netif_tx_disable(ndev); |
Fabio Estevam | 3169134 | 2013-05-15 07:06:26 +0000 | [diff] [blame] | 841 | netif_tx_lock_bh(ndev); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 844 | /* Whack a reset. We should wait for this. */ |
| 845 | writel(1, fep->hwp + FEC_ECNTRL); |
| 846 | udelay(10); |
| 847 | |
| 848 | /* |
| 849 | * enet-mac reset will reset mac address registers too, |
| 850 | * so need to reconfigure it. |
| 851 | */ |
| 852 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
| 853 | memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); |
| 854 | writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW); |
| 855 | writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH); |
| 856 | } |
| 857 | |
| 858 | /* Clear any outstanding interrupt. */ |
| 859 | writel(0xffc00000, fep->hwp + FEC_IEVENT); |
| 860 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 861 | /* Set maximum receive buffer size. */ |
| 862 | writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE); |
| 863 | |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 864 | fec_enet_bd_init(ndev); |
| 865 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 866 | /* Set receive and transmit descriptor base. */ |
| 867 | writel(fep->bd_dma, fep->hwp + FEC_R_DES_START); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 868 | if (fep->bufdesc_ex) |
| 869 | writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex) |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 870 | * fep->rx_ring_size, fep->hwp + FEC_X_DES_START); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 871 | else |
| 872 | writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 873 | * fep->rx_ring_size, fep->hwp + FEC_X_DES_START); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 874 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 875 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 876 | for (i = 0; i <= TX_RING_MOD_MASK; i++) { |
| 877 | if (fep->tx_skbuff[i]) { |
| 878 | dev_kfree_skb_any(fep->tx_skbuff[i]); |
| 879 | fep->tx_skbuff[i] = NULL; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /* Enable MII mode */ |
| 884 | if (duplex) { |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 885 | /* FD enable */ |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 886 | writel(0x04, fep->hwp + FEC_X_CNTRL); |
| 887 | } else { |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 888 | /* No Rcv on Xmit */ |
| 889 | rcntl |= 0x02; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 890 | writel(0x0, fep->hwp + FEC_X_CNTRL); |
| 891 | } |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 892 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 893 | fep->full_duplex = duplex; |
| 894 | |
| 895 | /* Set MII speed */ |
| 896 | writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); |
| 897 | |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 898 | #if !defined(CONFIG_M5272) |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 899 | /* set RX checksum */ |
| 900 | val = readl(fep->hwp + FEC_RACC); |
| 901 | if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) |
| 902 | val |= FEC_RACC_OPTIONS; |
| 903 | else |
| 904 | val &= ~FEC_RACC_OPTIONS; |
| 905 | writel(val, fep->hwp + FEC_RACC); |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 906 | #endif |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 907 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 908 | /* |
| 909 | * The phy interface and speed need to get configured |
| 910 | * differently on enet-mac. |
| 911 | */ |
| 912 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 913 | /* Enable flow control and length check */ |
| 914 | rcntl |= 0x40000000 | 0x00000020; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 915 | |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 916 | /* RGMII, RMII or MII */ |
| 917 | if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII) |
| 918 | rcntl |= (1 << 6); |
| 919 | else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 920 | rcntl |= (1 << 8); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 921 | else |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 922 | rcntl &= ~(1 << 8); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 923 | |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 924 | /* 1G, 100M or 10M */ |
| 925 | if (fep->phy_dev) { |
| 926 | if (fep->phy_dev->speed == SPEED_1000) |
| 927 | ecntl |= (1 << 5); |
| 928 | else if (fep->phy_dev->speed == SPEED_100) |
| 929 | rcntl &= ~(1 << 9); |
| 930 | else |
| 931 | rcntl |= (1 << 9); |
| 932 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 933 | } else { |
| 934 | #ifdef FEC_MIIGSK_ENR |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 935 | if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) { |
Eric Benard | 8d82f21 | 2012-01-12 06:10:28 +0000 | [diff] [blame] | 936 | u32 cfgr; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 937 | /* disable the gasket and wait */ |
| 938 | writel(0, fep->hwp + FEC_MIIGSK_ENR); |
| 939 | while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) |
| 940 | udelay(1); |
| 941 | |
| 942 | /* |
| 943 | * configure the gasket: |
| 944 | * RMII, 50 MHz, no loopback, no echo |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 945 | * MII, 25 MHz, no loopback, no echo |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 946 | */ |
Eric Benard | 8d82f21 | 2012-01-12 06:10:28 +0000 | [diff] [blame] | 947 | cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) |
| 948 | ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; |
| 949 | if (fep->phy_dev && fep->phy_dev->speed == SPEED_10) |
| 950 | cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; |
| 951 | writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 952 | |
| 953 | /* re-enable the gasket */ |
| 954 | writel(2, fep->hwp + FEC_MIIGSK_ENR); |
| 955 | } |
| 956 | #endif |
| 957 | } |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 958 | |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 959 | #if !defined(CONFIG_M5272) |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 960 | /* enable pause frame*/ |
| 961 | if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || |
| 962 | ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && |
| 963 | fep->phy_dev && fep->phy_dev->pause)) { |
| 964 | rcntl |= FEC_ENET_FCE; |
| 965 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 966 | /* set FIFO threshold parameter to reduce overrun */ |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 967 | writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); |
| 968 | writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); |
| 969 | writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); |
| 970 | writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); |
| 971 | |
| 972 | /* OPD */ |
| 973 | writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); |
| 974 | } else { |
| 975 | rcntl &= ~FEC_ENET_FCE; |
| 976 | } |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 977 | #endif /* !defined(CONFIG_M5272) */ |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 978 | |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 979 | writel(rcntl, fep->hwp + FEC_R_CNTRL); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 980 | |
Stefan Wahren | 84fe618 | 2014-03-12 11:28:19 +0100 | [diff] [blame] | 981 | /* Setup multicast filter. */ |
| 982 | set_multicast_list(ndev); |
| 983 | #ifndef CONFIG_M5272 |
| 984 | writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); |
| 985 | writel(0, fep->hwp + FEC_HASH_TABLE_LOW); |
| 986 | #endif |
| 987 | |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 988 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
| 989 | /* enable ENET endian swap */ |
| 990 | ecntl |= (1 << 8); |
| 991 | /* enable ENET store and forward mode */ |
| 992 | writel(1 << 8, fep->hwp + FEC_X_WMRK); |
| 993 | } |
| 994 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 995 | if (fep->bufdesc_ex) |
| 996 | ecntl |= (1 << 4); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 997 | |
Chris Healy | 38ae92d | 2013-06-25 23:18:52 -0700 | [diff] [blame] | 998 | #ifndef CONFIG_M5272 |
Jim Baxter | b9eef55 | 2013-07-01 14:57:54 +0100 | [diff] [blame] | 999 | /* Enable the MIB statistic event counters */ |
| 1000 | writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT); |
Chris Healy | 38ae92d | 2013-06-25 23:18:52 -0700 | [diff] [blame] | 1001 | #endif |
| 1002 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1003 | /* And last, enable the transmit and receive processing */ |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1004 | writel(ecntl, fep->hwp + FEC_ECNTRL); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1005 | writel(0, fep->hwp + FEC_R_DES_ACTIVE); |
| 1006 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1007 | if (fep->bufdesc_ex) |
| 1008 | fec_ptp_start_cyclecounter(ndev); |
| 1009 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1010 | /* Enable interrupts we wish to service */ |
| 1011 | writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1012 | |
| 1013 | if (netif_running(ndev)) { |
Fabio Estevam | 3169134 | 2013-05-15 07:06:26 +0000 | [diff] [blame] | 1014 | netif_tx_unlock_bh(ndev); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1015 | netif_wake_queue(ndev); |
Fabio Estevam | 1ed0d56 | 2013-05-15 07:06:27 +0000 | [diff] [blame] | 1016 | napi_enable(&fep->napi); |
| 1017 | netif_device_attach(ndev); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1018 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | static void |
| 1022 | fec_stop(struct net_device *ndev) |
| 1023 | { |
| 1024 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1025 | const struct platform_device_id *id_entry = |
| 1026 | platform_get_device_id(fep->pdev); |
Lothar Waßmann | 42431dc | 2011-12-07 21:59:30 +0000 | [diff] [blame] | 1027 | u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1028 | |
| 1029 | /* We cannot expect a graceful transmit stop without link !!! */ |
| 1030 | if (fep->link) { |
| 1031 | writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ |
| 1032 | udelay(10); |
| 1033 | if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1034 | netdev_err(ndev, "Graceful transmit stop did not complete!\n"); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* Whack a reset. We should wait for this. */ |
| 1038 | writel(1, fep->hwp + FEC_ECNTRL); |
| 1039 | udelay(10); |
| 1040 | writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); |
| 1041 | writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* We have to keep ENET enabled to have MII interrupt stay working */ |
Lothar Waßmann | 42431dc | 2011-12-07 21:59:30 +0000 | [diff] [blame] | 1044 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1045 | writel(2, fep->hwp + FEC_ECNTRL); |
Lothar Waßmann | 42431dc | 2011-12-07 21:59:30 +0000 | [diff] [blame] | 1046 | writel(rmii_mode, fep->hwp + FEC_R_CNTRL); |
| 1047 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | static void |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1052 | fec_timeout(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1054 | struct fec_enet_private *fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1056 | ndev->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1058 | fep->delay_work.timeout = true; |
| 1059 | schedule_delayed_work(&(fep->delay_work.delay_work), 0); |
| 1060 | } |
| 1061 | |
| 1062 | static void fec_enet_work(struct work_struct *work) |
| 1063 | { |
| 1064 | struct fec_enet_private *fep = |
| 1065 | container_of(work, |
| 1066 | struct fec_enet_private, |
| 1067 | delay_work.delay_work.work); |
| 1068 | |
| 1069 | if (fep->delay_work.timeout) { |
| 1070 | fep->delay_work.timeout = false; |
| 1071 | fec_restart(fep->netdev, fep->full_duplex); |
| 1072 | netif_wake_queue(fep->netdev); |
| 1073 | } |
Frank Li | 0319165 | 2013-07-25 14:05:53 +0800 | [diff] [blame] | 1074 | |
| 1075 | if (fep->delay_work.trig_tx) { |
| 1076 | fep->delay_work.trig_tx = false; |
| 1077 | writel(0, fep->hwp + FEC_X_DES_ACTIVE); |
| 1078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | static void |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1082 | fec_enet_tx(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
| 1084 | struct fec_enet_private *fep; |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 1085 | struct bufdesc *bdp; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1086 | unsigned short status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | struct sk_buff *skb; |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1088 | int index = 0; |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 1089 | int entries_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1091 | fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | bdp = fep->dirty_tx; |
| 1093 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1094 | /* get next bdp of dirty_tx */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 1095 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1096 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1097 | while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) { |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1098 | |
| 1099 | /* current queue is empty */ |
| 1100 | if (bdp == fep->cur_tx) |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1101 | break; |
| 1102 | |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 1103 | index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep); |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1104 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1105 | skb = fep->tx_skbuff[index]; |
Russell King | d6bf314 | 2014-07-08 00:23:19 +0100 | [diff] [blame] | 1106 | fep->tx_skbuff[index] = NULL; |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 1107 | if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr)) |
| 1108 | dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, |
| 1109 | bdp->cbd_datlen, DMA_TO_DEVICE); |
Sebastian Siewior | 2488a54 | 2013-12-02 10:52:55 +0100 | [diff] [blame] | 1110 | bdp->cbd_bufaddr = 0; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 1111 | if (!skb) { |
| 1112 | bdp = fec_enet_get_nextdesc(bdp, fep); |
| 1113 | continue; |
| 1114 | } |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | /* Check for errors. */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1117 | if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | BD_ENET_TX_RL | BD_ENET_TX_UN | |
| 1119 | BD_ENET_TX_CSL)) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1120 | ndev->stats.tx_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1121 | if (status & BD_ENET_TX_HB) /* No heartbeat */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1122 | ndev->stats.tx_heartbeat_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1123 | if (status & BD_ENET_TX_LC) /* Late collision */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1124 | ndev->stats.tx_window_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1125 | if (status & BD_ENET_TX_RL) /* Retrans limit */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1126 | ndev->stats.tx_aborted_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1127 | if (status & BD_ENET_TX_UN) /* Underrun */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1128 | ndev->stats.tx_fifo_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1129 | if (status & BD_ENET_TX_CSL) /* Carrier lost */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1130 | ndev->stats.tx_carrier_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } else { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1132 | ndev->stats.tx_packets++; |
Nimrod Andy | 6e90928 | 2014-06-12 08:16:22 +0800 | [diff] [blame] | 1133 | ndev->stats.tx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1136 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) && |
| 1137 | fep->bufdesc_ex) { |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1138 | struct skb_shared_hwtstamps shhwtstamps; |
| 1139 | unsigned long flags; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1140 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1141 | |
| 1142 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 1143 | spin_lock_irqsave(&fep->tmreg_lock, flags); |
| 1144 | shhwtstamps.hwtstamp = ns_to_ktime( |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1145 | timecounter_cyc2time(&fep->tc, ebdp->ts)); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1146 | spin_unlock_irqrestore(&fep->tmreg_lock, flags); |
| 1147 | skb_tstamp_tx(skb, &shhwtstamps); |
| 1148 | } |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1149 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1150 | if (status & BD_ENET_TX_READY) |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1151 | netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n"); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | /* Deferred means some collisions occurred during transmit, |
| 1154 | * but we eventually sent the packet OK. |
| 1155 | */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1156 | if (status & BD_ENET_TX_DEF) |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1157 | ndev->stats.collisions++; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1158 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1159 | /* Free the sk buffer associated with this last transmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | dev_kfree_skb_any(skb); |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1161 | |
| 1162 | fep->dirty_tx = bdp; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1163 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1164 | /* Update pointer to next buffer descriptor to be transmitted */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 1165 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1166 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1167 | /* Since we have freed up a buffer, the ring is no longer full |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | */ |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 1169 | if (netif_queue_stopped(ndev)) { |
| 1170 | entries_free = fec_enet_get_free_txdesc_num(fep); |
| 1171 | if (entries_free >= fep->tx_wake_threshold) |
| 1172 | netif_wake_queue(ndev); |
| 1173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1175 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | /* During a receive, the cur_rx points to the current incoming buffer. |
| 1179 | * When we update through the ring, if the next incoming buffer has |
| 1180 | * not been given to the system, we just set the empty indicator, |
| 1181 | * effectively tossing the packet. |
| 1182 | */ |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1183 | static int |
| 1184 | fec_enet_rx(struct net_device *ndev, int budget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1186 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1187 | const struct platform_device_id *id_entry = |
| 1188 | platform_get_device_id(fep->pdev); |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 1189 | struct bufdesc *bdp; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1190 | unsigned short status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | struct sk_buff *skb; |
| 1192 | ushort pkt_len; |
| 1193 | __u8 *data; |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1194 | int pkt_received = 0; |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 1195 | struct bufdesc_ex *ebdp = NULL; |
| 1196 | bool vlan_packet_rcvd = false; |
| 1197 | u16 vlan_tag; |
Duan Fugang-B38611 | d842a31 | 2013-11-14 09:57:10 +0800 | [diff] [blame] | 1198 | int index = 0; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1199 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 1200 | #ifdef CONFIG_M532x |
| 1201 | flush_cache_all(); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1202 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | /* First, grab all of the stats for the incoming packet. |
| 1205 | * These get messed up if we get called due to a busy condition. |
| 1206 | */ |
| 1207 | bdp = fep->cur_rx; |
| 1208 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1209 | while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1211 | if (pkt_received >= budget) |
| 1212 | break; |
| 1213 | pkt_received++; |
| 1214 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1215 | /* Since we have allocated space to hold a complete frame, |
| 1216 | * the last indicator should be set. |
| 1217 | */ |
| 1218 | if ((status & BD_ENET_RX_LAST) == 0) |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1219 | netdev_err(ndev, "rcv is not +last\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1221 | /* Check for errors. */ |
| 1222 | if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1224 | ndev->stats.rx_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1225 | if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) { |
| 1226 | /* Frame too long or too short. */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1227 | ndev->stats.rx_length_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1228 | } |
| 1229 | if (status & BD_ENET_RX_NO) /* Frame alignment */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1230 | ndev->stats.rx_frame_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1231 | if (status & BD_ENET_RX_CR) /* CRC Error */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1232 | ndev->stats.rx_crc_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1233 | if (status & BD_ENET_RX_OV) /* FIFO overrun */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1234 | ndev->stats.rx_fifo_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* Report late collisions as a frame error. |
| 1238 | * On this error, the BD is closed, but we don't know what we |
| 1239 | * have in the buffer. So, just drop this frame on the floor. |
| 1240 | */ |
| 1241 | if (status & BD_ENET_RX_CL) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1242 | ndev->stats.rx_errors++; |
| 1243 | ndev->stats.rx_frame_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1244 | goto rx_processing_done; |
| 1245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1247 | /* Process the incoming frame. */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1248 | ndev->stats.rx_packets++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1249 | pkt_len = bdp->cbd_datlen; |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1250 | ndev->stats.rx_bytes += pkt_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
Nimrod Andy | 61a4427 | 2014-06-12 08:16:18 +0800 | [diff] [blame] | 1252 | index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep); |
Duan Fugang-B38611 | d842a31 | 2013-11-14 09:57:10 +0800 | [diff] [blame] | 1253 | data = fep->rx_skbuff[index]->data; |
| 1254 | dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr, |
| 1255 | FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); |
Sascha Hauer | ccdc4f1 | 2009-01-28 23:03:09 +0000 | [diff] [blame] | 1256 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1257 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 1258 | swap_buffer(data, pkt_len); |
| 1259 | |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 1260 | /* Extract the enhanced buffer descriptor */ |
| 1261 | ebdp = NULL; |
| 1262 | if (fep->bufdesc_ex) |
| 1263 | ebdp = (struct bufdesc_ex *)bdp; |
| 1264 | |
| 1265 | /* If this is a VLAN packet remove the VLAN Tag */ |
| 1266 | vlan_packet_rcvd = false; |
| 1267 | if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) && |
| 1268 | fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) { |
| 1269 | /* Push and remove the vlan tag */ |
| 1270 | struct vlan_hdr *vlan_header = |
| 1271 | (struct vlan_hdr *) (data + ETH_HLEN); |
| 1272 | vlan_tag = ntohs(vlan_header->h_vlan_TCI); |
| 1273 | pkt_len -= VLAN_HLEN; |
| 1274 | |
| 1275 | vlan_packet_rcvd = true; |
| 1276 | } |
| 1277 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1278 | /* This does 16 byte alignment, exactly what we need. |
| 1279 | * The packet length includes FCS, but we don't want to |
| 1280 | * include that when passing upstream as it messes up |
| 1281 | * bridging applications. |
| 1282 | */ |
Fabio Estevam | b72061a | 2012-02-07 08:27:31 +0000 | [diff] [blame] | 1283 | skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Sascha Hauer | 8549889 | 2009-04-15 01:32:21 +0000 | [diff] [blame] | 1285 | if (unlikely(!skb)) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1286 | ndev->stats.rx_dropped++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1287 | } else { |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 1288 | int payload_offset = (2 * ETH_ALEN); |
Sascha Hauer | 8549889 | 2009-04-15 01:32:21 +0000 | [diff] [blame] | 1289 | skb_reserve(skb, NET_IP_ALIGN); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1290 | skb_put(skb, pkt_len - 4); /* Make room */ |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 1291 | |
| 1292 | /* Extract the frame data without the VLAN header. */ |
| 1293 | skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN)); |
| 1294 | if (vlan_packet_rcvd) |
| 1295 | payload_offset = (2 * ETH_ALEN) + VLAN_HLEN; |
| 1296 | skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN), |
| 1297 | data + payload_offset, |
| 1298 | pkt_len - 4 - (2 * ETH_ALEN)); |
| 1299 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1300 | skb->protocol = eth_type_trans(skb, ndev); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1301 | |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1302 | /* Get receive timestamp from the skb */ |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1303 | if (fep->hwts_rx_en && fep->bufdesc_ex) { |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1304 | struct skb_shared_hwtstamps *shhwtstamps = |
| 1305 | skb_hwtstamps(skb); |
| 1306 | unsigned long flags; |
| 1307 | |
| 1308 | memset(shhwtstamps, 0, sizeof(*shhwtstamps)); |
| 1309 | |
| 1310 | spin_lock_irqsave(&fep->tmreg_lock, flags); |
| 1311 | shhwtstamps->hwtstamp = ns_to_ktime( |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1312 | timecounter_cyc2time(&fep->tc, ebdp->ts)); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1313 | spin_unlock_irqrestore(&fep->tmreg_lock, flags); |
| 1314 | } |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1315 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 1316 | if (fep->bufdesc_ex && |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 1317 | (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 1318 | if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) { |
| 1319 | /* don't check it */ |
| 1320 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1321 | } else { |
| 1322 | skb_checksum_none_assert(skb); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 1326 | /* Handle received VLAN packets */ |
| 1327 | if (vlan_packet_rcvd) |
| 1328 | __vlan_hwaccel_put_tag(skb, |
| 1329 | htons(ETH_P_8021Q), |
| 1330 | vlan_tag); |
| 1331 | |
Richard Cochran | 0affdf3 | 2013-08-30 20:28:10 +0200 | [diff] [blame] | 1332 | napi_gro_receive(&fep->napi, skb); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1333 | } |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1334 | |
Duan Fugang-B38611 | d842a31 | 2013-11-14 09:57:10 +0800 | [diff] [blame] | 1335 | dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr, |
| 1336 | FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1337 | rx_processing_done: |
| 1338 | /* Clear the status flags for this buffer */ |
| 1339 | status &= ~BD_ENET_RX_STATS; |
| 1340 | |
| 1341 | /* Mark the buffer empty */ |
| 1342 | status |= BD_ENET_RX_EMPTY; |
| 1343 | bdp->cbd_sc = status; |
| 1344 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1345 | if (fep->bufdesc_ex) { |
| 1346 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 1347 | |
| 1348 | ebdp->cbd_esc = BD_ENET_RX_INT; |
| 1349 | ebdp->cbd_prot = 0; |
| 1350 | ebdp->cbd_bdu = 0; |
| 1351 | } |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1352 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1353 | /* Update BD pointer to next entry */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 1354 | bdp = fec_enet_get_nextdesc(bdp, fep); |
| 1355 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1356 | /* Doing this here will keep the FEC running while we process |
| 1357 | * incoming frames. On a heavily loaded network, we should be |
| 1358 | * able to keep up at the expense of system resources. |
| 1359 | */ |
| 1360 | writel(0, fep->hwp + FEC_R_DES_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 1362 | fep->cur_rx = bdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1364 | return pkt_received; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1367 | static irqreturn_t |
| 1368 | fec_enet_interrupt(int irq, void *dev_id) |
| 1369 | { |
| 1370 | struct net_device *ndev = dev_id; |
| 1371 | struct fec_enet_private *fep = netdev_priv(ndev); |
Russell King | 7a16807 | 2014-07-08 00:22:44 +0100 | [diff] [blame] | 1372 | const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1373 | uint int_events; |
| 1374 | irqreturn_t ret = IRQ_NONE; |
| 1375 | |
Russell King | 7a16807 | 2014-07-08 00:22:44 +0100 | [diff] [blame] | 1376 | int_events = readl(fep->hwp + FEC_IEVENT); |
| 1377 | writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1378 | |
Russell King | 7a16807 | 2014-07-08 00:22:44 +0100 | [diff] [blame] | 1379 | if (int_events & napi_mask) { |
| 1380 | ret = IRQ_HANDLED; |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1381 | |
Russell King | 7a16807 | 2014-07-08 00:22:44 +0100 | [diff] [blame] | 1382 | /* Disable the NAPI interrupts */ |
| 1383 | writel(FEC_ENET_MII, fep->hwp + FEC_IMASK); |
| 1384 | napi_schedule(&fep->napi); |
| 1385 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1386 | |
Russell King | 7a16807 | 2014-07-08 00:22:44 +0100 | [diff] [blame] | 1387 | if (int_events & FEC_ENET_MII) { |
| 1388 | ret = IRQ_HANDLED; |
| 1389 | complete(&fep->mdio_done); |
| 1390 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1391 | |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1395 | static int fec_enet_rx_napi(struct napi_struct *napi, int budget) |
| 1396 | { |
| 1397 | struct net_device *ndev = napi->dev; |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1398 | struct fec_enet_private *fep = netdev_priv(ndev); |
Russell King | 7a16807 | 2014-07-08 00:22:44 +0100 | [diff] [blame] | 1399 | int pkts; |
| 1400 | |
| 1401 | /* |
| 1402 | * Clear any pending transmit or receive interrupts before |
| 1403 | * processing the rings to avoid racing with the hardware. |
| 1404 | */ |
| 1405 | writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT); |
| 1406 | |
| 1407 | pkts = fec_enet_rx(ndev, budget); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1408 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 1409 | fec_enet_tx(ndev); |
| 1410 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1411 | if (pkts < budget) { |
| 1412 | napi_complete(napi); |
| 1413 | writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); |
| 1414 | } |
| 1415 | return pkts; |
| 1416 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 1417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | /* ------------------------------------------------------------------------- */ |
Fabio Estevam | 0c7768a | 2013-01-07 17:42:56 +0000 | [diff] [blame] | 1419 | static void fec_get_mac(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1421 | struct fec_enet_private *fep = netdev_priv(ndev); |
Jingoo Han | 94660ba | 2013-08-30 13:56:26 +0900 | [diff] [blame] | 1422 | struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev); |
Greg Ungerer | 7dd6a2a | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 1423 | unsigned char *iap, tmpaddr[ETH_ALEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1425 | /* |
| 1426 | * try to get mac address in following order: |
| 1427 | * |
| 1428 | * 1) module parameter via kernel command line in form |
| 1429 | * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 |
| 1430 | */ |
| 1431 | iap = macaddr; |
| 1432 | |
| 1433 | /* |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1434 | * 2) from device tree data |
| 1435 | */ |
| 1436 | if (!is_valid_ether_addr(iap)) { |
| 1437 | struct device_node *np = fep->pdev->dev.of_node; |
| 1438 | if (np) { |
| 1439 | const char *mac = of_get_mac_address(np); |
| 1440 | if (mac) |
| 1441 | iap = (unsigned char *) mac; |
| 1442 | } |
| 1443 | } |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1444 | |
| 1445 | /* |
| 1446 | * 3) from flash or fuse (via platform data) |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1447 | */ |
| 1448 | if (!is_valid_ether_addr(iap)) { |
| 1449 | #ifdef CONFIG_M5272 |
| 1450 | if (FEC_FLASHMAC) |
| 1451 | iap = (unsigned char *)FEC_FLASHMAC; |
| 1452 | #else |
| 1453 | if (pdata) |
Lothar Waßmann | 589efdc | 2011-12-07 21:59:29 +0000 | [diff] [blame] | 1454 | iap = (unsigned char *)&pdata->mac; |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1455 | #endif |
| 1456 | } |
| 1457 | |
| 1458 | /* |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1459 | * 4) FEC mac registers set by bootloader |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1460 | */ |
| 1461 | if (!is_valid_ether_addr(iap)) { |
Dan Carpenter | 7d7628f | 2013-08-29 11:25:14 +0300 | [diff] [blame] | 1462 | *((__be32 *) &tmpaddr[0]) = |
| 1463 | cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW)); |
| 1464 | *((__be16 *) &tmpaddr[4]) = |
| 1465 | cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | iap = &tmpaddr[0]; |
| 1467 | } |
| 1468 | |
Lucas Stach | ff5b2fa | 2013-06-03 00:38:39 +0000 | [diff] [blame] | 1469 | /* |
| 1470 | * 5) random mac address |
| 1471 | */ |
| 1472 | if (!is_valid_ether_addr(iap)) { |
| 1473 | /* Report it and use a random ethernet address instead */ |
| 1474 | netdev_err(ndev, "Invalid MAC address: %pM\n", iap); |
| 1475 | eth_hw_addr_random(ndev); |
| 1476 | netdev_info(ndev, "Using random MAC address: %pM\n", |
| 1477 | ndev->dev_addr); |
| 1478 | return; |
| 1479 | } |
| 1480 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1481 | memcpy(ndev->dev_addr, iap, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1483 | /* Adjust MAC if using macaddr */ |
| 1484 | if (iap == macaddr) |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1485 | ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
| 1488 | /* ------------------------------------------------------------------------- */ |
| 1489 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1490 | /* |
| 1491 | * Phy section |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1493 | static void fec_enet_adjust_link(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1495 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1496 | struct phy_device *phy_dev = fep->phy_dev; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1497 | int status_change = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1499 | /* Prevent a state halted on mii error */ |
| 1500 | if (fep->mii_timeout && phy_dev->state == PHY_HALTED) { |
| 1501 | phy_dev->state = PHY_RESUMING; |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1502 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1504 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1505 | if (phy_dev->link) { |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1506 | if (!fep->link) { |
Lothar Waßmann | 6ea0722 | 2011-12-07 21:59:27 +0000 | [diff] [blame] | 1507 | fep->link = phy_dev->link; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1508 | status_change = 1; |
| 1509 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1510 | |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1511 | if (fep->full_duplex != phy_dev->duplex) |
| 1512 | status_change = 1; |
| 1513 | |
| 1514 | if (phy_dev->speed != fep->speed) { |
| 1515 | fep->speed = phy_dev->speed; |
| 1516 | status_change = 1; |
| 1517 | } |
| 1518 | |
| 1519 | /* if any of the above changed restart the FEC */ |
| 1520 | if (status_change) |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1521 | fec_restart(ndev, phy_dev->duplex); |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1522 | } else { |
| 1523 | if (fep->link) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1524 | fec_stop(ndev); |
Lucas Stach | 8d7ed0f | 2013-04-16 00:42:42 +0000 | [diff] [blame] | 1525 | fep->link = phy_dev->link; |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1526 | status_change = 1; |
| 1527 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1530 | if (status_change) |
| 1531 | phy_print_status(phy_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1534 | static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | { |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1536 | struct fec_enet_private *fep = bus->priv; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1537 | unsigned long time_left; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1538 | |
| 1539 | fep->mii_timeout = 0; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1540 | init_completion(&fep->mdio_done); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1541 | |
| 1542 | /* start a read op */ |
| 1543 | writel(FEC_MMFR_ST | FEC_MMFR_OP_READ | |
| 1544 | FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | |
| 1545 | FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); |
| 1546 | |
| 1547 | /* wait for end of transfer */ |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1548 | time_left = wait_for_completion_timeout(&fep->mdio_done, |
| 1549 | usecs_to_jiffies(FEC_MII_TIMEOUT)); |
| 1550 | if (time_left == 0) { |
| 1551 | fep->mii_timeout = 1; |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1552 | netdev_err(fep->netdev, "MDIO read timeout\n"); |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1553 | return -ETIMEDOUT; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | /* return value */ |
| 1557 | return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); |
| 1558 | } |
| 1559 | |
| 1560 | static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 1561 | u16 value) |
| 1562 | { |
| 1563 | struct fec_enet_private *fep = bus->priv; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1564 | unsigned long time_left; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1565 | |
| 1566 | fep->mii_timeout = 0; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1567 | init_completion(&fep->mdio_done); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1568 | |
Shawn Guo | 862f098 | 2011-01-05 21:13:09 +0000 | [diff] [blame] | 1569 | /* start a write op */ |
| 1570 | writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1571 | FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | |
| 1572 | FEC_MMFR_TA | FEC_MMFR_DATA(value), |
| 1573 | fep->hwp + FEC_MII_DATA); |
| 1574 | |
| 1575 | /* wait for end of transfer */ |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1576 | time_left = wait_for_completion_timeout(&fep->mdio_done, |
| 1577 | usecs_to_jiffies(FEC_MII_TIMEOUT)); |
| 1578 | if (time_left == 0) { |
| 1579 | fep->mii_timeout = 1; |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1580 | netdev_err(fep->netdev, "MDIO write timeout\n"); |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1581 | return -ETIMEDOUT; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 1587 | static int fec_enet_clk_enable(struct net_device *ndev, bool enable) |
| 1588 | { |
| 1589 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1590 | int ret; |
| 1591 | |
| 1592 | if (enable) { |
| 1593 | ret = clk_prepare_enable(fep->clk_ahb); |
| 1594 | if (ret) |
| 1595 | return ret; |
| 1596 | ret = clk_prepare_enable(fep->clk_ipg); |
| 1597 | if (ret) |
| 1598 | goto failed_clk_ipg; |
| 1599 | if (fep->clk_enet_out) { |
| 1600 | ret = clk_prepare_enable(fep->clk_enet_out); |
| 1601 | if (ret) |
| 1602 | goto failed_clk_enet_out; |
| 1603 | } |
| 1604 | if (fep->clk_ptp) { |
| 1605 | ret = clk_prepare_enable(fep->clk_ptp); |
| 1606 | if (ret) |
| 1607 | goto failed_clk_ptp; |
| 1608 | } |
| 1609 | } else { |
| 1610 | clk_disable_unprepare(fep->clk_ahb); |
| 1611 | clk_disable_unprepare(fep->clk_ipg); |
| 1612 | if (fep->clk_enet_out) |
| 1613 | clk_disable_unprepare(fep->clk_enet_out); |
| 1614 | if (fep->clk_ptp) |
| 1615 | clk_disable_unprepare(fep->clk_ptp); |
| 1616 | } |
| 1617 | |
| 1618 | return 0; |
| 1619 | failed_clk_ptp: |
| 1620 | if (fep->clk_enet_out) |
| 1621 | clk_disable_unprepare(fep->clk_enet_out); |
| 1622 | failed_clk_enet_out: |
| 1623 | clk_disable_unprepare(fep->clk_ipg); |
| 1624 | failed_clk_ipg: |
| 1625 | clk_disable_unprepare(fep->clk_ahb); |
| 1626 | |
| 1627 | return ret; |
| 1628 | } |
| 1629 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1630 | static int fec_enet_mii_probe(struct net_device *ndev) |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1631 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1632 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1633 | const struct platform_device_id *id_entry = |
| 1634 | platform_get_device_id(fep->pdev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1635 | struct phy_device *phy_dev = NULL; |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1636 | char mdio_bus_id[MII_BUS_ID_SIZE]; |
| 1637 | char phy_name[MII_BUS_ID_SIZE + 3]; |
| 1638 | int phy_id; |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1639 | int dev_id = fep->dev_id; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1640 | |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1641 | fep->phy_dev = NULL; |
| 1642 | |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1643 | /* check for attached phy */ |
| 1644 | for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { |
| 1645 | if ((fep->mii_bus->phy_mask & (1 << phy_id))) |
| 1646 | continue; |
| 1647 | if (fep->mii_bus->phy_map[phy_id] == NULL) |
| 1648 | continue; |
| 1649 | if (fep->mii_bus->phy_map[phy_id]->phy_id == 0) |
| 1650 | continue; |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1651 | if (dev_id--) |
| 1652 | continue; |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1653 | strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); |
| 1654 | break; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1657 | if (phy_id >= PHY_MAX_ADDR) { |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1658 | netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); |
Florian Fainelli | ea51ade9 | 2012-02-13 01:23:22 +0000 | [diff] [blame] | 1659 | strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1660 | phy_id = 0; |
| 1661 | } |
| 1662 | |
Richard Zhao | a7ed07d | 2012-01-29 22:08:12 +0000 | [diff] [blame] | 1663 | snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id); |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1664 | phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1665 | fep->phy_interface); |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1666 | if (IS_ERR(phy_dev)) { |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1667 | netdev_err(ndev, "could not attach to PHY\n"); |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1668 | return PTR_ERR(phy_dev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | /* mask with MAC supported features */ |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1672 | if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) { |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1673 | phy_dev->supported &= PHY_GBIT_FEATURES; |
Russell King | b44592f | 2014-07-08 00:22:34 +0100 | [diff] [blame] | 1674 | phy_dev->supported &= ~SUPPORTED_1000baseT_Half; |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 1675 | #if !defined(CONFIG_M5272) |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1676 | phy_dev->supported |= SUPPORTED_Pause; |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 1677 | #endif |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1678 | } |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1679 | else |
| 1680 | phy_dev->supported &= PHY_BASIC_FEATURES; |
| 1681 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1682 | phy_dev->advertising = phy_dev->supported; |
| 1683 | |
| 1684 | fep->phy_dev = phy_dev; |
| 1685 | fep->link = 0; |
| 1686 | fep->full_duplex = 0; |
| 1687 | |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1688 | netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n", |
| 1689 | fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev), |
| 1690 | fep->phy_dev->irq); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1691 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1692 | return 0; |
| 1693 | } |
| 1694 | |
| 1695 | static int fec_enet_mii_init(struct platform_device *pdev) |
| 1696 | { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1697 | static struct mii_bus *fec0_mii_bus; |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1698 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 1699 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1700 | const struct platform_device_id *id_entry = |
| 1701 | platform_get_device_id(fep->pdev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1702 | int err = -ENXIO, i; |
| 1703 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1704 | /* |
| 1705 | * The dual fec interfaces are not equivalent with enet-mac. |
| 1706 | * Here are the differences: |
| 1707 | * |
| 1708 | * - fec0 supports MII & RMII modes while fec1 only supports RMII |
| 1709 | * - fec0 acts as the 1588 time master while fec1 is slave |
| 1710 | * - external phys can only be configured by fec0 |
| 1711 | * |
| 1712 | * That is to say fec1 can not work independently. It only works |
| 1713 | * when fec0 is working. The reason behind this design is that the |
| 1714 | * second interface is added primarily for Switch mode. |
| 1715 | * |
| 1716 | * Because of the last point above, both phys are attached on fec0 |
| 1717 | * mdio interface in board design, and need to be configured by |
| 1718 | * fec0 mii_bus. |
| 1719 | */ |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1720 | if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1721 | /* fec1 uses fec0 mii_bus */ |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 1722 | if (mii_cnt && fec0_mii_bus) { |
| 1723 | fep->mii_bus = fec0_mii_bus; |
| 1724 | mii_cnt++; |
| 1725 | return 0; |
| 1726 | } |
| 1727 | return -ENOENT; |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1730 | fep->mii_timeout = 0; |
| 1731 | |
| 1732 | /* |
| 1733 | * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed) |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1734 | * |
| 1735 | * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while |
| 1736 | * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 |
| 1737 | * Reference Manual has an error on this, and gets fixed on i.MX6Q |
| 1738 | * document. |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1739 | */ |
Nimrod Andy | 98a6eeb | 2014-05-20 13:23:09 +0800 | [diff] [blame] | 1740 | fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1741 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) |
| 1742 | fep->phy_speed--; |
| 1743 | fep->phy_speed <<= 1; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1744 | writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); |
| 1745 | |
| 1746 | fep->mii_bus = mdiobus_alloc(); |
| 1747 | if (fep->mii_bus == NULL) { |
| 1748 | err = -ENOMEM; |
| 1749 | goto err_out; |
| 1750 | } |
| 1751 | |
| 1752 | fep->mii_bus->name = "fec_enet_mii_bus"; |
| 1753 | fep->mii_bus->read = fec_enet_mdio_read; |
| 1754 | fep->mii_bus->write = fec_enet_mdio_write; |
Florian Fainelli | 391420f | 2012-01-09 23:59:13 +0000 | [diff] [blame] | 1755 | snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
| 1756 | pdev->name, fep->dev_id + 1); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1757 | fep->mii_bus->priv = fep; |
| 1758 | fep->mii_bus->parent = &pdev->dev; |
| 1759 | |
| 1760 | fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); |
| 1761 | if (!fep->mii_bus->irq) { |
| 1762 | err = -ENOMEM; |
| 1763 | goto err_out_free_mdiobus; |
| 1764 | } |
| 1765 | |
| 1766 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 1767 | fep->mii_bus->irq[i] = PHY_POLL; |
| 1768 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1769 | if (mdiobus_register(fep->mii_bus)) |
| 1770 | goto err_out_free_mdio_irq; |
| 1771 | |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 1772 | mii_cnt++; |
| 1773 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1774 | /* save fec0 mii_bus */ |
| 1775 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) |
| 1776 | fec0_mii_bus = fep->mii_bus; |
| 1777 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1778 | return 0; |
| 1779 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1780 | err_out_free_mdio_irq: |
| 1781 | kfree(fep->mii_bus->irq); |
| 1782 | err_out_free_mdiobus: |
| 1783 | mdiobus_free(fep->mii_bus); |
| 1784 | err_out: |
| 1785 | return err; |
| 1786 | } |
| 1787 | |
| 1788 | static void fec_enet_mii_remove(struct fec_enet_private *fep) |
| 1789 | { |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 1790 | if (--mii_cnt == 0) { |
| 1791 | mdiobus_unregister(fep->mii_bus); |
| 1792 | kfree(fep->mii_bus->irq); |
| 1793 | mdiobus_free(fep->mii_bus); |
| 1794 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1797 | static int fec_enet_get_settings(struct net_device *ndev, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1798 | struct ethtool_cmd *cmd) |
| 1799 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1800 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1801 | struct phy_device *phydev = fep->phy_dev; |
| 1802 | |
| 1803 | if (!phydev) |
| 1804 | return -ENODEV; |
| 1805 | |
| 1806 | return phy_ethtool_gset(phydev, cmd); |
| 1807 | } |
| 1808 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1809 | static int fec_enet_set_settings(struct net_device *ndev, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1810 | struct ethtool_cmd *cmd) |
| 1811 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1812 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1813 | struct phy_device *phydev = fep->phy_dev; |
| 1814 | |
| 1815 | if (!phydev) |
| 1816 | return -ENODEV; |
| 1817 | |
| 1818 | return phy_ethtool_sset(phydev, cmd); |
| 1819 | } |
| 1820 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1821 | static void fec_enet_get_drvinfo(struct net_device *ndev, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1822 | struct ethtool_drvinfo *info) |
| 1823 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1824 | struct fec_enet_private *fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 1826 | strlcpy(info->driver, fep->pdev->dev.driver->name, |
| 1827 | sizeof(info->driver)); |
| 1828 | strlcpy(info->version, "Revision: 1.0", sizeof(info->version)); |
| 1829 | strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1831 | |
Frank Li | 5ebae489 | 2013-01-06 16:25:07 +0000 | [diff] [blame] | 1832 | static int fec_enet_get_ts_info(struct net_device *ndev, |
| 1833 | struct ethtool_ts_info *info) |
| 1834 | { |
| 1835 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1836 | |
| 1837 | if (fep->bufdesc_ex) { |
| 1838 | |
| 1839 | info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1840 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1841 | SOF_TIMESTAMPING_SOFTWARE | |
| 1842 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 1843 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1844 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 1845 | if (fep->ptp_clock) |
| 1846 | info->phc_index = ptp_clock_index(fep->ptp_clock); |
| 1847 | else |
| 1848 | info->phc_index = -1; |
| 1849 | |
| 1850 | info->tx_types = (1 << HWTSTAMP_TX_OFF) | |
| 1851 | (1 << HWTSTAMP_TX_ON); |
| 1852 | |
| 1853 | info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | |
| 1854 | (1 << HWTSTAMP_FILTER_ALL); |
| 1855 | return 0; |
| 1856 | } else { |
| 1857 | return ethtool_op_get_ts_info(ndev, info); |
| 1858 | } |
| 1859 | } |
| 1860 | |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 1861 | #if !defined(CONFIG_M5272) |
| 1862 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1863 | static void fec_enet_get_pauseparam(struct net_device *ndev, |
| 1864 | struct ethtool_pauseparam *pause) |
| 1865 | { |
| 1866 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1867 | |
| 1868 | pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; |
| 1869 | pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; |
| 1870 | pause->rx_pause = pause->tx_pause; |
| 1871 | } |
| 1872 | |
| 1873 | static int fec_enet_set_pauseparam(struct net_device *ndev, |
| 1874 | struct ethtool_pauseparam *pause) |
| 1875 | { |
| 1876 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1877 | |
Russell King | 0b146ca | 2014-07-08 00:22:59 +0100 | [diff] [blame] | 1878 | if (!fep->phy_dev) |
| 1879 | return -ENODEV; |
| 1880 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1881 | if (pause->tx_pause != pause->rx_pause) { |
| 1882 | netdev_info(ndev, |
| 1883 | "hardware only support enable/disable both tx and rx"); |
| 1884 | return -EINVAL; |
| 1885 | } |
| 1886 | |
| 1887 | fep->pause_flag = 0; |
| 1888 | |
| 1889 | /* tx pause must be same as rx pause */ |
| 1890 | fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; |
| 1891 | fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; |
| 1892 | |
| 1893 | if (pause->rx_pause || pause->autoneg) { |
| 1894 | fep->phy_dev->supported |= ADVERTISED_Pause; |
| 1895 | fep->phy_dev->advertising |= ADVERTISED_Pause; |
| 1896 | } else { |
| 1897 | fep->phy_dev->supported &= ~ADVERTISED_Pause; |
| 1898 | fep->phy_dev->advertising &= ~ADVERTISED_Pause; |
| 1899 | } |
| 1900 | |
| 1901 | if (pause->autoneg) { |
| 1902 | if (netif_running(ndev)) |
| 1903 | fec_stop(ndev); |
| 1904 | phy_start_aneg(fep->phy_dev); |
| 1905 | } |
| 1906 | if (netif_running(ndev)) |
Russell King | 9671a42 | 2014-07-08 00:22:39 +0100 | [diff] [blame] | 1907 | fec_restart(ndev, fep->full_duplex); |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1908 | |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
Chris Healy | 38ae92d | 2013-06-25 23:18:52 -0700 | [diff] [blame] | 1912 | static const struct fec_stat { |
| 1913 | char name[ETH_GSTRING_LEN]; |
| 1914 | u16 offset; |
| 1915 | } fec_stats[] = { |
| 1916 | /* RMON TX */ |
| 1917 | { "tx_dropped", RMON_T_DROP }, |
| 1918 | { "tx_packets", RMON_T_PACKETS }, |
| 1919 | { "tx_broadcast", RMON_T_BC_PKT }, |
| 1920 | { "tx_multicast", RMON_T_MC_PKT }, |
| 1921 | { "tx_crc_errors", RMON_T_CRC_ALIGN }, |
| 1922 | { "tx_undersize", RMON_T_UNDERSIZE }, |
| 1923 | { "tx_oversize", RMON_T_OVERSIZE }, |
| 1924 | { "tx_fragment", RMON_T_FRAG }, |
| 1925 | { "tx_jabber", RMON_T_JAB }, |
| 1926 | { "tx_collision", RMON_T_COL }, |
| 1927 | { "tx_64byte", RMON_T_P64 }, |
| 1928 | { "tx_65to127byte", RMON_T_P65TO127 }, |
| 1929 | { "tx_128to255byte", RMON_T_P128TO255 }, |
| 1930 | { "tx_256to511byte", RMON_T_P256TO511 }, |
| 1931 | { "tx_512to1023byte", RMON_T_P512TO1023 }, |
| 1932 | { "tx_1024to2047byte", RMON_T_P1024TO2047 }, |
| 1933 | { "tx_GTE2048byte", RMON_T_P_GTE2048 }, |
| 1934 | { "tx_octets", RMON_T_OCTETS }, |
| 1935 | |
| 1936 | /* IEEE TX */ |
| 1937 | { "IEEE_tx_drop", IEEE_T_DROP }, |
| 1938 | { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK }, |
| 1939 | { "IEEE_tx_1col", IEEE_T_1COL }, |
| 1940 | { "IEEE_tx_mcol", IEEE_T_MCOL }, |
| 1941 | { "IEEE_tx_def", IEEE_T_DEF }, |
| 1942 | { "IEEE_tx_lcol", IEEE_T_LCOL }, |
| 1943 | { "IEEE_tx_excol", IEEE_T_EXCOL }, |
| 1944 | { "IEEE_tx_macerr", IEEE_T_MACERR }, |
| 1945 | { "IEEE_tx_cserr", IEEE_T_CSERR }, |
| 1946 | { "IEEE_tx_sqe", IEEE_T_SQE }, |
| 1947 | { "IEEE_tx_fdxfc", IEEE_T_FDXFC }, |
| 1948 | { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK }, |
| 1949 | |
| 1950 | /* RMON RX */ |
| 1951 | { "rx_packets", RMON_R_PACKETS }, |
| 1952 | { "rx_broadcast", RMON_R_BC_PKT }, |
| 1953 | { "rx_multicast", RMON_R_MC_PKT }, |
| 1954 | { "rx_crc_errors", RMON_R_CRC_ALIGN }, |
| 1955 | { "rx_undersize", RMON_R_UNDERSIZE }, |
| 1956 | { "rx_oversize", RMON_R_OVERSIZE }, |
| 1957 | { "rx_fragment", RMON_R_FRAG }, |
| 1958 | { "rx_jabber", RMON_R_JAB }, |
| 1959 | { "rx_64byte", RMON_R_P64 }, |
| 1960 | { "rx_65to127byte", RMON_R_P65TO127 }, |
| 1961 | { "rx_128to255byte", RMON_R_P128TO255 }, |
| 1962 | { "rx_256to511byte", RMON_R_P256TO511 }, |
| 1963 | { "rx_512to1023byte", RMON_R_P512TO1023 }, |
| 1964 | { "rx_1024to2047byte", RMON_R_P1024TO2047 }, |
| 1965 | { "rx_GTE2048byte", RMON_R_P_GTE2048 }, |
| 1966 | { "rx_octets", RMON_R_OCTETS }, |
| 1967 | |
| 1968 | /* IEEE RX */ |
| 1969 | { "IEEE_rx_drop", IEEE_R_DROP }, |
| 1970 | { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK }, |
| 1971 | { "IEEE_rx_crc", IEEE_R_CRC }, |
| 1972 | { "IEEE_rx_align", IEEE_R_ALIGN }, |
| 1973 | { "IEEE_rx_macerr", IEEE_R_MACERR }, |
| 1974 | { "IEEE_rx_fdxfc", IEEE_R_FDXFC }, |
| 1975 | { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK }, |
| 1976 | }; |
| 1977 | |
| 1978 | static void fec_enet_get_ethtool_stats(struct net_device *dev, |
| 1979 | struct ethtool_stats *stats, u64 *data) |
| 1980 | { |
| 1981 | struct fec_enet_private *fep = netdev_priv(dev); |
| 1982 | int i; |
| 1983 | |
| 1984 | for (i = 0; i < ARRAY_SIZE(fec_stats); i++) |
| 1985 | data[i] = readl(fep->hwp + fec_stats[i].offset); |
| 1986 | } |
| 1987 | |
| 1988 | static void fec_enet_get_strings(struct net_device *netdev, |
| 1989 | u32 stringset, u8 *data) |
| 1990 | { |
| 1991 | int i; |
| 1992 | switch (stringset) { |
| 1993 | case ETH_SS_STATS: |
| 1994 | for (i = 0; i < ARRAY_SIZE(fec_stats); i++) |
| 1995 | memcpy(data + i * ETH_GSTRING_LEN, |
| 1996 | fec_stats[i].name, ETH_GSTRING_LEN); |
| 1997 | break; |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | static int fec_enet_get_sset_count(struct net_device *dev, int sset) |
| 2002 | { |
| 2003 | switch (sset) { |
| 2004 | case ETH_SS_STATS: |
| 2005 | return ARRAY_SIZE(fec_stats); |
| 2006 | default: |
| 2007 | return -EOPNOTSUPP; |
| 2008 | } |
| 2009 | } |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 2010 | #endif /* !defined(CONFIG_M5272) */ |
Chris Healy | 38ae92d | 2013-06-25 23:18:52 -0700 | [diff] [blame] | 2011 | |
Chris Healy | 32bc9b4 | 2013-06-17 07:25:06 -0700 | [diff] [blame] | 2012 | static int fec_enet_nway_reset(struct net_device *dev) |
| 2013 | { |
| 2014 | struct fec_enet_private *fep = netdev_priv(dev); |
| 2015 | struct phy_device *phydev = fep->phy_dev; |
| 2016 | |
| 2017 | if (!phydev) |
| 2018 | return -ENODEV; |
| 2019 | |
| 2020 | return genphy_restart_aneg(phydev); |
| 2021 | } |
| 2022 | |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 2023 | static const struct ethtool_ops fec_enet_ethtool_ops = { |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 2024 | #if !defined(CONFIG_M5272) |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 2025 | .get_pauseparam = fec_enet_get_pauseparam, |
| 2026 | .set_pauseparam = fec_enet_set_pauseparam, |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 2027 | #endif |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2028 | .get_settings = fec_enet_get_settings, |
| 2029 | .set_settings = fec_enet_set_settings, |
| 2030 | .get_drvinfo = fec_enet_get_drvinfo, |
| 2031 | .get_link = ethtool_op_get_link, |
Frank Li | 5ebae489 | 2013-01-06 16:25:07 +0000 | [diff] [blame] | 2032 | .get_ts_info = fec_enet_get_ts_info, |
Chris Healy | 32bc9b4 | 2013-06-17 07:25:06 -0700 | [diff] [blame] | 2033 | .nway_reset = fec_enet_nway_reset, |
Chris Healy | 38ae92d | 2013-06-25 23:18:52 -0700 | [diff] [blame] | 2034 | #ifndef CONFIG_M5272 |
| 2035 | .get_ethtool_stats = fec_enet_get_ethtool_stats, |
| 2036 | .get_strings = fec_enet_get_strings, |
| 2037 | .get_sset_count = fec_enet_get_sset_count, |
| 2038 | #endif |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2039 | }; |
| 2040 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2041 | static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2042 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2043 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2044 | struct phy_device *phydev = fep->phy_dev; |
| 2045 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2046 | if (!netif_running(ndev)) |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2047 | return -EINVAL; |
| 2048 | |
| 2049 | if (!phydev) |
| 2050 | return -ENODEV; |
| 2051 | |
Ben Hutchings | 1d5244d | 2013-11-18 23:02:44 +0000 | [diff] [blame] | 2052 | if (fep->bufdesc_ex) { |
| 2053 | if (cmd == SIOCSHWTSTAMP) |
| 2054 | return fec_ptp_set(ndev, rq); |
| 2055 | if (cmd == SIOCGHWTSTAMP) |
| 2056 | return fec_ptp_get(ndev, rq); |
| 2057 | } |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 2058 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 2059 | return phy_mii_ioctl(phydev, rq, cmd); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2062 | static void fec_enet_free_buffers(struct net_device *ndev) |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2063 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2064 | struct fec_enet_private *fep = netdev_priv(ndev); |
Fabio Estevam | da2191e | 2013-03-20 12:31:07 -0300 | [diff] [blame] | 2065 | unsigned int i; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2066 | struct sk_buff *skb; |
| 2067 | struct bufdesc *bdp; |
| 2068 | |
| 2069 | bdp = fep->rx_bd_base; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2070 | for (i = 0; i < fep->rx_ring_size; i++) { |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2071 | skb = fep->rx_skbuff[i]; |
Russell King | 730ee36 | 2014-07-08 00:23:14 +0100 | [diff] [blame] | 2072 | fep->rx_skbuff[i] = NULL; |
| 2073 | if (skb) { |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 2074 | dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2075 | FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2076 | dev_kfree_skb(skb); |
Russell King | 730ee36 | 2014-07-08 00:23:14 +0100 | [diff] [blame] | 2077 | } |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2078 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | bdp = fep->tx_bd_base; |
Russell King | 8b7c9ef | 2014-07-08 00:23:25 +0100 | [diff] [blame] | 2082 | for (i = 0; i < fep->tx_ring_size; i++) { |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2083 | kfree(fep->tx_bounce[i]); |
Russell King | 8b7c9ef | 2014-07-08 00:23:25 +0100 | [diff] [blame] | 2084 | fep->tx_bounce[i] = NULL; |
| 2085 | skb = fep->tx_skbuff[i]; |
| 2086 | fep->tx_skbuff[i] = NULL; |
| 2087 | dev_kfree_skb(skb); |
| 2088 | } |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2091 | static int fec_enet_alloc_buffers(struct net_device *ndev) |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2092 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2093 | struct fec_enet_private *fep = netdev_priv(ndev); |
Fabio Estevam | da2191e | 2013-03-20 12:31:07 -0300 | [diff] [blame] | 2094 | unsigned int i; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2095 | struct sk_buff *skb; |
| 2096 | struct bufdesc *bdp; |
| 2097 | |
| 2098 | bdp = fep->rx_bd_base; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2099 | for (i = 0; i < fep->rx_ring_size; i++) { |
Russell King | 730ee36 | 2014-07-08 00:23:14 +0100 | [diff] [blame] | 2100 | dma_addr_t addr; |
| 2101 | |
Fabio Estevam | b72061a | 2012-02-07 08:27:31 +0000 | [diff] [blame] | 2102 | skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); |
Russell King | ffdce2c | 2014-07-08 00:23:30 +0100 | [diff] [blame^] | 2103 | if (!skb) |
| 2104 | goto err_alloc; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2105 | |
Russell King | 730ee36 | 2014-07-08 00:23:14 +0100 | [diff] [blame] | 2106 | addr = dma_map_single(&fep->pdev->dev, skb->data, |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2107 | FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); |
Russell King | 730ee36 | 2014-07-08 00:23:14 +0100 | [diff] [blame] | 2108 | if (dma_mapping_error(&fep->pdev->dev, addr)) { |
| 2109 | dev_kfree_skb(skb); |
Duan Fugang-B38611 | d842a31 | 2013-11-14 09:57:10 +0800 | [diff] [blame] | 2110 | if (net_ratelimit()) |
| 2111 | netdev_err(ndev, "Rx DMA memory map failed\n"); |
Russell King | ffdce2c | 2014-07-08 00:23:30 +0100 | [diff] [blame^] | 2112 | goto err_alloc; |
Duan Fugang-B38611 | d842a31 | 2013-11-14 09:57:10 +0800 | [diff] [blame] | 2113 | } |
Russell King | 730ee36 | 2014-07-08 00:23:14 +0100 | [diff] [blame] | 2114 | |
| 2115 | fep->rx_skbuff[i] = skb; |
| 2116 | bdp->cbd_bufaddr = addr; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2117 | bdp->cbd_sc = BD_ENET_RX_EMPTY; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 2118 | |
| 2119 | if (fep->bufdesc_ex) { |
| 2120 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 2121 | ebdp->cbd_esc = BD_ENET_RX_INT; |
| 2122 | } |
| 2123 | |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2124 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /* Set the last buffer to wrap. */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2128 | bdp = fec_enet_get_prevdesc(bdp, fep); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2129 | bdp->cbd_sc |= BD_SC_WRAP; |
| 2130 | |
| 2131 | bdp = fep->tx_bd_base; |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2132 | for (i = 0; i < fep->tx_ring_size; i++) { |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2133 | fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); |
Russell King | ffdce2c | 2014-07-08 00:23:30 +0100 | [diff] [blame^] | 2134 | if (!fep->tx_bounce[i]) |
| 2135 | goto err_alloc; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2136 | |
| 2137 | bdp->cbd_sc = 0; |
| 2138 | bdp->cbd_bufaddr = 0; |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 2139 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 2140 | if (fep->bufdesc_ex) { |
| 2141 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
Jim Baxter | 96d2222 | 2013-03-26 05:25:07 +0000 | [diff] [blame] | 2142 | ebdp->cbd_esc = BD_ENET_TX_INT; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2145 | bdp = fec_enet_get_nextdesc(bdp, fep); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /* Set the last buffer to wrap. */ |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2149 | bdp = fec_enet_get_prevdesc(bdp, fep); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2150 | bdp->cbd_sc |= BD_SC_WRAP; |
| 2151 | |
| 2152 | return 0; |
Russell King | ffdce2c | 2014-07-08 00:23:30 +0100 | [diff] [blame^] | 2153 | |
| 2154 | err_alloc: |
| 2155 | fec_enet_free_buffers(ndev); |
| 2156 | return -ENOMEM; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | static int |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2160 | fec_enet_open(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2162 | struct fec_enet_private *fep = netdev_priv(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2163 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
Nimrod Andy | 5bbde4d | 2014-05-27 15:51:08 +0800 | [diff] [blame] | 2165 | pinctrl_pm_select_default_state(&fep->pdev->dev); |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2166 | ret = fec_enet_clk_enable(ndev, true); |
| 2167 | if (ret) |
| 2168 | return ret; |
| 2169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | /* I should reset the ring buffers here, but I don't yet know |
| 2171 | * a simple way to do that. |
| 2172 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2174 | ret = fec_enet_alloc_buffers(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2175 | if (ret) |
| 2176 | return ret; |
| 2177 | |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 2178 | /* Probe and connect to PHY when open the interface */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2179 | ret = fec_enet_mii_probe(ndev); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 2180 | if (ret) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2181 | fec_enet_free_buffers(ndev); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 2182 | return ret; |
| 2183 | } |
Russell King | ce5eaf0 | 2014-02-18 12:55:42 +0000 | [diff] [blame] | 2184 | |
| 2185 | napi_enable(&fep->napi); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2186 | phy_start(fep->phy_dev); |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2187 | netif_start_queue(ndev); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 2188 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | static int |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2192 | fec_enet_close(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2194 | struct fec_enet_private *fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
Russell King | d76cfae | 2014-07-08 00:23:04 +0100 | [diff] [blame] | 2196 | phy_stop(fep->phy_dev); |
| 2197 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 2198 | /* Don't know what to do yet. */ |
Georg Hofmann | 3f104c3 | 2013-03-14 06:54:09 +0000 | [diff] [blame] | 2199 | napi_disable(&fep->napi); |
Russell King | b49cd50 | 2014-07-08 00:22:49 +0100 | [diff] [blame] | 2200 | netif_tx_disable(ndev); |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2201 | fec_stop(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
Russell King | 635cf17 | 2014-07-08 00:22:54 +0100 | [diff] [blame] | 2203 | phy_disconnect(fep->phy_dev); |
Russell King | 0b146ca | 2014-07-08 00:22:59 +0100 | [diff] [blame] | 2204 | fep->phy_dev = NULL; |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 2205 | |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2206 | fec_enet_clk_enable(ndev, false); |
Nimrod Andy | 5bbde4d | 2014-05-27 15:51:08 +0800 | [diff] [blame] | 2207 | pinctrl_pm_select_sleep_state(&fep->pdev->dev); |
Uwe Kleine-König | db8880b | 2011-01-19 20:26:39 +0100 | [diff] [blame] | 2208 | fec_enet_free_buffers(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | return 0; |
| 2211 | } |
| 2212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | /* Set or clear the multicast filter for this adaptor. |
| 2214 | * Skeleton taken from sunlance driver. |
| 2215 | * The CPM Ethernet implementation allows Multicast as well as individual |
| 2216 | * MAC address filtering. Some of the drivers check to make sure it is |
| 2217 | * a group multicast address, and discard those that are not. I guess I |
| 2218 | * will do the same for now, but just remove the test if you want |
| 2219 | * individual filtering as well (do the upper net layers want or support |
| 2220 | * this kind of feature?). |
| 2221 | */ |
| 2222 | |
| 2223 | #define HASH_BITS 6 /* #bits in hash */ |
| 2224 | #define CRC32_POLY 0xEDB88320 |
| 2225 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2226 | static void set_multicast_list(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2228 | struct fec_enet_private *fep = netdev_priv(ndev); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 2229 | struct netdev_hw_addr *ha; |
Jiri Pirko | 48e2f18 | 2010-02-22 09:22:26 +0000 | [diff] [blame] | 2230 | unsigned int i, bit, data, crc, tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | unsigned char hash; |
| 2232 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2233 | if (ndev->flags & IFF_PROMISC) { |
Sascha Hauer | f44d630 | 2009-04-15 03:11:30 +0000 | [diff] [blame] | 2234 | tmp = readl(fep->hwp + FEC_R_CNTRL); |
| 2235 | tmp |= 0x8; |
| 2236 | writel(tmp, fep->hwp + FEC_R_CNTRL); |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2237 | return; |
| 2238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2240 | tmp = readl(fep->hwp + FEC_R_CNTRL); |
| 2241 | tmp &= ~0x8; |
| 2242 | writel(tmp, fep->hwp + FEC_R_CNTRL); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2243 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2244 | if (ndev->flags & IFF_ALLMULTI) { |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2245 | /* Catch all multicast addresses, so set the |
| 2246 | * filter to all 1's |
| 2247 | */ |
| 2248 | writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 2249 | writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2251 | return; |
| 2252 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 2253 | |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2254 | /* Clear filter and add the addresses in hash register |
| 2255 | */ |
| 2256 | writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 2257 | writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2259 | netdev_for_each_mc_addr(ha, ndev) { |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2260 | /* calculate crc32 value of mac address */ |
| 2261 | crc = 0xffffffff; |
| 2262 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2263 | for (i = 0; i < ndev->addr_len; i++) { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 2264 | data = ha->addr[i]; |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2265 | for (bit = 0; bit < 8; bit++, data >>= 1) { |
| 2266 | crc = (crc >> 1) ^ |
| 2267 | (((crc ^ data) & 1) ? CRC32_POLY : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | } |
| 2269 | } |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 2270 | |
| 2271 | /* only upper 6 bits (HASH_BITS) are used |
| 2272 | * which point to specific bit in he hash registers |
| 2273 | */ |
| 2274 | hash = (crc >> (32 - HASH_BITS)) & 0x3f; |
| 2275 | |
| 2276 | if (hash > 31) { |
| 2277 | tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 2278 | tmp |= 1 << (hash - 32); |
| 2279 | writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 2280 | } else { |
| 2281 | tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
| 2282 | tmp |= 1 << hash; |
| 2283 | writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
| 2284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 2288 | /* Set a MAC change in hardware. */ |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2289 | static int |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2290 | fec_set_mac_address(struct net_device *ndev, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2292 | struct fec_enet_private *fep = netdev_priv(ndev); |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2293 | struct sockaddr *addr = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | |
Lucas Stach | 44934fa | 2014-03-30 21:32:08 +0200 | [diff] [blame] | 2295 | if (addr) { |
| 2296 | if (!is_valid_ether_addr(addr->sa_data)) |
| 2297 | return -EADDRNOTAVAIL; |
| 2298 | memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len); |
| 2299 | } |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2300 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2301 | writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | |
| 2302 | (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), |
Sascha Hauer | f44d630 | 2009-04-15 03:11:30 +0000 | [diff] [blame] | 2303 | fep->hwp + FEC_ADDR_LOW); |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2304 | writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), |
Mattias Walström | 7cff094 | 2010-05-05 00:55:48 -0700 | [diff] [blame] | 2305 | fep->hwp + FEC_ADDR_HIGH); |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2306 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | } |
| 2308 | |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 2309 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Ben Hutchings | 49ce9c2 | 2012-07-10 10:56:00 +0000 | [diff] [blame] | 2310 | /** |
| 2311 | * fec_poll_controller - FEC Poll controller function |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 2312 | * @dev: The FEC network adapter |
| 2313 | * |
| 2314 | * Polled functionality used by netconsole and others in non interrupt mode |
| 2315 | * |
| 2316 | */ |
Wei Yongjun | 47a5247 | 2013-03-20 05:06:11 +0000 | [diff] [blame] | 2317 | static void fec_poll_controller(struct net_device *dev) |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 2318 | { |
| 2319 | int i; |
| 2320 | struct fec_enet_private *fep = netdev_priv(dev); |
| 2321 | |
| 2322 | for (i = 0; i < FEC_IRQ_NUM; i++) { |
| 2323 | if (fep->irq[i] > 0) { |
| 2324 | disable_irq(fep->irq[i]); |
| 2325 | fec_enet_interrupt(fep->irq[i], dev); |
| 2326 | enable_irq(fep->irq[i]); |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | #endif |
| 2331 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 2332 | static int fec_set_features(struct net_device *netdev, |
| 2333 | netdev_features_t features) |
| 2334 | { |
| 2335 | struct fec_enet_private *fep = netdev_priv(netdev); |
| 2336 | netdev_features_t changed = features ^ netdev->features; |
| 2337 | |
| 2338 | netdev->features = features; |
| 2339 | |
| 2340 | /* Receive checksum has been changed */ |
| 2341 | if (changed & NETIF_F_RXCSUM) { |
| 2342 | if (features & NETIF_F_RXCSUM) |
| 2343 | fep->csum_flags |= FLAG_RX_CSUM_ENABLED; |
| 2344 | else |
| 2345 | fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; |
| 2346 | |
| 2347 | if (netif_running(netdev)) { |
| 2348 | fec_stop(netdev); |
| 2349 | fec_restart(netdev, fep->phy_dev->duplex); |
| 2350 | netif_wake_queue(netdev); |
| 2351 | } else { |
| 2352 | fec_restart(netdev, fep->phy_dev->duplex); |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2359 | static const struct net_device_ops fec_netdev_ops = { |
| 2360 | .ndo_open = fec_enet_open, |
| 2361 | .ndo_stop = fec_enet_close, |
| 2362 | .ndo_start_xmit = fec_enet_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 2363 | .ndo_set_rx_mode = set_multicast_list, |
Ben Hutchings | 635ecaa | 2009-07-09 17:59:01 +0000 | [diff] [blame] | 2364 | .ndo_change_mtu = eth_change_mtu, |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2365 | .ndo_validate_addr = eth_validate_addr, |
| 2366 | .ndo_tx_timeout = fec_timeout, |
| 2367 | .ndo_set_mac_address = fec_set_mac_address, |
Uwe Kleine-König | db8880b | 2011-01-19 20:26:39 +0100 | [diff] [blame] | 2368 | .ndo_do_ioctl = fec_enet_ioctl, |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 2369 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2370 | .ndo_poll_controller = fec_poll_controller, |
| 2371 | #endif |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 2372 | .ndo_set_features = fec_set_features, |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 2373 | }; |
| 2374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | /* |
| 2376 | * XXX: We need to clean up on failure exits here. |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2377 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2379 | static int fec_enet_init(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2381 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 2382 | const struct platform_device_id *id_entry = |
| 2383 | platform_get_device_id(fep->pdev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 2384 | struct bufdesc *cbd_base; |
Nimrod Andy | 55d0218 | 2014-06-12 08:16:21 +0800 | [diff] [blame] | 2385 | int bd_size; |
| 2386 | |
| 2387 | /* init the tx & rx ring size */ |
| 2388 | fep->tx_ring_size = TX_RING_SIZE; |
| 2389 | fep->rx_ring_size = RX_RING_SIZE; |
| 2390 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 2391 | fep->tx_stop_threshold = FEC_MAX_SKB_DESCS; |
| 2392 | fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2; |
| 2393 | |
Nimrod Andy | 55d0218 | 2014-06-12 08:16:21 +0800 | [diff] [blame] | 2394 | if (fep->bufdesc_ex) |
| 2395 | fep->bufdesc_size = sizeof(struct bufdesc_ex); |
| 2396 | else |
| 2397 | fep->bufdesc_size = sizeof(struct bufdesc); |
| 2398 | bd_size = (fep->tx_ring_size + fep->rx_ring_size) * |
| 2399 | fep->bufdesc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | |
Sascha Hauer | 8d4dd5c | 2009-04-15 01:32:17 +0000 | [diff] [blame] | 2401 | /* Allocate memory for buffer descriptors. */ |
Nimrod Andy | 55d0218 | 2014-06-12 08:16:21 +0800 | [diff] [blame] | 2402 | cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma, |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 2403 | GFP_KERNEL); |
| 2404 | if (!cbd_base) |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 2405 | return -ENOMEM; |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 2406 | |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 2407 | fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE, |
| 2408 | &fep->tso_hdrs_dma, GFP_KERNEL); |
| 2409 | if (!fep->tso_hdrs) { |
| 2410 | dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma); |
| 2411 | return -ENOMEM; |
| 2412 | } |
| 2413 | |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 2414 | memset(cbd_base, 0, PAGE_SIZE); |
Sebastian Siewior | 3b2b74c | 2008-05-01 14:08:12 +1000 | [diff] [blame] | 2415 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2416 | fep->netdev = ndev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 2418 | /* Get the Ethernet address */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2419 | fec_get_mac(ndev); |
Lucas Stach | 44934fa | 2014-03-30 21:32:08 +0200 | [diff] [blame] | 2420 | /* make sure MAC we just acquired is programmed into the hw */ |
| 2421 | fec_set_mac_address(ndev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | |
Sascha Hauer | 8d4dd5c | 2009-04-15 01:32:17 +0000 | [diff] [blame] | 2423 | /* Set receive and transmit descriptor base. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | fep->rx_bd_base = cbd_base; |
Nimrod Andy | 55d0218 | 2014-06-12 08:16:21 +0800 | [diff] [blame] | 2425 | if (fep->bufdesc_ex) |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 2426 | fep->tx_bd_base = (struct bufdesc *) |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2427 | (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size); |
Nimrod Andy | 55d0218 | 2014-06-12 08:16:21 +0800 | [diff] [blame] | 2428 | else |
Duan Fugang-B38611 | 36e24e2 | 2013-09-03 10:41:18 +0800 | [diff] [blame] | 2429 | fep->tx_bd_base = cbd_base + fep->rx_ring_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 2431 | /* The FEC Ethernet specific entries in the device structure */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2432 | ndev->watchdog_timeo = TX_TIMEOUT; |
| 2433 | ndev->netdev_ops = &fec_netdev_ops; |
| 2434 | ndev->ethtool_ops = &fec_enet_ethtool_ops; |
Rob Herring | 633e753 | 2010-02-05 08:56:20 +0000 | [diff] [blame] | 2435 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 2436 | writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); |
Fabio Estevam | 322555f | 2013-08-27 17:35:08 -0300 | [diff] [blame] | 2437 | netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT); |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 2438 | |
Nimrod Andy | 09d1e54 | 2014-06-12 08:16:20 +0800 | [diff] [blame] | 2439 | if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN) |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 2440 | /* enable hw VLAN support */ |
| 2441 | ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; |
Jim Baxter | cdffcf1 | 2013-07-02 22:52:56 +0100 | [diff] [blame] | 2442 | |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 2443 | if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) { |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 2444 | ndev->gso_max_segs = FEC_MAX_TSO_SEGS; |
| 2445 | |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 2446 | /* enable hw accelerator */ |
| 2447 | ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
Nimrod Andy | 79f3391 | 2014-06-12 08:16:23 +0800 | [diff] [blame] | 2448 | | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO); |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 2449 | fep->csum_flags |= FLAG_RX_CSUM_ENABLED; |
| 2450 | } |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 2451 | |
Nimrod Andy | 09d1e54 | 2014-06-12 08:16:20 +0800 | [diff] [blame] | 2452 | ndev->hw_features = ndev->features; |
| 2453 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 2454 | fec_restart(ndev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | return 0; |
| 2457 | } |
| 2458 | |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2459 | #ifdef CONFIG_OF |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 2460 | static void fec_reset_phy(struct platform_device *pdev) |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2461 | { |
| 2462 | int err, phy_reset; |
Shawn Guo | a3caad0 | 2012-06-27 03:45:24 +0000 | [diff] [blame] | 2463 | int msec = 1; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2464 | struct device_node *np = pdev->dev.of_node; |
| 2465 | |
| 2466 | if (!np) |
Shawn Guo | a9b2c8e | 2011-09-23 02:12:46 +0000 | [diff] [blame] | 2467 | return; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2468 | |
Shawn Guo | a3caad0 | 2012-06-27 03:45:24 +0000 | [diff] [blame] | 2469 | of_property_read_u32(np, "phy-reset-duration", &msec); |
| 2470 | /* A sane reset duration should not be longer than 1s */ |
| 2471 | if (msec > 1000) |
| 2472 | msec = 1; |
| 2473 | |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2474 | phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0); |
Fabio Estevam | 07dcf8e | 2013-02-18 10:20:31 +0000 | [diff] [blame] | 2475 | if (!gpio_is_valid(phy_reset)) |
| 2476 | return; |
| 2477 | |
Shawn Guo | 119fc00 | 2012-06-27 03:45:22 +0000 | [diff] [blame] | 2478 | err = devm_gpio_request_one(&pdev->dev, phy_reset, |
| 2479 | GPIOF_OUT_INIT_LOW, "phy-reset"); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2480 | if (err) { |
Fabio Estevam | 07dcf8e | 2013-02-18 10:20:31 +0000 | [diff] [blame] | 2481 | dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err); |
Shawn Guo | a9b2c8e | 2011-09-23 02:12:46 +0000 | [diff] [blame] | 2482 | return; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2483 | } |
Shawn Guo | a3caad0 | 2012-06-27 03:45:24 +0000 | [diff] [blame] | 2484 | msleep(msec); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2485 | gpio_set_value(phy_reset, 1); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2486 | } |
| 2487 | #else /* CONFIG_OF */ |
Fabio Estevam | 0c7768a | 2013-01-07 17:42:56 +0000 | [diff] [blame] | 2488 | static void fec_reset_phy(struct platform_device *pdev) |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2489 | { |
| 2490 | /* |
| 2491 | * In case of platform probe, the reset has been done |
| 2492 | * by machine code. |
| 2493 | */ |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2494 | } |
| 2495 | #endif /* CONFIG_OF */ |
| 2496 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 2497 | static int |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2498 | fec_probe(struct platform_device *pdev) |
| 2499 | { |
| 2500 | struct fec_enet_private *fep; |
Baruch Siach | 5eb32bd | 2010-05-24 00:36:13 -0700 | [diff] [blame] | 2501 | struct fec_platform_data *pdata; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2502 | struct net_device *ndev; |
| 2503 | int i, irq, ret = 0; |
| 2504 | struct resource *r; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2505 | const struct of_device_id *of_id; |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 2506 | static int dev_id; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2507 | |
| 2508 | of_id = of_match_device(fec_dt_ids, &pdev->dev); |
| 2509 | if (of_id) |
| 2510 | pdev->id_entry = of_id->data; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2511 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2512 | /* Init network device */ |
| 2513 | ndev = alloc_etherdev(sizeof(struct fec_enet_private)); |
Fabio Estevam | 83e519b | 2013-03-11 07:32:55 +0000 | [diff] [blame] | 2514 | if (!ndev) |
| 2515 | return -ENOMEM; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2516 | |
| 2517 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 2518 | |
| 2519 | /* setup board info structure */ |
| 2520 | fep = netdev_priv(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2521 | |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 2522 | #if !defined(CONFIG_M5272) |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 2523 | /* default enable pause frame auto negotiation */ |
| 2524 | if (pdev->id_entry && |
| 2525 | (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT)) |
| 2526 | fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; |
Guenter Roeck | d139193 | 2013-06-18 10:04:59 -0700 | [diff] [blame] | 2527 | #endif |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 2528 | |
Nimrod Andy | 5bbde4d | 2014-05-27 15:51:08 +0800 | [diff] [blame] | 2529 | /* Select default pin state */ |
| 2530 | pinctrl_pm_select_default_state(&pdev->dev); |
| 2531 | |
Fabio Estevam | 399db75 | 2013-07-21 13:25:03 -0300 | [diff] [blame] | 2532 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Tushar Behera | 941e173 | 2013-06-10 17:05:05 +0530 | [diff] [blame] | 2533 | fep->hwp = devm_ioremap_resource(&pdev->dev, r); |
| 2534 | if (IS_ERR(fep->hwp)) { |
| 2535 | ret = PTR_ERR(fep->hwp); |
| 2536 | goto failed_ioremap; |
| 2537 | } |
| 2538 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2539 | fep->pdev = pdev; |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 2540 | fep->dev_id = dev_id++; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2541 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 2542 | fep->bufdesc_ex = 0; |
| 2543 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2544 | platform_set_drvdata(pdev, ndev); |
| 2545 | |
Guenter Roeck | 6c5f780 | 2013-04-02 09:35:10 +0000 | [diff] [blame] | 2546 | ret = of_get_phy_mode(pdev->dev.of_node); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2547 | if (ret < 0) { |
Jingoo Han | 94660ba | 2013-08-30 13:56:26 +0900 | [diff] [blame] | 2548 | pdata = dev_get_platdata(&pdev->dev); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2549 | if (pdata) |
| 2550 | fep->phy_interface = pdata->phy; |
| 2551 | else |
| 2552 | fep->phy_interface = PHY_INTERFACE_MODE_MII; |
| 2553 | } else { |
| 2554 | fep->phy_interface = ret; |
| 2555 | } |
| 2556 | |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 2557 | fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 2558 | if (IS_ERR(fep->clk_ipg)) { |
| 2559 | ret = PTR_ERR(fep->clk_ipg); |
| 2560 | goto failed_clk; |
| 2561 | } |
| 2562 | |
| 2563 | fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 2564 | if (IS_ERR(fep->clk_ahb)) { |
| 2565 | ret = PTR_ERR(fep->clk_ahb); |
| 2566 | goto failed_clk; |
| 2567 | } |
| 2568 | |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 2569 | /* enet_out is optional, depends on board */ |
| 2570 | fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out"); |
| 2571 | if (IS_ERR(fep->clk_enet_out)) |
| 2572 | fep->clk_enet_out = NULL; |
| 2573 | |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 2574 | fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); |
Fabio Estevam | 7f7d6c2 | 2013-02-21 09:21:50 +0000 | [diff] [blame] | 2575 | fep->bufdesc_ex = |
| 2576 | pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX; |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 2577 | if (IS_ERR(fep->clk_ptp)) { |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 2578 | fep->clk_ptp = NULL; |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 2579 | fep->bufdesc_ex = 0; |
| 2580 | } |
| 2581 | |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2582 | ret = fec_enet_clk_enable(ndev, true); |
Fabio Estevam | 13a097b | 2013-07-21 13:25:02 -0300 | [diff] [blame] | 2583 | if (ret) |
| 2584 | goto failed_clk; |
| 2585 | |
Fabio Estevam | f4e9f3d | 2013-05-27 03:48:29 +0000 | [diff] [blame] | 2586 | fep->reg_phy = devm_regulator_get(&pdev->dev, "phy"); |
| 2587 | if (!IS_ERR(fep->reg_phy)) { |
| 2588 | ret = regulator_enable(fep->reg_phy); |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 2589 | if (ret) { |
| 2590 | dev_err(&pdev->dev, |
| 2591 | "Failed to enable phy regulator: %d\n", ret); |
| 2592 | goto failed_regulator; |
| 2593 | } |
Fabio Estevam | f6a4d60 | 2013-05-27 03:48:31 +0000 | [diff] [blame] | 2594 | } else { |
| 2595 | fep->reg_phy = NULL; |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | fec_reset_phy(pdev); |
| 2599 | |
Fabio Estevam | 7f7d6c2 | 2013-02-21 09:21:50 +0000 | [diff] [blame] | 2600 | if (fep->bufdesc_ex) |
Fabio Estevam | ca162a8 | 2013-06-07 10:48:00 +0000 | [diff] [blame] | 2601 | fec_ptp_init(pdev); |
Fabio Estevam | 7f7d6c2 | 2013-02-21 09:21:50 +0000 | [diff] [blame] | 2602 | |
| 2603 | ret = fec_enet_init(ndev); |
| 2604 | if (ret) |
| 2605 | goto failed_init; |
| 2606 | |
Xiao Jiang | c7c83d1 | 2011-09-29 02:15:56 +0000 | [diff] [blame] | 2607 | for (i = 0; i < FEC_IRQ_NUM; i++) { |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2608 | irq = platform_get_irq(pdev, i); |
Lothar Waßmann | 86f9f2c | 2011-12-07 21:59:28 +0000 | [diff] [blame] | 2609 | if (irq < 0) { |
| 2610 | if (i) |
| 2611 | break; |
| 2612 | ret = irq; |
| 2613 | goto failed_irq; |
| 2614 | } |
Fabio Estevam | 0d9b2ab | 2013-07-21 13:25:04 -0300 | [diff] [blame] | 2615 | ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt, |
Michael Opdenacker | 44a272d | 2013-09-13 05:44:38 +0200 | [diff] [blame] | 2616 | 0, pdev->name, ndev); |
Fabio Estevam | 0d9b2ab | 2013-07-21 13:25:04 -0300 | [diff] [blame] | 2617 | if (ret) |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2618 | goto failed_irq; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2621 | ret = fec_enet_mii_init(pdev); |
| 2622 | if (ret) |
| 2623 | goto failed_mii_init; |
| 2624 | |
Oskar Schirmer | 03c698c | 2010-10-07 02:30:30 +0000 | [diff] [blame] | 2625 | /* Carrier starts down, phylib will bring it up */ |
| 2626 | netif_carrier_off(ndev); |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2627 | fec_enet_clk_enable(ndev, false); |
Nimrod Andy | 5bbde4d | 2014-05-27 15:51:08 +0800 | [diff] [blame] | 2628 | pinctrl_pm_select_sleep_state(&pdev->dev); |
Oskar Schirmer | 03c698c | 2010-10-07 02:30:30 +0000 | [diff] [blame] | 2629 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2630 | ret = register_netdev(ndev); |
| 2631 | if (ret) |
| 2632 | goto failed_register; |
| 2633 | |
Fabio Estevam | eb1d064 | 2013-04-13 07:25:36 +0000 | [diff] [blame] | 2634 | if (fep->bufdesc_ex && fep->ptp_clock) |
| 2635 | netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); |
| 2636 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 2637 | INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2638 | return 0; |
| 2639 | |
| 2640 | failed_register: |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2641 | fec_enet_mii_remove(fep); |
| 2642 | failed_mii_init: |
Fabio Estevam | 7a2bbd8 | 2013-05-27 03:48:30 +0000 | [diff] [blame] | 2643 | failed_irq: |
Fabio Estevam | 7a2bbd8 | 2013-05-27 03:48:30 +0000 | [diff] [blame] | 2644 | failed_init: |
Fabio Estevam | f6a4d60 | 2013-05-27 03:48:31 +0000 | [diff] [blame] | 2645 | if (fep->reg_phy) |
| 2646 | regulator_disable(fep->reg_phy); |
Shawn Guo | 5fa9c0f | 2012-06-27 03:45:21 +0000 | [diff] [blame] | 2647 | failed_regulator: |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2648 | fec_enet_clk_enable(ndev, false); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2649 | failed_clk: |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2650 | failed_ioremap: |
| 2651 | free_netdev(ndev); |
| 2652 | |
| 2653 | return ret; |
| 2654 | } |
| 2655 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 2656 | static int |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2657 | fec_drv_remove(struct platform_device *pdev) |
| 2658 | { |
| 2659 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 2660 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 2661 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 2662 | cancel_delayed_work_sync(&(fep->delay_work.delay_work)); |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 2663 | unregister_netdev(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2664 | fec_enet_mii_remove(fep); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 2665 | del_timer_sync(&fep->time_keep); |
Fabio Estevam | f6a4d60 | 2013-05-27 03:48:31 +0000 | [diff] [blame] | 2666 | if (fep->reg_phy) |
| 2667 | regulator_disable(fep->reg_phy); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 2668 | if (fep->ptp_clock) |
| 2669 | ptp_clock_unregister(fep->ptp_clock); |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2670 | fec_enet_clk_enable(ndev, false); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2671 | free_netdev(ndev); |
Uwe Kleine-König | 28e2188 | 2011-01-13 21:44:18 +0100 | [diff] [blame] | 2672 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2673 | return 0; |
| 2674 | } |
| 2675 | |
Fabio Estevam | bf7bfd7 | 2013-04-16 08:17:46 +0000 | [diff] [blame] | 2676 | #ifdef CONFIG_PM_SLEEP |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2677 | static int |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2678 | fec_suspend(struct device *dev) |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2679 | { |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2680 | struct net_device *ndev = dev_get_drvdata(dev); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2681 | struct fec_enet_private *fep = netdev_priv(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2682 | |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2683 | if (netif_running(ndev)) { |
Russell King | d76cfae | 2014-07-08 00:23:04 +0100 | [diff] [blame] | 2684 | phy_stop(fep->phy_dev); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2685 | fec_stop(ndev); |
| 2686 | netif_device_detach(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2687 | } |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2688 | fec_enet_clk_enable(ndev, false); |
Nimrod Andy | 5bbde4d | 2014-05-27 15:51:08 +0800 | [diff] [blame] | 2689 | pinctrl_pm_select_sleep_state(&fep->pdev->dev); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2690 | |
Fabio Estevam | 238f7bc | 2013-05-27 03:48:33 +0000 | [diff] [blame] | 2691 | if (fep->reg_phy) |
| 2692 | regulator_disable(fep->reg_phy); |
| 2693 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2694 | return 0; |
| 2695 | } |
| 2696 | |
| 2697 | static int |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2698 | fec_resume(struct device *dev) |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2699 | { |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2700 | struct net_device *ndev = dev_get_drvdata(dev); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2701 | struct fec_enet_private *fep = netdev_priv(ndev); |
Fabio Estevam | 238f7bc | 2013-05-27 03:48:33 +0000 | [diff] [blame] | 2702 | int ret; |
| 2703 | |
| 2704 | if (fep->reg_phy) { |
| 2705 | ret = regulator_enable(fep->reg_phy); |
| 2706 | if (ret) |
| 2707 | return ret; |
| 2708 | } |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2709 | |
Nimrod Andy | 5bbde4d | 2014-05-27 15:51:08 +0800 | [diff] [blame] | 2710 | pinctrl_pm_select_default_state(&fep->pdev->dev); |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2711 | ret = fec_enet_clk_enable(ndev, true); |
Fabio Estevam | 13a097b | 2013-07-21 13:25:02 -0300 | [diff] [blame] | 2712 | if (ret) |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2713 | goto failed_clk; |
Fabio Estevam | 13a097b | 2013-07-21 13:25:02 -0300 | [diff] [blame] | 2714 | |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2715 | if (netif_running(ndev)) { |
| 2716 | fec_restart(ndev, fep->full_duplex); |
| 2717 | netif_device_attach(ndev); |
Russell King | d76cfae | 2014-07-08 00:23:04 +0100 | [diff] [blame] | 2718 | phy_start(fep->phy_dev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2719 | } |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2720 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2721 | return 0; |
Fabio Estevam | 13a097b | 2013-07-21 13:25:02 -0300 | [diff] [blame] | 2722 | |
Nimrod Andy | e8fcfcd | 2014-05-20 13:22:51 +0800 | [diff] [blame] | 2723 | failed_clk: |
Fabio Estevam | 13a097b | 2013-07-21 13:25:02 -0300 | [diff] [blame] | 2724 | if (fep->reg_phy) |
| 2725 | regulator_disable(fep->reg_phy); |
| 2726 | return ret; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2727 | } |
Fabio Estevam | bf7bfd7 | 2013-04-16 08:17:46 +0000 | [diff] [blame] | 2728 | #endif /* CONFIG_PM_SLEEP */ |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2729 | |
Fabio Estevam | bf7bfd7 | 2013-04-16 08:17:46 +0000 | [diff] [blame] | 2730 | static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume); |
Denis Kirjanov | 59d4289 | 2010-06-02 09:27:04 +0000 | [diff] [blame] | 2731 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2732 | static struct platform_driver fec_driver = { |
| 2733 | .driver = { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 2734 | .name = DRIVER_NAME, |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2735 | .owner = THIS_MODULE, |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2736 | .pm = &fec_pm_ops, |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2737 | .of_match_table = fec_dt_ids, |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2738 | }, |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 2739 | .id_table = fec_devtype, |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2740 | .probe = fec_probe, |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 2741 | .remove = fec_drv_remove, |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2742 | }; |
| 2743 | |
Fabio Estevam | aaca237 | 2012-01-23 16:44:50 +0000 | [diff] [blame] | 2744 | module_platform_driver(fec_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | |
Fabio Estevam | f8c0aca | 2013-07-20 16:20:36 -0300 | [diff] [blame] | 2746 | MODULE_ALIAS("platform:"DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | MODULE_LICENSE("GPL"); |