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