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/init.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/etherdevice.h> |
| 36 | #include <linux/skbuff.h> |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 37 | #include <linux/in.h> |
| 38 | #include <linux/ip.h> |
| 39 | #include <net/ip.h> |
| 40 | #include <linux/tcp.h> |
| 41 | #include <linux/udp.h> |
| 42 | #include <linux/icmp.h> |
Linus 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Greg Ungerer | 080853a | 2007-07-30 16:28:46 +1000 | [diff] [blame] | 58 | #include <asm/cacheflush.h> |
Sascha Hauer | 196719e | 2009-01-28 23:03:10 +0000 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include "fec.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Uwe Kleine-König | 085e79e | 2011-01-17 09:52:18 +0100 | [diff] [blame] | 62 | #if defined(CONFIG_ARM) |
Sascha Hauer | 196719e | 2009-01-28 23:03:10 +0000 | [diff] [blame] | 63 | #define FEC_ALIGNMENT 0xf |
| 64 | #else |
| 65 | #define FEC_ALIGNMENT 0x3 |
| 66 | #endif |
| 67 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 68 | #define DRIVER_NAME "fec" |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 69 | #define FEC_NAPI_WEIGHT 64 |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 70 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 71 | /* Pause frame feild and FIFO threshold */ |
| 72 | #define FEC_ENET_FCE (1 << 5) |
| 73 | #define FEC_ENET_RSEM_V 0x84 |
| 74 | #define FEC_ENET_RSFL_V 16 |
| 75 | #define FEC_ENET_RAEM_V 0x8 |
| 76 | #define FEC_ENET_RAFL_V 0x8 |
| 77 | #define FEC_ENET_OPD_V 0xFFF0 |
| 78 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 79 | /* Controller is ENET-MAC */ |
| 80 | #define FEC_QUIRK_ENET_MAC (1 << 0) |
| 81 | /* Controller needs driver to swap frame */ |
| 82 | #define FEC_QUIRK_SWAP_FRAME (1 << 1) |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 83 | /* Controller uses gasket */ |
| 84 | #define FEC_QUIRK_USE_GASKET (1 << 2) |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 85 | /* Controller has GBIT support */ |
| 86 | #define FEC_QUIRK_HAS_GBIT (1 << 3) |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 87 | /* Controller has extend desc buffer */ |
| 88 | #define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4) |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 89 | /* Controller has hardware checksum support */ |
| 90 | #define FEC_QUIRK_HAS_CSUM (1 << 5) |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 91 | |
| 92 | static struct platform_device_id fec_devtype[] = { |
| 93 | { |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 94 | /* keep it for coldfire */ |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 95 | .name = DRIVER_NAME, |
| 96 | .driver_data = 0, |
| 97 | }, { |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 98 | .name = "imx25-fec", |
| 99 | .driver_data = FEC_QUIRK_USE_GASKET, |
| 100 | }, { |
| 101 | .name = "imx27-fec", |
| 102 | .driver_data = 0, |
| 103 | }, { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 104 | .name = "imx28-fec", |
| 105 | .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME, |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 106 | }, { |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 107 | .name = "imx6q-fec", |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 108 | .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 109 | FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 110 | }, { |
Shawn Guo | 3680354 | 2013-05-19 04:38:46 +0000 | [diff] [blame] | 111 | .name = "mvf600-fec", |
Jingchang Lu | ca7c4a4 | 2013-04-11 21:12:45 +0000 | [diff] [blame] | 112 | .driver_data = FEC_QUIRK_ENET_MAC, |
| 113 | }, { |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 114 | /* sentinel */ |
| 115 | } |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 116 | }; |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 117 | MODULE_DEVICE_TABLE(platform, fec_devtype); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 118 | |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 119 | enum imx_fec_type { |
Lothar Waßmann | a7dd321 | 2011-12-07 21:59:25 +0000 | [diff] [blame] | 120 | IMX25_FEC = 1, /* runs on i.mx25/50/53 */ |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 121 | IMX27_FEC, /* runs on i.mx27/35/51 */ |
| 122 | IMX28_FEC, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 123 | IMX6Q_FEC, |
Shawn Guo | 3680354 | 2013-05-19 04:38:46 +0000 | [diff] [blame] | 124 | MVF600_FEC, |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static const struct of_device_id fec_dt_ids[] = { |
| 128 | { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, |
| 129 | { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, |
| 130 | { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 131 | { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, |
Shawn Guo | 3680354 | 2013-05-19 04:38:46 +0000 | [diff] [blame] | 132 | { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 133 | { /* sentinel */ } |
| 134 | }; |
| 135 | MODULE_DEVICE_TABLE(of, fec_dt_ids); |
| 136 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 137 | static unsigned char macaddr[ETH_ALEN]; |
| 138 | module_param_array(macaddr, byte, NULL, 0); |
| 139 | MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); |
| 140 | |
Greg Ungerer | 87f4abb | 2008-06-06 15:55:36 +1000 | [diff] [blame] | 141 | #if defined(CONFIG_M5272) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Some hardware gets it MAC address out of local flash memory. |
| 144 | * if this is non-zero then assume it is the address to get MAC from. |
| 145 | */ |
| 146 | #if defined(CONFIG_NETtel) |
| 147 | #define FEC_FLASHMAC 0xf0006006 |
| 148 | #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) |
| 149 | #define FEC_FLASHMAC 0xf0006000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | #elif defined(CONFIG_CANCam) |
| 151 | #define FEC_FLASHMAC 0xf0020000 |
Greg Ungerer | 7dd6a2a | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 152 | #elif defined (CONFIG_M5272C3) |
| 153 | #define FEC_FLASHMAC (0xffe04000 + 4) |
| 154 | #elif defined(CONFIG_MOD5272) |
Lothar Waßmann | a7dd321 | 2011-12-07 21:59:25 +0000 | [diff] [blame] | 155 | #define FEC_FLASHMAC 0xffc0406b |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | #else |
| 157 | #define FEC_FLASHMAC 0 |
| 158 | #endif |
Greg Ungerer | 43be636 | 2009-02-26 22:42:51 -0800 | [diff] [blame] | 159 | #endif /* CONFIG_M5272 */ |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 160 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 161 | #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE) |
Matt Waddel | 6b26529 | 2006-06-27 13:10:56 +1000 | [diff] [blame] | 162 | #error "FEC: descriptor ring size constants too large" |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 163 | #endif |
| 164 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 165 | /* Interrupt events/masks. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */ |
| 167 | #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */ |
| 168 | #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */ |
| 169 | #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */ |
| 170 | #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */ |
| 171 | #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */ |
| 172 | #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */ |
| 173 | #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */ |
| 174 | #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */ |
| 175 | #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */ |
| 176 | |
Wolfram Sang | 4bee1f9 | 2010-07-21 02:51:13 +0000 | [diff] [blame] | 177 | #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] | 178 | #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF)) |
Wolfram Sang | 4bee1f9 | 2010-07-21 02:51:13 +0000 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* The FEC stores dest/src/type, data, and checksum for receive packets. |
| 181 | */ |
| 182 | #define PKT_MAXBUF_SIZE 1518 |
| 183 | #define PKT_MINBUF_SIZE 64 |
| 184 | #define PKT_MAXBLR_SIZE 1520 |
| 185 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 186 | /* FEC receive acceleration */ |
| 187 | #define FEC_RACC_IPDIS (1 << 1) |
| 188 | #define FEC_RACC_PRODIS (1 << 2) |
| 189 | #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS) |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* |
Matt Waddel | 6b26529 | 2006-06-27 13:10:56 +1000 | [diff] [blame] | 192 | * 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] | 193 | * size bits. Other FEC hardware does not, so we need to take that into |
| 194 | * account when setting it. |
| 195 | */ |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 196 | #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ |
Uwe Kleine-König | 085e79e | 2011-01-17 09:52:18 +0100 | [diff] [blame] | 197 | defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) |
| 199 | #else |
| 200 | #define OPT_FRAME_SIZE 0 |
| 201 | #endif |
| 202 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 203 | /* FEC MII MMFR bits definition */ |
| 204 | #define FEC_MMFR_ST (1 << 30) |
| 205 | #define FEC_MMFR_OP_READ (2 << 28) |
| 206 | #define FEC_MMFR_OP_WRITE (1 << 28) |
| 207 | #define FEC_MMFR_PA(v) ((v & 0x1f) << 23) |
| 208 | #define FEC_MMFR_RA(v) ((v & 0x1f) << 18) |
| 209 | #define FEC_MMFR_TA (2 << 16) |
| 210 | #define FEC_MMFR_DATA(v) (v & 0xffff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Rogerio Pimentel | c3b084c | 2011-12-27 14:07:37 -0500 | [diff] [blame] | 212 | #define FEC_MII_TIMEOUT 30000 /* us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 214 | /* Transmitter timeout */ |
| 215 | #define TX_TIMEOUT (2 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 217 | #define FEC_PAUSE_FLAG_AUTONEG 0x1 |
| 218 | #define FEC_PAUSE_FLAG_ENABLE 0x2 |
| 219 | |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 220 | static int mii_cnt; |
| 221 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 222 | static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex) |
| 223 | { |
| 224 | struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp; |
| 225 | if (is_ex) |
| 226 | return (struct bufdesc *)(ex + 1); |
| 227 | else |
| 228 | return bdp + 1; |
| 229 | } |
| 230 | |
| 231 | static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex) |
| 232 | { |
| 233 | struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp; |
| 234 | if (is_ex) |
| 235 | return (struct bufdesc *)(ex - 1); |
| 236 | else |
| 237 | return bdp - 1; |
| 238 | } |
| 239 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 240 | static void *swap_buffer(void *bufaddr, int len) |
| 241 | { |
| 242 | int i; |
| 243 | unsigned int *buf = bufaddr; |
| 244 | |
Fabio Estevam | ffed61e | 2013-05-21 05:44:26 +0000 | [diff] [blame] | 245 | for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++) |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 246 | *buf = cpu_to_be32(*buf); |
| 247 | |
| 248 | return bufaddr; |
| 249 | } |
| 250 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 251 | static int |
| 252 | fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) |
| 253 | { |
| 254 | /* Only run for packets requiring a checksum. */ |
| 255 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 256 | return 0; |
| 257 | |
| 258 | if (unlikely(skb_cow_head(skb, 0))) |
| 259 | return -1; |
| 260 | |
| 261 | *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Denis Kirjanov | c7621cb | 2010-06-02 09:15:47 +0000 | [diff] [blame] | 266 | static netdev_tx_t |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 267 | fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 269 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 270 | const struct platform_device_id *id_entry = |
| 271 | platform_get_device_id(fep->pdev); |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 272 | struct bufdesc *bdp; |
Greg Ungerer | 9555b31 | 2009-08-06 17:58:18 +0000 | [diff] [blame] | 273 | void *bufaddr; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 274 | unsigned short status; |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 275 | unsigned int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (!fep->link) { |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 278 | /* Link is down or auto-negotiation is in progress. */ |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 279 | return NETDEV_TX_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* Fill in a Tx ring entry */ |
| 283 | bdp = fep->cur_tx; |
| 284 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 285 | status = bdp->cbd_sc; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 286 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 287 | if (status & BD_ENET_TX_READY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | /* Ooops. All transmit buffers are full. Bail out. |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 289 | * This should not happen, since ndev->tbusy should be set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | */ |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 291 | netdev_err(ndev, "tx queue full!\n"); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 292 | return NETDEV_TX_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 295 | /* Protocol checksum off-load for TCP and UDP. */ |
| 296 | if (fec_enet_clear_csum(skb, ndev)) { |
| 297 | kfree_skb(skb); |
| 298 | return NETDEV_TX_OK; |
| 299 | } |
| 300 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 301 | /* Clear all of the status flags */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 302 | status &= ~BD_ENET_TX_STATS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 304 | /* Set buffer length and buffer pointer */ |
Greg Ungerer | 9555b31 | 2009-08-06 17:58:18 +0000 | [diff] [blame] | 305 | bufaddr = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | bdp->cbd_datlen = skb->len; |
| 307 | |
| 308 | /* |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 309 | * On some FEC implementations data must be aligned on |
| 310 | * 4-byte boundaries. Use bounce buffers to copy data |
| 311 | * and get it aligned. Ugh. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 313 | if (fep->bufdesc_ex) |
| 314 | index = (struct bufdesc_ex *)bdp - |
| 315 | (struct bufdesc_ex *)fep->tx_bd_base; |
| 316 | else |
| 317 | index = bdp - fep->tx_bd_base; |
| 318 | |
Greg Ungerer | 9555b31 | 2009-08-06 17:58:18 +0000 | [diff] [blame] | 319 | if (((unsigned long) bufaddr) & FEC_ALIGNMENT) { |
Uwe Kleine-König | 8a73b0b | 2011-01-13 21:34:31 +0100 | [diff] [blame] | 320 | memcpy(fep->tx_bounce[index], skb->data, skb->len); |
Greg Ungerer | 9555b31 | 2009-08-06 17:58:18 +0000 | [diff] [blame] | 321 | bufaddr = fep->tx_bounce[index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 324 | /* |
| 325 | * Some design made an incorrect assumption on endian mode of |
| 326 | * the system that it's running on. As the result, driver has to |
| 327 | * swap every frame going to and coming from the controller. |
| 328 | */ |
| 329 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 330 | swap_buffer(bufaddr, skb->len); |
| 331 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 332 | /* Save skb pointer */ |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 333 | fep->tx_skbuff[index] = skb; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* Push the data cache so the CPM does not get stale memory |
| 336 | * data. |
| 337 | */ |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 338 | bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr, |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 339 | FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 341 | /* Send it on its way. Tell FEC it's ready, interrupt when done, |
| 342 | * it's the last BD of the frame, and to put the CRC on the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 344 | status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | | BD_ENET_TX_LAST | BD_ENET_TX_TC); |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 346 | bdp->cbd_sc = status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 348 | if (fep->bufdesc_ex) { |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 349 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 350 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 351 | ebdp->cbd_bdu = 0; |
| 352 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && |
| 353 | fep->hwts_tx_en)) { |
| 354 | ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT); |
| 355 | skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; |
| 356 | } else { |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 357 | ebdp->cbd_esc = BD_ENET_TX_INT; |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 358 | |
| 359 | /* Enable protocol checksum flags |
| 360 | * We do not bother with the IP Checksum bits as they |
| 361 | * are done by the kernel |
| 362 | */ |
| 363 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 364 | ebdp->cbd_esc |= BD_ENET_TX_PINS; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 365 | } |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 366 | } |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 367 | /* If this was the last BD in the ring, start at the beginning again. */ |
| 368 | if (status & BD_ENET_TX_WRAP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | bdp = fep->tx_bd_base; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 370 | else |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 371 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 373 | fep->cur_tx = bdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 375 | if (fep->cur_tx == fep->dirty_tx) |
| 376 | netif_stop_queue(ndev); |
Richard Cochran | 18a03b9 | 2011-06-12 02:18:59 +0000 | [diff] [blame] | 377 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 378 | /* Trigger transmission start */ |
| 379 | writel(0, fep->hwp + FEC_X_DES_ACTIVE); |
| 380 | |
| 381 | skb_tx_timestamp(skb); |
Richard Cochran | a0087a3 | 2011-06-19 03:31:40 +0000 | [diff] [blame] | 382 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 383 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 386 | /* Init RX & TX buffer descriptors |
| 387 | */ |
| 388 | static void fec_enet_bd_init(struct net_device *dev) |
| 389 | { |
| 390 | struct fec_enet_private *fep = netdev_priv(dev); |
| 391 | struct bufdesc *bdp; |
| 392 | unsigned int i; |
| 393 | |
| 394 | /* Initialize the receive buffer descriptors. */ |
| 395 | bdp = fep->rx_bd_base; |
| 396 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 397 | |
| 398 | /* Initialize the BD for every fragment in the page. */ |
| 399 | if (bdp->cbd_bufaddr) |
| 400 | bdp->cbd_sc = BD_ENET_RX_EMPTY; |
| 401 | else |
| 402 | bdp->cbd_sc = 0; |
| 403 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
| 404 | } |
| 405 | |
| 406 | /* Set the last buffer to wrap */ |
| 407 | bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex); |
| 408 | bdp->cbd_sc |= BD_SC_WRAP; |
| 409 | |
| 410 | fep->cur_rx = fep->rx_bd_base; |
| 411 | |
| 412 | /* ...and the same for transmit */ |
| 413 | bdp = fep->tx_bd_base; |
| 414 | fep->cur_tx = bdp; |
| 415 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 416 | |
| 417 | /* Initialize the BD for every fragment in the page. */ |
| 418 | bdp->cbd_sc = 0; |
| 419 | if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) { |
| 420 | dev_kfree_skb_any(fep->tx_skbuff[i]); |
| 421 | fep->tx_skbuff[i] = NULL; |
| 422 | } |
| 423 | bdp->cbd_bufaddr = 0; |
| 424 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
| 425 | } |
| 426 | |
| 427 | /* Set the last buffer to wrap */ |
| 428 | bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex); |
| 429 | bdp->cbd_sc |= BD_SC_WRAP; |
| 430 | fep->dirty_tx = bdp; |
| 431 | } |
| 432 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 433 | /* This function is called to start or restart the FEC during a link |
| 434 | * change. This only happens when switching between half and full |
| 435 | * duplex. |
| 436 | */ |
| 437 | static void |
| 438 | fec_restart(struct net_device *ndev, int duplex) |
| 439 | { |
| 440 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 441 | const struct platform_device_id *id_entry = |
| 442 | platform_get_device_id(fep->pdev); |
| 443 | int i; |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 444 | u32 val; |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 445 | u32 temp_mac[2]; |
| 446 | u32 rcntl = OPT_FRAME_SIZE | 0x04; |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 447 | u32 ecntl = 0x2; /* ETHEREN */ |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 448 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 449 | if (netif_running(ndev)) { |
| 450 | netif_device_detach(ndev); |
| 451 | napi_disable(&fep->napi); |
| 452 | netif_stop_queue(ndev); |
Fabio Estevam | 3169134 | 2013-05-15 07:06:26 +0000 | [diff] [blame] | 453 | netif_tx_lock_bh(ndev); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 456 | /* Whack a reset. We should wait for this. */ |
| 457 | writel(1, fep->hwp + FEC_ECNTRL); |
| 458 | udelay(10); |
| 459 | |
| 460 | /* |
| 461 | * enet-mac reset will reset mac address registers too, |
| 462 | * so need to reconfigure it. |
| 463 | */ |
| 464 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
| 465 | memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); |
| 466 | writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW); |
| 467 | writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH); |
| 468 | } |
| 469 | |
| 470 | /* Clear any outstanding interrupt. */ |
| 471 | writel(0xffc00000, fep->hwp + FEC_IEVENT); |
| 472 | |
| 473 | /* Reset all multicast. */ |
| 474 | writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 475 | writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
| 476 | #ifndef CONFIG_M5272 |
| 477 | writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); |
| 478 | writel(0, fep->hwp + FEC_HASH_TABLE_LOW); |
| 479 | #endif |
| 480 | |
| 481 | /* Set maximum receive buffer size. */ |
| 482 | writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE); |
| 483 | |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 484 | fec_enet_bd_init(ndev); |
| 485 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 486 | /* Set receive and transmit descriptor base. */ |
| 487 | writel(fep->bd_dma, fep->hwp + FEC_R_DES_START); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 488 | if (fep->bufdesc_ex) |
| 489 | writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex) |
| 490 | * RX_RING_SIZE, fep->hwp + FEC_X_DES_START); |
| 491 | else |
| 492 | writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) |
| 493 | * RX_RING_SIZE, fep->hwp + FEC_X_DES_START); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 494 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 495 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 496 | for (i = 0; i <= TX_RING_MOD_MASK; i++) { |
| 497 | if (fep->tx_skbuff[i]) { |
| 498 | dev_kfree_skb_any(fep->tx_skbuff[i]); |
| 499 | fep->tx_skbuff[i] = NULL; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* Enable MII mode */ |
| 504 | if (duplex) { |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 505 | /* FD enable */ |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 506 | writel(0x04, fep->hwp + FEC_X_CNTRL); |
| 507 | } else { |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 508 | /* No Rcv on Xmit */ |
| 509 | rcntl |= 0x02; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 510 | writel(0x0, fep->hwp + FEC_X_CNTRL); |
| 511 | } |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 512 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 513 | fep->full_duplex = duplex; |
| 514 | |
| 515 | /* Set MII speed */ |
| 516 | writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); |
| 517 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 518 | /* set RX checksum */ |
| 519 | val = readl(fep->hwp + FEC_RACC); |
| 520 | if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) |
| 521 | val |= FEC_RACC_OPTIONS; |
| 522 | else |
| 523 | val &= ~FEC_RACC_OPTIONS; |
| 524 | writel(val, fep->hwp + FEC_RACC); |
| 525 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 526 | /* |
| 527 | * The phy interface and speed need to get configured |
| 528 | * differently on enet-mac. |
| 529 | */ |
| 530 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 531 | /* Enable flow control and length check */ |
| 532 | rcntl |= 0x40000000 | 0x00000020; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 533 | |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 534 | /* RGMII, RMII or MII */ |
| 535 | if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII) |
| 536 | rcntl |= (1 << 6); |
| 537 | else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 538 | rcntl |= (1 << 8); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 539 | else |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 540 | rcntl &= ~(1 << 8); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 541 | |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 542 | /* 1G, 100M or 10M */ |
| 543 | if (fep->phy_dev) { |
| 544 | if (fep->phy_dev->speed == SPEED_1000) |
| 545 | ecntl |= (1 << 5); |
| 546 | else if (fep->phy_dev->speed == SPEED_100) |
| 547 | rcntl &= ~(1 << 9); |
| 548 | else |
| 549 | rcntl |= (1 << 9); |
| 550 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 551 | } else { |
| 552 | #ifdef FEC_MIIGSK_ENR |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 553 | if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) { |
Eric Benard | 8d82f21 | 2012-01-12 06:10:28 +0000 | [diff] [blame] | 554 | u32 cfgr; |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 555 | /* disable the gasket and wait */ |
| 556 | writel(0, fep->hwp + FEC_MIIGSK_ENR); |
| 557 | while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) |
| 558 | udelay(1); |
| 559 | |
| 560 | /* |
| 561 | * configure the gasket: |
| 562 | * RMII, 50 MHz, no loopback, no echo |
Shawn Guo | 0ca1e29 | 2011-07-01 18:11:22 +0800 | [diff] [blame] | 563 | * MII, 25 MHz, no loopback, no echo |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 564 | */ |
Eric Benard | 8d82f21 | 2012-01-12 06:10:28 +0000 | [diff] [blame] | 565 | cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) |
| 566 | ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; |
| 567 | if (fep->phy_dev && fep->phy_dev->speed == SPEED_10) |
| 568 | cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; |
| 569 | writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 570 | |
| 571 | /* re-enable the gasket */ |
| 572 | writel(2, fep->hwp + FEC_MIIGSK_ENR); |
| 573 | } |
| 574 | #endif |
| 575 | } |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 576 | |
| 577 | /* enable pause frame*/ |
| 578 | if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || |
| 579 | ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && |
| 580 | fep->phy_dev && fep->phy_dev->pause)) { |
| 581 | rcntl |= FEC_ENET_FCE; |
| 582 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 583 | /* set FIFO threshold parameter to reduce overrun */ |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 584 | writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); |
| 585 | writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); |
| 586 | writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); |
| 587 | writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); |
| 588 | |
| 589 | /* OPD */ |
| 590 | writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); |
| 591 | } else { |
| 592 | rcntl &= ~FEC_ENET_FCE; |
| 593 | } |
| 594 | |
Uwe Kleine-König | cd1f402 | 2011-01-25 22:11:25 +0100 | [diff] [blame] | 595 | writel(rcntl, fep->hwp + FEC_R_CNTRL); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 596 | |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 597 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
| 598 | /* enable ENET endian swap */ |
| 599 | ecntl |= (1 << 8); |
| 600 | /* enable ENET store and forward mode */ |
| 601 | writel(1 << 8, fep->hwp + FEC_X_WMRK); |
| 602 | } |
| 603 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 604 | if (fep->bufdesc_ex) |
| 605 | ecntl |= (1 << 4); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 606 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 607 | /* And last, enable the transmit and receive processing */ |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 608 | writel(ecntl, fep->hwp + FEC_ECNTRL); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 609 | writel(0, fep->hwp + FEC_R_DES_ACTIVE); |
| 610 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 611 | if (fep->bufdesc_ex) |
| 612 | fec_ptp_start_cyclecounter(ndev); |
| 613 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 614 | /* Enable interrupts we wish to service */ |
| 615 | writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 616 | |
| 617 | if (netif_running(ndev)) { |
Fabio Estevam | 3169134 | 2013-05-15 07:06:26 +0000 | [diff] [blame] | 618 | netif_tx_unlock_bh(ndev); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 619 | netif_wake_queue(ndev); |
Fabio Estevam | 1ed0d56 | 2013-05-15 07:06:27 +0000 | [diff] [blame] | 620 | napi_enable(&fep->napi); |
| 621 | netif_device_attach(ndev); |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 622 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static void |
| 626 | fec_stop(struct net_device *ndev) |
| 627 | { |
| 628 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 629 | const struct platform_device_id *id_entry = |
| 630 | platform_get_device_id(fep->pdev); |
Lothar Waßmann | 42431dc | 2011-12-07 21:59:30 +0000 | [diff] [blame] | 631 | 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] | 632 | |
| 633 | /* We cannot expect a graceful transmit stop without link !!! */ |
| 634 | if (fep->link) { |
| 635 | writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ |
| 636 | udelay(10); |
| 637 | if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 638 | netdev_err(ndev, "Graceful transmit stop did not complete!\n"); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* Whack a reset. We should wait for this. */ |
| 642 | writel(1, fep->hwp + FEC_ECNTRL); |
| 643 | udelay(10); |
| 644 | writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); |
| 645 | writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 646 | |
| 647 | /* 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] | 648 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 649 | writel(2, fep->hwp + FEC_ECNTRL); |
Lothar Waßmann | 42431dc | 2011-12-07 21:59:30 +0000 | [diff] [blame] | 650 | writel(rmii_mode, fep->hwp + FEC_R_CNTRL); |
| 651 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | static void |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 656 | fec_timeout(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 658 | struct fec_enet_private *fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 660 | ndev->stats.tx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 662 | fep->delay_work.timeout = true; |
| 663 | schedule_delayed_work(&(fep->delay_work.delay_work), 0); |
| 664 | } |
| 665 | |
| 666 | static void fec_enet_work(struct work_struct *work) |
| 667 | { |
| 668 | struct fec_enet_private *fep = |
| 669 | container_of(work, |
| 670 | struct fec_enet_private, |
| 671 | delay_work.delay_work.work); |
| 672 | |
| 673 | if (fep->delay_work.timeout) { |
| 674 | fep->delay_work.timeout = false; |
| 675 | fec_restart(fep->netdev, fep->full_duplex); |
| 676 | netif_wake_queue(fep->netdev); |
| 677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | static void |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 681 | fec_enet_tx(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
| 683 | struct fec_enet_private *fep; |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 684 | struct bufdesc *bdp; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 685 | unsigned short status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | struct sk_buff *skb; |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 687 | int index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 689 | fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | bdp = fep->dirty_tx; |
| 691 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 692 | /* get next bdp of dirty_tx */ |
| 693 | if (bdp->cbd_sc & BD_ENET_TX_WRAP) |
| 694 | bdp = fep->tx_bd_base; |
| 695 | else |
| 696 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
| 697 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 698 | while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) { |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 699 | |
| 700 | /* current queue is empty */ |
| 701 | if (bdp == fep->cur_tx) |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 702 | break; |
| 703 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 704 | if (fep->bufdesc_ex) |
| 705 | index = (struct bufdesc_ex *)bdp - |
| 706 | (struct bufdesc_ex *)fep->tx_bd_base; |
| 707 | else |
| 708 | index = bdp - fep->tx_bd_base; |
| 709 | |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 710 | dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, |
| 711 | FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 712 | bdp->cbd_bufaddr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 714 | skb = fep->tx_skbuff[index]; |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | /* Check for errors. */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 717 | if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | BD_ENET_TX_RL | BD_ENET_TX_UN | |
| 719 | BD_ENET_TX_CSL)) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 720 | ndev->stats.tx_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 721 | if (status & BD_ENET_TX_HB) /* No heartbeat */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 722 | ndev->stats.tx_heartbeat_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 723 | if (status & BD_ENET_TX_LC) /* Late collision */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 724 | ndev->stats.tx_window_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 725 | if (status & BD_ENET_TX_RL) /* Retrans limit */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 726 | ndev->stats.tx_aborted_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 727 | if (status & BD_ENET_TX_UN) /* Underrun */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 728 | ndev->stats.tx_fifo_errors++; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 729 | if (status & BD_ENET_TX_CSL) /* Carrier lost */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 730 | ndev->stats.tx_carrier_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } else { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 732 | ndev->stats.tx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 735 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) && |
| 736 | fep->bufdesc_ex) { |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 737 | struct skb_shared_hwtstamps shhwtstamps; |
| 738 | unsigned long flags; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 739 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 740 | |
| 741 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 742 | spin_lock_irqsave(&fep->tmreg_lock, flags); |
| 743 | shhwtstamps.hwtstamp = ns_to_ktime( |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 744 | timecounter_cyc2time(&fep->tc, ebdp->ts)); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 745 | spin_unlock_irqrestore(&fep->tmreg_lock, flags); |
| 746 | skb_tstamp_tx(skb, &shhwtstamps); |
| 747 | } |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 748 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 749 | if (status & BD_ENET_TX_READY) |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 750 | netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n"); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | /* Deferred means some collisions occurred during transmit, |
| 753 | * but we eventually sent the packet OK. |
| 754 | */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 755 | if (status & BD_ENET_TX_DEF) |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 756 | ndev->stats.collisions++; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 757 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 758 | /* Free the sk buffer associated with this last transmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | dev_kfree_skb_any(skb); |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 760 | fep->tx_skbuff[index] = NULL; |
| 761 | |
| 762 | fep->dirty_tx = bdp; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 763 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 764 | /* Update pointer to next buffer descriptor to be transmitted */ |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 765 | if (status & BD_ENET_TX_WRAP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | bdp = fep->tx_bd_base; |
| 767 | else |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 768 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 769 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 770 | /* 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] | 771 | */ |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 772 | if (fep->dirty_tx != fep->cur_tx) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 773 | if (netif_queue_stopped(ndev)) |
| 774 | netif_wake_queue(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 777 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
| 781 | /* During a receive, the cur_rx points to the current incoming buffer. |
| 782 | * When we update through the ring, if the next incoming buffer has |
| 783 | * not been given to the system, we just set the empty indicator, |
| 784 | * effectively tossing the packet. |
| 785 | */ |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 786 | static int |
| 787 | fec_enet_rx(struct net_device *ndev, int budget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 789 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 790 | const struct platform_device_id *id_entry = |
| 791 | platform_get_device_id(fep->pdev); |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 792 | struct bufdesc *bdp; |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 793 | unsigned short status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | struct sk_buff *skb; |
| 795 | ushort pkt_len; |
| 796 | __u8 *data; |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 797 | int pkt_received = 0; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 798 | |
Greg Ungerer | 0e702ab | 2006-06-27 13:19:33 +1000 | [diff] [blame] | 799 | #ifdef CONFIG_M532x |
| 800 | flush_cache_all(); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 801 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | /* First, grab all of the stats for the incoming packet. |
| 804 | * These get messed up if we get called due to a busy condition. |
| 805 | */ |
| 806 | bdp = fep->cur_rx; |
| 807 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 808 | while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 810 | if (pkt_received >= budget) |
| 811 | break; |
| 812 | pkt_received++; |
| 813 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 814 | /* Since we have allocated space to hold a complete frame, |
| 815 | * the last indicator should be set. |
| 816 | */ |
| 817 | if ((status & BD_ENET_RX_LAST) == 0) |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 818 | netdev_err(ndev, "rcv is not +last\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 820 | if (!fep->opened) |
| 821 | goto rx_processing_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 823 | /* Check for errors. */ |
| 824 | 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] | 825 | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 826 | ndev->stats.rx_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 827 | if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) { |
| 828 | /* Frame too long or too short. */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 829 | ndev->stats.rx_length_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 830 | } |
| 831 | if (status & BD_ENET_RX_NO) /* Frame alignment */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 832 | ndev->stats.rx_frame_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 833 | if (status & BD_ENET_RX_CR) /* CRC Error */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 834 | ndev->stats.rx_crc_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 835 | if (status & BD_ENET_RX_OV) /* FIFO overrun */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 836 | ndev->stats.rx_fifo_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 838 | |
| 839 | /* Report late collisions as a frame error. |
| 840 | * On this error, the BD is closed, but we don't know what we |
| 841 | * have in the buffer. So, just drop this frame on the floor. |
| 842 | */ |
| 843 | if (status & BD_ENET_RX_CL) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 844 | ndev->stats.rx_errors++; |
| 845 | ndev->stats.rx_frame_errors++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 846 | goto rx_processing_done; |
| 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 849 | /* Process the incoming frame. */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 850 | ndev->stats.rx_packets++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 851 | pkt_len = bdp->cbd_datlen; |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 852 | ndev->stats.rx_bytes += pkt_len; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 853 | data = (__u8*)__va(bdp->cbd_bufaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 855 | dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, |
| 856 | FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE); |
Sascha Hauer | ccdc4f1 | 2009-01-28 23:03:09 +0000 | [diff] [blame] | 857 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 858 | if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) |
| 859 | swap_buffer(data, pkt_len); |
| 860 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 861 | /* This does 16 byte alignment, exactly what we need. |
| 862 | * The packet length includes FCS, but we don't want to |
| 863 | * include that when passing upstream as it messes up |
| 864 | * bridging applications. |
| 865 | */ |
Fabio Estevam | b72061a | 2012-02-07 08:27:31 +0000 | [diff] [blame] | 866 | skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Sascha Hauer | 8549889 | 2009-04-15 01:32:21 +0000 | [diff] [blame] | 868 | if (unlikely(!skb)) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 869 | ndev->stats.rx_dropped++; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 870 | } else { |
Sascha Hauer | 8549889 | 2009-04-15 01:32:21 +0000 | [diff] [blame] | 871 | skb_reserve(skb, NET_IP_ALIGN); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 872 | skb_put(skb, pkt_len - 4); /* Make room */ |
| 873 | skb_copy_to_linear_data(skb, data, pkt_len - 4); |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 874 | skb->protocol = eth_type_trans(skb, ndev); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 875 | |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 876 | /* Get receive timestamp from the skb */ |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 877 | if (fep->hwts_rx_en && fep->bufdesc_ex) { |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 878 | struct skb_shared_hwtstamps *shhwtstamps = |
| 879 | skb_hwtstamps(skb); |
| 880 | unsigned long flags; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 881 | struct bufdesc_ex *ebdp = |
| 882 | (struct bufdesc_ex *)bdp; |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 883 | |
| 884 | memset(shhwtstamps, 0, sizeof(*shhwtstamps)); |
| 885 | |
| 886 | spin_lock_irqsave(&fep->tmreg_lock, flags); |
| 887 | shhwtstamps->hwtstamp = ns_to_ktime( |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 888 | timecounter_cyc2time(&fep->tc, ebdp->ts)); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 889 | spin_unlock_irqrestore(&fep->tmreg_lock, flags); |
| 890 | } |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 891 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 892 | if (fep->bufdesc_ex && |
| 893 | (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { |
| 894 | struct bufdesc_ex *ebdp = |
| 895 | (struct bufdesc_ex *)bdp; |
| 896 | if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) { |
| 897 | /* don't check it */ |
| 898 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 899 | } else { |
| 900 | skb_checksum_none_assert(skb); |
| 901 | } |
| 902 | } |
| 903 | |
Richard Cochran | 18a03b9 | 2011-06-12 02:18:59 +0000 | [diff] [blame] | 904 | if (!skb_defer_rx_timestamp(skb)) |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 905 | napi_gro_receive(&fep->napi, skb); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 906 | } |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 907 | |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 908 | bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data, |
| 909 | FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 910 | rx_processing_done: |
| 911 | /* Clear the status flags for this buffer */ |
| 912 | status &= ~BD_ENET_RX_STATS; |
| 913 | |
| 914 | /* Mark the buffer empty */ |
| 915 | status |= BD_ENET_RX_EMPTY; |
| 916 | bdp->cbd_sc = status; |
| 917 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 918 | if (fep->bufdesc_ex) { |
| 919 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 920 | |
| 921 | ebdp->cbd_esc = BD_ENET_RX_INT; |
| 922 | ebdp->cbd_prot = 0; |
| 923 | ebdp->cbd_bdu = 0; |
| 924 | } |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 925 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 926 | /* Update BD pointer to next entry */ |
| 927 | if (status & BD_ENET_RX_WRAP) |
| 928 | bdp = fep->rx_bd_base; |
| 929 | else |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 930 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 931 | /* Doing this here will keep the FEC running while we process |
| 932 | * incoming frames. On a heavily loaded network, we should be |
| 933 | * able to keep up at the expense of system resources. |
| 934 | */ |
| 935 | writel(0, fep->hwp + FEC_R_DES_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } |
Sascha Hauer | 2e28532 | 2009-04-15 01:32:16 +0000 | [diff] [blame] | 937 | fep->cur_rx = bdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 939 | return pkt_received; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 942 | static irqreturn_t |
| 943 | fec_enet_interrupt(int irq, void *dev_id) |
| 944 | { |
| 945 | struct net_device *ndev = dev_id; |
| 946 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 947 | uint int_events; |
| 948 | irqreturn_t ret = IRQ_NONE; |
| 949 | |
| 950 | do { |
| 951 | int_events = readl(fep->hwp + FEC_IEVENT); |
| 952 | writel(int_events, fep->hwp + FEC_IEVENT); |
| 953 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 954 | if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) { |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 955 | ret = IRQ_HANDLED; |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 956 | |
| 957 | /* Disable the RX interrupt */ |
| 958 | if (napi_schedule_prep(&fep->napi)) { |
| 959 | writel(FEC_RX_DISABLED_IMASK, |
| 960 | fep->hwp + FEC_IMASK); |
| 961 | __napi_schedule(&fep->napi); |
| 962 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 965 | if (int_events & FEC_ENET_MII) { |
| 966 | ret = IRQ_HANDLED; |
| 967 | complete(&fep->mdio_done); |
| 968 | } |
| 969 | } while (int_events); |
| 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 974 | static int fec_enet_rx_napi(struct napi_struct *napi, int budget) |
| 975 | { |
| 976 | struct net_device *ndev = napi->dev; |
| 977 | int pkts = fec_enet_rx(ndev, budget); |
| 978 | struct fec_enet_private *fep = netdev_priv(ndev); |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 979 | |
Frank Li | de5fb0a | 2013-03-03 17:34:25 +0000 | [diff] [blame] | 980 | fec_enet_tx(ndev); |
| 981 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 982 | if (pkts < budget) { |
| 983 | napi_complete(napi); |
| 984 | writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); |
| 985 | } |
| 986 | return pkts; |
| 987 | } |
Uwe Kleine-König | 4599365 | 2011-01-19 20:47:04 +0100 | [diff] [blame] | 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | /* ------------------------------------------------------------------------- */ |
Fabio Estevam | 0c7768a | 2013-01-07 17:42:56 +0000 | [diff] [blame] | 990 | static void fec_get_mac(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 992 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 993 | struct fec_platform_data *pdata = fep->pdev->dev.platform_data; |
Greg Ungerer | 7dd6a2a | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 994 | unsigned char *iap, tmpaddr[ETH_ALEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 996 | /* |
| 997 | * try to get mac address in following order: |
| 998 | * |
| 999 | * 1) module parameter via kernel command line in form |
| 1000 | * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 |
| 1001 | */ |
| 1002 | iap = macaddr; |
| 1003 | |
| 1004 | /* |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1005 | * 2) from device tree data |
| 1006 | */ |
| 1007 | if (!is_valid_ether_addr(iap)) { |
| 1008 | struct device_node *np = fep->pdev->dev.of_node; |
| 1009 | if (np) { |
| 1010 | const char *mac = of_get_mac_address(np); |
| 1011 | if (mac) |
| 1012 | iap = (unsigned char *) mac; |
| 1013 | } |
| 1014 | } |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1015 | |
| 1016 | /* |
| 1017 | * 3) from flash or fuse (via platform data) |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1018 | */ |
| 1019 | if (!is_valid_ether_addr(iap)) { |
| 1020 | #ifdef CONFIG_M5272 |
| 1021 | if (FEC_FLASHMAC) |
| 1022 | iap = (unsigned char *)FEC_FLASHMAC; |
| 1023 | #else |
| 1024 | if (pdata) |
Lothar Waßmann | 589efdc | 2011-12-07 21:59:29 +0000 | [diff] [blame] | 1025 | iap = (unsigned char *)&pdata->mac; |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1026 | #endif |
| 1027 | } |
| 1028 | |
| 1029 | /* |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1030 | * 4) FEC mac registers set by bootloader |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1031 | */ |
| 1032 | if (!is_valid_ether_addr(iap)) { |
| 1033 | *((unsigned long *) &tmpaddr[0]) = |
| 1034 | be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW)); |
| 1035 | *((unsigned short *) &tmpaddr[4]) = |
| 1036 | be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | iap = &tmpaddr[0]; |
| 1038 | } |
| 1039 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1040 | memcpy(ndev->dev_addr, iap, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1042 | /* Adjust MAC if using macaddr */ |
| 1043 | if (iap == macaddr) |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1044 | 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] | 1045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
| 1047 | /* ------------------------------------------------------------------------- */ |
| 1048 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1049 | /* |
| 1050 | * Phy section |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1052 | static void fec_enet_adjust_link(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); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1055 | struct phy_device *phy_dev = fep->phy_dev; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1056 | int status_change = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1058 | /* Prevent a state halted on mii error */ |
| 1059 | if (fep->mii_timeout && phy_dev->state == PHY_HALTED) { |
| 1060 | phy_dev->state = PHY_RESUMING; |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1061 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1063 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1064 | if (phy_dev->link) { |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1065 | if (!fep->link) { |
Lothar Waßmann | 6ea0722 | 2011-12-07 21:59:27 +0000 | [diff] [blame] | 1066 | fep->link = phy_dev->link; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1067 | status_change = 1; |
| 1068 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1069 | |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1070 | if (fep->full_duplex != phy_dev->duplex) |
| 1071 | status_change = 1; |
| 1072 | |
| 1073 | if (phy_dev->speed != fep->speed) { |
| 1074 | fep->speed = phy_dev->speed; |
| 1075 | status_change = 1; |
| 1076 | } |
| 1077 | |
| 1078 | /* if any of the above changed restart the FEC */ |
| 1079 | if (status_change) |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1080 | fec_restart(ndev, phy_dev->duplex); |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1081 | } else { |
| 1082 | if (fep->link) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1083 | fec_stop(ndev); |
Lucas Stach | 8d7ed0f | 2013-04-16 00:42:42 +0000 | [diff] [blame] | 1084 | fep->link = phy_dev->link; |
Lucas Stach | d97e749 | 2013-03-14 05:12:01 +0000 | [diff] [blame] | 1085 | status_change = 1; |
| 1086 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1089 | if (status_change) |
| 1090 | phy_print_status(phy_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1093 | 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] | 1094 | { |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1095 | struct fec_enet_private *fep = bus->priv; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1096 | unsigned long time_left; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1097 | |
| 1098 | fep->mii_timeout = 0; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1099 | init_completion(&fep->mdio_done); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1100 | |
| 1101 | /* start a read op */ |
| 1102 | writel(FEC_MMFR_ST | FEC_MMFR_OP_READ | |
| 1103 | FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | |
| 1104 | FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); |
| 1105 | |
| 1106 | /* wait for end of transfer */ |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1107 | time_left = wait_for_completion_timeout(&fep->mdio_done, |
| 1108 | usecs_to_jiffies(FEC_MII_TIMEOUT)); |
| 1109 | if (time_left == 0) { |
| 1110 | fep->mii_timeout = 1; |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1111 | netdev_err(fep->netdev, "MDIO read timeout\n"); |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1112 | return -ETIMEDOUT; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /* return value */ |
| 1116 | return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); |
| 1117 | } |
| 1118 | |
| 1119 | static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 1120 | u16 value) |
| 1121 | { |
| 1122 | struct fec_enet_private *fep = bus->priv; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1123 | unsigned long time_left; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1124 | |
| 1125 | fep->mii_timeout = 0; |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1126 | init_completion(&fep->mdio_done); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1127 | |
Shawn Guo | 862f098 | 2011-01-05 21:13:09 +0000 | [diff] [blame] | 1128 | /* start a write op */ |
| 1129 | writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1130 | FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | |
| 1131 | FEC_MMFR_TA | FEC_MMFR_DATA(value), |
| 1132 | fep->hwp + FEC_MII_DATA); |
| 1133 | |
| 1134 | /* wait for end of transfer */ |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1135 | time_left = wait_for_completion_timeout(&fep->mdio_done, |
| 1136 | usecs_to_jiffies(FEC_MII_TIMEOUT)); |
| 1137 | if (time_left == 0) { |
| 1138 | fep->mii_timeout = 1; |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1139 | netdev_err(fep->netdev, "MDIO write timeout\n"); |
Baruch Siach | 97b72e4 | 2010-07-11 21:12:51 +0000 | [diff] [blame] | 1140 | return -ETIMEDOUT; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | static int fec_enet_mdio_reset(struct mii_bus *bus) |
| 1147 | { |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1151 | static int fec_enet_mii_probe(struct net_device *ndev) |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1152 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1153 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1154 | const struct platform_device_id *id_entry = |
| 1155 | platform_get_device_id(fep->pdev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1156 | struct phy_device *phy_dev = NULL; |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1157 | char mdio_bus_id[MII_BUS_ID_SIZE]; |
| 1158 | char phy_name[MII_BUS_ID_SIZE + 3]; |
| 1159 | int phy_id; |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1160 | int dev_id = fep->dev_id; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1161 | |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1162 | fep->phy_dev = NULL; |
| 1163 | |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1164 | /* check for attached phy */ |
| 1165 | for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { |
| 1166 | if ((fep->mii_bus->phy_mask & (1 << phy_id))) |
| 1167 | continue; |
| 1168 | if (fep->mii_bus->phy_map[phy_id] == NULL) |
| 1169 | continue; |
| 1170 | if (fep->mii_bus->phy_map[phy_id]->phy_id == 0) |
| 1171 | continue; |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1172 | if (dev_id--) |
| 1173 | continue; |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1174 | strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); |
| 1175 | break; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1178 | if (phy_id >= PHY_MAX_ADDR) { |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1179 | netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); |
Florian Fainelli | ea51ade9 | 2012-02-13 01:23:22 +0000 | [diff] [blame] | 1180 | strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1181 | phy_id = 0; |
| 1182 | } |
| 1183 | |
Richard Zhao | a7ed07d | 2012-01-29 22:08:12 +0000 | [diff] [blame] | 1184 | 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] | 1185 | phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1186 | fep->phy_interface); |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1187 | if (IS_ERR(phy_dev)) { |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1188 | netdev_err(ndev, "could not attach to PHY\n"); |
Greg Ungerer | 6fcc040 | 2010-10-11 21:03:05 +0000 | [diff] [blame] | 1189 | return PTR_ERR(phy_dev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* mask with MAC supported features */ |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1193 | if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) { |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1194 | phy_dev->supported &= PHY_GBIT_FEATURES; |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1195 | phy_dev->supported |= SUPPORTED_Pause; |
| 1196 | } |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1197 | else |
| 1198 | phy_dev->supported &= PHY_BASIC_FEATURES; |
| 1199 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1200 | phy_dev->advertising = phy_dev->supported; |
| 1201 | |
| 1202 | fep->phy_dev = phy_dev; |
| 1203 | fep->link = 0; |
| 1204 | fep->full_duplex = 0; |
| 1205 | |
Joe Perches | 31b7720 | 2013-04-13 19:03:17 +0000 | [diff] [blame] | 1206 | netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n", |
| 1207 | fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev), |
| 1208 | fep->phy_dev->irq); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1209 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static int fec_enet_mii_init(struct platform_device *pdev) |
| 1214 | { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1215 | static struct mii_bus *fec0_mii_bus; |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1216 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 1217 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1218 | const struct platform_device_id *id_entry = |
| 1219 | platform_get_device_id(fep->pdev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1220 | int err = -ENXIO, i; |
| 1221 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1222 | /* |
| 1223 | * The dual fec interfaces are not equivalent with enet-mac. |
| 1224 | * Here are the differences: |
| 1225 | * |
| 1226 | * - fec0 supports MII & RMII modes while fec1 only supports RMII |
| 1227 | * - fec0 acts as the 1588 time master while fec1 is slave |
| 1228 | * - external phys can only be configured by fec0 |
| 1229 | * |
| 1230 | * That is to say fec1 can not work independently. It only works |
| 1231 | * when fec0 is working. The reason behind this design is that the |
| 1232 | * second interface is added primarily for Switch mode. |
| 1233 | * |
| 1234 | * Because of the last point above, both phys are attached on fec0 |
| 1235 | * mdio interface in board design, and need to be configured by |
| 1236 | * fec0 mii_bus. |
| 1237 | */ |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1238 | 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] | 1239 | /* fec1 uses fec0 mii_bus */ |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 1240 | if (mii_cnt && fec0_mii_bus) { |
| 1241 | fep->mii_bus = fec0_mii_bus; |
| 1242 | mii_cnt++; |
| 1243 | return 0; |
| 1244 | } |
| 1245 | return -ENOENT; |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1248 | fep->mii_timeout = 0; |
| 1249 | |
| 1250 | /* |
| 1251 | * 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] | 1252 | * |
| 1253 | * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while |
| 1254 | * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 |
| 1255 | * Reference Manual has an error on this, and gets fixed on i.MX6Q |
| 1256 | * document. |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1257 | */ |
Sascha Hauer | f4d40de | 2012-03-07 09:30:49 +0100 | [diff] [blame] | 1258 | fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000); |
Shawn Guo | 230dec6 | 2011-09-23 02:12:48 +0000 | [diff] [blame] | 1259 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) |
| 1260 | fep->phy_speed--; |
| 1261 | fep->phy_speed <<= 1; |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1262 | writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); |
| 1263 | |
| 1264 | fep->mii_bus = mdiobus_alloc(); |
| 1265 | if (fep->mii_bus == NULL) { |
| 1266 | err = -ENOMEM; |
| 1267 | goto err_out; |
| 1268 | } |
| 1269 | |
| 1270 | fep->mii_bus->name = "fec_enet_mii_bus"; |
| 1271 | fep->mii_bus->read = fec_enet_mdio_read; |
| 1272 | fep->mii_bus->write = fec_enet_mdio_write; |
| 1273 | fep->mii_bus->reset = fec_enet_mdio_reset; |
Florian Fainelli | 391420f | 2012-01-09 23:59:13 +0000 | [diff] [blame] | 1274 | snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
| 1275 | pdev->name, fep->dev_id + 1); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1276 | fep->mii_bus->priv = fep; |
| 1277 | fep->mii_bus->parent = &pdev->dev; |
| 1278 | |
| 1279 | fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); |
| 1280 | if (!fep->mii_bus->irq) { |
| 1281 | err = -ENOMEM; |
| 1282 | goto err_out_free_mdiobus; |
| 1283 | } |
| 1284 | |
| 1285 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 1286 | fep->mii_bus->irq[i] = PHY_POLL; |
| 1287 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1288 | if (mdiobus_register(fep->mii_bus)) |
| 1289 | goto err_out_free_mdio_irq; |
| 1290 | |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 1291 | mii_cnt++; |
| 1292 | |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 1293 | /* save fec0 mii_bus */ |
| 1294 | if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) |
| 1295 | fec0_mii_bus = fep->mii_bus; |
| 1296 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1297 | return 0; |
| 1298 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1299 | err_out_free_mdio_irq: |
| 1300 | kfree(fep->mii_bus->irq); |
| 1301 | err_out_free_mdiobus: |
| 1302 | mdiobus_free(fep->mii_bus); |
| 1303 | err_out: |
| 1304 | return err; |
| 1305 | } |
| 1306 | |
| 1307 | static void fec_enet_mii_remove(struct fec_enet_private *fep) |
| 1308 | { |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 1309 | if (--mii_cnt == 0) { |
| 1310 | mdiobus_unregister(fep->mii_bus); |
| 1311 | kfree(fep->mii_bus->irq); |
| 1312 | mdiobus_free(fep->mii_bus); |
| 1313 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1316 | static int fec_enet_get_settings(struct net_device *ndev, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1317 | struct ethtool_cmd *cmd) |
| 1318 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1319 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1320 | struct phy_device *phydev = fep->phy_dev; |
| 1321 | |
| 1322 | if (!phydev) |
| 1323 | return -ENODEV; |
| 1324 | |
| 1325 | return phy_ethtool_gset(phydev, cmd); |
| 1326 | } |
| 1327 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1328 | static int fec_enet_set_settings(struct net_device *ndev, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1329 | struct ethtool_cmd *cmd) |
| 1330 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1331 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1332 | struct phy_device *phydev = fep->phy_dev; |
| 1333 | |
| 1334 | if (!phydev) |
| 1335 | return -ENODEV; |
| 1336 | |
| 1337 | return phy_ethtool_sset(phydev, cmd); |
| 1338 | } |
| 1339 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1340 | static void fec_enet_get_drvinfo(struct net_device *ndev, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1341 | struct ethtool_drvinfo *info) |
| 1342 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1343 | struct fec_enet_private *fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 1345 | strlcpy(info->driver, fep->pdev->dev.driver->name, |
| 1346 | sizeof(info->driver)); |
| 1347 | strlcpy(info->version, "Revision: 1.0", sizeof(info->version)); |
| 1348 | strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1350 | |
Frank Li | 5ebae489 | 2013-01-06 16:25:07 +0000 | [diff] [blame] | 1351 | static int fec_enet_get_ts_info(struct net_device *ndev, |
| 1352 | struct ethtool_ts_info *info) |
| 1353 | { |
| 1354 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1355 | |
| 1356 | if (fep->bufdesc_ex) { |
| 1357 | |
| 1358 | info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1359 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1360 | SOF_TIMESTAMPING_SOFTWARE | |
| 1361 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 1362 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1363 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 1364 | if (fep->ptp_clock) |
| 1365 | info->phc_index = ptp_clock_index(fep->ptp_clock); |
| 1366 | else |
| 1367 | info->phc_index = -1; |
| 1368 | |
| 1369 | info->tx_types = (1 << HWTSTAMP_TX_OFF) | |
| 1370 | (1 << HWTSTAMP_TX_ON); |
| 1371 | |
| 1372 | info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | |
| 1373 | (1 << HWTSTAMP_FILTER_ALL); |
| 1374 | return 0; |
| 1375 | } else { |
| 1376 | return ethtool_op_get_ts_info(ndev, info); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1380 | static void fec_enet_get_pauseparam(struct net_device *ndev, |
| 1381 | struct ethtool_pauseparam *pause) |
| 1382 | { |
| 1383 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1384 | |
| 1385 | pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; |
| 1386 | pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; |
| 1387 | pause->rx_pause = pause->tx_pause; |
| 1388 | } |
| 1389 | |
| 1390 | static int fec_enet_set_pauseparam(struct net_device *ndev, |
| 1391 | struct ethtool_pauseparam *pause) |
| 1392 | { |
| 1393 | struct fec_enet_private *fep = netdev_priv(ndev); |
| 1394 | |
| 1395 | if (pause->tx_pause != pause->rx_pause) { |
| 1396 | netdev_info(ndev, |
| 1397 | "hardware only support enable/disable both tx and rx"); |
| 1398 | return -EINVAL; |
| 1399 | } |
| 1400 | |
| 1401 | fep->pause_flag = 0; |
| 1402 | |
| 1403 | /* tx pause must be same as rx pause */ |
| 1404 | fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; |
| 1405 | fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; |
| 1406 | |
| 1407 | if (pause->rx_pause || pause->autoneg) { |
| 1408 | fep->phy_dev->supported |= ADVERTISED_Pause; |
| 1409 | fep->phy_dev->advertising |= ADVERTISED_Pause; |
| 1410 | } else { |
| 1411 | fep->phy_dev->supported &= ~ADVERTISED_Pause; |
| 1412 | fep->phy_dev->advertising &= ~ADVERTISED_Pause; |
| 1413 | } |
| 1414 | |
| 1415 | if (pause->autoneg) { |
| 1416 | if (netif_running(ndev)) |
| 1417 | fec_stop(ndev); |
| 1418 | phy_start_aneg(fep->phy_dev); |
| 1419 | } |
| 1420 | if (netif_running(ndev)) |
| 1421 | fec_restart(ndev, 0); |
| 1422 | |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1426 | static const struct ethtool_ops fec_enet_ethtool_ops = { |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1427 | .get_pauseparam = fec_enet_get_pauseparam, |
| 1428 | .set_pauseparam = fec_enet_set_pauseparam, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1429 | .get_settings = fec_enet_get_settings, |
| 1430 | .set_settings = fec_enet_set_settings, |
| 1431 | .get_drvinfo = fec_enet_get_drvinfo, |
| 1432 | .get_link = ethtool_op_get_link, |
Frank Li | 5ebae489 | 2013-01-06 16:25:07 +0000 | [diff] [blame] | 1433 | .get_ts_info = fec_enet_get_ts_info, |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1434 | }; |
| 1435 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1436 | 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] | 1437 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1438 | struct fec_enet_private *fep = netdev_priv(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1439 | struct phy_device *phydev = fep->phy_dev; |
| 1440 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1441 | if (!netif_running(ndev)) |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1442 | return -EINVAL; |
| 1443 | |
| 1444 | if (!phydev) |
| 1445 | return -ENODEV; |
| 1446 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1447 | if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex) |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1448 | return fec_ptp_ioctl(ndev, rq, cmd); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1449 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 1450 | return phy_mii_ioctl(phydev, rq, cmd); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1453 | static void fec_enet_free_buffers(struct net_device *ndev) |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1454 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1455 | struct fec_enet_private *fep = netdev_priv(ndev); |
Fabio Estevam | da2191e | 2013-03-20 12:31:07 -0300 | [diff] [blame] | 1456 | unsigned int i; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1457 | struct sk_buff *skb; |
| 1458 | struct bufdesc *bdp; |
| 1459 | |
| 1460 | bdp = fep->rx_bd_base; |
| 1461 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1462 | skb = fep->rx_skbuff[i]; |
| 1463 | |
| 1464 | if (bdp->cbd_bufaddr) |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 1465 | dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1466 | FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); |
| 1467 | if (skb) |
| 1468 | dev_kfree_skb(skb); |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1469 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | bdp = fep->tx_bd_base; |
| 1473 | for (i = 0; i < TX_RING_SIZE; i++) |
| 1474 | kfree(fep->tx_bounce[i]); |
| 1475 | } |
| 1476 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1477 | static int fec_enet_alloc_buffers(struct net_device *ndev) |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1478 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1479 | struct fec_enet_private *fep = netdev_priv(ndev); |
Fabio Estevam | da2191e | 2013-03-20 12:31:07 -0300 | [diff] [blame] | 1480 | unsigned int i; |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1481 | struct sk_buff *skb; |
| 1482 | struct bufdesc *bdp; |
| 1483 | |
| 1484 | bdp = fep->rx_bd_base; |
| 1485 | for (i = 0; i < RX_RING_SIZE; i++) { |
Fabio Estevam | b72061a | 2012-02-07 08:27:31 +0000 | [diff] [blame] | 1486 | skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1487 | if (!skb) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1488 | fec_enet_free_buffers(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1489 | return -ENOMEM; |
| 1490 | } |
| 1491 | fep->rx_skbuff[i] = skb; |
| 1492 | |
Uwe Kleine-König | d1ab1f5 | 2011-01-20 09:26:38 +0100 | [diff] [blame] | 1493 | bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data, |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1494 | FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE); |
| 1495 | bdp->cbd_sc = BD_ENET_RX_EMPTY; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (fep->bufdesc_ex) { |
| 1498 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
| 1499 | ebdp->cbd_esc = BD_ENET_RX_INT; |
| 1500 | } |
| 1501 | |
| 1502 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /* Set the last buffer to wrap. */ |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1506 | bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1507 | bdp->cbd_sc |= BD_SC_WRAP; |
| 1508 | |
| 1509 | bdp = fep->tx_bd_base; |
| 1510 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 1511 | fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); |
| 1512 | |
| 1513 | bdp->cbd_sc = 0; |
| 1514 | bdp->cbd_bufaddr = 0; |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 1515 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1516 | if (fep->bufdesc_ex) { |
| 1517 | struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; |
Jim Baxter | 96d2222 | 2013-03-26 05:25:07 +0000 | [diff] [blame] | 1518 | ebdp->cbd_esc = BD_ENET_TX_INT; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* Set the last buffer to wrap. */ |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1525 | bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1526 | bdp->cbd_sc |= BD_SC_WRAP; |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | static int |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1532 | fec_enet_open(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1534 | struct fec_enet_private *fep = netdev_priv(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1535 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1537 | napi_enable(&fep->napi); |
| 1538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | /* I should reset the ring buffers here, but I don't yet know |
| 1540 | * a simple way to do that. |
| 1541 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1543 | ret = fec_enet_alloc_buffers(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1544 | if (ret) |
| 1545 | return ret; |
| 1546 | |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1547 | /* Probe and connect to PHY when open the interface */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1548 | ret = fec_enet_mii_probe(ndev); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1549 | if (ret) { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1550 | fec_enet_free_buffers(ndev); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1551 | return ret; |
| 1552 | } |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1553 | phy_start(fep->phy_dev); |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1554 | netif_start_queue(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | fep->opened = 1; |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1556 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | static int |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1560 | fec_enet_close(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1562 | struct fec_enet_private *fep = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1564 | /* Don't know what to do yet. */ |
Georg Hofmann | 3f104c3 | 2013-03-14 06:54:09 +0000 | [diff] [blame] | 1565 | napi_disable(&fep->napi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | fep->opened = 0; |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1567 | netif_stop_queue(ndev); |
| 1568 | fec_stop(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
Uwe Kleine-König | e497ba8 | 2011-01-17 20:04:23 +0100 | [diff] [blame] | 1570 | if (fep->phy_dev) { |
| 1571 | phy_stop(fep->phy_dev); |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1572 | phy_disconnect(fep->phy_dev); |
Uwe Kleine-König | e497ba8 | 2011-01-17 20:04:23 +0100 | [diff] [blame] | 1573 | } |
Bryan Wu | 418bd0d | 2010-05-28 03:40:39 -0700 | [diff] [blame] | 1574 | |
Uwe Kleine-König | db8880b | 2011-01-19 20:26:39 +0100 | [diff] [blame] | 1575 | fec_enet_free_buffers(ndev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | /* Set or clear the multicast filter for this adaptor. |
| 1581 | * Skeleton taken from sunlance driver. |
| 1582 | * The CPM Ethernet implementation allows Multicast as well as individual |
| 1583 | * MAC address filtering. Some of the drivers check to make sure it is |
| 1584 | * a group multicast address, and discard those that are not. I guess I |
| 1585 | * will do the same for now, but just remove the test if you want |
| 1586 | * individual filtering as well (do the upper net layers want or support |
| 1587 | * this kind of feature?). |
| 1588 | */ |
| 1589 | |
| 1590 | #define HASH_BITS 6 /* #bits in hash */ |
| 1591 | #define CRC32_POLY 0xEDB88320 |
| 1592 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1593 | static void set_multicast_list(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1595 | struct fec_enet_private *fep = netdev_priv(ndev); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1596 | struct netdev_hw_addr *ha; |
Jiri Pirko | 48e2f18 | 2010-02-22 09:22:26 +0000 | [diff] [blame] | 1597 | unsigned int i, bit, data, crc, tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | unsigned char hash; |
| 1599 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1600 | if (ndev->flags & IFF_PROMISC) { |
Sascha Hauer | f44d630 | 2009-04-15 03:11:30 +0000 | [diff] [blame] | 1601 | tmp = readl(fep->hwp + FEC_R_CNTRL); |
| 1602 | tmp |= 0x8; |
| 1603 | writel(tmp, fep->hwp + FEC_R_CNTRL); |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1604 | return; |
| 1605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1607 | tmp = readl(fep->hwp + FEC_R_CNTRL); |
| 1608 | tmp &= ~0x8; |
| 1609 | writel(tmp, fep->hwp + FEC_R_CNTRL); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1610 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1611 | if (ndev->flags & IFF_ALLMULTI) { |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1612 | /* Catch all multicast addresses, so set the |
| 1613 | * filter to all 1's |
| 1614 | */ |
| 1615 | writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 1616 | writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1618 | return; |
| 1619 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1620 | |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1621 | /* Clear filter and add the addresses in hash register |
| 1622 | */ |
| 1623 | writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 1624 | writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1626 | netdev_for_each_mc_addr(ha, ndev) { |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1627 | /* calculate crc32 value of mac address */ |
| 1628 | crc = 0xffffffff; |
| 1629 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1630 | for (i = 0; i < ndev->addr_len; i++) { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1631 | data = ha->addr[i]; |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1632 | for (bit = 0; bit < 8; bit++, data >>= 1) { |
| 1633 | crc = (crc >> 1) ^ |
| 1634 | (((crc ^ data) & 1) ? CRC32_POLY : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
Sascha Hauer | 4e83183 | 2009-04-15 01:32:19 +0000 | [diff] [blame] | 1637 | |
| 1638 | /* only upper 6 bits (HASH_BITS) are used |
| 1639 | * which point to specific bit in he hash registers |
| 1640 | */ |
| 1641 | hash = (crc >> (32 - HASH_BITS)) & 0x3f; |
| 1642 | |
| 1643 | if (hash > 31) { |
| 1644 | tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 1645 | tmp |= 1 << (hash - 32); |
| 1646 | writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); |
| 1647 | } else { |
| 1648 | tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
| 1649 | tmp |= 1 << hash; |
| 1650 | writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW); |
| 1651 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1655 | /* Set a MAC change in hardware. */ |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1656 | static int |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1657 | fec_set_mac_address(struct net_device *ndev, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1659 | struct fec_enet_private *fep = netdev_priv(ndev); |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1660 | struct sockaddr *addr = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1662 | if (!is_valid_ether_addr(addr->sa_data)) |
| 1663 | return -EADDRNOTAVAIL; |
| 1664 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1665 | memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len); |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1666 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1667 | writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | |
| 1668 | (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), |
Sascha Hauer | f44d630 | 2009-04-15 03:11:30 +0000 | [diff] [blame] | 1669 | fep->hwp + FEC_ADDR_LOW); |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1670 | writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), |
Mattias Walström | 7cff094 | 2010-05-05 00:55:48 -0700 | [diff] [blame] | 1671 | fep->hwp + FEC_ADDR_HIGH); |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1672 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 1675 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Ben Hutchings | 49ce9c2 | 2012-07-10 10:56:00 +0000 | [diff] [blame] | 1676 | /** |
| 1677 | * fec_poll_controller - FEC Poll controller function |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 1678 | * @dev: The FEC network adapter |
| 1679 | * |
| 1680 | * Polled functionality used by netconsole and others in non interrupt mode |
| 1681 | * |
| 1682 | */ |
Wei Yongjun | 47a5247 | 2013-03-20 05:06:11 +0000 | [diff] [blame] | 1683 | static void fec_poll_controller(struct net_device *dev) |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 1684 | { |
| 1685 | int i; |
| 1686 | struct fec_enet_private *fep = netdev_priv(dev); |
| 1687 | |
| 1688 | for (i = 0; i < FEC_IRQ_NUM; i++) { |
| 1689 | if (fep->irq[i] > 0) { |
| 1690 | disable_irq(fep->irq[i]); |
| 1691 | fec_enet_interrupt(fep->irq[i], dev); |
| 1692 | enable_irq(fep->irq[i]); |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | #endif |
| 1697 | |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 1698 | static int fec_set_features(struct net_device *netdev, |
| 1699 | netdev_features_t features) |
| 1700 | { |
| 1701 | struct fec_enet_private *fep = netdev_priv(netdev); |
| 1702 | netdev_features_t changed = features ^ netdev->features; |
| 1703 | |
| 1704 | netdev->features = features; |
| 1705 | |
| 1706 | /* Receive checksum has been changed */ |
| 1707 | if (changed & NETIF_F_RXCSUM) { |
| 1708 | if (features & NETIF_F_RXCSUM) |
| 1709 | fep->csum_flags |= FLAG_RX_CSUM_ENABLED; |
| 1710 | else |
| 1711 | fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; |
| 1712 | |
| 1713 | if (netif_running(netdev)) { |
| 1714 | fec_stop(netdev); |
| 1715 | fec_restart(netdev, fep->phy_dev->duplex); |
| 1716 | netif_wake_queue(netdev); |
| 1717 | } else { |
| 1718 | fec_restart(netdev, fep->phy_dev->duplex); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1725 | static const struct net_device_ops fec_netdev_ops = { |
| 1726 | .ndo_open = fec_enet_open, |
| 1727 | .ndo_stop = fec_enet_close, |
| 1728 | .ndo_start_xmit = fec_enet_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1729 | .ndo_set_rx_mode = set_multicast_list, |
Ben Hutchings | 635ecaa | 2009-07-09 17:59:01 +0000 | [diff] [blame] | 1730 | .ndo_change_mtu = eth_change_mtu, |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1731 | .ndo_validate_addr = eth_validate_addr, |
| 1732 | .ndo_tx_timeout = fec_timeout, |
| 1733 | .ndo_set_mac_address = fec_set_mac_address, |
Uwe Kleine-König | db8880b | 2011-01-19 20:26:39 +0100 | [diff] [blame] | 1734 | .ndo_do_ioctl = fec_enet_ioctl, |
Xiao Jiang | 7f5c6ad | 2011-09-29 02:15:57 +0000 | [diff] [blame] | 1735 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1736 | .ndo_poll_controller = fec_poll_controller, |
| 1737 | #endif |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 1738 | .ndo_set_features = fec_set_features, |
Sascha Hauer | 009fda8 | 2009-04-15 01:32:23 +0000 | [diff] [blame] | 1739 | }; |
| 1740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* |
| 1742 | * XXX: We need to clean up on failure exits here. |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1743 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1745 | static int fec_enet_init(struct net_device *ndev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | { |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1747 | struct fec_enet_private *fep = netdev_priv(ndev); |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 1748 | const struct platform_device_id *id_entry = |
| 1749 | platform_get_device_id(fep->pdev); |
Sascha Hauer | f0b3fbe | 2009-04-15 01:32:24 +0000 | [diff] [blame] | 1750 | struct bufdesc *cbd_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | |
Sascha Hauer | 8d4dd5c | 2009-04-15 01:32:17 +0000 | [diff] [blame] | 1752 | /* Allocate memory for buffer descriptors. */ |
| 1753 | cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma, |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 1754 | GFP_KERNEL); |
| 1755 | if (!cbd_base) |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 1756 | return -ENOMEM; |
Greg Ungerer | 562d2f8 | 2005-11-07 14:09:50 +1000 | [diff] [blame] | 1757 | |
Frank Li | 14109a5 | 2013-03-26 16:12:03 +0000 | [diff] [blame] | 1758 | memset(cbd_base, 0, PAGE_SIZE); |
Sebastian Siewior | 3b2b74c | 2008-05-01 14:08:12 +1000 | [diff] [blame] | 1759 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1760 | fep->netdev = ndev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
Shawn Guo | 49da97d | 2011-01-05 21:13:11 +0000 | [diff] [blame] | 1762 | /* Get the Ethernet address */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1763 | fec_get_mac(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
Sascha Hauer | 8d4dd5c | 2009-04-15 01:32:17 +0000 | [diff] [blame] | 1765 | /* Set receive and transmit descriptor base. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | fep->rx_bd_base = cbd_base; |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1767 | if (fep->bufdesc_ex) |
| 1768 | fep->tx_bd_base = (struct bufdesc *) |
| 1769 | (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE); |
| 1770 | else |
| 1771 | fep->tx_bd_base = cbd_base + RX_RING_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
Sascha Hauer | 22f6b86 | 2009-04-15 01:32:18 +0000 | [diff] [blame] | 1773 | /* The FEC Ethernet specific entries in the device structure */ |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1774 | ndev->watchdog_timeo = TX_TIMEOUT; |
| 1775 | ndev->netdev_ops = &fec_netdev_ops; |
| 1776 | ndev->ethtool_ops = &fec_enet_ethtool_ops; |
Rob Herring | 633e753 | 2010-02-05 08:56:20 +0000 | [diff] [blame] | 1777 | |
Frank Li | dc97538 | 2013-01-28 18:31:42 +0000 | [diff] [blame] | 1778 | writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); |
| 1779 | netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT); |
| 1780 | |
Shawn Guo | 4849625 | 2013-05-08 21:08:22 +0000 | [diff] [blame] | 1781 | if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) { |
| 1782 | /* enable hw accelerator */ |
| 1783 | ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
| 1784 | | NETIF_F_RXCSUM); |
| 1785 | ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
| 1786 | | NETIF_F_RXCSUM); |
| 1787 | fep->csum_flags |= FLAG_RX_CSUM_ENABLED; |
| 1788 | } |
Jim Baxter | 4c09eed | 2013-04-19 08:10:49 +0000 | [diff] [blame] | 1789 | |
Uwe Kleine-König | c556167 | 2011-01-19 11:58:12 +0100 | [diff] [blame] | 1790 | fec_restart(ndev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | return 0; |
| 1793 | } |
| 1794 | |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1795 | #ifdef CONFIG_OF |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 1796 | static void fec_reset_phy(struct platform_device *pdev) |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1797 | { |
| 1798 | int err, phy_reset; |
Shawn Guo | a3caad0 | 2012-06-27 03:45:24 +0000 | [diff] [blame] | 1799 | int msec = 1; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1800 | struct device_node *np = pdev->dev.of_node; |
| 1801 | |
| 1802 | if (!np) |
Shawn Guo | a9b2c8e | 2011-09-23 02:12:46 +0000 | [diff] [blame] | 1803 | return; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1804 | |
Shawn Guo | a3caad0 | 2012-06-27 03:45:24 +0000 | [diff] [blame] | 1805 | of_property_read_u32(np, "phy-reset-duration", &msec); |
| 1806 | /* A sane reset duration should not be longer than 1s */ |
| 1807 | if (msec > 1000) |
| 1808 | msec = 1; |
| 1809 | |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1810 | phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0); |
Fabio Estevam | 07dcf8e | 2013-02-18 10:20:31 +0000 | [diff] [blame] | 1811 | if (!gpio_is_valid(phy_reset)) |
| 1812 | return; |
| 1813 | |
Shawn Guo | 119fc00 | 2012-06-27 03:45:22 +0000 | [diff] [blame] | 1814 | err = devm_gpio_request_one(&pdev->dev, phy_reset, |
| 1815 | GPIOF_OUT_INIT_LOW, "phy-reset"); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1816 | if (err) { |
Fabio Estevam | 07dcf8e | 2013-02-18 10:20:31 +0000 | [diff] [blame] | 1817 | 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] | 1818 | return; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1819 | } |
Shawn Guo | a3caad0 | 2012-06-27 03:45:24 +0000 | [diff] [blame] | 1820 | msleep(msec); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1821 | gpio_set_value(phy_reset, 1); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1822 | } |
| 1823 | #else /* CONFIG_OF */ |
Fabio Estevam | 0c7768a | 2013-01-07 17:42:56 +0000 | [diff] [blame] | 1824 | static void fec_reset_phy(struct platform_device *pdev) |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1825 | { |
| 1826 | /* |
| 1827 | * In case of platform probe, the reset has been done |
| 1828 | * by machine code. |
| 1829 | */ |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1830 | } |
| 1831 | #endif /* CONFIG_OF */ |
| 1832 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 1833 | static int |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1834 | fec_probe(struct platform_device *pdev) |
| 1835 | { |
| 1836 | struct fec_enet_private *fep; |
Baruch Siach | 5eb32bd | 2010-05-24 00:36:13 -0700 | [diff] [blame] | 1837 | struct fec_platform_data *pdata; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1838 | struct net_device *ndev; |
| 1839 | int i, irq, ret = 0; |
| 1840 | struct resource *r; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1841 | const struct of_device_id *of_id; |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1842 | static int dev_id; |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1843 | |
| 1844 | of_id = of_match_device(fec_dt_ids, &pdev->dev); |
| 1845 | if (of_id) |
| 1846 | pdev->id_entry = of_id->data; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1847 | |
| 1848 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1849 | if (!r) |
| 1850 | return -ENXIO; |
| 1851 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1852 | /* Init network device */ |
| 1853 | ndev = alloc_etherdev(sizeof(struct fec_enet_private)); |
Fabio Estevam | 83e519b | 2013-03-11 07:32:55 +0000 | [diff] [blame] | 1854 | if (!ndev) |
| 1855 | return -ENOMEM; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1856 | |
| 1857 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 1858 | |
| 1859 | /* setup board info structure */ |
| 1860 | fep = netdev_priv(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1861 | |
Frank Li | baa70a5 | 2013-01-16 16:55:58 +0000 | [diff] [blame] | 1862 | /* default enable pause frame auto negotiation */ |
| 1863 | if (pdev->id_entry && |
| 1864 | (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT)) |
| 1865 | fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; |
| 1866 | |
Fabio Estevam | 83e519b | 2013-03-11 07:32:55 +0000 | [diff] [blame] | 1867 | fep->hwp = devm_request_and_ioremap(&pdev->dev, r); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1868 | fep->pdev = pdev; |
Shawn Guo | 43af940 | 2011-12-05 05:01:15 +0000 | [diff] [blame] | 1869 | fep->dev_id = dev_id++; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1870 | |
Frank Li | ff43da8 | 2013-01-03 16:04:23 +0000 | [diff] [blame] | 1871 | fep->bufdesc_ex = 0; |
| 1872 | |
Uwe Kleine-König | 24e531b | 2011-01-13 22:29:05 +0100 | [diff] [blame] | 1873 | if (!fep->hwp) { |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1874 | ret = -ENOMEM; |
| 1875 | goto failed_ioremap; |
| 1876 | } |
| 1877 | |
| 1878 | platform_set_drvdata(pdev, ndev); |
| 1879 | |
Guenter Roeck | 6c5f780 | 2013-04-02 09:35:10 +0000 | [diff] [blame] | 1880 | ret = of_get_phy_mode(pdev->dev.of_node); |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 1881 | if (ret < 0) { |
| 1882 | pdata = pdev->dev.platform_data; |
| 1883 | if (pdata) |
| 1884 | fep->phy_interface = pdata->phy; |
| 1885 | else |
| 1886 | fep->phy_interface = PHY_INTERFACE_MODE_MII; |
| 1887 | } else { |
| 1888 | fep->phy_interface = ret; |
| 1889 | } |
| 1890 | |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1891 | fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 1892 | if (IS_ERR(fep->clk_ipg)) { |
| 1893 | ret = PTR_ERR(fep->clk_ipg); |
| 1894 | goto failed_clk; |
| 1895 | } |
| 1896 | |
| 1897 | fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 1898 | if (IS_ERR(fep->clk_ahb)) { |
| 1899 | ret = PTR_ERR(fep->clk_ahb); |
| 1900 | goto failed_clk; |
| 1901 | } |
| 1902 | |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 1903 | /* enet_out is optional, depends on board */ |
| 1904 | fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out"); |
| 1905 | if (IS_ERR(fep->clk_enet_out)) |
| 1906 | fep->clk_enet_out = NULL; |
| 1907 | |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1908 | fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); |
Fabio Estevam | 7f7d6c2 | 2013-02-21 09:21:50 +0000 | [diff] [blame] | 1909 | fep->bufdesc_ex = |
| 1910 | pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX; |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1911 | if (IS_ERR(fep->clk_ptp)) { |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 1912 | fep->clk_ptp = NULL; |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1913 | fep->bufdesc_ex = 0; |
| 1914 | } |
| 1915 | |
| 1916 | clk_prepare_enable(fep->clk_ahb); |
| 1917 | clk_prepare_enable(fep->clk_ipg); |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 1918 | clk_prepare_enable(fep->clk_enet_out); |
| 1919 | clk_prepare_enable(fep->clk_ptp); |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1920 | |
Fabio Estevam | f4e9f3d | 2013-05-27 03:48:29 +0000 | [diff] [blame] | 1921 | fep->reg_phy = devm_regulator_get(&pdev->dev, "phy"); |
| 1922 | if (!IS_ERR(fep->reg_phy)) { |
| 1923 | ret = regulator_enable(fep->reg_phy); |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1924 | if (ret) { |
| 1925 | dev_err(&pdev->dev, |
| 1926 | "Failed to enable phy regulator: %d\n", ret); |
| 1927 | goto failed_regulator; |
| 1928 | } |
Fabio Estevam | f6a4d60 | 2013-05-27 03:48:31 +0000 | [diff] [blame] | 1929 | } else { |
| 1930 | fep->reg_phy = NULL; |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | fec_reset_phy(pdev); |
| 1934 | |
Fabio Estevam | 7f7d6c2 | 2013-02-21 09:21:50 +0000 | [diff] [blame] | 1935 | if (fep->bufdesc_ex) |
| 1936 | fec_ptp_init(ndev, pdev); |
| 1937 | |
| 1938 | ret = fec_enet_init(ndev); |
| 1939 | if (ret) |
| 1940 | goto failed_init; |
| 1941 | |
Xiao Jiang | c7c83d1 | 2011-09-29 02:15:56 +0000 | [diff] [blame] | 1942 | for (i = 0; i < FEC_IRQ_NUM; i++) { |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1943 | irq = platform_get_irq(pdev, i); |
Lothar Waßmann | 86f9f2c | 2011-12-07 21:59:28 +0000 | [diff] [blame] | 1944 | if (irq < 0) { |
| 1945 | if (i) |
| 1946 | break; |
| 1947 | ret = irq; |
| 1948 | goto failed_irq; |
| 1949 | } |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1950 | ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev); |
| 1951 | if (ret) { |
Uwe Kleine-König | b2b09ad | 2011-01-13 21:49:05 +0100 | [diff] [blame] | 1952 | while (--i >= 0) { |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1953 | irq = platform_get_irq(pdev, i); |
| 1954 | free_irq(irq, ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1955 | } |
| 1956 | goto failed_irq; |
| 1957 | } |
| 1958 | } |
| 1959 | |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1960 | ret = fec_enet_mii_init(pdev); |
| 1961 | if (ret) |
| 1962 | goto failed_mii_init; |
| 1963 | |
Oskar Schirmer | 03c698c | 2010-10-07 02:30:30 +0000 | [diff] [blame] | 1964 | /* Carrier starts down, phylib will bring it up */ |
| 1965 | netif_carrier_off(ndev); |
| 1966 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1967 | ret = register_netdev(ndev); |
| 1968 | if (ret) |
| 1969 | goto failed_register; |
| 1970 | |
Fabio Estevam | eb1d064 | 2013-04-13 07:25:36 +0000 | [diff] [blame] | 1971 | if (fep->bufdesc_ex && fep->ptp_clock) |
| 1972 | netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); |
| 1973 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 1974 | INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1975 | return 0; |
| 1976 | |
| 1977 | failed_register: |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 1978 | fec_enet_mii_remove(fep); |
| 1979 | failed_mii_init: |
Fabio Estevam | 7a2bbd8 | 2013-05-27 03:48:30 +0000 | [diff] [blame] | 1980 | failed_irq: |
Fabio Estevam | e2f8d55 | 2013-02-22 06:40:45 +0000 | [diff] [blame] | 1981 | for (i = 0; i < FEC_IRQ_NUM; i++) { |
| 1982 | irq = platform_get_irq(pdev, i); |
| 1983 | if (irq > 0) |
| 1984 | free_irq(irq, ndev); |
| 1985 | } |
Fabio Estevam | 7a2bbd8 | 2013-05-27 03:48:30 +0000 | [diff] [blame] | 1986 | failed_init: |
Fabio Estevam | f6a4d60 | 2013-05-27 03:48:31 +0000 | [diff] [blame] | 1987 | if (fep->reg_phy) |
| 1988 | regulator_disable(fep->reg_phy); |
Shawn Guo | 5fa9c0f | 2012-06-27 03:45:21 +0000 | [diff] [blame] | 1989 | failed_regulator: |
Sascha Hauer | f4d40de | 2012-03-07 09:30:49 +0100 | [diff] [blame] | 1990 | clk_disable_unprepare(fep->clk_ahb); |
| 1991 | clk_disable_unprepare(fep->clk_ipg); |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 1992 | clk_disable_unprepare(fep->clk_enet_out); |
| 1993 | clk_disable_unprepare(fep->clk_ptp); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1994 | failed_clk: |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 1995 | failed_ioremap: |
| 1996 | free_netdev(ndev); |
| 1997 | |
| 1998 | return ret; |
| 1999 | } |
| 2000 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 2001 | static int |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2002 | fec_drv_remove(struct platform_device *pdev) |
| 2003 | { |
| 2004 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 2005 | struct fec_enet_private *fep = netdev_priv(ndev); |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 2006 | int i; |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2007 | |
Frank Li | 54309fa | 2013-05-07 14:08:44 +0000 | [diff] [blame] | 2008 | cancel_delayed_work_sync(&(fep->delay_work.delay_work)); |
Lothar Waßmann | e163cc9 | 2011-12-07 21:59:31 +0000 | [diff] [blame] | 2009 | unregister_netdev(ndev); |
Bryan Wu | e6b043d | 2010-03-31 02:10:44 +0000 | [diff] [blame] | 2010 | fec_enet_mii_remove(fep); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 2011 | del_timer_sync(&fep->time_keep); |
Fabio Estevam | c55284e | 2013-05-27 03:48:32 +0000 | [diff] [blame] | 2012 | for (i = 0; i < FEC_IRQ_NUM; i++) { |
| 2013 | int irq = platform_get_irq(pdev, i); |
| 2014 | if (irq > 0) |
| 2015 | free_irq(irq, ndev); |
| 2016 | } |
Fabio Estevam | f6a4d60 | 2013-05-27 03:48:31 +0000 | [diff] [blame] | 2017 | if (fep->reg_phy) |
| 2018 | regulator_disable(fep->reg_phy); |
Frank Li | 6605b73 | 2012-10-30 18:25:31 +0000 | [diff] [blame] | 2019 | clk_disable_unprepare(fep->clk_ptp); |
| 2020 | if (fep->ptp_clock) |
| 2021 | ptp_clock_unregister(fep->ptp_clock); |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 2022 | clk_disable_unprepare(fep->clk_enet_out); |
Sascha Hauer | f4d40de | 2012-03-07 09:30:49 +0100 | [diff] [blame] | 2023 | clk_disable_unprepare(fep->clk_ahb); |
| 2024 | clk_disable_unprepare(fep->clk_ipg); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2025 | free_netdev(ndev); |
Uwe Kleine-König | 28e2188 | 2011-01-13 21:44:18 +0100 | [diff] [blame] | 2026 | |
Uwe Kleine-König | b3cde36 | 2011-01-25 18:03:37 +0100 | [diff] [blame] | 2027 | platform_set_drvdata(pdev, NULL); |
| 2028 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2029 | return 0; |
| 2030 | } |
| 2031 | |
Fabio Estevam | bf7bfd7 | 2013-04-16 08:17:46 +0000 | [diff] [blame] | 2032 | #ifdef CONFIG_PM_SLEEP |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2033 | static int |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2034 | fec_suspend(struct device *dev) |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2035 | { |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2036 | struct net_device *ndev = dev_get_drvdata(dev); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2037 | struct fec_enet_private *fep = netdev_priv(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2038 | |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2039 | if (netif_running(ndev)) { |
| 2040 | fec_stop(ndev); |
| 2041 | netif_device_detach(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2042 | } |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 2043 | clk_disable_unprepare(fep->clk_enet_out); |
Sascha Hauer | f4d40de | 2012-03-07 09:30:49 +0100 | [diff] [blame] | 2044 | clk_disable_unprepare(fep->clk_ahb); |
| 2045 | clk_disable_unprepare(fep->clk_ipg); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2046 | |
Fabio Estevam | 238f7bc | 2013-05-27 03:48:33 +0000 | [diff] [blame^] | 2047 | if (fep->reg_phy) |
| 2048 | regulator_disable(fep->reg_phy); |
| 2049 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2050 | return 0; |
| 2051 | } |
| 2052 | |
| 2053 | static int |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2054 | fec_resume(struct device *dev) |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2055 | { |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2056 | struct net_device *ndev = dev_get_drvdata(dev); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2057 | struct fec_enet_private *fep = netdev_priv(ndev); |
Fabio Estevam | 238f7bc | 2013-05-27 03:48:33 +0000 | [diff] [blame^] | 2058 | int ret; |
| 2059 | |
| 2060 | if (fep->reg_phy) { |
| 2061 | ret = regulator_enable(fep->reg_phy); |
| 2062 | if (ret) |
| 2063 | return ret; |
| 2064 | } |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2065 | |
Linus Torvalds | 38f56f3 | 2013-05-07 11:06:17 -0700 | [diff] [blame] | 2066 | clk_prepare_enable(fep->clk_enet_out); |
Sascha Hauer | f4d40de | 2012-03-07 09:30:49 +0100 | [diff] [blame] | 2067 | clk_prepare_enable(fep->clk_ahb); |
| 2068 | clk_prepare_enable(fep->clk_ipg); |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2069 | if (netif_running(ndev)) { |
| 2070 | fec_restart(ndev, fep->full_duplex); |
| 2071 | netif_device_attach(ndev); |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2072 | } |
Uwe Kleine-König | 04e5216 | 2011-01-13 21:53:40 +0100 | [diff] [blame] | 2073 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2074 | return 0; |
| 2075 | } |
Fabio Estevam | bf7bfd7 | 2013-04-16 08:17:46 +0000 | [diff] [blame] | 2076 | #endif /* CONFIG_PM_SLEEP */ |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2077 | |
Fabio Estevam | bf7bfd7 | 2013-04-16 08:17:46 +0000 | [diff] [blame] | 2078 | static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume); |
Denis Kirjanov | 59d4289 | 2010-06-02 09:27:04 +0000 | [diff] [blame] | 2079 | |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2080 | static struct platform_driver fec_driver = { |
| 2081 | .driver = { |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 2082 | .name = DRIVER_NAME, |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2083 | .owner = THIS_MODULE, |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2084 | .pm = &fec_pm_ops, |
Shawn Guo | ca2cc33 | 2011-06-25 02:04:35 +0800 | [diff] [blame] | 2085 | .of_match_table = fec_dt_ids, |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2086 | }, |
Shawn Guo | b5680e0 | 2011-01-05 21:13:13 +0000 | [diff] [blame] | 2087 | .id_table = fec_devtype, |
Eric Benard | 87cad5c | 2010-06-18 04:19:54 +0000 | [diff] [blame] | 2088 | .probe = fec_probe, |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 2089 | .remove = fec_drv_remove, |
Sascha Hauer | ead7318 | 2009-01-28 23:03:11 +0000 | [diff] [blame] | 2090 | }; |
| 2091 | |
Fabio Estevam | aaca237 | 2012-01-23 16:44:50 +0000 | [diff] [blame] | 2092 | module_platform_driver(fec_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | |
| 2094 | MODULE_LICENSE("GPL"); |