1*1f508124SFabio 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> 51793fc096SFrank Li #include <linux/platform_device.h> 527f854420SAndrew Lunn #include <linux/mdio.h> 53793fc096SFrank Li #include <linux/phy.h> 54793fc096SFrank Li #include <linux/fec.h> 55793fc096SFrank Li #include <linux/of.h> 56793fc096SFrank Li #include <linux/of_device.h> 57793fc096SFrank Li #include <linux/of_gpio.h> 58407066f8SUwe Kleine-König #include <linux/of_mdio.h> 59793fc096SFrank Li #include <linux/of_net.h> 60793fc096SFrank Li #include <linux/regulator/consumer.h> 61cdffcf1bSJim Baxter #include <linux/if_vlan.h> 62a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h> 63c259c132SFrank Li #include <linux/prefetch.h> 6429380905SLucas Stach #include <soc/imx/cpuidle.h> 65793fc096SFrank Li 66793fc096SFrank Li #include <asm/cacheflush.h> 67793fc096SFrank Li 68793fc096SFrank Li #include "fec.h" 69793fc096SFrank Li 70772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev); 71d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev); 72772e42b0SChristoph Müllner 73793fc096SFrank Li #define DRIVER_NAME "fec" 74793fc096SFrank Li 754d494cdcSFugang Duan #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0)) 764d494cdcSFugang Duan 77793fc096SFrank Li /* Pause frame feild and FIFO threshold */ 78793fc096SFrank Li #define FEC_ENET_FCE (1 << 5) 79793fc096SFrank Li #define FEC_ENET_RSEM_V 0x84 80793fc096SFrank Li #define FEC_ENET_RSFL_V 16 81793fc096SFrank Li #define FEC_ENET_RAEM_V 0x8 82793fc096SFrank Li #define FEC_ENET_RAFL_V 0x8 83793fc096SFrank Li #define FEC_ENET_OPD_V 0xFFF0 848fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT 100 /* ms */ 85793fc096SFrank Li 86793fc096SFrank Li static struct platform_device_id fec_devtype[] = { 87793fc096SFrank Li { 88793fc096SFrank Li /* keep it for coldfire */ 89793fc096SFrank Li .name = DRIVER_NAME, 90793fc096SFrank Li .driver_data = 0, 91793fc096SFrank Li }, { 92793fc096SFrank Li .name = "imx25-fec", 932b30842bSAndrew Lunn .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR, 94793fc096SFrank Li }, { 95793fc096SFrank Li .name = "imx27-fec", 962b30842bSAndrew Lunn .driver_data = FEC_QUIRK_MIB_CLEAR, 97793fc096SFrank Li }, { 98793fc096SFrank Li .name = "imx28-fec", 993d125f9cSStefan Agner .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME | 10018803495SGreg Ungerer FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC, 101793fc096SFrank Li }, { 102793fc096SFrank Li .name = "imx6q-fec", 103793fc096SFrank Li .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 104cdffcf1bSJim Baxter FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 10518803495SGreg Ungerer FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 | 10618803495SGreg Ungerer FEC_QUIRK_HAS_RACC, 107793fc096SFrank Li }, { 10836803542SShawn Guo .name = "mvf600-fec", 10918803495SGreg Ungerer .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC, 110ca7c4a45SJingchang Lu }, { 11195a77470SFugang Duan .name = "imx6sx-fec", 11295a77470SFugang Duan .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 11395a77470SFugang Duan FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 114f88c7edeSNimrod Andy FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 11518803495SGreg Ungerer FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 116ff7566b8SFugang Duan FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE, 11795a77470SFugang Duan }, { 118a51d3ab5SFugang Duan .name = "imx6ul-fec", 119a51d3ab5SFugang Duan .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 120a51d3ab5SFugang Duan FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 12199492ad4SFugang Duan FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 | 12299492ad4SFugang Duan FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC | 12399492ad4SFugang Duan FEC_QUIRK_HAS_COALESCE, 124a51d3ab5SFugang Duan }, { 125793fc096SFrank Li /* sentinel */ 126793fc096SFrank Li } 127793fc096SFrank Li }; 128793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype); 129793fc096SFrank Li 130793fc096SFrank Li enum imx_fec_type { 131793fc096SFrank Li IMX25_FEC = 1, /* runs on i.mx25/50/53 */ 132793fc096SFrank Li IMX27_FEC, /* runs on i.mx27/35/51 */ 133793fc096SFrank Li IMX28_FEC, 134793fc096SFrank Li IMX6Q_FEC, 13536803542SShawn Guo MVF600_FEC, 136ba593e00SFugang Duan IMX6SX_FEC, 137a51d3ab5SFugang Duan IMX6UL_FEC, 138793fc096SFrank Li }; 139793fc096SFrank Li 140793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = { 141793fc096SFrank Li { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, 142793fc096SFrank Li { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, 143793fc096SFrank Li { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, 144793fc096SFrank Li { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, 14536803542SShawn Guo { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, 146ba593e00SFugang Duan { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], }, 147a51d3ab5SFugang Duan { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], }, 148793fc096SFrank Li { /* sentinel */ } 149793fc096SFrank Li }; 150793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids); 151793fc096SFrank Li 152793fc096SFrank Li static unsigned char macaddr[ETH_ALEN]; 153793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0); 154793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); 155793fc096SFrank Li 156793fc096SFrank Li #if defined(CONFIG_M5272) 157793fc096SFrank Li /* 158793fc096SFrank Li * Some hardware gets it MAC address out of local flash memory. 159793fc096SFrank Li * if this is non-zero then assume it is the address to get MAC from. 160793fc096SFrank Li */ 161793fc096SFrank Li #if defined(CONFIG_NETtel) 162793fc096SFrank Li #define FEC_FLASHMAC 0xf0006006 163793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) 164793fc096SFrank Li #define FEC_FLASHMAC 0xf0006000 165793fc096SFrank Li #elif defined(CONFIG_CANCam) 166793fc096SFrank Li #define FEC_FLASHMAC 0xf0020000 167793fc096SFrank Li #elif defined (CONFIG_M5272C3) 168793fc096SFrank Li #define FEC_FLASHMAC (0xffe04000 + 4) 169793fc096SFrank Li #elif defined(CONFIG_MOD5272) 170793fc096SFrank Li #define FEC_FLASHMAC 0xffc0406b 171793fc096SFrank Li #else 172793fc096SFrank Li #define FEC_FLASHMAC 0 173793fc096SFrank Li #endif 174793fc096SFrank Li #endif /* CONFIG_M5272 */ 175793fc096SFrank Li 176cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets. 177fbbeefddSAndrew Lunn * 178fbbeefddSAndrew Lunn * 2048 byte skbufs are allocated. However, alignment requirements 179fbbeefddSAndrew Lunn * varies between FEC variants. Worst case is 64, so round down by 64. 180793fc096SFrank Li */ 181fbbeefddSAndrew Lunn #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64)) 182793fc096SFrank Li #define PKT_MINBUF_SIZE 64 183793fc096SFrank Li 1844c09eed9SJim Baxter /* FEC receive acceleration */ 1854c09eed9SJim Baxter #define FEC_RACC_IPDIS (1 << 1) 1864c09eed9SJim Baxter #define FEC_RACC_PRODIS (1 << 2) 1873ac72b7bSEric Nelson #define FEC_RACC_SHIFT16 BIT(7) 1884c09eed9SJim Baxter #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS) 1894c09eed9SJim Baxter 1902b30842bSAndrew Lunn /* MIB Control Register */ 1912b30842bSAndrew Lunn #define FEC_MIB_CTRLSTAT_DISABLE BIT(31) 1922b30842bSAndrew Lunn 193793fc096SFrank Li /* 194793fc096SFrank Li * The 5270/5271/5280/5282/532x RX control register also contains maximum frame 195793fc096SFrank Li * size bits. Other FEC hardware does not, so we need to take that into 196793fc096SFrank Li * account when setting it. 197793fc096SFrank Li */ 198793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 1993f1dcc6aSLucas Stach defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 2003f1dcc6aSLucas Stach defined(CONFIG_ARM64) 201793fc096SFrank Li #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) 202793fc096SFrank Li #else 203793fc096SFrank Li #define OPT_FRAME_SIZE 0 204793fc096SFrank Li #endif 205793fc096SFrank Li 206793fc096SFrank Li /* FEC MII MMFR bits definition */ 207793fc096SFrank Li #define FEC_MMFR_ST (1 << 30) 208793fc096SFrank Li #define FEC_MMFR_OP_READ (2 << 28) 209793fc096SFrank Li #define FEC_MMFR_OP_WRITE (1 << 28) 210793fc096SFrank Li #define FEC_MMFR_PA(v) ((v & 0x1f) << 23) 211793fc096SFrank Li #define FEC_MMFR_RA(v) ((v & 0x1f) << 18) 212793fc096SFrank Li #define FEC_MMFR_TA (2 << 16) 213793fc096SFrank Li #define FEC_MMFR_DATA(v) (v & 0xffff) 214de40ed31SNimrod Andy /* FEC ECR bits definition */ 215de40ed31SNimrod Andy #define FEC_ECR_MAGICEN (1 << 2) 216de40ed31SNimrod Andy #define FEC_ECR_SLEEP (1 << 3) 217793fc096SFrank Li 218793fc096SFrank Li #define FEC_MII_TIMEOUT 30000 /* us */ 219793fc096SFrank Li 220793fc096SFrank Li /* Transmitter timeout */ 221793fc096SFrank Li #define TX_TIMEOUT (2 * HZ) 222793fc096SFrank Li 223793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG 0x1 224793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE 0x2 225de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0) 226de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE (0x1 << 1) 227de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2) 228793fc096SFrank Li 2291b7bde6dSNimrod Andy #define COPYBREAK_DEFAULT 256 2301b7bde6dSNimrod Andy 23179f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */ 23279f33912SNimrod Andy #define FEC_MAX_TSO_SEGS 100 23379f33912SNimrod Andy #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 23479f33912SNimrod Andy 23579f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \ 23679f33912SNimrod Andy ((addr >= txq->tso_hdrs_dma) && \ 2377355f276STroy Kisky (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE)) 23879f33912SNimrod Andy 239793fc096SFrank Li static int mii_cnt; 240793fc096SFrank Li 2417355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, 2427355f276STroy Kisky struct bufdesc_prop *bd) 243793fc096SFrank Li { 2447355f276STroy Kisky return (bdp >= bd->last) ? bd->base 245145d6e29SFugang Duan : (struct bufdesc *)(((void *)bdp) + bd->dsize); 246793fc096SFrank Li } 247793fc096SFrank Li 2487355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, 2497355f276STroy Kisky struct bufdesc_prop *bd) 25036e24e2eSDuan Fugang-B38611 { 2517355f276STroy Kisky return (bdp <= bd->base) ? bd->last 252145d6e29SFugang Duan : (struct bufdesc *)(((void *)bdp) - bd->dsize); 25336e24e2eSDuan Fugang-B38611 } 25436e24e2eSDuan Fugang-B38611 2557355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp, 2567355f276STroy Kisky struct bufdesc_prop *bd) 25761a4427bSNimrod Andy { 2587355f276STroy Kisky return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2; 25961a4427bSNimrod Andy } 26061a4427bSNimrod Andy 2617355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq) 2626e909283SNimrod Andy { 2636e909283SNimrod Andy int entries; 2646e909283SNimrod Andy 2657355f276STroy Kisky entries = (((const char *)txq->dirty_tx - 2667355f276STroy Kisky (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1; 2676e909283SNimrod Andy 2687355f276STroy Kisky return entries >= 0 ? entries : entries + txq->bd.ring_size; 2696e909283SNimrod Andy } 2706e909283SNimrod Andy 271c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len) 272793fc096SFrank Li { 273793fc096SFrank Li int i; 274793fc096SFrank Li unsigned int *buf = bufaddr; 275793fc096SFrank Li 2767b487d07SLothar Waßmann for (i = 0; i < len; i += 4, buf++) 277e453789aSLothar Waßmann swab32s(buf); 278793fc096SFrank Li } 279793fc096SFrank Li 2801310b544SLothar Waßmann static void swap_buffer2(void *dst_buf, void *src_buf, int len) 2811310b544SLothar Waßmann { 2821310b544SLothar Waßmann int i; 2831310b544SLothar Waßmann unsigned int *src = src_buf; 2841310b544SLothar Waßmann unsigned int *dst = dst_buf; 2851310b544SLothar Waßmann 2861310b544SLothar Waßmann for (i = 0; i < len; i += 4, src++, dst++) 2871310b544SLothar Waßmann *dst = swab32p(src); 2881310b544SLothar Waßmann } 2891310b544SLothar Waßmann 290344756f6SRussell King static void fec_dump(struct net_device *ndev) 291344756f6SRussell King { 292344756f6SRussell King struct fec_enet_private *fep = netdev_priv(ndev); 2934d494cdcSFugang Duan struct bufdesc *bdp; 2944d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 2954d494cdcSFugang Duan int index = 0; 296344756f6SRussell King 297344756f6SRussell King netdev_info(ndev, "TX ring dump\n"); 298344756f6SRussell King pr_info("Nr SC addr len SKB\n"); 299344756f6SRussell King 3004d494cdcSFugang Duan txq = fep->tx_queue[0]; 3017355f276STroy Kisky bdp = txq->bd.base; 3024d494cdcSFugang Duan 303344756f6SRussell King do { 3045cfa3039SJohannes Berg pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n", 305344756f6SRussell King index, 3067355f276STroy Kisky bdp == txq->bd.cur ? 'S' : ' ', 3074d494cdcSFugang Duan bdp == txq->dirty_tx ? 'H' : ' ', 3085cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_sc), 3095cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 3105cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_datlen), 3114d494cdcSFugang Duan txq->tx_skbuff[index]); 3127355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 313344756f6SRussell King index++; 3147355f276STroy Kisky } while (bdp != txq->bd.base); 315344756f6SRussell King } 316344756f6SRussell King 31762a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb) 31862a02c98SFugang Duan { 31962a02c98SFugang Duan return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4; 32062a02c98SFugang Duan } 32162a02c98SFugang Duan 3224c09eed9SJim Baxter static int 3234c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) 3244c09eed9SJim Baxter { 3254c09eed9SJim Baxter /* Only run for packets requiring a checksum. */ 3264c09eed9SJim Baxter if (skb->ip_summed != CHECKSUM_PARTIAL) 3274c09eed9SJim Baxter return 0; 3284c09eed9SJim Baxter 3294c09eed9SJim Baxter if (unlikely(skb_cow_head(skb, 0))) 3304c09eed9SJim Baxter return -1; 3314c09eed9SJim Baxter 33262a02c98SFugang Duan if (is_ipv4_pkt(skb)) 33396c50caaSNimrod Andy ip_hdr(skb)->check = 0; 3344c09eed9SJim Baxter *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; 3354c09eed9SJim Baxter 3364c09eed9SJim Baxter return 0; 3374c09eed9SJim Baxter } 3384c09eed9SJim Baxter 339c4bc44c6SKevin Hao static struct bufdesc * 3404d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq, 3414d494cdcSFugang Duan struct sk_buff *skb, 3424d494cdcSFugang Duan struct net_device *ndev) 3436e909283SNimrod Andy { 3446e909283SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 3457355f276STroy Kisky struct bufdesc *bdp = txq->bd.cur; 3466e909283SNimrod Andy struct bufdesc_ex *ebdp; 3476e909283SNimrod Andy int nr_frags = skb_shinfo(skb)->nr_frags; 3486e909283SNimrod Andy int frag, frag_len; 3496e909283SNimrod Andy unsigned short status; 3506e909283SNimrod Andy unsigned int estatus = 0; 3516e909283SNimrod Andy skb_frag_t *this_frag; 3526e909283SNimrod Andy unsigned int index; 3536e909283SNimrod Andy void *bufaddr; 354d6bf3143SRussell King dma_addr_t addr; 3556e909283SNimrod Andy int i; 3566e909283SNimrod Andy 3576e909283SNimrod Andy for (frag = 0; frag < nr_frags; frag++) { 3586e909283SNimrod Andy this_frag = &skb_shinfo(skb)->frags[frag]; 3597355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 3606e909283SNimrod Andy ebdp = (struct bufdesc_ex *)bdp; 3616e909283SNimrod Andy 3625cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 3636e909283SNimrod Andy status &= ~BD_ENET_TX_STATS; 3646e909283SNimrod Andy status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 3656e909283SNimrod Andy frag_len = skb_shinfo(skb)->frags[frag].size; 3666e909283SNimrod Andy 3676e909283SNimrod Andy /* Handle the last BD specially */ 3686e909283SNimrod Andy if (frag == nr_frags - 1) { 3696e909283SNimrod Andy status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 3706e909283SNimrod Andy if (fep->bufdesc_ex) { 3716e909283SNimrod Andy estatus |= BD_ENET_TX_INT; 3726e909283SNimrod Andy if (unlikely(skb_shinfo(skb)->tx_flags & 3736e909283SNimrod Andy SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 3746e909283SNimrod Andy estatus |= BD_ENET_TX_TS; 3756e909283SNimrod Andy } 3766e909283SNimrod Andy } 3776e909283SNimrod Andy 3786e909283SNimrod Andy if (fep->bufdesc_ex) { 3796b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 38053bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 3816e909283SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 3826e909283SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 3836e909283SNimrod Andy ebdp->cbd_bdu = 0; 3845cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 3856e909283SNimrod Andy } 3866e909283SNimrod Andy 3876e909283SNimrod Andy bufaddr = page_address(this_frag->page.p) + this_frag->page_offset; 3886e909283SNimrod Andy 3897355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 39041ef84ceSFugang Duan if (((unsigned long) bufaddr) & fep->tx_align || 3916b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 3924d494cdcSFugang Duan memcpy(txq->tx_bounce[index], bufaddr, frag_len); 3934d494cdcSFugang Duan bufaddr = txq->tx_bounce[index]; 3946e909283SNimrod Andy 3956b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 3966e909283SNimrod Andy swap_buffer(bufaddr, frag_len); 3976e909283SNimrod Andy } 3986e909283SNimrod Andy 399d6bf3143SRussell King addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len, 400d6bf3143SRussell King DMA_TO_DEVICE); 401d6bf3143SRussell King if (dma_mapping_error(&fep->pdev->dev, addr)) { 4026e909283SNimrod Andy if (net_ratelimit()) 4036e909283SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 4046e909283SNimrod Andy goto dma_mapping_error; 4056e909283SNimrod Andy } 4066e909283SNimrod Andy 4075cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(addr); 4085cfa3039SJohannes Berg bdp->cbd_datlen = cpu_to_fec16(frag_len); 409be293467STroy Kisky /* Make sure the updates to rest of the descriptor are 410be293467STroy Kisky * performed before transferring ownership. 411be293467STroy Kisky */ 412be293467STroy Kisky wmb(); 4135cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 4146e909283SNimrod Andy } 4156e909283SNimrod Andy 416c4bc44c6SKevin Hao return bdp; 4176e909283SNimrod Andy dma_mapping_error: 4187355f276STroy Kisky bdp = txq->bd.cur; 4196e909283SNimrod Andy for (i = 0; i < frag; i++) { 4207355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 4215cfa3039SJohannes Berg dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr), 4225cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE); 4236e909283SNimrod Andy } 424c4bc44c6SKevin Hao return ERR_PTR(-ENOMEM); 4256e909283SNimrod Andy } 4266e909283SNimrod Andy 4274d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, 4284d494cdcSFugang Duan struct sk_buff *skb, struct net_device *ndev) 4296e909283SNimrod Andy { 4306e909283SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 4316e909283SNimrod Andy int nr_frags = skb_shinfo(skb)->nr_frags; 4326e909283SNimrod Andy struct bufdesc *bdp, *last_bdp; 4336e909283SNimrod Andy void *bufaddr; 434d6bf3143SRussell King dma_addr_t addr; 4356e909283SNimrod Andy unsigned short status; 4366e909283SNimrod Andy unsigned short buflen; 4376e909283SNimrod Andy unsigned int estatus = 0; 4386e909283SNimrod Andy unsigned int index; 43979f33912SNimrod Andy int entries_free; 4406e909283SNimrod Andy 4417355f276STroy Kisky entries_free = fec_enet_get_free_txdesc_num(txq); 44279f33912SNimrod Andy if (entries_free < MAX_SKB_FRAGS + 1) { 44379f33912SNimrod Andy dev_kfree_skb_any(skb); 44479f33912SNimrod Andy if (net_ratelimit()) 44579f33912SNimrod Andy netdev_err(ndev, "NOT enough BD for SG!\n"); 44679f33912SNimrod Andy return NETDEV_TX_OK; 44779f33912SNimrod Andy } 44879f33912SNimrod Andy 4496e909283SNimrod Andy /* Protocol checksum off-load for TCP and UDP. */ 4506e909283SNimrod Andy if (fec_enet_clear_csum(skb, ndev)) { 4516e909283SNimrod Andy dev_kfree_skb_any(skb); 4526e909283SNimrod Andy return NETDEV_TX_OK; 4536e909283SNimrod Andy } 4546e909283SNimrod Andy 4556e909283SNimrod Andy /* Fill in a Tx ring entry */ 4567355f276STroy Kisky bdp = txq->bd.cur; 457c4bc44c6SKevin Hao last_bdp = bdp; 4585cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 4596e909283SNimrod Andy status &= ~BD_ENET_TX_STATS; 4606e909283SNimrod Andy 4616e909283SNimrod Andy /* Set buffer length and buffer pointer */ 4626e909283SNimrod Andy bufaddr = skb->data; 4636e909283SNimrod Andy buflen = skb_headlen(skb); 4646e909283SNimrod Andy 4657355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 46641ef84ceSFugang Duan if (((unsigned long) bufaddr) & fep->tx_align || 4676b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 4684d494cdcSFugang Duan memcpy(txq->tx_bounce[index], skb->data, buflen); 4694d494cdcSFugang Duan bufaddr = txq->tx_bounce[index]; 4706e909283SNimrod Andy 4716b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 4726e909283SNimrod Andy swap_buffer(bufaddr, buflen); 4736e909283SNimrod Andy } 4746e909283SNimrod Andy 475d6bf3143SRussell King /* Push the data cache so the CPM does not get stale memory data. */ 476d6bf3143SRussell King addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE); 477d6bf3143SRussell King if (dma_mapping_error(&fep->pdev->dev, addr)) { 4786e909283SNimrod Andy dev_kfree_skb_any(skb); 4796e909283SNimrod Andy if (net_ratelimit()) 4806e909283SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 4816e909283SNimrod Andy return NETDEV_TX_OK; 4826e909283SNimrod Andy } 4836e909283SNimrod Andy 4846e909283SNimrod Andy if (nr_frags) { 485c4bc44c6SKevin Hao last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev); 486fc75ba51STroy Kisky if (IS_ERR(last_bdp)) { 487fc75ba51STroy Kisky dma_unmap_single(&fep->pdev->dev, addr, 488fc75ba51STroy Kisky buflen, DMA_TO_DEVICE); 489fc75ba51STroy Kisky dev_kfree_skb_any(skb); 490c4bc44c6SKevin Hao return NETDEV_TX_OK; 491fc75ba51STroy Kisky } 4926e909283SNimrod Andy } else { 4936e909283SNimrod Andy status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 4946e909283SNimrod Andy if (fep->bufdesc_ex) { 4956e909283SNimrod Andy estatus = BD_ENET_TX_INT; 4966e909283SNimrod Andy if (unlikely(skb_shinfo(skb)->tx_flags & 4976e909283SNimrod Andy SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 4986e909283SNimrod Andy estatus |= BD_ENET_TX_TS; 4996e909283SNimrod Andy } 5006e909283SNimrod Andy } 501fc75ba51STroy Kisky bdp->cbd_bufaddr = cpu_to_fec32(addr); 502fc75ba51STroy Kisky bdp->cbd_datlen = cpu_to_fec16(buflen); 5036e909283SNimrod Andy 5046e909283SNimrod Andy if (fep->bufdesc_ex) { 5056e909283SNimrod Andy 5066e909283SNimrod Andy struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 5076e909283SNimrod Andy 5086e909283SNimrod Andy if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 5096e909283SNimrod Andy fep->hwts_tx_en)) 5106e909283SNimrod Andy skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 5116e909283SNimrod Andy 5126b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 51353bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 514befe8213SNimrod Andy 5156e909283SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 5166e909283SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 5176e909283SNimrod Andy 5186e909283SNimrod Andy ebdp->cbd_bdu = 0; 5195cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 5206e909283SNimrod Andy } 5216e909283SNimrod Andy 5227355f276STroy Kisky index = fec_enet_get_bd_index(last_bdp, &txq->bd); 5236e909283SNimrod Andy /* Save skb pointer */ 5244d494cdcSFugang Duan txq->tx_skbuff[index] = skb; 5256e909283SNimrod Andy 526be293467STroy Kisky /* Make sure the updates to rest of the descriptor are performed before 527be293467STroy Kisky * transferring ownership. 528be293467STroy Kisky */ 529be293467STroy Kisky wmb(); 5306e909283SNimrod Andy 5316e909283SNimrod Andy /* Send it on its way. Tell FEC it's ready, interrupt when done, 5326e909283SNimrod Andy * it's the last BD of the frame, and to put the CRC on the end. 5336e909283SNimrod Andy */ 5346e909283SNimrod Andy status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); 5355cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 5366e909283SNimrod Andy 537793fc096SFrank Li /* If this was the last BD in the ring, start at the beginning again. */ 5387355f276STroy Kisky bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd); 539793fc096SFrank Li 5407a2a8451SEric Dumazet skb_tx_timestamp(skb); 5417a2a8451SEric Dumazet 542c4bc44c6SKevin Hao /* Make sure the update to bdp and tx_skbuff are performed before 5437355f276STroy Kisky * txq->bd.cur. 544c4bc44c6SKevin Hao */ 545c4bc44c6SKevin Hao wmb(); 5467355f276STroy Kisky txq->bd.cur = bdp; 547793fc096SFrank Li 548793fc096SFrank Li /* Trigger transmission start */ 54953bb20d1STroy Kisky writel(0, txq->bd.reg_desc_active); 550793fc096SFrank Li 5516e909283SNimrod Andy return 0; 552793fc096SFrank Li } 553793fc096SFrank Li 55479f33912SNimrod Andy static int 5554d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb, 5564d494cdcSFugang Duan struct net_device *ndev, 55779f33912SNimrod Andy struct bufdesc *bdp, int index, char *data, 55879f33912SNimrod Andy int size, bool last_tcp, bool is_last) 55979f33912SNimrod Andy { 56079f33912SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 56161cd2ebbSFabian Frederick struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 56279f33912SNimrod Andy unsigned short status; 56379f33912SNimrod Andy unsigned int estatus = 0; 564d6bf3143SRussell King dma_addr_t addr; 56579f33912SNimrod Andy 5665cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 56779f33912SNimrod Andy status &= ~BD_ENET_TX_STATS; 56879f33912SNimrod Andy 56979f33912SNimrod Andy status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 57079f33912SNimrod Andy 57141ef84ceSFugang Duan if (((unsigned long) data) & fep->tx_align || 5726b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 5734d494cdcSFugang Duan memcpy(txq->tx_bounce[index], data, size); 5744d494cdcSFugang Duan data = txq->tx_bounce[index]; 57579f33912SNimrod Andy 5766b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 57779f33912SNimrod Andy swap_buffer(data, size); 57879f33912SNimrod Andy } 57979f33912SNimrod Andy 580d6bf3143SRussell King addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE); 581d6bf3143SRussell King if (dma_mapping_error(&fep->pdev->dev, addr)) { 58279f33912SNimrod Andy dev_kfree_skb_any(skb); 58379f33912SNimrod Andy if (net_ratelimit()) 58479f33912SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 58579f33912SNimrod Andy return NETDEV_TX_BUSY; 58679f33912SNimrod Andy } 58779f33912SNimrod Andy 5885cfa3039SJohannes Berg bdp->cbd_datlen = cpu_to_fec16(size); 5895cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(addr); 590d6bf3143SRussell King 59179f33912SNimrod Andy if (fep->bufdesc_ex) { 5926b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 59353bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 59479f33912SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 59579f33912SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 59679f33912SNimrod Andy ebdp->cbd_bdu = 0; 5975cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 59879f33912SNimrod Andy } 59979f33912SNimrod Andy 60079f33912SNimrod Andy /* Handle the last BD specially */ 60179f33912SNimrod Andy if (last_tcp) 60279f33912SNimrod Andy status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC); 60379f33912SNimrod Andy if (is_last) { 60479f33912SNimrod Andy status |= BD_ENET_TX_INTR; 60579f33912SNimrod Andy if (fep->bufdesc_ex) 6065cfa3039SJohannes Berg ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT); 60779f33912SNimrod Andy } 60879f33912SNimrod Andy 6095cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 61079f33912SNimrod Andy 61179f33912SNimrod Andy return 0; 61279f33912SNimrod Andy } 61379f33912SNimrod Andy 61479f33912SNimrod Andy static int 6154d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq, 6164d494cdcSFugang Duan struct sk_buff *skb, struct net_device *ndev, 61779f33912SNimrod Andy struct bufdesc *bdp, int index) 61879f33912SNimrod Andy { 61979f33912SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 62079f33912SNimrod Andy int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 62161cd2ebbSFabian Frederick struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 62279f33912SNimrod Andy void *bufaddr; 62379f33912SNimrod Andy unsigned long dmabuf; 62479f33912SNimrod Andy unsigned short status; 62579f33912SNimrod Andy unsigned int estatus = 0; 62679f33912SNimrod Andy 6275cfa3039SJohannes Berg status = fec16_to_cpu(bdp->cbd_sc); 62879f33912SNimrod Andy status &= ~BD_ENET_TX_STATS; 62979f33912SNimrod Andy status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 63079f33912SNimrod Andy 6314d494cdcSFugang Duan bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 6324d494cdcSFugang Duan dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE; 63341ef84ceSFugang Duan if (((unsigned long)bufaddr) & fep->tx_align || 6346b7e4008SLothar Waßmann fep->quirks & FEC_QUIRK_SWAP_FRAME) { 6354d494cdcSFugang Duan memcpy(txq->tx_bounce[index], skb->data, hdr_len); 6364d494cdcSFugang Duan bufaddr = txq->tx_bounce[index]; 63779f33912SNimrod Andy 6386b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 63979f33912SNimrod Andy swap_buffer(bufaddr, hdr_len); 64079f33912SNimrod Andy 64179f33912SNimrod Andy dmabuf = dma_map_single(&fep->pdev->dev, bufaddr, 64279f33912SNimrod Andy hdr_len, DMA_TO_DEVICE); 64379f33912SNimrod Andy if (dma_mapping_error(&fep->pdev->dev, dmabuf)) { 64479f33912SNimrod Andy dev_kfree_skb_any(skb); 64579f33912SNimrod Andy if (net_ratelimit()) 64679f33912SNimrod Andy netdev_err(ndev, "Tx DMA memory map failed\n"); 64779f33912SNimrod Andy return NETDEV_TX_BUSY; 64879f33912SNimrod Andy } 64979f33912SNimrod Andy } 65079f33912SNimrod Andy 6515cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(dmabuf); 6525cfa3039SJohannes Berg bdp->cbd_datlen = cpu_to_fec16(hdr_len); 65379f33912SNimrod Andy 65479f33912SNimrod Andy if (fep->bufdesc_ex) { 6556b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) 65653bb20d1STroy Kisky estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 65779f33912SNimrod Andy if (skb->ip_summed == CHECKSUM_PARTIAL) 65879f33912SNimrod Andy estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 65979f33912SNimrod Andy ebdp->cbd_bdu = 0; 6605cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(estatus); 66179f33912SNimrod Andy } 66279f33912SNimrod Andy 6635cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(status); 66479f33912SNimrod Andy 66579f33912SNimrod Andy return 0; 66679f33912SNimrod Andy } 66779f33912SNimrod Andy 6684d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq, 6694d494cdcSFugang Duan struct sk_buff *skb, 6704d494cdcSFugang Duan struct net_device *ndev) 67179f33912SNimrod Andy { 67279f33912SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 67379f33912SNimrod Andy int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 67479f33912SNimrod Andy int total_len, data_left; 6757355f276STroy Kisky struct bufdesc *bdp = txq->bd.cur; 67679f33912SNimrod Andy struct tso_t tso; 67779f33912SNimrod Andy unsigned int index = 0; 67879f33912SNimrod Andy int ret; 67979f33912SNimrod Andy 6807355f276STroy Kisky if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) { 68179f33912SNimrod Andy dev_kfree_skb_any(skb); 68279f33912SNimrod Andy if (net_ratelimit()) 68379f33912SNimrod Andy netdev_err(ndev, "NOT enough BD for TSO!\n"); 68479f33912SNimrod Andy return NETDEV_TX_OK; 68579f33912SNimrod Andy } 68679f33912SNimrod Andy 68779f33912SNimrod Andy /* Protocol checksum off-load for TCP and UDP. */ 68879f33912SNimrod Andy if (fec_enet_clear_csum(skb, ndev)) { 68979f33912SNimrod Andy dev_kfree_skb_any(skb); 69079f33912SNimrod Andy return NETDEV_TX_OK; 69179f33912SNimrod Andy } 69279f33912SNimrod Andy 69379f33912SNimrod Andy /* Initialize the TSO handler, and prepare the first payload */ 69479f33912SNimrod Andy tso_start(skb, &tso); 69579f33912SNimrod Andy 69679f33912SNimrod Andy total_len = skb->len - hdr_len; 69779f33912SNimrod Andy while (total_len > 0) { 69879f33912SNimrod Andy char *hdr; 69979f33912SNimrod Andy 7007355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 70179f33912SNimrod Andy data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 70279f33912SNimrod Andy total_len -= data_left; 70379f33912SNimrod Andy 70479f33912SNimrod Andy /* prepare packet headers: MAC + IP + TCP */ 7054d494cdcSFugang Duan hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 70679f33912SNimrod Andy tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 7074d494cdcSFugang Duan ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index); 70879f33912SNimrod Andy if (ret) 70979f33912SNimrod Andy goto err_release; 71079f33912SNimrod Andy 71179f33912SNimrod Andy while (data_left > 0) { 71279f33912SNimrod Andy int size; 71379f33912SNimrod Andy 71479f33912SNimrod Andy size = min_t(int, tso.size, data_left); 7157355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 7167355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 7174d494cdcSFugang Duan ret = fec_enet_txq_put_data_tso(txq, skb, ndev, 7184d494cdcSFugang Duan bdp, index, 7194d494cdcSFugang Duan tso.data, size, 7204d494cdcSFugang Duan size == data_left, 72179f33912SNimrod Andy total_len == 0); 72279f33912SNimrod Andy if (ret) 72379f33912SNimrod Andy goto err_release; 72479f33912SNimrod Andy 72579f33912SNimrod Andy data_left -= size; 72679f33912SNimrod Andy tso_build_data(skb, &tso, size); 72779f33912SNimrod Andy } 72879f33912SNimrod Andy 7297355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 73079f33912SNimrod Andy } 73179f33912SNimrod Andy 73279f33912SNimrod Andy /* Save skb pointer */ 7334d494cdcSFugang Duan txq->tx_skbuff[index] = skb; 73479f33912SNimrod Andy 73579f33912SNimrod Andy skb_tx_timestamp(skb); 7367355f276STroy Kisky txq->bd.cur = bdp; 73779f33912SNimrod Andy 73879f33912SNimrod Andy /* Trigger transmission start */ 7396b7e4008SLothar Waßmann if (!(fep->quirks & FEC_QUIRK_ERR007885) || 74053bb20d1STroy Kisky !readl(txq->bd.reg_desc_active) || 74153bb20d1STroy Kisky !readl(txq->bd.reg_desc_active) || 74253bb20d1STroy Kisky !readl(txq->bd.reg_desc_active) || 74353bb20d1STroy Kisky !readl(txq->bd.reg_desc_active)) 74453bb20d1STroy Kisky writel(0, txq->bd.reg_desc_active); 74579f33912SNimrod Andy 74679f33912SNimrod Andy return 0; 74779f33912SNimrod Andy 74879f33912SNimrod Andy err_release: 74979f33912SNimrod Andy /* TODO: Release all used data descriptors for TSO */ 75079f33912SNimrod Andy return ret; 75179f33912SNimrod Andy } 75279f33912SNimrod Andy 75361a4427bSNimrod Andy static netdev_tx_t 75461a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) 75561a4427bSNimrod Andy { 75661a4427bSNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 7576e909283SNimrod Andy int entries_free; 7584d494cdcSFugang Duan unsigned short queue; 7594d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 7604d494cdcSFugang Duan struct netdev_queue *nq; 76161a4427bSNimrod Andy int ret; 76261a4427bSNimrod Andy 7634d494cdcSFugang Duan queue = skb_get_queue_mapping(skb); 7644d494cdcSFugang Duan txq = fep->tx_queue[queue]; 7654d494cdcSFugang Duan nq = netdev_get_tx_queue(ndev, queue); 7664d494cdcSFugang Duan 76779f33912SNimrod Andy if (skb_is_gso(skb)) 7684d494cdcSFugang Duan ret = fec_enet_txq_submit_tso(txq, skb, ndev); 76979f33912SNimrod Andy else 7704d494cdcSFugang Duan ret = fec_enet_txq_submit_skb(txq, skb, ndev); 7716e909283SNimrod Andy if (ret) 7726e909283SNimrod Andy return ret; 77361a4427bSNimrod Andy 7747355f276STroy Kisky entries_free = fec_enet_get_free_txdesc_num(txq); 7754d494cdcSFugang Duan if (entries_free <= txq->tx_stop_threshold) 7764d494cdcSFugang Duan netif_tx_stop_queue(nq); 77761a4427bSNimrod Andy 77861a4427bSNimrod Andy return NETDEV_TX_OK; 77961a4427bSNimrod Andy } 78061a4427bSNimrod Andy 781a210576cSDavid S. Miller /* Init RX & TX buffer descriptors 782a210576cSDavid S. Miller */ 783a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev) 784a210576cSDavid S. Miller { 785a210576cSDavid S. Miller struct fec_enet_private *fep = netdev_priv(dev); 7864d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 7874d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 788a210576cSDavid S. Miller struct bufdesc *bdp; 789a210576cSDavid S. Miller unsigned int i; 79059d0f746SFrank Li unsigned int q; 791a210576cSDavid S. Miller 79259d0f746SFrank Li for (q = 0; q < fep->num_rx_queues; q++) { 793a210576cSDavid S. Miller /* Initialize the receive buffer descriptors. */ 79459d0f746SFrank Li rxq = fep->rx_queue[q]; 7957355f276STroy Kisky bdp = rxq->bd.base; 7964d494cdcSFugang Duan 7977355f276STroy Kisky for (i = 0; i < rxq->bd.ring_size; i++) { 798a210576cSDavid S. Miller 799a210576cSDavid S. Miller /* Initialize the BD for every fragment in the page. */ 800a210576cSDavid S. Miller if (bdp->cbd_bufaddr) 8015cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 802a210576cSDavid S. Miller else 8035cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(0); 8047355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 805a210576cSDavid S. Miller } 806a210576cSDavid S. Miller 807a210576cSDavid S. Miller /* Set the last buffer to wrap */ 8087355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 8095cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 810a210576cSDavid S. Miller 8117355f276STroy Kisky rxq->bd.cur = rxq->bd.base; 81259d0f746SFrank Li } 813a210576cSDavid S. Miller 81459d0f746SFrank Li for (q = 0; q < fep->num_tx_queues; q++) { 815a210576cSDavid S. Miller /* ...and the same for transmit */ 81659d0f746SFrank Li txq = fep->tx_queue[q]; 8177355f276STroy Kisky bdp = txq->bd.base; 8187355f276STroy Kisky txq->bd.cur = bdp; 819a210576cSDavid S. Miller 8207355f276STroy Kisky for (i = 0; i < txq->bd.ring_size; i++) { 821a210576cSDavid S. Miller /* Initialize the BD for every fragment in the page. */ 8225cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(0); 823178e5f57SFugang Duan if (bdp->cbd_bufaddr && 824178e5f57SFugang Duan !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 825178e5f57SFugang Duan dma_unmap_single(&fep->pdev->dev, 826178e5f57SFugang Duan fec32_to_cpu(bdp->cbd_bufaddr), 827178e5f57SFugang Duan fec16_to_cpu(bdp->cbd_datlen), 828178e5f57SFugang Duan DMA_TO_DEVICE); 8294d494cdcSFugang Duan if (txq->tx_skbuff[i]) { 8304d494cdcSFugang Duan dev_kfree_skb_any(txq->tx_skbuff[i]); 8314d494cdcSFugang Duan txq->tx_skbuff[i] = NULL; 832a210576cSDavid S. Miller } 8335cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(0); 8347355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 835a210576cSDavid S. Miller } 836a210576cSDavid S. Miller 837a210576cSDavid S. Miller /* Set the last buffer to wrap */ 8387355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 8395cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 8404d494cdcSFugang Duan txq->dirty_tx = bdp; 841a210576cSDavid S. Miller } 84259d0f746SFrank Li } 84359d0f746SFrank Li 844ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev) 845ce99d0d3SFrank Li { 846ce99d0d3SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 847ce99d0d3SFrank Li int i; 848ce99d0d3SFrank Li 849ce99d0d3SFrank Li for (i = 0; i < fep->num_rx_queues; i++) 85053bb20d1STroy Kisky writel(0, fep->rx_queue[i]->bd.reg_desc_active); 851ce99d0d3SFrank Li } 852ce99d0d3SFrank Li 85359d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev) 85459d0f746SFrank Li { 85559d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 85659d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 85759d0f746SFrank Li struct fec_enet_priv_rx_q *rxq; 85859d0f746SFrank Li int i; 85959d0f746SFrank Li 86059d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) { 86159d0f746SFrank Li rxq = fep->rx_queue[i]; 8627355f276STroy Kisky writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i)); 863fbbeefddSAndrew Lunn writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i)); 86459d0f746SFrank Li 86559d0f746SFrank Li /* enable DMA1/2 */ 86659d0f746SFrank Li if (i) 86759d0f746SFrank Li writel(RCMR_MATCHEN | RCMR_CMP(i), 86859d0f746SFrank Li fep->hwp + FEC_RCMR(i)); 86959d0f746SFrank Li } 87059d0f746SFrank Li 87159d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 87259d0f746SFrank Li txq = fep->tx_queue[i]; 8737355f276STroy Kisky writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i)); 87459d0f746SFrank Li 87559d0f746SFrank Li /* enable DMA1/2 */ 87659d0f746SFrank Li if (i) 87759d0f746SFrank Li writel(DMA_CLASS_EN | IDLE_SLOPE(i), 87859d0f746SFrank Li fep->hwp + FEC_DMA_CFG(i)); 87959d0f746SFrank Li } 88059d0f746SFrank Li } 88159d0f746SFrank Li 88259d0f746SFrank Li static void fec_enet_reset_skb(struct net_device *ndev) 88359d0f746SFrank Li { 88459d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 88559d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 88659d0f746SFrank Li int i, j; 88759d0f746SFrank Li 88859d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 88959d0f746SFrank Li txq = fep->tx_queue[i]; 89059d0f746SFrank Li 8917355f276STroy Kisky for (j = 0; j < txq->bd.ring_size; j++) { 89259d0f746SFrank Li if (txq->tx_skbuff[j]) { 89359d0f746SFrank Li dev_kfree_skb_any(txq->tx_skbuff[j]); 89459d0f746SFrank Li txq->tx_skbuff[j] = NULL; 89559d0f746SFrank Li } 89659d0f746SFrank Li } 89759d0f746SFrank Li } 89859d0f746SFrank Li } 899a210576cSDavid S. Miller 900dbc64a8eSRussell King /* 901dbc64a8eSRussell King * This function is called to start or restart the FEC during a link 902dbc64a8eSRussell King * change, transmit timeout, or to reconfigure the FEC. The network 903dbc64a8eSRussell King * packet processing for this device must be stopped before this call. 904793fc096SFrank Li */ 905793fc096SFrank Li static void 906ef83337dSRussell King fec_restart(struct net_device *ndev) 907793fc096SFrank Li { 908793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 9094c09eed9SJim Baxter u32 val; 910793fc096SFrank Li u32 temp_mac[2]; 911793fc096SFrank Li u32 rcntl = OPT_FRAME_SIZE | 0x04; 912793fc096SFrank Li u32 ecntl = 0x2; /* ETHEREN */ 913793fc096SFrank Li 914106c314cSFugang Duan /* Whack a reset. We should wait for this. 915106c314cSFugang Duan * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 916106c314cSFugang Duan * instead of reset MAC itself. 917106c314cSFugang Duan */ 9186b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) { 919106c314cSFugang Duan writel(0, fep->hwp + FEC_ECNTRL); 920106c314cSFugang Duan } else { 921793fc096SFrank Li writel(1, fep->hwp + FEC_ECNTRL); 922793fc096SFrank Li udelay(10); 923106c314cSFugang Duan } 924793fc096SFrank Li 925793fc096SFrank Li /* 926793fc096SFrank Li * enet-mac reset will reset mac address registers too, 927793fc096SFrank Li * so need to reconfigure it. 928793fc096SFrank Li */ 929793fc096SFrank Li memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); 9305cfa3039SJohannes Berg writel((__force u32)cpu_to_be32(temp_mac[0]), 9315cfa3039SJohannes Berg fep->hwp + FEC_ADDR_LOW); 9325cfa3039SJohannes Berg writel((__force u32)cpu_to_be32(temp_mac[1]), 9335cfa3039SJohannes Berg fep->hwp + FEC_ADDR_HIGH); 934793fc096SFrank Li 935793fc096SFrank Li /* Clear any outstanding interrupt. */ 936e17f7fecSNimrod Andy writel(0xffffffff, fep->hwp + FEC_IEVENT); 937793fc096SFrank Li 938a210576cSDavid S. Miller fec_enet_bd_init(ndev); 939a210576cSDavid S. Miller 94059d0f746SFrank Li fec_enet_enable_ring(ndev); 941793fc096SFrank Li 94259d0f746SFrank Li /* Reset tx SKB buffers. */ 94359d0f746SFrank Li fec_enet_reset_skb(ndev); 944793fc096SFrank Li 945793fc096SFrank Li /* Enable MII mode */ 946ef83337dSRussell King if (fep->full_duplex == DUPLEX_FULL) { 947793fc096SFrank Li /* FD enable */ 948793fc096SFrank Li writel(0x04, fep->hwp + FEC_X_CNTRL); 949793fc096SFrank Li } else { 950793fc096SFrank Li /* No Rcv on Xmit */ 951793fc096SFrank Li rcntl |= 0x02; 952793fc096SFrank Li writel(0x0, fep->hwp + FEC_X_CNTRL); 953793fc096SFrank Li } 954793fc096SFrank Li 955793fc096SFrank Li /* Set MII speed */ 956793fc096SFrank Li writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 957793fc096SFrank Li 958d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 95918803495SGreg Ungerer if (fep->quirks & FEC_QUIRK_HAS_RACC) { 9604c09eed9SJim Baxter val = readl(fep->hwp + FEC_RACC); 9613ac72b7bSEric Nelson /* align IP header */ 9623ac72b7bSEric Nelson val |= FEC_RACC_SHIFT16; 9634c09eed9SJim Baxter if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) 9643ac72b7bSEric Nelson /* set RX checksum */ 9654c09eed9SJim Baxter val |= FEC_RACC_OPTIONS; 9664c09eed9SJim Baxter else 9674c09eed9SJim Baxter val &= ~FEC_RACC_OPTIONS; 9684c09eed9SJim Baxter writel(val, fep->hwp + FEC_RACC); 96955cd48c8STroy Kisky writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL); 97032867fccSFabio Estevam } 971d1391930SGuenter Roeck #endif 9724c09eed9SJim Baxter 973793fc096SFrank Li /* 974793fc096SFrank Li * The phy interface and speed need to get configured 975793fc096SFrank Li * differently on enet-mac. 976793fc096SFrank Li */ 9776b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_ENET_MAC) { 978793fc096SFrank Li /* Enable flow control and length check */ 979793fc096SFrank Li rcntl |= 0x40000000 | 0x00000020; 980793fc096SFrank Li 981793fc096SFrank Li /* RGMII, RMII or MII */ 982e813bb2bSMarkus Pargmann if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII || 983e813bb2bSMarkus Pargmann fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID || 984e813bb2bSMarkus Pargmann fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID || 985e813bb2bSMarkus Pargmann fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) 986793fc096SFrank Li rcntl |= (1 << 6); 987793fc096SFrank Li else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 988793fc096SFrank Li rcntl |= (1 << 8); 989793fc096SFrank Li else 990793fc096SFrank Li rcntl &= ~(1 << 8); 991793fc096SFrank Li 992793fc096SFrank Li /* 1G, 100M or 10M */ 99345f5c327SPhilippe Reynes if (ndev->phydev) { 99445f5c327SPhilippe Reynes if (ndev->phydev->speed == SPEED_1000) 995793fc096SFrank Li ecntl |= (1 << 5); 99645f5c327SPhilippe Reynes else if (ndev->phydev->speed == SPEED_100) 997793fc096SFrank Li rcntl &= ~(1 << 9); 998793fc096SFrank Li else 999793fc096SFrank Li rcntl |= (1 << 9); 1000793fc096SFrank Li } 1001793fc096SFrank Li } else { 1002793fc096SFrank Li #ifdef FEC_MIIGSK_ENR 10036b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_USE_GASKET) { 1004793fc096SFrank Li u32 cfgr; 1005793fc096SFrank Li /* disable the gasket and wait */ 1006793fc096SFrank Li writel(0, fep->hwp + FEC_MIIGSK_ENR); 1007793fc096SFrank Li while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) 1008793fc096SFrank Li udelay(1); 1009793fc096SFrank Li 1010793fc096SFrank Li /* 1011793fc096SFrank Li * configure the gasket: 1012793fc096SFrank Li * RMII, 50 MHz, no loopback, no echo 1013793fc096SFrank Li * MII, 25 MHz, no loopback, no echo 1014793fc096SFrank Li */ 1015793fc096SFrank Li cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1016793fc096SFrank Li ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; 101745f5c327SPhilippe Reynes if (ndev->phydev && ndev->phydev->speed == SPEED_10) 1018793fc096SFrank Li cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; 1019793fc096SFrank Li writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); 1020793fc096SFrank Li 1021793fc096SFrank Li /* re-enable the gasket */ 1022793fc096SFrank Li writel(2, fep->hwp + FEC_MIIGSK_ENR); 1023793fc096SFrank Li } 1024793fc096SFrank Li #endif 1025793fc096SFrank Li } 1026793fc096SFrank Li 1027d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 1028793fc096SFrank Li /* enable pause frame*/ 1029793fc096SFrank Li if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || 1030793fc096SFrank Li ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && 103145f5c327SPhilippe Reynes ndev->phydev && ndev->phydev->pause)) { 1032793fc096SFrank Li rcntl |= FEC_ENET_FCE; 1033793fc096SFrank Li 10344c09eed9SJim Baxter /* set FIFO threshold parameter to reduce overrun */ 1035793fc096SFrank Li writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); 1036793fc096SFrank Li writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); 1037793fc096SFrank Li writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); 1038793fc096SFrank Li writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); 1039793fc096SFrank Li 1040793fc096SFrank Li /* OPD */ 1041793fc096SFrank Li writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); 1042793fc096SFrank Li } else { 1043793fc096SFrank Li rcntl &= ~FEC_ENET_FCE; 1044793fc096SFrank Li } 1045d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */ 1046793fc096SFrank Li 1047793fc096SFrank Li writel(rcntl, fep->hwp + FEC_R_CNTRL); 1048793fc096SFrank Li 104984fe6182SStefan Wahren /* Setup multicast filter. */ 105084fe6182SStefan Wahren set_multicast_list(ndev); 105184fe6182SStefan Wahren #ifndef CONFIG_M5272 105284fe6182SStefan Wahren writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); 105384fe6182SStefan Wahren writel(0, fep->hwp + FEC_HASH_TABLE_LOW); 105484fe6182SStefan Wahren #endif 105584fe6182SStefan Wahren 10566b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1057793fc096SFrank Li /* enable ENET endian swap */ 1058793fc096SFrank Li ecntl |= (1 << 8); 1059793fc096SFrank Li /* enable ENET store and forward mode */ 1060793fc096SFrank Li writel(1 << 8, fep->hwp + FEC_X_WMRK); 1061793fc096SFrank Li } 1062793fc096SFrank Li 1063793fc096SFrank Li if (fep->bufdesc_ex) 1064793fc096SFrank Li ecntl |= (1 << 4); 1065793fc096SFrank Li 106638ae92dcSChris Healy #ifndef CONFIG_M5272 1067b9eef55cSJim Baxter /* Enable the MIB statistic event counters */ 1068b9eef55cSJim Baxter writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT); 106938ae92dcSChris Healy #endif 107038ae92dcSChris Healy 1071793fc096SFrank Li /* And last, enable the transmit and receive processing */ 1072793fc096SFrank Li writel(ecntl, fep->hwp + FEC_ECNTRL); 1073ce99d0d3SFrank Li fec_enet_active_rxring(ndev); 1074793fc096SFrank Li 1075793fc096SFrank Li if (fep->bufdesc_ex) 1076793fc096SFrank Li fec_ptp_start_cyclecounter(ndev); 1077793fc096SFrank Li 1078793fc096SFrank Li /* Enable interrupts we wish to service */ 10790c5a3aefSNimrod Andy if (fep->link) 1080793fc096SFrank Li writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 10810c5a3aefSNimrod Andy else 10820c5a3aefSNimrod Andy writel(FEC_ENET_MII, fep->hwp + FEC_IMASK); 1083d851b47bSFugang Duan 1084d851b47bSFugang Duan /* Init the interrupt coalescing */ 1085d851b47bSFugang Duan fec_enet_itr_coal_init(ndev); 1086d851b47bSFugang Duan 1087793fc096SFrank Li } 1088793fc096SFrank Li 1089793fc096SFrank Li static void 1090793fc096SFrank Li fec_stop(struct net_device *ndev) 1091793fc096SFrank Li { 1092793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1093de40ed31SNimrod Andy struct fec_platform_data *pdata = fep->pdev->dev.platform_data; 1094793fc096SFrank Li u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8); 1095de40ed31SNimrod Andy u32 val; 1096793fc096SFrank Li 1097793fc096SFrank Li /* We cannot expect a graceful transmit stop without link !!! */ 1098793fc096SFrank Li if (fep->link) { 1099793fc096SFrank Li writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ 1100793fc096SFrank Li udelay(10); 1101793fc096SFrank Li if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) 110231b7720cSJoe Perches netdev_err(ndev, "Graceful transmit stop did not complete!\n"); 1103793fc096SFrank Li } 1104793fc096SFrank Li 1105106c314cSFugang Duan /* Whack a reset. We should wait for this. 1106106c314cSFugang Duan * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1107106c314cSFugang Duan * instead of reset MAC itself. 1108106c314cSFugang Duan */ 1109de40ed31SNimrod Andy if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 11106b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) { 1111106c314cSFugang Duan writel(0, fep->hwp + FEC_ECNTRL); 1112106c314cSFugang Duan } else { 1113793fc096SFrank Li writel(1, fep->hwp + FEC_ECNTRL); 1114793fc096SFrank Li udelay(10); 1115106c314cSFugang Duan } 1116793fc096SFrank Li writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1117de40ed31SNimrod Andy } else { 1118de40ed31SNimrod Andy writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK); 1119de40ed31SNimrod Andy val = readl(fep->hwp + FEC_ECNTRL); 1120de40ed31SNimrod Andy val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 1121de40ed31SNimrod Andy writel(val, fep->hwp + FEC_ECNTRL); 1122de40ed31SNimrod Andy 1123de40ed31SNimrod Andy if (pdata && pdata->sleep_mode_enable) 1124de40ed31SNimrod Andy pdata->sleep_mode_enable(true); 1125de40ed31SNimrod Andy } 1126de40ed31SNimrod Andy writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1127793fc096SFrank Li 1128793fc096SFrank Li /* We have to keep ENET enabled to have MII interrupt stay working */ 1129de40ed31SNimrod Andy if (fep->quirks & FEC_QUIRK_ENET_MAC && 1130de40ed31SNimrod Andy !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 1131793fc096SFrank Li writel(2, fep->hwp + FEC_ECNTRL); 1132793fc096SFrank Li writel(rmii_mode, fep->hwp + FEC_R_CNTRL); 1133793fc096SFrank Li } 1134793fc096SFrank Li } 1135793fc096SFrank Li 1136793fc096SFrank Li 1137793fc096SFrank Li static void 1138793fc096SFrank Li fec_timeout(struct net_device *ndev) 1139793fc096SFrank Li { 1140793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1141793fc096SFrank Li 1142344756f6SRussell King fec_dump(ndev); 1143344756f6SRussell King 1144793fc096SFrank Li ndev->stats.tx_errors++; 1145793fc096SFrank Li 114636cdc743SRussell King schedule_work(&fep->tx_timeout_work); 114754309fa6SFrank Li } 114854309fa6SFrank Li 114936cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work) 115054309fa6SFrank Li { 115154309fa6SFrank Li struct fec_enet_private *fep = 115236cdc743SRussell King container_of(work, struct fec_enet_private, tx_timeout_work); 11538ce5624fSRussell King struct net_device *ndev = fep->netdev; 115454309fa6SFrank Li 1155da1774e5SRussell King rtnl_lock(); 11568ce5624fSRussell King if (netif_device_present(ndev) || netif_running(ndev)) { 1157dbc64a8eSRussell King napi_disable(&fep->napi); 1158dbc64a8eSRussell King netif_tx_lock_bh(ndev); 1159ef83337dSRussell King fec_restart(ndev); 11608ce5624fSRussell King netif_wake_queue(ndev); 11616af42d42SRussell King netif_tx_unlock_bh(ndev); 1162dbc64a8eSRussell King napi_enable(&fep->napi); 11638ce5624fSRussell King } 1164da1774e5SRussell King rtnl_unlock(); 116554309fa6SFrank Li } 1166793fc096SFrank Li 1167793fc096SFrank Li static void 1168bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts, 1169bfd4ecddSRussell King struct skb_shared_hwtstamps *hwtstamps) 1170bfd4ecddSRussell King { 1171bfd4ecddSRussell King unsigned long flags; 1172bfd4ecddSRussell King u64 ns; 1173bfd4ecddSRussell King 1174bfd4ecddSRussell King spin_lock_irqsave(&fep->tmreg_lock, flags); 1175bfd4ecddSRussell King ns = timecounter_cyc2time(&fep->tc, ts); 1176bfd4ecddSRussell King spin_unlock_irqrestore(&fep->tmreg_lock, flags); 1177bfd4ecddSRussell King 1178bfd4ecddSRussell King memset(hwtstamps, 0, sizeof(*hwtstamps)); 1179bfd4ecddSRussell King hwtstamps->hwtstamp = ns_to_ktime(ns); 1180bfd4ecddSRussell King } 1181bfd4ecddSRussell King 1182bfd4ecddSRussell King static void 11834d494cdcSFugang Duan fec_enet_tx_queue(struct net_device *ndev, u16 queue_id) 1184793fc096SFrank Li { 1185793fc096SFrank Li struct fec_enet_private *fep; 1186a2fe37b6SFabio Estevam struct bufdesc *bdp; 1187793fc096SFrank Li unsigned short status; 1188793fc096SFrank Li struct sk_buff *skb; 11894d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 11904d494cdcSFugang Duan struct netdev_queue *nq; 1191793fc096SFrank Li int index = 0; 119279f33912SNimrod Andy int entries_free; 1193793fc096SFrank Li 1194793fc096SFrank Li fep = netdev_priv(ndev); 11954d494cdcSFugang Duan 11964d494cdcSFugang Duan queue_id = FEC_ENET_GET_QUQUE(queue_id); 11974d494cdcSFugang Duan 11984d494cdcSFugang Duan txq = fep->tx_queue[queue_id]; 11994d494cdcSFugang Duan /* get next bdp of dirty_tx */ 12004d494cdcSFugang Duan nq = netdev_get_tx_queue(ndev, queue_id); 12014d494cdcSFugang Duan bdp = txq->dirty_tx; 1202793fc096SFrank Li 1203793fc096SFrank Li /* get next bdp of dirty_tx */ 12047355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1205793fc096SFrank Li 12067355f276STroy Kisky while (bdp != READ_ONCE(txq->bd.cur)) { 12077355f276STroy Kisky /* Order the load of bd.cur and cbd_sc */ 1208c4bc44c6SKevin Hao rmb(); 12095cfa3039SJohannes Berg status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc)); 1210c4bc44c6SKevin Hao if (status & BD_ENET_TX_READY) 1211793fc096SFrank Li break; 1212793fc096SFrank Li 12137355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &txq->bd); 12142b995f63SNimrod Andy 1215a2fe37b6SFabio Estevam skb = txq->tx_skbuff[index]; 1216a2fe37b6SFabio Estevam txq->tx_skbuff[index] = NULL; 12175cfa3039SJohannes Berg if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 12185cfa3039SJohannes Berg dma_unmap_single(&fep->pdev->dev, 12195cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 12205cfa3039SJohannes Berg fec16_to_cpu(bdp->cbd_datlen), 12215cfa3039SJohannes Berg DMA_TO_DEVICE); 12225cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(0); 12237fafe803STroy Kisky if (!skb) 12247fafe803STroy Kisky goto skb_done; 1225793fc096SFrank Li 1226793fc096SFrank Li /* Check for errors. */ 1227793fc096SFrank Li if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | 1228793fc096SFrank Li BD_ENET_TX_RL | BD_ENET_TX_UN | 1229793fc096SFrank Li BD_ENET_TX_CSL)) { 1230793fc096SFrank Li ndev->stats.tx_errors++; 1231793fc096SFrank Li if (status & BD_ENET_TX_HB) /* No heartbeat */ 1232793fc096SFrank Li ndev->stats.tx_heartbeat_errors++; 1233793fc096SFrank Li if (status & BD_ENET_TX_LC) /* Late collision */ 1234793fc096SFrank Li ndev->stats.tx_window_errors++; 1235793fc096SFrank Li if (status & BD_ENET_TX_RL) /* Retrans limit */ 1236793fc096SFrank Li ndev->stats.tx_aborted_errors++; 1237793fc096SFrank Li if (status & BD_ENET_TX_UN) /* Underrun */ 1238793fc096SFrank Li ndev->stats.tx_fifo_errors++; 1239793fc096SFrank Li if (status & BD_ENET_TX_CSL) /* Carrier lost */ 1240793fc096SFrank Li ndev->stats.tx_carrier_errors++; 1241793fc096SFrank Li } else { 1242793fc096SFrank Li ndev->stats.tx_packets++; 12436e909283SNimrod Andy ndev->stats.tx_bytes += skb->len; 1244793fc096SFrank Li } 1245793fc096SFrank Li 1246793fc096SFrank Li if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) && 1247793fc096SFrank Li fep->bufdesc_ex) { 1248793fc096SFrank Li struct skb_shared_hwtstamps shhwtstamps; 1249793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1250793fc096SFrank Li 12515cfa3039SJohannes Berg fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps); 1252793fc096SFrank Li skb_tstamp_tx(skb, &shhwtstamps); 1253793fc096SFrank Li } 1254793fc096SFrank Li 1255793fc096SFrank Li /* Deferred means some collisions occurred during transmit, 1256793fc096SFrank Li * but we eventually sent the packet OK. 1257793fc096SFrank Li */ 1258793fc096SFrank Li if (status & BD_ENET_TX_DEF) 1259793fc096SFrank Li ndev->stats.collisions++; 1260793fc096SFrank Li 1261793fc096SFrank Li /* Free the sk buffer associated with this last transmit */ 1262793fc096SFrank Li dev_kfree_skb_any(skb); 12637fafe803STroy Kisky skb_done: 1264c4bc44c6SKevin Hao /* Make sure the update to bdp and tx_skbuff are performed 1265c4bc44c6SKevin Hao * before dirty_tx 1266c4bc44c6SKevin Hao */ 1267c4bc44c6SKevin Hao wmb(); 12684d494cdcSFugang Duan txq->dirty_tx = bdp; 1269793fc096SFrank Li 1270793fc096SFrank Li /* Update pointer to next buffer descriptor to be transmitted */ 12717355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1272793fc096SFrank Li 1273793fc096SFrank Li /* Since we have freed up a buffer, the ring is no longer full 1274793fc096SFrank Li */ 127579f33912SNimrod Andy if (netif_queue_stopped(ndev)) { 12767355f276STroy Kisky entries_free = fec_enet_get_free_txdesc_num(txq); 12774d494cdcSFugang Duan if (entries_free >= txq->tx_wake_threshold) 12784d494cdcSFugang Duan netif_tx_wake_queue(nq); 1279793fc096SFrank Li } 128079f33912SNimrod Andy } 1281ccea2968SRussell King 1282c10bc0e7SFugang Duan /* ERR006358: Keep the transmitter going */ 12837355f276STroy Kisky if (bdp != txq->bd.cur && 128453bb20d1STroy Kisky readl(txq->bd.reg_desc_active) == 0) 128553bb20d1STroy Kisky writel(0, txq->bd.reg_desc_active); 12864d494cdcSFugang Duan } 12874d494cdcSFugang Duan 12884d494cdcSFugang Duan static void 12894d494cdcSFugang Duan fec_enet_tx(struct net_device *ndev) 12904d494cdcSFugang Duan { 12914d494cdcSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 12924d494cdcSFugang Duan u16 queue_id; 12934d494cdcSFugang Duan /* First process class A queue, then Class B and Best Effort queue */ 12944d494cdcSFugang Duan for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) { 12954d494cdcSFugang Duan clear_bit(queue_id, &fep->work_tx); 12964d494cdcSFugang Duan fec_enet_tx_queue(ndev, queue_id); 12974d494cdcSFugang Duan } 12984d494cdcSFugang Duan return; 1299793fc096SFrank Li } 1300793fc096SFrank Li 13011b7bde6dSNimrod Andy static int 13021b7bde6dSNimrod Andy fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb) 13031b7bde6dSNimrod Andy { 13041b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 13051b7bde6dSNimrod Andy int off; 13061b7bde6dSNimrod Andy 13071b7bde6dSNimrod Andy off = ((unsigned long)skb->data) & fep->rx_align; 13081b7bde6dSNimrod Andy if (off) 13091b7bde6dSNimrod Andy skb_reserve(skb, fep->rx_align + 1 - off); 13101b7bde6dSNimrod Andy 13115cfa3039SJohannes 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)); 13125cfa3039SJohannes Berg if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) { 13131b7bde6dSNimrod Andy if (net_ratelimit()) 13141b7bde6dSNimrod Andy netdev_err(ndev, "Rx DMA memory map failed\n"); 13151b7bde6dSNimrod Andy return -ENOMEM; 13161b7bde6dSNimrod Andy } 13171b7bde6dSNimrod Andy 13181b7bde6dSNimrod Andy return 0; 13191b7bde6dSNimrod Andy } 13201b7bde6dSNimrod Andy 13211b7bde6dSNimrod Andy static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb, 13221310b544SLothar Waßmann struct bufdesc *bdp, u32 length, bool swap) 13231b7bde6dSNimrod Andy { 13241b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 13251b7bde6dSNimrod Andy struct sk_buff *new_skb; 13261b7bde6dSNimrod Andy 13271b7bde6dSNimrod Andy if (length > fep->rx_copybreak) 13281b7bde6dSNimrod Andy return false; 13291b7bde6dSNimrod Andy 13301b7bde6dSNimrod Andy new_skb = netdev_alloc_skb(ndev, length); 13311b7bde6dSNimrod Andy if (!new_skb) 13321b7bde6dSNimrod Andy return false; 13331b7bde6dSNimrod Andy 13345cfa3039SJohannes Berg dma_sync_single_for_cpu(&fep->pdev->dev, 13355cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 13361b7bde6dSNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 13371b7bde6dSNimrod Andy DMA_FROM_DEVICE); 13381310b544SLothar Waßmann if (!swap) 13391b7bde6dSNimrod Andy memcpy(new_skb->data, (*skb)->data, length); 13401310b544SLothar Waßmann else 13411310b544SLothar Waßmann swap_buffer2(new_skb->data, (*skb)->data, length); 13421b7bde6dSNimrod Andy *skb = new_skb; 13431b7bde6dSNimrod Andy 13441b7bde6dSNimrod Andy return true; 13451b7bde6dSNimrod Andy } 13461b7bde6dSNimrod Andy 13477355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer. 1348793fc096SFrank Li * When we update through the ring, if the next incoming buffer has 1349793fc096SFrank Li * not been given to the system, we just set the empty indicator, 1350793fc096SFrank Li * effectively tossing the packet. 1351793fc096SFrank Li */ 1352793fc096SFrank Li static int 13534d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) 1354793fc096SFrank Li { 1355793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 13564d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 1357793fc096SFrank Li struct bufdesc *bdp; 1358793fc096SFrank Li unsigned short status; 13591b7bde6dSNimrod Andy struct sk_buff *skb_new = NULL; 1360793fc096SFrank Li struct sk_buff *skb; 1361793fc096SFrank Li ushort pkt_len; 1362793fc096SFrank Li __u8 *data; 1363793fc096SFrank Li int pkt_received = 0; 1364cdffcf1bSJim Baxter struct bufdesc_ex *ebdp = NULL; 1365cdffcf1bSJim Baxter bool vlan_packet_rcvd = false; 1366cdffcf1bSJim Baxter u16 vlan_tag; 1367d842a31fSDuan Fugang-B38611 int index = 0; 13681b7bde6dSNimrod Andy bool is_copybreak; 13696b7e4008SLothar Waßmann bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME; 1370793fc096SFrank Li 1371793fc096SFrank Li #ifdef CONFIG_M532x 1372793fc096SFrank Li flush_cache_all(); 1373793fc096SFrank Li #endif 13744d494cdcSFugang Duan queue_id = FEC_ENET_GET_QUQUE(queue_id); 13754d494cdcSFugang Duan rxq = fep->rx_queue[queue_id]; 1376793fc096SFrank Li 1377793fc096SFrank Li /* First, grab all of the stats for the incoming packet. 1378793fc096SFrank Li * These get messed up if we get called due to a busy condition. 1379793fc096SFrank Li */ 13807355f276STroy Kisky bdp = rxq->bd.cur; 1381793fc096SFrank Li 13825cfa3039SJohannes Berg while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) { 1383793fc096SFrank Li 1384793fc096SFrank Li if (pkt_received >= budget) 1385793fc096SFrank Li break; 1386793fc096SFrank Li pkt_received++; 1387793fc096SFrank Li 1388ed63f1dcSRussell King writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT); 1389db3421c1SRussell King 1390793fc096SFrank Li /* Check for errors. */ 1391095098e1STroy Kisky status ^= BD_ENET_RX_LAST; 1392793fc096SFrank Li if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | 1393095098e1STroy Kisky BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST | 1394095098e1STroy Kisky BD_ENET_RX_CL)) { 1395793fc096SFrank Li ndev->stats.rx_errors++; 1396095098e1STroy Kisky if (status & BD_ENET_RX_OV) { 1397095098e1STroy Kisky /* FIFO overrun */ 1398095098e1STroy Kisky ndev->stats.rx_fifo_errors++; 1399095098e1STroy Kisky goto rx_processing_done; 1400095098e1STroy Kisky } 1401095098e1STroy Kisky if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH 1402095098e1STroy Kisky | BD_ENET_RX_LAST)) { 1403793fc096SFrank Li /* Frame too long or too short. */ 1404793fc096SFrank Li ndev->stats.rx_length_errors++; 1405095098e1STroy Kisky if (status & BD_ENET_RX_LAST) 1406095098e1STroy Kisky netdev_err(ndev, "rcv is not +last\n"); 1407793fc096SFrank Li } 1408793fc096SFrank Li if (status & BD_ENET_RX_CR) /* CRC Error */ 1409793fc096SFrank Li ndev->stats.rx_crc_errors++; 1410095098e1STroy Kisky /* Report late collisions as a frame error. */ 1411095098e1STroy Kisky if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL)) 1412793fc096SFrank Li ndev->stats.rx_frame_errors++; 1413793fc096SFrank Li goto rx_processing_done; 1414793fc096SFrank Li } 1415793fc096SFrank Li 1416793fc096SFrank Li /* Process the incoming frame. */ 1417793fc096SFrank Li ndev->stats.rx_packets++; 14185cfa3039SJohannes Berg pkt_len = fec16_to_cpu(bdp->cbd_datlen); 1419793fc096SFrank Li ndev->stats.rx_bytes += pkt_len; 1420793fc096SFrank Li 14217355f276STroy Kisky index = fec_enet_get_bd_index(bdp, &rxq->bd); 14221b7bde6dSNimrod Andy skb = rxq->rx_skbuff[index]; 14231b7bde6dSNimrod Andy 14241b7bde6dSNimrod Andy /* The packet length includes FCS, but we don't want to 14251b7bde6dSNimrod Andy * include that when passing upstream as it messes up 14261b7bde6dSNimrod Andy * bridging applications. 14271b7bde6dSNimrod Andy */ 14281310b544SLothar Waßmann is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4, 14291310b544SLothar Waßmann need_swap); 14301b7bde6dSNimrod Andy if (!is_copybreak) { 14311b7bde6dSNimrod Andy skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); 14321b7bde6dSNimrod Andy if (unlikely(!skb_new)) { 14331b7bde6dSNimrod Andy ndev->stats.rx_dropped++; 14341b7bde6dSNimrod Andy goto rx_processing_done; 14351b7bde6dSNimrod Andy } 14365cfa3039SJohannes Berg dma_unmap_single(&fep->pdev->dev, 14375cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 1438b64bf4b7SNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 1439b64bf4b7SNimrod Andy DMA_FROM_DEVICE); 14401b7bde6dSNimrod Andy } 1441793fc096SFrank Li 14421b7bde6dSNimrod Andy prefetch(skb->data - NET_IP_ALIGN); 14431b7bde6dSNimrod Andy skb_put(skb, pkt_len - 4); 14441b7bde6dSNimrod Andy data = skb->data; 14453ac72b7bSEric Nelson 1446235bde1eSFabio Estevam if (!is_copybreak && need_swap) 1447235bde1eSFabio Estevam swap_buffer(data, pkt_len); 1448235bde1eSFabio Estevam 14493ac72b7bSEric Nelson #if !defined(CONFIG_M5272) 14503ac72b7bSEric Nelson if (fep->quirks & FEC_QUIRK_HAS_RACC) 14513ac72b7bSEric Nelson data = skb_pull_inline(skb, 2); 14523ac72b7bSEric Nelson #endif 14533ac72b7bSEric Nelson 1454cdffcf1bSJim Baxter /* Extract the enhanced buffer descriptor */ 1455cdffcf1bSJim Baxter ebdp = NULL; 1456cdffcf1bSJim Baxter if (fep->bufdesc_ex) 1457cdffcf1bSJim Baxter ebdp = (struct bufdesc_ex *)bdp; 1458cdffcf1bSJim Baxter 1459cdffcf1bSJim Baxter /* If this is a VLAN packet remove the VLAN Tag */ 1460cdffcf1bSJim Baxter vlan_packet_rcvd = false; 1461cdffcf1bSJim Baxter if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) && 14625cfa3039SJohannes Berg fep->bufdesc_ex && 14635cfa3039SJohannes Berg (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) { 1464cdffcf1bSJim Baxter /* Push and remove the vlan tag */ 1465cdffcf1bSJim Baxter struct vlan_hdr *vlan_header = 1466cdffcf1bSJim Baxter (struct vlan_hdr *) (data + ETH_HLEN); 1467cdffcf1bSJim Baxter vlan_tag = ntohs(vlan_header->h_vlan_TCI); 1468cdffcf1bSJim Baxter 1469cdffcf1bSJim Baxter vlan_packet_rcvd = true; 14701b7bde6dSNimrod Andy 1471af5cbc98SNimrod Andy memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2); 14721b7bde6dSNimrod Andy skb_pull(skb, VLAN_HLEN); 1473cdffcf1bSJim Baxter } 1474cdffcf1bSJim Baxter 1475793fc096SFrank Li skb->protocol = eth_type_trans(skb, ndev); 1476793fc096SFrank Li 1477793fc096SFrank Li /* Get receive timestamp from the skb */ 1478bfd4ecddSRussell King if (fep->hwts_rx_en && fep->bufdesc_ex) 14795cfa3039SJohannes Berg fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), 1480bfd4ecddSRussell King skb_hwtstamps(skb)); 1481793fc096SFrank Li 14824c09eed9SJim Baxter if (fep->bufdesc_ex && 14834c09eed9SJim Baxter (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { 14845cfa3039SJohannes Berg if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) { 14854c09eed9SJim Baxter /* don't check it */ 14864c09eed9SJim Baxter skb->ip_summed = CHECKSUM_UNNECESSARY; 14874c09eed9SJim Baxter } else { 14884c09eed9SJim Baxter skb_checksum_none_assert(skb); 14894c09eed9SJim Baxter } 14904c09eed9SJim Baxter } 14914c09eed9SJim Baxter 1492cdffcf1bSJim Baxter /* Handle received VLAN packets */ 1493cdffcf1bSJim Baxter if (vlan_packet_rcvd) 1494cdffcf1bSJim Baxter __vlan_hwaccel_put_tag(skb, 1495cdffcf1bSJim Baxter htons(ETH_P_8021Q), 1496cdffcf1bSJim Baxter vlan_tag); 1497cdffcf1bSJim Baxter 1498793fc096SFrank Li napi_gro_receive(&fep->napi, skb); 1499793fc096SFrank Li 15001b7bde6dSNimrod Andy if (is_copybreak) { 15015cfa3039SJohannes Berg dma_sync_single_for_device(&fep->pdev->dev, 15025cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 1503b64bf4b7SNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 1504b64bf4b7SNimrod Andy DMA_FROM_DEVICE); 15051b7bde6dSNimrod Andy } else { 15061b7bde6dSNimrod Andy rxq->rx_skbuff[index] = skb_new; 15071b7bde6dSNimrod Andy fec_enet_new_rxbdp(ndev, bdp, skb_new); 15081b7bde6dSNimrod Andy } 15091b7bde6dSNimrod Andy 1510793fc096SFrank Li rx_processing_done: 1511793fc096SFrank Li /* Clear the status flags for this buffer */ 1512793fc096SFrank Li status &= ~BD_ENET_RX_STATS; 1513793fc096SFrank Li 1514793fc096SFrank Li /* Mark the buffer empty */ 1515793fc096SFrank Li status |= BD_ENET_RX_EMPTY; 1516793fc096SFrank Li 1517793fc096SFrank Li if (fep->bufdesc_ex) { 1518793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1519793fc096SFrank Li 15205cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 1521793fc096SFrank Li ebdp->cbd_prot = 0; 1522793fc096SFrank Li ebdp->cbd_bdu = 0; 1523793fc096SFrank Li } 1524be293467STroy Kisky /* Make sure the updates to rest of the descriptor are 1525be293467STroy Kisky * performed before transferring ownership. 1526be293467STroy Kisky */ 1527be293467STroy Kisky wmb(); 1528be293467STroy Kisky bdp->cbd_sc = cpu_to_fec16(status); 1529793fc096SFrank Li 1530793fc096SFrank Li /* Update BD pointer to next entry */ 15317355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 153236e24e2eSDuan Fugang-B38611 1533793fc096SFrank Li /* Doing this here will keep the FEC running while we process 1534793fc096SFrank Li * incoming frames. On a heavily loaded network, we should be 1535793fc096SFrank Li * able to keep up at the expense of system resources. 1536793fc096SFrank Li */ 153753bb20d1STroy Kisky writel(0, rxq->bd.reg_desc_active); 1538793fc096SFrank Li } 15397355f276STroy Kisky rxq->bd.cur = bdp; 1540793fc096SFrank Li return pkt_received; 1541793fc096SFrank Li } 1542793fc096SFrank Li 15434d494cdcSFugang Duan static int 15444d494cdcSFugang Duan fec_enet_rx(struct net_device *ndev, int budget) 15454d494cdcSFugang Duan { 15464d494cdcSFugang Duan int pkt_received = 0; 15474d494cdcSFugang Duan u16 queue_id; 15484d494cdcSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 15494d494cdcSFugang Duan 15504d494cdcSFugang Duan for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) { 15511c021bb7SUwe Kleine-König int ret; 15521c021bb7SUwe Kleine-König 15531c021bb7SUwe Kleine-König ret = fec_enet_rx_queue(ndev, 15544d494cdcSFugang Duan budget - pkt_received, queue_id); 15551c021bb7SUwe Kleine-König 15561c021bb7SUwe Kleine-König if (ret < budget - pkt_received) 15571c021bb7SUwe Kleine-König clear_bit(queue_id, &fep->work_rx); 15581c021bb7SUwe Kleine-König 15591c021bb7SUwe Kleine-König pkt_received += ret; 15604d494cdcSFugang Duan } 15614d494cdcSFugang Duan return pkt_received; 15624d494cdcSFugang Duan } 15634d494cdcSFugang Duan 15644d494cdcSFugang Duan static bool 15654d494cdcSFugang Duan fec_enet_collect_events(struct fec_enet_private *fep, uint int_events) 15664d494cdcSFugang Duan { 15674d494cdcSFugang Duan if (int_events == 0) 15684d494cdcSFugang Duan return false; 15694d494cdcSFugang Duan 15705e62d98cSTroy Kisky if (int_events & FEC_ENET_RXF_0) 15714d494cdcSFugang Duan fep->work_rx |= (1 << 2); 1572ce99d0d3SFrank Li if (int_events & FEC_ENET_RXF_1) 1573ce99d0d3SFrank Li fep->work_rx |= (1 << 0); 1574ce99d0d3SFrank Li if (int_events & FEC_ENET_RXF_2) 1575ce99d0d3SFrank Li fep->work_rx |= (1 << 1); 15764d494cdcSFugang Duan 15775e62d98cSTroy Kisky if (int_events & FEC_ENET_TXF_0) 15784d494cdcSFugang Duan fep->work_tx |= (1 << 2); 1579ce99d0d3SFrank Li if (int_events & FEC_ENET_TXF_1) 1580ce99d0d3SFrank Li fep->work_tx |= (1 << 0); 1581ce99d0d3SFrank Li if (int_events & FEC_ENET_TXF_2) 1582ce99d0d3SFrank Li fep->work_tx |= (1 << 1); 15834d494cdcSFugang Duan 15844d494cdcSFugang Duan return true; 15854d494cdcSFugang Duan } 15864d494cdcSFugang Duan 1587793fc096SFrank Li static irqreturn_t 1588793fc096SFrank Li fec_enet_interrupt(int irq, void *dev_id) 1589793fc096SFrank Li { 1590793fc096SFrank Li struct net_device *ndev = dev_id; 1591793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1592793fc096SFrank Li uint int_events; 1593793fc096SFrank Li irqreturn_t ret = IRQ_NONE; 1594793fc096SFrank Li 1595793fc096SFrank Li int_events = readl(fep->hwp + FEC_IEVENT); 159694191fd6SNimrod Andy writel(int_events, fep->hwp + FEC_IEVENT); 15974d494cdcSFugang Duan fec_enet_collect_events(fep, int_events); 1598793fc096SFrank Li 159961615cd2SFugang Duan if ((fep->work_tx || fep->work_rx) && fep->link) { 1600793fc096SFrank Li ret = IRQ_HANDLED; 1601793fc096SFrank Li 160294191fd6SNimrod Andy if (napi_schedule_prep(&fep->napi)) { 16037a16807cSRussell King /* Disable the NAPI interrupts */ 160480dc6a9fSTroy Kisky writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK); 160594191fd6SNimrod Andy __napi_schedule(&fep->napi); 160694191fd6SNimrod Andy } 1607793fc096SFrank Li } 1608793fc096SFrank Li 1609793fc096SFrank Li if (int_events & FEC_ENET_MII) { 1610793fc096SFrank Li ret = IRQ_HANDLED; 1611793fc096SFrank Li complete(&fep->mdio_done); 1612793fc096SFrank Li } 1613793fc096SFrank Li return ret; 1614793fc096SFrank Li } 1615793fc096SFrank Li 1616793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget) 1617793fc096SFrank Li { 1618793fc096SFrank Li struct net_device *ndev = napi->dev; 1619793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 16207a16807cSRussell King int pkts; 16217a16807cSRussell King 16227a16807cSRussell King pkts = fec_enet_rx(ndev, budget); 1623793fc096SFrank Li 1624793fc096SFrank Li fec_enet_tx(ndev); 1625793fc096SFrank Li 1626793fc096SFrank Li if (pkts < budget) { 16276ad20165SEric Dumazet napi_complete_done(napi, pkts); 1628793fc096SFrank Li writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1629793fc096SFrank Li } 1630793fc096SFrank Li return pkts; 1631793fc096SFrank Li } 1632793fc096SFrank Li 1633793fc096SFrank Li /* ------------------------------------------------------------------------- */ 1634793fc096SFrank Li static void fec_get_mac(struct net_device *ndev) 1635793fc096SFrank Li { 1636793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 163794660ba0SJingoo Han struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev); 1638793fc096SFrank Li unsigned char *iap, tmpaddr[ETH_ALEN]; 1639793fc096SFrank Li 1640793fc096SFrank Li /* 1641793fc096SFrank Li * try to get mac address in following order: 1642793fc096SFrank Li * 1643793fc096SFrank Li * 1) module parameter via kernel command line in form 1644793fc096SFrank Li * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 1645793fc096SFrank Li */ 1646793fc096SFrank Li iap = macaddr; 1647793fc096SFrank Li 1648793fc096SFrank Li /* 1649793fc096SFrank Li * 2) from device tree data 1650793fc096SFrank Li */ 1651793fc096SFrank Li if (!is_valid_ether_addr(iap)) { 1652793fc096SFrank Li struct device_node *np = fep->pdev->dev.of_node; 1653793fc096SFrank Li if (np) { 1654793fc096SFrank Li const char *mac = of_get_mac_address(np); 1655793fc096SFrank Li if (mac) 1656793fc096SFrank Li iap = (unsigned char *) mac; 1657793fc096SFrank Li } 1658793fc096SFrank Li } 1659793fc096SFrank Li 1660793fc096SFrank Li /* 1661793fc096SFrank Li * 3) from flash or fuse (via platform data) 1662793fc096SFrank Li */ 1663793fc096SFrank Li if (!is_valid_ether_addr(iap)) { 1664793fc096SFrank Li #ifdef CONFIG_M5272 1665793fc096SFrank Li if (FEC_FLASHMAC) 1666793fc096SFrank Li iap = (unsigned char *)FEC_FLASHMAC; 1667793fc096SFrank Li #else 1668793fc096SFrank Li if (pdata) 1669793fc096SFrank Li iap = (unsigned char *)&pdata->mac; 1670793fc096SFrank Li #endif 1671793fc096SFrank Li } 1672793fc096SFrank Li 1673793fc096SFrank Li /* 1674793fc096SFrank Li * 4) FEC mac registers set by bootloader 1675793fc096SFrank Li */ 1676793fc096SFrank Li if (!is_valid_ether_addr(iap)) { 16777d7628f3SDan Carpenter *((__be32 *) &tmpaddr[0]) = 16787d7628f3SDan Carpenter cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW)); 16797d7628f3SDan Carpenter *((__be16 *) &tmpaddr[4]) = 16807d7628f3SDan Carpenter cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); 1681793fc096SFrank Li iap = &tmpaddr[0]; 1682793fc096SFrank Li } 1683793fc096SFrank Li 1684ff5b2fabSLucas Stach /* 1685ff5b2fabSLucas Stach * 5) random mac address 1686ff5b2fabSLucas Stach */ 1687ff5b2fabSLucas Stach if (!is_valid_ether_addr(iap)) { 1688ff5b2fabSLucas Stach /* Report it and use a random ethernet address instead */ 1689ff5b2fabSLucas Stach netdev_err(ndev, "Invalid MAC address: %pM\n", iap); 1690ff5b2fabSLucas Stach eth_hw_addr_random(ndev); 1691ff5b2fabSLucas Stach netdev_info(ndev, "Using random MAC address: %pM\n", 1692ff5b2fabSLucas Stach ndev->dev_addr); 1693ff5b2fabSLucas Stach return; 1694ff5b2fabSLucas Stach } 1695ff5b2fabSLucas Stach 1696793fc096SFrank Li memcpy(ndev->dev_addr, iap, ETH_ALEN); 1697793fc096SFrank Li 1698793fc096SFrank Li /* Adjust MAC if using macaddr */ 1699793fc096SFrank Li if (iap == macaddr) 1700793fc096SFrank Li ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id; 1701793fc096SFrank Li } 1702793fc096SFrank Li 1703793fc096SFrank Li /* ------------------------------------------------------------------------- */ 1704793fc096SFrank Li 1705793fc096SFrank Li /* 1706793fc096SFrank Li * Phy section 1707793fc096SFrank Li */ 1708793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev) 1709793fc096SFrank Li { 1710793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 171145f5c327SPhilippe Reynes struct phy_device *phy_dev = ndev->phydev; 1712793fc096SFrank Li int status_change = 0; 1713793fc096SFrank Li 1714793fc096SFrank Li /* Prevent a state halted on mii error */ 1715793fc096SFrank Li if (fep->mii_timeout && phy_dev->state == PHY_HALTED) { 1716793fc096SFrank Li phy_dev->state = PHY_RESUMING; 171754309fa6SFrank Li return; 1718793fc096SFrank Li } 1719793fc096SFrank Li 17208ce5624fSRussell King /* 17218ce5624fSRussell King * If the netdev is down, or is going down, we're not interested 17228ce5624fSRussell King * in link state events, so just mark our idea of the link as down 17238ce5624fSRussell King * and ignore the event. 17248ce5624fSRussell King */ 17258ce5624fSRussell King if (!netif_running(ndev) || !netif_device_present(ndev)) { 17268ce5624fSRussell King fep->link = 0; 17278ce5624fSRussell King } else if (phy_dev->link) { 1728793fc096SFrank Li if (!fep->link) { 1729793fc096SFrank Li fep->link = phy_dev->link; 1730793fc096SFrank Li status_change = 1; 1731793fc096SFrank Li } 1732793fc096SFrank Li 1733ef83337dSRussell King if (fep->full_duplex != phy_dev->duplex) { 1734ef83337dSRussell King fep->full_duplex = phy_dev->duplex; 1735793fc096SFrank Li status_change = 1; 1736ef83337dSRussell King } 1737793fc096SFrank Li 1738793fc096SFrank Li if (phy_dev->speed != fep->speed) { 1739793fc096SFrank Li fep->speed = phy_dev->speed; 1740793fc096SFrank Li status_change = 1; 1741793fc096SFrank Li } 1742793fc096SFrank Li 1743793fc096SFrank Li /* if any of the above changed restart the FEC */ 1744dbc64a8eSRussell King if (status_change) { 1745dbc64a8eSRussell King napi_disable(&fep->napi); 1746dbc64a8eSRussell King netif_tx_lock_bh(ndev); 1747ef83337dSRussell King fec_restart(ndev); 1748dbc64a8eSRussell King netif_wake_queue(ndev); 17496af42d42SRussell King netif_tx_unlock_bh(ndev); 1750dbc64a8eSRussell King napi_enable(&fep->napi); 1751dbc64a8eSRussell King } 1752793fc096SFrank Li } else { 1753793fc096SFrank Li if (fep->link) { 1754f208ce10SRussell King napi_disable(&fep->napi); 1755f208ce10SRussell King netif_tx_lock_bh(ndev); 1756793fc096SFrank Li fec_stop(ndev); 1757f208ce10SRussell King netif_tx_unlock_bh(ndev); 1758f208ce10SRussell King napi_enable(&fep->napi); 17596e0895c2SDavid S. Miller fep->link = phy_dev->link; 1760793fc096SFrank Li status_change = 1; 1761793fc096SFrank Li } 1762793fc096SFrank Li } 1763793fc096SFrank Li 1764793fc096SFrank Li if (status_change) 1765793fc096SFrank Li phy_print_status(phy_dev); 1766793fc096SFrank Li } 1767793fc096SFrank Li 1768793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum) 1769793fc096SFrank Li { 1770793fc096SFrank Li struct fec_enet_private *fep = bus->priv; 17718fff755eSAndrew Lunn struct device *dev = &fep->pdev->dev; 1772793fc096SFrank Li unsigned long time_left; 17738fff755eSAndrew Lunn int ret = 0; 17748fff755eSAndrew Lunn 17758fff755eSAndrew Lunn ret = pm_runtime_get_sync(dev); 1776b0c6ce24SFabio Estevam if (ret < 0) 17778fff755eSAndrew Lunn return ret; 1778793fc096SFrank Li 1779793fc096SFrank Li fep->mii_timeout = 0; 1780aac27c7aSRussell King reinit_completion(&fep->mdio_done); 1781793fc096SFrank Li 1782793fc096SFrank Li /* start a read op */ 1783793fc096SFrank Li writel(FEC_MMFR_ST | FEC_MMFR_OP_READ | 1784793fc096SFrank Li FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | 1785793fc096SFrank Li FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); 1786793fc096SFrank Li 1787793fc096SFrank Li /* wait for end of transfer */ 1788793fc096SFrank Li time_left = wait_for_completion_timeout(&fep->mdio_done, 1789793fc096SFrank Li usecs_to_jiffies(FEC_MII_TIMEOUT)); 1790793fc096SFrank Li if (time_left == 0) { 1791793fc096SFrank Li fep->mii_timeout = 1; 179231b7720cSJoe Perches netdev_err(fep->netdev, "MDIO read timeout\n"); 17938fff755eSAndrew Lunn ret = -ETIMEDOUT; 17948fff755eSAndrew Lunn goto out; 1795793fc096SFrank Li } 1796793fc096SFrank Li 17978fff755eSAndrew Lunn ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); 17988fff755eSAndrew Lunn 17998fff755eSAndrew Lunn out: 18008fff755eSAndrew Lunn pm_runtime_mark_last_busy(dev); 18018fff755eSAndrew Lunn pm_runtime_put_autosuspend(dev); 18028fff755eSAndrew Lunn 18038fff755eSAndrew Lunn return ret; 1804793fc096SFrank Li } 1805793fc096SFrank Li 1806793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum, 1807793fc096SFrank Li u16 value) 1808793fc096SFrank Li { 1809793fc096SFrank Li struct fec_enet_private *fep = bus->priv; 18108fff755eSAndrew Lunn struct device *dev = &fep->pdev->dev; 1811793fc096SFrank Li unsigned long time_left; 181242ea4457SMaciej S. Szmigiero int ret; 18138fff755eSAndrew Lunn 18148fff755eSAndrew Lunn ret = pm_runtime_get_sync(dev); 1815b0c6ce24SFabio Estevam if (ret < 0) 18168fff755eSAndrew Lunn return ret; 181742ea4457SMaciej S. Szmigiero else 181842ea4457SMaciej S. Szmigiero ret = 0; 1819793fc096SFrank Li 1820793fc096SFrank Li fep->mii_timeout = 0; 1821aac27c7aSRussell King reinit_completion(&fep->mdio_done); 1822793fc096SFrank Li 1823793fc096SFrank Li /* start a write op */ 1824793fc096SFrank Li writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE | 1825793fc096SFrank Li FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | 1826793fc096SFrank Li FEC_MMFR_TA | FEC_MMFR_DATA(value), 1827793fc096SFrank Li fep->hwp + FEC_MII_DATA); 1828793fc096SFrank Li 1829793fc096SFrank Li /* wait for end of transfer */ 1830793fc096SFrank Li time_left = wait_for_completion_timeout(&fep->mdio_done, 1831793fc096SFrank Li usecs_to_jiffies(FEC_MII_TIMEOUT)); 1832793fc096SFrank Li if (time_left == 0) { 1833793fc096SFrank Li fep->mii_timeout = 1; 183431b7720cSJoe Perches netdev_err(fep->netdev, "MDIO write timeout\n"); 18358fff755eSAndrew Lunn ret = -ETIMEDOUT; 1836793fc096SFrank Li } 1837793fc096SFrank Li 18388fff755eSAndrew Lunn pm_runtime_mark_last_busy(dev); 18398fff755eSAndrew Lunn pm_runtime_put_autosuspend(dev); 18408fff755eSAndrew Lunn 18418fff755eSAndrew Lunn return ret; 1842793fc096SFrank Li } 1843793fc096SFrank Li 1844e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable) 1845e8fcfcd5SNimrod Andy { 1846e8fcfcd5SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 1847e8fcfcd5SNimrod Andy int ret; 1848e8fcfcd5SNimrod Andy 1849e8fcfcd5SNimrod Andy if (enable) { 1850e8fcfcd5SNimrod Andy ret = clk_prepare_enable(fep->clk_ahb); 1851e8fcfcd5SNimrod Andy if (ret) 1852e8fcfcd5SNimrod Andy return ret; 185301e5943aSUwe Kleine-König 1854e8fcfcd5SNimrod Andy ret = clk_prepare_enable(fep->clk_enet_out); 1855e8fcfcd5SNimrod Andy if (ret) 1856e8fcfcd5SNimrod Andy goto failed_clk_enet_out; 185701e5943aSUwe Kleine-König 1858e8fcfcd5SNimrod Andy if (fep->clk_ptp) { 185991c0d987SNimrod Andy mutex_lock(&fep->ptp_clk_mutex); 1860e8fcfcd5SNimrod Andy ret = clk_prepare_enable(fep->clk_ptp); 186191c0d987SNimrod Andy if (ret) { 186291c0d987SNimrod Andy mutex_unlock(&fep->ptp_clk_mutex); 1863e8fcfcd5SNimrod Andy goto failed_clk_ptp; 186491c0d987SNimrod Andy } else { 186591c0d987SNimrod Andy fep->ptp_clk_on = true; 186691c0d987SNimrod Andy } 186791c0d987SNimrod Andy mutex_unlock(&fep->ptp_clk_mutex); 1868e8fcfcd5SNimrod Andy } 186901e5943aSUwe Kleine-König 18709b5330edSFugang Duan ret = clk_prepare_enable(fep->clk_ref); 18719b5330edSFugang Duan if (ret) 18729b5330edSFugang Duan goto failed_clk_ref; 18731b0a83acSRichard Leitner 18741b0a83acSRichard Leitner phy_reset_after_clk_enable(ndev->phydev); 1875e8fcfcd5SNimrod Andy } else { 1876e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_ahb); 1877e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_enet_out); 187891c0d987SNimrod Andy if (fep->clk_ptp) { 187991c0d987SNimrod Andy mutex_lock(&fep->ptp_clk_mutex); 1880e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_ptp); 188191c0d987SNimrod Andy fep->ptp_clk_on = false; 188291c0d987SNimrod Andy mutex_unlock(&fep->ptp_clk_mutex); 188391c0d987SNimrod Andy } 18849b5330edSFugang Duan clk_disable_unprepare(fep->clk_ref); 1885e8fcfcd5SNimrod Andy } 1886e8fcfcd5SNimrod Andy 1887e8fcfcd5SNimrod Andy return 0; 18889b5330edSFugang Duan 18899b5330edSFugang Duan failed_clk_ref: 18909b5330edSFugang Duan if (fep->clk_ref) 18919b5330edSFugang Duan clk_disable_unprepare(fep->clk_ref); 1892e8fcfcd5SNimrod Andy failed_clk_ptp: 1893e8fcfcd5SNimrod Andy if (fep->clk_enet_out) 1894e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_enet_out); 1895e8fcfcd5SNimrod Andy failed_clk_enet_out: 1896e8fcfcd5SNimrod Andy clk_disable_unprepare(fep->clk_ahb); 1897e8fcfcd5SNimrod Andy 1898e8fcfcd5SNimrod Andy return ret; 1899e8fcfcd5SNimrod Andy } 1900e8fcfcd5SNimrod Andy 1901793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev) 1902793fc096SFrank Li { 1903793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1904793fc096SFrank Li struct phy_device *phy_dev = NULL; 1905793fc096SFrank Li char mdio_bus_id[MII_BUS_ID_SIZE]; 1906793fc096SFrank Li char phy_name[MII_BUS_ID_SIZE + 3]; 1907793fc096SFrank Li int phy_id; 1908793fc096SFrank Li int dev_id = fep->dev_id; 1909793fc096SFrank Li 1910407066f8SUwe Kleine-König if (fep->phy_node) { 1911407066f8SUwe Kleine-König phy_dev = of_phy_connect(ndev, fep->phy_node, 1912407066f8SUwe Kleine-König &fec_enet_adjust_link, 0, 1913407066f8SUwe Kleine-König fep->phy_interface); 19149558df3aSAndrew Lunn if (!phy_dev) { 19159558df3aSAndrew Lunn netdev_err(ndev, "Unable to connect to phy\n"); 1916213a9922SNimrod Andy return -ENODEV; 19179558df3aSAndrew Lunn } 1918407066f8SUwe Kleine-König } else { 1919793fc096SFrank Li /* check for attached phy */ 1920793fc096SFrank Li for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { 19217f854420SAndrew Lunn if (!mdiobus_is_registered_device(fep->mii_bus, phy_id)) 1922793fc096SFrank Li continue; 1923793fc096SFrank Li if (dev_id--) 1924793fc096SFrank Li continue; 1925949bdd20SRickard Strandqvist strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); 1926793fc096SFrank Li break; 1927793fc096SFrank Li } 1928793fc096SFrank Li 1929793fc096SFrank Li if (phy_id >= PHY_MAX_ADDR) { 193031b7720cSJoe Perches netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); 1931949bdd20SRickard Strandqvist strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); 1932793fc096SFrank Li phy_id = 0; 1933793fc096SFrank Li } 1934793fc096SFrank Li 1935407066f8SUwe Kleine-König snprintf(phy_name, sizeof(phy_name), 1936407066f8SUwe Kleine-König PHY_ID_FMT, mdio_bus_id, phy_id); 1937793fc096SFrank Li phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 1938793fc096SFrank Li fep->phy_interface); 1939407066f8SUwe Kleine-König } 1940407066f8SUwe Kleine-König 1941793fc096SFrank Li if (IS_ERR(phy_dev)) { 194231b7720cSJoe Perches netdev_err(ndev, "could not attach to PHY\n"); 1943793fc096SFrank Li return PTR_ERR(phy_dev); 1944793fc096SFrank Li } 1945793fc096SFrank Li 1946793fc096SFrank Li /* mask with MAC supported features */ 19476b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_GBIT) { 1948793fc096SFrank Li phy_dev->supported &= PHY_GBIT_FEATURES; 1949b44592ffSRussell King phy_dev->supported &= ~SUPPORTED_1000baseT_Half; 1950d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 1951793fc096SFrank Li phy_dev->supported |= SUPPORTED_Pause; 1952d1391930SGuenter Roeck #endif 1953793fc096SFrank Li } 1954793fc096SFrank Li else 1955793fc096SFrank Li phy_dev->supported &= PHY_BASIC_FEATURES; 1956793fc096SFrank Li 1957793fc096SFrank Li phy_dev->advertising = phy_dev->supported; 1958793fc096SFrank Li 1959793fc096SFrank Li fep->link = 0; 1960793fc096SFrank Li fep->full_duplex = 0; 1961793fc096SFrank Li 19622220943aSAndrew Lunn phy_attached_info(phy_dev); 1963793fc096SFrank Li 1964793fc096SFrank Li return 0; 1965793fc096SFrank Li } 1966793fc096SFrank Li 1967793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev) 1968793fc096SFrank Li { 1969793fc096SFrank Li static struct mii_bus *fec0_mii_bus; 1970793fc096SFrank Li struct net_device *ndev = platform_get_drvdata(pdev); 1971793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 1972407066f8SUwe Kleine-König struct device_node *node; 1973e7f4dc35SAndrew Lunn int err = -ENXIO; 197463c60732SUwe Kleine-König u32 mii_speed, holdtime; 1975793fc096SFrank Li 1976793fc096SFrank Li /* 19773d125f9cSStefan Agner * The i.MX28 dual fec interfaces are not equal. 1978793fc096SFrank Li * Here are the differences: 1979793fc096SFrank Li * 1980793fc096SFrank Li * - fec0 supports MII & RMII modes while fec1 only supports RMII 1981793fc096SFrank Li * - fec0 acts as the 1588 time master while fec1 is slave 1982793fc096SFrank Li * - external phys can only be configured by fec0 1983793fc096SFrank Li * 1984793fc096SFrank Li * That is to say fec1 can not work independently. It only works 1985793fc096SFrank Li * when fec0 is working. The reason behind this design is that the 1986793fc096SFrank Li * second interface is added primarily for Switch mode. 1987793fc096SFrank Li * 1988793fc096SFrank Li * Because of the last point above, both phys are attached on fec0 1989793fc096SFrank Li * mdio interface in board design, and need to be configured by 1990793fc096SFrank Li * fec0 mii_bus. 1991793fc096SFrank Li */ 19923d125f9cSStefan Agner if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) { 1993793fc096SFrank Li /* fec1 uses fec0 mii_bus */ 1994793fc096SFrank Li if (mii_cnt && fec0_mii_bus) { 1995793fc096SFrank Li fep->mii_bus = fec0_mii_bus; 1996793fc096SFrank Li mii_cnt++; 1997793fc096SFrank Li return 0; 1998793fc096SFrank Li } 1999793fc096SFrank Li return -ENOENT; 2000793fc096SFrank Li } 2001793fc096SFrank Li 2002793fc096SFrank Li fep->mii_timeout = 0; 2003793fc096SFrank Li 2004793fc096SFrank Li /* 2005793fc096SFrank Li * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed) 2006793fc096SFrank Li * 2007793fc096SFrank Li * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while 2008793fc096SFrank Li * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 2009793fc096SFrank Li * Reference Manual has an error on this, and gets fixed on i.MX6Q 2010793fc096SFrank Li * document. 2011793fc096SFrank Li */ 201263c60732SUwe Kleine-König mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000); 20136b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_ENET_MAC) 201463c60732SUwe Kleine-König mii_speed--; 201563c60732SUwe Kleine-König if (mii_speed > 63) { 201663c60732SUwe Kleine-König dev_err(&pdev->dev, 2017981a0547SPeter Meerwald-Stadler "fec clock (%lu) too fast to get right mii speed\n", 201863c60732SUwe Kleine-König clk_get_rate(fep->clk_ipg)); 201963c60732SUwe Kleine-König err = -EINVAL; 202063c60732SUwe Kleine-König goto err_out; 202163c60732SUwe Kleine-König } 202263c60732SUwe Kleine-König 202363c60732SUwe Kleine-König /* 202463c60732SUwe Kleine-König * The i.MX28 and i.MX6 types have another filed in the MSCR (aka 202563c60732SUwe Kleine-König * MII_SPEED) register that defines the MDIO output hold time. Earlier 202663c60732SUwe Kleine-König * versions are RAZ there, so just ignore the difference and write the 202763c60732SUwe Kleine-König * register always. 202863c60732SUwe Kleine-König * The minimal hold time according to IEE802.3 (clause 22) is 10 ns. 202963c60732SUwe Kleine-König * HOLDTIME + 1 is the number of clk cycles the fec is holding the 203063c60732SUwe Kleine-König * output. 203163c60732SUwe Kleine-König * The HOLDTIME bitfield takes values between 0 and 7 (inclusive). 203263c60732SUwe Kleine-König * Given that ceil(clkrate / 5000000) <= 64, the calculation for 203363c60732SUwe Kleine-König * holdtime cannot result in a value greater than 3. 203463c60732SUwe Kleine-König */ 203563c60732SUwe Kleine-König holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1; 203663c60732SUwe Kleine-König 203763c60732SUwe Kleine-König fep->phy_speed = mii_speed << 1 | holdtime << 8; 203863c60732SUwe Kleine-König 2039793fc096SFrank Li writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 2040793fc096SFrank Li 2041793fc096SFrank Li fep->mii_bus = mdiobus_alloc(); 2042793fc096SFrank Li if (fep->mii_bus == NULL) { 2043793fc096SFrank Li err = -ENOMEM; 2044793fc096SFrank Li goto err_out; 2045793fc096SFrank Li } 2046793fc096SFrank Li 2047793fc096SFrank Li fep->mii_bus->name = "fec_enet_mii_bus"; 2048793fc096SFrank Li fep->mii_bus->read = fec_enet_mdio_read; 2049793fc096SFrank Li fep->mii_bus->write = fec_enet_mdio_write; 2050793fc096SFrank Li snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 2051793fc096SFrank Li pdev->name, fep->dev_id + 1); 2052793fc096SFrank Li fep->mii_bus->priv = fep; 2053793fc096SFrank Li fep->mii_bus->parent = &pdev->dev; 2054793fc096SFrank Li 2055407066f8SUwe Kleine-König node = of_get_child_by_name(pdev->dev.of_node, "mdio"); 2056407066f8SUwe Kleine-König if (node) { 2057407066f8SUwe Kleine-König err = of_mdiobus_register(fep->mii_bus, node); 2058407066f8SUwe Kleine-König of_node_put(node); 2059407066f8SUwe Kleine-König } else { 2060407066f8SUwe Kleine-König err = mdiobus_register(fep->mii_bus); 2061407066f8SUwe Kleine-König } 2062407066f8SUwe Kleine-König 2063407066f8SUwe Kleine-König if (err) 2064e7f4dc35SAndrew Lunn goto err_out_free_mdiobus; 2065793fc096SFrank Li 2066793fc096SFrank Li mii_cnt++; 2067793fc096SFrank Li 2068793fc096SFrank Li /* save fec0 mii_bus */ 20693d125f9cSStefan Agner if (fep->quirks & FEC_QUIRK_SINGLE_MDIO) 2070793fc096SFrank Li fec0_mii_bus = fep->mii_bus; 2071793fc096SFrank Li 2072793fc096SFrank Li return 0; 2073793fc096SFrank Li 2074793fc096SFrank Li err_out_free_mdiobus: 2075793fc096SFrank Li mdiobus_free(fep->mii_bus); 2076793fc096SFrank Li err_out: 2077793fc096SFrank Li return err; 2078793fc096SFrank Li } 2079793fc096SFrank Li 2080793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep) 2081793fc096SFrank Li { 2082793fc096SFrank Li if (--mii_cnt == 0) { 2083793fc096SFrank Li mdiobus_unregister(fep->mii_bus); 2084793fc096SFrank Li mdiobus_free(fep->mii_bus); 2085793fc096SFrank Li } 2086793fc096SFrank Li } 2087793fc096SFrank Li 2088793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev, 2089793fc096SFrank Li struct ethtool_drvinfo *info) 2090793fc096SFrank Li { 2091793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2092793fc096SFrank Li 2093793fc096SFrank Li strlcpy(info->driver, fep->pdev->dev.driver->name, 2094793fc096SFrank Li sizeof(info->driver)); 2095793fc096SFrank Li strlcpy(info->version, "Revision: 1.0", sizeof(info->version)); 2096793fc096SFrank Li strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); 2097793fc096SFrank Li } 2098793fc096SFrank Li 2099db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev) 2100db65f35fSPhilippe Reynes { 2101db65f35fSPhilippe Reynes struct fec_enet_private *fep = netdev_priv(ndev); 2102db65f35fSPhilippe Reynes struct resource *r; 2103db65f35fSPhilippe Reynes int s = 0; 2104db65f35fSPhilippe Reynes 2105db65f35fSPhilippe Reynes r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0); 2106db65f35fSPhilippe Reynes if (r) 2107db65f35fSPhilippe Reynes s = resource_size(r); 2108db65f35fSPhilippe Reynes 2109db65f35fSPhilippe Reynes return s; 2110db65f35fSPhilippe Reynes } 2111db65f35fSPhilippe Reynes 2112db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */ 2113db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 21143f1dcc6aSLucas Stach defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 21153f1dcc6aSLucas Stach defined(CONFIG_ARM64) 2116db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = { 2117db65f35fSPhilippe Reynes FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0, 2118db65f35fSPhilippe Reynes FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL, 2119db65f35fSPhilippe Reynes FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1, 2120db65f35fSPhilippe Reynes FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH, 2121db65f35fSPhilippe Reynes FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, 2122db65f35fSPhilippe Reynes FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1, 2123db65f35fSPhilippe Reynes FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2, 2124db65f35fSPhilippe Reynes FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0, 2125db65f35fSPhilippe Reynes FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM, 2126db65f35fSPhilippe Reynes FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2, 2127db65f35fSPhilippe Reynes FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1, 2128db65f35fSPhilippe Reynes FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME, 2129db65f35fSPhilippe Reynes RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT, 2130db65f35fSPhilippe Reynes RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG, 2131db65f35fSPhilippe Reynes RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255, 2132db65f35fSPhilippe Reynes RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047, 2133db65f35fSPhilippe Reynes RMON_T_P_GTE2048, RMON_T_OCTETS, 2134db65f35fSPhilippe Reynes IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF, 2135db65f35fSPhilippe Reynes IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE, 2136db65f35fSPhilippe Reynes IEEE_T_FDXFC, IEEE_T_OCTETS_OK, 2137db65f35fSPhilippe Reynes RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN, 2138db65f35fSPhilippe Reynes RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB, 2139db65f35fSPhilippe Reynes RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255, 2140db65f35fSPhilippe Reynes RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047, 2141db65f35fSPhilippe Reynes RMON_R_P_GTE2048, RMON_R_OCTETS, 2142db65f35fSPhilippe Reynes IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR, 2143db65f35fSPhilippe Reynes IEEE_R_FDXFC, IEEE_R_OCTETS_OK 2144db65f35fSPhilippe Reynes }; 2145db65f35fSPhilippe Reynes #else 2146db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = { 2147db65f35fSPhilippe Reynes FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0, 2148db65f35fSPhilippe Reynes FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0, 2149db65f35fSPhilippe Reynes FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED, 2150db65f35fSPhilippe Reynes FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL, 2151db65f35fSPhilippe Reynes FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, 2152db65f35fSPhilippe Reynes FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0, 2153db65f35fSPhilippe Reynes FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0, 2154db65f35fSPhilippe Reynes FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0, 2155db65f35fSPhilippe Reynes FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2 2156db65f35fSPhilippe Reynes }; 2157db65f35fSPhilippe Reynes #endif 2158db65f35fSPhilippe Reynes 2159db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev, 2160db65f35fSPhilippe Reynes struct ethtool_regs *regs, void *regbuf) 2161db65f35fSPhilippe Reynes { 2162db65f35fSPhilippe Reynes struct fec_enet_private *fep = netdev_priv(ndev); 2163db65f35fSPhilippe Reynes u32 __iomem *theregs = (u32 __iomem *)fep->hwp; 2164db65f35fSPhilippe Reynes u32 *buf = (u32 *)regbuf; 2165db65f35fSPhilippe Reynes u32 i, off; 2166db65f35fSPhilippe Reynes 2167db65f35fSPhilippe Reynes memset(buf, 0, regs->len); 2168db65f35fSPhilippe Reynes 2169db65f35fSPhilippe Reynes for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) { 2170db65f35fSPhilippe Reynes off = fec_enet_register_offset[i] / 4; 2171db65f35fSPhilippe Reynes buf[off] = readl(&theregs[off]); 2172db65f35fSPhilippe Reynes } 2173db65f35fSPhilippe Reynes } 2174db65f35fSPhilippe Reynes 2175793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev, 2176793fc096SFrank Li struct ethtool_ts_info *info) 2177793fc096SFrank Li { 2178793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2179793fc096SFrank Li 2180793fc096SFrank Li if (fep->bufdesc_ex) { 2181793fc096SFrank Li 2182793fc096SFrank Li info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 2183793fc096SFrank Li SOF_TIMESTAMPING_RX_SOFTWARE | 2184793fc096SFrank Li SOF_TIMESTAMPING_SOFTWARE | 2185793fc096SFrank Li SOF_TIMESTAMPING_TX_HARDWARE | 2186793fc096SFrank Li SOF_TIMESTAMPING_RX_HARDWARE | 2187793fc096SFrank Li SOF_TIMESTAMPING_RAW_HARDWARE; 2188793fc096SFrank Li if (fep->ptp_clock) 2189793fc096SFrank Li info->phc_index = ptp_clock_index(fep->ptp_clock); 2190793fc096SFrank Li else 2191793fc096SFrank Li info->phc_index = -1; 2192793fc096SFrank Li 2193793fc096SFrank Li info->tx_types = (1 << HWTSTAMP_TX_OFF) | 2194793fc096SFrank Li (1 << HWTSTAMP_TX_ON); 2195793fc096SFrank Li 2196793fc096SFrank Li info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 2197793fc096SFrank Li (1 << HWTSTAMP_FILTER_ALL); 2198793fc096SFrank Li return 0; 2199793fc096SFrank Li } else { 2200793fc096SFrank Li return ethtool_op_get_ts_info(ndev, info); 2201793fc096SFrank Li } 2202793fc096SFrank Li } 2203793fc096SFrank Li 2204d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 2205d1391930SGuenter Roeck 2206793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev, 2207793fc096SFrank Li struct ethtool_pauseparam *pause) 2208793fc096SFrank Li { 2209793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2210793fc096SFrank Li 2211793fc096SFrank Li pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; 2212793fc096SFrank Li pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; 2213793fc096SFrank Li pause->rx_pause = pause->tx_pause; 2214793fc096SFrank Li } 2215793fc096SFrank Li 2216793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev, 2217793fc096SFrank Li struct ethtool_pauseparam *pause) 2218793fc096SFrank Li { 2219793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2220793fc096SFrank Li 222145f5c327SPhilippe Reynes if (!ndev->phydev) 22220b146ca8SRussell King return -ENODEV; 22230b146ca8SRussell King 2224793fc096SFrank Li if (pause->tx_pause != pause->rx_pause) { 2225793fc096SFrank Li netdev_info(ndev, 2226793fc096SFrank Li "hardware only support enable/disable both tx and rx"); 2227793fc096SFrank Li return -EINVAL; 2228793fc096SFrank Li } 2229793fc096SFrank Li 2230793fc096SFrank Li fep->pause_flag = 0; 2231793fc096SFrank Li 2232793fc096SFrank Li /* tx pause must be same as rx pause */ 2233793fc096SFrank Li fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; 2234793fc096SFrank Li fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; 2235793fc096SFrank Li 2236793fc096SFrank Li if (pause->rx_pause || pause->autoneg) { 223745f5c327SPhilippe Reynes ndev->phydev->supported |= ADVERTISED_Pause; 223845f5c327SPhilippe Reynes ndev->phydev->advertising |= ADVERTISED_Pause; 2239793fc096SFrank Li } else { 224045f5c327SPhilippe Reynes ndev->phydev->supported &= ~ADVERTISED_Pause; 224145f5c327SPhilippe Reynes ndev->phydev->advertising &= ~ADVERTISED_Pause; 2242793fc096SFrank Li } 2243793fc096SFrank Li 2244793fc096SFrank Li if (pause->autoneg) { 2245793fc096SFrank Li if (netif_running(ndev)) 2246793fc096SFrank Li fec_stop(ndev); 224745f5c327SPhilippe Reynes phy_start_aneg(ndev->phydev); 2248793fc096SFrank Li } 2249dbc64a8eSRussell King if (netif_running(ndev)) { 2250dbc64a8eSRussell King napi_disable(&fep->napi); 2251dbc64a8eSRussell King netif_tx_lock_bh(ndev); 2252ef83337dSRussell King fec_restart(ndev); 2253dbc64a8eSRussell King netif_wake_queue(ndev); 22546af42d42SRussell King netif_tx_unlock_bh(ndev); 2255dbc64a8eSRussell King napi_enable(&fep->napi); 2256dbc64a8eSRussell King } 2257793fc096SFrank Li 2258793fc096SFrank Li return 0; 2259793fc096SFrank Li } 2260793fc096SFrank Li 226138ae92dcSChris Healy static const struct fec_stat { 226238ae92dcSChris Healy char name[ETH_GSTRING_LEN]; 226338ae92dcSChris Healy u16 offset; 226438ae92dcSChris Healy } fec_stats[] = { 226538ae92dcSChris Healy /* RMON TX */ 226638ae92dcSChris Healy { "tx_dropped", RMON_T_DROP }, 226738ae92dcSChris Healy { "tx_packets", RMON_T_PACKETS }, 226838ae92dcSChris Healy { "tx_broadcast", RMON_T_BC_PKT }, 226938ae92dcSChris Healy { "tx_multicast", RMON_T_MC_PKT }, 227038ae92dcSChris Healy { "tx_crc_errors", RMON_T_CRC_ALIGN }, 227138ae92dcSChris Healy { "tx_undersize", RMON_T_UNDERSIZE }, 227238ae92dcSChris Healy { "tx_oversize", RMON_T_OVERSIZE }, 227338ae92dcSChris Healy { "tx_fragment", RMON_T_FRAG }, 227438ae92dcSChris Healy { "tx_jabber", RMON_T_JAB }, 227538ae92dcSChris Healy { "tx_collision", RMON_T_COL }, 227638ae92dcSChris Healy { "tx_64byte", RMON_T_P64 }, 227738ae92dcSChris Healy { "tx_65to127byte", RMON_T_P65TO127 }, 227838ae92dcSChris Healy { "tx_128to255byte", RMON_T_P128TO255 }, 227938ae92dcSChris Healy { "tx_256to511byte", RMON_T_P256TO511 }, 228038ae92dcSChris Healy { "tx_512to1023byte", RMON_T_P512TO1023 }, 228138ae92dcSChris Healy { "tx_1024to2047byte", RMON_T_P1024TO2047 }, 228238ae92dcSChris Healy { "tx_GTE2048byte", RMON_T_P_GTE2048 }, 228338ae92dcSChris Healy { "tx_octets", RMON_T_OCTETS }, 228438ae92dcSChris Healy 228538ae92dcSChris Healy /* IEEE TX */ 228638ae92dcSChris Healy { "IEEE_tx_drop", IEEE_T_DROP }, 228738ae92dcSChris Healy { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK }, 228838ae92dcSChris Healy { "IEEE_tx_1col", IEEE_T_1COL }, 228938ae92dcSChris Healy { "IEEE_tx_mcol", IEEE_T_MCOL }, 229038ae92dcSChris Healy { "IEEE_tx_def", IEEE_T_DEF }, 229138ae92dcSChris Healy { "IEEE_tx_lcol", IEEE_T_LCOL }, 229238ae92dcSChris Healy { "IEEE_tx_excol", IEEE_T_EXCOL }, 229338ae92dcSChris Healy { "IEEE_tx_macerr", IEEE_T_MACERR }, 229438ae92dcSChris Healy { "IEEE_tx_cserr", IEEE_T_CSERR }, 229538ae92dcSChris Healy { "IEEE_tx_sqe", IEEE_T_SQE }, 229638ae92dcSChris Healy { "IEEE_tx_fdxfc", IEEE_T_FDXFC }, 229738ae92dcSChris Healy { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK }, 229838ae92dcSChris Healy 229938ae92dcSChris Healy /* RMON RX */ 230038ae92dcSChris Healy { "rx_packets", RMON_R_PACKETS }, 230138ae92dcSChris Healy { "rx_broadcast", RMON_R_BC_PKT }, 230238ae92dcSChris Healy { "rx_multicast", RMON_R_MC_PKT }, 230338ae92dcSChris Healy { "rx_crc_errors", RMON_R_CRC_ALIGN }, 230438ae92dcSChris Healy { "rx_undersize", RMON_R_UNDERSIZE }, 230538ae92dcSChris Healy { "rx_oversize", RMON_R_OVERSIZE }, 230638ae92dcSChris Healy { "rx_fragment", RMON_R_FRAG }, 230738ae92dcSChris Healy { "rx_jabber", RMON_R_JAB }, 230838ae92dcSChris Healy { "rx_64byte", RMON_R_P64 }, 230938ae92dcSChris Healy { "rx_65to127byte", RMON_R_P65TO127 }, 231038ae92dcSChris Healy { "rx_128to255byte", RMON_R_P128TO255 }, 231138ae92dcSChris Healy { "rx_256to511byte", RMON_R_P256TO511 }, 231238ae92dcSChris Healy { "rx_512to1023byte", RMON_R_P512TO1023 }, 231338ae92dcSChris Healy { "rx_1024to2047byte", RMON_R_P1024TO2047 }, 231438ae92dcSChris Healy { "rx_GTE2048byte", RMON_R_P_GTE2048 }, 231538ae92dcSChris Healy { "rx_octets", RMON_R_OCTETS }, 231638ae92dcSChris Healy 231738ae92dcSChris Healy /* IEEE RX */ 231838ae92dcSChris Healy { "IEEE_rx_drop", IEEE_R_DROP }, 231938ae92dcSChris Healy { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK }, 232038ae92dcSChris Healy { "IEEE_rx_crc", IEEE_R_CRC }, 232138ae92dcSChris Healy { "IEEE_rx_align", IEEE_R_ALIGN }, 232238ae92dcSChris Healy { "IEEE_rx_macerr", IEEE_R_MACERR }, 232338ae92dcSChris Healy { "IEEE_rx_fdxfc", IEEE_R_FDXFC }, 232438ae92dcSChris Healy { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK }, 232538ae92dcSChris Healy }; 232638ae92dcSChris Healy 2327f85de666SNikita Yushchenko #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64)) 2328f85de666SNikita Yushchenko 232980cca775SNikita Yushchenko static void fec_enet_update_ethtool_stats(struct net_device *dev) 233038ae92dcSChris Healy { 233138ae92dcSChris Healy struct fec_enet_private *fep = netdev_priv(dev); 233238ae92dcSChris Healy int i; 233338ae92dcSChris Healy 233438ae92dcSChris Healy for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 233580cca775SNikita Yushchenko fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset); 233680cca775SNikita Yushchenko } 233780cca775SNikita Yushchenko 233880cca775SNikita Yushchenko static void fec_enet_get_ethtool_stats(struct net_device *dev, 233980cca775SNikita Yushchenko struct ethtool_stats *stats, u64 *data) 234080cca775SNikita Yushchenko { 234180cca775SNikita Yushchenko struct fec_enet_private *fep = netdev_priv(dev); 234280cca775SNikita Yushchenko 234380cca775SNikita Yushchenko if (netif_running(dev)) 234480cca775SNikita Yushchenko fec_enet_update_ethtool_stats(dev); 234580cca775SNikita Yushchenko 2346f85de666SNikita Yushchenko memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE); 234738ae92dcSChris Healy } 234838ae92dcSChris Healy 234938ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev, 235038ae92dcSChris Healy u32 stringset, u8 *data) 235138ae92dcSChris Healy { 235238ae92dcSChris Healy int i; 235338ae92dcSChris Healy switch (stringset) { 235438ae92dcSChris Healy case ETH_SS_STATS: 235538ae92dcSChris Healy for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 235638ae92dcSChris Healy memcpy(data + i * ETH_GSTRING_LEN, 235738ae92dcSChris Healy fec_stats[i].name, ETH_GSTRING_LEN); 235838ae92dcSChris Healy break; 235938ae92dcSChris Healy } 236038ae92dcSChris Healy } 236138ae92dcSChris Healy 236238ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset) 236338ae92dcSChris Healy { 236438ae92dcSChris Healy switch (sset) { 236538ae92dcSChris Healy case ETH_SS_STATS: 236638ae92dcSChris Healy return ARRAY_SIZE(fec_stats); 236738ae92dcSChris Healy default: 236838ae92dcSChris Healy return -EOPNOTSUPP; 236938ae92dcSChris Healy } 237038ae92dcSChris Healy } 2371f85de666SNikita Yushchenko 23722b30842bSAndrew Lunn static void fec_enet_clear_ethtool_stats(struct net_device *dev) 23732b30842bSAndrew Lunn { 23742b30842bSAndrew Lunn struct fec_enet_private *fep = netdev_priv(dev); 23752b30842bSAndrew Lunn int i; 23762b30842bSAndrew Lunn 23772b30842bSAndrew Lunn /* Disable MIB statistics counters */ 23782b30842bSAndrew Lunn writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT); 23792b30842bSAndrew Lunn 23802b30842bSAndrew Lunn for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 23812b30842bSAndrew Lunn writel(0, fep->hwp + fec_stats[i].offset); 23822b30842bSAndrew Lunn 23832b30842bSAndrew Lunn /* Don't disable MIB statistics counters */ 23842b30842bSAndrew Lunn writel(0, fep->hwp + FEC_MIB_CTRLSTAT); 23852b30842bSAndrew Lunn } 23862b30842bSAndrew Lunn 2387f85de666SNikita Yushchenko #else /* !defined(CONFIG_M5272) */ 2388f85de666SNikita Yushchenko #define FEC_STATS_SIZE 0 2389f85de666SNikita Yushchenko static inline void fec_enet_update_ethtool_stats(struct net_device *dev) 2390f85de666SNikita Yushchenko { 2391f85de666SNikita Yushchenko } 239241e8e404SFabio Estevam 239341e8e404SFabio Estevam static inline void fec_enet_clear_ethtool_stats(struct net_device *dev) 239441e8e404SFabio Estevam { 239541e8e404SFabio Estevam } 2396d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */ 239738ae92dcSChris Healy 2398d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb). 2399d851b47bSFugang Duan * TCTT unit is cycle_ns * 64 cycle 2400d851b47bSFugang Duan * So, the ICTT value = X us / (cycle_ns * 64) 2401d851b47bSFugang Duan */ 2402d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us) 2403d851b47bSFugang Duan { 2404d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2405d851b47bSFugang Duan 2406d851b47bSFugang Duan return us * (fep->itr_clk_rate / 64000) / 1000; 2407d851b47bSFugang Duan } 2408d851b47bSFugang Duan 2409d851b47bSFugang Duan /* Set threshold for interrupt coalescing */ 2410d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev) 2411d851b47bSFugang Duan { 2412d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2413d851b47bSFugang Duan int rx_itr, tx_itr; 2414d851b47bSFugang Duan 2415d851b47bSFugang Duan /* Must be greater than zero to avoid unpredictable behavior */ 2416d851b47bSFugang Duan if (!fep->rx_time_itr || !fep->rx_pkts_itr || 2417d851b47bSFugang Duan !fep->tx_time_itr || !fep->tx_pkts_itr) 2418d851b47bSFugang Duan return; 2419d851b47bSFugang Duan 2420d851b47bSFugang Duan /* Select enet system clock as Interrupt Coalescing 2421d851b47bSFugang Duan * timer Clock Source 2422d851b47bSFugang Duan */ 2423d851b47bSFugang Duan rx_itr = FEC_ITR_CLK_SEL; 2424d851b47bSFugang Duan tx_itr = FEC_ITR_CLK_SEL; 2425d851b47bSFugang Duan 2426d851b47bSFugang Duan /* set ICFT and ICTT */ 2427d851b47bSFugang Duan rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr); 2428d851b47bSFugang Duan rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr)); 2429d851b47bSFugang Duan tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr); 2430d851b47bSFugang Duan tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr)); 2431d851b47bSFugang Duan 2432d851b47bSFugang Duan rx_itr |= FEC_ITR_EN; 2433d851b47bSFugang Duan tx_itr |= FEC_ITR_EN; 2434d851b47bSFugang Duan 2435d851b47bSFugang Duan writel(tx_itr, fep->hwp + FEC_TXIC0); 2436d851b47bSFugang Duan writel(rx_itr, fep->hwp + FEC_RXIC0); 2437ff7566b8SFugang Duan if (fep->quirks & FEC_QUIRK_HAS_AVB) { 2438d851b47bSFugang Duan writel(tx_itr, fep->hwp + FEC_TXIC1); 2439d851b47bSFugang Duan writel(rx_itr, fep->hwp + FEC_RXIC1); 2440d851b47bSFugang Duan writel(tx_itr, fep->hwp + FEC_TXIC2); 2441d851b47bSFugang Duan writel(rx_itr, fep->hwp + FEC_RXIC2); 2442d851b47bSFugang Duan } 2443ff7566b8SFugang Duan } 2444d851b47bSFugang Duan 2445d851b47bSFugang Duan static int 2446d851b47bSFugang Duan fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec) 2447d851b47bSFugang Duan { 2448d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2449d851b47bSFugang Duan 2450ff7566b8SFugang Duan if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 2451d851b47bSFugang Duan return -EOPNOTSUPP; 2452d851b47bSFugang Duan 2453d851b47bSFugang Duan ec->rx_coalesce_usecs = fep->rx_time_itr; 2454d851b47bSFugang Duan ec->rx_max_coalesced_frames = fep->rx_pkts_itr; 2455d851b47bSFugang Duan 2456d851b47bSFugang Duan ec->tx_coalesce_usecs = fep->tx_time_itr; 2457d851b47bSFugang Duan ec->tx_max_coalesced_frames = fep->tx_pkts_itr; 2458d851b47bSFugang Duan 2459d851b47bSFugang Duan return 0; 2460d851b47bSFugang Duan } 2461d851b47bSFugang Duan 2462d851b47bSFugang Duan static int 2463d851b47bSFugang Duan fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec) 2464d851b47bSFugang Duan { 2465d851b47bSFugang Duan struct fec_enet_private *fep = netdev_priv(ndev); 2466d851b47bSFugang Duan unsigned int cycle; 2467d851b47bSFugang Duan 2468ff7566b8SFugang Duan if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 2469d851b47bSFugang Duan return -EOPNOTSUPP; 2470d851b47bSFugang Duan 2471d851b47bSFugang Duan if (ec->rx_max_coalesced_frames > 255) { 24729f647a6dSColin Ian King pr_err("Rx coalesced frames exceed hardware limitation\n"); 2473d851b47bSFugang Duan return -EINVAL; 2474d851b47bSFugang Duan } 2475d851b47bSFugang Duan 2476d851b47bSFugang Duan if (ec->tx_max_coalesced_frames > 255) { 24779f647a6dSColin Ian King pr_err("Tx coalesced frame exceed hardware limitation\n"); 2478d851b47bSFugang Duan return -EINVAL; 2479d851b47bSFugang Duan } 2480d851b47bSFugang Duan 2481d851b47bSFugang Duan cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr); 2482d851b47bSFugang Duan if (cycle > 0xFFFF) { 24839f647a6dSColin Ian King pr_err("Rx coalesced usec exceed hardware limitation\n"); 2484d851b47bSFugang Duan return -EINVAL; 2485d851b47bSFugang Duan } 2486d851b47bSFugang Duan 2487d851b47bSFugang Duan cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr); 2488d851b47bSFugang Duan if (cycle > 0xFFFF) { 24899f647a6dSColin Ian King pr_err("Rx coalesced usec exceed hardware limitation\n"); 2490d851b47bSFugang Duan return -EINVAL; 2491d851b47bSFugang Duan } 2492d851b47bSFugang Duan 2493d851b47bSFugang Duan fep->rx_time_itr = ec->rx_coalesce_usecs; 2494d851b47bSFugang Duan fep->rx_pkts_itr = ec->rx_max_coalesced_frames; 2495d851b47bSFugang Duan 2496d851b47bSFugang Duan fep->tx_time_itr = ec->tx_coalesce_usecs; 2497d851b47bSFugang Duan fep->tx_pkts_itr = ec->tx_max_coalesced_frames; 2498d851b47bSFugang Duan 2499d851b47bSFugang Duan fec_enet_itr_coal_set(ndev); 2500d851b47bSFugang Duan 2501d851b47bSFugang Duan return 0; 2502d851b47bSFugang Duan } 2503d851b47bSFugang Duan 2504d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev) 2505d851b47bSFugang Duan { 2506d851b47bSFugang Duan struct ethtool_coalesce ec; 2507d851b47bSFugang Duan 2508d851b47bSFugang Duan ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT; 2509d851b47bSFugang Duan ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT; 2510d851b47bSFugang Duan 2511d851b47bSFugang Duan ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT; 2512d851b47bSFugang Duan ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT; 2513d851b47bSFugang Duan 2514d851b47bSFugang Duan fec_enet_set_coalesce(ndev, &ec); 2515d851b47bSFugang Duan } 2516d851b47bSFugang Duan 25171b7bde6dSNimrod Andy static int fec_enet_get_tunable(struct net_device *netdev, 25181b7bde6dSNimrod Andy const struct ethtool_tunable *tuna, 25191b7bde6dSNimrod Andy void *data) 25201b7bde6dSNimrod Andy { 25211b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(netdev); 25221b7bde6dSNimrod Andy int ret = 0; 25231b7bde6dSNimrod Andy 25241b7bde6dSNimrod Andy switch (tuna->id) { 25251b7bde6dSNimrod Andy case ETHTOOL_RX_COPYBREAK: 25261b7bde6dSNimrod Andy *(u32 *)data = fep->rx_copybreak; 25271b7bde6dSNimrod Andy break; 25281b7bde6dSNimrod Andy default: 25291b7bde6dSNimrod Andy ret = -EINVAL; 25301b7bde6dSNimrod Andy break; 25311b7bde6dSNimrod Andy } 25321b7bde6dSNimrod Andy 25331b7bde6dSNimrod Andy return ret; 25341b7bde6dSNimrod Andy } 25351b7bde6dSNimrod Andy 25361b7bde6dSNimrod Andy static int fec_enet_set_tunable(struct net_device *netdev, 25371b7bde6dSNimrod Andy const struct ethtool_tunable *tuna, 25381b7bde6dSNimrod Andy const void *data) 25391b7bde6dSNimrod Andy { 25401b7bde6dSNimrod Andy struct fec_enet_private *fep = netdev_priv(netdev); 25411b7bde6dSNimrod Andy int ret = 0; 25421b7bde6dSNimrod Andy 25431b7bde6dSNimrod Andy switch (tuna->id) { 25441b7bde6dSNimrod Andy case ETHTOOL_RX_COPYBREAK: 25451b7bde6dSNimrod Andy fep->rx_copybreak = *(u32 *)data; 25461b7bde6dSNimrod Andy break; 25471b7bde6dSNimrod Andy default: 25481b7bde6dSNimrod Andy ret = -EINVAL; 25491b7bde6dSNimrod Andy break; 25501b7bde6dSNimrod Andy } 25511b7bde6dSNimrod Andy 25521b7bde6dSNimrod Andy return ret; 25531b7bde6dSNimrod Andy } 25541b7bde6dSNimrod Andy 2555de40ed31SNimrod Andy static void 2556de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2557de40ed31SNimrod Andy { 2558de40ed31SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 2559de40ed31SNimrod Andy 2560de40ed31SNimrod Andy if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) { 2561de40ed31SNimrod Andy wol->supported = WAKE_MAGIC; 2562de40ed31SNimrod Andy wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0; 2563de40ed31SNimrod Andy } else { 2564de40ed31SNimrod Andy wol->supported = wol->wolopts = 0; 2565de40ed31SNimrod Andy } 2566de40ed31SNimrod Andy } 2567de40ed31SNimrod Andy 2568de40ed31SNimrod Andy static int 2569de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2570de40ed31SNimrod Andy { 2571de40ed31SNimrod Andy struct fec_enet_private *fep = netdev_priv(ndev); 2572de40ed31SNimrod Andy 2573de40ed31SNimrod Andy if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET)) 2574de40ed31SNimrod Andy return -EINVAL; 2575de40ed31SNimrod Andy 2576de40ed31SNimrod Andy if (wol->wolopts & ~WAKE_MAGIC) 2577de40ed31SNimrod Andy return -EINVAL; 2578de40ed31SNimrod Andy 2579de40ed31SNimrod Andy device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC); 2580de40ed31SNimrod Andy if (device_may_wakeup(&ndev->dev)) { 2581de40ed31SNimrod Andy fep->wol_flag |= FEC_WOL_FLAG_ENABLE; 2582de40ed31SNimrod Andy if (fep->irq[0] > 0) 2583de40ed31SNimrod Andy enable_irq_wake(fep->irq[0]); 2584de40ed31SNimrod Andy } else { 2585de40ed31SNimrod Andy fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE); 2586de40ed31SNimrod Andy if (fep->irq[0] > 0) 2587de40ed31SNimrod Andy disable_irq_wake(fep->irq[0]); 2588de40ed31SNimrod Andy } 2589de40ed31SNimrod Andy 2590de40ed31SNimrod Andy return 0; 2591de40ed31SNimrod Andy } 2592de40ed31SNimrod Andy 2593793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = { 2594793fc096SFrank Li .get_drvinfo = fec_enet_get_drvinfo, 2595db65f35fSPhilippe Reynes .get_regs_len = fec_enet_get_regs_len, 2596db65f35fSPhilippe Reynes .get_regs = fec_enet_get_regs, 259711d59289SFlorian Fainelli .nway_reset = phy_ethtool_nway_reset, 2598c1d7c48fSRussell King .get_link = ethtool_op_get_link, 2599d851b47bSFugang Duan .get_coalesce = fec_enet_get_coalesce, 2600d851b47bSFugang Duan .set_coalesce = fec_enet_set_coalesce, 260138ae92dcSChris Healy #ifndef CONFIG_M5272 2602c1d7c48fSRussell King .get_pauseparam = fec_enet_get_pauseparam, 2603c1d7c48fSRussell King .set_pauseparam = fec_enet_set_pauseparam, 260438ae92dcSChris Healy .get_strings = fec_enet_get_strings, 2605c1d7c48fSRussell King .get_ethtool_stats = fec_enet_get_ethtool_stats, 260638ae92dcSChris Healy .get_sset_count = fec_enet_get_sset_count, 260738ae92dcSChris Healy #endif 2608c1d7c48fSRussell King .get_ts_info = fec_enet_get_ts_info, 26091b7bde6dSNimrod Andy .get_tunable = fec_enet_get_tunable, 26101b7bde6dSNimrod Andy .set_tunable = fec_enet_set_tunable, 2611de40ed31SNimrod Andy .get_wol = fec_enet_get_wol, 2612de40ed31SNimrod Andy .set_wol = fec_enet_set_wol, 26139365fbf5SPhilippe Reynes .get_link_ksettings = phy_ethtool_get_link_ksettings, 26149365fbf5SPhilippe Reynes .set_link_ksettings = phy_ethtool_set_link_ksettings, 2615793fc096SFrank Li }; 2616793fc096SFrank Li 2617793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 2618793fc096SFrank Li { 2619793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 262045f5c327SPhilippe Reynes struct phy_device *phydev = ndev->phydev; 2621793fc096SFrank Li 2622793fc096SFrank Li if (!netif_running(ndev)) 2623793fc096SFrank Li return -EINVAL; 2624793fc096SFrank Li 2625793fc096SFrank Li if (!phydev) 2626793fc096SFrank Li return -ENODEV; 2627793fc096SFrank Li 26281d5244d0SBen Hutchings if (fep->bufdesc_ex) { 26291d5244d0SBen Hutchings if (cmd == SIOCSHWTSTAMP) 26301d5244d0SBen Hutchings return fec_ptp_set(ndev, rq); 26311d5244d0SBen Hutchings if (cmd == SIOCGHWTSTAMP) 26321d5244d0SBen Hutchings return fec_ptp_get(ndev, rq); 26331d5244d0SBen Hutchings } 2634793fc096SFrank Li 2635793fc096SFrank Li return phy_mii_ioctl(phydev, rq, cmd); 2636793fc096SFrank Li } 2637793fc096SFrank Li 2638793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev) 2639793fc096SFrank Li { 2640793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2641793fc096SFrank Li unsigned int i; 2642793fc096SFrank Li struct sk_buff *skb; 2643793fc096SFrank Li struct bufdesc *bdp; 26444d494cdcSFugang Duan struct fec_enet_priv_tx_q *txq; 26454d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 264659d0f746SFrank Li unsigned int q; 2647793fc096SFrank Li 264859d0f746SFrank Li for (q = 0; q < fep->num_rx_queues; q++) { 264959d0f746SFrank Li rxq = fep->rx_queue[q]; 26507355f276STroy Kisky bdp = rxq->bd.base; 26517355f276STroy Kisky for (i = 0; i < rxq->bd.ring_size; i++) { 26524d494cdcSFugang Duan skb = rxq->rx_skbuff[i]; 26534d494cdcSFugang Duan rxq->rx_skbuff[i] = NULL; 2654730ee360SRussell King if (skb) { 265559d0f746SFrank Li dma_unmap_single(&fep->pdev->dev, 26565cfa3039SJohannes Berg fec32_to_cpu(bdp->cbd_bufaddr), 2657b64bf4b7SNimrod Andy FEC_ENET_RX_FRSIZE - fep->rx_align, 265859d0f746SFrank Li DMA_FROM_DEVICE); 2659793fc096SFrank Li dev_kfree_skb(skb); 2660730ee360SRussell King } 26617355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 266259d0f746SFrank Li } 2663793fc096SFrank Li } 2664793fc096SFrank Li 266559d0f746SFrank Li for (q = 0; q < fep->num_tx_queues; q++) { 266659d0f746SFrank Li txq = fep->tx_queue[q]; 26677355f276STroy Kisky bdp = txq->bd.base; 26687355f276STroy Kisky for (i = 0; i < txq->bd.ring_size; i++) { 26694d494cdcSFugang Duan kfree(txq->tx_bounce[i]); 26704d494cdcSFugang Duan txq->tx_bounce[i] = NULL; 26714d494cdcSFugang Duan skb = txq->tx_skbuff[i]; 26724d494cdcSFugang Duan txq->tx_skbuff[i] = NULL; 26738b7c9efaSRussell King dev_kfree_skb(skb); 26748b7c9efaSRussell King } 2675793fc096SFrank Li } 267659d0f746SFrank Li } 2677793fc096SFrank Li 267859d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev) 267959d0f746SFrank Li { 268059d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 268159d0f746SFrank Li int i; 268259d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 268359d0f746SFrank Li 268459d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) 268559d0f746SFrank Li if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) { 268659d0f746SFrank Li txq = fep->tx_queue[i]; 268794920128SFugang Duan dma_free_coherent(&fep->pdev->dev, 26887355f276STroy Kisky txq->bd.ring_size * TSO_HEADER_SIZE, 268959d0f746SFrank Li txq->tso_hdrs, 269059d0f746SFrank Li txq->tso_hdrs_dma); 269159d0f746SFrank Li } 269259d0f746SFrank Li 269359d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) 269459d0f746SFrank Li kfree(fep->rx_queue[i]); 269559d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) 269659d0f746SFrank Li kfree(fep->tx_queue[i]); 269759d0f746SFrank Li } 269859d0f746SFrank Li 269959d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev) 270059d0f746SFrank Li { 270159d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 270259d0f746SFrank Li int i; 270359d0f746SFrank Li int ret = 0; 270459d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 270559d0f746SFrank Li 270659d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 270759d0f746SFrank Li txq = kzalloc(sizeof(*txq), GFP_KERNEL); 270859d0f746SFrank Li if (!txq) { 270959d0f746SFrank Li ret = -ENOMEM; 271059d0f746SFrank Li goto alloc_failed; 271159d0f746SFrank Li } 271259d0f746SFrank Li 271359d0f746SFrank Li fep->tx_queue[i] = txq; 27147355f276STroy Kisky txq->bd.ring_size = TX_RING_SIZE; 27157355f276STroy Kisky fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size; 271659d0f746SFrank Li 271759d0f746SFrank Li txq->tx_stop_threshold = FEC_MAX_SKB_DESCS; 271859d0f746SFrank Li txq->tx_wake_threshold = 27197355f276STroy Kisky (txq->bd.ring_size - txq->tx_stop_threshold) / 2; 272059d0f746SFrank Li 272194920128SFugang Duan txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev, 27227355f276STroy Kisky txq->bd.ring_size * TSO_HEADER_SIZE, 272359d0f746SFrank Li &txq->tso_hdrs_dma, 272459d0f746SFrank Li GFP_KERNEL); 272559d0f746SFrank Li if (!txq->tso_hdrs) { 272659d0f746SFrank Li ret = -ENOMEM; 272759d0f746SFrank Li goto alloc_failed; 272859d0f746SFrank Li } 272959d0f746SFrank Li } 273059d0f746SFrank Li 273159d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) { 273259d0f746SFrank Li fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]), 273359d0f746SFrank Li GFP_KERNEL); 273459d0f746SFrank Li if (!fep->rx_queue[i]) { 273559d0f746SFrank Li ret = -ENOMEM; 273659d0f746SFrank Li goto alloc_failed; 273759d0f746SFrank Li } 273859d0f746SFrank Li 27397355f276STroy Kisky fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE; 27407355f276STroy Kisky fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size; 274159d0f746SFrank Li } 274259d0f746SFrank Li return ret; 274359d0f746SFrank Li 274459d0f746SFrank Li alloc_failed: 274559d0f746SFrank Li fec_enet_free_queue(ndev); 274659d0f746SFrank Li return ret; 274759d0f746SFrank Li } 274859d0f746SFrank Li 274959d0f746SFrank Li static int 275059d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue) 2751793fc096SFrank Li { 2752793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2753793fc096SFrank Li unsigned int i; 2754793fc096SFrank Li struct sk_buff *skb; 2755793fc096SFrank Li struct bufdesc *bdp; 27564d494cdcSFugang Duan struct fec_enet_priv_rx_q *rxq; 2757793fc096SFrank Li 275859d0f746SFrank Li rxq = fep->rx_queue[queue]; 27597355f276STroy Kisky bdp = rxq->bd.base; 27607355f276STroy Kisky for (i = 0; i < rxq->bd.ring_size; i++) { 2761793fc096SFrank Li skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); 2762ffdce2ccSRussell King if (!skb) 2763ffdce2ccSRussell King goto err_alloc; 2764793fc096SFrank Li 27651b7bde6dSNimrod Andy if (fec_enet_new_rxbdp(ndev, bdp, skb)) { 2766730ee360SRussell King dev_kfree_skb(skb); 2767ffdce2ccSRussell King goto err_alloc; 2768d842a31fSDuan Fugang-B38611 } 2769730ee360SRussell King 27704d494cdcSFugang Duan rxq->rx_skbuff[i] = skb; 27715cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 2772793fc096SFrank Li 2773793fc096SFrank Li if (fep->bufdesc_ex) { 2774793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 27755cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 2776793fc096SFrank Li } 2777793fc096SFrank Li 27787355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 2779793fc096SFrank Li } 2780793fc096SFrank Li 2781793fc096SFrank Li /* Set the last buffer to wrap. */ 27827355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 27835cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 278459d0f746SFrank Li return 0; 2785793fc096SFrank Li 278659d0f746SFrank Li err_alloc: 278759d0f746SFrank Li fec_enet_free_buffers(ndev); 278859d0f746SFrank Li return -ENOMEM; 278959d0f746SFrank Li } 279059d0f746SFrank Li 279159d0f746SFrank Li static int 279259d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue) 279359d0f746SFrank Li { 279459d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 279559d0f746SFrank Li unsigned int i; 279659d0f746SFrank Li struct bufdesc *bdp; 279759d0f746SFrank Li struct fec_enet_priv_tx_q *txq; 279859d0f746SFrank Li 279959d0f746SFrank Li txq = fep->tx_queue[queue]; 28007355f276STroy Kisky bdp = txq->bd.base; 28017355f276STroy Kisky for (i = 0; i < txq->bd.ring_size; i++) { 28024d494cdcSFugang Duan txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); 28034d494cdcSFugang Duan if (!txq->tx_bounce[i]) 2804ffdce2ccSRussell King goto err_alloc; 2805793fc096SFrank Li 28065cfa3039SJohannes Berg bdp->cbd_sc = cpu_to_fec16(0); 28075cfa3039SJohannes Berg bdp->cbd_bufaddr = cpu_to_fec32(0); 2808793fc096SFrank Li 2809793fc096SFrank Li if (fep->bufdesc_ex) { 2810793fc096SFrank Li struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 28115cfa3039SJohannes Berg ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT); 2812793fc096SFrank Li } 2813793fc096SFrank Li 28147355f276STroy Kisky bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 2815793fc096SFrank Li } 2816793fc096SFrank Li 2817793fc096SFrank Li /* Set the last buffer to wrap. */ 28187355f276STroy Kisky bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 28195cfa3039SJohannes Berg bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 2820793fc096SFrank Li 2821793fc096SFrank Li return 0; 2822ffdce2ccSRussell King 2823ffdce2ccSRussell King err_alloc: 2824ffdce2ccSRussell King fec_enet_free_buffers(ndev); 2825ffdce2ccSRussell King return -ENOMEM; 2826793fc096SFrank Li } 2827793fc096SFrank Li 282859d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev) 282959d0f746SFrank Li { 283059d0f746SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 283159d0f746SFrank Li unsigned int i; 283259d0f746SFrank Li 283359d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) 283459d0f746SFrank Li if (fec_enet_alloc_rxq_buffers(ndev, i)) 283559d0f746SFrank Li return -ENOMEM; 283659d0f746SFrank Li 283759d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) 283859d0f746SFrank Li if (fec_enet_alloc_txq_buffers(ndev, i)) 283959d0f746SFrank Li return -ENOMEM; 284059d0f746SFrank Li return 0; 284159d0f746SFrank Li } 284259d0f746SFrank Li 2843793fc096SFrank Li static int 2844793fc096SFrank Li fec_enet_open(struct net_device *ndev) 2845793fc096SFrank Li { 2846793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2847793fc096SFrank Li int ret; 28481b0a83acSRichard Leitner bool reset_again; 2849793fc096SFrank Li 28508fff755eSAndrew Lunn ret = pm_runtime_get_sync(&fep->pdev->dev); 2851b0c6ce24SFabio Estevam if (ret < 0) 28528fff755eSAndrew Lunn return ret; 28538fff755eSAndrew Lunn 28545bbde4d2SNimrod Andy pinctrl_pm_select_default_state(&fep->pdev->dev); 2855e8fcfcd5SNimrod Andy ret = fec_enet_clk_enable(ndev, true); 2856e8fcfcd5SNimrod Andy if (ret) 28578fff755eSAndrew Lunn goto clk_enable; 2858e8fcfcd5SNimrod Andy 28591b0a83acSRichard Leitner /* During the first fec_enet_open call the PHY isn't probed at this 28601b0a83acSRichard Leitner * point. Therefore the phy_reset_after_clk_enable() call within 28611b0a83acSRichard Leitner * fec_enet_clk_enable() fails. As we need this reset in order to be 28621b0a83acSRichard Leitner * sure the PHY is working correctly we check if we need to reset again 28631b0a83acSRichard Leitner * later when the PHY is probed 28641b0a83acSRichard Leitner */ 28651b0a83acSRichard Leitner if (ndev->phydev && ndev->phydev->drv) 28661b0a83acSRichard Leitner reset_again = false; 28671b0a83acSRichard Leitner else 28681b0a83acSRichard Leitner reset_again = true; 28691b0a83acSRichard Leitner 2870793fc096SFrank Li /* I should reset the ring buffers here, but I don't yet know 2871793fc096SFrank Li * a simple way to do that. 2872793fc096SFrank Li */ 2873793fc096SFrank Li 2874793fc096SFrank Li ret = fec_enet_alloc_buffers(ndev); 2875793fc096SFrank Li if (ret) 2876681d2421SFabio Estevam goto err_enet_alloc; 2877793fc096SFrank Li 287855dd2753SNimrod Andy /* Init MAC prior to mii bus probe */ 287955dd2753SNimrod Andy fec_restart(ndev); 288055dd2753SNimrod Andy 2881793fc096SFrank Li /* Probe and connect to PHY when open the interface */ 2882793fc096SFrank Li ret = fec_enet_mii_probe(ndev); 2883681d2421SFabio Estevam if (ret) 2884681d2421SFabio Estevam goto err_enet_mii_probe; 2885ce5eaf02SRussell King 28861b0a83acSRichard Leitner /* Call phy_reset_after_clk_enable() again if it failed during 28871b0a83acSRichard Leitner * phy_reset_after_clk_enable() before because the PHY wasn't probed. 28881b0a83acSRichard Leitner */ 28891b0a83acSRichard Leitner if (reset_again) 28901b0a83acSRichard Leitner phy_reset_after_clk_enable(ndev->phydev); 28911b0a83acSRichard Leitner 289229380905SLucas Stach if (fep->quirks & FEC_QUIRK_ERR006687) 289329380905SLucas Stach imx6q_cpuidle_fec_irqs_used(); 289429380905SLucas Stach 2895ce5eaf02SRussell King napi_enable(&fep->napi); 289645f5c327SPhilippe Reynes phy_start(ndev->phydev); 28974d494cdcSFugang Duan netif_tx_start_all_queues(ndev); 28984d494cdcSFugang Duan 2899de40ed31SNimrod Andy device_set_wakeup_enable(&ndev->dev, fep->wol_flag & 2900de40ed31SNimrod Andy FEC_WOL_FLAG_ENABLE); 2901de40ed31SNimrod Andy 2902793fc096SFrank Li return 0; 2903681d2421SFabio Estevam 2904681d2421SFabio Estevam err_enet_mii_probe: 2905681d2421SFabio Estevam fec_enet_free_buffers(ndev); 2906681d2421SFabio Estevam err_enet_alloc: 2907681d2421SFabio Estevam fec_enet_clk_enable(ndev, false); 29088fff755eSAndrew Lunn clk_enable: 29098fff755eSAndrew Lunn pm_runtime_mark_last_busy(&fep->pdev->dev); 29108fff755eSAndrew Lunn pm_runtime_put_autosuspend(&fep->pdev->dev); 2911681d2421SFabio Estevam pinctrl_pm_select_sleep_state(&fep->pdev->dev); 2912681d2421SFabio Estevam return ret; 2913793fc096SFrank Li } 2914793fc096SFrank Li 2915793fc096SFrank Li static int 2916793fc096SFrank Li fec_enet_close(struct net_device *ndev) 2917793fc096SFrank Li { 2918793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2919793fc096SFrank Li 292045f5c327SPhilippe Reynes phy_stop(ndev->phydev); 2921d76cfae9SRussell King 292231a6de34SRussell King if (netif_device_present(ndev)) { 2923793fc096SFrank Li napi_disable(&fep->napi); 2924b49cd504SRussell King netif_tx_disable(ndev); 2925793fc096SFrank Li fec_stop(ndev); 292631a6de34SRussell King } 2927793fc096SFrank Li 292845f5c327SPhilippe Reynes phy_disconnect(ndev->phydev); 2929793fc096SFrank Li 293029380905SLucas Stach if (fep->quirks & FEC_QUIRK_ERR006687) 293129380905SLucas Stach imx6q_cpuidle_fec_irqs_unused(); 293229380905SLucas Stach 293380cca775SNikita Yushchenko fec_enet_update_ethtool_stats(ndev); 293480cca775SNikita Yushchenko 2935e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 29365bbde4d2SNimrod Andy pinctrl_pm_select_sleep_state(&fep->pdev->dev); 29378fff755eSAndrew Lunn pm_runtime_mark_last_busy(&fep->pdev->dev); 29388fff755eSAndrew Lunn pm_runtime_put_autosuspend(&fep->pdev->dev); 29398fff755eSAndrew Lunn 2940793fc096SFrank Li fec_enet_free_buffers(ndev); 2941793fc096SFrank Li 2942793fc096SFrank Li return 0; 2943793fc096SFrank Li } 2944793fc096SFrank Li 2945793fc096SFrank Li /* Set or clear the multicast filter for this adaptor. 2946793fc096SFrank Li * Skeleton taken from sunlance driver. 2947793fc096SFrank Li * The CPM Ethernet implementation allows Multicast as well as individual 2948793fc096SFrank Li * MAC address filtering. Some of the drivers check to make sure it is 2949793fc096SFrank Li * a group multicast address, and discard those that are not. I guess I 2950793fc096SFrank Li * will do the same for now, but just remove the test if you want 2951793fc096SFrank Li * individual filtering as well (do the upper net layers want or support 2952793fc096SFrank Li * this kind of feature?). 2953793fc096SFrank Li */ 2954793fc096SFrank Li 29556176e89cSJiri Kosina #define FEC_HASH_BITS 6 /* #bits in hash */ 2956793fc096SFrank Li #define CRC32_POLY 0xEDB88320 2957793fc096SFrank Li 2958793fc096SFrank Li static void set_multicast_list(struct net_device *ndev) 2959793fc096SFrank Li { 2960793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 2961793fc096SFrank Li struct netdev_hw_addr *ha; 2962793fc096SFrank Li unsigned int i, bit, data, crc, tmp; 2963793fc096SFrank Li unsigned char hash; 296401f8902bSRui Sousa unsigned int hash_high = 0, hash_low = 0; 2965793fc096SFrank Li 2966793fc096SFrank Li if (ndev->flags & IFF_PROMISC) { 2967793fc096SFrank Li tmp = readl(fep->hwp + FEC_R_CNTRL); 2968793fc096SFrank Li tmp |= 0x8; 2969793fc096SFrank Li writel(tmp, fep->hwp + FEC_R_CNTRL); 2970793fc096SFrank Li return; 2971793fc096SFrank Li } 2972793fc096SFrank Li 2973793fc096SFrank Li tmp = readl(fep->hwp + FEC_R_CNTRL); 2974793fc096SFrank Li tmp &= ~0x8; 2975793fc096SFrank Li writel(tmp, fep->hwp + FEC_R_CNTRL); 2976793fc096SFrank Li 2977793fc096SFrank Li if (ndev->flags & IFF_ALLMULTI) { 2978793fc096SFrank Li /* Catch all multicast addresses, so set the 2979793fc096SFrank Li * filter to all 1's 2980793fc096SFrank Li */ 2981793fc096SFrank Li writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 2982793fc096SFrank Li writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 2983793fc096SFrank Li 2984793fc096SFrank Li return; 2985793fc096SFrank Li } 2986793fc096SFrank Li 298701f8902bSRui Sousa /* Add the addresses in hash register */ 2988793fc096SFrank Li netdev_for_each_mc_addr(ha, ndev) { 2989793fc096SFrank Li /* calculate crc32 value of mac address */ 2990793fc096SFrank Li crc = 0xffffffff; 2991793fc096SFrank Li 2992793fc096SFrank Li for (i = 0; i < ndev->addr_len; i++) { 2993793fc096SFrank Li data = ha->addr[i]; 2994793fc096SFrank Li for (bit = 0; bit < 8; bit++, data >>= 1) { 2995793fc096SFrank Li crc = (crc >> 1) ^ 2996793fc096SFrank Li (((crc ^ data) & 1) ? CRC32_POLY : 0); 2997793fc096SFrank Li } 2998793fc096SFrank Li } 2999793fc096SFrank Li 30006176e89cSJiri Kosina /* only upper 6 bits (FEC_HASH_BITS) are used 3001981a0547SPeter Meerwald-Stadler * which point to specific bit in the hash registers 3002793fc096SFrank Li */ 30036176e89cSJiri Kosina hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f; 3004793fc096SFrank Li 300501f8902bSRui Sousa if (hash > 31) 300601f8902bSRui Sousa hash_high |= 1 << (hash - 32); 300701f8902bSRui Sousa else 300801f8902bSRui Sousa hash_low |= 1 << hash; 3009793fc096SFrank Li } 301001f8902bSRui Sousa 301101f8902bSRui Sousa writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 301201f8902bSRui Sousa writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3013793fc096SFrank Li } 3014793fc096SFrank Li 3015793fc096SFrank Li /* Set a MAC change in hardware. */ 3016793fc096SFrank Li static int 3017793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p) 3018793fc096SFrank Li { 3019793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3020793fc096SFrank Li struct sockaddr *addr = p; 3021793fc096SFrank Li 302244934facSLucas Stach if (addr) { 3023793fc096SFrank Li if (!is_valid_ether_addr(addr->sa_data)) 3024793fc096SFrank Li return -EADDRNOTAVAIL; 3025793fc096SFrank Li memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len); 302644934facSLucas Stach } 3027793fc096SFrank Li 30289638d19eSNimrod Andy /* Add netif status check here to avoid system hang in below case: 30299638d19eSNimrod Andy * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx; 30309638d19eSNimrod Andy * After ethx down, fec all clocks are gated off and then register 30319638d19eSNimrod Andy * access causes system hang. 30329638d19eSNimrod Andy */ 30339638d19eSNimrod Andy if (!netif_running(ndev)) 30349638d19eSNimrod Andy return 0; 30359638d19eSNimrod Andy 3036793fc096SFrank Li writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | 3037793fc096SFrank Li (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), 3038793fc096SFrank Li fep->hwp + FEC_ADDR_LOW); 3039793fc096SFrank Li writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), 3040793fc096SFrank Li fep->hwp + FEC_ADDR_HIGH); 3041793fc096SFrank Li return 0; 3042793fc096SFrank Li } 3043793fc096SFrank Li 3044793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER 3045793fc096SFrank Li /** 3046793fc096SFrank Li * fec_poll_controller - FEC Poll controller function 3047793fc096SFrank Li * @dev: The FEC network adapter 3048793fc096SFrank Li * 3049793fc096SFrank Li * Polled functionality used by netconsole and others in non interrupt mode 3050793fc096SFrank Li * 3051793fc096SFrank Li */ 3052793fc096SFrank Li static void fec_poll_controller(struct net_device *dev) 3053793fc096SFrank Li { 3054793fc096SFrank Li int i; 3055793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(dev); 3056793fc096SFrank Li 3057793fc096SFrank Li for (i = 0; i < FEC_IRQ_NUM; i++) { 3058793fc096SFrank Li if (fep->irq[i] > 0) { 3059793fc096SFrank Li disable_irq(fep->irq[i]); 3060793fc096SFrank Li fec_enet_interrupt(fep->irq[i], dev); 3061793fc096SFrank Li enable_irq(fep->irq[i]); 3062793fc096SFrank Li } 3063793fc096SFrank Li } 3064793fc096SFrank Li } 3065793fc096SFrank Li #endif 3066793fc096SFrank Li 30675bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev, 30684c09eed9SJim Baxter netdev_features_t features) 30694c09eed9SJim Baxter { 30704c09eed9SJim Baxter struct fec_enet_private *fep = netdev_priv(netdev); 30714c09eed9SJim Baxter netdev_features_t changed = features ^ netdev->features; 30724c09eed9SJim Baxter 30734c09eed9SJim Baxter netdev->features = features; 30744c09eed9SJim Baxter 30754c09eed9SJim Baxter /* Receive checksum has been changed */ 30764c09eed9SJim Baxter if (changed & NETIF_F_RXCSUM) { 30774c09eed9SJim Baxter if (features & NETIF_F_RXCSUM) 30784c09eed9SJim Baxter fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 30794c09eed9SJim Baxter else 30804c09eed9SJim Baxter fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; 30818506fa1dSRussell King } 30825bc26726SNimrod Andy } 30834c09eed9SJim Baxter 30845bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev, 30855bc26726SNimrod Andy netdev_features_t features) 30865bc26726SNimrod Andy { 30875bc26726SNimrod Andy struct fec_enet_private *fep = netdev_priv(netdev); 30885bc26726SNimrod Andy netdev_features_t changed = features ^ netdev->features; 30895bc26726SNimrod Andy 30905b40f709SFabio Estevam if (netif_running(netdev) && changed & NETIF_F_RXCSUM) { 30915bc26726SNimrod Andy napi_disable(&fep->napi); 30925bc26726SNimrod Andy netif_tx_lock_bh(netdev); 30935bc26726SNimrod Andy fec_stop(netdev); 30945bc26726SNimrod Andy fec_enet_set_netdev_features(netdev, features); 3095ef83337dSRussell King fec_restart(netdev); 30964d494cdcSFugang Duan netif_tx_wake_all_queues(netdev); 30976af42d42SRussell King netif_tx_unlock_bh(netdev); 3098dbc64a8eSRussell King napi_enable(&fep->napi); 30995bc26726SNimrod Andy } else { 31005bc26726SNimrod Andy fec_enet_set_netdev_features(netdev, features); 31014c09eed9SJim Baxter } 31024c09eed9SJim Baxter 31034c09eed9SJim Baxter return 0; 31044c09eed9SJim Baxter } 31054c09eed9SJim Baxter 3106793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = { 3107793fc096SFrank Li .ndo_open = fec_enet_open, 3108793fc096SFrank Li .ndo_stop = fec_enet_close, 3109793fc096SFrank Li .ndo_start_xmit = fec_enet_start_xmit, 3110793fc096SFrank Li .ndo_set_rx_mode = set_multicast_list, 3111793fc096SFrank Li .ndo_validate_addr = eth_validate_addr, 3112793fc096SFrank Li .ndo_tx_timeout = fec_timeout, 3113793fc096SFrank Li .ndo_set_mac_address = fec_set_mac_address, 3114793fc096SFrank Li .ndo_do_ioctl = fec_enet_ioctl, 3115793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER 3116793fc096SFrank Li .ndo_poll_controller = fec_poll_controller, 3117793fc096SFrank Li #endif 31184c09eed9SJim Baxter .ndo_set_features = fec_set_features, 3119793fc096SFrank Li }; 3120793fc096SFrank Li 312153bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = { 312253bb20d1STroy Kisky FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2 312353bb20d1STroy Kisky }; 312453bb20d1STroy Kisky 312553bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = { 312653bb20d1STroy Kisky FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2 312753bb20d1STroy Kisky }; 312853bb20d1STroy Kisky 3129793fc096SFrank Li /* 3130793fc096SFrank Li * XXX: We need to clean up on failure exits here. 3131793fc096SFrank Li * 3132793fc096SFrank Li */ 3133793fc096SFrank Li static int fec_enet_init(struct net_device *ndev) 3134793fc096SFrank Li { 3135793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3136793fc096SFrank Li struct bufdesc *cbd_base; 31374d494cdcSFugang Duan dma_addr_t bd_dma; 313855d0218aSNimrod Andy int bd_size; 313959d0f746SFrank Li unsigned int i; 31407355f276STroy Kisky unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) : 31417355f276STroy Kisky sizeof(struct bufdesc); 31427355f276STroy Kisky unsigned dsize_log2 = __fls(dsize); 314355d0218aSNimrod Andy 31447355f276STroy Kisky WARN_ON(dsize != (1 << dsize_log2)); 31453f1dcc6aSLucas Stach #if defined(CONFIG_ARM) || defined(CONFIG_ARM64) 314641ef84ceSFugang Duan fep->rx_align = 0xf; 314741ef84ceSFugang Duan fep->tx_align = 0xf; 314841ef84ceSFugang Duan #else 314941ef84ceSFugang Duan fep->rx_align = 0x3; 315041ef84ceSFugang Duan fep->tx_align = 0x3; 315141ef84ceSFugang Duan #endif 315241ef84ceSFugang Duan 315359d0f746SFrank Li fec_enet_alloc_queue(ndev); 315479f33912SNimrod Andy 31557355f276STroy Kisky bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize; 3156793fc096SFrank Li 3157793fc096SFrank Li /* Allocate memory for buffer descriptors. */ 3158c0a1a0a6SLucas Stach cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma, 3159793fc096SFrank Li GFP_KERNEL); 31604d494cdcSFugang Duan if (!cbd_base) { 31614d494cdcSFugang Duan return -ENOMEM; 31624d494cdcSFugang Duan } 3163793fc096SFrank Li 31644d494cdcSFugang Duan memset(cbd_base, 0, bd_size); 3165793fc096SFrank Li 3166793fc096SFrank Li /* Get the Ethernet address */ 3167793fc096SFrank Li fec_get_mac(ndev); 316844934facSLucas Stach /* make sure MAC we just acquired is programmed into the hw */ 316944934facSLucas Stach fec_set_mac_address(ndev, NULL); 3170793fc096SFrank Li 3171793fc096SFrank Li /* Set receive and transmit descriptor base. */ 317259d0f746SFrank Li for (i = 0; i < fep->num_rx_queues; i++) { 31737355f276STroy Kisky struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i]; 31747355f276STroy Kisky unsigned size = dsize * rxq->bd.ring_size; 31757355f276STroy Kisky 31767355f276STroy Kisky rxq->bd.qid = i; 31777355f276STroy Kisky rxq->bd.base = cbd_base; 31787355f276STroy Kisky rxq->bd.cur = cbd_base; 31797355f276STroy Kisky rxq->bd.dma = bd_dma; 31807355f276STroy Kisky rxq->bd.dsize = dsize; 31817355f276STroy Kisky rxq->bd.dsize_log2 = dsize_log2; 318253bb20d1STroy Kisky rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i]; 31837355f276STroy Kisky bd_dma += size; 31847355f276STroy Kisky cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 31857355f276STroy Kisky rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 318659d0f746SFrank Li } 318759d0f746SFrank Li 318859d0f746SFrank Li for (i = 0; i < fep->num_tx_queues; i++) { 31897355f276STroy Kisky struct fec_enet_priv_tx_q *txq = fep->tx_queue[i]; 31907355f276STroy Kisky unsigned size = dsize * txq->bd.ring_size; 31917355f276STroy Kisky 31927355f276STroy Kisky txq->bd.qid = i; 31937355f276STroy Kisky txq->bd.base = cbd_base; 31947355f276STroy Kisky txq->bd.cur = cbd_base; 31957355f276STroy Kisky txq->bd.dma = bd_dma; 31967355f276STroy Kisky txq->bd.dsize = dsize; 31977355f276STroy Kisky txq->bd.dsize_log2 = dsize_log2; 319853bb20d1STroy Kisky txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i]; 31997355f276STroy Kisky bd_dma += size; 32007355f276STroy Kisky cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 32017355f276STroy Kisky txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 320259d0f746SFrank Li } 32034d494cdcSFugang Duan 3204793fc096SFrank Li 3205793fc096SFrank Li /* The FEC Ethernet specific entries in the device structure */ 3206793fc096SFrank Li ndev->watchdog_timeo = TX_TIMEOUT; 3207793fc096SFrank Li ndev->netdev_ops = &fec_netdev_ops; 3208793fc096SFrank Li ndev->ethtool_ops = &fec_enet_ethtool_ops; 3209793fc096SFrank Li 3210793fc096SFrank Li writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); 3211322555f5SFabio Estevam netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT); 3212793fc096SFrank Li 32136b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_VLAN) 3214cdffcf1bSJim Baxter /* enable hw VLAN support */ 3215cdffcf1bSJim Baxter ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; 3216cdffcf1bSJim Baxter 32176b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_CSUM) { 321879f33912SNimrod Andy ndev->gso_max_segs = FEC_MAX_TSO_SEGS; 321979f33912SNimrod Andy 32204c09eed9SJim Baxter /* enable hw accelerator */ 32214c09eed9SJim Baxter ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM 322279f33912SNimrod Andy | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO); 32234c09eed9SJim Baxter fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 322448496255SShawn Guo } 32254c09eed9SJim Baxter 32266b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_AVB) { 322741ef84ceSFugang Duan fep->tx_align = 0; 322841ef84ceSFugang Duan fep->rx_align = 0x3f; 322941ef84ceSFugang Duan } 323041ef84ceSFugang Duan 323109d1e541SNimrod Andy ndev->hw_features = ndev->features; 323209d1e541SNimrod Andy 3233ef83337dSRussell King fec_restart(ndev); 3234793fc096SFrank Li 32352b30842bSAndrew Lunn if (fep->quirks & FEC_QUIRK_MIB_CLEAR) 32362b30842bSAndrew Lunn fec_enet_clear_ethtool_stats(ndev); 32372b30842bSAndrew Lunn else 323880cca775SNikita Yushchenko fec_enet_update_ethtool_stats(ndev); 323980cca775SNikita Yushchenko 3240793fc096SFrank Li return 0; 3241793fc096SFrank Li } 3242793fc096SFrank Li 3243793fc096SFrank Li #ifdef CONFIG_OF 32449269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev) 3245793fc096SFrank Li { 3246793fc096SFrank Li int err, phy_reset; 3247962d8cdcSBernhard Walle bool active_high = false; 3248159a0760SQuentin Schulz int msec = 1, phy_post_delay = 0; 3249793fc096SFrank Li struct device_node *np = pdev->dev.of_node; 3250793fc096SFrank Li 3251793fc096SFrank Li if (!np) 32529269e556SFugang Duan return 0; 3253793fc096SFrank Li 325461e04ccbSFugang Duan err = of_property_read_u32(np, "phy-reset-duration", &msec); 3255793fc096SFrank Li /* A sane reset duration should not be longer than 1s */ 325661e04ccbSFugang Duan if (!err && msec > 1000) 3257793fc096SFrank Li msec = 1; 3258793fc096SFrank Li 3259793fc096SFrank Li phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0); 32609269e556SFugang Duan if (phy_reset == -EPROBE_DEFER) 32619269e556SFugang Duan return phy_reset; 32629269e556SFugang Duan else if (!gpio_is_valid(phy_reset)) 32639269e556SFugang Duan return 0; 3264793fc096SFrank Li 3265159a0760SQuentin Schulz err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay); 3266159a0760SQuentin Schulz /* valid reset duration should be less than 1s */ 3267159a0760SQuentin Schulz if (!err && phy_post_delay > 1000) 3268159a0760SQuentin Schulz return -EINVAL; 3269159a0760SQuentin Schulz 3270962d8cdcSBernhard Walle active_high = of_property_read_bool(np, "phy-reset-active-high"); 327164f10f6eSBernhard Walle 3272793fc096SFrank Li err = devm_gpio_request_one(&pdev->dev, phy_reset, 3273962d8cdcSBernhard Walle active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, 327464f10f6eSBernhard Walle "phy-reset"); 3275793fc096SFrank Li if (err) { 3276793fc096SFrank Li dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err); 32779269e556SFugang Duan return err; 3278793fc096SFrank Li } 3279eb37c563SStefan Wahren 3280eb37c563SStefan Wahren if (msec > 20) 3281793fc096SFrank Li msleep(msec); 3282eb37c563SStefan Wahren else 3283eb37c563SStefan Wahren usleep_range(msec * 1000, msec * 1000 + 1000); 3284eb37c563SStefan Wahren 3285962d8cdcSBernhard Walle gpio_set_value_cansleep(phy_reset, !active_high); 32869269e556SFugang Duan 3287159a0760SQuentin Schulz if (!phy_post_delay) 3288159a0760SQuentin Schulz return 0; 3289159a0760SQuentin Schulz 3290159a0760SQuentin Schulz if (phy_post_delay > 20) 3291159a0760SQuentin Schulz msleep(phy_post_delay); 3292159a0760SQuentin Schulz else 3293159a0760SQuentin Schulz usleep_range(phy_post_delay * 1000, 3294159a0760SQuentin Schulz phy_post_delay * 1000 + 1000); 3295159a0760SQuentin Schulz 32969269e556SFugang Duan return 0; 3297793fc096SFrank Li } 3298793fc096SFrank Li #else /* CONFIG_OF */ 32999269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev) 3300793fc096SFrank Li { 3301793fc096SFrank Li /* 3302793fc096SFrank Li * In case of platform probe, the reset has been done 3303793fc096SFrank Li * by machine code. 3304793fc096SFrank Li */ 33059269e556SFugang Duan return 0; 3306793fc096SFrank Li } 3307793fc096SFrank Li #endif /* CONFIG_OF */ 3308793fc096SFrank Li 33099fc095f1SFugang Duan static void 33109fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx) 33119fc095f1SFugang Duan { 33129fc095f1SFugang Duan struct device_node *np = pdev->dev.of_node; 33139fc095f1SFugang Duan 33149fc095f1SFugang Duan *num_tx = *num_rx = 1; 33159fc095f1SFugang Duan 33169fc095f1SFugang Duan if (!np || !of_device_is_available(np)) 33179fc095f1SFugang Duan return; 33189fc095f1SFugang Duan 33199fc095f1SFugang Duan /* parse the num of tx and rx queues */ 332073b1c90dSSaurabh Sengar of_property_read_u32(np, "fsl,num-tx-queues", num_tx); 3321b7bd75cfSFrank Li 332273b1c90dSSaurabh Sengar of_property_read_u32(np, "fsl,num-rx-queues", num_rx); 33239fc095f1SFugang Duan 33249fc095f1SFugang Duan if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) { 3325b7bd75cfSFrank Li dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n", 33269fc095f1SFugang Duan *num_tx); 33279fc095f1SFugang Duan *num_tx = 1; 33289fc095f1SFugang Duan return; 33299fc095f1SFugang Duan } 33309fc095f1SFugang Duan 33319fc095f1SFugang Duan if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) { 3332b7bd75cfSFrank Li dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n", 33339fc095f1SFugang Duan *num_rx); 33349fc095f1SFugang Duan *num_rx = 1; 33359fc095f1SFugang Duan return; 33369fc095f1SFugang Duan } 33379fc095f1SFugang Duan 33389fc095f1SFugang Duan } 33399fc095f1SFugang Duan 33404ad1ceecSTroy Kisky static int fec_enet_get_irq_cnt(struct platform_device *pdev) 33414ad1ceecSTroy Kisky { 33424ad1ceecSTroy Kisky int irq_cnt = platform_irq_count(pdev); 33434ad1ceecSTroy Kisky 33444ad1ceecSTroy Kisky if (irq_cnt > FEC_IRQ_NUM) 33454ad1ceecSTroy Kisky irq_cnt = FEC_IRQ_NUM; /* last for pps */ 33464ad1ceecSTroy Kisky else if (irq_cnt == 2) 33474ad1ceecSTroy Kisky irq_cnt = 1; /* last for pps */ 33484ad1ceecSTroy Kisky else if (irq_cnt <= 0) 33494ad1ceecSTroy Kisky irq_cnt = 1; /* At least 1 irq is needed */ 33504ad1ceecSTroy Kisky return irq_cnt; 33514ad1ceecSTroy Kisky } 33524ad1ceecSTroy Kisky 3353793fc096SFrank Li static int 3354793fc096SFrank Li fec_probe(struct platform_device *pdev) 3355793fc096SFrank Li { 3356793fc096SFrank Li struct fec_enet_private *fep; 3357793fc096SFrank Li struct fec_platform_data *pdata; 3358793fc096SFrank Li struct net_device *ndev; 3359793fc096SFrank Li int i, irq, ret = 0; 3360793fc096SFrank Li struct resource *r; 3361793fc096SFrank Li const struct of_device_id *of_id; 3362793fc096SFrank Li static int dev_id; 3363407066f8SUwe Kleine-König struct device_node *np = pdev->dev.of_node, *phy_node; 3364b7bd75cfSFrank Li int num_tx_qs; 3365b7bd75cfSFrank Li int num_rx_qs; 33664ad1ceecSTroy Kisky char irq_name[8]; 33674ad1ceecSTroy Kisky int irq_cnt; 3368793fc096SFrank Li 33699fc095f1SFugang Duan fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs); 33709fc095f1SFugang Duan 3371793fc096SFrank Li /* Init network device */ 337280cca775SNikita Yushchenko ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) + 3373f85de666SNikita Yushchenko FEC_STATS_SIZE, num_tx_qs, num_rx_qs); 3374793fc096SFrank Li if (!ndev) 3375793fc096SFrank Li return -ENOMEM; 3376793fc096SFrank Li 3377793fc096SFrank Li SET_NETDEV_DEV(ndev, &pdev->dev); 3378793fc096SFrank Li 3379793fc096SFrank Li /* setup board info structure */ 3380793fc096SFrank Li fep = netdev_priv(ndev); 3381793fc096SFrank Li 33826b7e4008SLothar Waßmann of_id = of_match_device(fec_dt_ids, &pdev->dev); 33836b7e4008SLothar Waßmann if (of_id) 33846b7e4008SLothar Waßmann pdev->id_entry = of_id->data; 33856b7e4008SLothar Waßmann fep->quirks = pdev->id_entry->driver_data; 33866b7e4008SLothar Waßmann 33870c818594SHubert Feurstein fep->netdev = ndev; 33889fc095f1SFugang Duan fep->num_rx_queues = num_rx_qs; 33899fc095f1SFugang Duan fep->num_tx_queues = num_tx_qs; 33909fc095f1SFugang Duan 3391d1391930SGuenter Roeck #if !defined(CONFIG_M5272) 3392793fc096SFrank Li /* default enable pause frame auto negotiation */ 33936b7e4008SLothar Waßmann if (fep->quirks & FEC_QUIRK_HAS_GBIT) 3394793fc096SFrank Li fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; 3395d1391930SGuenter Roeck #endif 3396793fc096SFrank Li 33975bbde4d2SNimrod Andy /* Select default pin state */ 33985bbde4d2SNimrod Andy pinctrl_pm_select_default_state(&pdev->dev); 33995bbde4d2SNimrod Andy 3400399db75bSFabio Estevam r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3401941e173aSTushar Behera fep->hwp = devm_ioremap_resource(&pdev->dev, r); 3402941e173aSTushar Behera if (IS_ERR(fep->hwp)) { 3403941e173aSTushar Behera ret = PTR_ERR(fep->hwp); 3404941e173aSTushar Behera goto failed_ioremap; 3405941e173aSTushar Behera } 3406941e173aSTushar Behera 3407793fc096SFrank Li fep->pdev = pdev; 3408793fc096SFrank Li fep->dev_id = dev_id++; 3409793fc096SFrank Li 3410793fc096SFrank Li platform_set_drvdata(pdev, ndev); 3411793fc096SFrank Li 341229380905SLucas Stach if ((of_machine_is_compatible("fsl,imx6q") || 341329380905SLucas Stach of_machine_is_compatible("fsl,imx6dl")) && 341429380905SLucas Stach !of_property_read_bool(np, "fsl,err006687-workaround-present")) 341529380905SLucas Stach fep->quirks |= FEC_QUIRK_ERR006687; 341629380905SLucas Stach 3417de40ed31SNimrod Andy if (of_get_property(np, "fsl,magic-packet", NULL)) 3418de40ed31SNimrod Andy fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET; 3419de40ed31SNimrod Andy 3420407066f8SUwe Kleine-König phy_node = of_parse_phandle(np, "phy-handle", 0); 3421407066f8SUwe Kleine-König if (!phy_node && of_phy_is_fixed_link(np)) { 3422407066f8SUwe Kleine-König ret = of_phy_register_fixed_link(np); 3423407066f8SUwe Kleine-König if (ret < 0) { 3424407066f8SUwe Kleine-König dev_err(&pdev->dev, 3425407066f8SUwe Kleine-König "broken fixed-link specification\n"); 3426407066f8SUwe Kleine-König goto failed_phy; 3427407066f8SUwe Kleine-König } 3428407066f8SUwe Kleine-König phy_node = of_node_get(np); 3429407066f8SUwe Kleine-König } 3430407066f8SUwe Kleine-König fep->phy_node = phy_node; 3431407066f8SUwe Kleine-König 34326c5f7808SGuenter Roeck ret = of_get_phy_mode(pdev->dev.of_node); 3433793fc096SFrank Li if (ret < 0) { 343494660ba0SJingoo Han pdata = dev_get_platdata(&pdev->dev); 3435793fc096SFrank Li if (pdata) 3436793fc096SFrank Li fep->phy_interface = pdata->phy; 3437793fc096SFrank Li else 3438793fc096SFrank Li fep->phy_interface = PHY_INTERFACE_MODE_MII; 3439793fc096SFrank Li } else { 3440793fc096SFrank Li fep->phy_interface = ret; 3441793fc096SFrank Li } 3442793fc096SFrank Li 3443793fc096SFrank Li fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 3444793fc096SFrank Li if (IS_ERR(fep->clk_ipg)) { 3445793fc096SFrank Li ret = PTR_ERR(fep->clk_ipg); 3446793fc096SFrank Li goto failed_clk; 3447793fc096SFrank Li } 3448793fc096SFrank Li 3449793fc096SFrank Li fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 3450793fc096SFrank Li if (IS_ERR(fep->clk_ahb)) { 3451793fc096SFrank Li ret = PTR_ERR(fep->clk_ahb); 3452793fc096SFrank Li goto failed_clk; 3453793fc096SFrank Li } 3454793fc096SFrank Li 3455d851b47bSFugang Duan fep->itr_clk_rate = clk_get_rate(fep->clk_ahb); 3456d851b47bSFugang Duan 345738f56f33SLinus Torvalds /* enet_out is optional, depends on board */ 345838f56f33SLinus Torvalds fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out"); 345938f56f33SLinus Torvalds if (IS_ERR(fep->clk_enet_out)) 346038f56f33SLinus Torvalds fep->clk_enet_out = NULL; 346138f56f33SLinus Torvalds 346291c0d987SNimrod Andy fep->ptp_clk_on = false; 346391c0d987SNimrod Andy mutex_init(&fep->ptp_clk_mutex); 34649b5330edSFugang Duan 34659b5330edSFugang Duan /* clk_ref is optional, depends on board */ 34669b5330edSFugang Duan fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref"); 34679b5330edSFugang Duan if (IS_ERR(fep->clk_ref)) 34689b5330edSFugang Duan fep->clk_ref = NULL; 34699b5330edSFugang Duan 34706b7e4008SLothar Waßmann fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX; 3471793fc096SFrank Li fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); 3472793fc096SFrank Li if (IS_ERR(fep->clk_ptp)) { 347338f56f33SLinus Torvalds fep->clk_ptp = NULL; 3474217b5844SLothar Waßmann fep->bufdesc_ex = false; 3475793fc096SFrank Li } 3476793fc096SFrank Li 3477e8fcfcd5SNimrod Andy ret = fec_enet_clk_enable(ndev, true); 347813a097bdSFabio Estevam if (ret) 347913a097bdSFabio Estevam goto failed_clk; 348013a097bdSFabio Estevam 34818fff755eSAndrew Lunn ret = clk_prepare_enable(fep->clk_ipg); 34828fff755eSAndrew Lunn if (ret) 34838fff755eSAndrew Lunn goto failed_clk_ipg; 34848fff755eSAndrew Lunn 3485f4e9f3d2SFabio Estevam fep->reg_phy = devm_regulator_get(&pdev->dev, "phy"); 3486f4e9f3d2SFabio Estevam if (!IS_ERR(fep->reg_phy)) { 3487f4e9f3d2SFabio Estevam ret = regulator_enable(fep->reg_phy); 3488793fc096SFrank Li if (ret) { 3489793fc096SFrank Li dev_err(&pdev->dev, 3490793fc096SFrank Li "Failed to enable phy regulator: %d\n", ret); 34919269e556SFugang Duan clk_disable_unprepare(fep->clk_ipg); 3492793fc096SFrank Li goto failed_regulator; 3493793fc096SFrank Li } 3494f6a4d607SFabio Estevam } else { 34953f38c683SFugang Duan if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) { 34963f38c683SFugang Duan ret = -EPROBE_DEFER; 34973f38c683SFugang Duan goto failed_regulator; 34983f38c683SFugang Duan } 3499f6a4d607SFabio Estevam fep->reg_phy = NULL; 3500793fc096SFrank Li } 3501793fc096SFrank Li 35028fff755eSAndrew Lunn pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT); 35038fff755eSAndrew Lunn pm_runtime_use_autosuspend(&pdev->dev); 350414d2b7c1SLucas Stach pm_runtime_get_noresume(&pdev->dev); 35058fff755eSAndrew Lunn pm_runtime_set_active(&pdev->dev); 35068fff755eSAndrew Lunn pm_runtime_enable(&pdev->dev); 35078fff755eSAndrew Lunn 35089269e556SFugang Duan ret = fec_reset_phy(pdev); 35099269e556SFugang Duan if (ret) 35109269e556SFugang Duan goto failed_reset; 3511793fc096SFrank Li 35124ad1ceecSTroy Kisky irq_cnt = fec_enet_get_irq_cnt(pdev); 3513793fc096SFrank Li if (fep->bufdesc_ex) 35144ad1ceecSTroy Kisky fec_ptp_init(pdev, irq_cnt); 3515793fc096SFrank Li 3516793fc096SFrank Li ret = fec_enet_init(ndev); 3517793fc096SFrank Li if (ret) 3518793fc096SFrank Li goto failed_init; 3519793fc096SFrank Li 35204ad1ceecSTroy Kisky for (i = 0; i < irq_cnt; i++) { 35214ad1ceecSTroy Kisky sprintf(irq_name, "int%d", i); 35224ad1ceecSTroy Kisky irq = platform_get_irq_byname(pdev, irq_name); 35234ad1ceecSTroy Kisky if (irq < 0) 3524793fc096SFrank Li irq = platform_get_irq(pdev, i); 3525793fc096SFrank Li if (irq < 0) { 3526793fc096SFrank Li ret = irq; 3527793fc096SFrank Li goto failed_irq; 3528793fc096SFrank Li } 35290d9b2ab1SFabio Estevam ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt, 353044a272ddSMichael Opdenacker 0, pdev->name, ndev); 35310d9b2ab1SFabio Estevam if (ret) 3532793fc096SFrank Li goto failed_irq; 3533de40ed31SNimrod Andy 3534de40ed31SNimrod Andy fep->irq[i] = irq; 3535793fc096SFrank Li } 3536793fc096SFrank Li 3537b4d39b53SFugang Duan init_completion(&fep->mdio_done); 3538793fc096SFrank Li ret = fec_enet_mii_init(pdev); 3539793fc096SFrank Li if (ret) 3540793fc096SFrank Li goto failed_mii_init; 3541793fc096SFrank Li 3542793fc096SFrank Li /* Carrier starts down, phylib will bring it up */ 3543793fc096SFrank Li netif_carrier_off(ndev); 3544e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 35455bbde4d2SNimrod Andy pinctrl_pm_select_sleep_state(&pdev->dev); 3546793fc096SFrank Li 3547793fc096SFrank Li ret = register_netdev(ndev); 3548793fc096SFrank Li if (ret) 3549793fc096SFrank Li goto failed_register; 3550793fc096SFrank Li 3551de40ed31SNimrod Andy device_init_wakeup(&ndev->dev, fep->wol_flag & 3552de40ed31SNimrod Andy FEC_WOL_HAS_MAGIC_PACKET); 3553de40ed31SNimrod Andy 3554eb1d0640SFabio Estevam if (fep->bufdesc_ex && fep->ptp_clock) 3555eb1d0640SFabio Estevam netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); 3556eb1d0640SFabio Estevam 35571b7bde6dSNimrod Andy fep->rx_copybreak = COPYBREAK_DEFAULT; 355836cdc743SRussell King INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work); 35598fff755eSAndrew Lunn 35608fff755eSAndrew Lunn pm_runtime_mark_last_busy(&pdev->dev); 35618fff755eSAndrew Lunn pm_runtime_put_autosuspend(&pdev->dev); 35628fff755eSAndrew Lunn 3563793fc096SFrank Li return 0; 3564793fc096SFrank Li 3565793fc096SFrank Li failed_register: 3566793fc096SFrank Li fec_enet_mii_remove(fep); 3567793fc096SFrank Li failed_mii_init: 35687a2bbd8dSFabio Estevam failed_irq: 35697a2bbd8dSFabio Estevam failed_init: 357032cba57bSLucas Stach fec_ptp_stop(pdev); 3571f6a4d607SFabio Estevam if (fep->reg_phy) 3572f6a4d607SFabio Estevam regulator_disable(fep->reg_phy); 35739269e556SFugang Duan failed_reset: 35749269e556SFugang Duan pm_runtime_put(&pdev->dev); 35759269e556SFugang Duan pm_runtime_disable(&pdev->dev); 3576793fc096SFrank Li failed_regulator: 35778fff755eSAndrew Lunn failed_clk_ipg: 3578e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 3579793fc096SFrank Li failed_clk: 358082005b1cSJohan Hovold if (of_phy_is_fixed_link(np)) 358182005b1cSJohan Hovold of_phy_deregister_fixed_link(np); 3582407066f8SUwe Kleine-König of_node_put(phy_node); 3583d1616f07SFugang Duan failed_phy: 3584d1616f07SFugang Duan dev_id--; 3585793fc096SFrank Li failed_ioremap: 3586793fc096SFrank Li free_netdev(ndev); 3587793fc096SFrank Li 3588793fc096SFrank Li return ret; 3589793fc096SFrank Li } 3590793fc096SFrank Li 3591793fc096SFrank Li static int 3592793fc096SFrank Li fec_drv_remove(struct platform_device *pdev) 3593793fc096SFrank Li { 3594793fc096SFrank Li struct net_device *ndev = platform_get_drvdata(pdev); 3595793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 359682005b1cSJohan Hovold struct device_node *np = pdev->dev.of_node; 3597793fc096SFrank Li 359836cdc743SRussell King cancel_work_sync(&fep->tx_timeout_work); 359932cba57bSLucas Stach fec_ptp_stop(pdev); 3600793fc096SFrank Li unregister_netdev(ndev); 3601793fc096SFrank Li fec_enet_mii_remove(fep); 3602f6a4d607SFabio Estevam if (fep->reg_phy) 3603f6a4d607SFabio Estevam regulator_disable(fep->reg_phy); 3604a069215cSFlorian Fainelli pm_runtime_put(&pdev->dev); 3605a069215cSFlorian Fainelli pm_runtime_disable(&pdev->dev); 360682005b1cSJohan Hovold if (of_phy_is_fixed_link(np)) 360782005b1cSJohan Hovold of_phy_deregister_fixed_link(np); 3608407066f8SUwe Kleine-König of_node_put(fep->phy_node); 3609793fc096SFrank Li free_netdev(ndev); 3610793fc096SFrank Li 3611793fc096SFrank Li return 0; 3612793fc096SFrank Li } 3613793fc096SFrank Li 3614dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev) 3615793fc096SFrank Li { 3616793fc096SFrank Li struct net_device *ndev = dev_get_drvdata(dev); 3617793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3618793fc096SFrank Li 3619da1774e5SRussell King rtnl_lock(); 3620793fc096SFrank Li if (netif_running(ndev)) { 3621de40ed31SNimrod Andy if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) 3622de40ed31SNimrod Andy fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON; 362345f5c327SPhilippe Reynes phy_stop(ndev->phydev); 362431a6de34SRussell King napi_disable(&fep->napi); 362531a6de34SRussell King netif_tx_lock_bh(ndev); 3626793fc096SFrank Li netif_device_detach(ndev); 362731a6de34SRussell King netif_tx_unlock_bh(ndev); 362831a6de34SRussell King fec_stop(ndev); 3629e8fcfcd5SNimrod Andy fec_enet_clk_enable(ndev, false); 3630de40ed31SNimrod Andy if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) 36315bbde4d2SNimrod Andy pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3632f4c4a4e0SNimrod Andy } 3633f4c4a4e0SNimrod Andy rtnl_unlock(); 3634793fc096SFrank Li 3635de40ed31SNimrod Andy if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) 3636238f7bc7SFabio Estevam regulator_disable(fep->reg_phy); 3637238f7bc7SFabio Estevam 3638858eeb7dSNimrod Andy /* SOC supply clock to phy, when clock is disabled, phy link down 3639858eeb7dSNimrod Andy * SOC control phy regulator, when regulator is disabled, phy link down 3640858eeb7dSNimrod Andy */ 3641858eeb7dSNimrod Andy if (fep->clk_enet_out || fep->reg_phy) 3642858eeb7dSNimrod Andy fep->link = 0; 3643858eeb7dSNimrod Andy 3644793fc096SFrank Li return 0; 3645793fc096SFrank Li } 3646793fc096SFrank Li 3647dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev) 3648793fc096SFrank Li { 3649793fc096SFrank Li struct net_device *ndev = dev_get_drvdata(dev); 3650793fc096SFrank Li struct fec_enet_private *fep = netdev_priv(ndev); 3651de40ed31SNimrod Andy struct fec_platform_data *pdata = fep->pdev->dev.platform_data; 3652238f7bc7SFabio Estevam int ret; 3653de40ed31SNimrod Andy int val; 3654238f7bc7SFabio Estevam 3655de40ed31SNimrod Andy if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) { 3656238f7bc7SFabio Estevam ret = regulator_enable(fep->reg_phy); 3657238f7bc7SFabio Estevam if (ret) 3658238f7bc7SFabio Estevam return ret; 3659238f7bc7SFabio Estevam } 3660793fc096SFrank Li 3661da1774e5SRussell King rtnl_lock(); 3662793fc096SFrank Li if (netif_running(ndev)) { 3663f4c4a4e0SNimrod Andy ret = fec_enet_clk_enable(ndev, true); 3664f4c4a4e0SNimrod Andy if (ret) { 3665f4c4a4e0SNimrod Andy rtnl_unlock(); 3666f4c4a4e0SNimrod Andy goto failed_clk; 3667f4c4a4e0SNimrod Andy } 3668de40ed31SNimrod Andy if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) { 3669de40ed31SNimrod Andy if (pdata && pdata->sleep_mode_enable) 3670de40ed31SNimrod Andy pdata->sleep_mode_enable(false); 3671de40ed31SNimrod Andy val = readl(fep->hwp + FEC_ECNTRL); 3672de40ed31SNimrod Andy val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 3673de40ed31SNimrod Andy writel(val, fep->hwp + FEC_ECNTRL); 3674de40ed31SNimrod Andy fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON; 3675de40ed31SNimrod Andy } else { 3676de40ed31SNimrod Andy pinctrl_pm_select_default_state(&fep->pdev->dev); 3677de40ed31SNimrod Andy } 3678ef83337dSRussell King fec_restart(ndev); 367931a6de34SRussell King netif_tx_lock_bh(ndev); 3680793fc096SFrank Li netif_device_attach(ndev); 36816af42d42SRussell King netif_tx_unlock_bh(ndev); 36826af42d42SRussell King napi_enable(&fep->napi); 368345f5c327SPhilippe Reynes phy_start(ndev->phydev); 3684793fc096SFrank Li } 3685da1774e5SRussell King rtnl_unlock(); 3686793fc096SFrank Li 3687793fc096SFrank Li return 0; 368813a097bdSFabio Estevam 3689e8fcfcd5SNimrod Andy failed_clk: 369013a097bdSFabio Estevam if (fep->reg_phy) 369113a097bdSFabio Estevam regulator_disable(fep->reg_phy); 369213a097bdSFabio Estevam return ret; 3693793fc096SFrank Li } 3694793fc096SFrank Li 36958fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev) 36968fff755eSAndrew Lunn { 36978fff755eSAndrew Lunn struct net_device *ndev = dev_get_drvdata(dev); 36988fff755eSAndrew Lunn struct fec_enet_private *fep = netdev_priv(ndev); 36998fff755eSAndrew Lunn 37008fff755eSAndrew Lunn clk_disable_unprepare(fep->clk_ipg); 37018fff755eSAndrew Lunn 37028fff755eSAndrew Lunn return 0; 37038fff755eSAndrew Lunn } 37048fff755eSAndrew Lunn 37058fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev) 37068fff755eSAndrew Lunn { 37078fff755eSAndrew Lunn struct net_device *ndev = dev_get_drvdata(dev); 37088fff755eSAndrew Lunn struct fec_enet_private *fep = netdev_priv(ndev); 37098fff755eSAndrew Lunn 37108fff755eSAndrew Lunn return clk_prepare_enable(fep->clk_ipg); 37118fff755eSAndrew Lunn } 37128fff755eSAndrew Lunn 37138fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = { 37148fff755eSAndrew Lunn SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume) 37158fff755eSAndrew Lunn SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL) 37168fff755eSAndrew Lunn }; 3717793fc096SFrank Li 3718793fc096SFrank Li static struct platform_driver fec_driver = { 3719793fc096SFrank Li .driver = { 3720793fc096SFrank Li .name = DRIVER_NAME, 3721793fc096SFrank Li .pm = &fec_pm_ops, 3722793fc096SFrank Li .of_match_table = fec_dt_ids, 3723793fc096SFrank Li }, 3724793fc096SFrank Li .id_table = fec_devtype, 3725793fc096SFrank Li .probe = fec_probe, 3726793fc096SFrank Li .remove = fec_drv_remove, 3727793fc096SFrank Li }; 3728793fc096SFrank Li 3729793fc096SFrank Li module_platform_driver(fec_driver); 3730793fc096SFrank Li 3731f8c0aca9SFabio Estevam MODULE_ALIAS("platform:"DRIVER_NAME); 3732793fc096SFrank Li MODULE_LICENSE("GPL"); 3733