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