11f508124SFabio Estevam // SPDX-License-Identifier: GPL-2.0+ 2793fc096SFrank Li /* 3793fc096SFrank Li * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx. 4793fc096SFrank Li * Copyright (c) 1997 Dan Malek (dmalek@jlc.net) 5793fc096SFrank Li * 6793fc096SFrank Li * Right now, I am very wasteful with the buffers. I allocate memory 7793fc096SFrank Li * pages and then divide them into 2K frame buffers. This way I know I 8793fc096SFrank Li * have buffers large enough to hold one frame within one buffer descriptor. 9793fc096SFrank Li * Once I get this working, I will use 64 or 128 byte CPM buffers, which 10793fc096SFrank Li * will be much more memory efficient and will easily handle lots of 11793fc096SFrank Li * small packets. 12793fc096SFrank Li * 13793fc096SFrank Li * Much better multiple PHY support by Magnus Damm. 14793fc096SFrank Li * Copyright (c) 2000 Ericsson Radio Systems AB. 15793fc096SFrank Li * 16793fc096SFrank Li * Support for FEC controller of ColdFire processors. 17793fc096SFrank Li * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com) 18793fc096SFrank Li * 19793fc096SFrank Li * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be) 20793fc096SFrank Li * Copyright (c) 2004-2006 Macq Electronique SA. 21793fc096SFrank Li * 22793fc096SFrank Li * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. 23793fc096SFrank Li */ 24793fc096SFrank Li 25793fc096SFrank Li #include <linux/module.h> 26793fc096SFrank Li #include <linux/kernel.h> 27793fc096SFrank Li #include <linux/string.h> 288fff755eSAndrew Lunn #include <linux/pm_runtime.h> 29793fc096SFrank Li #include <linux/ptrace.h> 30793fc096SFrank Li #include <linux/errno.h> 31793fc096SFrank Li #include <linux/ioport.h> 32793fc096SFrank Li #include <linux/slab.h> 33793fc096SFrank Li #include <linux/interrupt.h> 34793fc096SFrank Li #include <linux/delay.h> 35793fc096SFrank Li #include <linux/netdevice.h> 36793fc096SFrank Li #include <linux/etherdevice.h> 37793fc096SFrank Li #include <linux/skbuff.h> 384c09eed9SJim Baxter #include <linux/in.h> 394c09eed9SJim Baxter #include <linux/ip.h> 404c09eed9SJim Baxter #include <net/ip.h> 4179f33912SNimrod Andy #include <net/tso.h> 424c09eed9SJim Baxter #include <linux/tcp.h> 434c09eed9SJim Baxter #include <linux/udp.h> 444c09eed9SJim Baxter #include <linux/icmp.h> 45793fc096SFrank Li #include <linux/spinlock.h> 46793fc096SFrank Li #include <linux/workqueue.h> 47793fc096SFrank Li #include <linux/bitops.h> 48793fc096SFrank Li #include <linux/io.h> 49793fc096SFrank Li #include <linux/irq.h> 50793fc096SFrank Li #include <linux/clk.h> 5116f6e983SKrzysztof Kozlowski #include <linux/crc32.h> 52793fc096SFrank Li #include <linux/platform_device.h> 537f854420SAndrew Lunn #include <linux/mdio.h> 54793fc096SFrank Li #include <linux/phy.h> 55793fc096SFrank Li #include <linux/fec.h> 56793fc096SFrank Li #include <linux/of.h> 57793fc096SFrank Li #include <linux/of_device.h> 58793fc096SFrank Li #include <linux/of_gpio.h> 59407066f8SUwe Kleine-König #include <linux/of_mdio.h> 60793fc096SFrank Li #include <linux/of_net.h> 61793fc096SFrank Li #include <linux/regulator/consumer.h> 62cdffcf1bSJim Baxter #include <linux/if_vlan.h> 63a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h> 64c259c132SFrank Li #include <linux/prefetch.h> 65da722186SMartin Fuzzey #include <linux/mfd/syscon.h> 66da722186SMartin Fuzzey #include <linux/regmap.h> 6729380905SLucas Stach #include <soc/imx/cpuidle.h> 68793fc096SFrank Li 69793fc096SFrank Li #include <asm/cacheflush.h> 70793fc096SFrank Li 71793fc096SFrank Li #include "fec.h" 72793fc096SFrank Li 73772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev); 74d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev); 75772e42b0SChristoph Müllner 76793fc096SFrank Li #define DRIVER_NAME "fec" 77793fc096SFrank Li 78793fc096SFrank Li /* Pause frame feild and FIFO threshold */ 79793fc096SFrank Li #define FEC_ENET_FCE (1 << 5) 80793fc096SFrank Li #define FEC_ENET_RSEM_V 0x84 81793fc096SFrank Li #define FEC_ENET_RSFL_V 16 82793fc096SFrank Li #define FEC_ENET_RAEM_V 0x8 83793fc096SFrank Li #define FEC_ENET_RAFL_V 0x8 84793fc096SFrank Li #define FEC_ENET_OPD_V 0xFFF0 858fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT 100 /* ms */ 86793fc096SFrank Li 87da722186SMartin Fuzzey struct fec_devinfo { 88da722186SMartin Fuzzey u32 quirks; 89da722186SMartin Fuzzey }; 90da722186SMartin Fuzzey 91da722186SMartin Fuzzey static const struct fec_devinfo fec_imx25_info = { 92da722186SMartin Fuzzey .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR | 93da722186SMartin Fuzzey FEC_QUIRK_HAS_FRREG, 94da722186SMartin Fuzzey }; 95da722186SMartin Fuzzey 96da722186SMartin Fuzzey static const struct fec_devinfo fec_imx27_info = { 97da722186SMartin Fuzzey .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG, 98da722186SMartin Fuzzey }; 99da722186SMartin Fuzzey 100da722186SMartin Fuzzey static const struct fec_devinfo fec_imx28_info = { 101da722186SMartin Fuzzey .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME | 102da722186SMartin Fuzzey FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC | 103da722186SMartin Fuzzey FEC_QUIRK_HAS_FRREG, 104da722186SMartin Fuzzey }; 105da722186SMartin Fuzzey 106da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6q_info = { 107da722186SMartin Fuzzey .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 108da722186SMartin Fuzzey FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 109da722186SMartin Fuzzey FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 | 110da722186SMartin Fuzzey FEC_QUIRK_HAS_RACC, 111da722186SMartin Fuzzey }; 112da722186SMartin Fuzzey 113da722186SMartin Fuzzey static const struct fec_devinfo fec_mvf600_info = { 114da722186SMartin Fuzzey .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC, 115da722186SMartin Fuzzey }; 116da722186SMartin Fuzzey 117da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6x_info = { 118da722186SMartin Fuzzey .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 119da722186SMartin Fuzzey FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 120da722186SMartin Fuzzey FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 121da722186SMartin Fuzzey FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 122da722186SMartin Fuzzey FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE, 123da722186SMartin Fuzzey }; 124da722186SMartin Fuzzey 125da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6ul_info = { 126da722186SMartin Fuzzey .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 127da722186SMartin Fuzzey FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 128da722186SMartin Fuzzey FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 | 129da722186SMartin Fuzzey FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC | 130da722186SMartin Fuzzey FEC_QUIRK_HAS_COALESCE, 131da722186SMartin Fuzzey }; 132da722186SMartin Fuzzey 133793fc096SFrank Li static struct platform_device_id fec_devtype[] = { 134793fc096SFrank Li { 135793fc096SFrank Li /* keep it for coldfire */ 136793fc096SFrank Li .name = DRIVER_NAME, 137793fc096SFrank Li .driver_data = 0, 138793fc096SFrank Li }, { 139793fc096SFrank Li .name = "imx25-fec", 140da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_imx25_info, 141793fc096SFrank Li }, { 142793fc096SFrank Li .name = "imx27-fec", 143da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_imx27_info, 144793fc096SFrank Li }, { 145793fc096SFrank Li .name = "imx28-fec", 146da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_imx28_info, 147793fc096SFrank Li }, { 148793fc096SFrank Li .name = "imx6q-fec", 149da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_imx6q_info, 150793fc096SFrank Li }, { 15136803542SShawn Guo .name = "mvf600-fec", 152da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_mvf600_info, 153ca7c4a45SJingchang Lu }, { 15495a77470SFugang Duan .name = "imx6sx-fec", 155da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_imx6x_info, 15695a77470SFugang Duan }, { 157a51d3ab5SFugang Duan .name = "imx6ul-fec", 158da722186SMartin Fuzzey .driver_data = (kernel_ulong_t)&fec_imx6ul_info, 159a51d3ab5SFugang Duan }, { 160793fc096SFrank Li /* sentinel */ 161793fc096SFrank Li } 162793fc096SFrank Li }; 163793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype); 164793fc096SFrank Li 165793fc096SFrank Li enum imx_fec_type { 166793fc096SFrank Li IMX25_FEC = 1, /* runs on i.mx25/50/53 */ 167793fc096SFrank Li IMX27_FEC, /* runs on i.mx27/35/51 */ 168793fc096SFrank Li IMX28_FEC, 169793fc096SFrank Li IMX6Q_FEC, 17036803542SShawn Guo MVF600_FEC, 171ba593e00SFugang Duan IMX6SX_FEC, 172a51d3ab5SFugang Duan IMX6UL_FEC, 173793fc096SFrank Li }; 174793fc096SFrank Li 175793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = { 176793fc096SFrank Li { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, 177793fc096SFrank Li { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, 178793fc096SFrank Li { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, 179793fc096SFrank Li { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, 18036803542SShawn Guo { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, 181ba593e00SFugang Duan { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], }, 182a51d3ab5SFugang Duan { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], }, 183793fc096SFrank Li { /* sentinel */ } 184793fc096SFrank Li }; 185793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids); 186793fc096SFrank Li 187793fc096SFrank Li static unsigned char macaddr[ETH_ALEN]; 188793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0); 189793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); 190793fc096SFrank Li 191793fc096SFrank Li #if defined(CONFIG_M5272) 192793fc096SFrank Li /* 193793fc096SFrank Li * Some hardware gets it MAC address out of local flash memory. 194793fc096SFrank Li * if this is non-zero then assume it is the address to get MAC from. 195793fc096SFrank Li */ 196793fc096SFrank Li #if defined(CONFIG_NETtel) 197793fc096SFrank Li #define FEC_FLASHMAC 0xf0006006 198793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) 199793fc096SFrank Li #define FEC_FLASHMAC 0xf0006000 200793fc096SFrank Li #elif defined(CONFIG_CANCam) 201793fc096SFrank Li #define FEC_FLASHMAC 0xf0020000 202793fc096SFrank Li #elif defined (CONFIG_M5272C3) 203793fc096SFrank Li #define FEC_FLASHMAC (0xffe04000 + 4) 204793fc096SFrank Li #elif defined(CONFIG_MOD5272) 205793fc096SFrank Li #define FEC_FLASHMAC 0xffc0406b 206793fc096SFrank Li #else 207793fc096SFrank Li #define FEC_FLASHMAC 0 208793fc096SFrank Li #endif 209793fc096SFrank Li #endif /* CONFIG_M5272 */ 210793fc096SFrank Li 211cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets. 212fbbeefddSAndrew Lunn * 213fbbeefddSAndrew Lunn * 2048 byte skbufs are allocated. However, alignment requirements 214fbbeefddSAndrew Lunn * varies between FEC variants. Worst case is 64, so round down by 64. 215793fc096SFrank Li */ 216fbbeefddSAndrew Lunn #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64)) 217793fc096SFrank Li #define PKT_MINBUF_SIZE 64 218793fc096SFrank Li 2194c09eed9SJim Baxter /* FEC receive acceleration */ 2204c09eed9SJim Baxter #define FEC_RACC_IPDIS (1 << 1) 2214c09eed9SJim Baxter #define FEC_RACC_PRODIS (1 << 2) 2223ac72b7bSEric Nelson #define FEC_RACC_SHIFT16 BIT(7) 2234c09eed9SJim Baxter #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS) 2244c09eed9SJim Baxter 2252b30842bSAndrew Lunn /* MIB Control Register */ 2262b30842bSAndrew Lunn #define FEC_MIB_CTRLSTAT_DISABLE BIT(31) 2272b30842bSAndrew Lunn 228793fc096SFrank Li /* 229793fc096SFrank Li * The 5270/5271/5280/5282/532x RX control register also contains maximum frame 230793fc096SFrank Li * size bits. Other FEC hardware does not, so we need to take that into 231793fc096SFrank Li * account when setting it. 232793fc096SFrank Li */ 233793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 2343f1dcc6aSLucas Stach defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 2353f1dcc6aSLucas Stach defined(CONFIG_ARM64) 236793fc096SFrank Li #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) 237793fc096SFrank Li #else 238793fc096SFrank Li #define OPT_FRAME_SIZE 0 239793fc096SFrank Li #endif 240793fc096SFrank Li 241793fc096SFrank Li /* FEC MII MMFR bits definition */ 242793fc096SFrank Li #define FEC_MMFR_ST (1 << 30) 243d3ee8ec7SMarco Hartmann #define FEC_MMFR_ST_C45 (0) 244793fc096SFrank Li #define FEC_MMFR_OP_READ (2 << 28) 245d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_READ_C45 (3 << 28) 246793fc096SFrank Li #define FEC_MMFR_OP_WRITE (1 << 28) 247d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_ADDR_WRITE (0) 248793fc096SFrank Li #define FEC_MMFR_PA(v) ((v & 0x1f) << 23) 249793fc096SFrank Li #define FEC_MMFR_RA(v) ((v & 0x1f) << 18) 250793fc096SFrank Li #define FEC_MMFR_TA (2 << 16) 251793fc096SFrank Li #define FEC_MMFR_DATA(v) (v & 0xffff) 252de40ed31SNimrod Andy /* FEC ECR bits definition */ 253de40ed31SNimrod Andy #define FEC_ECR_MAGICEN (1 << 2) 254de40ed31SNimrod Andy #define FEC_ECR_SLEEP (1 << 3) 255793fc096SFrank Li 256793fc096SFrank Li #define FEC_MII_TIMEOUT 30000 /* us */ 257793fc096SFrank Li 258793fc096SFrank Li /* Transmitter timeout */ 259793fc096SFrank Li #define TX_TIMEOUT (2 * HZ) 260793fc096SFrank Li 261793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG 0x1 262793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE 0x2 263de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0) 264de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE (0x1 << 1) 265de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2) 266793fc096SFrank Li 2671b7bde6dSNimrod Andy #define COPYBREAK_DEFAULT 256 2681b7bde6dSNimrod Andy 26979f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */ 27079f33912SNimrod Andy #define FEC_MAX_TSO_SEGS 100 27179f33912SNimrod Andy #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 27279f33912SNimrod Andy 27379f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \ 27479f33912SNimrod Andy ((addr >= txq->tso_hdrs_dma) && \ 2757355f276STroy Kisky (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE)) 27679f33912SNimrod Andy 277793fc096SFrank Li static int mii_cnt; 278793fc096SFrank Li 2797355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, 2807355f276STroy Kisky struct bufdesc_prop *bd) 281793fc096SFrank Li { 2827355f276STroy Kisky return (bdp >= bd->last) ? bd->base 283145d6e29SFugang Duan : (struct bufdesc *)(((void *)bdp) + bd->dsize); 284793fc096SFrank Li } 285793fc096SFrank Li 2867355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, 2877355f276STroy Kisky struct bufdesc_prop *bd) 28836e24e2eSDuan Fugang-B38611 { 2897355f276STroy Kisky return (bdp <= bd->base) ? bd->last 290145d6e29SFugang Duan : (struct bufdesc *)(((void *)bdp) - bd->dsize); 29136e24e2eSDuan Fugang-B38611 } 29236e24e2eSDuan Fugang-B38611 2937355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp, 2947355f276STroy Kisky struct bufdesc_prop *bd) 29561a4427bSNimrod Andy { 2967355f276STroy Kisky return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2; 29761a4427bSNimrod Andy } 29861a4427bSNimrod Andy 2997355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq) 3006e909283SNimrod Andy { 3016e909283SNimrod Andy int entries; 3026e909283SNimrod Andy 3037355f276STroy Kisky entries = (((const char *)txq->dirty_tx - 3047355f276STroy Kisky (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1; 3056e909283SNimrod Andy 3067355f276STroy Kisky return entries >= 0 ? entries : entries + txq->bd.ring_size; 3076e909283SNimrod Andy } 3086e909283SNimrod Andy 309c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len) 310793fc096SFrank Li { 311793fc096SFrank Li int i; 312793fc096SFrank Li unsigned int *buf = bufaddr; 313793fc096SFrank Li 3147b487d07SLothar Waßmann for (i = 0; i < len; i += 4, buf++) 315e453789aSLothar Waßmann swab32s(buf); 316793fc096SFrank Li } 317793fc096SFrank Li 3181310b544SLothar Waßmann static void swap_buffer2(void *dst_buf, void *src_buf, int len) 3191310b544SLothar Waßmann { 3201310b544SLothar Waßmann int i; 3211310b544SLothar Waßmann unsigned int *src = src_buf; 3221310b544SLothar Waßmann unsigned int *dst = dst_buf; 3231310b544SLothar Waßmann 3241310b544SLothar Waßmann for (i = 0; i < len; i += 4, src++, dst++) 3251310b544SLothar Waßmann *dst = swab32p(src); 3261310b544SLothar Waßmann } 3271310b544SLothar Waßmann 328344756f6SRussell King static void fec_dump(struct net_device *ndev) 329344756f6SRussell King { 330344756f6SRussell King struct fec_enet_private *fep = netdev_priv(ndev); 3314d494cdcSFugang Duan struct bufdesc *bdp; 3324d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 3334d494cdcSFugang Duan int index = 0; 334344756f6SRussell King 335344756f6SRussell King netdev_info(ndev, "TX ring dump\n"); 336344756f6SRussell King pr_info("Nr SC addr len SKB\n"); 337344756f6SRussell King 3384d494cdcSFugang Duan txq = fep->tx_queue[0]; 3397355f276STroy Kisky bdp = txq->bd.base; 3404d494cdcSFugang Duan 341344756f6SRussell King do { 3425cfa3039SJohannes Berg pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n", 343344756f6SRussell King index, 3447355f276STroy Kisky bdp == txq->bd.cur ? 'S' : ' ', 3454d494cdcSFugang Duan bdp == txq->dirty_tx ? 'H' : ' ', 3465cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_sc), 3475cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 3485cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_datlen), 3494d494cdcSFugang Duan txq->tx_skbuff[index]); 3507355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 351344756f6SRussell King index++; 3527355f276STroy Kisky } while (bdp != txq->bd.base); 353344756f6SRussell King } 354344756f6SRussell King 35562a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb) 35662a02c98SFugang Duan { 35762a02c98SFugang Duan return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4; 35862a02c98SFugang Duan } 35962a02c98SFugang Duan 3604c09eed9SJim Baxter static int 3614c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) 3624c09eed9SJim Baxter { 3634c09eed9SJim Baxter /* Only run for packets requiring a checksum. */ 3644c09eed9SJim Baxter if (skb->ip_summed != CHECKSUM_PARTIAL) 3654c09eed9SJim Baxter return 0; 3664c09eed9SJim Baxter 3674c09eed9SJim Baxter if (unlikely(skb_cow_head(skb, 0))) 3684c09eed9SJim Baxter return -1; 3694c09eed9SJim Baxter 37062a02c98SFugang Duan if (is_ipv4_pkt(skb)) 37196c50caaSNimrod Andy ip_hdr(skb)->check = 0; 3724c09eed9SJim Baxter *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; 3734c09eed9SJim Baxter 3744c09eed9SJim Baxter return 0; 3754c09eed9SJim Baxter } 3764c09eed9SJim Baxter 377c4bc44c6SKevin Hao static struct bufdesc * 3784d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq, 3794d494cdcSFugang Duan struct sk_buff *skb, 3804d494cdcSFugang Duan struct net_device *ndev) 3816e909283SNimrod Andy { 3826e909283SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 3837355f276STroy Kisky struct bufdesc *bdp = txq->bd.cur; 3846e909283SNimrod Andy struct bufdesc_ex *ebdp; 3856e909283SNimrod Andy int nr_frags = skb_shinfo(skb)->nr_frags; 3866e909283SNimrod Andy int frag, frag_len; 3876e909283SNimrod Andy unsigned short status; 3886e909283SNimrod Andy unsigned int estatus = 0; 3896e909283SNimrod Andy skb_frag_t *this_frag; 3906e909283SNimrod Andy unsigned int index; 3916e909283SNimrod Andy void *bufaddr; 392d6bf3143SRussell King dma_addr_t addr; 3936e909283SNimrod Andy int i; 3946e909283SNimrod Andy 3956e909283SNimrod Andy for (frag = 0; frag < nr_frags; frag++) { 3966e909283SNimrod Andy this_frag = &skb_shinfo(skb)->frags[frag]; 3977355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 3986e909283SNimrod Andy ebdp = (struct bufdesc_ex *)bdp; 3996e909283SNimrod Andy 4005cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 4016e909283SNimrod Andy status &= ~BD_ENET_TX_STATS; 4026e909283SNimrod Andy status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 403d7840976SMatthew Wilcox (Oracle) frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]); 4046e909283SNimrod Andy 4056e909283SNimrod Andy /* Handle the last BD specially */ 4066e909283SNimrod Andy if (frag == nr_frags - 1) { 4076e909283SNimrod Andy status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 4086e909283SNimrod Andy if (fep->bufdesc_ex) { 4096e909283SNimrod Andy estatus |= BD_ENET_TX_INT; 4106e909283SNimrod Andy if (unlikely(skb_shinfo(skb)->tx_flags & 4116e909283SNimrod Andy SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 4126e909283SNimrod Andy estatus |= BD_ENET_TX_TS; 4136e909283SNimrod Andy } 4146e909283SNimrod Andy } 4156e909283SNimrod Andy 4166e909283SNimrod Andy if (fep->bufdesc_ex) { 4176b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 41853bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 4196e909283SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 4206e909283SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 4216e909283SNimrod Andy ebdp->cbd_bdu = 0; 4225cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 4236e909283SNimrod Andy } 4246e909283SNimrod Andy 425d7840976SMatthew Wilcox (Oracle) bufaddr = skb_frag_address(this_frag); 4266e909283SNimrod Andy 4277355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 42841ef84ceSFugang Duan if (((unsigned long) bufaddr) & fep->tx_align || 4296b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 4304d494cdcSFugang Duan memcpy(txq->tx_bounce[index], bufaddr, frag_len); 4314d494cdcSFugang Duan bufaddr = txq->tx_bounce[index]; 4326e909283SNimrod Andy 4336b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 4346e909283SNimrod Andy swap_buffer(bufaddr, frag_len); 4356e909283SNimrod Andy } 4366e909283SNimrod Andy 437d6bf3143SRussell King addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len, 438d6bf3143SRussell King DMA_TO_DEVICE); 439d6bf3143SRussell King if (dma_mapping_error(&fep->pdev->dev, addr)) { 4406e909283SNimrod Andy if (net_ratelimit()) 4416e909283SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 4426e909283SNimrod Andy goto dma_mapping_error; 4436e909283SNimrod Andy } 4446e909283SNimrod Andy 4455cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(addr); 4465cfa3039SJohannes Berg bdp->cbd_datlen = cpu_to_fec16(frag_len); 447be293467STroy Kisky /* Make sure the updates to rest of the descriptor are 448be293467STroy Kisky * performed before transferring ownership. 449be293467STroy Kisky */ 450be293467STroy Kisky wmb(); 4515cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 4526e909283SNimrod Andy } 4536e909283SNimrod Andy 454c4bc44c6SKevin Hao return bdp; 4556e909283SNimrod Andy dma_mapping_error: 4567355f276STroy Kisky bdp = txq->bd.cur; 4576e909283SNimrod Andy for (i = 0; i < frag; i++) { 4587355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 4595cfa3039SJohannes Berg dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr), 4605cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE); 4616e909283SNimrod Andy } 462c4bc44c6SKevin Hao return ERR_PTR(-ENOMEM); 4636e909283SNimrod Andy } 4646e909283SNimrod Andy 4654d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, 4664d494cdcSFugang Duan struct sk_buff *skb, struct net_device *ndev) 4676e909283SNimrod Andy { 4686e909283SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 4696e909283SNimrod Andy int nr_frags = skb_shinfo(skb)->nr_frags; 4706e909283SNimrod Andy struct bufdesc *bdp, *last_bdp; 4716e909283SNimrod Andy void *bufaddr; 472d6bf3143SRussell King dma_addr_t addr; 4736e909283SNimrod Andy unsigned short status; 4746e909283SNimrod Andy unsigned short buflen; 4756e909283SNimrod Andy unsigned int estatus = 0; 4766e909283SNimrod Andy unsigned int index; 47779f33912SNimrod Andy int entries_free; 4786e909283SNimrod Andy 4797355f276STroy Kisky entries_free = fec_enet_get_free_txdesc_num(txq); 48079f33912SNimrod Andy if (entries_free < MAX_SKB_FRAGS + 1) { 48179f33912SNimrod Andy dev_kfree_skb_any(skb); 48279f33912SNimrod Andy if (net_ratelimit()) 48379f33912SNimrod Andy netdev_err(ndev, "NOT enough BD for SG!\n"); 48479f33912SNimrod Andy return NETDEV_TX_OK; 48579f33912SNimrod Andy } 48679f33912SNimrod Andy 4876e909283SNimrod Andy /* Protocol checksum off-load for TCP and UDP. */ 4886e909283SNimrod Andy if (fec_enet_clear_csum(skb, ndev)) { 4896e909283SNimrod Andy dev_kfree_skb_any(skb); 4906e909283SNimrod Andy return NETDEV_TX_OK; 4916e909283SNimrod Andy } 4926e909283SNimrod Andy 4936e909283SNimrod Andy /* Fill in a Tx ring entry */ 4947355f276STroy Kisky bdp = txq->bd.cur; 495c4bc44c6SKevin Hao last_bdp = bdp; 4965cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 4976e909283SNimrod Andy status &= ~BD_ENET_TX_STATS; 4986e909283SNimrod Andy 4996e909283SNimrod Andy /* Set buffer length and buffer pointer */ 5006e909283SNimrod Andy bufaddr = skb->data; 5016e909283SNimrod Andy buflen = skb_headlen(skb); 5026e909283SNimrod Andy 5037355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 50441ef84ceSFugang Duan if (((unsigned long) bufaddr) & fep->tx_align || 5056b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 5064d494cdcSFugang Duan memcpy(txq->tx_bounce[index], skb->data, buflen); 5074d494cdcSFugang Duan bufaddr = txq->tx_bounce[index]; 5086e909283SNimrod Andy 5096b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 5106e909283SNimrod Andy swap_buffer(bufaddr, buflen); 5116e909283SNimrod Andy } 5126e909283SNimrod Andy 513d6bf3143SRussell King /* Push the data cache so the CPM does not get stale memory data. */ 514d6bf3143SRussell King addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE); 515d6bf3143SRussell King if (dma_mapping_error(&fep->pdev->dev, addr)) { 5166e909283SNimrod Andy dev_kfree_skb_any(skb); 5176e909283SNimrod Andy if (net_ratelimit()) 5186e909283SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 5196e909283SNimrod Andy return NETDEV_TX_OK; 5206e909283SNimrod Andy } 5216e909283SNimrod Andy 5226e909283SNimrod Andy if (nr_frags) { 523c4bc44c6SKevin Hao last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev); 524fc75ba51STroy Kisky if (IS_ERR(last_bdp)) { 525fc75ba51STroy Kisky dma_unmap_single(&fep->pdev->dev, addr, 526fc75ba51STroy Kisky buflen, DMA_TO_DEVICE); 527fc75ba51STroy Kisky dev_kfree_skb_any(skb); 528c4bc44c6SKevin Hao return NETDEV_TX_OK; 529fc75ba51STroy Kisky } 5306e909283SNimrod Andy } else { 5316e909283SNimrod Andy status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 5326e909283SNimrod Andy if (fep->bufdesc_ex) { 5336e909283SNimrod Andy estatus = BD_ENET_TX_INT; 5346e909283SNimrod Andy if (unlikely(skb_shinfo(skb)->tx_flags & 5356e909283SNimrod Andy SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 5366e909283SNimrod Andy estatus |= BD_ENET_TX_TS; 5376e909283SNimrod Andy } 5386e909283SNimrod Andy } 539fc75ba51STroy Kisky bdp->cbd_bufaddr = cpu_to_fec32(addr); 540fc75ba51STroy Kisky bdp->cbd_datlen = cpu_to_fec16(buflen); 5416e909283SNimrod Andy 5426e909283SNimrod Andy if (fep->bufdesc_ex) { 5436e909283SNimrod Andy 5446e909283SNimrod Andy struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 5456e909283SNimrod Andy 5466e909283SNimrod Andy if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 5476e909283SNimrod Andy fep->hwts_tx_en)) 5486e909283SNimrod Andy skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 5496e909283SNimrod Andy 5506b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 55153bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 552befe8213SNimrod Andy 5536e909283SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 5546e909283SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 5556e909283SNimrod Andy 5566e909283SNimrod Andy ebdp->cbd_bdu = 0; 5575cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 5586e909283SNimrod Andy } 5596e909283SNimrod Andy 5607355f276STroy Kisky index = fec_enet_get_bd_index(last_bdp, &txq->bd); 5616e909283SNimrod Andy /* Save skb pointer */ 5624d494cdcSFugang Duan txq->tx_skbuff[index] = skb; 5636e909283SNimrod Andy 564be293467STroy Kisky /* Make sure the updates to rest of the descriptor are performed before 565be293467STroy Kisky * transferring ownership. 566be293467STroy Kisky */ 567be293467STroy Kisky wmb(); 5686e909283SNimrod Andy 5696e909283SNimrod Andy /* Send it on its way. Tell FEC it's ready, interrupt when done, 5706e909283SNimrod Andy * it's the last BD of the frame, and to put the CRC on the end. 5716e909283SNimrod Andy */ 5726e909283SNimrod Andy status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); 5735cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 5746e909283SNimrod Andy 575793fc096SFrank Li /* If this was the last BD in the ring, start at the beginning again. */ 5767355f276STroy Kisky bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd); 577793fc096SFrank Li 5787a2a8451SEric Dumazet skb_tx_timestamp(skb); 5797a2a8451SEric Dumazet 580c4bc44c6SKevin Hao /* Make sure the update to bdp and tx_skbuff are performed before 5817355f276STroy Kisky * txq->bd.cur. 582c4bc44c6SKevin Hao */ 583c4bc44c6SKevin Hao wmb(); 5847355f276STroy Kisky txq->bd.cur = bdp; 585793fc096SFrank Li 586793fc096SFrank Li /* Trigger transmission start */ 58753bb20d1STroy Kisky writel(0, txq->bd.reg_desc_active); 588793fc096SFrank Li 5896e909283SNimrod Andy return 0; 590793fc096SFrank Li } 591793fc096SFrank Li 59279f33912SNimrod Andy static int 5934d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb, 5944d494cdcSFugang Duan struct net_device *ndev, 59579f33912SNimrod Andy struct bufdesc *bdp, int index, char *data, 59679f33912SNimrod Andy int size, bool last_tcp, bool is_last) 59779f33912SNimrod Andy { 59879f33912SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 59961cd2ebbSFabian Frederick struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 60079f33912SNimrod Andy unsigned short status; 60179f33912SNimrod Andy unsigned int estatus = 0; 602d6bf3143SRussell King dma_addr_t addr; 60379f33912SNimrod Andy 6045cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 60579f33912SNimrod Andy status &= ~BD_ENET_TX_STATS; 60679f33912SNimrod Andy 60779f33912SNimrod Andy status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 60879f33912SNimrod Andy 60941ef84ceSFugang Duan if (((unsigned long) data) & fep->tx_align || 6106b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 6114d494cdcSFugang Duan memcpy(txq->tx_bounce[index], data, size); 6124d494cdcSFugang Duan data = txq->tx_bounce[index]; 61379f33912SNimrod Andy 6146b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 61579f33912SNimrod Andy swap_buffer(data, size); 61679f33912SNimrod Andy } 61779f33912SNimrod Andy 618d6bf3143SRussell King addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE); 619d6bf3143SRussell King if (dma_mapping_error(&fep->pdev->dev, addr)) { 62079f33912SNimrod Andy dev_kfree_skb_any(skb); 62179f33912SNimrod Andy if (net_ratelimit()) 62279f33912SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 62379f33912SNimrod Andy return NETDEV_TX_BUSY; 62479f33912SNimrod Andy } 62579f33912SNimrod Andy 6265cfa3039SJohannes Berg bdp->cbd_datlen = cpu_to_fec16(size); 6275cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(addr); 628d6bf3143SRussell King 62979f33912SNimrod Andy if (fep->bufdesc_ex) { 6306b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 63153bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 63279f33912SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 63379f33912SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 63479f33912SNimrod Andy ebdp->cbd_bdu = 0; 6355cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 63679f33912SNimrod Andy } 63779f33912SNimrod Andy 63879f33912SNimrod Andy /* Handle the last BD specially */ 63979f33912SNimrod Andy if (last_tcp) 64079f33912SNimrod Andy status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC); 64179f33912SNimrod Andy if (is_last) { 64279f33912SNimrod Andy status |= BD_ENET_TX_INTR; 64379f33912SNimrod Andy if (fep->bufdesc_ex) 6445cfa3039SJohannes Berg ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT); 64579f33912SNimrod Andy } 64679f33912SNimrod Andy 6475cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 64879f33912SNimrod Andy 64979f33912SNimrod Andy return 0; 65079f33912SNimrod Andy } 65179f33912SNimrod Andy 65279f33912SNimrod Andy static int 6534d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq, 6544d494cdcSFugang Duan struct sk_buff *skb, struct net_device *ndev, 65579f33912SNimrod Andy struct bufdesc *bdp, int index) 65679f33912SNimrod Andy { 65779f33912SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 65879f33912SNimrod Andy int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 65961cd2ebbSFabian Frederick struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 66079f33912SNimrod Andy void *bufaddr; 66179f33912SNimrod Andy unsigned long dmabuf; 66279f33912SNimrod Andy unsigned short status; 66379f33912SNimrod Andy unsigned int estatus = 0; 66479f33912SNimrod Andy 6655cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 66679f33912SNimrod Andy status &= ~BD_ENET_TX_STATS; 66779f33912SNimrod Andy status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 66879f33912SNimrod Andy 6694d494cdcSFugang Duan bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 6704d494cdcSFugang Duan dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE; 67141ef84ceSFugang Duan if (((unsigned long)bufaddr) & fep->tx_align || 6726b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 6734d494cdcSFugang Duan memcpy(txq->tx_bounce[index], skb->data, hdr_len); 6744d494cdcSFugang Duan bufaddr = txq->tx_bounce[index]; 67579f33912SNimrod Andy 6766b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 67779f33912SNimrod Andy swap_buffer(bufaddr, hdr_len); 67879f33912SNimrod Andy 67979f33912SNimrod Andy dmabuf = dma_map_single(&fep->pdev->dev, bufaddr, 68079f33912SNimrod Andy hdr_len, DMA_TO_DEVICE); 68179f33912SNimrod Andy if (dma_mapping_error(&fep->pdev->dev, dmabuf)) { 68279f33912SNimrod Andy dev_kfree_skb_any(skb); 68379f33912SNimrod Andy if (net_ratelimit()) 68479f33912SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 68579f33912SNimrod Andy return NETDEV_TX_BUSY; 68679f33912SNimrod Andy } 68779f33912SNimrod Andy } 68879f33912SNimrod Andy 6895cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(dmabuf); 6905cfa3039SJohannes Berg bdp->cbd_datlen = cpu_to_fec16(hdr_len); 69179f33912SNimrod Andy 69279f33912SNimrod Andy if (fep->bufdesc_ex) { 6936b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 69453bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 69579f33912SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 69679f33912SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 69779f33912SNimrod Andy ebdp->cbd_bdu = 0; 6985cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 69979f33912SNimrod Andy } 70079f33912SNimrod Andy 7015cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 70279f33912SNimrod Andy 70379f33912SNimrod Andy return 0; 70479f33912SNimrod Andy } 70579f33912SNimrod Andy 7064d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq, 7074d494cdcSFugang Duan struct sk_buff *skb, 7084d494cdcSFugang Duan struct net_device *ndev) 70979f33912SNimrod Andy { 71079f33912SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 711761b331cSEric Dumazet int hdr_len, total_len, data_left; 7127355f276STroy Kisky struct bufdesc *bdp = txq->bd.cur; 71379f33912SNimrod Andy struct tso_t tso; 71479f33912SNimrod Andy unsigned int index = 0; 71579f33912SNimrod Andy int ret; 71679f33912SNimrod Andy 7177355f276STroy Kisky if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) { 71879f33912SNimrod Andy dev_kfree_skb_any(skb); 71979f33912SNimrod Andy if (net_ratelimit()) 72079f33912SNimrod Andy netdev_err(ndev, "NOT enough BD for TSO!\n"); 72179f33912SNimrod Andy return NETDEV_TX_OK; 72279f33912SNimrod Andy } 72379f33912SNimrod Andy 72479f33912SNimrod Andy /* Protocol checksum off-load for TCP and UDP. */ 72579f33912SNimrod Andy if (fec_enet_clear_csum(skb, ndev)) { 72679f33912SNimrod Andy dev_kfree_skb_any(skb); 72779f33912SNimrod Andy return NETDEV_TX_OK; 72879f33912SNimrod Andy } 72979f33912SNimrod Andy 73079f33912SNimrod Andy /* Initialize the TSO handler, and prepare the first payload */ 731761b331cSEric Dumazet hdr_len = tso_start(skb, &tso); 73279f33912SNimrod Andy 73379f33912SNimrod Andy total_len = skb->len - hdr_len; 73479f33912SNimrod Andy while (total_len > 0) { 73579f33912SNimrod Andy char *hdr; 73679f33912SNimrod Andy 7377355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 73879f33912SNimrod Andy data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 73979f33912SNimrod Andy total_len -= data_left; 74079f33912SNimrod Andy 74179f33912SNimrod Andy /* prepare packet headers: MAC + IP + TCP */ 7424d494cdcSFugang Duan hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 74379f33912SNimrod Andy tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 7444d494cdcSFugang Duan ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index); 74579f33912SNimrod Andy if (ret) 74679f33912SNimrod Andy goto err_release; 74779f33912SNimrod Andy 74879f33912SNimrod Andy while (data_left > 0) { 74979f33912SNimrod Andy int size; 75079f33912SNimrod Andy 75179f33912SNimrod Andy size = min_t(int, tso.size, data_left); 7527355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 7537355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 7544d494cdcSFugang Duan ret = fec_enet_txq_put_data_tso(txq, skb, ndev, 7554d494cdcSFugang Duan bdp, index, 7564d494cdcSFugang Duan tso.data, size, 7574d494cdcSFugang Duan size == data_left, 75879f33912SNimrod Andy total_len == 0); 75979f33912SNimrod Andy if (ret) 76079f33912SNimrod Andy goto err_release; 76179f33912SNimrod Andy 76279f33912SNimrod Andy data_left -= size; 76379f33912SNimrod Andy tso_build_data(skb, &tso, size); 76479f33912SNimrod Andy } 76579f33912SNimrod Andy 7667355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 76779f33912SNimrod Andy } 76879f33912SNimrod Andy 76979f33912SNimrod Andy /* Save skb pointer */ 7704d494cdcSFugang Duan txq->tx_skbuff[index] = skb; 77179f33912SNimrod Andy 77279f33912SNimrod Andy skb_tx_timestamp(skb); 7737355f276STroy Kisky txq->bd.cur = bdp; 77479f33912SNimrod Andy 77579f33912SNimrod Andy /* Trigger transmission start */ 7766b7e4008SLothar Waßmann if (!(fep->quirks & FEC_QUIRK_ERR007885) || 77753bb20d1STroy Kisky !readl(txq->bd.reg_desc_active) || 77853bb20d1STroy Kisky !readl(txq->bd.reg_desc_active) || 77953bb20d1STroy Kisky !readl(txq->bd.reg_desc_active) || 78053bb20d1STroy Kisky !readl(txq->bd.reg_desc_active)) 78153bb20d1STroy Kisky writel(0, txq->bd.reg_desc_active); 78279f33912SNimrod Andy 78379f33912SNimrod Andy return 0; 78479f33912SNimrod Andy 78579f33912SNimrod Andy err_release: 78679f33912SNimrod Andy /* TODO: Release all used data descriptors for TSO */ 78779f33912SNimrod Andy return ret; 78879f33912SNimrod Andy } 78979f33912SNimrod Andy 79061a4427bSNimrod Andy static netdev_tx_t 79161a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) 79261a4427bSNimrod Andy { 79361a4427bSNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 7946e909283SNimrod Andy int entries_free; 7954d494cdcSFugang Duan unsigned short queue; 7964d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 7974d494cdcSFugang Duan struct netdev_queue *nq; 79861a4427bSNimrod Andy int ret; 79961a4427bSNimrod Andy 8004d494cdcSFugang Duan queue = skb_get_queue_mapping(skb); 8014d494cdcSFugang Duan txq = fep->tx_queue[queue]; 8024d494cdcSFugang Duan nq = netdev_get_tx_queue(ndev, queue); 8034d494cdcSFugang Duan 80479f33912SNimrod Andy if (skb_is_gso(skb)) 8054d494cdcSFugang Duan ret = fec_enet_txq_submit_tso(txq, skb, ndev); 80679f33912SNimrod Andy else 8074d494cdcSFugang Duan ret = fec_enet_txq_submit_skb(txq, skb, ndev); 8086e909283SNimrod Andy if (ret) 8096e909283SNimrod Andy return ret; 81061a4427bSNimrod Andy 8117355f276STroy Kisky entries_free = fec_enet_get_free_txdesc_num(txq); 8124d494cdcSFugang Duan if (entries_free <= txq->tx_stop_threshold) 8134d494cdcSFugang Duan netif_tx_stop_queue(nq); 81461a4427bSNimrod Andy 81561a4427bSNimrod Andy return NETDEV_TX_OK; 81661a4427bSNimrod Andy } 81761a4427bSNimrod Andy 818a210576cSDavid S. Miller /* Init RX & TX buffer descriptors 819a210576cSDavid S. Miller */ 820a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev) 821a210576cSDavid S. Miller { 822a210576cSDavid S. Miller struct fec_enet_private *fep = netdev_priv(dev); 8234d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 8244d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 825a210576cSDavid S. Miller struct bufdesc *bdp; 826a210576cSDavid S. Miller unsigned int i; 82759d0f746SFrank Li unsigned int q; 828a210576cSDavid S. Miller 82959d0f746SFrank Li for (q = 0; q < fep->num_rx_queues; q++) { 830a210576cSDavid S. Miller /* Initialize the receive buffer descriptors. */ 83159d0f746SFrank Li rxq = fep->rx_queue[q]; 8327355f276STroy Kisky bdp = rxq->bd.base; 8334d494cdcSFugang Duan 8347355f276STroy Kisky for (i = 0; i < rxq->bd.ring_size; i++) { 835a210576cSDavid S. Miller 836a210576cSDavid S. Miller /* Initialize the BD for every fragment in the page. */ 837a210576cSDavid S. Miller if (bdp->cbd_bufaddr) 8385cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 839a210576cSDavid S. Miller else 8405cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(0); 8417355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 842a210576cSDavid S. Miller } 843a210576cSDavid S. Miller 844a210576cSDavid S. Miller /* Set the last buffer to wrap */ 8457355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 8465cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 847a210576cSDavid S. Miller 8487355f276STroy Kisky rxq->bd.cur = rxq->bd.base; 84959d0f746SFrank Li } 850a210576cSDavid S. Miller 85159d0f746SFrank Li for (q = 0; q < fep->num_tx_queues; q++) { 852a210576cSDavid S. Miller /* ...and the same for transmit */ 85359d0f746SFrank Li txq = fep->tx_queue[q]; 8547355f276STroy Kisky bdp = txq->bd.base; 8557355f276STroy Kisky txq->bd.cur = bdp; 856a210576cSDavid S. Miller 8577355f276STroy Kisky for (i = 0; i < txq->bd.ring_size; i++) { 858a210576cSDavid S. Miller /* Initialize the BD for every fragment in the page. */ 8595cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(0); 860178e5f57SFugang Duan if (bdp->cbd_bufaddr && 861178e5f57SFugang Duan !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 862178e5f57SFugang Duan dma_unmap_single(&fep->pdev->dev, 863178e5f57SFugang Duan fec32_to_cpu(bdp->cbd_bufaddr), 864178e5f57SFugang Duan fec16_to_cpu(bdp->cbd_datlen), 865178e5f57SFugang Duan DMA_TO_DEVICE); 8664d494cdcSFugang Duan if (txq->tx_skbuff[i]) { 8674d494cdcSFugang Duan dev_kfree_skb_any(txq->tx_skbuff[i]); 8684d494cdcSFugang Duan txq->tx_skbuff[i] = NULL; 869a210576cSDavid S. Miller } 8705cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(0); 8717355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 872a210576cSDavid S. Miller } 873a210576cSDavid S. Miller 874a210576cSDavid S. Miller /* Set the last buffer to wrap */ 8757355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 8765cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 8774d494cdcSFugang Duan txq->dirty_tx = bdp; 878a210576cSDavid S. Miller } 87959d0f746SFrank Li } 88059d0f746SFrank Li 881ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev) 882ce99d0d3SFrank Li { 883ce99d0d3SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 884ce99d0d3SFrank Li int i; 885ce99d0d3SFrank Li 886ce99d0d3SFrank Li for (i = 0; i < fep->num_rx_queues; i++) 88753bb20d1STroy Kisky writel(0, fep->rx_queue[i]->bd.reg_desc_active); 888ce99d0d3SFrank Li } 889ce99d0d3SFrank Li 89059d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev) 89159d0f746SFrank Li { 89259d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 89359d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 89459d0f746SFrank Li struct fec_enet_priv_rx_q *rxq; 89559d0f746SFrank Li int i; 89659d0f746SFrank Li 89759d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) { 89859d0f746SFrank Li rxq = fep->rx_queue[i]; 8997355f276STroy Kisky writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i)); 900fbbeefddSAndrew Lunn writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i)); 90159d0f746SFrank Li 90259d0f746SFrank Li /* enable DMA1/2 */ 90359d0f746SFrank Li if (i) 90459d0f746SFrank Li writel(RCMR_MATCHEN | RCMR_CMP(i), 90559d0f746SFrank Li fep->hwp + FEC_RCMR(i)); 90659d0f746SFrank Li } 90759d0f746SFrank Li 90859d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 90959d0f746SFrank Li txq = fep->tx_queue[i]; 9107355f276STroy Kisky writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i)); 91159d0f746SFrank Li 91259d0f746SFrank Li /* enable DMA1/2 */ 91359d0f746SFrank Li if (i) 91459d0f746SFrank Li writel(DMA_CLASS_EN | IDLE_SLOPE(i), 91559d0f746SFrank Li fep->hwp + FEC_DMA_CFG(i)); 91659d0f746SFrank Li } 91759d0f746SFrank Li } 91859d0f746SFrank Li 91959d0f746SFrank Li static void fec_enet_reset_skb(struct net_device *ndev) 92059d0f746SFrank Li { 92159d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 92259d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 92359d0f746SFrank Li int i, j; 92459d0f746SFrank Li 92559d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 92659d0f746SFrank Li txq = fep->tx_queue[i]; 92759d0f746SFrank Li 9287355f276STroy Kisky for (j = 0; j < txq->bd.ring_size; j++) { 92959d0f746SFrank Li if (txq->tx_skbuff[j]) { 93059d0f746SFrank Li dev_kfree_skb_any(txq->tx_skbuff[j]); 93159d0f746SFrank Li txq->tx_skbuff[j] = NULL; 93259d0f746SFrank Li } 93359d0f746SFrank Li } 93459d0f746SFrank Li } 93559d0f746SFrank Li } 936a210576cSDavid S. Miller 937dbc64a8eSRussell King /* 938dbc64a8eSRussell King * This function is called to start or restart the FEC during a link 939dbc64a8eSRussell King * change, transmit timeout, or to reconfigure the FEC. The network 940dbc64a8eSRussell King * packet processing for this device must be stopped before this call. 941793fc096SFrank Li */ 942793fc096SFrank Li static void 943ef83337dSRussell King fec_restart(struct net_device *ndev) 944793fc096SFrank Li { 945793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 9464c09eed9SJim Baxter u32 val; 947793fc096SFrank Li u32 temp_mac[2]; 948793fc096SFrank Li u32 rcntl = OPT_FRAME_SIZE | 0x04; 949793fc096SFrank Li u32 ecntl = 0x2; /* ETHEREN */ 950793fc096SFrank Li 951106c314cSFugang Duan /* Whack a reset. We should wait for this. 952106c314cSFugang Duan * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 953106c314cSFugang Duan * instead of reset MAC itself. 954106c314cSFugang Duan */ 9556b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) { 956106c314cSFugang Duan writel(0, fep->hwp + FEC_ECNTRL); 957106c314cSFugang Duan } else { 958793fc096SFrank Li writel(1, fep->hwp + FEC_ECNTRL); 959793fc096SFrank Li udelay(10); 960106c314cSFugang Duan } 961793fc096SFrank Li 962793fc096SFrank Li /* 963793fc096SFrank Li * enet-mac reset will reset mac address registers too, 964793fc096SFrank Li * so need to reconfigure it. 965793fc096SFrank Li */ 966793fc096SFrank Li memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); 9675cfa3039SJohannes Berg writel((__force u32)cpu_to_be32(temp_mac[0]), 9685cfa3039SJohannes Berg fep->hwp + FEC_ADDR_LOW); 9695cfa3039SJohannes Berg writel((__force u32)cpu_to_be32(temp_mac[1]), 9705cfa3039SJohannes Berg fep->hwp + FEC_ADDR_HIGH); 971793fc096SFrank Li 972f166f890SAndrew Lunn /* Clear any outstanding interrupt, except MDIO. */ 973f166f890SAndrew Lunn writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT); 974793fc096SFrank Li 975a210576cSDavid S. Miller fec_enet_bd_init(ndev); 976a210576cSDavid S. Miller 97759d0f746SFrank Li fec_enet_enable_ring(ndev); 978793fc096SFrank Li 97959d0f746SFrank Li /* Reset tx SKB buffers. */ 98059d0f746SFrank Li fec_enet_reset_skb(ndev); 981793fc096SFrank Li 982793fc096SFrank Li /* Enable MII mode */ 983ef83337dSRussell King if (fep->full_duplex == DUPLEX_FULL) { 984793fc096SFrank Li /* FD enable */ 985793fc096SFrank Li writel(0x04, fep->hwp + FEC_X_CNTRL); 986793fc096SFrank Li } else { 987793fc096SFrank Li /* No Rcv on Xmit */ 988793fc096SFrank Li rcntl |= 0x02; 989793fc096SFrank Li writel(0x0, fep->hwp + FEC_X_CNTRL); 990793fc096SFrank Li } 991793fc096SFrank Li 992793fc096SFrank Li /* Set MII speed */ 993793fc096SFrank Li writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 994793fc096SFrank Li 995d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 99618803495SGreg Ungerer if (fep->quirks & FEC_QUIRK_HAS_RACC) { 9974c09eed9SJim Baxter val = readl(fep->hwp + FEC_RACC); 9983ac72b7bSEric Nelson /* align IP header */ 9993ac72b7bSEric Nelson val |= FEC_RACC_SHIFT16; 10004c09eed9SJim Baxter if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) 10013ac72b7bSEric Nelson /* set RX checksum */ 10024c09eed9SJim Baxter val |= FEC_RACC_OPTIONS; 10034c09eed9SJim Baxter else 10044c09eed9SJim Baxter val &= ~FEC_RACC_OPTIONS; 10054c09eed9SJim Baxter writel(val, fep->hwp + FEC_RACC); 100655cd48c8STroy Kisky writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL); 100732867fccSFabio Estevam } 1008d1391930SGuenter Roeck #endif 10094c09eed9SJim Baxter 1010793fc096SFrank Li /* 1011793fc096SFrank Li * The phy interface and speed need to get configured 1012793fc096SFrank Li * differently on enet-mac. 1013793fc096SFrank Li */ 10146b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1015793fc096SFrank Li /* Enable flow control and length check */ 1016793fc096SFrank Li rcntl |= 0x40000000 | 0x00000020; 1017793fc096SFrank Li 1018793fc096SFrank Li /* RGMII, RMII or MII */ 1019e813bb2bSMarkus Pargmann if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII || 1020e813bb2bSMarkus Pargmann fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID || 1021e813bb2bSMarkus Pargmann fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID || 1022e813bb2bSMarkus Pargmann fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) 1023793fc096SFrank Li rcntl |= (1 << 6); 1024793fc096SFrank Li else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1025793fc096SFrank Li rcntl |= (1 << 8); 1026793fc096SFrank Li else 1027793fc096SFrank Li rcntl &= ~(1 << 8); 1028793fc096SFrank Li 1029793fc096SFrank Li /* 1G, 100M or 10M */ 103045f5c327SPhilippe Reynes if (ndev->phydev) { 103145f5c327SPhilippe Reynes if (ndev->phydev->speed == SPEED_1000) 1032793fc096SFrank Li ecntl |= (1 << 5); 103345f5c327SPhilippe Reynes else if (ndev->phydev->speed == SPEED_100) 1034793fc096SFrank Li rcntl &= ~(1 << 9); 1035793fc096SFrank Li else 1036793fc096SFrank Li rcntl |= (1 << 9); 1037793fc096SFrank Li } 1038793fc096SFrank Li } else { 1039793fc096SFrank Li #ifdef FEC_MIIGSK_ENR 10406b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_USE_GASKET) { 1041793fc096SFrank Li u32 cfgr; 1042793fc096SFrank Li /* disable the gasket and wait */ 1043793fc096SFrank Li writel(0, fep->hwp + FEC_MIIGSK_ENR); 1044793fc096SFrank Li while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) 1045793fc096SFrank Li udelay(1); 1046793fc096SFrank Li 1047793fc096SFrank Li /* 1048793fc096SFrank Li * configure the gasket: 1049793fc096SFrank Li * RMII, 50 MHz, no loopback, no echo 1050793fc096SFrank Li * MII, 25 MHz, no loopback, no echo 1051793fc096SFrank Li */ 1052793fc096SFrank Li cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1053793fc096SFrank Li ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; 105445f5c327SPhilippe Reynes if (ndev->phydev && ndev->phydev->speed == SPEED_10) 1055793fc096SFrank Li cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; 1056793fc096SFrank Li writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); 1057793fc096SFrank Li 1058793fc096SFrank Li /* re-enable the gasket */ 1059793fc096SFrank Li writel(2, fep->hwp + FEC_MIIGSK_ENR); 1060793fc096SFrank Li } 1061793fc096SFrank Li #endif 1062793fc096SFrank Li } 1063793fc096SFrank Li 1064d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 1065793fc096SFrank Li /* enable pause frame*/ 1066793fc096SFrank Li if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || 1067793fc096SFrank Li ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && 106845f5c327SPhilippe Reynes ndev->phydev && ndev->phydev->pause)) { 1069793fc096SFrank Li rcntl |= FEC_ENET_FCE; 1070793fc096SFrank Li 10714c09eed9SJim Baxter /* set FIFO threshold parameter to reduce overrun */ 1072793fc096SFrank Li writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); 1073793fc096SFrank Li writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); 1074793fc096SFrank Li writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); 1075793fc096SFrank Li writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); 1076793fc096SFrank Li 1077793fc096SFrank Li /* OPD */ 1078793fc096SFrank Li writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); 1079793fc096SFrank Li } else { 1080793fc096SFrank Li rcntl &= ~FEC_ENET_FCE; 1081793fc096SFrank Li } 1082d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */ 1083793fc096SFrank Li 1084793fc096SFrank Li writel(rcntl, fep->hwp + FEC_R_CNTRL); 1085793fc096SFrank Li 108684fe6182SStefan Wahren /* Setup multicast filter. */ 108784fe6182SStefan Wahren set_multicast_list(ndev); 108884fe6182SStefan Wahren #ifndef CONFIG_M5272 108984fe6182SStefan Wahren writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); 109084fe6182SStefan Wahren writel(0, fep->hwp + FEC_HASH_TABLE_LOW); 109184fe6182SStefan Wahren #endif 109284fe6182SStefan Wahren 10936b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1094793fc096SFrank Li /* enable ENET endian swap */ 1095793fc096SFrank Li ecntl |= (1 << 8); 1096793fc096SFrank Li /* enable ENET store and forward mode */ 1097793fc096SFrank Li writel(1 << 8, fep->hwp + FEC_X_WMRK); 1098793fc096SFrank Li } 1099793fc096SFrank Li 1100793fc096SFrank Li if (fep->bufdesc_ex) 1101793fc096SFrank Li ecntl |= (1 << 4); 1102793fc096SFrank Li 110338ae92dcSChris Healy #ifndef CONFIG_M5272 1104b9eef55cSJim Baxter /* Enable the MIB statistic event counters */ 1105b9eef55cSJim Baxter writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT); 110638ae92dcSChris Healy #endif 110738ae92dcSChris Healy 1108793fc096SFrank Li /* And last, enable the transmit and receive processing */ 1109793fc096SFrank Li writel(ecntl, fep->hwp + FEC_ECNTRL); 1110ce99d0d3SFrank Li fec_enet_active_rxring(ndev); 1111793fc096SFrank Li 1112793fc096SFrank Li if (fep->bufdesc_ex) 1113793fc096SFrank Li fec_ptp_start_cyclecounter(ndev); 1114793fc096SFrank Li 1115793fc096SFrank Li /* Enable interrupts we wish to service */ 11160c5a3aefSNimrod Andy if (fep->link) 1117793fc096SFrank Li writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 11180c5a3aefSNimrod Andy else 1119f166f890SAndrew Lunn writel(0, fep->hwp + FEC_IMASK); 1120d851b47bSFugang Duan 1121d851b47bSFugang Duan /* Init the interrupt coalescing */ 1122d851b47bSFugang Duan fec_enet_itr_coal_init(ndev); 1123d851b47bSFugang Duan 1124793fc096SFrank Li } 1125793fc096SFrank Li 1126da722186SMartin Fuzzey static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled) 1127da722186SMartin Fuzzey { 1128da722186SMartin Fuzzey struct fec_platform_data *pdata = fep->pdev->dev.platform_data; 1129da722186SMartin Fuzzey struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr; 1130da722186SMartin Fuzzey 1131da722186SMartin Fuzzey if (stop_gpr->gpr) { 1132da722186SMartin Fuzzey if (enabled) 1133da722186SMartin Fuzzey regmap_update_bits(stop_gpr->gpr, stop_gpr->reg, 1134da722186SMartin Fuzzey BIT(stop_gpr->bit), 1135da722186SMartin Fuzzey BIT(stop_gpr->bit)); 1136da722186SMartin Fuzzey else 1137da722186SMartin Fuzzey regmap_update_bits(stop_gpr->gpr, stop_gpr->reg, 1138da722186SMartin Fuzzey BIT(stop_gpr->bit), 0); 1139da722186SMartin Fuzzey } else if (pdata && pdata->sleep_mode_enable) { 1140da722186SMartin Fuzzey pdata->sleep_mode_enable(enabled); 1141da722186SMartin Fuzzey } 1142da722186SMartin Fuzzey } 1143da722186SMartin Fuzzey 1144793fc096SFrank Li static void 1145793fc096SFrank Li fec_stop(struct net_device *ndev) 1146793fc096SFrank Li { 1147793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1148793fc096SFrank Li u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8); 1149de40ed31SNimrod Andy u32 val; 1150793fc096SFrank Li 1151793fc096SFrank Li /* We cannot expect a graceful transmit stop without link !!! */ 1152793fc096SFrank Li if (fep->link) { 1153793fc096SFrank Li writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ 1154793fc096SFrank Li udelay(10); 1155793fc096SFrank Li if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) 115631b7720cSJoe Perches netdev_err(ndev, "Graceful transmit stop did not complete!\n"); 1157793fc096SFrank Li } 1158793fc096SFrank Li 1159106c314cSFugang Duan /* Whack a reset. We should wait for this. 1160106c314cSFugang Duan * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1161106c314cSFugang Duan * instead of reset MAC itself. 1162106c314cSFugang Duan */ 1163de40ed31SNimrod Andy if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 11646b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) { 1165106c314cSFugang Duan writel(0, fep->hwp + FEC_ECNTRL); 1166106c314cSFugang Duan } else { 1167793fc096SFrank Li writel(1, fep->hwp + FEC_ECNTRL); 1168793fc096SFrank Li udelay(10); 1169106c314cSFugang Duan } 1170793fc096SFrank Li writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1171de40ed31SNimrod Andy } else { 1172de40ed31SNimrod Andy writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK); 1173de40ed31SNimrod Andy val = readl(fep->hwp + FEC_ECNTRL); 1174de40ed31SNimrod Andy val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 1175de40ed31SNimrod Andy writel(val, fep->hwp + FEC_ECNTRL); 1176da722186SMartin Fuzzey fec_enet_stop_mode(fep, true); 1177de40ed31SNimrod Andy } 1178de40ed31SNimrod Andy writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1179793fc096SFrank Li 1180793fc096SFrank Li /* We have to keep ENET enabled to have MII interrupt stay working */ 1181de40ed31SNimrod Andy if (fep->quirks & FEC_QUIRK_ENET_MAC && 1182de40ed31SNimrod Andy !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 1183793fc096SFrank Li writel(2, fep->hwp + FEC_ECNTRL); 1184793fc096SFrank Li writel(rmii_mode, fep->hwp + FEC_R_CNTRL); 1185793fc096SFrank Li } 1186793fc096SFrank Li } 1187793fc096SFrank Li 1188793fc096SFrank Li 1189793fc096SFrank Li static void 11900290bd29SMichael S. Tsirkin fec_timeout(struct net_device *ndev, unsigned int txqueue) 1191793fc096SFrank Li { 1192793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1193793fc096SFrank Li 1194344756f6SRussell King fec_dump(ndev); 1195344756f6SRussell King 1196793fc096SFrank Li ndev->stats.tx_errors++; 1197793fc096SFrank Li 119836cdc743SRussell King schedule_work(&fep->tx_timeout_work); 119954309fa6SFrank Li } 120054309fa6SFrank Li 120136cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work) 120254309fa6SFrank Li { 120354309fa6SFrank Li struct fec_enet_private *fep = 120436cdc743SRussell King container_of(work, struct fec_enet_private, tx_timeout_work); 12058ce5624fSRussell King struct net_device *ndev = fep->netdev; 120654309fa6SFrank Li 1207da1774e5SRussell King rtnl_lock(); 12088ce5624fSRussell King if (netif_device_present(ndev) || netif_running(ndev)) { 1209dbc64a8eSRussell King napi_disable(&fep->napi); 1210dbc64a8eSRussell King netif_tx_lock_bh(ndev); 1211ef83337dSRussell King fec_restart(ndev); 1212657ade07SRickard x Andersson netif_tx_wake_all_queues(ndev); 12136af42d42SRussell King netif_tx_unlock_bh(ndev); 1214dbc64a8eSRussell King napi_enable(&fep->napi); 12158ce5624fSRussell King } 1216da1774e5SRussell King rtnl_unlock(); 121754309fa6SFrank Li } 1218793fc096SFrank Li 1219793fc096SFrank Li static void 1220bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts, 1221bfd4ecddSRussell King struct skb_shared_hwtstamps *hwtstamps) 1222bfd4ecddSRussell King { 1223bfd4ecddSRussell King unsigned long flags; 1224bfd4ecddSRussell King u64 ns; 1225bfd4ecddSRussell King 1226bfd4ecddSRussell King spin_lock_irqsave(&fep->tmreg_lock, flags); 1227bfd4ecddSRussell King ns = timecounter_cyc2time(&fep->tc, ts); 1228bfd4ecddSRussell King spin_unlock_irqrestore(&fep->tmreg_lock, flags); 1229bfd4ecddSRussell King 1230bfd4ecddSRussell King memset(hwtstamps, 0, sizeof(*hwtstamps)); 1231bfd4ecddSRussell King hwtstamps->hwtstamp = ns_to_ktime(ns); 1232bfd4ecddSRussell King } 1233bfd4ecddSRussell King 1234bfd4ecddSRussell King static void 12354d494cdcSFugang Duan fec_enet_tx_queue(struct net_device *ndev, u16 queue_id) 1236793fc096SFrank Li { 1237793fc096SFrank Li struct fec_enet_private *fep; 1238a2fe37b6SFabio Estevam struct bufdesc *bdp; 1239793fc096SFrank Li unsigned short status; 1240793fc096SFrank Li struct sk_buff *skb; 12414d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 12424d494cdcSFugang Duan struct netdev_queue *nq; 1243793fc096SFrank Li int index = 0; 124479f33912SNimrod Andy int entries_free; 1245793fc096SFrank Li 1246793fc096SFrank Li fep = netdev_priv(ndev); 12474d494cdcSFugang Duan 12484d494cdcSFugang Duan txq = fep->tx_queue[queue_id]; 12494d494cdcSFugang Duan /* get next bdp of dirty_tx */ 12504d494cdcSFugang Duan nq = netdev_get_tx_queue(ndev, queue_id); 12514d494cdcSFugang Duan bdp = txq->dirty_tx; 1252793fc096SFrank Li 1253793fc096SFrank Li /* get next bdp of dirty_tx */ 12547355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1255793fc096SFrank Li 12567355f276STroy Kisky while (bdp != READ_ONCE(txq->bd.cur)) { 12577355f276STroy Kisky /* Order the load of bd.cur and cbd_sc */ 1258c4bc44c6SKevin Hao rmb(); 12595cfa3039SJohannes Berg status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc)); 1260c4bc44c6SKevin Hao if (status & BD_ENET_TX_READY) 1261793fc096SFrank Li break; 1262793fc096SFrank Li 12637355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 12642b995f63SNimrod Andy 1265a2fe37b6SFabio Estevam skb = txq->tx_skbuff[index]; 1266a2fe37b6SFabio Estevam txq->tx_skbuff[index] = NULL; 12675cfa3039SJohannes Berg if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 12685cfa3039SJohannes Berg dma_unmap_single(&fep->pdev->dev, 12695cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 12705cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_datlen), 12715cfa3039SJohannes Berg DMA_TO_DEVICE); 12725cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(0); 12737fafe803STroy Kisky if (!skb) 12747fafe803STroy Kisky goto skb_done; 1275793fc096SFrank Li 1276793fc096SFrank Li /* Check for errors. */ 1277793fc096SFrank Li if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | 1278793fc096SFrank Li BD_ENET_TX_RL | BD_ENET_TX_UN | 1279793fc096SFrank Li BD_ENET_TX_CSL)) { 1280793fc096SFrank Li ndev->stats.tx_errors++; 1281793fc096SFrank Li if (status & BD_ENET_TX_HB) /* No heartbeat */ 1282793fc096SFrank Li ndev->stats.tx_heartbeat_errors++; 1283793fc096SFrank Li if (status & BD_ENET_TX_LC) /* Late collision */ 1284793fc096SFrank Li ndev->stats.tx_window_errors++; 1285793fc096SFrank Li if (status & BD_ENET_TX_RL) /* Retrans limit */ 1286793fc096SFrank Li ndev->stats.tx_aborted_errors++; 1287793fc096SFrank Li if (status & BD_ENET_TX_UN) /* Underrun */ 1288793fc096SFrank Li ndev->stats.tx_fifo_errors++; 1289793fc096SFrank Li if (status & BD_ENET_TX_CSL) /* Carrier lost */ 1290793fc096SFrank Li ndev->stats.tx_carrier_errors++; 1291793fc096SFrank Li } else { 1292793fc096SFrank Li ndev->stats.tx_packets++; 12936e909283SNimrod Andy ndev->stats.tx_bytes += skb->len; 1294793fc096SFrank Li } 1295793fc096SFrank Li 129634074639SSergey Organov /* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who 129734074639SSergey Organov * are to time stamp the packet, so we still need to check time 129834074639SSergey Organov * stamping enabled flag. 129934074639SSergey Organov */ 130034074639SSergey Organov if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS && 130134074639SSergey Organov fep->hwts_tx_en) && 1302793fc096SFrank Li fep->bufdesc_ex) { 1303793fc096SFrank Li struct skb_shared_hwtstamps shhwtstamps; 1304793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1305793fc096SFrank Li 13065cfa3039SJohannes Berg fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps); 1307793fc096SFrank Li skb_tstamp_tx(skb, &shhwtstamps); 1308793fc096SFrank Li } 1309793fc096SFrank Li 1310793fc096SFrank Li /* Deferred means some collisions occurred during transmit, 1311793fc096SFrank Li * but we eventually sent the packet OK. 1312793fc096SFrank Li */ 1313793fc096SFrank Li if (status & BD_ENET_TX_DEF) 1314793fc096SFrank Li ndev->stats.collisions++; 1315793fc096SFrank Li 1316793fc096SFrank Li /* Free the sk buffer associated with this last transmit */ 1317793fc096SFrank Li dev_kfree_skb_any(skb); 13187fafe803STroy Kisky skb_done: 1319c4bc44c6SKevin Hao /* Make sure the update to bdp and tx_skbuff are performed 1320c4bc44c6SKevin Hao * before dirty_tx 1321c4bc44c6SKevin Hao */ 1322c4bc44c6SKevin Hao wmb(); 13234d494cdcSFugang Duan txq->dirty_tx = bdp; 1324793fc096SFrank Li 1325793fc096SFrank Li /* Update pointer to next buffer descriptor to be transmitted */ 13267355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1327793fc096SFrank Li 1328793fc096SFrank Li /* Since we have freed up a buffer, the ring is no longer full 1329793fc096SFrank Li */ 1330657ade07SRickard x Andersson if (netif_tx_queue_stopped(nq)) { 13317355f276STroy Kisky entries_free = fec_enet_get_free_txdesc_num(txq); 13324d494cdcSFugang Duan if (entries_free >= txq->tx_wake_threshold) 13334d494cdcSFugang Duan netif_tx_wake_queue(nq); 1334793fc096SFrank Li } 133579f33912SNimrod Andy } 1336ccea2968SRussell King 1337c10bc0e7SFugang Duan /* ERR006358: Keep the transmitter going */ 13387355f276STroy Kisky if (bdp != txq->bd.cur && 133953bb20d1STroy Kisky readl(txq->bd.reg_desc_active) == 0) 134053bb20d1STroy Kisky writel(0, txq->bd.reg_desc_active); 13414d494cdcSFugang Duan } 13424d494cdcSFugang Duan 13437cdaa4ccSTobias Waldekranz static void fec_enet_tx(struct net_device *ndev) 13444d494cdcSFugang Duan { 13454d494cdcSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 13467cdaa4ccSTobias Waldekranz int i; 13477cdaa4ccSTobias Waldekranz 13487cdaa4ccSTobias Waldekranz /* Make sure that AVB queues are processed first. */ 13497cdaa4ccSTobias Waldekranz for (i = fep->num_tx_queues - 1; i >= 0; i--) 13507cdaa4ccSTobias Waldekranz fec_enet_tx_queue(ndev, i); 1351793fc096SFrank Li } 1352793fc096SFrank Li 13531b7bde6dSNimrod Andy static int 13541b7bde6dSNimrod Andy fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb) 13551b7bde6dSNimrod Andy { 13561b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 13571b7bde6dSNimrod Andy int off; 13581b7bde6dSNimrod Andy 13591b7bde6dSNimrod Andy off = ((unsigned long)skb->data) & fep->rx_align; 13601b7bde6dSNimrod Andy if (off) 13611b7bde6dSNimrod Andy skb_reserve(skb, fep->rx_align + 1 - off); 13621b7bde6dSNimrod Andy 13635cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE)); 13645cfa3039SJohannes Berg if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) { 13651b7bde6dSNimrod Andy if (net_ratelimit()) 13661b7bde6dSNimrod Andy netdev_err(ndev, "Rx DMA memory map failed\n"); 13671b7bde6dSNimrod Andy return -ENOMEM; 13681b7bde6dSNimrod Andy } 13691b7bde6dSNimrod Andy 13701b7bde6dSNimrod Andy return 0; 13711b7bde6dSNimrod Andy } 13721b7bde6dSNimrod Andy 13731b7bde6dSNimrod Andy static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb, 13741310b544SLothar Waßmann struct bufdesc *bdp, u32 length, bool swap) 13751b7bde6dSNimrod Andy { 13761b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 13771b7bde6dSNimrod Andy struct sk_buff *new_skb; 13781b7bde6dSNimrod Andy 13791b7bde6dSNimrod Andy if (length > fep->rx_copybreak) 13801b7bde6dSNimrod Andy return false; 13811b7bde6dSNimrod Andy 13821b7bde6dSNimrod Andy new_skb = netdev_alloc_skb(ndev, length); 13831b7bde6dSNimrod Andy if (!new_skb) 13841b7bde6dSNimrod Andy return false; 13851b7bde6dSNimrod Andy 13865cfa3039SJohannes Berg dma_sync_single_for_cpu(&fep->pdev->dev, 13875cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 13881b7bde6dSNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 13891b7bde6dSNimrod Andy DMA_FROM_DEVICE); 13901310b544SLothar Waßmann if (!swap) 13911b7bde6dSNimrod Andy memcpy(new_skb->data, (*skb)->data, length); 13921310b544SLothar Waßmann else 13931310b544SLothar Waßmann swap_buffer2(new_skb->data, (*skb)->data, length); 13941b7bde6dSNimrod Andy *skb = new_skb; 13951b7bde6dSNimrod Andy 13961b7bde6dSNimrod Andy return true; 13971b7bde6dSNimrod Andy } 13981b7bde6dSNimrod Andy 13997355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer. 1400793fc096SFrank Li * When we update through the ring, if the next incoming buffer has 1401793fc096SFrank Li * not been given to the system, we just set the empty indicator, 1402793fc096SFrank Li * effectively tossing the packet. 1403793fc096SFrank Li */ 1404793fc096SFrank Li static int 14054d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) 1406793fc096SFrank Li { 1407793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 14084d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 1409793fc096SFrank Li struct bufdesc *bdp; 1410793fc096SFrank Li unsigned short status; 14111b7bde6dSNimrod Andy struct sk_buff *skb_new = NULL; 1412793fc096SFrank Li struct sk_buff *skb; 1413793fc096SFrank Li ushort pkt_len; 1414793fc096SFrank Li __u8 *data; 1415793fc096SFrank Li int pkt_received = 0; 1416cdffcf1bSJim Baxter struct bufdesc_ex *ebdp = NULL; 1417cdffcf1bSJim Baxter bool vlan_packet_rcvd = false; 1418cdffcf1bSJim Baxter u16 vlan_tag; 1419d842a31fSDuan Fugang-B38611 int index = 0; 14201b7bde6dSNimrod Andy bool is_copybreak; 14216b7e4008SLothar Waßmann bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME; 1422793fc096SFrank Li 1423793fc096SFrank Li #ifdef CONFIG_M532x 1424793fc096SFrank Li flush_cache_all(); 1425793fc096SFrank Li #endif 14264d494cdcSFugang Duan rxq = fep->rx_queue[queue_id]; 1427793fc096SFrank Li 1428793fc096SFrank Li /* First, grab all of the stats for the incoming packet. 1429793fc096SFrank Li * These get messed up if we get called due to a busy condition. 1430793fc096SFrank Li */ 14317355f276STroy Kisky bdp = rxq->bd.cur; 1432793fc096SFrank Li 14335cfa3039SJohannes Berg while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) { 1434793fc096SFrank Li 1435793fc096SFrank Li if (pkt_received >= budget) 1436793fc096SFrank Li break; 1437793fc096SFrank Li pkt_received++; 1438793fc096SFrank Li 1439ed63f1dcSRussell King writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT); 1440db3421c1SRussell King 1441793fc096SFrank Li /* Check for errors. */ 1442095098e1STroy Kisky status ^= BD_ENET_RX_LAST; 1443793fc096SFrank Li if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | 1444095098e1STroy Kisky BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST | 1445095098e1STroy Kisky BD_ENET_RX_CL)) { 1446793fc096SFrank Li ndev->stats.rx_errors++; 1447095098e1STroy Kisky if (status & BD_ENET_RX_OV) { 1448095098e1STroy Kisky /* FIFO overrun */ 1449095098e1STroy Kisky ndev->stats.rx_fifo_errors++; 1450095098e1STroy Kisky goto rx_processing_done; 1451095098e1STroy Kisky } 1452095098e1STroy Kisky if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH 1453095098e1STroy Kisky | BD_ENET_RX_LAST)) { 1454793fc096SFrank Li /* Frame too long or too short. */ 1455793fc096SFrank Li ndev->stats.rx_length_errors++; 1456095098e1STroy Kisky if (status & BD_ENET_RX_LAST) 1457095098e1STroy Kisky netdev_err(ndev, "rcv is not +last\n"); 1458793fc096SFrank Li } 1459793fc096SFrank Li if (status & BD_ENET_RX_CR) /* CRC Error */ 1460793fc096SFrank Li ndev->stats.rx_crc_errors++; 1461095098e1STroy Kisky /* Report late collisions as a frame error. */ 1462095098e1STroy Kisky if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL)) 1463793fc096SFrank Li ndev->stats.rx_frame_errors++; 1464793fc096SFrank Li goto rx_processing_done; 1465793fc096SFrank Li } 1466793fc096SFrank Li 1467793fc096SFrank Li /* Process the incoming frame. */ 1468793fc096SFrank Li ndev->stats.rx_packets++; 14695cfa3039SJohannes Berg pkt_len = fec16_to_cpu(bdp->cbd_datlen); 1470793fc096SFrank Li ndev->stats.rx_bytes += pkt_len; 1471793fc096SFrank Li 14727355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &rxq->bd); 14731b7bde6dSNimrod Andy skb = rxq->rx_skbuff[index]; 14741b7bde6dSNimrod Andy 14751b7bde6dSNimrod Andy /* The packet length includes FCS, but we don't want to 14761b7bde6dSNimrod Andy * include that when passing upstream as it messes up 14771b7bde6dSNimrod Andy * bridging applications. 14781b7bde6dSNimrod Andy */ 14791310b544SLothar Waßmann is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4, 14801310b544SLothar Waßmann need_swap); 14811b7bde6dSNimrod Andy if (!is_copybreak) { 14821b7bde6dSNimrod Andy skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); 14831b7bde6dSNimrod Andy if (unlikely(!skb_new)) { 14841b7bde6dSNimrod Andy ndev->stats.rx_dropped++; 14851b7bde6dSNimrod Andy goto rx_processing_done; 14861b7bde6dSNimrod Andy } 14875cfa3039SJohannes Berg dma_unmap_single(&fep->pdev->dev, 14885cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 1489b64bf4b7SNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 1490b64bf4b7SNimrod Andy DMA_FROM_DEVICE); 14911b7bde6dSNimrod Andy } 1492793fc096SFrank Li 14931b7bde6dSNimrod Andy prefetch(skb->data - NET_IP_ALIGN); 14941b7bde6dSNimrod Andy skb_put(skb, pkt_len - 4); 14951b7bde6dSNimrod Andy data = skb->data; 14963ac72b7bSEric Nelson 1497235bde1eSFabio Estevam if (!is_copybreak && need_swap) 1498235bde1eSFabio Estevam swap_buffer(data, pkt_len); 1499235bde1eSFabio Estevam 15003ac72b7bSEric Nelson #if !defined(CONFIG_M5272) 15013ac72b7bSEric Nelson if (fep->quirks & FEC_QUIRK_HAS_RACC) 15023ac72b7bSEric Nelson data = skb_pull_inline(skb, 2); 15033ac72b7bSEric Nelson #endif 15043ac72b7bSEric Nelson 1505cdffcf1bSJim Baxter /* Extract the enhanced buffer descriptor */ 1506cdffcf1bSJim Baxter ebdp = NULL; 1507cdffcf1bSJim Baxter if (fep->bufdesc_ex) 1508cdffcf1bSJim Baxter ebdp = (struct bufdesc_ex *)bdp; 1509cdffcf1bSJim Baxter 1510cdffcf1bSJim Baxter /* If this is a VLAN packet remove the VLAN Tag */ 1511cdffcf1bSJim Baxter vlan_packet_rcvd = false; 1512cdffcf1bSJim Baxter if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) && 15135cfa3039SJohannes Berg fep->bufdesc_ex && 15145cfa3039SJohannes Berg (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) { 1515cdffcf1bSJim Baxter /* Push and remove the vlan tag */ 1516cdffcf1bSJim Baxter struct vlan_hdr *vlan_header = 1517cdffcf1bSJim Baxter (struct vlan_hdr *) (data + ETH_HLEN); 1518cdffcf1bSJim Baxter vlan_tag = ntohs(vlan_header->h_vlan_TCI); 1519cdffcf1bSJim Baxter 1520cdffcf1bSJim Baxter vlan_packet_rcvd = true; 15211b7bde6dSNimrod Andy 1522af5cbc98SNimrod Andy memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2); 15231b7bde6dSNimrod Andy skb_pull(skb, VLAN_HLEN); 1524cdffcf1bSJim Baxter } 1525cdffcf1bSJim Baxter 1526793fc096SFrank Li skb->protocol = eth_type_trans(skb, ndev); 1527793fc096SFrank Li 1528793fc096SFrank Li /* Get receive timestamp from the skb */ 1529bfd4ecddSRussell King if (fep->hwts_rx_en && fep->bufdesc_ex) 15305cfa3039SJohannes Berg fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), 1531bfd4ecddSRussell King skb_hwtstamps(skb)); 1532793fc096SFrank Li 15334c09eed9SJim Baxter if (fep->bufdesc_ex && 15344c09eed9SJim Baxter (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { 15355cfa3039SJohannes Berg if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) { 15364c09eed9SJim Baxter /* don't check it */ 15374c09eed9SJim Baxter skb->ip_summed = CHECKSUM_UNNECESSARY; 15384c09eed9SJim Baxter } else { 15394c09eed9SJim Baxter skb_checksum_none_assert(skb); 15404c09eed9SJim Baxter } 15414c09eed9SJim Baxter } 15424c09eed9SJim Baxter 1543cdffcf1bSJim Baxter /* Handle received VLAN packets */ 1544cdffcf1bSJim Baxter if (vlan_packet_rcvd) 1545cdffcf1bSJim Baxter __vlan_hwaccel_put_tag(skb, 1546cdffcf1bSJim Baxter htons(ETH_P_8021Q), 1547cdffcf1bSJim Baxter vlan_tag); 1548cdffcf1bSJim Baxter 15497cdaa4ccSTobias Waldekranz skb_record_rx_queue(skb, queue_id); 1550793fc096SFrank Li napi_gro_receive(&fep->napi, skb); 1551793fc096SFrank Li 15521b7bde6dSNimrod Andy if (is_copybreak) { 15535cfa3039SJohannes Berg dma_sync_single_for_device(&fep->pdev->dev, 15545cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 1555b64bf4b7SNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 1556b64bf4b7SNimrod Andy DMA_FROM_DEVICE); 15571b7bde6dSNimrod Andy } else { 15581b7bde6dSNimrod Andy rxq->rx_skbuff[index] = skb_new; 15591b7bde6dSNimrod Andy fec_enet_new_rxbdp(ndev, bdp, skb_new); 15601b7bde6dSNimrod Andy } 15611b7bde6dSNimrod Andy 1562793fc096SFrank Li rx_processing_done: 1563793fc096SFrank Li /* Clear the status flags for this buffer */ 1564793fc096SFrank Li status &= ~BD_ENET_RX_STATS; 1565793fc096SFrank Li 1566793fc096SFrank Li /* Mark the buffer empty */ 1567793fc096SFrank Li status |= BD_ENET_RX_EMPTY; 1568793fc096SFrank Li 1569793fc096SFrank Li if (fep->bufdesc_ex) { 1570793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1571793fc096SFrank Li 15725cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 1573793fc096SFrank Li ebdp->cbd_prot = 0; 1574793fc096SFrank Li ebdp->cbd_bdu = 0; 1575793fc096SFrank Li } 1576be293467STroy Kisky /* Make sure the updates to rest of the descriptor are 1577be293467STroy Kisky * performed before transferring ownership. 1578be293467STroy Kisky */ 1579be293467STroy Kisky wmb(); 1580be293467STroy Kisky bdp->cbd_sc = cpu_to_fec16(status); 1581793fc096SFrank Li 1582793fc096SFrank Li /* Update BD pointer to next entry */ 15837355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 158436e24e2eSDuan Fugang-B38611 1585793fc096SFrank Li /* Doing this here will keep the FEC running while we process 1586793fc096SFrank Li * incoming frames. On a heavily loaded network, we should be 1587793fc096SFrank Li * able to keep up at the expense of system resources. 1588793fc096SFrank Li */ 158953bb20d1STroy Kisky writel(0, rxq->bd.reg_desc_active); 1590793fc096SFrank Li } 15917355f276STroy Kisky rxq->bd.cur = bdp; 1592793fc096SFrank Li return pkt_received; 1593793fc096SFrank Li } 1594793fc096SFrank Li 15957cdaa4ccSTobias Waldekranz static int fec_enet_rx(struct net_device *ndev, int budget) 15964d494cdcSFugang Duan { 15974d494cdcSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 15987cdaa4ccSTobias Waldekranz int i, done = 0; 15994d494cdcSFugang Duan 16007cdaa4ccSTobias Waldekranz /* Make sure that AVB queues are processed first. */ 16017cdaa4ccSTobias Waldekranz for (i = fep->num_rx_queues - 1; i >= 0; i--) 16027cdaa4ccSTobias Waldekranz done += fec_enet_rx_queue(ndev, budget - done, i); 16031c021bb7SUwe Kleine-König 16047cdaa4ccSTobias Waldekranz return done; 16054d494cdcSFugang Duan } 16064d494cdcSFugang Duan 16077cdaa4ccSTobias Waldekranz static bool fec_enet_collect_events(struct fec_enet_private *fep) 16084d494cdcSFugang Duan { 1609793fc096SFrank Li uint int_events; 1610793fc096SFrank Li 1611793fc096SFrank Li int_events = readl(fep->hwp + FEC_IEVENT); 1612f166f890SAndrew Lunn 1613f166f890SAndrew Lunn /* Don't clear MDIO events, we poll for those */ 1614f166f890SAndrew Lunn int_events &= ~FEC_ENET_MII; 1615f166f890SAndrew Lunn 161694191fd6SNimrod Andy writel(int_events, fep->hwp + FEC_IEVENT); 1617793fc096SFrank Li 16187cdaa4ccSTobias Waldekranz return int_events != 0; 16197cdaa4ccSTobias Waldekranz } 16207cdaa4ccSTobias Waldekranz 16217cdaa4ccSTobias Waldekranz static irqreturn_t 16227cdaa4ccSTobias Waldekranz fec_enet_interrupt(int irq, void *dev_id) 16237cdaa4ccSTobias Waldekranz { 16247cdaa4ccSTobias Waldekranz struct net_device *ndev = dev_id; 16257cdaa4ccSTobias Waldekranz struct fec_enet_private *fep = netdev_priv(ndev); 16267cdaa4ccSTobias Waldekranz irqreturn_t ret = IRQ_NONE; 16277cdaa4ccSTobias Waldekranz 16287cdaa4ccSTobias Waldekranz if (fec_enet_collect_events(fep) && fep->link) { 1629793fc096SFrank Li ret = IRQ_HANDLED; 1630793fc096SFrank Li 163194191fd6SNimrod Andy if (napi_schedule_prep(&fep->napi)) { 1632f166f890SAndrew Lunn /* Disable interrupts */ 1633f166f890SAndrew Lunn writel(0, fep->hwp + FEC_IMASK); 163494191fd6SNimrod Andy __napi_schedule(&fep->napi); 163594191fd6SNimrod Andy } 1636793fc096SFrank Li } 1637793fc096SFrank Li 1638793fc096SFrank Li return ret; 1639793fc096SFrank Li } 1640793fc096SFrank Li 1641793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget) 1642793fc096SFrank Li { 1643793fc096SFrank Li struct net_device *ndev = napi->dev; 1644793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 16457cdaa4ccSTobias Waldekranz int done = 0; 16467a16807cSRussell King 16477cdaa4ccSTobias Waldekranz do { 16487cdaa4ccSTobias Waldekranz done += fec_enet_rx(ndev, budget - done); 1649793fc096SFrank Li fec_enet_tx(ndev); 16507cdaa4ccSTobias Waldekranz } while ((done < budget) && fec_enet_collect_events(fep)); 1651793fc096SFrank Li 16527cdaa4ccSTobias Waldekranz if (done < budget) { 16537cdaa4ccSTobias Waldekranz napi_complete_done(napi, done); 1654793fc096SFrank Li writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1655793fc096SFrank Li } 16567cdaa4ccSTobias Waldekranz 16577cdaa4ccSTobias Waldekranz return done; 1658793fc096SFrank Li } 1659793fc096SFrank Li 1660793fc096SFrank Li /* ------------------------------------------------------------------------- */ 1661793fc096SFrank Li static void fec_get_mac(struct net_device *ndev) 1662793fc096SFrank Li { 1663793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 166494660ba0SJingoo Han struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev); 1665793fc096SFrank Li unsigned char *iap, tmpaddr[ETH_ALEN]; 1666793fc096SFrank Li 1667793fc096SFrank Li /* 1668793fc096SFrank Li * try to get mac address in following order: 1669793fc096SFrank Li * 1670793fc096SFrank Li * 1) module parameter via kernel command line in form 1671793fc096SFrank Li * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 1672793fc096SFrank Li */ 1673793fc096SFrank Li iap = macaddr; 1674793fc096SFrank Li 1675793fc096SFrank Li /* 1676793fc096SFrank Li * 2) from device tree data 1677793fc096SFrank Li */ 1678793fc096SFrank Li if (!is_valid_ether_addr(iap)) { 1679793fc096SFrank Li struct device_node *np = fep->pdev->dev.of_node; 1680793fc096SFrank Li if (np) { 1681793fc096SFrank Li const char *mac = of_get_mac_address(np); 1682a51645f7SPetr Štetiar if (!IS_ERR(mac)) 1683793fc096SFrank Li iap = (unsigned char *) mac; 1684793fc096SFrank Li } 1685793fc096SFrank Li } 1686793fc096SFrank Li 1687793fc096SFrank Li /* 1688793fc096SFrank Li * 3) from flash or fuse (via platform data) 1689793fc096SFrank Li */ 1690793fc096SFrank Li if (!is_valid_ether_addr(iap)) { 1691793fc096SFrank Li #ifdef CONFIG_M5272 1692793fc096SFrank Li if (FEC_FLASHMAC) 1693793fc096SFrank Li iap = (unsigned char *)FEC_FLASHMAC; 1694793fc096SFrank Li #else 1695793fc096SFrank Li if (pdata) 1696793fc096SFrank Li iap = (unsigned char *)&pdata->mac; 1697793fc096SFrank Li #endif 1698793fc096SFrank Li } 1699793fc096SFrank Li 1700793fc096SFrank Li /* 1701793fc096SFrank Li * 4) FEC mac registers set by bootloader 1702793fc096SFrank Li */ 1703793fc096SFrank Li if (!is_valid_ether_addr(iap)) { 17047d7628f3SDan Carpenter *((__be32 *) &tmpaddr[0]) = 17057d7628f3SDan Carpenter cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW)); 17067d7628f3SDan Carpenter *((__be16 *) &tmpaddr[4]) = 17077d7628f3SDan Carpenter cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); 1708793fc096SFrank Li iap = &tmpaddr[0]; 1709793fc096SFrank Li } 1710793fc096SFrank Li 1711ff5b2fabSLucas Stach /* 1712ff5b2fabSLucas Stach * 5) random mac address 1713ff5b2fabSLucas Stach */ 1714ff5b2fabSLucas Stach if (!is_valid_ether_addr(iap)) { 1715ff5b2fabSLucas Stach /* Report it and use a random ethernet address instead */ 1716a19a0582SFabio Estevam dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap); 1717ff5b2fabSLucas Stach eth_hw_addr_random(ndev); 1718a19a0582SFabio Estevam dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n", 1719ff5b2fabSLucas Stach ndev->dev_addr); 1720ff5b2fabSLucas Stach return; 1721ff5b2fabSLucas Stach } 1722ff5b2fabSLucas Stach 1723793fc096SFrank Li memcpy(ndev->dev_addr, iap, ETH_ALEN); 1724793fc096SFrank Li 1725793fc096SFrank Li /* Adjust MAC if using macaddr */ 1726793fc096SFrank Li if (iap == macaddr) 1727793fc096SFrank Li ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id; 1728793fc096SFrank Li } 1729793fc096SFrank Li 1730793fc096SFrank Li /* ------------------------------------------------------------------------- */ 1731793fc096SFrank Li 1732793fc096SFrank Li /* 1733793fc096SFrank Li * Phy section 1734793fc096SFrank Li */ 1735793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev) 1736793fc096SFrank Li { 1737793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 173845f5c327SPhilippe Reynes struct phy_device *phy_dev = ndev->phydev; 1739793fc096SFrank Li int status_change = 0; 1740793fc096SFrank Li 17418ce5624fSRussell King /* 17428ce5624fSRussell King * If the netdev is down, or is going down, we're not interested 17438ce5624fSRussell King * in link state events, so just mark our idea of the link as down 17448ce5624fSRussell King * and ignore the event. 17458ce5624fSRussell King */ 17468ce5624fSRussell King if (!netif_running(ndev) || !netif_device_present(ndev)) { 17478ce5624fSRussell King fep->link = 0; 17488ce5624fSRussell King } else if (phy_dev->link) { 1749793fc096SFrank Li if (!fep->link) { 1750793fc096SFrank Li fep->link = phy_dev->link; 1751793fc096SFrank Li status_change = 1; 1752793fc096SFrank Li } 1753793fc096SFrank Li 1754ef83337dSRussell King if (fep->full_duplex != phy_dev->duplex) { 1755ef83337dSRussell King fep->full_duplex = phy_dev->duplex; 1756793fc096SFrank Li status_change = 1; 1757ef83337dSRussell King } 1758793fc096SFrank Li 1759793fc096SFrank Li if (phy_dev->speed != fep->speed) { 1760793fc096SFrank Li fep->speed = phy_dev->speed; 1761793fc096SFrank Li status_change = 1; 1762793fc096SFrank Li } 1763793fc096SFrank Li 1764793fc096SFrank Li /* if any of the above changed restart the FEC */ 1765dbc64a8eSRussell King if (status_change) { 1766dbc64a8eSRussell King napi_disable(&fep->napi); 1767dbc64a8eSRussell King netif_tx_lock_bh(ndev); 1768ef83337dSRussell King fec_restart(ndev); 1769657ade07SRickard x Andersson netif_tx_wake_all_queues(ndev); 17706af42d42SRussell King netif_tx_unlock_bh(ndev); 1771dbc64a8eSRussell King napi_enable(&fep->napi); 1772dbc64a8eSRussell King } 1773793fc096SFrank Li } else { 1774793fc096SFrank Li if (fep->link) { 1775f208ce10SRussell King napi_disable(&fep->napi); 1776f208ce10SRussell King netif_tx_lock_bh(ndev); 1777793fc096SFrank Li fec_stop(ndev); 1778f208ce10SRussell King netif_tx_unlock_bh(ndev); 1779f208ce10SRussell King napi_enable(&fep->napi); 17806e0895c2SDavid S. Miller fep->link = phy_dev->link; 1781793fc096SFrank Li status_change = 1; 1782793fc096SFrank Li } 1783793fc096SFrank Li } 1784793fc096SFrank Li 1785793fc096SFrank Li if (status_change) 1786793fc096SFrank Li phy_print_status(phy_dev); 1787793fc096SFrank Li } 1788793fc096SFrank Li 1789f166f890SAndrew Lunn static int fec_enet_mdio_wait(struct fec_enet_private *fep) 1790f166f890SAndrew Lunn { 1791f166f890SAndrew Lunn uint ievent; 1792f166f890SAndrew Lunn int ret; 1793f166f890SAndrew Lunn 1794f166f890SAndrew Lunn ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent, 1795f166f890SAndrew Lunn ievent & FEC_ENET_MII, 2, 30000); 1796f166f890SAndrew Lunn 1797f166f890SAndrew Lunn if (!ret) 1798f166f890SAndrew Lunn writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT); 1799f166f890SAndrew Lunn 1800f166f890SAndrew Lunn return ret; 1801f166f890SAndrew Lunn } 1802f166f890SAndrew Lunn 1803793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum) 1804793fc096SFrank Li { 1805793fc096SFrank Li struct fec_enet_private *fep = bus->priv; 18068fff755eSAndrew Lunn struct device *dev = &fep->pdev->dev; 1807d3ee8ec7SMarco Hartmann int ret = 0, frame_start, frame_addr, frame_op; 1808d3ee8ec7SMarco Hartmann bool is_c45 = !!(regnum & MII_ADDR_C45); 18098fff755eSAndrew Lunn 18108fff755eSAndrew Lunn ret = pm_runtime_get_sync(dev); 1811b0c6ce24SFabio Estevam if (ret < 0) 18128fff755eSAndrew Lunn return ret; 1813793fc096SFrank Li 1814d3ee8ec7SMarco Hartmann if (is_c45) { 1815d3ee8ec7SMarco Hartmann frame_start = FEC_MMFR_ST_C45; 1816d3ee8ec7SMarco Hartmann 1817d3ee8ec7SMarco Hartmann /* write address */ 1818d3ee8ec7SMarco Hartmann frame_addr = (regnum >> 16); 1819d3ee8ec7SMarco Hartmann writel(frame_start | FEC_MMFR_OP_ADDR_WRITE | 1820d3ee8ec7SMarco Hartmann FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 1821d3ee8ec7SMarco Hartmann FEC_MMFR_TA | (regnum & 0xFFFF), 1822d3ee8ec7SMarco Hartmann fep->hwp + FEC_MII_DATA); 1823d3ee8ec7SMarco Hartmann 1824d3ee8ec7SMarco Hartmann /* wait for end of transfer */ 1825f166f890SAndrew Lunn ret = fec_enet_mdio_wait(fep); 1826f166f890SAndrew Lunn if (ret) { 1827d3ee8ec7SMarco Hartmann netdev_err(fep->netdev, "MDIO address write timeout\n"); 1828d3ee8ec7SMarco Hartmann goto out; 1829d3ee8ec7SMarco Hartmann } 1830d3ee8ec7SMarco Hartmann 1831d3ee8ec7SMarco Hartmann frame_op = FEC_MMFR_OP_READ_C45; 1832d3ee8ec7SMarco Hartmann 1833d3ee8ec7SMarco Hartmann } else { 1834d3ee8ec7SMarco Hartmann /* C22 read */ 1835d3ee8ec7SMarco Hartmann frame_op = FEC_MMFR_OP_READ; 1836d3ee8ec7SMarco Hartmann frame_start = FEC_MMFR_ST; 1837d3ee8ec7SMarco Hartmann frame_addr = regnum; 1838d3ee8ec7SMarco Hartmann } 1839d3ee8ec7SMarco Hartmann 1840793fc096SFrank Li /* start a read op */ 1841d3ee8ec7SMarco Hartmann writel(frame_start | frame_op | 1842d3ee8ec7SMarco Hartmann FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 1843793fc096SFrank Li FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); 1844793fc096SFrank Li 1845793fc096SFrank Li /* wait for end of transfer */ 1846f166f890SAndrew Lunn ret = fec_enet_mdio_wait(fep); 1847f166f890SAndrew Lunn if (ret) { 184831b7720cSJoe Perches netdev_err(fep->netdev, "MDIO read timeout\n"); 18498fff755eSAndrew Lunn goto out; 1850793fc096SFrank Li } 1851793fc096SFrank Li 18528fff755eSAndrew Lunn ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); 18538fff755eSAndrew Lunn 18548fff755eSAndrew Lunn out: 18558fff755eSAndrew Lunn pm_runtime_mark_last_busy(dev); 18568fff755eSAndrew Lunn pm_runtime_put_autosuspend(dev); 18578fff755eSAndrew Lunn 18588fff755eSAndrew Lunn return ret; 1859793fc096SFrank Li } 1860793fc096SFrank Li 1861793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum, 1862793fc096SFrank Li u16 value) 1863793fc096SFrank Li { 1864793fc096SFrank Li struct fec_enet_private *fep = bus->priv; 18658fff755eSAndrew Lunn struct device *dev = &fep->pdev->dev; 1866d3ee8ec7SMarco Hartmann int ret, frame_start, frame_addr; 1867d3ee8ec7SMarco Hartmann bool is_c45 = !!(regnum & MII_ADDR_C45); 18688fff755eSAndrew Lunn 18698fff755eSAndrew Lunn ret = pm_runtime_get_sync(dev); 1870b0c6ce24SFabio Estevam if (ret < 0) 18718fff755eSAndrew Lunn return ret; 187242ea4457SMaciej S. Szmigiero else 187342ea4457SMaciej S. Szmigiero ret = 0; 1874793fc096SFrank Li 1875d3ee8ec7SMarco Hartmann if (is_c45) { 1876d3ee8ec7SMarco Hartmann frame_start = FEC_MMFR_ST_C45; 1877d3ee8ec7SMarco Hartmann 1878d3ee8ec7SMarco Hartmann /* write address */ 1879d3ee8ec7SMarco Hartmann frame_addr = (regnum >> 16); 1880d3ee8ec7SMarco Hartmann writel(frame_start | FEC_MMFR_OP_ADDR_WRITE | 1881d3ee8ec7SMarco Hartmann FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 1882d3ee8ec7SMarco Hartmann FEC_MMFR_TA | (regnum & 0xFFFF), 1883d3ee8ec7SMarco Hartmann fep->hwp + FEC_MII_DATA); 1884d3ee8ec7SMarco Hartmann 1885d3ee8ec7SMarco Hartmann /* wait for end of transfer */ 1886f166f890SAndrew Lunn ret = fec_enet_mdio_wait(fep); 1887f166f890SAndrew Lunn if (ret) { 1888d3ee8ec7SMarco Hartmann netdev_err(fep->netdev, "MDIO address write timeout\n"); 1889d3ee8ec7SMarco Hartmann goto out; 1890d3ee8ec7SMarco Hartmann } 1891d3ee8ec7SMarco Hartmann } else { 1892d3ee8ec7SMarco Hartmann /* C22 write */ 1893d3ee8ec7SMarco Hartmann frame_start = FEC_MMFR_ST; 1894d3ee8ec7SMarco Hartmann frame_addr = regnum; 1895d3ee8ec7SMarco Hartmann } 1896d3ee8ec7SMarco Hartmann 1897793fc096SFrank Li /* start a write op */ 1898d3ee8ec7SMarco Hartmann writel(frame_start | FEC_MMFR_OP_WRITE | 1899d3ee8ec7SMarco Hartmann FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 1900793fc096SFrank Li FEC_MMFR_TA | FEC_MMFR_DATA(value), 1901793fc096SFrank Li fep->hwp + FEC_MII_DATA); 1902793fc096SFrank Li 1903793fc096SFrank Li /* wait for end of transfer */ 1904f166f890SAndrew Lunn ret = fec_enet_mdio_wait(fep); 1905f166f890SAndrew Lunn if (ret) 190631b7720cSJoe Perches netdev_err(fep->netdev, "MDIO write timeout\n"); 1907793fc096SFrank Li 1908d3ee8ec7SMarco Hartmann out: 19098fff755eSAndrew Lunn pm_runtime_mark_last_busy(dev); 19108fff755eSAndrew Lunn pm_runtime_put_autosuspend(dev); 19118fff755eSAndrew Lunn 19128fff755eSAndrew Lunn return ret; 1913793fc096SFrank Li } 1914793fc096SFrank Li 1915e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable) 1916e8fcfcd5SNimrod Andy { 1917e8fcfcd5SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 1918e8fcfcd5SNimrod Andy int ret; 1919e8fcfcd5SNimrod Andy 1920e8fcfcd5SNimrod Andy if (enable) { 1921e8fcfcd5SNimrod Andy ret = clk_prepare_enable(fep->clk_enet_out); 1922e8fcfcd5SNimrod Andy if (ret) 1923d7c3a206SAndy Duan return ret; 192401e5943aSUwe Kleine-König 1925e8fcfcd5SNimrod Andy if (fep->clk_ptp) { 192691c0d987SNimrod Andy mutex_lock(&fep->ptp_clk_mutex); 1927e8fcfcd5SNimrod Andy ret = clk_prepare_enable(fep->clk_ptp); 192891c0d987SNimrod Andy if (ret) { 192991c0d987SNimrod Andy mutex_unlock(&fep->ptp_clk_mutex); 1930e8fcfcd5SNimrod Andy goto failed_clk_ptp; 193191c0d987SNimrod Andy } else { 193291c0d987SNimrod Andy fep->ptp_clk_on = true; 193391c0d987SNimrod Andy } 193491c0d987SNimrod Andy mutex_unlock(&fep->ptp_clk_mutex); 1935e8fcfcd5SNimrod Andy } 193601e5943aSUwe Kleine-König 19379b5330edSFugang Duan ret = clk_prepare_enable(fep->clk_ref); 19389b5330edSFugang Duan if (ret) 19399b5330edSFugang Duan goto failed_clk_ref; 19401b0a83acSRichard Leitner 19411b0a83acSRichard Leitner phy_reset_after_clk_enable(ndev->phydev); 1942e8fcfcd5SNimrod Andy } else { 1943e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_enet_out); 194491c0d987SNimrod Andy if (fep->clk_ptp) { 194591c0d987SNimrod Andy mutex_lock(&fep->ptp_clk_mutex); 1946e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_ptp); 194791c0d987SNimrod Andy fep->ptp_clk_on = false; 194891c0d987SNimrod Andy mutex_unlock(&fep->ptp_clk_mutex); 194991c0d987SNimrod Andy } 19509b5330edSFugang Duan clk_disable_unprepare(fep->clk_ref); 1951e8fcfcd5SNimrod Andy } 1952e8fcfcd5SNimrod Andy 1953e8fcfcd5SNimrod Andy return 0; 19549b5330edSFugang Duan 19559b5330edSFugang Duan failed_clk_ref: 1956a74d19baSLiu Xiang if (fep->clk_ptp) { 1957a74d19baSLiu Xiang mutex_lock(&fep->ptp_clk_mutex); 1958a74d19baSLiu Xiang clk_disable_unprepare(fep->clk_ptp); 1959a74d19baSLiu Xiang fep->ptp_clk_on = false; 1960a74d19baSLiu Xiang mutex_unlock(&fep->ptp_clk_mutex); 1961a74d19baSLiu Xiang } 1962e8fcfcd5SNimrod Andy failed_clk_ptp: 1963e8fcfcd5SNimrod Andy if (fep->clk_enet_out) 1964e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_enet_out); 1965e8fcfcd5SNimrod Andy 1966e8fcfcd5SNimrod Andy return ret; 1967e8fcfcd5SNimrod Andy } 1968e8fcfcd5SNimrod Andy 1969793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev) 1970793fc096SFrank Li { 1971793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1972793fc096SFrank Li struct phy_device *phy_dev = NULL; 1973793fc096SFrank Li char mdio_bus_id[MII_BUS_ID_SIZE]; 1974793fc096SFrank Li char phy_name[MII_BUS_ID_SIZE + 3]; 1975793fc096SFrank Li int phy_id; 1976793fc096SFrank Li int dev_id = fep->dev_id; 1977793fc096SFrank Li 1978407066f8SUwe Kleine-König if (fep->phy_node) { 1979407066f8SUwe Kleine-König phy_dev = of_phy_connect(ndev, fep->phy_node, 1980407066f8SUwe Kleine-König &fec_enet_adjust_link, 0, 1981407066f8SUwe Kleine-König fep->phy_interface); 19829558df3aSAndrew Lunn if (!phy_dev) { 19839558df3aSAndrew Lunn netdev_err(ndev, "Unable to connect to phy\n"); 1984213a9922SNimrod Andy return -ENODEV; 19859558df3aSAndrew Lunn } 1986407066f8SUwe Kleine-König } else { 1987793fc096SFrank Li /* check for attached phy */ 1988793fc096SFrank Li for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { 19897f854420SAndrew Lunn if (!mdiobus_is_registered_device(fep->mii_bus, phy_id)) 1990793fc096SFrank Li continue; 1991793fc096SFrank Li if (dev_id--) 1992793fc096SFrank Li continue; 1993949bdd20SRickard Strandqvist strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); 1994793fc096SFrank Li break; 1995793fc096SFrank Li } 1996793fc096SFrank Li 1997793fc096SFrank Li if (phy_id >= PHY_MAX_ADDR) { 199831b7720cSJoe Perches netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); 1999949bdd20SRickard Strandqvist strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); 2000793fc096SFrank Li phy_id = 0; 2001793fc096SFrank Li } 2002793fc096SFrank Li 2003407066f8SUwe Kleine-König snprintf(phy_name, sizeof(phy_name), 2004407066f8SUwe Kleine-König PHY_ID_FMT, mdio_bus_id, phy_id); 2005793fc096SFrank Li phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 2006793fc096SFrank Li fep->phy_interface); 2007407066f8SUwe Kleine-König } 2008407066f8SUwe Kleine-König 2009793fc096SFrank Li if (IS_ERR(phy_dev)) { 201031b7720cSJoe Perches netdev_err(ndev, "could not attach to PHY\n"); 2011793fc096SFrank Li return PTR_ERR(phy_dev); 2012793fc096SFrank Li } 2013793fc096SFrank Li 2014793fc096SFrank Li /* mask with MAC supported features */ 20156b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_GBIT) { 201658056c1eSAndrew Lunn phy_set_max_speed(phy_dev, 1000); 201741124fa6SAndrew Lunn phy_remove_link_mode(phy_dev, 201841124fa6SAndrew Lunn ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 2019d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 2020c306ad36SAndrew Lunn phy_support_sym_pause(phy_dev); 2021d1391930SGuenter Roeck #endif 2022793fc096SFrank Li } 2023793fc096SFrank Li else 202458056c1eSAndrew Lunn phy_set_max_speed(phy_dev, 100); 2025793fc096SFrank Li 2026793fc096SFrank Li fep->link = 0; 2027793fc096SFrank Li fep->full_duplex = 0; 2028793fc096SFrank Li 20292220943aSAndrew Lunn phy_attached_info(phy_dev); 2030793fc096SFrank Li 2031793fc096SFrank Li return 0; 2032793fc096SFrank Li } 2033793fc096SFrank Li 2034793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev) 2035793fc096SFrank Li { 2036793fc096SFrank Li static struct mii_bus *fec0_mii_bus; 2037793fc096SFrank Li struct net_device *ndev = platform_get_drvdata(pdev); 2038793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 20393c01eb62SAndrew Lunn bool suppress_preamble = false; 2040407066f8SUwe Kleine-König struct device_node *node; 2041e7f4dc35SAndrew Lunn int err = -ENXIO; 204263c60732SUwe Kleine-König u32 mii_speed, holdtime; 20433e782985SAndrew Lunn u32 bus_freq; 2044793fc096SFrank Li 2045793fc096SFrank Li /* 20463d125f9cSStefan Agner * The i.MX28 dual fec interfaces are not equal. 2047793fc096SFrank Li * Here are the differences: 2048793fc096SFrank Li * 2049793fc096SFrank Li * - fec0 supports MII & RMII modes while fec1 only supports RMII 2050793fc096SFrank Li * - fec0 acts as the 1588 time master while fec1 is slave 2051793fc096SFrank Li * - external phys can only be configured by fec0 2052793fc096SFrank Li * 2053793fc096SFrank Li * That is to say fec1 can not work independently. It only works 2054793fc096SFrank Li * when fec0 is working. The reason behind this design is that the 2055793fc096SFrank Li * second interface is added primarily for Switch mode. 2056793fc096SFrank Li * 2057793fc096SFrank Li * Because of the last point above, both phys are attached on fec0 2058793fc096SFrank Li * mdio interface in board design, and need to be configured by 2059793fc096SFrank Li * fec0 mii_bus. 2060793fc096SFrank Li */ 20613d125f9cSStefan Agner if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) { 2062793fc096SFrank Li /* fec1 uses fec0 mii_bus */ 2063793fc096SFrank Li if (mii_cnt && fec0_mii_bus) { 2064793fc096SFrank Li fep->mii_bus = fec0_mii_bus; 2065793fc096SFrank Li mii_cnt++; 2066793fc096SFrank Li return 0; 2067793fc096SFrank Li } 2068793fc096SFrank Li return -ENOENT; 2069793fc096SFrank Li } 2070793fc096SFrank Li 20713e782985SAndrew Lunn bus_freq = 2500000; /* 2.5MHz by default */ 20723e782985SAndrew Lunn node = of_get_child_by_name(pdev->dev.of_node, "mdio"); 20733c01eb62SAndrew Lunn if (node) { 20743e782985SAndrew Lunn of_property_read_u32(node, "clock-frequency", &bus_freq); 20753c01eb62SAndrew Lunn suppress_preamble = of_property_read_bool(node, 20763c01eb62SAndrew Lunn "suppress-preamble"); 20773c01eb62SAndrew Lunn } 20783e782985SAndrew Lunn 2079793fc096SFrank Li /* 20803e782985SAndrew Lunn * Set MII speed (= clk_get_rate() / 2 * phy_speed) 2081793fc096SFrank Li * 2082793fc096SFrank Li * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while 2083793fc096SFrank Li * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 2084793fc096SFrank Li * Reference Manual has an error on this, and gets fixed on i.MX6Q 2085793fc096SFrank Li * document. 2086793fc096SFrank Li */ 20873e782985SAndrew Lunn mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2); 20886b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_ENET_MAC) 208963c60732SUwe Kleine-König mii_speed--; 209063c60732SUwe Kleine-König if (mii_speed > 63) { 209163c60732SUwe Kleine-König dev_err(&pdev->dev, 2092981a0547SPeter Meerwald-Stadler "fec clock (%lu) too fast to get right mii speed\n", 209363c60732SUwe Kleine-König clk_get_rate(fep->clk_ipg)); 209463c60732SUwe Kleine-König err = -EINVAL; 209563c60732SUwe Kleine-König goto err_out; 209663c60732SUwe Kleine-König } 209763c60732SUwe Kleine-König 209863c60732SUwe Kleine-König /* 209963c60732SUwe Kleine-König * The i.MX28 and i.MX6 types have another filed in the MSCR (aka 210063c60732SUwe Kleine-König * MII_SPEED) register that defines the MDIO output hold time. Earlier 210163c60732SUwe Kleine-König * versions are RAZ there, so just ignore the difference and write the 210263c60732SUwe Kleine-König * register always. 210363c60732SUwe Kleine-König * The minimal hold time according to IEE802.3 (clause 22) is 10 ns. 210463c60732SUwe Kleine-König * HOLDTIME + 1 is the number of clk cycles the fec is holding the 210563c60732SUwe Kleine-König * output. 210663c60732SUwe Kleine-König * The HOLDTIME bitfield takes values between 0 and 7 (inclusive). 210763c60732SUwe Kleine-König * Given that ceil(clkrate / 5000000) <= 64, the calculation for 210863c60732SUwe Kleine-König * holdtime cannot result in a value greater than 3. 210963c60732SUwe Kleine-König */ 211063c60732SUwe Kleine-König holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1; 211163c60732SUwe Kleine-König 211263c60732SUwe Kleine-König fep->phy_speed = mii_speed << 1 | holdtime << 8; 211363c60732SUwe Kleine-König 21143c01eb62SAndrew Lunn if (suppress_preamble) 21153c01eb62SAndrew Lunn fep->phy_speed |= BIT(7); 21163c01eb62SAndrew Lunn 2117f166f890SAndrew Lunn /* Clear MMFR to avoid to generate MII event by writing MSCR. 2118f166f890SAndrew Lunn * MII event generation condition: 2119f166f890SAndrew Lunn * - writing MSCR: 2120f166f890SAndrew Lunn * - mmfr[31:0]_not_zero & mscr[7:0]_is_zero & 2121f166f890SAndrew Lunn * mscr_reg_data_in[7:0] != 0 2122f166f890SAndrew Lunn * - writing MMFR: 2123f166f890SAndrew Lunn * - mscr[7:0]_not_zero 2124f166f890SAndrew Lunn */ 2125f166f890SAndrew Lunn writel(0, fep->hwp + FEC_MII_DATA); 2126f166f890SAndrew Lunn 2127793fc096SFrank Li writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 2128793fc096SFrank Li 2129f166f890SAndrew Lunn /* Clear any pending transaction complete indication */ 2130f166f890SAndrew Lunn writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT); 2131f166f890SAndrew Lunn 2132793fc096SFrank Li fep->mii_bus = mdiobus_alloc(); 2133793fc096SFrank Li if (fep->mii_bus == NULL) { 2134793fc096SFrank Li err = -ENOMEM; 2135793fc096SFrank Li goto err_out; 2136793fc096SFrank Li } 2137793fc096SFrank Li 2138793fc096SFrank Li fep->mii_bus->name = "fec_enet_mii_bus"; 2139793fc096SFrank Li fep->mii_bus->read = fec_enet_mdio_read; 2140793fc096SFrank Li fep->mii_bus->write = fec_enet_mdio_write; 2141793fc096SFrank Li snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 2142793fc096SFrank Li pdev->name, fep->dev_id + 1); 2143793fc096SFrank Li fep->mii_bus->priv = fep; 2144793fc096SFrank Li fep->mii_bus->parent = &pdev->dev; 2145793fc096SFrank Li 2146407066f8SUwe Kleine-König err = of_mdiobus_register(fep->mii_bus, node); 2147407066f8SUwe Kleine-König of_node_put(node); 2148407066f8SUwe Kleine-König if (err) 2149e7f4dc35SAndrew Lunn goto err_out_free_mdiobus; 2150793fc096SFrank Li 2151793fc096SFrank Li mii_cnt++; 2152793fc096SFrank Li 2153793fc096SFrank Li /* save fec0 mii_bus */ 21543d125f9cSStefan Agner if (fep->quirks & FEC_QUIRK_SINGLE_MDIO) 2155793fc096SFrank Li fec0_mii_bus = fep->mii_bus; 2156793fc096SFrank Li 2157793fc096SFrank Li return 0; 2158793fc096SFrank Li 2159793fc096SFrank Li err_out_free_mdiobus: 2160793fc096SFrank Li mdiobus_free(fep->mii_bus); 2161793fc096SFrank Li err_out: 2162793fc096SFrank Li return err; 2163793fc096SFrank Li } 2164793fc096SFrank Li 2165793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep) 2166793fc096SFrank Li { 2167793fc096SFrank Li if (--mii_cnt == 0) { 2168793fc096SFrank Li mdiobus_unregister(fep->mii_bus); 2169793fc096SFrank Li mdiobus_free(fep->mii_bus); 2170793fc096SFrank Li } 2171793fc096SFrank Li } 2172793fc096SFrank Li 2173793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev, 2174793fc096SFrank Li struct ethtool_drvinfo *info) 2175793fc096SFrank Li { 2176793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2177793fc096SFrank Li 2178793fc096SFrank Li strlcpy(info->driver, fep->pdev->dev.driver->name, 2179793fc096SFrank Li sizeof(info->driver)); 2180793fc096SFrank Li strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); 2181793fc096SFrank Li } 2182793fc096SFrank Li 2183db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev) 2184db65f35fSPhilippe Reynes { 2185db65f35fSPhilippe Reynes struct fec_enet_private *fep = netdev_priv(ndev); 2186db65f35fSPhilippe Reynes struct resource *r; 2187db65f35fSPhilippe Reynes int s = 0; 2188db65f35fSPhilippe Reynes 2189db65f35fSPhilippe Reynes r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0); 2190db65f35fSPhilippe Reynes if (r) 2191db65f35fSPhilippe Reynes s = resource_size(r); 2192db65f35fSPhilippe Reynes 2193db65f35fSPhilippe Reynes return s; 2194db65f35fSPhilippe Reynes } 2195db65f35fSPhilippe Reynes 2196db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */ 2197db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 21983f1dcc6aSLucas Stach defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 219978cc6e7eSFlorian Fainelli defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST) 2200f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 2; 2201db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = { 2202db65f35fSPhilippe Reynes FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0, 2203db65f35fSPhilippe Reynes FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL, 2204db65f35fSPhilippe Reynes FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1, 2205db65f35fSPhilippe Reynes FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH, 2206db65f35fSPhilippe Reynes FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, 2207db65f35fSPhilippe Reynes FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1, 2208db65f35fSPhilippe Reynes FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2, 2209db65f35fSPhilippe Reynes FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0, 2210db65f35fSPhilippe Reynes FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM, 2211db65f35fSPhilippe Reynes FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2, 2212db65f35fSPhilippe Reynes FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1, 2213db65f35fSPhilippe Reynes FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME, 2214db65f35fSPhilippe Reynes RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT, 2215db65f35fSPhilippe Reynes RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG, 2216db65f35fSPhilippe Reynes RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255, 2217db65f35fSPhilippe Reynes RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047, 2218db65f35fSPhilippe Reynes RMON_T_P_GTE2048, RMON_T_OCTETS, 2219db65f35fSPhilippe Reynes IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF, 2220db65f35fSPhilippe Reynes IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE, 2221db65f35fSPhilippe Reynes IEEE_T_FDXFC, IEEE_T_OCTETS_OK, 2222db65f35fSPhilippe Reynes RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN, 2223db65f35fSPhilippe Reynes RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB, 2224db65f35fSPhilippe Reynes RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255, 2225db65f35fSPhilippe Reynes RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047, 2226db65f35fSPhilippe Reynes RMON_R_P_GTE2048, RMON_R_OCTETS, 2227db65f35fSPhilippe Reynes IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR, 2228db65f35fSPhilippe Reynes IEEE_R_FDXFC, IEEE_R_OCTETS_OK 2229db65f35fSPhilippe Reynes }; 2230db65f35fSPhilippe Reynes #else 2231f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 1; 2232db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = { 2233db65f35fSPhilippe Reynes FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0, 2234db65f35fSPhilippe Reynes FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0, 2235db65f35fSPhilippe Reynes FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED, 2236db65f35fSPhilippe Reynes FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL, 2237db65f35fSPhilippe Reynes FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, 2238db65f35fSPhilippe Reynes FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0, 2239db65f35fSPhilippe Reynes FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0, 2240db65f35fSPhilippe Reynes FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0, 2241db65f35fSPhilippe Reynes FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2 2242db65f35fSPhilippe Reynes }; 2243db65f35fSPhilippe Reynes #endif 2244db65f35fSPhilippe Reynes 2245db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev, 2246db65f35fSPhilippe Reynes struct ethtool_regs *regs, void *regbuf) 2247db65f35fSPhilippe Reynes { 2248db65f35fSPhilippe Reynes struct fec_enet_private *fep = netdev_priv(ndev); 2249db65f35fSPhilippe Reynes u32 __iomem *theregs = (u32 __iomem *)fep->hwp; 2250c72a0bc0SAndrew Lunn struct device *dev = &fep->pdev->dev; 2251db65f35fSPhilippe Reynes u32 *buf = (u32 *)regbuf; 2252db65f35fSPhilippe Reynes u32 i, off; 2253c72a0bc0SAndrew Lunn int ret; 2254c72a0bc0SAndrew Lunn 2255c72a0bc0SAndrew Lunn ret = pm_runtime_get_sync(dev); 2256c72a0bc0SAndrew Lunn if (ret < 0) 2257c72a0bc0SAndrew Lunn return; 2258db65f35fSPhilippe Reynes 2259f9bcc9f3SVivien Didelot regs->version = fec_enet_register_version; 2260f9bcc9f3SVivien Didelot 2261db65f35fSPhilippe Reynes memset(buf, 0, regs->len); 2262db65f35fSPhilippe Reynes 2263db65f35fSPhilippe Reynes for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) { 2264ec20a63aSFugang Duan off = fec_enet_register_offset[i]; 2265ec20a63aSFugang Duan 2266ec20a63aSFugang Duan if ((off == FEC_R_BOUND || off == FEC_R_FSTART) && 2267ec20a63aSFugang Duan !(fep->quirks & FEC_QUIRK_HAS_FRREG)) 2268ec20a63aSFugang Duan continue; 2269ec20a63aSFugang Duan 2270ec20a63aSFugang Duan off >>= 2; 2271db65f35fSPhilippe Reynes buf[off] = readl(&theregs[off]); 2272db65f35fSPhilippe Reynes } 2273c72a0bc0SAndrew Lunn 2274c72a0bc0SAndrew Lunn pm_runtime_mark_last_busy(dev); 2275c72a0bc0SAndrew Lunn pm_runtime_put_autosuspend(dev); 2276db65f35fSPhilippe Reynes } 2277db65f35fSPhilippe Reynes 2278793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev, 2279793fc096SFrank Li struct ethtool_ts_info *info) 2280793fc096SFrank Li { 2281793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2282793fc096SFrank Li 2283793fc096SFrank Li if (fep->bufdesc_ex) { 2284793fc096SFrank Li 2285793fc096SFrank Li info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 2286793fc096SFrank Li SOF_TIMESTAMPING_RX_SOFTWARE | 2287793fc096SFrank Li SOF_TIMESTAMPING_SOFTWARE | 2288793fc096SFrank Li SOF_TIMESTAMPING_TX_HARDWARE | 2289793fc096SFrank Li SOF_TIMESTAMPING_RX_HARDWARE | 2290793fc096SFrank Li SOF_TIMESTAMPING_RAW_HARDWARE; 2291793fc096SFrank Li if (fep->ptp_clock) 2292793fc096SFrank Li info->phc_index = ptp_clock_index(fep->ptp_clock); 2293793fc096SFrank Li else 2294793fc096SFrank Li info->phc_index = -1; 2295793fc096SFrank Li 2296793fc096SFrank Li info->tx_types = (1 << HWTSTAMP_TX_OFF) | 2297793fc096SFrank Li (1 << HWTSTAMP_TX_ON); 2298793fc096SFrank Li 2299793fc096SFrank Li info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 2300793fc096SFrank Li (1 << HWTSTAMP_FILTER_ALL); 2301793fc096SFrank Li return 0; 2302793fc096SFrank Li } else { 2303793fc096SFrank Li return ethtool_op_get_ts_info(ndev, info); 2304793fc096SFrank Li } 2305793fc096SFrank Li } 2306793fc096SFrank Li 2307d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 2308d1391930SGuenter Roeck 2309793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev, 2310793fc096SFrank Li struct ethtool_pauseparam *pause) 2311793fc096SFrank Li { 2312793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2313793fc096SFrank Li 2314793fc096SFrank Li pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; 2315793fc096SFrank Li pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; 2316793fc096SFrank Li pause->rx_pause = pause->tx_pause; 2317793fc096SFrank Li } 2318793fc096SFrank Li 2319793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev, 2320793fc096SFrank Li struct ethtool_pauseparam *pause) 2321793fc096SFrank Li { 2322793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2323793fc096SFrank Li 232445f5c327SPhilippe Reynes if (!ndev->phydev) 23250b146ca8SRussell King return -ENODEV; 23260b146ca8SRussell King 2327793fc096SFrank Li if (pause->tx_pause != pause->rx_pause) { 2328793fc096SFrank Li netdev_info(ndev, 2329793fc096SFrank Li "hardware only support enable/disable both tx and rx"); 2330793fc096SFrank Li return -EINVAL; 2331793fc096SFrank Li } 2332793fc096SFrank Li 2333793fc096SFrank Li fep->pause_flag = 0; 2334793fc096SFrank Li 2335793fc096SFrank Li /* tx pause must be same as rx pause */ 2336793fc096SFrank Li fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; 2337793fc096SFrank Li fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; 2338793fc096SFrank Li 23390c122405SAndrew Lunn phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause, 23400c122405SAndrew Lunn pause->autoneg); 2341793fc096SFrank Li 2342793fc096SFrank Li if (pause->autoneg) { 2343793fc096SFrank Li if (netif_running(ndev)) 2344793fc096SFrank Li fec_stop(ndev); 234545f5c327SPhilippe Reynes phy_start_aneg(ndev->phydev); 2346793fc096SFrank Li } 2347dbc64a8eSRussell King if (netif_running(ndev)) { 2348dbc64a8eSRussell King napi_disable(&fep->napi); 2349dbc64a8eSRussell King netif_tx_lock_bh(ndev); 2350ef83337dSRussell King fec_restart(ndev); 2351657ade07SRickard x Andersson netif_tx_wake_all_queues(ndev); 23526af42d42SRussell King netif_tx_unlock_bh(ndev); 2353dbc64a8eSRussell King napi_enable(&fep->napi); 2354dbc64a8eSRussell King } 2355793fc096SFrank Li 2356793fc096SFrank Li return 0; 2357793fc096SFrank Li } 2358793fc096SFrank Li 235938ae92dcSChris Healy static const struct fec_stat { 236038ae92dcSChris Healy char name[ETH_GSTRING_LEN]; 236138ae92dcSChris Healy u16 offset; 236238ae92dcSChris Healy } fec_stats[] = { 236338ae92dcSChris Healy /* RMON TX */ 236438ae92dcSChris Healy { "tx_dropped", RMON_T_DROP }, 236538ae92dcSChris Healy { "tx_packets", RMON_T_PACKETS }, 236638ae92dcSChris Healy { "tx_broadcast", RMON_T_BC_PKT }, 236738ae92dcSChris Healy { "tx_multicast", RMON_T_MC_PKT }, 236838ae92dcSChris Healy { "tx_crc_errors", RMON_T_CRC_ALIGN }, 236938ae92dcSChris Healy { "tx_undersize", RMON_T_UNDERSIZE }, 237038ae92dcSChris Healy { "tx_oversize", RMON_T_OVERSIZE }, 237138ae92dcSChris Healy { "tx_fragment", RMON_T_FRAG }, 237238ae92dcSChris Healy { "tx_jabber", RMON_T_JAB }, 237338ae92dcSChris Healy { "tx_collision", RMON_T_COL }, 237438ae92dcSChris Healy { "tx_64byte", RMON_T_P64 }, 237538ae92dcSChris Healy { "tx_65to127byte", RMON_T_P65TO127 }, 237638ae92dcSChris Healy { "tx_128to255byte", RMON_T_P128TO255 }, 237738ae92dcSChris Healy { "tx_256to511byte", RMON_T_P256TO511 }, 237838ae92dcSChris Healy { "tx_512to1023byte", RMON_T_P512TO1023 }, 237938ae92dcSChris Healy { "tx_1024to2047byte", RMON_T_P1024TO2047 }, 238038ae92dcSChris Healy { "tx_GTE2048byte", RMON_T_P_GTE2048 }, 238138ae92dcSChris Healy { "tx_octets", RMON_T_OCTETS }, 238238ae92dcSChris Healy 238338ae92dcSChris Healy /* IEEE TX */ 238438ae92dcSChris Healy { "IEEE_tx_drop", IEEE_T_DROP }, 238538ae92dcSChris Healy { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK }, 238638ae92dcSChris Healy { "IEEE_tx_1col", IEEE_T_1COL }, 238738ae92dcSChris Healy { "IEEE_tx_mcol", IEEE_T_MCOL }, 238838ae92dcSChris Healy { "IEEE_tx_def", IEEE_T_DEF }, 238938ae92dcSChris Healy { "IEEE_tx_lcol", IEEE_T_LCOL }, 239038ae92dcSChris Healy { "IEEE_tx_excol", IEEE_T_EXCOL }, 239138ae92dcSChris Healy { "IEEE_tx_macerr", IEEE_T_MACERR }, 239238ae92dcSChris Healy { "IEEE_tx_cserr", IEEE_T_CSERR }, 239338ae92dcSChris Healy { "IEEE_tx_sqe", IEEE_T_SQE }, 239438ae92dcSChris Healy { "IEEE_tx_fdxfc", IEEE_T_FDXFC }, 239538ae92dcSChris Healy { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK }, 239638ae92dcSChris Healy 239738ae92dcSChris Healy /* RMON RX */ 239838ae92dcSChris Healy { "rx_packets", RMON_R_PACKETS }, 239938ae92dcSChris Healy { "rx_broadcast", RMON_R_BC_PKT }, 240038ae92dcSChris Healy { "rx_multicast", RMON_R_MC_PKT }, 240138ae92dcSChris Healy { "rx_crc_errors", RMON_R_CRC_ALIGN }, 240238ae92dcSChris Healy { "rx_undersize", RMON_R_UNDERSIZE }, 240338ae92dcSChris Healy { "rx_oversize", RMON_R_OVERSIZE }, 240438ae92dcSChris Healy { "rx_fragment", RMON_R_FRAG }, 240538ae92dcSChris Healy { "rx_jabber", RMON_R_JAB }, 240638ae92dcSChris Healy { "rx_64byte", RMON_R_P64 }, 240738ae92dcSChris Healy { "rx_65to127byte", RMON_R_P65TO127 }, 240838ae92dcSChris Healy { "rx_128to255byte", RMON_R_P128TO255 }, 240938ae92dcSChris Healy { "rx_256to511byte", RMON_R_P256TO511 }, 241038ae92dcSChris Healy { "rx_512to1023byte", RMON_R_P512TO1023 }, 241138ae92dcSChris Healy { "rx_1024to2047byte", RMON_R_P1024TO2047 }, 241238ae92dcSChris Healy { "rx_GTE2048byte", RMON_R_P_GTE2048 }, 241338ae92dcSChris Healy { "rx_octets", RMON_R_OCTETS }, 241438ae92dcSChris Healy 241538ae92dcSChris Healy /* IEEE RX */ 241638ae92dcSChris Healy { "IEEE_rx_drop", IEEE_R_DROP }, 241738ae92dcSChris Healy { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK }, 241838ae92dcSChris Healy { "IEEE_rx_crc", IEEE_R_CRC }, 241938ae92dcSChris Healy { "IEEE_rx_align", IEEE_R_ALIGN }, 242038ae92dcSChris Healy { "IEEE_rx_macerr", IEEE_R_MACERR }, 242138ae92dcSChris Healy { "IEEE_rx_fdxfc", IEEE_R_FDXFC }, 242238ae92dcSChris Healy { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK }, 242338ae92dcSChris Healy }; 242438ae92dcSChris Healy 2425f85de666SNikita Yushchenko #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64)) 2426f85de666SNikita Yushchenko 242780cca775SNikita Yushchenko static void fec_enet_update_ethtool_stats(struct net_device *dev) 242838ae92dcSChris Healy { 242938ae92dcSChris Healy struct fec_enet_private *fep = netdev_priv(dev); 243038ae92dcSChris Healy int i; 243138ae92dcSChris Healy 243238ae92dcSChris Healy for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 243380cca775SNikita Yushchenko fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset); 243480cca775SNikita Yushchenko } 243580cca775SNikita Yushchenko 243680cca775SNikita Yushchenko static void fec_enet_get_ethtool_stats(struct net_device *dev, 243780cca775SNikita Yushchenko struct ethtool_stats *stats, u64 *data) 243880cca775SNikita Yushchenko { 243980cca775SNikita Yushchenko struct fec_enet_private *fep = netdev_priv(dev); 244080cca775SNikita Yushchenko 244180cca775SNikita Yushchenko if (netif_running(dev)) 244280cca775SNikita Yushchenko fec_enet_update_ethtool_stats(dev); 244380cca775SNikita Yushchenko 2444f85de666SNikita Yushchenko memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE); 244538ae92dcSChris Healy } 244638ae92dcSChris Healy 244738ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev, 244838ae92dcSChris Healy u32 stringset, u8 *data) 244938ae92dcSChris Healy { 245038ae92dcSChris Healy int i; 245138ae92dcSChris Healy switch (stringset) { 245238ae92dcSChris Healy case ETH_SS_STATS: 245338ae92dcSChris Healy for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 245438ae92dcSChris Healy memcpy(data + i * ETH_GSTRING_LEN, 245538ae92dcSChris Healy fec_stats[i].name, ETH_GSTRING_LEN); 245638ae92dcSChris Healy break; 245738ae92dcSChris Healy } 245838ae92dcSChris Healy } 245938ae92dcSChris Healy 246038ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset) 246138ae92dcSChris Healy { 246238ae92dcSChris Healy switch (sset) { 246338ae92dcSChris Healy case ETH_SS_STATS: 246438ae92dcSChris Healy return ARRAY_SIZE(fec_stats); 246538ae92dcSChris Healy default: 246638ae92dcSChris Healy return -EOPNOTSUPP; 246738ae92dcSChris Healy } 246838ae92dcSChris Healy } 2469f85de666SNikita Yushchenko 24702b30842bSAndrew Lunn static void fec_enet_clear_ethtool_stats(struct net_device *dev) 24712b30842bSAndrew Lunn { 24722b30842bSAndrew Lunn struct fec_enet_private *fep = netdev_priv(dev); 24732b30842bSAndrew Lunn int i; 24742b30842bSAndrew Lunn 24752b30842bSAndrew Lunn /* Disable MIB statistics counters */ 24762b30842bSAndrew Lunn writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT); 24772b30842bSAndrew Lunn 24782b30842bSAndrew Lunn for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 24792b30842bSAndrew Lunn writel(0, fep->hwp + fec_stats[i].offset); 24802b30842bSAndrew Lunn 24812b30842bSAndrew Lunn /* Don't disable MIB statistics counters */ 24822b30842bSAndrew Lunn writel(0, fep->hwp + FEC_MIB_CTRLSTAT); 24832b30842bSAndrew Lunn } 24842b30842bSAndrew Lunn 2485f85de666SNikita Yushchenko #else /* !defined(CONFIG_M5272) */ 2486f85de666SNikita Yushchenko #define FEC_STATS_SIZE 0 2487f85de666SNikita Yushchenko static inline void fec_enet_update_ethtool_stats(struct net_device *dev) 2488f85de666SNikita Yushchenko { 2489f85de666SNikita Yushchenko } 249041e8e404SFabio Estevam 249141e8e404SFabio Estevam static inline void fec_enet_clear_ethtool_stats(struct net_device *dev) 249241e8e404SFabio Estevam { 249341e8e404SFabio Estevam } 2494d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */ 249538ae92dcSChris Healy 2496d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb). 2497d851b47bSFugang Duan * TCTT unit is cycle_ns * 64 cycle 2498d851b47bSFugang Duan * So, the ICTT value = X us / (cycle_ns * 64) 2499d851b47bSFugang Duan */ 2500d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us) 2501d851b47bSFugang Duan { 2502d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2503d851b47bSFugang Duan 2504d851b47bSFugang Duan return us * (fep->itr_clk_rate / 64000) / 1000; 2505d851b47bSFugang Duan } 2506d851b47bSFugang Duan 2507d851b47bSFugang Duan /* Set threshold for interrupt coalescing */ 2508d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev) 2509d851b47bSFugang Duan { 2510d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2511d851b47bSFugang Duan int rx_itr, tx_itr; 2512d851b47bSFugang Duan 2513d851b47bSFugang Duan /* Must be greater than zero to avoid unpredictable behavior */ 2514d851b47bSFugang Duan if (!fep->rx_time_itr || !fep->rx_pkts_itr || 2515d851b47bSFugang Duan !fep->tx_time_itr || !fep->tx_pkts_itr) 2516d851b47bSFugang Duan return; 2517d851b47bSFugang Duan 2518d851b47bSFugang Duan /* Select enet system clock as Interrupt Coalescing 2519d851b47bSFugang Duan * timer Clock Source 2520d851b47bSFugang Duan */ 2521d851b47bSFugang Duan rx_itr = FEC_ITR_CLK_SEL; 2522d851b47bSFugang Duan tx_itr = FEC_ITR_CLK_SEL; 2523d851b47bSFugang Duan 2524d851b47bSFugang Duan /* set ICFT and ICTT */ 2525d851b47bSFugang Duan rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr); 2526d851b47bSFugang Duan rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr)); 2527d851b47bSFugang Duan tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr); 2528d851b47bSFugang Duan tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr)); 2529d851b47bSFugang Duan 2530d851b47bSFugang Duan rx_itr |= FEC_ITR_EN; 2531d851b47bSFugang Duan tx_itr |= FEC_ITR_EN; 2532d851b47bSFugang Duan 2533d851b47bSFugang Duan writel(tx_itr, fep->hwp + FEC_TXIC0); 2534d851b47bSFugang Duan writel(rx_itr, fep->hwp + FEC_RXIC0); 2535ff7566b8SFugang Duan if (fep->quirks & FEC_QUIRK_HAS_AVB) { 2536d851b47bSFugang Duan writel(tx_itr, fep->hwp + FEC_TXIC1); 2537d851b47bSFugang Duan writel(rx_itr, fep->hwp + FEC_RXIC1); 2538d851b47bSFugang Duan writel(tx_itr, fep->hwp + FEC_TXIC2); 2539d851b47bSFugang Duan writel(rx_itr, fep->hwp + FEC_RXIC2); 2540d851b47bSFugang Duan } 2541ff7566b8SFugang Duan } 2542d851b47bSFugang Duan 2543d851b47bSFugang Duan static int 2544d851b47bSFugang Duan fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec) 2545d851b47bSFugang Duan { 2546d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2547d851b47bSFugang Duan 2548ff7566b8SFugang Duan if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 2549d851b47bSFugang Duan return -EOPNOTSUPP; 2550d851b47bSFugang Duan 2551d851b47bSFugang Duan ec->rx_coalesce_usecs = fep->rx_time_itr; 2552d851b47bSFugang Duan ec->rx_max_coalesced_frames = fep->rx_pkts_itr; 2553d851b47bSFugang Duan 2554d851b47bSFugang Duan ec->tx_coalesce_usecs = fep->tx_time_itr; 2555d851b47bSFugang Duan ec->tx_max_coalesced_frames = fep->tx_pkts_itr; 2556d851b47bSFugang Duan 2557d851b47bSFugang Duan return 0; 2558d851b47bSFugang Duan } 2559d851b47bSFugang Duan 2560d851b47bSFugang Duan static int 2561d851b47bSFugang Duan fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec) 2562d851b47bSFugang Duan { 2563d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2564517a772cSFabio Estevam struct device *dev = &fep->pdev->dev; 2565d851b47bSFugang Duan unsigned int cycle; 2566d851b47bSFugang Duan 2567ff7566b8SFugang Duan if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 2568d851b47bSFugang Duan return -EOPNOTSUPP; 2569d851b47bSFugang Duan 2570d851b47bSFugang Duan if (ec->rx_max_coalesced_frames > 255) { 2571517a772cSFabio Estevam dev_err(dev, "Rx coalesced frames exceed hardware limitation\n"); 2572d851b47bSFugang Duan return -EINVAL; 2573d851b47bSFugang Duan } 2574d851b47bSFugang Duan 2575d851b47bSFugang Duan if (ec->tx_max_coalesced_frames > 255) { 2576517a772cSFabio Estevam dev_err(dev, "Tx coalesced frame exceed hardware limitation\n"); 2577d851b47bSFugang Duan return -EINVAL; 2578d851b47bSFugang Duan } 2579d851b47bSFugang Duan 2580ab14961dSJakub Kicinski cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs); 2581d851b47bSFugang Duan if (cycle > 0xFFFF) { 2582517a772cSFabio Estevam dev_err(dev, "Rx coalesced usec exceed hardware limitation\n"); 2583d851b47bSFugang Duan return -EINVAL; 2584d851b47bSFugang Duan } 2585d851b47bSFugang Duan 2586ab14961dSJakub Kicinski cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs); 2587d851b47bSFugang Duan if (cycle > 0xFFFF) { 2588ab14961dSJakub Kicinski dev_err(dev, "Tx coalesced usec exceed hardware limitation\n"); 2589d851b47bSFugang Duan return -EINVAL; 2590d851b47bSFugang Duan } 2591d851b47bSFugang Duan 2592d851b47bSFugang Duan fep->rx_time_itr = ec->rx_coalesce_usecs; 2593d851b47bSFugang Duan fep->rx_pkts_itr = ec->rx_max_coalesced_frames; 2594d851b47bSFugang Duan 2595d851b47bSFugang Duan fep->tx_time_itr = ec->tx_coalesce_usecs; 2596d851b47bSFugang Duan fep->tx_pkts_itr = ec->tx_max_coalesced_frames; 2597d851b47bSFugang Duan 2598d851b47bSFugang Duan fec_enet_itr_coal_set(ndev); 2599d851b47bSFugang Duan 2600d851b47bSFugang Duan return 0; 2601d851b47bSFugang Duan } 2602d851b47bSFugang Duan 2603d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev) 2604d851b47bSFugang Duan { 2605d851b47bSFugang Duan struct ethtool_coalesce ec; 2606d851b47bSFugang Duan 2607d851b47bSFugang Duan ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT; 2608d851b47bSFugang Duan ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT; 2609d851b47bSFugang Duan 2610d851b47bSFugang Duan ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT; 2611d851b47bSFugang Duan ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT; 2612d851b47bSFugang Duan 2613d851b47bSFugang Duan fec_enet_set_coalesce(ndev, &ec); 2614d851b47bSFugang Duan } 2615d851b47bSFugang Duan 26161b7bde6dSNimrod Andy static int fec_enet_get_tunable(struct net_device *netdev, 26171b7bde6dSNimrod Andy const struct ethtool_tunable *tuna, 26181b7bde6dSNimrod Andy void *data) 26191b7bde6dSNimrod Andy { 26201b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(netdev); 26211b7bde6dSNimrod Andy int ret = 0; 26221b7bde6dSNimrod Andy 26231b7bde6dSNimrod Andy switch (tuna->id) { 26241b7bde6dSNimrod Andy case ETHTOOL_RX_COPYBREAK: 26251b7bde6dSNimrod Andy *(u32 *)data = fep->rx_copybreak; 26261b7bde6dSNimrod Andy break; 26271b7bde6dSNimrod Andy default: 26281b7bde6dSNimrod Andy ret = -EINVAL; 26291b7bde6dSNimrod Andy break; 26301b7bde6dSNimrod Andy } 26311b7bde6dSNimrod Andy 26321b7bde6dSNimrod Andy return ret; 26331b7bde6dSNimrod Andy } 26341b7bde6dSNimrod Andy 26351b7bde6dSNimrod Andy static int fec_enet_set_tunable(struct net_device *netdev, 26361b7bde6dSNimrod Andy const struct ethtool_tunable *tuna, 26371b7bde6dSNimrod Andy const void *data) 26381b7bde6dSNimrod Andy { 26391b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(netdev); 26401b7bde6dSNimrod Andy int ret = 0; 26411b7bde6dSNimrod Andy 26421b7bde6dSNimrod Andy switch (tuna->id) { 26431b7bde6dSNimrod Andy case ETHTOOL_RX_COPYBREAK: 26441b7bde6dSNimrod Andy fep->rx_copybreak = *(u32 *)data; 26451b7bde6dSNimrod Andy break; 26461b7bde6dSNimrod Andy default: 26471b7bde6dSNimrod Andy ret = -EINVAL; 26481b7bde6dSNimrod Andy break; 26491b7bde6dSNimrod Andy } 26501b7bde6dSNimrod Andy 26511b7bde6dSNimrod Andy return ret; 26521b7bde6dSNimrod Andy } 26531b7bde6dSNimrod Andy 2654de40ed31SNimrod Andy static void 2655de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2656de40ed31SNimrod Andy { 2657de40ed31SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 2658de40ed31SNimrod Andy 2659de40ed31SNimrod Andy if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) { 2660de40ed31SNimrod Andy wol->supported = WAKE_MAGIC; 2661de40ed31SNimrod Andy wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0; 2662de40ed31SNimrod Andy } else { 2663de40ed31SNimrod Andy wol->supported = wol->wolopts = 0; 2664de40ed31SNimrod Andy } 2665de40ed31SNimrod Andy } 2666de40ed31SNimrod Andy 2667de40ed31SNimrod Andy static int 2668de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2669de40ed31SNimrod Andy { 2670de40ed31SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 2671de40ed31SNimrod Andy 2672de40ed31SNimrod Andy if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET)) 2673de40ed31SNimrod Andy return -EINVAL; 2674de40ed31SNimrod Andy 2675de40ed31SNimrod Andy if (wol->wolopts & ~WAKE_MAGIC) 2676de40ed31SNimrod Andy return -EINVAL; 2677de40ed31SNimrod Andy 2678de40ed31SNimrod Andy device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC); 2679de40ed31SNimrod Andy if (device_may_wakeup(&ndev->dev)) { 2680de40ed31SNimrod Andy fep->wol_flag |= FEC_WOL_FLAG_ENABLE; 2681de40ed31SNimrod Andy if (fep->irq[0] > 0) 2682de40ed31SNimrod Andy enable_irq_wake(fep->irq[0]); 2683de40ed31SNimrod Andy } else { 2684de40ed31SNimrod Andy fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE); 2685de40ed31SNimrod Andy if (fep->irq[0] > 0) 2686de40ed31SNimrod Andy disable_irq_wake(fep->irq[0]); 2687de40ed31SNimrod Andy } 2688de40ed31SNimrod Andy 2689de40ed31SNimrod Andy return 0; 2690de40ed31SNimrod Andy } 2691de40ed31SNimrod Andy 2692793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = { 2693d5e3c87dSJakub Kicinski .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 2694d5e3c87dSJakub Kicinski ETHTOOL_COALESCE_MAX_FRAMES, 2695793fc096SFrank Li .get_drvinfo = fec_enet_get_drvinfo, 2696db65f35fSPhilippe Reynes .get_regs_len = fec_enet_get_regs_len, 2697db65f35fSPhilippe Reynes .get_regs = fec_enet_get_regs, 269811d59289SFlorian Fainelli .nway_reset = phy_ethtool_nway_reset, 2699c1d7c48fSRussell King .get_link = ethtool_op_get_link, 2700d851b47bSFugang Duan .get_coalesce = fec_enet_get_coalesce, 2701d851b47bSFugang Duan .set_coalesce = fec_enet_set_coalesce, 270238ae92dcSChris Healy #ifndef CONFIG_M5272 2703c1d7c48fSRussell King .get_pauseparam = fec_enet_get_pauseparam, 2704c1d7c48fSRussell King .set_pauseparam = fec_enet_set_pauseparam, 270538ae92dcSChris Healy .get_strings = fec_enet_get_strings, 2706c1d7c48fSRussell King .get_ethtool_stats = fec_enet_get_ethtool_stats, 270738ae92dcSChris Healy .get_sset_count = fec_enet_get_sset_count, 270838ae92dcSChris Healy #endif 2709c1d7c48fSRussell King .get_ts_info = fec_enet_get_ts_info, 27101b7bde6dSNimrod Andy .get_tunable = fec_enet_get_tunable, 27111b7bde6dSNimrod Andy .set_tunable = fec_enet_set_tunable, 2712de40ed31SNimrod Andy .get_wol = fec_enet_get_wol, 2713de40ed31SNimrod Andy .set_wol = fec_enet_set_wol, 27149365fbf5SPhilippe Reynes .get_link_ksettings = phy_ethtool_get_link_ksettings, 27159365fbf5SPhilippe Reynes .set_link_ksettings = phy_ethtool_set_link_ksettings, 2716793fc096SFrank Li }; 2717793fc096SFrank Li 2718793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 2719793fc096SFrank Li { 2720793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 272145f5c327SPhilippe Reynes struct phy_device *phydev = ndev->phydev; 2722793fc096SFrank Li 2723793fc096SFrank Li if (!netif_running(ndev)) 2724793fc096SFrank Li return -EINVAL; 2725793fc096SFrank Li 2726793fc096SFrank Li if (!phydev) 2727793fc096SFrank Li return -ENODEV; 2728793fc096SFrank Li 27291d5244d0SBen Hutchings if (fep->bufdesc_ex) { 273034074639SSergey Organov bool use_fec_hwts = !phy_has_hwtstamp(phydev); 273134074639SSergey Organov 273234074639SSergey Organov if (cmd == SIOCSHWTSTAMP) { 273334074639SSergey Organov if (use_fec_hwts) 27341d5244d0SBen Hutchings return fec_ptp_set(ndev, rq); 273534074639SSergey Organov fec_ptp_disable_hwts(ndev); 273634074639SSergey Organov } else if (cmd == SIOCGHWTSTAMP) { 273734074639SSergey Organov if (use_fec_hwts) 27381d5244d0SBen Hutchings return fec_ptp_get(ndev, rq); 27391d5244d0SBen Hutchings } 274034074639SSergey Organov } 2741793fc096SFrank Li 2742793fc096SFrank Li return phy_mii_ioctl(phydev, rq, cmd); 2743793fc096SFrank Li } 2744793fc096SFrank Li 2745793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev) 2746793fc096SFrank Li { 2747793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2748793fc096SFrank Li unsigned int i; 2749793fc096SFrank Li struct sk_buff *skb; 2750793fc096SFrank Li struct bufdesc *bdp; 27514d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 27524d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 275359d0f746SFrank Li unsigned int q; 2754793fc096SFrank Li 275559d0f746SFrank Li for (q = 0; q < fep->num_rx_queues; q++) { 275659d0f746SFrank Li rxq = fep->rx_queue[q]; 27577355f276STroy Kisky bdp = rxq->bd.base; 27587355f276STroy Kisky for (i = 0; i < rxq->bd.ring_size; i++) { 27594d494cdcSFugang Duan skb = rxq->rx_skbuff[i]; 27604d494cdcSFugang Duan rxq->rx_skbuff[i] = NULL; 2761730ee360SRussell King if (skb) { 276259d0f746SFrank Li dma_unmap_single(&fep->pdev->dev, 27635cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 2764b64bf4b7SNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 276559d0f746SFrank Li DMA_FROM_DEVICE); 2766793fc096SFrank Li dev_kfree_skb(skb); 2767730ee360SRussell King } 27687355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 276959d0f746SFrank Li } 2770793fc096SFrank Li } 2771793fc096SFrank Li 277259d0f746SFrank Li for (q = 0; q < fep->num_tx_queues; q++) { 277359d0f746SFrank Li txq = fep->tx_queue[q]; 27747355f276STroy Kisky for (i = 0; i < txq->bd.ring_size; i++) { 27754d494cdcSFugang Duan kfree(txq->tx_bounce[i]); 27764d494cdcSFugang Duan txq->tx_bounce[i] = NULL; 27774d494cdcSFugang Duan skb = txq->tx_skbuff[i]; 27784d494cdcSFugang Duan txq->tx_skbuff[i] = NULL; 27798b7c9efaSRussell King dev_kfree_skb(skb); 27808b7c9efaSRussell King } 2781793fc096SFrank Li } 278259d0f746SFrank Li } 2783793fc096SFrank Li 278459d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev) 278559d0f746SFrank Li { 278659d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 278759d0f746SFrank Li int i; 278859d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 278959d0f746SFrank Li 279059d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) 279159d0f746SFrank Li if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) { 279259d0f746SFrank Li txq = fep->tx_queue[i]; 279394920128SFugang Duan dma_free_coherent(&fep->pdev->dev, 27947355f276STroy Kisky txq->bd.ring_size * TSO_HEADER_SIZE, 279559d0f746SFrank Li txq->tso_hdrs, 279659d0f746SFrank Li txq->tso_hdrs_dma); 279759d0f746SFrank Li } 279859d0f746SFrank Li 279959d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) 280059d0f746SFrank Li kfree(fep->rx_queue[i]); 280159d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) 280259d0f746SFrank Li kfree(fep->tx_queue[i]); 280359d0f746SFrank Li } 280459d0f746SFrank Li 280559d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev) 280659d0f746SFrank Li { 280759d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 280859d0f746SFrank Li int i; 280959d0f746SFrank Li int ret = 0; 281059d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 281159d0f746SFrank Li 281259d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 281359d0f746SFrank Li txq = kzalloc(sizeof(*txq), GFP_KERNEL); 281459d0f746SFrank Li if (!txq) { 281559d0f746SFrank Li ret = -ENOMEM; 281659d0f746SFrank Li goto alloc_failed; 281759d0f746SFrank Li } 281859d0f746SFrank Li 281959d0f746SFrank Li fep->tx_queue[i] = txq; 28207355f276STroy Kisky txq->bd.ring_size = TX_RING_SIZE; 28217355f276STroy Kisky fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size; 282259d0f746SFrank Li 282359d0f746SFrank Li txq->tx_stop_threshold = FEC_MAX_SKB_DESCS; 282459d0f746SFrank Li txq->tx_wake_threshold = 28257355f276STroy Kisky (txq->bd.ring_size - txq->tx_stop_threshold) / 2; 282659d0f746SFrank Li 282794920128SFugang Duan txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev, 28287355f276STroy Kisky txq->bd.ring_size * TSO_HEADER_SIZE, 282959d0f746SFrank Li &txq->tso_hdrs_dma, 283059d0f746SFrank Li GFP_KERNEL); 283159d0f746SFrank Li if (!txq->tso_hdrs) { 283259d0f746SFrank Li ret = -ENOMEM; 283359d0f746SFrank Li goto alloc_failed; 283459d0f746SFrank Li } 283559d0f746SFrank Li } 283659d0f746SFrank Li 283759d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) { 283859d0f746SFrank Li fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]), 283959d0f746SFrank Li GFP_KERNEL); 284059d0f746SFrank Li if (!fep->rx_queue[i]) { 284159d0f746SFrank Li ret = -ENOMEM; 284259d0f746SFrank Li goto alloc_failed; 284359d0f746SFrank Li } 284459d0f746SFrank Li 28457355f276STroy Kisky fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE; 28467355f276STroy Kisky fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size; 284759d0f746SFrank Li } 284859d0f746SFrank Li return ret; 284959d0f746SFrank Li 285059d0f746SFrank Li alloc_failed: 285159d0f746SFrank Li fec_enet_free_queue(ndev); 285259d0f746SFrank Li return ret; 285359d0f746SFrank Li } 285459d0f746SFrank Li 285559d0f746SFrank Li static int 285659d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue) 2857793fc096SFrank Li { 2858793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2859793fc096SFrank Li unsigned int i; 2860793fc096SFrank Li struct sk_buff *skb; 2861793fc096SFrank Li struct bufdesc *bdp; 28624d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 2863793fc096SFrank Li 286459d0f746SFrank Li rxq = fep->rx_queue[queue]; 28657355f276STroy Kisky bdp = rxq->bd.base; 28667355f276STroy Kisky for (i = 0; i < rxq->bd.ring_size; i++) { 2867793fc096SFrank Li skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); 2868ffdce2ccSRussell King if (!skb) 2869ffdce2ccSRussell King goto err_alloc; 2870793fc096SFrank Li 28711b7bde6dSNimrod Andy if (fec_enet_new_rxbdp(ndev, bdp, skb)) { 2872730ee360SRussell King dev_kfree_skb(skb); 2873ffdce2ccSRussell King goto err_alloc; 2874d842a31fSDuan Fugang-B38611 } 2875730ee360SRussell King 28764d494cdcSFugang Duan rxq->rx_skbuff[i] = skb; 28775cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 2878793fc096SFrank Li 2879793fc096SFrank Li if (fep->bufdesc_ex) { 2880793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 28815cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 2882793fc096SFrank Li } 2883793fc096SFrank Li 28847355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 2885793fc096SFrank Li } 2886793fc096SFrank Li 2887793fc096SFrank Li /* Set the last buffer to wrap. */ 28887355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 28895cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 289059d0f746SFrank Li return 0; 2891793fc096SFrank Li 289259d0f746SFrank Li err_alloc: 289359d0f746SFrank Li fec_enet_free_buffers(ndev); 289459d0f746SFrank Li return -ENOMEM; 289559d0f746SFrank Li } 289659d0f746SFrank Li 289759d0f746SFrank Li static int 289859d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue) 289959d0f746SFrank Li { 290059d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 290159d0f746SFrank Li unsigned int i; 290259d0f746SFrank Li struct bufdesc *bdp; 290359d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 290459d0f746SFrank Li 290559d0f746SFrank Li txq = fep->tx_queue[queue]; 29067355f276STroy Kisky bdp = txq->bd.base; 29077355f276STroy Kisky for (i = 0; i < txq->bd.ring_size; i++) { 29084d494cdcSFugang Duan txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); 29094d494cdcSFugang Duan if (!txq->tx_bounce[i]) 2910ffdce2ccSRussell King goto err_alloc; 2911793fc096SFrank Li 29125cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(0); 29135cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(0); 2914793fc096SFrank Li 2915793fc096SFrank Li if (fep->bufdesc_ex) { 2916793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 29175cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT); 2918793fc096SFrank Li } 2919793fc096SFrank Li 29207355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 2921793fc096SFrank Li } 2922793fc096SFrank Li 2923793fc096SFrank Li /* Set the last buffer to wrap. */ 29247355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 29255cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 2926793fc096SFrank Li 2927793fc096SFrank Li return 0; 2928ffdce2ccSRussell King 2929ffdce2ccSRussell King err_alloc: 2930ffdce2ccSRussell King fec_enet_free_buffers(ndev); 2931ffdce2ccSRussell King return -ENOMEM; 2932793fc096SFrank Li } 2933793fc096SFrank Li 293459d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev) 293559d0f746SFrank Li { 293659d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 293759d0f746SFrank Li unsigned int i; 293859d0f746SFrank Li 293959d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) 294059d0f746SFrank Li if (fec_enet_alloc_rxq_buffers(ndev, i)) 294159d0f746SFrank Li return -ENOMEM; 294259d0f746SFrank Li 294359d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) 294459d0f746SFrank Li if (fec_enet_alloc_txq_buffers(ndev, i)) 294559d0f746SFrank Li return -ENOMEM; 294659d0f746SFrank Li return 0; 294759d0f746SFrank Li } 294859d0f746SFrank Li 2949793fc096SFrank Li static int 2950793fc096SFrank Li fec_enet_open(struct net_device *ndev) 2951793fc096SFrank Li { 2952793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2953793fc096SFrank Li int ret; 29541b0a83acSRichard Leitner bool reset_again; 2955793fc096SFrank Li 29568fff755eSAndrew Lunn ret = pm_runtime_get_sync(&fep->pdev->dev); 2957b0c6ce24SFabio Estevam if (ret < 0) 29588fff755eSAndrew Lunn return ret; 29598fff755eSAndrew Lunn 29605bbde4d2SNimrod Andy pinctrl_pm_select_default_state(&fep->pdev->dev); 2961e8fcfcd5SNimrod Andy ret = fec_enet_clk_enable(ndev, true); 2962e8fcfcd5SNimrod Andy if (ret) 29638fff755eSAndrew Lunn goto clk_enable; 2964e8fcfcd5SNimrod Andy 29651b0a83acSRichard Leitner /* During the first fec_enet_open call the PHY isn't probed at this 29661b0a83acSRichard Leitner * point. Therefore the phy_reset_after_clk_enable() call within 29671b0a83acSRichard Leitner * fec_enet_clk_enable() fails. As we need this reset in order to be 29681b0a83acSRichard Leitner * sure the PHY is working correctly we check if we need to reset again 29691b0a83acSRichard Leitner * later when the PHY is probed 29701b0a83acSRichard Leitner */ 29711b0a83acSRichard Leitner if (ndev->phydev && ndev->phydev->drv) 29721b0a83acSRichard Leitner reset_again = false; 29731b0a83acSRichard Leitner else 29741b0a83acSRichard Leitner reset_again = true; 29751b0a83acSRichard Leitner 2976793fc096SFrank Li /* I should reset the ring buffers here, but I don't yet know 2977793fc096SFrank Li * a simple way to do that. 2978793fc096SFrank Li */ 2979793fc096SFrank Li 2980793fc096SFrank Li ret = fec_enet_alloc_buffers(ndev); 2981793fc096SFrank Li if (ret) 2982681d2421SFabio Estevam goto err_enet_alloc; 2983793fc096SFrank Li 298455dd2753SNimrod Andy /* Init MAC prior to mii bus probe */ 298555dd2753SNimrod Andy fec_restart(ndev); 298655dd2753SNimrod Andy 2987793fc096SFrank Li /* Probe and connect to PHY when open the interface */ 2988793fc096SFrank Li ret = fec_enet_mii_probe(ndev); 2989681d2421SFabio Estevam if (ret) 2990681d2421SFabio Estevam goto err_enet_mii_probe; 2991ce5eaf02SRussell King 29921b0a83acSRichard Leitner /* Call phy_reset_after_clk_enable() again if it failed during 29931b0a83acSRichard Leitner * phy_reset_after_clk_enable() before because the PHY wasn't probed. 29941b0a83acSRichard Leitner */ 29951b0a83acSRichard Leitner if (reset_again) 29961b0a83acSRichard Leitner phy_reset_after_clk_enable(ndev->phydev); 29971b0a83acSRichard Leitner 299829380905SLucas Stach if (fep->quirks & FEC_QUIRK_ERR006687) 299929380905SLucas Stach imx6q_cpuidle_fec_irqs_used(); 300029380905SLucas Stach 3001ce5eaf02SRussell King napi_enable(&fep->napi); 300245f5c327SPhilippe Reynes phy_start(ndev->phydev); 30034d494cdcSFugang Duan netif_tx_start_all_queues(ndev); 30044d494cdcSFugang Duan 3005de40ed31SNimrod Andy device_set_wakeup_enable(&ndev->dev, fep->wol_flag & 3006de40ed31SNimrod Andy FEC_WOL_FLAG_ENABLE); 3007de40ed31SNimrod Andy 3008793fc096SFrank Li return 0; 3009681d2421SFabio Estevam 3010681d2421SFabio Estevam err_enet_mii_probe: 3011681d2421SFabio Estevam fec_enet_free_buffers(ndev); 3012681d2421SFabio Estevam err_enet_alloc: 3013681d2421SFabio Estevam fec_enet_clk_enable(ndev, false); 30148fff755eSAndrew Lunn clk_enable: 30158fff755eSAndrew Lunn pm_runtime_mark_last_busy(&fep->pdev->dev); 30168fff755eSAndrew Lunn pm_runtime_put_autosuspend(&fep->pdev->dev); 3017681d2421SFabio Estevam pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3018681d2421SFabio Estevam return ret; 3019793fc096SFrank Li } 3020793fc096SFrank Li 3021793fc096SFrank Li static int 3022793fc096SFrank Li fec_enet_close(struct net_device *ndev) 3023793fc096SFrank Li { 3024793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3025793fc096SFrank Li 302645f5c327SPhilippe Reynes phy_stop(ndev->phydev); 3027d76cfae9SRussell King 302831a6de34SRussell King if (netif_device_present(ndev)) { 3029793fc096SFrank Li napi_disable(&fep->napi); 3030b49cd504SRussell King netif_tx_disable(ndev); 3031793fc096SFrank Li fec_stop(ndev); 303231a6de34SRussell King } 3033793fc096SFrank Li 303445f5c327SPhilippe Reynes phy_disconnect(ndev->phydev); 3035793fc096SFrank Li 303629380905SLucas Stach if (fep->quirks & FEC_QUIRK_ERR006687) 303729380905SLucas Stach imx6q_cpuidle_fec_irqs_unused(); 303829380905SLucas Stach 303980cca775SNikita Yushchenko fec_enet_update_ethtool_stats(ndev); 304080cca775SNikita Yushchenko 3041e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 30425bbde4d2SNimrod Andy pinctrl_pm_select_sleep_state(&fep->pdev->dev); 30438fff755eSAndrew Lunn pm_runtime_mark_last_busy(&fep->pdev->dev); 30448fff755eSAndrew Lunn pm_runtime_put_autosuspend(&fep->pdev->dev); 30458fff755eSAndrew Lunn 3046793fc096SFrank Li fec_enet_free_buffers(ndev); 3047793fc096SFrank Li 3048793fc096SFrank Li return 0; 3049793fc096SFrank Li } 3050793fc096SFrank Li 3051793fc096SFrank Li /* Set or clear the multicast filter for this adaptor. 3052793fc096SFrank Li * Skeleton taken from sunlance driver. 3053793fc096SFrank Li * The CPM Ethernet implementation allows Multicast as well as individual 3054793fc096SFrank Li * MAC address filtering. Some of the drivers check to make sure it is 3055793fc096SFrank Li * a group multicast address, and discard those that are not. I guess I 3056793fc096SFrank Li * will do the same for now, but just remove the test if you want 3057793fc096SFrank Li * individual filtering as well (do the upper net layers want or support 3058793fc096SFrank Li * this kind of feature?). 3059793fc096SFrank Li */ 3060793fc096SFrank Li 30616176e89cSJiri Kosina #define FEC_HASH_BITS 6 /* #bits in hash */ 3062793fc096SFrank Li 3063793fc096SFrank Li static void set_multicast_list(struct net_device *ndev) 3064793fc096SFrank Li { 3065793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3066793fc096SFrank Li struct netdev_hw_addr *ha; 306716f6e983SKrzysztof Kozlowski unsigned int crc, tmp; 3068793fc096SFrank Li unsigned char hash; 306901f8902bSRui Sousa unsigned int hash_high = 0, hash_low = 0; 3070793fc096SFrank Li 3071793fc096SFrank Li if (ndev->flags & IFF_PROMISC) { 3072793fc096SFrank Li tmp = readl(fep->hwp + FEC_R_CNTRL); 3073793fc096SFrank Li tmp |= 0x8; 3074793fc096SFrank Li writel(tmp, fep->hwp + FEC_R_CNTRL); 3075793fc096SFrank Li return; 3076793fc096SFrank Li } 3077793fc096SFrank Li 3078793fc096SFrank Li tmp = readl(fep->hwp + FEC_R_CNTRL); 3079793fc096SFrank Li tmp &= ~0x8; 3080793fc096SFrank Li writel(tmp, fep->hwp + FEC_R_CNTRL); 3081793fc096SFrank Li 3082793fc096SFrank Li if (ndev->flags & IFF_ALLMULTI) { 3083793fc096SFrank Li /* Catch all multicast addresses, so set the 3084793fc096SFrank Li * filter to all 1's 3085793fc096SFrank Li */ 3086793fc096SFrank Li writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 3087793fc096SFrank Li writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3088793fc096SFrank Li 3089793fc096SFrank Li return; 3090793fc096SFrank Li } 3091793fc096SFrank Li 309201f8902bSRui Sousa /* Add the addresses in hash register */ 3093793fc096SFrank Li netdev_for_each_mc_addr(ha, ndev) { 3094793fc096SFrank Li /* calculate crc32 value of mac address */ 309516f6e983SKrzysztof Kozlowski crc = ether_crc_le(ndev->addr_len, ha->addr); 3096793fc096SFrank Li 30976176e89cSJiri Kosina /* only upper 6 bits (FEC_HASH_BITS) are used 3098981a0547SPeter Meerwald-Stadler * which point to specific bit in the hash registers 3099793fc096SFrank Li */ 31006176e89cSJiri Kosina hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f; 3101793fc096SFrank Li 310201f8902bSRui Sousa if (hash > 31) 310301f8902bSRui Sousa hash_high |= 1 << (hash - 32); 310401f8902bSRui Sousa else 310501f8902bSRui Sousa hash_low |= 1 << hash; 3106793fc096SFrank Li } 310701f8902bSRui Sousa 310801f8902bSRui Sousa writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 310901f8902bSRui Sousa writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3110793fc096SFrank Li } 3111793fc096SFrank Li 3112793fc096SFrank Li /* Set a MAC change in hardware. */ 3113793fc096SFrank Li static int 3114793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p) 3115793fc096SFrank Li { 3116793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3117793fc096SFrank Li struct sockaddr *addr = p; 3118793fc096SFrank Li 311944934facSLucas Stach if (addr) { 3120793fc096SFrank Li if (!is_valid_ether_addr(addr->sa_data)) 3121793fc096SFrank Li return -EADDRNOTAVAIL; 3122793fc096SFrank Li memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len); 312344934facSLucas Stach } 3124793fc096SFrank Li 31259638d19eSNimrod Andy /* Add netif status check here to avoid system hang in below case: 31269638d19eSNimrod Andy * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx; 31279638d19eSNimrod Andy * After ethx down, fec all clocks are gated off and then register 31289638d19eSNimrod Andy * access causes system hang. 31299638d19eSNimrod Andy */ 31309638d19eSNimrod Andy if (!netif_running(ndev)) 31319638d19eSNimrod Andy return 0; 31329638d19eSNimrod Andy 3133793fc096SFrank Li writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | 3134793fc096SFrank Li (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), 3135793fc096SFrank Li fep->hwp + FEC_ADDR_LOW); 3136793fc096SFrank Li writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), 3137793fc096SFrank Li fep->hwp + FEC_ADDR_HIGH); 3138793fc096SFrank Li return 0; 3139793fc096SFrank Li } 3140793fc096SFrank Li 3141793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER 3142793fc096SFrank Li /** 3143793fc096SFrank Li * fec_poll_controller - FEC Poll controller function 3144793fc096SFrank Li * @dev: The FEC network adapter 3145793fc096SFrank Li * 3146793fc096SFrank Li * Polled functionality used by netconsole and others in non interrupt mode 3147793fc096SFrank Li * 3148793fc096SFrank Li */ 3149793fc096SFrank Li static void fec_poll_controller(struct net_device *dev) 3150793fc096SFrank Li { 3151793fc096SFrank Li int i; 3152793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(dev); 3153793fc096SFrank Li 3154793fc096SFrank Li for (i = 0; i < FEC_IRQ_NUM; i++) { 3155793fc096SFrank Li if (fep->irq[i] > 0) { 3156793fc096SFrank Li disable_irq(fep->irq[i]); 3157793fc096SFrank Li fec_enet_interrupt(fep->irq[i], dev); 3158793fc096SFrank Li enable_irq(fep->irq[i]); 3159793fc096SFrank Li } 3160793fc096SFrank Li } 3161793fc096SFrank Li } 3162793fc096SFrank Li #endif 3163793fc096SFrank Li 31645bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev, 31654c09eed9SJim Baxter netdev_features_t features) 31664c09eed9SJim Baxter { 31674c09eed9SJim Baxter struct fec_enet_private *fep = netdev_priv(netdev); 31684c09eed9SJim Baxter netdev_features_t changed = features ^ netdev->features; 31694c09eed9SJim Baxter 31704c09eed9SJim Baxter netdev->features = features; 31714c09eed9SJim Baxter 31724c09eed9SJim Baxter /* Receive checksum has been changed */ 31734c09eed9SJim Baxter if (changed & NETIF_F_RXCSUM) { 31744c09eed9SJim Baxter if (features & NETIF_F_RXCSUM) 31754c09eed9SJim Baxter fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 31764c09eed9SJim Baxter else 31774c09eed9SJim Baxter fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; 31788506fa1dSRussell King } 31795bc26726SNimrod Andy } 31804c09eed9SJim Baxter 31815bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev, 31825bc26726SNimrod Andy netdev_features_t features) 31835bc26726SNimrod Andy { 31845bc26726SNimrod Andy struct fec_enet_private *fep = netdev_priv(netdev); 31855bc26726SNimrod Andy netdev_features_t changed = features ^ netdev->features; 31865bc26726SNimrod Andy 31875b40f709SFabio Estevam if (netif_running(netdev) && changed & NETIF_F_RXCSUM) { 31885bc26726SNimrod Andy napi_disable(&fep->napi); 31895bc26726SNimrod Andy netif_tx_lock_bh(netdev); 31905bc26726SNimrod Andy fec_stop(netdev); 31915bc26726SNimrod Andy fec_enet_set_netdev_features(netdev, features); 3192ef83337dSRussell King fec_restart(netdev); 31934d494cdcSFugang Duan netif_tx_wake_all_queues(netdev); 31946af42d42SRussell King netif_tx_unlock_bh(netdev); 3195dbc64a8eSRussell King napi_enable(&fep->napi); 31965bc26726SNimrod Andy } else { 31975bc26726SNimrod Andy fec_enet_set_netdev_features(netdev, features); 31984c09eed9SJim Baxter } 31994c09eed9SJim Baxter 32004c09eed9SJim Baxter return 0; 32014c09eed9SJim Baxter } 32024c09eed9SJim Baxter 3203793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = { 3204793fc096SFrank Li .ndo_open = fec_enet_open, 3205793fc096SFrank Li .ndo_stop = fec_enet_close, 3206793fc096SFrank Li .ndo_start_xmit = fec_enet_start_xmit, 3207793fc096SFrank Li .ndo_set_rx_mode = set_multicast_list, 3208793fc096SFrank Li .ndo_validate_addr = eth_validate_addr, 3209793fc096SFrank Li .ndo_tx_timeout = fec_timeout, 3210793fc096SFrank Li .ndo_set_mac_address = fec_set_mac_address, 3211793fc096SFrank Li .ndo_do_ioctl = fec_enet_ioctl, 3212793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER 3213793fc096SFrank Li .ndo_poll_controller = fec_poll_controller, 3214793fc096SFrank Li #endif 32154c09eed9SJim Baxter .ndo_set_features = fec_set_features, 3216793fc096SFrank Li }; 3217793fc096SFrank Li 321853bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = { 321953bb20d1STroy Kisky FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2 322053bb20d1STroy Kisky }; 322153bb20d1STroy Kisky 322253bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = { 322353bb20d1STroy Kisky FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2 322453bb20d1STroy Kisky }; 322553bb20d1STroy Kisky 3226793fc096SFrank Li /* 3227793fc096SFrank Li * XXX: We need to clean up on failure exits here. 3228793fc096SFrank Li * 3229793fc096SFrank Li */ 3230793fc096SFrank Li static int fec_enet_init(struct net_device *ndev) 3231793fc096SFrank Li { 3232793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3233793fc096SFrank Li struct bufdesc *cbd_base; 32344d494cdcSFugang Duan dma_addr_t bd_dma; 323555d0218aSNimrod Andy int bd_size; 323659d0f746SFrank Li unsigned int i; 32377355f276STroy Kisky unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) : 32387355f276STroy Kisky sizeof(struct bufdesc); 32397355f276STroy Kisky unsigned dsize_log2 = __fls(dsize); 3240453e9dc4SStefan Agner int ret; 324155d0218aSNimrod Andy 32427355f276STroy Kisky WARN_ON(dsize != (1 << dsize_log2)); 32433f1dcc6aSLucas Stach #if defined(CONFIG_ARM) || defined(CONFIG_ARM64) 324441ef84ceSFugang Duan fep->rx_align = 0xf; 324541ef84ceSFugang Duan fep->tx_align = 0xf; 324641ef84ceSFugang Duan #else 324741ef84ceSFugang Duan fep->rx_align = 0x3; 324841ef84ceSFugang Duan fep->tx_align = 0x3; 324941ef84ceSFugang Duan #endif 325041ef84ceSFugang Duan 3251453e9dc4SStefan Agner /* Check mask of the streaming and coherent API */ 3252453e9dc4SStefan Agner ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32)); 3253453e9dc4SStefan Agner if (ret < 0) { 3254453e9dc4SStefan Agner dev_warn(&fep->pdev->dev, "No suitable DMA available\n"); 3255453e9dc4SStefan Agner return ret; 3256453e9dc4SStefan Agner } 3257453e9dc4SStefan Agner 325859d0f746SFrank Li fec_enet_alloc_queue(ndev); 325979f33912SNimrod Andy 32607355f276STroy Kisky bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize; 3261793fc096SFrank Li 3262793fc096SFrank Li /* Allocate memory for buffer descriptors. */ 3263c0a1a0a6SLucas Stach cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma, 3264793fc096SFrank Li GFP_KERNEL); 32654d494cdcSFugang Duan if (!cbd_base) { 32664d494cdcSFugang Duan return -ENOMEM; 32674d494cdcSFugang Duan } 3268793fc096SFrank Li 3269793fc096SFrank Li /* Get the Ethernet address */ 3270793fc096SFrank Li fec_get_mac(ndev); 327144934facSLucas Stach /* make sure MAC we just acquired is programmed into the hw */ 327244934facSLucas Stach fec_set_mac_address(ndev, NULL); 3273793fc096SFrank Li 3274793fc096SFrank Li /* Set receive and transmit descriptor base. */ 327559d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) { 32767355f276STroy Kisky struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i]; 32777355f276STroy Kisky unsigned size = dsize * rxq->bd.ring_size; 32787355f276STroy Kisky 32797355f276STroy Kisky rxq->bd.qid = i; 32807355f276STroy Kisky rxq->bd.base = cbd_base; 32817355f276STroy Kisky rxq->bd.cur = cbd_base; 32827355f276STroy Kisky rxq->bd.dma = bd_dma; 32837355f276STroy Kisky rxq->bd.dsize = dsize; 32847355f276STroy Kisky rxq->bd.dsize_log2 = dsize_log2; 328553bb20d1STroy Kisky rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i]; 32867355f276STroy Kisky bd_dma += size; 32877355f276STroy Kisky cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 32887355f276STroy Kisky rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 328959d0f746SFrank Li } 329059d0f746SFrank Li 329159d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 32927355f276STroy Kisky struct fec_enet_priv_tx_q *txq = fep->tx_queue[i]; 32937355f276STroy Kisky unsigned size = dsize * txq->bd.ring_size; 32947355f276STroy Kisky 32957355f276STroy Kisky txq->bd.qid = i; 32967355f276STroy Kisky txq->bd.base = cbd_base; 32977355f276STroy Kisky txq->bd.cur = cbd_base; 32987355f276STroy Kisky txq->bd.dma = bd_dma; 32997355f276STroy Kisky txq->bd.dsize = dsize; 33007355f276STroy Kisky txq->bd.dsize_log2 = dsize_log2; 330153bb20d1STroy Kisky txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i]; 33027355f276STroy Kisky bd_dma += size; 33037355f276STroy Kisky cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 33047355f276STroy Kisky txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 330559d0f746SFrank Li } 33064d494cdcSFugang Duan 3307793fc096SFrank Li 3308793fc096SFrank Li /* The FEC Ethernet specific entries in the device structure */ 3309793fc096SFrank Li ndev->watchdog_timeo = TX_TIMEOUT; 3310793fc096SFrank Li ndev->netdev_ops = &fec_netdev_ops; 3311793fc096SFrank Li ndev->ethtool_ops = &fec_enet_ethtool_ops; 3312793fc096SFrank Li 3313793fc096SFrank Li writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); 3314322555f5SFabio Estevam netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT); 3315793fc096SFrank Li 33166b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_VLAN) 3317cdffcf1bSJim Baxter /* enable hw VLAN support */ 3318cdffcf1bSJim Baxter ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; 3319cdffcf1bSJim Baxter 33206b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_CSUM) { 332179f33912SNimrod Andy ndev->gso_max_segs = FEC_MAX_TSO_SEGS; 332279f33912SNimrod Andy 33234c09eed9SJim Baxter /* enable hw accelerator */ 33244c09eed9SJim Baxter ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM 332579f33912SNimrod Andy | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO); 33264c09eed9SJim Baxter fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 332748496255SShawn Guo } 33284c09eed9SJim Baxter 33296b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) { 333041ef84ceSFugang Duan fep->tx_align = 0; 333141ef84ceSFugang Duan fep->rx_align = 0x3f; 333241ef84ceSFugang Duan } 333341ef84ceSFugang Duan 333409d1e541SNimrod Andy ndev->hw_features = ndev->features; 333509d1e541SNimrod Andy 3336ef83337dSRussell King fec_restart(ndev); 3337793fc096SFrank Li 33382b30842bSAndrew Lunn if (fep->quirks & FEC_QUIRK_MIB_CLEAR) 33392b30842bSAndrew Lunn fec_enet_clear_ethtool_stats(ndev); 33402b30842bSAndrew Lunn else 334180cca775SNikita Yushchenko fec_enet_update_ethtool_stats(ndev); 334280cca775SNikita Yushchenko 3343793fc096SFrank Li return 0; 3344793fc096SFrank Li } 3345793fc096SFrank Li 3346793fc096SFrank Li #ifdef CONFIG_OF 33479269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev) 3348793fc096SFrank Li { 3349793fc096SFrank Li int err, phy_reset; 3350962d8cdcSBernhard Walle bool active_high = false; 3351159a0760SQuentin Schulz int msec = 1, phy_post_delay = 0; 3352793fc096SFrank Li struct device_node *np = pdev->dev.of_node; 3353793fc096SFrank Li 3354793fc096SFrank Li if (!np) 33559269e556SFugang Duan return 0; 3356793fc096SFrank Li 335761e04ccbSFugang Duan err = of_property_read_u32(np, "phy-reset-duration", &msec); 3358793fc096SFrank Li /* A sane reset duration should not be longer than 1s */ 335961e04ccbSFugang Duan if (!err && msec > 1000) 3360793fc096SFrank Li msec = 1; 3361793fc096SFrank Li 3362793fc096SFrank Li phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0); 33639269e556SFugang Duan if (phy_reset == -EPROBE_DEFER) 33649269e556SFugang Duan return phy_reset; 33659269e556SFugang Duan else if (!gpio_is_valid(phy_reset)) 33669269e556SFugang Duan return 0; 3367793fc096SFrank Li 3368159a0760SQuentin Schulz err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay); 3369159a0760SQuentin Schulz /* valid reset duration should be less than 1s */ 3370159a0760SQuentin Schulz if (!err && phy_post_delay > 1000) 3371159a0760SQuentin Schulz return -EINVAL; 3372159a0760SQuentin Schulz 3373962d8cdcSBernhard Walle active_high = of_property_read_bool(np, "phy-reset-active-high"); 337464f10f6eSBernhard Walle 3375793fc096SFrank Li err = devm_gpio_request_one(&pdev->dev, phy_reset, 3376962d8cdcSBernhard Walle active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, 337764f10f6eSBernhard Walle "phy-reset"); 3378793fc096SFrank Li if (err) { 3379793fc096SFrank Li dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err); 33809269e556SFugang Duan return err; 3381793fc096SFrank Li } 3382eb37c563SStefan Wahren 3383eb37c563SStefan Wahren if (msec > 20) 3384793fc096SFrank Li msleep(msec); 3385eb37c563SStefan Wahren else 3386eb37c563SStefan Wahren usleep_range(msec * 1000, msec * 1000 + 1000); 3387eb37c563SStefan Wahren 3388962d8cdcSBernhard Walle gpio_set_value_cansleep(phy_reset, !active_high); 33899269e556SFugang Duan 3390159a0760SQuentin Schulz if (!phy_post_delay) 3391159a0760SQuentin Schulz return 0; 3392159a0760SQuentin Schulz 3393159a0760SQuentin Schulz if (phy_post_delay > 20) 3394159a0760SQuentin Schulz msleep(phy_post_delay); 3395159a0760SQuentin Schulz else 3396159a0760SQuentin Schulz usleep_range(phy_post_delay * 1000, 3397159a0760SQuentin Schulz phy_post_delay * 1000 + 1000); 3398159a0760SQuentin Schulz 33999269e556SFugang Duan return 0; 3400793fc096SFrank Li } 3401793fc096SFrank Li #else /* CONFIG_OF */ 34029269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev) 3403793fc096SFrank Li { 3404793fc096SFrank Li /* 3405793fc096SFrank Li * In case of platform probe, the reset has been done 3406793fc096SFrank Li * by machine code. 3407793fc096SFrank Li */ 34089269e556SFugang Duan return 0; 3409793fc096SFrank Li } 3410793fc096SFrank Li #endif /* CONFIG_OF */ 3411793fc096SFrank Li 34129fc095f1SFugang Duan static void 34139fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx) 34149fc095f1SFugang Duan { 34159fc095f1SFugang Duan struct device_node *np = pdev->dev.of_node; 34169fc095f1SFugang Duan 34179fc095f1SFugang Duan *num_tx = *num_rx = 1; 34189fc095f1SFugang Duan 34199fc095f1SFugang Duan if (!np || !of_device_is_available(np)) 34209fc095f1SFugang Duan return; 34219fc095f1SFugang Duan 34229fc095f1SFugang Duan /* parse the num of tx and rx queues */ 342373b1c90dSSaurabh Sengar of_property_read_u32(np, "fsl,num-tx-queues", num_tx); 3424b7bd75cfSFrank Li 342573b1c90dSSaurabh Sengar of_property_read_u32(np, "fsl,num-rx-queues", num_rx); 34269fc095f1SFugang Duan 34279fc095f1SFugang Duan if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) { 3428b7bd75cfSFrank Li dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n", 34299fc095f1SFugang Duan *num_tx); 34309fc095f1SFugang Duan *num_tx = 1; 34319fc095f1SFugang Duan return; 34329fc095f1SFugang Duan } 34339fc095f1SFugang Duan 34349fc095f1SFugang Duan if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) { 3435b7bd75cfSFrank Li dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n", 34369fc095f1SFugang Duan *num_rx); 34379fc095f1SFugang Duan *num_rx = 1; 34389fc095f1SFugang Duan return; 34399fc095f1SFugang Duan } 34409fc095f1SFugang Duan 34419fc095f1SFugang Duan } 34429fc095f1SFugang Duan 34434ad1ceecSTroy Kisky static int fec_enet_get_irq_cnt(struct platform_device *pdev) 34444ad1ceecSTroy Kisky { 34454ad1ceecSTroy Kisky int irq_cnt = platform_irq_count(pdev); 34464ad1ceecSTroy Kisky 34474ad1ceecSTroy Kisky if (irq_cnt > FEC_IRQ_NUM) 34484ad1ceecSTroy Kisky irq_cnt = FEC_IRQ_NUM; /* last for pps */ 34494ad1ceecSTroy Kisky else if (irq_cnt == 2) 34504ad1ceecSTroy Kisky irq_cnt = 1; /* last for pps */ 34514ad1ceecSTroy Kisky else if (irq_cnt <= 0) 34524ad1ceecSTroy Kisky irq_cnt = 1; /* At least 1 irq is needed */ 34534ad1ceecSTroy Kisky return irq_cnt; 34544ad1ceecSTroy Kisky } 34554ad1ceecSTroy Kisky 3456da722186SMartin Fuzzey static int fec_enet_init_stop_mode(struct fec_enet_private *fep, 3457da722186SMartin Fuzzey struct device_node *np) 3458da722186SMartin Fuzzey { 3459da722186SMartin Fuzzey struct device_node *gpr_np; 34608a448bf8SFugang Duan u32 out_val[3]; 3461da722186SMartin Fuzzey int ret = 0; 3462da722186SMartin Fuzzey 34638a448bf8SFugang Duan gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0); 3464da722186SMartin Fuzzey if (!gpr_np) 3465da722186SMartin Fuzzey return 0; 3466da722186SMartin Fuzzey 34678a448bf8SFugang Duan ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val, 34688a448bf8SFugang Duan ARRAY_SIZE(out_val)); 34698a448bf8SFugang Duan if (ret) { 34708a448bf8SFugang Duan dev_dbg(&fep->pdev->dev, "no stop mode property\n"); 34718a448bf8SFugang Duan return ret; 34728a448bf8SFugang Duan } 34738a448bf8SFugang Duan 3474da722186SMartin Fuzzey fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np); 3475da722186SMartin Fuzzey if (IS_ERR(fep->stop_gpr.gpr)) { 3476da722186SMartin Fuzzey dev_err(&fep->pdev->dev, "could not find gpr regmap\n"); 3477da722186SMartin Fuzzey ret = PTR_ERR(fep->stop_gpr.gpr); 3478da722186SMartin Fuzzey fep->stop_gpr.gpr = NULL; 3479da722186SMartin Fuzzey goto out; 3480da722186SMartin Fuzzey } 3481da722186SMartin Fuzzey 34828a448bf8SFugang Duan fep->stop_gpr.reg = out_val[1]; 34838a448bf8SFugang Duan fep->stop_gpr.bit = out_val[2]; 3484da722186SMartin Fuzzey 3485da722186SMartin Fuzzey out: 3486da722186SMartin Fuzzey of_node_put(gpr_np); 3487da722186SMartin Fuzzey 3488da722186SMartin Fuzzey return ret; 3489da722186SMartin Fuzzey } 3490da722186SMartin Fuzzey 3491793fc096SFrank Li static int 3492793fc096SFrank Li fec_probe(struct platform_device *pdev) 3493793fc096SFrank Li { 3494793fc096SFrank Li struct fec_enet_private *fep; 3495793fc096SFrank Li struct fec_platform_data *pdata; 34960c65b2b9SAndrew Lunn phy_interface_t interface; 3497793fc096SFrank Li struct net_device *ndev; 3498793fc096SFrank Li int i, irq, ret = 0; 3499793fc096SFrank Li const struct of_device_id *of_id; 3500793fc096SFrank Li static int dev_id; 3501407066f8SUwe Kleine-König struct device_node *np = pdev->dev.of_node, *phy_node; 3502b7bd75cfSFrank Li int num_tx_qs; 3503b7bd75cfSFrank Li int num_rx_qs; 35044ad1ceecSTroy Kisky char irq_name[8]; 35054ad1ceecSTroy Kisky int irq_cnt; 3506da722186SMartin Fuzzey struct fec_devinfo *dev_info; 3507793fc096SFrank Li 35089fc095f1SFugang Duan fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs); 35099fc095f1SFugang Duan 3510793fc096SFrank Li /* Init network device */ 351180cca775SNikita Yushchenko ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) + 3512f85de666SNikita Yushchenko FEC_STATS_SIZE, num_tx_qs, num_rx_qs); 3513793fc096SFrank Li if (!ndev) 3514793fc096SFrank Li return -ENOMEM; 3515793fc096SFrank Li 3516793fc096SFrank Li SET_NETDEV_DEV(ndev, &pdev->dev); 3517793fc096SFrank Li 3518793fc096SFrank Li /* setup board info structure */ 3519793fc096SFrank Li fep = netdev_priv(ndev); 3520793fc096SFrank Li 35216b7e4008SLothar Waßmann of_id = of_match_device(fec_dt_ids, &pdev->dev); 35226b7e4008SLothar Waßmann if (of_id) 35236b7e4008SLothar Waßmann pdev->id_entry = of_id->data; 3524da722186SMartin Fuzzey dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data; 3525da722186SMartin Fuzzey if (dev_info) 3526da722186SMartin Fuzzey fep->quirks = dev_info->quirks; 35276b7e4008SLothar Waßmann 35280c818594SHubert Feurstein fep->netdev = ndev; 35299fc095f1SFugang Duan fep->num_rx_queues = num_rx_qs; 35309fc095f1SFugang Duan fep->num_tx_queues = num_tx_qs; 35319fc095f1SFugang Duan 3532d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 3533793fc096SFrank Li /* default enable pause frame auto negotiation */ 35346b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_GBIT) 3535793fc096SFrank Li fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; 3536d1391930SGuenter Roeck #endif 3537793fc096SFrank Li 35385bbde4d2SNimrod Andy /* Select default pin state */ 35395bbde4d2SNimrod Andy pinctrl_pm_select_default_state(&pdev->dev); 35405bbde4d2SNimrod Andy 35414f830a5aSYueHaibing fep->hwp = devm_platform_ioremap_resource(pdev, 0); 3542941e173aSTushar Behera if (IS_ERR(fep->hwp)) { 3543941e173aSTushar Behera ret = PTR_ERR(fep->hwp); 3544941e173aSTushar Behera goto failed_ioremap; 3545941e173aSTushar Behera } 3546941e173aSTushar Behera 3547793fc096SFrank Li fep->pdev = pdev; 3548793fc096SFrank Li fep->dev_id = dev_id++; 3549793fc096SFrank Li 3550793fc096SFrank Li platform_set_drvdata(pdev, ndev); 3551793fc096SFrank Li 355229380905SLucas Stach if ((of_machine_is_compatible("fsl,imx6q") || 355329380905SLucas Stach of_machine_is_compatible("fsl,imx6dl")) && 355429380905SLucas Stach !of_property_read_bool(np, "fsl,err006687-workaround-present")) 355529380905SLucas Stach fep->quirks |= FEC_QUIRK_ERR006687; 355629380905SLucas Stach 3557de40ed31SNimrod Andy if (of_get_property(np, "fsl,magic-packet", NULL)) 3558de40ed31SNimrod Andy fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET; 3559de40ed31SNimrod Andy 35608a448bf8SFugang Duan ret = fec_enet_init_stop_mode(fep, np); 3561da722186SMartin Fuzzey if (ret) 3562da722186SMartin Fuzzey goto failed_stop_mode; 3563da722186SMartin Fuzzey 3564407066f8SUwe Kleine-König phy_node = of_parse_phandle(np, "phy-handle", 0); 3565407066f8SUwe Kleine-König if (!phy_node && of_phy_is_fixed_link(np)) { 3566407066f8SUwe Kleine-König ret = of_phy_register_fixed_link(np); 3567407066f8SUwe Kleine-König if (ret < 0) { 3568407066f8SUwe Kleine-König dev_err(&pdev->dev, 3569407066f8SUwe Kleine-König "broken fixed-link specification\n"); 3570407066f8SUwe Kleine-König goto failed_phy; 3571407066f8SUwe Kleine-König } 3572407066f8SUwe Kleine-König phy_node = of_node_get(np); 3573407066f8SUwe Kleine-König } 3574407066f8SUwe Kleine-König fep->phy_node = phy_node; 3575407066f8SUwe Kleine-König 35760c65b2b9SAndrew Lunn ret = of_get_phy_mode(pdev->dev.of_node, &interface); 35770c65b2b9SAndrew Lunn if (ret) { 357894660ba0SJingoo Han pdata = dev_get_platdata(&pdev->dev); 3579793fc096SFrank Li if (pdata) 3580793fc096SFrank Li fep->phy_interface = pdata->phy; 3581793fc096SFrank Li else 3582793fc096SFrank Li fep->phy_interface = PHY_INTERFACE_MODE_MII; 3583793fc096SFrank Li } else { 35840c65b2b9SAndrew Lunn fep->phy_interface = interface; 3585793fc096SFrank Li } 3586793fc096SFrank Li 3587793fc096SFrank Li fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 3588793fc096SFrank Li if (IS_ERR(fep->clk_ipg)) { 3589793fc096SFrank Li ret = PTR_ERR(fep->clk_ipg); 3590793fc096SFrank Li goto failed_clk; 3591793fc096SFrank Li } 3592793fc096SFrank Li 3593793fc096SFrank Li fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 3594793fc096SFrank Li if (IS_ERR(fep->clk_ahb)) { 3595793fc096SFrank Li ret = PTR_ERR(fep->clk_ahb); 3596793fc096SFrank Li goto failed_clk; 3597793fc096SFrank Li } 3598793fc096SFrank Li 3599d851b47bSFugang Duan fep->itr_clk_rate = clk_get_rate(fep->clk_ahb); 3600d851b47bSFugang Duan 360138f56f33SLinus Torvalds /* enet_out is optional, depends on board */ 360238f56f33SLinus Torvalds fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out"); 360338f56f33SLinus Torvalds if (IS_ERR(fep->clk_enet_out)) 360438f56f33SLinus Torvalds fep->clk_enet_out = NULL; 360538f56f33SLinus Torvalds 360691c0d987SNimrod Andy fep->ptp_clk_on = false; 360791c0d987SNimrod Andy mutex_init(&fep->ptp_clk_mutex); 36089b5330edSFugang Duan 36099b5330edSFugang Duan /* clk_ref is optional, depends on board */ 36109b5330edSFugang Duan fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref"); 36119b5330edSFugang Duan if (IS_ERR(fep->clk_ref)) 36129b5330edSFugang Duan fep->clk_ref = NULL; 36139b5330edSFugang Duan 36146b7e4008SLothar Waßmann fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX; 3615793fc096SFrank Li fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); 3616793fc096SFrank Li if (IS_ERR(fep->clk_ptp)) { 361738f56f33SLinus Torvalds fep->clk_ptp = NULL; 3618217b5844SLothar Waßmann fep->bufdesc_ex = false; 3619793fc096SFrank Li } 3620793fc096SFrank Li 3621e8fcfcd5SNimrod Andy ret = fec_enet_clk_enable(ndev, true); 362213a097bdSFabio Estevam if (ret) 362313a097bdSFabio Estevam goto failed_clk; 362413a097bdSFabio Estevam 36258fff755eSAndrew Lunn ret = clk_prepare_enable(fep->clk_ipg); 36268fff755eSAndrew Lunn if (ret) 36278fff755eSAndrew Lunn goto failed_clk_ipg; 3628d7c3a206SAndy Duan ret = clk_prepare_enable(fep->clk_ahb); 3629d7c3a206SAndy Duan if (ret) 3630d7c3a206SAndy Duan goto failed_clk_ahb; 36318fff755eSAndrew Lunn 363225974d8aSStefan Agner fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy"); 3633f4e9f3d2SFabio Estevam if (!IS_ERR(fep->reg_phy)) { 3634f4e9f3d2SFabio Estevam ret = regulator_enable(fep->reg_phy); 3635793fc096SFrank Li if (ret) { 3636793fc096SFrank Li dev_err(&pdev->dev, 3637793fc096SFrank Li "Failed to enable phy regulator: %d\n", ret); 3638793fc096SFrank Li goto failed_regulator; 3639793fc096SFrank Li } 3640f6a4d607SFabio Estevam } else { 36413f38c683SFugang Duan if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) { 36423f38c683SFugang Duan ret = -EPROBE_DEFER; 36433f38c683SFugang Duan goto failed_regulator; 36443f38c683SFugang Duan } 3645f6a4d607SFabio Estevam fep->reg_phy = NULL; 3646793fc096SFrank Li } 3647793fc096SFrank Li 36488fff755eSAndrew Lunn pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT); 36498fff755eSAndrew Lunn pm_runtime_use_autosuspend(&pdev->dev); 365014d2b7c1SLucas Stach pm_runtime_get_noresume(&pdev->dev); 36518fff755eSAndrew Lunn pm_runtime_set_active(&pdev->dev); 36528fff755eSAndrew Lunn pm_runtime_enable(&pdev->dev); 36538fff755eSAndrew Lunn 36549269e556SFugang Duan ret = fec_reset_phy(pdev); 36559269e556SFugang Duan if (ret) 36569269e556SFugang Duan goto failed_reset; 3657793fc096SFrank Li 36584ad1ceecSTroy Kisky irq_cnt = fec_enet_get_irq_cnt(pdev); 3659793fc096SFrank Li if (fep->bufdesc_ex) 36604ad1ceecSTroy Kisky fec_ptp_init(pdev, irq_cnt); 3661793fc096SFrank Li 3662793fc096SFrank Li ret = fec_enet_init(ndev); 3663793fc096SFrank Li if (ret) 3664793fc096SFrank Li goto failed_init; 3665793fc096SFrank Li 36664ad1ceecSTroy Kisky for (i = 0; i < irq_cnt; i++) { 36673ded9f2bSArnd Bergmann snprintf(irq_name, sizeof(irq_name), "int%d", i); 36683b56be21SAnson Huang irq = platform_get_irq_byname_optional(pdev, irq_name); 36694ad1ceecSTroy Kisky if (irq < 0) 3670793fc096SFrank Li irq = platform_get_irq(pdev, i); 3671793fc096SFrank Li if (irq < 0) { 3672793fc096SFrank Li ret = irq; 3673793fc096SFrank Li goto failed_irq; 3674793fc096SFrank Li } 36750d9b2ab1SFabio Estevam ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt, 367644a272ddSMichael Opdenacker 0, pdev->name, ndev); 36770d9b2ab1SFabio Estevam if (ret) 3678793fc096SFrank Li goto failed_irq; 3679de40ed31SNimrod Andy 3680de40ed31SNimrod Andy fep->irq[i] = irq; 3681793fc096SFrank Li } 3682793fc096SFrank Li 3683793fc096SFrank Li ret = fec_enet_mii_init(pdev); 3684793fc096SFrank Li if (ret) 3685793fc096SFrank Li goto failed_mii_init; 3686793fc096SFrank Li 3687793fc096SFrank Li /* Carrier starts down, phylib will bring it up */ 3688793fc096SFrank Li netif_carrier_off(ndev); 3689e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 36905bbde4d2SNimrod Andy pinctrl_pm_select_sleep_state(&pdev->dev); 3691793fc096SFrank Li 369259193053SAndrew Lunn ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN; 369359193053SAndrew Lunn 3694793fc096SFrank Li ret = register_netdev(ndev); 3695793fc096SFrank Li if (ret) 3696793fc096SFrank Li goto failed_register; 3697793fc096SFrank Li 3698de40ed31SNimrod Andy device_init_wakeup(&ndev->dev, fep->wol_flag & 3699de40ed31SNimrod Andy FEC_WOL_HAS_MAGIC_PACKET); 3700de40ed31SNimrod Andy 3701eb1d0640SFabio Estevam if (fep->bufdesc_ex && fep->ptp_clock) 3702eb1d0640SFabio Estevam netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); 3703eb1d0640SFabio Estevam 37041b7bde6dSNimrod Andy fep->rx_copybreak = COPYBREAK_DEFAULT; 370536cdc743SRussell King INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work); 37068fff755eSAndrew Lunn 37078fff755eSAndrew Lunn pm_runtime_mark_last_busy(&pdev->dev); 37088fff755eSAndrew Lunn pm_runtime_put_autosuspend(&pdev->dev); 37098fff755eSAndrew Lunn 3710793fc096SFrank Li return 0; 3711793fc096SFrank Li 3712793fc096SFrank Li failed_register: 3713793fc096SFrank Li fec_enet_mii_remove(fep); 3714793fc096SFrank Li failed_mii_init: 37157a2bbd8dSFabio Estevam failed_irq: 37167a2bbd8dSFabio Estevam failed_init: 371732cba57bSLucas Stach fec_ptp_stop(pdev); 37189269e556SFugang Duan failed_reset: 3719ce8d24f9SAndy Duan pm_runtime_put_noidle(&pdev->dev); 37209269e556SFugang Duan pm_runtime_disable(&pdev->dev); 3721*c6165cf0SFugang Duan if (fep->reg_phy) 3722*c6165cf0SFugang Duan regulator_disable(fep->reg_phy); 3723793fc096SFrank Li failed_regulator: 3724d7c3a206SAndy Duan clk_disable_unprepare(fep->clk_ahb); 3725d7c3a206SAndy Duan failed_clk_ahb: 3726d7c3a206SAndy Duan clk_disable_unprepare(fep->clk_ipg); 37278fff755eSAndrew Lunn failed_clk_ipg: 3728e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 3729793fc096SFrank Li failed_clk: 373082005b1cSJohan Hovold if (of_phy_is_fixed_link(np)) 373182005b1cSJohan Hovold of_phy_deregister_fixed_link(np); 3732407066f8SUwe Kleine-König of_node_put(phy_node); 3733da722186SMartin Fuzzey failed_stop_mode: 3734d1616f07SFugang Duan failed_phy: 3735d1616f07SFugang Duan dev_id--; 3736793fc096SFrank Li failed_ioremap: 3737793fc096SFrank Li free_netdev(ndev); 3738793fc096SFrank Li 3739793fc096SFrank Li return ret; 3740793fc096SFrank Li } 3741793fc096SFrank Li 3742793fc096SFrank Li static int 3743793fc096SFrank Li fec_drv_remove(struct platform_device *pdev) 3744793fc096SFrank Li { 3745793fc096SFrank Li struct net_device *ndev = platform_get_drvdata(pdev); 3746793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 374782005b1cSJohan Hovold struct device_node *np = pdev->dev.of_node; 3748a31eda65SChuhong Yuan int ret; 3749a31eda65SChuhong Yuan 3750a31eda65SChuhong Yuan ret = pm_runtime_get_sync(&pdev->dev); 3751a31eda65SChuhong Yuan if (ret < 0) 3752a31eda65SChuhong Yuan return ret; 3753793fc096SFrank Li 375436cdc743SRussell King cancel_work_sync(&fep->tx_timeout_work); 375532cba57bSLucas Stach fec_ptp_stop(pdev); 3756793fc096SFrank Li unregister_netdev(ndev); 3757793fc096SFrank Li fec_enet_mii_remove(fep); 3758f6a4d607SFabio Estevam if (fep->reg_phy) 3759f6a4d607SFabio Estevam regulator_disable(fep->reg_phy); 3760a31eda65SChuhong Yuan 376182005b1cSJohan Hovold if (of_phy_is_fixed_link(np)) 376282005b1cSJohan Hovold of_phy_deregister_fixed_link(np); 3763407066f8SUwe Kleine-König of_node_put(fep->phy_node); 3764793fc096SFrank Li free_netdev(ndev); 3765793fc096SFrank Li 3766a31eda65SChuhong Yuan clk_disable_unprepare(fep->clk_ahb); 3767a31eda65SChuhong Yuan clk_disable_unprepare(fep->clk_ipg); 3768a31eda65SChuhong Yuan pm_runtime_put_noidle(&pdev->dev); 3769a31eda65SChuhong Yuan pm_runtime_disable(&pdev->dev); 3770a31eda65SChuhong Yuan 3771793fc096SFrank Li return 0; 3772793fc096SFrank Li } 3773793fc096SFrank Li 3774dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev) 3775793fc096SFrank Li { 3776793fc096SFrank Li struct net_device *ndev = dev_get_drvdata(dev); 3777793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3778793fc096SFrank Li 3779da1774e5SRussell King rtnl_lock(); 3780793fc096SFrank Li if (netif_running(ndev)) { 3781de40ed31SNimrod Andy if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) 3782de40ed31SNimrod Andy fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON; 378345f5c327SPhilippe Reynes phy_stop(ndev->phydev); 378431a6de34SRussell King napi_disable(&fep->napi); 378531a6de34SRussell King netif_tx_lock_bh(ndev); 3786793fc096SFrank Li netif_device_detach(ndev); 378731a6de34SRussell King netif_tx_unlock_bh(ndev); 378831a6de34SRussell King fec_stop(ndev); 3789e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 3790de40ed31SNimrod Andy if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) 37915bbde4d2SNimrod Andy pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3792f4c4a4e0SNimrod Andy } 3793f4c4a4e0SNimrod Andy rtnl_unlock(); 3794793fc096SFrank Li 3795de40ed31SNimrod Andy if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) 3796238f7bc7SFabio Estevam regulator_disable(fep->reg_phy); 3797238f7bc7SFabio Estevam 3798858eeb7dSNimrod Andy /* SOC supply clock to phy, when clock is disabled, phy link down 3799858eeb7dSNimrod Andy * SOC control phy regulator, when regulator is disabled, phy link down 3800858eeb7dSNimrod Andy */ 3801858eeb7dSNimrod Andy if (fep->clk_enet_out || fep->reg_phy) 3802858eeb7dSNimrod Andy fep->link = 0; 3803858eeb7dSNimrod Andy 3804793fc096SFrank Li return 0; 3805793fc096SFrank Li } 3806793fc096SFrank Li 3807dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev) 3808793fc096SFrank Li { 3809793fc096SFrank Li struct net_device *ndev = dev_get_drvdata(dev); 3810793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3811238f7bc7SFabio Estevam int ret; 3812de40ed31SNimrod Andy int val; 3813238f7bc7SFabio Estevam 3814de40ed31SNimrod Andy if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) { 3815238f7bc7SFabio Estevam ret = regulator_enable(fep->reg_phy); 3816238f7bc7SFabio Estevam if (ret) 3817238f7bc7SFabio Estevam return ret; 3818238f7bc7SFabio Estevam } 3819793fc096SFrank Li 3820da1774e5SRussell King rtnl_lock(); 3821793fc096SFrank Li if (netif_running(ndev)) { 3822f4c4a4e0SNimrod Andy ret = fec_enet_clk_enable(ndev, true); 3823f4c4a4e0SNimrod Andy if (ret) { 3824f4c4a4e0SNimrod Andy rtnl_unlock(); 3825f4c4a4e0SNimrod Andy goto failed_clk; 3826f4c4a4e0SNimrod Andy } 3827de40ed31SNimrod Andy if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) { 3828da722186SMartin Fuzzey fec_enet_stop_mode(fep, false); 3829da722186SMartin Fuzzey 3830de40ed31SNimrod Andy val = readl(fep->hwp + FEC_ECNTRL); 3831de40ed31SNimrod Andy val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 3832de40ed31SNimrod Andy writel(val, fep->hwp + FEC_ECNTRL); 3833de40ed31SNimrod Andy fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON; 3834de40ed31SNimrod Andy } else { 3835de40ed31SNimrod Andy pinctrl_pm_select_default_state(&fep->pdev->dev); 3836de40ed31SNimrod Andy } 3837ef83337dSRussell King fec_restart(ndev); 383831a6de34SRussell King netif_tx_lock_bh(ndev); 3839793fc096SFrank Li netif_device_attach(ndev); 38406af42d42SRussell King netif_tx_unlock_bh(ndev); 38416af42d42SRussell King napi_enable(&fep->napi); 384245f5c327SPhilippe Reynes phy_start(ndev->phydev); 3843793fc096SFrank Li } 3844da1774e5SRussell King rtnl_unlock(); 3845793fc096SFrank Li 3846793fc096SFrank Li return 0; 384713a097bdSFabio Estevam 3848e8fcfcd5SNimrod Andy failed_clk: 384913a097bdSFabio Estevam if (fep->reg_phy) 385013a097bdSFabio Estevam regulator_disable(fep->reg_phy); 385113a097bdSFabio Estevam return ret; 3852793fc096SFrank Li } 3853793fc096SFrank Li 38548fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev) 38558fff755eSAndrew Lunn { 38568fff755eSAndrew Lunn struct net_device *ndev = dev_get_drvdata(dev); 38578fff755eSAndrew Lunn struct fec_enet_private *fep = netdev_priv(ndev); 38588fff755eSAndrew Lunn 3859d7c3a206SAndy Duan clk_disable_unprepare(fep->clk_ahb); 38608fff755eSAndrew Lunn clk_disable_unprepare(fep->clk_ipg); 38618fff755eSAndrew Lunn 38628fff755eSAndrew Lunn return 0; 38638fff755eSAndrew Lunn } 38648fff755eSAndrew Lunn 38658fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev) 38668fff755eSAndrew Lunn { 38678fff755eSAndrew Lunn struct net_device *ndev = dev_get_drvdata(dev); 38688fff755eSAndrew Lunn struct fec_enet_private *fep = netdev_priv(ndev); 3869d7c3a206SAndy Duan int ret; 38708fff755eSAndrew Lunn 3871d7c3a206SAndy Duan ret = clk_prepare_enable(fep->clk_ahb); 3872d7c3a206SAndy Duan if (ret) 3873d7c3a206SAndy Duan return ret; 3874d7c3a206SAndy Duan ret = clk_prepare_enable(fep->clk_ipg); 3875d7c3a206SAndy Duan if (ret) 3876d7c3a206SAndy Duan goto failed_clk_ipg; 3877d7c3a206SAndy Duan 3878d7c3a206SAndy Duan return 0; 3879d7c3a206SAndy Duan 3880d7c3a206SAndy Duan failed_clk_ipg: 3881d7c3a206SAndy Duan clk_disable_unprepare(fep->clk_ahb); 3882d7c3a206SAndy Duan return ret; 38838fff755eSAndrew Lunn } 38848fff755eSAndrew Lunn 38858fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = { 38868fff755eSAndrew Lunn SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume) 38878fff755eSAndrew Lunn SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL) 38888fff755eSAndrew Lunn }; 3889793fc096SFrank Li 3890793fc096SFrank Li static struct platform_driver fec_driver = { 3891793fc096SFrank Li .driver = { 3892793fc096SFrank Li .name = DRIVER_NAME, 3893793fc096SFrank Li .pm = &fec_pm_ops, 3894793fc096SFrank Li .of_match_table = fec_dt_ids, 3895272bb0e9SFabio Estevam .suppress_bind_attrs = true, 3896793fc096SFrank Li }, 3897793fc096SFrank Li .id_table = fec_devtype, 3898793fc096SFrank Li .probe = fec_probe, 3899793fc096SFrank Li .remove = fec_drv_remove, 3900793fc096SFrank Li }; 3901793fc096SFrank Li 3902793fc096SFrank Li module_platform_driver(fec_driver); 3903793fc096SFrank Li 3904f8c0aca9SFabio Estevam MODULE_ALIAS("platform:"DRIVER_NAME); 3905793fc096SFrank Li MODULE_LICENSE("GPL"); 3906