1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx. 4 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net) 5 * 6 * Right now, I am very wasteful with the buffers. I allocate memory 7 * pages and then divide them into 2K frame buffers. This way I know I 8 * have buffers large enough to hold one frame within one buffer descriptor. 9 * Once I get this working, I will use 64 or 128 byte CPM buffers, which 10 * will be much more memory efficient and will easily handle lots of 11 * small packets. 12 * 13 * Much better multiple PHY support by Magnus Damm. 14 * Copyright (c) 2000 Ericsson Radio Systems AB. 15 * 16 * Support for FEC controller of ColdFire processors. 17 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com) 18 * 19 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be) 20 * Copyright (c) 2004-2006 Macq Electronique SA. 21 * 22 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. 23 */ 24 25 #include <linux/module.h> 26 #include <linux/kernel.h> 27 #include <linux/string.h> 28 #include <linux/pm_runtime.h> 29 #include <linux/ptrace.h> 30 #include <linux/errno.h> 31 #include <linux/ioport.h> 32 #include <linux/slab.h> 33 #include <linux/interrupt.h> 34 #include <linux/delay.h> 35 #include <linux/netdevice.h> 36 #include <linux/etherdevice.h> 37 #include <linux/skbuff.h> 38 #include <linux/in.h> 39 #include <linux/ip.h> 40 #include <net/ip.h> 41 #include <net/selftests.h> 42 #include <net/tso.h> 43 #include <linux/tcp.h> 44 #include <linux/udp.h> 45 #include <linux/icmp.h> 46 #include <linux/spinlock.h> 47 #include <linux/workqueue.h> 48 #include <linux/bitops.h> 49 #include <linux/io.h> 50 #include <linux/irq.h> 51 #include <linux/clk.h> 52 #include <linux/crc32.h> 53 #include <linux/platform_device.h> 54 #include <linux/mdio.h> 55 #include <linux/phy.h> 56 #include <linux/fec.h> 57 #include <linux/of.h> 58 #include <linux/of_device.h> 59 #include <linux/of_gpio.h> 60 #include <linux/of_mdio.h> 61 #include <linux/of_net.h> 62 #include <linux/regulator/consumer.h> 63 #include <linux/if_vlan.h> 64 #include <linux/pinctrl/consumer.h> 65 #include <linux/prefetch.h> 66 #include <linux/mfd/syscon.h> 67 #include <linux/regmap.h> 68 #include <soc/imx/cpuidle.h> 69 #include <linux/filter.h> 70 #include <linux/bpf.h> 71 72 #include <asm/cacheflush.h> 73 74 #include "fec.h" 75 76 static void set_multicast_list(struct net_device *ndev); 77 static void fec_enet_itr_coal_set(struct net_device *ndev); 78 79 #define DRIVER_NAME "fec" 80 81 static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2}; 82 83 /* Pause frame feild and FIFO threshold */ 84 #define FEC_ENET_FCE (1 << 5) 85 #define FEC_ENET_RSEM_V 0x84 86 #define FEC_ENET_RSFL_V 16 87 #define FEC_ENET_RAEM_V 0x8 88 #define FEC_ENET_RAFL_V 0x8 89 #define FEC_ENET_OPD_V 0xFFF0 90 #define FEC_MDIO_PM_TIMEOUT 100 /* ms */ 91 92 struct fec_devinfo { 93 u32 quirks; 94 }; 95 96 static const struct fec_devinfo fec_imx25_info = { 97 .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR | 98 FEC_QUIRK_HAS_FRREG, 99 }; 100 101 static const struct fec_devinfo fec_imx27_info = { 102 .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG, 103 }; 104 105 static const struct fec_devinfo fec_imx28_info = { 106 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME | 107 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC | 108 FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII | 109 FEC_QUIRK_NO_HARD_RESET, 110 }; 111 112 static const struct fec_devinfo fec_imx6q_info = { 113 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 114 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 115 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 | 116 FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII | 117 FEC_QUIRK_HAS_PMQOS, 118 }; 119 120 static const struct fec_devinfo fec_mvf600_info = { 121 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC, 122 }; 123 124 static const struct fec_devinfo fec_imx6x_info = { 125 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 126 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 127 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 128 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 129 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE | 130 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES, 131 }; 132 133 static const struct fec_devinfo fec_imx6ul_info = { 134 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 135 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 136 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 | 137 FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC | 138 FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII, 139 }; 140 141 static const struct fec_devinfo fec_imx8mq_info = { 142 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 143 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 144 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 145 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 146 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE | 147 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES | 148 FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2, 149 }; 150 151 static const struct fec_devinfo fec_imx8qm_info = { 152 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 153 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 154 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 155 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 156 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE | 157 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES | 158 FEC_QUIRK_DELAYED_CLKS_SUPPORT, 159 }; 160 161 static const struct fec_devinfo fec_s32v234_info = { 162 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 163 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 164 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 165 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE, 166 }; 167 168 static struct platform_device_id fec_devtype[] = { 169 { 170 /* keep it for coldfire */ 171 .name = DRIVER_NAME, 172 .driver_data = 0, 173 }, { 174 .name = "imx25-fec", 175 .driver_data = (kernel_ulong_t)&fec_imx25_info, 176 }, { 177 .name = "imx27-fec", 178 .driver_data = (kernel_ulong_t)&fec_imx27_info, 179 }, { 180 .name = "imx28-fec", 181 .driver_data = (kernel_ulong_t)&fec_imx28_info, 182 }, { 183 .name = "imx6q-fec", 184 .driver_data = (kernel_ulong_t)&fec_imx6q_info, 185 }, { 186 .name = "mvf600-fec", 187 .driver_data = (kernel_ulong_t)&fec_mvf600_info, 188 }, { 189 .name = "imx6sx-fec", 190 .driver_data = (kernel_ulong_t)&fec_imx6x_info, 191 }, { 192 .name = "imx6ul-fec", 193 .driver_data = (kernel_ulong_t)&fec_imx6ul_info, 194 }, { 195 .name = "imx8mq-fec", 196 .driver_data = (kernel_ulong_t)&fec_imx8mq_info, 197 }, { 198 .name = "imx8qm-fec", 199 .driver_data = (kernel_ulong_t)&fec_imx8qm_info, 200 }, { 201 .name = "s32v234-fec", 202 .driver_data = (kernel_ulong_t)&fec_s32v234_info, 203 }, { 204 /* sentinel */ 205 } 206 }; 207 MODULE_DEVICE_TABLE(platform, fec_devtype); 208 209 enum imx_fec_type { 210 IMX25_FEC = 1, /* runs on i.mx25/50/53 */ 211 IMX27_FEC, /* runs on i.mx27/35/51 */ 212 IMX28_FEC, 213 IMX6Q_FEC, 214 MVF600_FEC, 215 IMX6SX_FEC, 216 IMX6UL_FEC, 217 IMX8MQ_FEC, 218 IMX8QM_FEC, 219 S32V234_FEC, 220 }; 221 222 static const struct of_device_id fec_dt_ids[] = { 223 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, 224 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, 225 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, 226 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, 227 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, 228 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], }, 229 { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], }, 230 { .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], }, 231 { .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], }, 232 { .compatible = "fsl,s32v234-fec", .data = &fec_devtype[S32V234_FEC], }, 233 { /* sentinel */ } 234 }; 235 MODULE_DEVICE_TABLE(of, fec_dt_ids); 236 237 static unsigned char macaddr[ETH_ALEN]; 238 module_param_array(macaddr, byte, NULL, 0); 239 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); 240 241 #if defined(CONFIG_M5272) 242 /* 243 * Some hardware gets it MAC address out of local flash memory. 244 * if this is non-zero then assume it is the address to get MAC from. 245 */ 246 #if defined(CONFIG_NETtel) 247 #define FEC_FLASHMAC 0xf0006006 248 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) 249 #define FEC_FLASHMAC 0xf0006000 250 #elif defined(CONFIG_CANCam) 251 #define FEC_FLASHMAC 0xf0020000 252 #elif defined (CONFIG_M5272C3) 253 #define FEC_FLASHMAC (0xffe04000 + 4) 254 #elif defined(CONFIG_MOD5272) 255 #define FEC_FLASHMAC 0xffc0406b 256 #else 257 #define FEC_FLASHMAC 0 258 #endif 259 #endif /* CONFIG_M5272 */ 260 261 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets. 262 * 263 * 2048 byte skbufs are allocated. However, alignment requirements 264 * varies between FEC variants. Worst case is 64, so round down by 64. 265 */ 266 #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64)) 267 #define PKT_MINBUF_SIZE 64 268 269 /* FEC receive acceleration */ 270 #define FEC_RACC_IPDIS (1 << 1) 271 #define FEC_RACC_PRODIS (1 << 2) 272 #define FEC_RACC_SHIFT16 BIT(7) 273 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS) 274 275 /* MIB Control Register */ 276 #define FEC_MIB_CTRLSTAT_DISABLE BIT(31) 277 278 /* 279 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame 280 * size bits. Other FEC hardware does not, so we need to take that into 281 * account when setting it. 282 */ 283 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 284 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 285 defined(CONFIG_ARM64) 286 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) 287 #else 288 #define OPT_FRAME_SIZE 0 289 #endif 290 291 /* FEC MII MMFR bits definition */ 292 #define FEC_MMFR_ST (1 << 30) 293 #define FEC_MMFR_ST_C45 (0) 294 #define FEC_MMFR_OP_READ (2 << 28) 295 #define FEC_MMFR_OP_READ_C45 (3 << 28) 296 #define FEC_MMFR_OP_WRITE (1 << 28) 297 #define FEC_MMFR_OP_ADDR_WRITE (0) 298 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23) 299 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18) 300 #define FEC_MMFR_TA (2 << 16) 301 #define FEC_MMFR_DATA(v) (v & 0xffff) 302 /* FEC ECR bits definition */ 303 #define FEC_ECR_MAGICEN (1 << 2) 304 #define FEC_ECR_SLEEP (1 << 3) 305 306 #define FEC_MII_TIMEOUT 30000 /* us */ 307 308 /* Transmitter timeout */ 309 #define TX_TIMEOUT (2 * HZ) 310 311 #define FEC_PAUSE_FLAG_AUTONEG 0x1 312 #define FEC_PAUSE_FLAG_ENABLE 0x2 313 #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0) 314 #define FEC_WOL_FLAG_ENABLE (0x1 << 1) 315 #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2) 316 317 #define COPYBREAK_DEFAULT 256 318 319 /* Max number of allowed TCP segments for software TSO */ 320 #define FEC_MAX_TSO_SEGS 100 321 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 322 323 #define IS_TSO_HEADER(txq, addr) \ 324 ((addr >= txq->tso_hdrs_dma) && \ 325 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE)) 326 327 static int mii_cnt; 328 329 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, 330 struct bufdesc_prop *bd) 331 { 332 return (bdp >= bd->last) ? bd->base 333 : (struct bufdesc *)(((void *)bdp) + bd->dsize); 334 } 335 336 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, 337 struct bufdesc_prop *bd) 338 { 339 return (bdp <= bd->base) ? bd->last 340 : (struct bufdesc *)(((void *)bdp) - bd->dsize); 341 } 342 343 static int fec_enet_get_bd_index(struct bufdesc *bdp, 344 struct bufdesc_prop *bd) 345 { 346 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2; 347 } 348 349 static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq) 350 { 351 int entries; 352 353 entries = (((const char *)txq->dirty_tx - 354 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1; 355 356 return entries >= 0 ? entries : entries + txq->bd.ring_size; 357 } 358 359 static void swap_buffer(void *bufaddr, int len) 360 { 361 int i; 362 unsigned int *buf = bufaddr; 363 364 for (i = 0; i < len; i += 4, buf++) 365 swab32s(buf); 366 } 367 368 static void swap_buffer2(void *dst_buf, void *src_buf, int len) 369 { 370 int i; 371 unsigned int *src = src_buf; 372 unsigned int *dst = dst_buf; 373 374 for (i = 0; i < len; i += 4, src++, dst++) 375 *dst = swab32p(src); 376 } 377 378 static void fec_dump(struct net_device *ndev) 379 { 380 struct fec_enet_private *fep = netdev_priv(ndev); 381 struct bufdesc *bdp; 382 struct fec_enet_priv_tx_q *txq; 383 int index = 0; 384 385 netdev_info(ndev, "TX ring dump\n"); 386 pr_info("Nr SC addr len SKB\n"); 387 388 txq = fep->tx_queue[0]; 389 bdp = txq->bd.base; 390 391 do { 392 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n", 393 index, 394 bdp == txq->bd.cur ? 'S' : ' ', 395 bdp == txq->dirty_tx ? 'H' : ' ', 396 fec16_to_cpu(bdp->cbd_sc), 397 fec32_to_cpu(bdp->cbd_bufaddr), 398 fec16_to_cpu(bdp->cbd_datlen), 399 txq->tx_skbuff[index]); 400 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 401 index++; 402 } while (bdp != txq->bd.base); 403 } 404 405 static inline bool is_ipv4_pkt(struct sk_buff *skb) 406 { 407 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4; 408 } 409 410 static int 411 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) 412 { 413 /* Only run for packets requiring a checksum. */ 414 if (skb->ip_summed != CHECKSUM_PARTIAL) 415 return 0; 416 417 if (unlikely(skb_cow_head(skb, 0))) 418 return -1; 419 420 if (is_ipv4_pkt(skb)) 421 ip_hdr(skb)->check = 0; 422 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; 423 424 return 0; 425 } 426 427 static int 428 fec_enet_create_page_pool(struct fec_enet_private *fep, 429 struct fec_enet_priv_rx_q *rxq, int size) 430 { 431 struct page_pool_params pp_params = { 432 .order = 0, 433 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV, 434 .pool_size = size, 435 .nid = dev_to_node(&fep->pdev->dev), 436 .dev = &fep->pdev->dev, 437 .dma_dir = DMA_FROM_DEVICE, 438 .offset = FEC_ENET_XDP_HEADROOM, 439 .max_len = FEC_ENET_RX_FRSIZE, 440 }; 441 int err; 442 443 rxq->page_pool = page_pool_create(&pp_params); 444 if (IS_ERR(rxq->page_pool)) { 445 err = PTR_ERR(rxq->page_pool); 446 rxq->page_pool = NULL; 447 return err; 448 } 449 450 err = xdp_rxq_info_reg(&rxq->xdp_rxq, fep->netdev, rxq->id, 0); 451 if (err < 0) 452 goto err_free_pp; 453 454 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL, 455 rxq->page_pool); 456 if (err) 457 goto err_unregister_rxq; 458 459 return 0; 460 461 err_unregister_rxq: 462 xdp_rxq_info_unreg(&rxq->xdp_rxq); 463 err_free_pp: 464 page_pool_destroy(rxq->page_pool); 465 rxq->page_pool = NULL; 466 return err; 467 } 468 469 static struct bufdesc * 470 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq, 471 struct sk_buff *skb, 472 struct net_device *ndev) 473 { 474 struct fec_enet_private *fep = netdev_priv(ndev); 475 struct bufdesc *bdp = txq->bd.cur; 476 struct bufdesc_ex *ebdp; 477 int nr_frags = skb_shinfo(skb)->nr_frags; 478 int frag, frag_len; 479 unsigned short status; 480 unsigned int estatus = 0; 481 skb_frag_t *this_frag; 482 unsigned int index; 483 void *bufaddr; 484 dma_addr_t addr; 485 int i; 486 487 for (frag = 0; frag < nr_frags; frag++) { 488 this_frag = &skb_shinfo(skb)->frags[frag]; 489 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 490 ebdp = (struct bufdesc_ex *)bdp; 491 492 status = fec16_to_cpu(bdp->cbd_sc); 493 status &= ~BD_ENET_TX_STATS; 494 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 495 frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]); 496 497 /* Handle the last BD specially */ 498 if (frag == nr_frags - 1) { 499 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 500 if (fep->bufdesc_ex) { 501 estatus |= BD_ENET_TX_INT; 502 if (unlikely(skb_shinfo(skb)->tx_flags & 503 SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 504 estatus |= BD_ENET_TX_TS; 505 } 506 } 507 508 if (fep->bufdesc_ex) { 509 if (fep->quirks & FEC_QUIRK_HAS_AVB) 510 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 511 if (skb->ip_summed == CHECKSUM_PARTIAL) 512 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 513 514 ebdp->cbd_bdu = 0; 515 ebdp->cbd_esc = cpu_to_fec32(estatus); 516 } 517 518 bufaddr = skb_frag_address(this_frag); 519 520 index = fec_enet_get_bd_index(bdp, &txq->bd); 521 if (((unsigned long) bufaddr) & fep->tx_align || 522 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 523 memcpy(txq->tx_bounce[index], bufaddr, frag_len); 524 bufaddr = txq->tx_bounce[index]; 525 526 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 527 swap_buffer(bufaddr, frag_len); 528 } 529 530 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len, 531 DMA_TO_DEVICE); 532 if (dma_mapping_error(&fep->pdev->dev, addr)) { 533 if (net_ratelimit()) 534 netdev_err(ndev, "Tx DMA memory map failed\n"); 535 goto dma_mapping_error; 536 } 537 538 bdp->cbd_bufaddr = cpu_to_fec32(addr); 539 bdp->cbd_datlen = cpu_to_fec16(frag_len); 540 /* Make sure the updates to rest of the descriptor are 541 * performed before transferring ownership. 542 */ 543 wmb(); 544 bdp->cbd_sc = cpu_to_fec16(status); 545 } 546 547 return bdp; 548 dma_mapping_error: 549 bdp = txq->bd.cur; 550 for (i = 0; i < frag; i++) { 551 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 552 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr), 553 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE); 554 } 555 return ERR_PTR(-ENOMEM); 556 } 557 558 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, 559 struct sk_buff *skb, struct net_device *ndev) 560 { 561 struct fec_enet_private *fep = netdev_priv(ndev); 562 int nr_frags = skb_shinfo(skb)->nr_frags; 563 struct bufdesc *bdp, *last_bdp; 564 void *bufaddr; 565 dma_addr_t addr; 566 unsigned short status; 567 unsigned short buflen; 568 unsigned int estatus = 0; 569 unsigned int index; 570 int entries_free; 571 572 entries_free = fec_enet_get_free_txdesc_num(txq); 573 if (entries_free < MAX_SKB_FRAGS + 1) { 574 dev_kfree_skb_any(skb); 575 if (net_ratelimit()) 576 netdev_err(ndev, "NOT enough BD for SG!\n"); 577 return NETDEV_TX_OK; 578 } 579 580 /* Protocol checksum off-load for TCP and UDP. */ 581 if (fec_enet_clear_csum(skb, ndev)) { 582 dev_kfree_skb_any(skb); 583 return NETDEV_TX_OK; 584 } 585 586 /* Fill in a Tx ring entry */ 587 bdp = txq->bd.cur; 588 last_bdp = bdp; 589 status = fec16_to_cpu(bdp->cbd_sc); 590 status &= ~BD_ENET_TX_STATS; 591 592 /* Set buffer length and buffer pointer */ 593 bufaddr = skb->data; 594 buflen = skb_headlen(skb); 595 596 index = fec_enet_get_bd_index(bdp, &txq->bd); 597 if (((unsigned long) bufaddr) & fep->tx_align || 598 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 599 memcpy(txq->tx_bounce[index], skb->data, buflen); 600 bufaddr = txq->tx_bounce[index]; 601 602 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 603 swap_buffer(bufaddr, buflen); 604 } 605 606 /* Push the data cache so the CPM does not get stale memory data. */ 607 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE); 608 if (dma_mapping_error(&fep->pdev->dev, addr)) { 609 dev_kfree_skb_any(skb); 610 if (net_ratelimit()) 611 netdev_err(ndev, "Tx DMA memory map failed\n"); 612 return NETDEV_TX_OK; 613 } 614 615 if (nr_frags) { 616 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev); 617 if (IS_ERR(last_bdp)) { 618 dma_unmap_single(&fep->pdev->dev, addr, 619 buflen, DMA_TO_DEVICE); 620 dev_kfree_skb_any(skb); 621 return NETDEV_TX_OK; 622 } 623 } else { 624 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 625 if (fep->bufdesc_ex) { 626 estatus = BD_ENET_TX_INT; 627 if (unlikely(skb_shinfo(skb)->tx_flags & 628 SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 629 estatus |= BD_ENET_TX_TS; 630 } 631 } 632 bdp->cbd_bufaddr = cpu_to_fec32(addr); 633 bdp->cbd_datlen = cpu_to_fec16(buflen); 634 635 if (fep->bufdesc_ex) { 636 637 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 638 639 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 640 fep->hwts_tx_en)) 641 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 642 643 if (fep->quirks & FEC_QUIRK_HAS_AVB) 644 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 645 646 if (skb->ip_summed == CHECKSUM_PARTIAL) 647 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 648 649 ebdp->cbd_bdu = 0; 650 ebdp->cbd_esc = cpu_to_fec32(estatus); 651 } 652 653 index = fec_enet_get_bd_index(last_bdp, &txq->bd); 654 /* Save skb pointer */ 655 txq->tx_skbuff[index] = skb; 656 657 /* Make sure the updates to rest of the descriptor are performed before 658 * transferring ownership. 659 */ 660 wmb(); 661 662 /* Send it on its way. Tell FEC it's ready, interrupt when done, 663 * it's the last BD of the frame, and to put the CRC on the end. 664 */ 665 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); 666 bdp->cbd_sc = cpu_to_fec16(status); 667 668 /* If this was the last BD in the ring, start at the beginning again. */ 669 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd); 670 671 skb_tx_timestamp(skb); 672 673 /* Make sure the update to bdp and tx_skbuff are performed before 674 * txq->bd.cur. 675 */ 676 wmb(); 677 txq->bd.cur = bdp; 678 679 /* Trigger transmission start */ 680 writel(0, txq->bd.reg_desc_active); 681 682 return 0; 683 } 684 685 static int 686 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb, 687 struct net_device *ndev, 688 struct bufdesc *bdp, int index, char *data, 689 int size, bool last_tcp, bool is_last) 690 { 691 struct fec_enet_private *fep = netdev_priv(ndev); 692 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 693 unsigned short status; 694 unsigned int estatus = 0; 695 dma_addr_t addr; 696 697 status = fec16_to_cpu(bdp->cbd_sc); 698 status &= ~BD_ENET_TX_STATS; 699 700 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 701 702 if (((unsigned long) data) & fep->tx_align || 703 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 704 memcpy(txq->tx_bounce[index], data, size); 705 data = txq->tx_bounce[index]; 706 707 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 708 swap_buffer(data, size); 709 } 710 711 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE); 712 if (dma_mapping_error(&fep->pdev->dev, addr)) { 713 dev_kfree_skb_any(skb); 714 if (net_ratelimit()) 715 netdev_err(ndev, "Tx DMA memory map failed\n"); 716 return NETDEV_TX_OK; 717 } 718 719 bdp->cbd_datlen = cpu_to_fec16(size); 720 bdp->cbd_bufaddr = cpu_to_fec32(addr); 721 722 if (fep->bufdesc_ex) { 723 if (fep->quirks & FEC_QUIRK_HAS_AVB) 724 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 725 if (skb->ip_summed == CHECKSUM_PARTIAL) 726 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 727 ebdp->cbd_bdu = 0; 728 ebdp->cbd_esc = cpu_to_fec32(estatus); 729 } 730 731 /* Handle the last BD specially */ 732 if (last_tcp) 733 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC); 734 if (is_last) { 735 status |= BD_ENET_TX_INTR; 736 if (fep->bufdesc_ex) 737 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT); 738 } 739 740 bdp->cbd_sc = cpu_to_fec16(status); 741 742 return 0; 743 } 744 745 static int 746 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq, 747 struct sk_buff *skb, struct net_device *ndev, 748 struct bufdesc *bdp, int index) 749 { 750 struct fec_enet_private *fep = netdev_priv(ndev); 751 int hdr_len = skb_tcp_all_headers(skb); 752 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 753 void *bufaddr; 754 unsigned long dmabuf; 755 unsigned short status; 756 unsigned int estatus = 0; 757 758 status = fec16_to_cpu(bdp->cbd_sc); 759 status &= ~BD_ENET_TX_STATS; 760 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 761 762 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 763 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE; 764 if (((unsigned long)bufaddr) & fep->tx_align || 765 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 766 memcpy(txq->tx_bounce[index], skb->data, hdr_len); 767 bufaddr = txq->tx_bounce[index]; 768 769 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 770 swap_buffer(bufaddr, hdr_len); 771 772 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr, 773 hdr_len, DMA_TO_DEVICE); 774 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) { 775 dev_kfree_skb_any(skb); 776 if (net_ratelimit()) 777 netdev_err(ndev, "Tx DMA memory map failed\n"); 778 return NETDEV_TX_OK; 779 } 780 } 781 782 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf); 783 bdp->cbd_datlen = cpu_to_fec16(hdr_len); 784 785 if (fep->bufdesc_ex) { 786 if (fep->quirks & FEC_QUIRK_HAS_AVB) 787 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 788 if (skb->ip_summed == CHECKSUM_PARTIAL) 789 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 790 ebdp->cbd_bdu = 0; 791 ebdp->cbd_esc = cpu_to_fec32(estatus); 792 } 793 794 bdp->cbd_sc = cpu_to_fec16(status); 795 796 return 0; 797 } 798 799 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq, 800 struct sk_buff *skb, 801 struct net_device *ndev) 802 { 803 struct fec_enet_private *fep = netdev_priv(ndev); 804 int hdr_len, total_len, data_left; 805 struct bufdesc *bdp = txq->bd.cur; 806 struct tso_t tso; 807 unsigned int index = 0; 808 int ret; 809 810 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) { 811 dev_kfree_skb_any(skb); 812 if (net_ratelimit()) 813 netdev_err(ndev, "NOT enough BD for TSO!\n"); 814 return NETDEV_TX_OK; 815 } 816 817 /* Protocol checksum off-load for TCP and UDP. */ 818 if (fec_enet_clear_csum(skb, ndev)) { 819 dev_kfree_skb_any(skb); 820 return NETDEV_TX_OK; 821 } 822 823 /* Initialize the TSO handler, and prepare the first payload */ 824 hdr_len = tso_start(skb, &tso); 825 826 total_len = skb->len - hdr_len; 827 while (total_len > 0) { 828 char *hdr; 829 830 index = fec_enet_get_bd_index(bdp, &txq->bd); 831 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 832 total_len -= data_left; 833 834 /* prepare packet headers: MAC + IP + TCP */ 835 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 836 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 837 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index); 838 if (ret) 839 goto err_release; 840 841 while (data_left > 0) { 842 int size; 843 844 size = min_t(int, tso.size, data_left); 845 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 846 index = fec_enet_get_bd_index(bdp, &txq->bd); 847 ret = fec_enet_txq_put_data_tso(txq, skb, ndev, 848 bdp, index, 849 tso.data, size, 850 size == data_left, 851 total_len == 0); 852 if (ret) 853 goto err_release; 854 855 data_left -= size; 856 tso_build_data(skb, &tso, size); 857 } 858 859 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 860 } 861 862 /* Save skb pointer */ 863 txq->tx_skbuff[index] = skb; 864 865 skb_tx_timestamp(skb); 866 txq->bd.cur = bdp; 867 868 /* Trigger transmission start */ 869 if (!(fep->quirks & FEC_QUIRK_ERR007885) || 870 !readl(txq->bd.reg_desc_active) || 871 !readl(txq->bd.reg_desc_active) || 872 !readl(txq->bd.reg_desc_active) || 873 !readl(txq->bd.reg_desc_active)) 874 writel(0, txq->bd.reg_desc_active); 875 876 return 0; 877 878 err_release: 879 /* TODO: Release all used data descriptors for TSO */ 880 return ret; 881 } 882 883 static netdev_tx_t 884 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) 885 { 886 struct fec_enet_private *fep = netdev_priv(ndev); 887 int entries_free; 888 unsigned short queue; 889 struct fec_enet_priv_tx_q *txq; 890 struct netdev_queue *nq; 891 int ret; 892 893 queue = skb_get_queue_mapping(skb); 894 txq = fep->tx_queue[queue]; 895 nq = netdev_get_tx_queue(ndev, queue); 896 897 if (skb_is_gso(skb)) 898 ret = fec_enet_txq_submit_tso(txq, skb, ndev); 899 else 900 ret = fec_enet_txq_submit_skb(txq, skb, ndev); 901 if (ret) 902 return ret; 903 904 entries_free = fec_enet_get_free_txdesc_num(txq); 905 if (entries_free <= txq->tx_stop_threshold) 906 netif_tx_stop_queue(nq); 907 908 return NETDEV_TX_OK; 909 } 910 911 /* Init RX & TX buffer descriptors 912 */ 913 static void fec_enet_bd_init(struct net_device *dev) 914 { 915 struct fec_enet_private *fep = netdev_priv(dev); 916 struct fec_enet_priv_tx_q *txq; 917 struct fec_enet_priv_rx_q *rxq; 918 struct bufdesc *bdp; 919 unsigned int i; 920 unsigned int q; 921 922 for (q = 0; q < fep->num_rx_queues; q++) { 923 /* Initialize the receive buffer descriptors. */ 924 rxq = fep->rx_queue[q]; 925 bdp = rxq->bd.base; 926 927 for (i = 0; i < rxq->bd.ring_size; i++) { 928 929 /* Initialize the BD for every fragment in the page. */ 930 if (bdp->cbd_bufaddr) 931 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 932 else 933 bdp->cbd_sc = cpu_to_fec16(0); 934 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 935 } 936 937 /* Set the last buffer to wrap */ 938 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 939 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 940 941 rxq->bd.cur = rxq->bd.base; 942 } 943 944 for (q = 0; q < fep->num_tx_queues; q++) { 945 /* ...and the same for transmit */ 946 txq = fep->tx_queue[q]; 947 bdp = txq->bd.base; 948 txq->bd.cur = bdp; 949 950 for (i = 0; i < txq->bd.ring_size; i++) { 951 /* Initialize the BD for every fragment in the page. */ 952 bdp->cbd_sc = cpu_to_fec16(0); 953 if (bdp->cbd_bufaddr && 954 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 955 dma_unmap_single(&fep->pdev->dev, 956 fec32_to_cpu(bdp->cbd_bufaddr), 957 fec16_to_cpu(bdp->cbd_datlen), 958 DMA_TO_DEVICE); 959 if (txq->tx_skbuff[i]) { 960 dev_kfree_skb_any(txq->tx_skbuff[i]); 961 txq->tx_skbuff[i] = NULL; 962 } 963 bdp->cbd_bufaddr = cpu_to_fec32(0); 964 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 965 } 966 967 /* Set the last buffer to wrap */ 968 bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 969 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 970 txq->dirty_tx = bdp; 971 } 972 } 973 974 static void fec_enet_active_rxring(struct net_device *ndev) 975 { 976 struct fec_enet_private *fep = netdev_priv(ndev); 977 int i; 978 979 for (i = 0; i < fep->num_rx_queues; i++) 980 writel(0, fep->rx_queue[i]->bd.reg_desc_active); 981 } 982 983 static void fec_enet_enable_ring(struct net_device *ndev) 984 { 985 struct fec_enet_private *fep = netdev_priv(ndev); 986 struct fec_enet_priv_tx_q *txq; 987 struct fec_enet_priv_rx_q *rxq; 988 int i; 989 990 for (i = 0; i < fep->num_rx_queues; i++) { 991 rxq = fep->rx_queue[i]; 992 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i)); 993 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i)); 994 995 /* enable DMA1/2 */ 996 if (i) 997 writel(RCMR_MATCHEN | RCMR_CMP(i), 998 fep->hwp + FEC_RCMR(i)); 999 } 1000 1001 for (i = 0; i < fep->num_tx_queues; i++) { 1002 txq = fep->tx_queue[i]; 1003 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i)); 1004 1005 /* enable DMA1/2 */ 1006 if (i) 1007 writel(DMA_CLASS_EN | IDLE_SLOPE(i), 1008 fep->hwp + FEC_DMA_CFG(i)); 1009 } 1010 } 1011 1012 static void fec_enet_reset_skb(struct net_device *ndev) 1013 { 1014 struct fec_enet_private *fep = netdev_priv(ndev); 1015 struct fec_enet_priv_tx_q *txq; 1016 int i, j; 1017 1018 for (i = 0; i < fep->num_tx_queues; i++) { 1019 txq = fep->tx_queue[i]; 1020 1021 for (j = 0; j < txq->bd.ring_size; j++) { 1022 if (txq->tx_skbuff[j]) { 1023 dev_kfree_skb_any(txq->tx_skbuff[j]); 1024 txq->tx_skbuff[j] = NULL; 1025 } 1026 } 1027 } 1028 } 1029 1030 /* 1031 * This function is called to start or restart the FEC during a link 1032 * change, transmit timeout, or to reconfigure the FEC. The network 1033 * packet processing for this device must be stopped before this call. 1034 */ 1035 static void 1036 fec_restart(struct net_device *ndev) 1037 { 1038 struct fec_enet_private *fep = netdev_priv(ndev); 1039 u32 temp_mac[2]; 1040 u32 rcntl = OPT_FRAME_SIZE | 0x04; 1041 u32 ecntl = 0x2; /* ETHEREN */ 1042 1043 /* Whack a reset. We should wait for this. 1044 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1045 * instead of reset MAC itself. 1046 */ 1047 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES || 1048 ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) { 1049 writel(0, fep->hwp + FEC_ECNTRL); 1050 } else { 1051 writel(1, fep->hwp + FEC_ECNTRL); 1052 udelay(10); 1053 } 1054 1055 /* 1056 * enet-mac reset will reset mac address registers too, 1057 * so need to reconfigure it. 1058 */ 1059 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); 1060 writel((__force u32)cpu_to_be32(temp_mac[0]), 1061 fep->hwp + FEC_ADDR_LOW); 1062 writel((__force u32)cpu_to_be32(temp_mac[1]), 1063 fep->hwp + FEC_ADDR_HIGH); 1064 1065 /* Clear any outstanding interrupt, except MDIO. */ 1066 writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT); 1067 1068 fec_enet_bd_init(ndev); 1069 1070 fec_enet_enable_ring(ndev); 1071 1072 /* Reset tx SKB buffers. */ 1073 fec_enet_reset_skb(ndev); 1074 1075 /* Enable MII mode */ 1076 if (fep->full_duplex == DUPLEX_FULL) { 1077 /* FD enable */ 1078 writel(0x04, fep->hwp + FEC_X_CNTRL); 1079 } else { 1080 /* No Rcv on Xmit */ 1081 rcntl |= 0x02; 1082 writel(0x0, fep->hwp + FEC_X_CNTRL); 1083 } 1084 1085 /* Set MII speed */ 1086 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1087 1088 #if !defined(CONFIG_M5272) 1089 if (fep->quirks & FEC_QUIRK_HAS_RACC) { 1090 u32 val = readl(fep->hwp + FEC_RACC); 1091 1092 /* align IP header */ 1093 val |= FEC_RACC_SHIFT16; 1094 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) 1095 /* set RX checksum */ 1096 val |= FEC_RACC_OPTIONS; 1097 else 1098 val &= ~FEC_RACC_OPTIONS; 1099 writel(val, fep->hwp + FEC_RACC); 1100 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL); 1101 } 1102 #endif 1103 1104 /* 1105 * The phy interface and speed need to get configured 1106 * differently on enet-mac. 1107 */ 1108 if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1109 /* Enable flow control and length check */ 1110 rcntl |= 0x40000000 | 0x00000020; 1111 1112 /* RGMII, RMII or MII */ 1113 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII || 1114 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID || 1115 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID || 1116 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) 1117 rcntl |= (1 << 6); 1118 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1119 rcntl |= (1 << 8); 1120 else 1121 rcntl &= ~(1 << 8); 1122 1123 /* 1G, 100M or 10M */ 1124 if (ndev->phydev) { 1125 if (ndev->phydev->speed == SPEED_1000) 1126 ecntl |= (1 << 5); 1127 else if (ndev->phydev->speed == SPEED_100) 1128 rcntl &= ~(1 << 9); 1129 else 1130 rcntl |= (1 << 9); 1131 } 1132 } else { 1133 #ifdef FEC_MIIGSK_ENR 1134 if (fep->quirks & FEC_QUIRK_USE_GASKET) { 1135 u32 cfgr; 1136 /* disable the gasket and wait */ 1137 writel(0, fep->hwp + FEC_MIIGSK_ENR); 1138 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) 1139 udelay(1); 1140 1141 /* 1142 * configure the gasket: 1143 * RMII, 50 MHz, no loopback, no echo 1144 * MII, 25 MHz, no loopback, no echo 1145 */ 1146 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1147 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; 1148 if (ndev->phydev && ndev->phydev->speed == SPEED_10) 1149 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; 1150 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); 1151 1152 /* re-enable the gasket */ 1153 writel(2, fep->hwp + FEC_MIIGSK_ENR); 1154 } 1155 #endif 1156 } 1157 1158 #if !defined(CONFIG_M5272) 1159 /* enable pause frame*/ 1160 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || 1161 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && 1162 ndev->phydev && ndev->phydev->pause)) { 1163 rcntl |= FEC_ENET_FCE; 1164 1165 /* set FIFO threshold parameter to reduce overrun */ 1166 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); 1167 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); 1168 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); 1169 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); 1170 1171 /* OPD */ 1172 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); 1173 } else { 1174 rcntl &= ~FEC_ENET_FCE; 1175 } 1176 #endif /* !defined(CONFIG_M5272) */ 1177 1178 writel(rcntl, fep->hwp + FEC_R_CNTRL); 1179 1180 /* Setup multicast filter. */ 1181 set_multicast_list(ndev); 1182 #ifndef CONFIG_M5272 1183 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); 1184 writel(0, fep->hwp + FEC_HASH_TABLE_LOW); 1185 #endif 1186 1187 if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1188 /* enable ENET endian swap */ 1189 ecntl |= (1 << 8); 1190 /* enable ENET store and forward mode */ 1191 writel(1 << 8, fep->hwp + FEC_X_WMRK); 1192 } 1193 1194 if (fep->bufdesc_ex) 1195 ecntl |= (1 << 4); 1196 1197 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT && 1198 fep->rgmii_txc_dly) 1199 ecntl |= FEC_ENET_TXC_DLY; 1200 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT && 1201 fep->rgmii_rxc_dly) 1202 ecntl |= FEC_ENET_RXC_DLY; 1203 1204 #ifndef CONFIG_M5272 1205 /* Enable the MIB statistic event counters */ 1206 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT); 1207 #endif 1208 1209 /* And last, enable the transmit and receive processing */ 1210 writel(ecntl, fep->hwp + FEC_ECNTRL); 1211 fec_enet_active_rxring(ndev); 1212 1213 if (fep->bufdesc_ex) 1214 fec_ptp_start_cyclecounter(ndev); 1215 1216 /* Enable interrupts we wish to service */ 1217 if (fep->link) 1218 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1219 else 1220 writel(0, fep->hwp + FEC_IMASK); 1221 1222 /* Init the interrupt coalescing */ 1223 fec_enet_itr_coal_set(ndev); 1224 } 1225 1226 static int fec_enet_ipc_handle_init(struct fec_enet_private *fep) 1227 { 1228 if (!(of_machine_is_compatible("fsl,imx8qm") || 1229 of_machine_is_compatible("fsl,imx8qxp") || 1230 of_machine_is_compatible("fsl,imx8dxl"))) 1231 return 0; 1232 1233 return imx_scu_get_handle(&fep->ipc_handle); 1234 } 1235 1236 static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled) 1237 { 1238 struct device_node *np = fep->pdev->dev.of_node; 1239 u32 rsrc_id, val; 1240 int idx; 1241 1242 if (!np || !fep->ipc_handle) 1243 return; 1244 1245 idx = of_alias_get_id(np, "ethernet"); 1246 if (idx < 0) 1247 idx = 0; 1248 rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0; 1249 1250 val = enabled ? 1 : 0; 1251 imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val); 1252 } 1253 1254 static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled) 1255 { 1256 struct fec_platform_data *pdata = fep->pdev->dev.platform_data; 1257 struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr; 1258 1259 if (stop_gpr->gpr) { 1260 if (enabled) 1261 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg, 1262 BIT(stop_gpr->bit), 1263 BIT(stop_gpr->bit)); 1264 else 1265 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg, 1266 BIT(stop_gpr->bit), 0); 1267 } else if (pdata && pdata->sleep_mode_enable) { 1268 pdata->sleep_mode_enable(enabled); 1269 } else { 1270 fec_enet_ipg_stop_set(fep, enabled); 1271 } 1272 } 1273 1274 static void fec_irqs_disable(struct net_device *ndev) 1275 { 1276 struct fec_enet_private *fep = netdev_priv(ndev); 1277 1278 writel(0, fep->hwp + FEC_IMASK); 1279 } 1280 1281 static void fec_irqs_disable_except_wakeup(struct net_device *ndev) 1282 { 1283 struct fec_enet_private *fep = netdev_priv(ndev); 1284 1285 writel(0, fep->hwp + FEC_IMASK); 1286 writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK); 1287 } 1288 1289 static void 1290 fec_stop(struct net_device *ndev) 1291 { 1292 struct fec_enet_private *fep = netdev_priv(ndev); 1293 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8); 1294 u32 val; 1295 1296 /* We cannot expect a graceful transmit stop without link !!! */ 1297 if (fep->link) { 1298 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ 1299 udelay(10); 1300 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) 1301 netdev_err(ndev, "Graceful transmit stop did not complete!\n"); 1302 } 1303 1304 /* Whack a reset. We should wait for this. 1305 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1306 * instead of reset MAC itself. 1307 */ 1308 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 1309 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) { 1310 writel(0, fep->hwp + FEC_ECNTRL); 1311 } else { 1312 writel(1, fep->hwp + FEC_ECNTRL); 1313 udelay(10); 1314 } 1315 } else { 1316 val = readl(fep->hwp + FEC_ECNTRL); 1317 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 1318 writel(val, fep->hwp + FEC_ECNTRL); 1319 } 1320 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1321 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1322 1323 /* We have to keep ENET enabled to have MII interrupt stay working */ 1324 if (fep->quirks & FEC_QUIRK_ENET_MAC && 1325 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 1326 writel(2, fep->hwp + FEC_ECNTRL); 1327 writel(rmii_mode, fep->hwp + FEC_R_CNTRL); 1328 } 1329 } 1330 1331 1332 static void 1333 fec_timeout(struct net_device *ndev, unsigned int txqueue) 1334 { 1335 struct fec_enet_private *fep = netdev_priv(ndev); 1336 1337 fec_dump(ndev); 1338 1339 ndev->stats.tx_errors++; 1340 1341 schedule_work(&fep->tx_timeout_work); 1342 } 1343 1344 static void fec_enet_timeout_work(struct work_struct *work) 1345 { 1346 struct fec_enet_private *fep = 1347 container_of(work, struct fec_enet_private, tx_timeout_work); 1348 struct net_device *ndev = fep->netdev; 1349 1350 rtnl_lock(); 1351 if (netif_device_present(ndev) || netif_running(ndev)) { 1352 napi_disable(&fep->napi); 1353 netif_tx_lock_bh(ndev); 1354 fec_restart(ndev); 1355 netif_tx_wake_all_queues(ndev); 1356 netif_tx_unlock_bh(ndev); 1357 napi_enable(&fep->napi); 1358 } 1359 rtnl_unlock(); 1360 } 1361 1362 static void 1363 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts, 1364 struct skb_shared_hwtstamps *hwtstamps) 1365 { 1366 unsigned long flags; 1367 u64 ns; 1368 1369 spin_lock_irqsave(&fep->tmreg_lock, flags); 1370 ns = timecounter_cyc2time(&fep->tc, ts); 1371 spin_unlock_irqrestore(&fep->tmreg_lock, flags); 1372 1373 memset(hwtstamps, 0, sizeof(*hwtstamps)); 1374 hwtstamps->hwtstamp = ns_to_ktime(ns); 1375 } 1376 1377 static void 1378 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id) 1379 { 1380 struct fec_enet_private *fep; 1381 struct bufdesc *bdp; 1382 unsigned short status; 1383 struct sk_buff *skb; 1384 struct fec_enet_priv_tx_q *txq; 1385 struct netdev_queue *nq; 1386 int index = 0; 1387 int entries_free; 1388 1389 fep = netdev_priv(ndev); 1390 1391 txq = fep->tx_queue[queue_id]; 1392 /* get next bdp of dirty_tx */ 1393 nq = netdev_get_tx_queue(ndev, queue_id); 1394 bdp = txq->dirty_tx; 1395 1396 /* get next bdp of dirty_tx */ 1397 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1398 1399 while (bdp != READ_ONCE(txq->bd.cur)) { 1400 /* Order the load of bd.cur and cbd_sc */ 1401 rmb(); 1402 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc)); 1403 if (status & BD_ENET_TX_READY) 1404 break; 1405 1406 index = fec_enet_get_bd_index(bdp, &txq->bd); 1407 1408 skb = txq->tx_skbuff[index]; 1409 txq->tx_skbuff[index] = NULL; 1410 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 1411 dma_unmap_single(&fep->pdev->dev, 1412 fec32_to_cpu(bdp->cbd_bufaddr), 1413 fec16_to_cpu(bdp->cbd_datlen), 1414 DMA_TO_DEVICE); 1415 bdp->cbd_bufaddr = cpu_to_fec32(0); 1416 if (!skb) 1417 goto skb_done; 1418 1419 /* Check for errors. */ 1420 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | 1421 BD_ENET_TX_RL | BD_ENET_TX_UN | 1422 BD_ENET_TX_CSL)) { 1423 ndev->stats.tx_errors++; 1424 if (status & BD_ENET_TX_HB) /* No heartbeat */ 1425 ndev->stats.tx_heartbeat_errors++; 1426 if (status & BD_ENET_TX_LC) /* Late collision */ 1427 ndev->stats.tx_window_errors++; 1428 if (status & BD_ENET_TX_RL) /* Retrans limit */ 1429 ndev->stats.tx_aborted_errors++; 1430 if (status & BD_ENET_TX_UN) /* Underrun */ 1431 ndev->stats.tx_fifo_errors++; 1432 if (status & BD_ENET_TX_CSL) /* Carrier lost */ 1433 ndev->stats.tx_carrier_errors++; 1434 } else { 1435 ndev->stats.tx_packets++; 1436 ndev->stats.tx_bytes += skb->len; 1437 } 1438 1439 /* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who 1440 * are to time stamp the packet, so we still need to check time 1441 * stamping enabled flag. 1442 */ 1443 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS && 1444 fep->hwts_tx_en) && 1445 fep->bufdesc_ex) { 1446 struct skb_shared_hwtstamps shhwtstamps; 1447 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1448 1449 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps); 1450 skb_tstamp_tx(skb, &shhwtstamps); 1451 } 1452 1453 /* Deferred means some collisions occurred during transmit, 1454 * but we eventually sent the packet OK. 1455 */ 1456 if (status & BD_ENET_TX_DEF) 1457 ndev->stats.collisions++; 1458 1459 /* Free the sk buffer associated with this last transmit */ 1460 dev_kfree_skb_any(skb); 1461 skb_done: 1462 /* Make sure the update to bdp and tx_skbuff are performed 1463 * before dirty_tx 1464 */ 1465 wmb(); 1466 txq->dirty_tx = bdp; 1467 1468 /* Update pointer to next buffer descriptor to be transmitted */ 1469 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1470 1471 /* Since we have freed up a buffer, the ring is no longer full 1472 */ 1473 if (netif_tx_queue_stopped(nq)) { 1474 entries_free = fec_enet_get_free_txdesc_num(txq); 1475 if (entries_free >= txq->tx_wake_threshold) 1476 netif_tx_wake_queue(nq); 1477 } 1478 } 1479 1480 /* ERR006358: Keep the transmitter going */ 1481 if (bdp != txq->bd.cur && 1482 readl(txq->bd.reg_desc_active) == 0) 1483 writel(0, txq->bd.reg_desc_active); 1484 } 1485 1486 static void fec_enet_tx(struct net_device *ndev) 1487 { 1488 struct fec_enet_private *fep = netdev_priv(ndev); 1489 int i; 1490 1491 /* Make sure that AVB queues are processed first. */ 1492 for (i = fep->num_tx_queues - 1; i >= 0; i--) 1493 fec_enet_tx_queue(ndev, i); 1494 } 1495 1496 static int __maybe_unused 1497 fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb) 1498 { 1499 struct fec_enet_private *fep = netdev_priv(ndev); 1500 int off; 1501 1502 off = ((unsigned long)skb->data) & fep->rx_align; 1503 if (off) 1504 skb_reserve(skb, fep->rx_align + 1 - off); 1505 1506 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE)); 1507 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) { 1508 if (net_ratelimit()) 1509 netdev_err(ndev, "Rx DMA memory map failed\n"); 1510 return -ENOMEM; 1511 } 1512 1513 return 0; 1514 } 1515 1516 static bool __maybe_unused 1517 fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb, 1518 struct bufdesc *bdp, u32 length, bool swap) 1519 { 1520 struct fec_enet_private *fep = netdev_priv(ndev); 1521 struct sk_buff *new_skb; 1522 1523 if (length > fep->rx_copybreak) 1524 return false; 1525 1526 new_skb = netdev_alloc_skb(ndev, length); 1527 if (!new_skb) 1528 return false; 1529 1530 dma_sync_single_for_cpu(&fep->pdev->dev, 1531 fec32_to_cpu(bdp->cbd_bufaddr), 1532 FEC_ENET_RX_FRSIZE - fep->rx_align, 1533 DMA_FROM_DEVICE); 1534 if (!swap) 1535 memcpy(new_skb->data, (*skb)->data, length); 1536 else 1537 swap_buffer2(new_skb->data, (*skb)->data, length); 1538 *skb = new_skb; 1539 1540 return true; 1541 } 1542 1543 static void fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq, 1544 struct bufdesc *bdp, int index) 1545 { 1546 struct page *new_page; 1547 dma_addr_t phys_addr; 1548 1549 new_page = page_pool_dev_alloc_pages(rxq->page_pool); 1550 WARN_ON(!new_page); 1551 rxq->rx_skb_info[index].page = new_page; 1552 1553 rxq->rx_skb_info[index].offset = FEC_ENET_XDP_HEADROOM; 1554 phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM; 1555 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr); 1556 } 1557 1558 /* During a receive, the bd_rx.cur points to the current incoming buffer. 1559 * When we update through the ring, if the next incoming buffer has 1560 * not been given to the system, we just set the empty indicator, 1561 * effectively tossing the packet. 1562 */ 1563 static int 1564 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) 1565 { 1566 struct fec_enet_private *fep = netdev_priv(ndev); 1567 struct fec_enet_priv_rx_q *rxq; 1568 struct bufdesc *bdp; 1569 unsigned short status; 1570 struct sk_buff *skb; 1571 ushort pkt_len; 1572 __u8 *data; 1573 int pkt_received = 0; 1574 struct bufdesc_ex *ebdp = NULL; 1575 bool vlan_packet_rcvd = false; 1576 u16 vlan_tag; 1577 int index = 0; 1578 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME; 1579 struct page *page; 1580 1581 #ifdef CONFIG_M532x 1582 flush_cache_all(); 1583 #endif 1584 rxq = fep->rx_queue[queue_id]; 1585 1586 /* First, grab all of the stats for the incoming packet. 1587 * These get messed up if we get called due to a busy condition. 1588 */ 1589 bdp = rxq->bd.cur; 1590 1591 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) { 1592 1593 if (pkt_received >= budget) 1594 break; 1595 pkt_received++; 1596 1597 writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT); 1598 1599 /* Check for errors. */ 1600 status ^= BD_ENET_RX_LAST; 1601 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | 1602 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST | 1603 BD_ENET_RX_CL)) { 1604 ndev->stats.rx_errors++; 1605 if (status & BD_ENET_RX_OV) { 1606 /* FIFO overrun */ 1607 ndev->stats.rx_fifo_errors++; 1608 goto rx_processing_done; 1609 } 1610 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH 1611 | BD_ENET_RX_LAST)) { 1612 /* Frame too long or too short. */ 1613 ndev->stats.rx_length_errors++; 1614 if (status & BD_ENET_RX_LAST) 1615 netdev_err(ndev, "rcv is not +last\n"); 1616 } 1617 if (status & BD_ENET_RX_CR) /* CRC Error */ 1618 ndev->stats.rx_crc_errors++; 1619 /* Report late collisions as a frame error. */ 1620 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL)) 1621 ndev->stats.rx_frame_errors++; 1622 goto rx_processing_done; 1623 } 1624 1625 /* Process the incoming frame. */ 1626 ndev->stats.rx_packets++; 1627 pkt_len = fec16_to_cpu(bdp->cbd_datlen); 1628 ndev->stats.rx_bytes += pkt_len; 1629 1630 index = fec_enet_get_bd_index(bdp, &rxq->bd); 1631 page = rxq->rx_skb_info[index].page; 1632 dma_sync_single_for_cpu(&fep->pdev->dev, 1633 fec32_to_cpu(bdp->cbd_bufaddr), 1634 pkt_len, 1635 DMA_FROM_DEVICE); 1636 prefetch(page_address(page)); 1637 fec_enet_update_cbd(rxq, bdp, index); 1638 1639 /* The packet length includes FCS, but we don't want to 1640 * include that when passing upstream as it messes up 1641 * bridging applications. 1642 */ 1643 skb = build_skb(page_address(page), PAGE_SIZE); 1644 skb_reserve(skb, FEC_ENET_XDP_HEADROOM); 1645 skb_put(skb, pkt_len - 4); 1646 skb_mark_for_recycle(skb); 1647 data = skb->data; 1648 1649 if (need_swap) 1650 swap_buffer(data, pkt_len); 1651 1652 #if !defined(CONFIG_M5272) 1653 if (fep->quirks & FEC_QUIRK_HAS_RACC) 1654 data = skb_pull_inline(skb, 2); 1655 #endif 1656 1657 /* Extract the enhanced buffer descriptor */ 1658 ebdp = NULL; 1659 if (fep->bufdesc_ex) 1660 ebdp = (struct bufdesc_ex *)bdp; 1661 1662 /* If this is a VLAN packet remove the VLAN Tag */ 1663 vlan_packet_rcvd = false; 1664 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) && 1665 fep->bufdesc_ex && 1666 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) { 1667 /* Push and remove the vlan tag */ 1668 struct vlan_hdr *vlan_header = 1669 (struct vlan_hdr *) (data + ETH_HLEN); 1670 vlan_tag = ntohs(vlan_header->h_vlan_TCI); 1671 1672 vlan_packet_rcvd = true; 1673 1674 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2); 1675 skb_pull(skb, VLAN_HLEN); 1676 } 1677 1678 skb->protocol = eth_type_trans(skb, ndev); 1679 1680 /* Get receive timestamp from the skb */ 1681 if (fep->hwts_rx_en && fep->bufdesc_ex) 1682 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), 1683 skb_hwtstamps(skb)); 1684 1685 if (fep->bufdesc_ex && 1686 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { 1687 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) { 1688 /* don't check it */ 1689 skb->ip_summed = CHECKSUM_UNNECESSARY; 1690 } else { 1691 skb_checksum_none_assert(skb); 1692 } 1693 } 1694 1695 /* Handle received VLAN packets */ 1696 if (vlan_packet_rcvd) 1697 __vlan_hwaccel_put_tag(skb, 1698 htons(ETH_P_8021Q), 1699 vlan_tag); 1700 1701 skb_record_rx_queue(skb, queue_id); 1702 napi_gro_receive(&fep->napi, skb); 1703 1704 rx_processing_done: 1705 /* Clear the status flags for this buffer */ 1706 status &= ~BD_ENET_RX_STATS; 1707 1708 /* Mark the buffer empty */ 1709 status |= BD_ENET_RX_EMPTY; 1710 1711 if (fep->bufdesc_ex) { 1712 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1713 1714 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 1715 ebdp->cbd_prot = 0; 1716 ebdp->cbd_bdu = 0; 1717 } 1718 /* Make sure the updates to rest of the descriptor are 1719 * performed before transferring ownership. 1720 */ 1721 wmb(); 1722 bdp->cbd_sc = cpu_to_fec16(status); 1723 1724 /* Update BD pointer to next entry */ 1725 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 1726 1727 /* Doing this here will keep the FEC running while we process 1728 * incoming frames. On a heavily loaded network, we should be 1729 * able to keep up at the expense of system resources. 1730 */ 1731 writel(0, rxq->bd.reg_desc_active); 1732 } 1733 rxq->bd.cur = bdp; 1734 return pkt_received; 1735 } 1736 1737 static int fec_enet_rx(struct net_device *ndev, int budget) 1738 { 1739 struct fec_enet_private *fep = netdev_priv(ndev); 1740 int i, done = 0; 1741 1742 /* Make sure that AVB queues are processed first. */ 1743 for (i = fep->num_rx_queues - 1; i >= 0; i--) 1744 done += fec_enet_rx_queue(ndev, budget - done, i); 1745 1746 return done; 1747 } 1748 1749 static bool fec_enet_collect_events(struct fec_enet_private *fep) 1750 { 1751 uint int_events; 1752 1753 int_events = readl(fep->hwp + FEC_IEVENT); 1754 1755 /* Don't clear MDIO events, we poll for those */ 1756 int_events &= ~FEC_ENET_MII; 1757 1758 writel(int_events, fep->hwp + FEC_IEVENT); 1759 1760 return int_events != 0; 1761 } 1762 1763 static irqreturn_t 1764 fec_enet_interrupt(int irq, void *dev_id) 1765 { 1766 struct net_device *ndev = dev_id; 1767 struct fec_enet_private *fep = netdev_priv(ndev); 1768 irqreturn_t ret = IRQ_NONE; 1769 1770 if (fec_enet_collect_events(fep) && fep->link) { 1771 ret = IRQ_HANDLED; 1772 1773 if (napi_schedule_prep(&fep->napi)) { 1774 /* Disable interrupts */ 1775 writel(0, fep->hwp + FEC_IMASK); 1776 __napi_schedule(&fep->napi); 1777 } 1778 } 1779 1780 return ret; 1781 } 1782 1783 static int fec_enet_rx_napi(struct napi_struct *napi, int budget) 1784 { 1785 struct net_device *ndev = napi->dev; 1786 struct fec_enet_private *fep = netdev_priv(ndev); 1787 int done = 0; 1788 1789 do { 1790 done += fec_enet_rx(ndev, budget - done); 1791 fec_enet_tx(ndev); 1792 } while ((done < budget) && fec_enet_collect_events(fep)); 1793 1794 if (done < budget) { 1795 napi_complete_done(napi, done); 1796 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1797 } 1798 1799 return done; 1800 } 1801 1802 /* ------------------------------------------------------------------------- */ 1803 static int fec_get_mac(struct net_device *ndev) 1804 { 1805 struct fec_enet_private *fep = netdev_priv(ndev); 1806 unsigned char *iap, tmpaddr[ETH_ALEN]; 1807 int ret; 1808 1809 /* 1810 * try to get mac address in following order: 1811 * 1812 * 1) module parameter via kernel command line in form 1813 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 1814 */ 1815 iap = macaddr; 1816 1817 /* 1818 * 2) from device tree data 1819 */ 1820 if (!is_valid_ether_addr(iap)) { 1821 struct device_node *np = fep->pdev->dev.of_node; 1822 if (np) { 1823 ret = of_get_mac_address(np, tmpaddr); 1824 if (!ret) 1825 iap = tmpaddr; 1826 else if (ret == -EPROBE_DEFER) 1827 return ret; 1828 } 1829 } 1830 1831 /* 1832 * 3) from flash or fuse (via platform data) 1833 */ 1834 if (!is_valid_ether_addr(iap)) { 1835 #ifdef CONFIG_M5272 1836 if (FEC_FLASHMAC) 1837 iap = (unsigned char *)FEC_FLASHMAC; 1838 #else 1839 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev); 1840 1841 if (pdata) 1842 iap = (unsigned char *)&pdata->mac; 1843 #endif 1844 } 1845 1846 /* 1847 * 4) FEC mac registers set by bootloader 1848 */ 1849 if (!is_valid_ether_addr(iap)) { 1850 *((__be32 *) &tmpaddr[0]) = 1851 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW)); 1852 *((__be16 *) &tmpaddr[4]) = 1853 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); 1854 iap = &tmpaddr[0]; 1855 } 1856 1857 /* 1858 * 5) random mac address 1859 */ 1860 if (!is_valid_ether_addr(iap)) { 1861 /* Report it and use a random ethernet address instead */ 1862 dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap); 1863 eth_hw_addr_random(ndev); 1864 dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n", 1865 ndev->dev_addr); 1866 return 0; 1867 } 1868 1869 /* Adjust MAC if using macaddr */ 1870 eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0); 1871 1872 return 0; 1873 } 1874 1875 /* ------------------------------------------------------------------------- */ 1876 1877 /* 1878 * Phy section 1879 */ 1880 static void fec_enet_adjust_link(struct net_device *ndev) 1881 { 1882 struct fec_enet_private *fep = netdev_priv(ndev); 1883 struct phy_device *phy_dev = ndev->phydev; 1884 int status_change = 0; 1885 1886 /* 1887 * If the netdev is down, or is going down, we're not interested 1888 * in link state events, so just mark our idea of the link as down 1889 * and ignore the event. 1890 */ 1891 if (!netif_running(ndev) || !netif_device_present(ndev)) { 1892 fep->link = 0; 1893 } else if (phy_dev->link) { 1894 if (!fep->link) { 1895 fep->link = phy_dev->link; 1896 status_change = 1; 1897 } 1898 1899 if (fep->full_duplex != phy_dev->duplex) { 1900 fep->full_duplex = phy_dev->duplex; 1901 status_change = 1; 1902 } 1903 1904 if (phy_dev->speed != fep->speed) { 1905 fep->speed = phy_dev->speed; 1906 status_change = 1; 1907 } 1908 1909 /* if any of the above changed restart the FEC */ 1910 if (status_change) { 1911 napi_disable(&fep->napi); 1912 netif_tx_lock_bh(ndev); 1913 fec_restart(ndev); 1914 netif_tx_wake_all_queues(ndev); 1915 netif_tx_unlock_bh(ndev); 1916 napi_enable(&fep->napi); 1917 } 1918 } else { 1919 if (fep->link) { 1920 napi_disable(&fep->napi); 1921 netif_tx_lock_bh(ndev); 1922 fec_stop(ndev); 1923 netif_tx_unlock_bh(ndev); 1924 napi_enable(&fep->napi); 1925 fep->link = phy_dev->link; 1926 status_change = 1; 1927 } 1928 } 1929 1930 if (status_change) 1931 phy_print_status(phy_dev); 1932 } 1933 1934 static int fec_enet_mdio_wait(struct fec_enet_private *fep) 1935 { 1936 uint ievent; 1937 int ret; 1938 1939 ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent, 1940 ievent & FEC_ENET_MII, 2, 30000); 1941 1942 if (!ret) 1943 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT); 1944 1945 return ret; 1946 } 1947 1948 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum) 1949 { 1950 struct fec_enet_private *fep = bus->priv; 1951 struct device *dev = &fep->pdev->dev; 1952 int ret = 0, frame_start, frame_addr, frame_op; 1953 bool is_c45 = !!(regnum & MII_ADDR_C45); 1954 1955 ret = pm_runtime_resume_and_get(dev); 1956 if (ret < 0) 1957 return ret; 1958 1959 if (is_c45) { 1960 frame_start = FEC_MMFR_ST_C45; 1961 1962 /* write address */ 1963 frame_addr = (regnum >> 16); 1964 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE | 1965 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 1966 FEC_MMFR_TA | (regnum & 0xFFFF), 1967 fep->hwp + FEC_MII_DATA); 1968 1969 /* wait for end of transfer */ 1970 ret = fec_enet_mdio_wait(fep); 1971 if (ret) { 1972 netdev_err(fep->netdev, "MDIO address write timeout\n"); 1973 goto out; 1974 } 1975 1976 frame_op = FEC_MMFR_OP_READ_C45; 1977 1978 } else { 1979 /* C22 read */ 1980 frame_op = FEC_MMFR_OP_READ; 1981 frame_start = FEC_MMFR_ST; 1982 frame_addr = regnum; 1983 } 1984 1985 /* start a read op */ 1986 writel(frame_start | frame_op | 1987 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 1988 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); 1989 1990 /* wait for end of transfer */ 1991 ret = fec_enet_mdio_wait(fep); 1992 if (ret) { 1993 netdev_err(fep->netdev, "MDIO read timeout\n"); 1994 goto out; 1995 } 1996 1997 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); 1998 1999 out: 2000 pm_runtime_mark_last_busy(dev); 2001 pm_runtime_put_autosuspend(dev); 2002 2003 return ret; 2004 } 2005 2006 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum, 2007 u16 value) 2008 { 2009 struct fec_enet_private *fep = bus->priv; 2010 struct device *dev = &fep->pdev->dev; 2011 int ret, frame_start, frame_addr; 2012 bool is_c45 = !!(regnum & MII_ADDR_C45); 2013 2014 ret = pm_runtime_resume_and_get(dev); 2015 if (ret < 0) 2016 return ret; 2017 2018 if (is_c45) { 2019 frame_start = FEC_MMFR_ST_C45; 2020 2021 /* write address */ 2022 frame_addr = (regnum >> 16); 2023 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE | 2024 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 2025 FEC_MMFR_TA | (regnum & 0xFFFF), 2026 fep->hwp + FEC_MII_DATA); 2027 2028 /* wait for end of transfer */ 2029 ret = fec_enet_mdio_wait(fep); 2030 if (ret) { 2031 netdev_err(fep->netdev, "MDIO address write timeout\n"); 2032 goto out; 2033 } 2034 } else { 2035 /* C22 write */ 2036 frame_start = FEC_MMFR_ST; 2037 frame_addr = regnum; 2038 } 2039 2040 /* start a write op */ 2041 writel(frame_start | FEC_MMFR_OP_WRITE | 2042 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 2043 FEC_MMFR_TA | FEC_MMFR_DATA(value), 2044 fep->hwp + FEC_MII_DATA); 2045 2046 /* wait for end of transfer */ 2047 ret = fec_enet_mdio_wait(fep); 2048 if (ret) 2049 netdev_err(fep->netdev, "MDIO write timeout\n"); 2050 2051 out: 2052 pm_runtime_mark_last_busy(dev); 2053 pm_runtime_put_autosuspend(dev); 2054 2055 return ret; 2056 } 2057 2058 static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev) 2059 { 2060 struct fec_enet_private *fep = netdev_priv(ndev); 2061 struct phy_device *phy_dev = ndev->phydev; 2062 2063 if (phy_dev) { 2064 phy_reset_after_clk_enable(phy_dev); 2065 } else if (fep->phy_node) { 2066 /* 2067 * If the PHY still is not bound to the MAC, but there is 2068 * OF PHY node and a matching PHY device instance already, 2069 * use the OF PHY node to obtain the PHY device instance, 2070 * and then use that PHY device instance when triggering 2071 * the PHY reset. 2072 */ 2073 phy_dev = of_phy_find_device(fep->phy_node); 2074 phy_reset_after_clk_enable(phy_dev); 2075 put_device(&phy_dev->mdio.dev); 2076 } 2077 } 2078 2079 static int fec_enet_clk_enable(struct net_device *ndev, bool enable) 2080 { 2081 struct fec_enet_private *fep = netdev_priv(ndev); 2082 int ret; 2083 2084 if (enable) { 2085 ret = clk_prepare_enable(fep->clk_enet_out); 2086 if (ret) 2087 return ret; 2088 2089 if (fep->clk_ptp) { 2090 mutex_lock(&fep->ptp_clk_mutex); 2091 ret = clk_prepare_enable(fep->clk_ptp); 2092 if (ret) { 2093 mutex_unlock(&fep->ptp_clk_mutex); 2094 goto failed_clk_ptp; 2095 } else { 2096 fep->ptp_clk_on = true; 2097 } 2098 mutex_unlock(&fep->ptp_clk_mutex); 2099 } 2100 2101 ret = clk_prepare_enable(fep->clk_ref); 2102 if (ret) 2103 goto failed_clk_ref; 2104 2105 ret = clk_prepare_enable(fep->clk_2x_txclk); 2106 if (ret) 2107 goto failed_clk_2x_txclk; 2108 2109 fec_enet_phy_reset_after_clk_enable(ndev); 2110 } else { 2111 clk_disable_unprepare(fep->clk_enet_out); 2112 if (fep->clk_ptp) { 2113 mutex_lock(&fep->ptp_clk_mutex); 2114 clk_disable_unprepare(fep->clk_ptp); 2115 fep->ptp_clk_on = false; 2116 mutex_unlock(&fep->ptp_clk_mutex); 2117 } 2118 clk_disable_unprepare(fep->clk_ref); 2119 clk_disable_unprepare(fep->clk_2x_txclk); 2120 } 2121 2122 return 0; 2123 2124 failed_clk_2x_txclk: 2125 if (fep->clk_ref) 2126 clk_disable_unprepare(fep->clk_ref); 2127 failed_clk_ref: 2128 if (fep->clk_ptp) { 2129 mutex_lock(&fep->ptp_clk_mutex); 2130 clk_disable_unprepare(fep->clk_ptp); 2131 fep->ptp_clk_on = false; 2132 mutex_unlock(&fep->ptp_clk_mutex); 2133 } 2134 failed_clk_ptp: 2135 clk_disable_unprepare(fep->clk_enet_out); 2136 2137 return ret; 2138 } 2139 2140 static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep, 2141 struct device_node *np) 2142 { 2143 u32 rgmii_tx_delay, rgmii_rx_delay; 2144 2145 /* For rgmii tx internal delay, valid values are 0ps and 2000ps */ 2146 if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) { 2147 if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) { 2148 dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps"); 2149 return -EINVAL; 2150 } else if (rgmii_tx_delay == 2000) { 2151 fep->rgmii_txc_dly = true; 2152 } 2153 } 2154 2155 /* For rgmii rx internal delay, valid values are 0ps and 2000ps */ 2156 if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) { 2157 if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) { 2158 dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps"); 2159 return -EINVAL; 2160 } else if (rgmii_rx_delay == 2000) { 2161 fep->rgmii_rxc_dly = true; 2162 } 2163 } 2164 2165 return 0; 2166 } 2167 2168 static int fec_enet_mii_probe(struct net_device *ndev) 2169 { 2170 struct fec_enet_private *fep = netdev_priv(ndev); 2171 struct phy_device *phy_dev = NULL; 2172 char mdio_bus_id[MII_BUS_ID_SIZE]; 2173 char phy_name[MII_BUS_ID_SIZE + 3]; 2174 int phy_id; 2175 int dev_id = fep->dev_id; 2176 2177 if (fep->phy_node) { 2178 phy_dev = of_phy_connect(ndev, fep->phy_node, 2179 &fec_enet_adjust_link, 0, 2180 fep->phy_interface); 2181 if (!phy_dev) { 2182 netdev_err(ndev, "Unable to connect to phy\n"); 2183 return -ENODEV; 2184 } 2185 } else { 2186 /* check for attached phy */ 2187 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { 2188 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id)) 2189 continue; 2190 if (dev_id--) 2191 continue; 2192 strscpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); 2193 break; 2194 } 2195 2196 if (phy_id >= PHY_MAX_ADDR) { 2197 netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); 2198 strscpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); 2199 phy_id = 0; 2200 } 2201 2202 snprintf(phy_name, sizeof(phy_name), 2203 PHY_ID_FMT, mdio_bus_id, phy_id); 2204 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 2205 fep->phy_interface); 2206 } 2207 2208 if (IS_ERR(phy_dev)) { 2209 netdev_err(ndev, "could not attach to PHY\n"); 2210 return PTR_ERR(phy_dev); 2211 } 2212 2213 /* mask with MAC supported features */ 2214 if (fep->quirks & FEC_QUIRK_HAS_GBIT) { 2215 phy_set_max_speed(phy_dev, 1000); 2216 phy_remove_link_mode(phy_dev, 2217 ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 2218 #if !defined(CONFIG_M5272) 2219 phy_support_sym_pause(phy_dev); 2220 #endif 2221 } 2222 else 2223 phy_set_max_speed(phy_dev, 100); 2224 2225 fep->link = 0; 2226 fep->full_duplex = 0; 2227 2228 phy_dev->mac_managed_pm = 1; 2229 2230 phy_attached_info(phy_dev); 2231 2232 return 0; 2233 } 2234 2235 static int fec_enet_mii_init(struct platform_device *pdev) 2236 { 2237 static struct mii_bus *fec0_mii_bus; 2238 struct net_device *ndev = platform_get_drvdata(pdev); 2239 struct fec_enet_private *fep = netdev_priv(ndev); 2240 bool suppress_preamble = false; 2241 struct device_node *node; 2242 int err = -ENXIO; 2243 u32 mii_speed, holdtime; 2244 u32 bus_freq; 2245 2246 /* 2247 * The i.MX28 dual fec interfaces are not equal. 2248 * Here are the differences: 2249 * 2250 * - fec0 supports MII & RMII modes while fec1 only supports RMII 2251 * - fec0 acts as the 1588 time master while fec1 is slave 2252 * - external phys can only be configured by fec0 2253 * 2254 * That is to say fec1 can not work independently. It only works 2255 * when fec0 is working. The reason behind this design is that the 2256 * second interface is added primarily for Switch mode. 2257 * 2258 * Because of the last point above, both phys are attached on fec0 2259 * mdio interface in board design, and need to be configured by 2260 * fec0 mii_bus. 2261 */ 2262 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) { 2263 /* fec1 uses fec0 mii_bus */ 2264 if (mii_cnt && fec0_mii_bus) { 2265 fep->mii_bus = fec0_mii_bus; 2266 mii_cnt++; 2267 return 0; 2268 } 2269 return -ENOENT; 2270 } 2271 2272 bus_freq = 2500000; /* 2.5MHz by default */ 2273 node = of_get_child_by_name(pdev->dev.of_node, "mdio"); 2274 if (node) { 2275 of_property_read_u32(node, "clock-frequency", &bus_freq); 2276 suppress_preamble = of_property_read_bool(node, 2277 "suppress-preamble"); 2278 } 2279 2280 /* 2281 * Set MII speed (= clk_get_rate() / 2 * phy_speed) 2282 * 2283 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while 2284 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 2285 * Reference Manual has an error on this, and gets fixed on i.MX6Q 2286 * document. 2287 */ 2288 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2); 2289 if (fep->quirks & FEC_QUIRK_ENET_MAC) 2290 mii_speed--; 2291 if (mii_speed > 63) { 2292 dev_err(&pdev->dev, 2293 "fec clock (%lu) too fast to get right mii speed\n", 2294 clk_get_rate(fep->clk_ipg)); 2295 err = -EINVAL; 2296 goto err_out; 2297 } 2298 2299 /* 2300 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka 2301 * MII_SPEED) register that defines the MDIO output hold time. Earlier 2302 * versions are RAZ there, so just ignore the difference and write the 2303 * register always. 2304 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns. 2305 * HOLDTIME + 1 is the number of clk cycles the fec is holding the 2306 * output. 2307 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive). 2308 * Given that ceil(clkrate / 5000000) <= 64, the calculation for 2309 * holdtime cannot result in a value greater than 3. 2310 */ 2311 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1; 2312 2313 fep->phy_speed = mii_speed << 1 | holdtime << 8; 2314 2315 if (suppress_preamble) 2316 fep->phy_speed |= BIT(7); 2317 2318 if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) { 2319 /* Clear MMFR to avoid to generate MII event by writing MSCR. 2320 * MII event generation condition: 2321 * - writing MSCR: 2322 * - mmfr[31:0]_not_zero & mscr[7:0]_is_zero & 2323 * mscr_reg_data_in[7:0] != 0 2324 * - writing MMFR: 2325 * - mscr[7:0]_not_zero 2326 */ 2327 writel(0, fep->hwp + FEC_MII_DATA); 2328 } 2329 2330 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 2331 2332 /* Clear any pending transaction complete indication */ 2333 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT); 2334 2335 fep->mii_bus = mdiobus_alloc(); 2336 if (fep->mii_bus == NULL) { 2337 err = -ENOMEM; 2338 goto err_out; 2339 } 2340 2341 fep->mii_bus->name = "fec_enet_mii_bus"; 2342 fep->mii_bus->read = fec_enet_mdio_read; 2343 fep->mii_bus->write = fec_enet_mdio_write; 2344 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 2345 pdev->name, fep->dev_id + 1); 2346 fep->mii_bus->priv = fep; 2347 fep->mii_bus->parent = &pdev->dev; 2348 2349 err = of_mdiobus_register(fep->mii_bus, node); 2350 if (err) 2351 goto err_out_free_mdiobus; 2352 of_node_put(node); 2353 2354 mii_cnt++; 2355 2356 /* save fec0 mii_bus */ 2357 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO) 2358 fec0_mii_bus = fep->mii_bus; 2359 2360 return 0; 2361 2362 err_out_free_mdiobus: 2363 mdiobus_free(fep->mii_bus); 2364 err_out: 2365 of_node_put(node); 2366 return err; 2367 } 2368 2369 static void fec_enet_mii_remove(struct fec_enet_private *fep) 2370 { 2371 if (--mii_cnt == 0) { 2372 mdiobus_unregister(fep->mii_bus); 2373 mdiobus_free(fep->mii_bus); 2374 } 2375 } 2376 2377 static void fec_enet_get_drvinfo(struct net_device *ndev, 2378 struct ethtool_drvinfo *info) 2379 { 2380 struct fec_enet_private *fep = netdev_priv(ndev); 2381 2382 strscpy(info->driver, fep->pdev->dev.driver->name, 2383 sizeof(info->driver)); 2384 strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); 2385 } 2386 2387 static int fec_enet_get_regs_len(struct net_device *ndev) 2388 { 2389 struct fec_enet_private *fep = netdev_priv(ndev); 2390 struct resource *r; 2391 int s = 0; 2392 2393 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0); 2394 if (r) 2395 s = resource_size(r); 2396 2397 return s; 2398 } 2399 2400 /* List of registers that can be safety be read to dump them with ethtool */ 2401 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 2402 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 2403 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST) 2404 static __u32 fec_enet_register_version = 2; 2405 static u32 fec_enet_register_offset[] = { 2406 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0, 2407 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL, 2408 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1, 2409 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH, 2410 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, 2411 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1, 2412 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2, 2413 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0, 2414 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM, 2415 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2, 2416 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1, 2417 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME, 2418 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT, 2419 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG, 2420 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255, 2421 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047, 2422 RMON_T_P_GTE2048, RMON_T_OCTETS, 2423 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF, 2424 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE, 2425 IEEE_T_FDXFC, IEEE_T_OCTETS_OK, 2426 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN, 2427 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB, 2428 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255, 2429 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047, 2430 RMON_R_P_GTE2048, RMON_R_OCTETS, 2431 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR, 2432 IEEE_R_FDXFC, IEEE_R_OCTETS_OK 2433 }; 2434 /* for i.MX6ul */ 2435 static u32 fec_enet_register_offset_6ul[] = { 2436 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0, 2437 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL, 2438 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0, 2439 FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, 2440 FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0, 2441 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM, 2442 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, 2443 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT, 2444 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG, 2445 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255, 2446 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047, 2447 RMON_T_P_GTE2048, RMON_T_OCTETS, 2448 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF, 2449 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE, 2450 IEEE_T_FDXFC, IEEE_T_OCTETS_OK, 2451 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN, 2452 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB, 2453 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255, 2454 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047, 2455 RMON_R_P_GTE2048, RMON_R_OCTETS, 2456 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR, 2457 IEEE_R_FDXFC, IEEE_R_OCTETS_OK 2458 }; 2459 #else 2460 static __u32 fec_enet_register_version = 1; 2461 static u32 fec_enet_register_offset[] = { 2462 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0, 2463 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0, 2464 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED, 2465 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL, 2466 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, 2467 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0, 2468 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0, 2469 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0, 2470 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2 2471 }; 2472 #endif 2473 2474 static void fec_enet_get_regs(struct net_device *ndev, 2475 struct ethtool_regs *regs, void *regbuf) 2476 { 2477 struct fec_enet_private *fep = netdev_priv(ndev); 2478 u32 __iomem *theregs = (u32 __iomem *)fep->hwp; 2479 struct device *dev = &fep->pdev->dev; 2480 u32 *buf = (u32 *)regbuf; 2481 u32 i, off; 2482 int ret; 2483 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 2484 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 2485 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST) 2486 u32 *reg_list; 2487 u32 reg_cnt; 2488 2489 if (!of_machine_is_compatible("fsl,imx6ul")) { 2490 reg_list = fec_enet_register_offset; 2491 reg_cnt = ARRAY_SIZE(fec_enet_register_offset); 2492 } else { 2493 reg_list = fec_enet_register_offset_6ul; 2494 reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul); 2495 } 2496 #else 2497 /* coldfire */ 2498 static u32 *reg_list = fec_enet_register_offset; 2499 static const u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset); 2500 #endif 2501 ret = pm_runtime_resume_and_get(dev); 2502 if (ret < 0) 2503 return; 2504 2505 regs->version = fec_enet_register_version; 2506 2507 memset(buf, 0, regs->len); 2508 2509 for (i = 0; i < reg_cnt; i++) { 2510 off = reg_list[i]; 2511 2512 if ((off == FEC_R_BOUND || off == FEC_R_FSTART) && 2513 !(fep->quirks & FEC_QUIRK_HAS_FRREG)) 2514 continue; 2515 2516 off >>= 2; 2517 buf[off] = readl(&theregs[off]); 2518 } 2519 2520 pm_runtime_mark_last_busy(dev); 2521 pm_runtime_put_autosuspend(dev); 2522 } 2523 2524 static int fec_enet_get_ts_info(struct net_device *ndev, 2525 struct ethtool_ts_info *info) 2526 { 2527 struct fec_enet_private *fep = netdev_priv(ndev); 2528 2529 if (fep->bufdesc_ex) { 2530 2531 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 2532 SOF_TIMESTAMPING_RX_SOFTWARE | 2533 SOF_TIMESTAMPING_SOFTWARE | 2534 SOF_TIMESTAMPING_TX_HARDWARE | 2535 SOF_TIMESTAMPING_RX_HARDWARE | 2536 SOF_TIMESTAMPING_RAW_HARDWARE; 2537 if (fep->ptp_clock) 2538 info->phc_index = ptp_clock_index(fep->ptp_clock); 2539 else 2540 info->phc_index = -1; 2541 2542 info->tx_types = (1 << HWTSTAMP_TX_OFF) | 2543 (1 << HWTSTAMP_TX_ON); 2544 2545 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 2546 (1 << HWTSTAMP_FILTER_ALL); 2547 return 0; 2548 } else { 2549 return ethtool_op_get_ts_info(ndev, info); 2550 } 2551 } 2552 2553 #if !defined(CONFIG_M5272) 2554 2555 static void fec_enet_get_pauseparam(struct net_device *ndev, 2556 struct ethtool_pauseparam *pause) 2557 { 2558 struct fec_enet_private *fep = netdev_priv(ndev); 2559 2560 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; 2561 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; 2562 pause->rx_pause = pause->tx_pause; 2563 } 2564 2565 static int fec_enet_set_pauseparam(struct net_device *ndev, 2566 struct ethtool_pauseparam *pause) 2567 { 2568 struct fec_enet_private *fep = netdev_priv(ndev); 2569 2570 if (!ndev->phydev) 2571 return -ENODEV; 2572 2573 if (pause->tx_pause != pause->rx_pause) { 2574 netdev_info(ndev, 2575 "hardware only support enable/disable both tx and rx"); 2576 return -EINVAL; 2577 } 2578 2579 fep->pause_flag = 0; 2580 2581 /* tx pause must be same as rx pause */ 2582 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; 2583 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; 2584 2585 phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause, 2586 pause->autoneg); 2587 2588 if (pause->autoneg) { 2589 if (netif_running(ndev)) 2590 fec_stop(ndev); 2591 phy_start_aneg(ndev->phydev); 2592 } 2593 if (netif_running(ndev)) { 2594 napi_disable(&fep->napi); 2595 netif_tx_lock_bh(ndev); 2596 fec_restart(ndev); 2597 netif_tx_wake_all_queues(ndev); 2598 netif_tx_unlock_bh(ndev); 2599 napi_enable(&fep->napi); 2600 } 2601 2602 return 0; 2603 } 2604 2605 static const struct fec_stat { 2606 char name[ETH_GSTRING_LEN]; 2607 u16 offset; 2608 } fec_stats[] = { 2609 /* RMON TX */ 2610 { "tx_dropped", RMON_T_DROP }, 2611 { "tx_packets", RMON_T_PACKETS }, 2612 { "tx_broadcast", RMON_T_BC_PKT }, 2613 { "tx_multicast", RMON_T_MC_PKT }, 2614 { "tx_crc_errors", RMON_T_CRC_ALIGN }, 2615 { "tx_undersize", RMON_T_UNDERSIZE }, 2616 { "tx_oversize", RMON_T_OVERSIZE }, 2617 { "tx_fragment", RMON_T_FRAG }, 2618 { "tx_jabber", RMON_T_JAB }, 2619 { "tx_collision", RMON_T_COL }, 2620 { "tx_64byte", RMON_T_P64 }, 2621 { "tx_65to127byte", RMON_T_P65TO127 }, 2622 { "tx_128to255byte", RMON_T_P128TO255 }, 2623 { "tx_256to511byte", RMON_T_P256TO511 }, 2624 { "tx_512to1023byte", RMON_T_P512TO1023 }, 2625 { "tx_1024to2047byte", RMON_T_P1024TO2047 }, 2626 { "tx_GTE2048byte", RMON_T_P_GTE2048 }, 2627 { "tx_octets", RMON_T_OCTETS }, 2628 2629 /* IEEE TX */ 2630 { "IEEE_tx_drop", IEEE_T_DROP }, 2631 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK }, 2632 { "IEEE_tx_1col", IEEE_T_1COL }, 2633 { "IEEE_tx_mcol", IEEE_T_MCOL }, 2634 { "IEEE_tx_def", IEEE_T_DEF }, 2635 { "IEEE_tx_lcol", IEEE_T_LCOL }, 2636 { "IEEE_tx_excol", IEEE_T_EXCOL }, 2637 { "IEEE_tx_macerr", IEEE_T_MACERR }, 2638 { "IEEE_tx_cserr", IEEE_T_CSERR }, 2639 { "IEEE_tx_sqe", IEEE_T_SQE }, 2640 { "IEEE_tx_fdxfc", IEEE_T_FDXFC }, 2641 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK }, 2642 2643 /* RMON RX */ 2644 { "rx_packets", RMON_R_PACKETS }, 2645 { "rx_broadcast", RMON_R_BC_PKT }, 2646 { "rx_multicast", RMON_R_MC_PKT }, 2647 { "rx_crc_errors", RMON_R_CRC_ALIGN }, 2648 { "rx_undersize", RMON_R_UNDERSIZE }, 2649 { "rx_oversize", RMON_R_OVERSIZE }, 2650 { "rx_fragment", RMON_R_FRAG }, 2651 { "rx_jabber", RMON_R_JAB }, 2652 { "rx_64byte", RMON_R_P64 }, 2653 { "rx_65to127byte", RMON_R_P65TO127 }, 2654 { "rx_128to255byte", RMON_R_P128TO255 }, 2655 { "rx_256to511byte", RMON_R_P256TO511 }, 2656 { "rx_512to1023byte", RMON_R_P512TO1023 }, 2657 { "rx_1024to2047byte", RMON_R_P1024TO2047 }, 2658 { "rx_GTE2048byte", RMON_R_P_GTE2048 }, 2659 { "rx_octets", RMON_R_OCTETS }, 2660 2661 /* IEEE RX */ 2662 { "IEEE_rx_drop", IEEE_R_DROP }, 2663 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK }, 2664 { "IEEE_rx_crc", IEEE_R_CRC }, 2665 { "IEEE_rx_align", IEEE_R_ALIGN }, 2666 { "IEEE_rx_macerr", IEEE_R_MACERR }, 2667 { "IEEE_rx_fdxfc", IEEE_R_FDXFC }, 2668 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK }, 2669 }; 2670 2671 #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64)) 2672 2673 static void fec_enet_update_ethtool_stats(struct net_device *dev) 2674 { 2675 struct fec_enet_private *fep = netdev_priv(dev); 2676 int i; 2677 2678 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2679 fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset); 2680 } 2681 2682 static void fec_enet_get_ethtool_stats(struct net_device *dev, 2683 struct ethtool_stats *stats, u64 *data) 2684 { 2685 struct fec_enet_private *fep = netdev_priv(dev); 2686 2687 if (netif_running(dev)) 2688 fec_enet_update_ethtool_stats(dev); 2689 2690 memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE); 2691 } 2692 2693 static void fec_enet_get_strings(struct net_device *netdev, 2694 u32 stringset, u8 *data) 2695 { 2696 int i; 2697 switch (stringset) { 2698 case ETH_SS_STATS: 2699 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2700 memcpy(data + i * ETH_GSTRING_LEN, 2701 fec_stats[i].name, ETH_GSTRING_LEN); 2702 break; 2703 case ETH_SS_TEST: 2704 net_selftest_get_strings(data); 2705 break; 2706 } 2707 } 2708 2709 static int fec_enet_get_sset_count(struct net_device *dev, int sset) 2710 { 2711 switch (sset) { 2712 case ETH_SS_STATS: 2713 return ARRAY_SIZE(fec_stats); 2714 case ETH_SS_TEST: 2715 return net_selftest_get_count(); 2716 default: 2717 return -EOPNOTSUPP; 2718 } 2719 } 2720 2721 static void fec_enet_clear_ethtool_stats(struct net_device *dev) 2722 { 2723 struct fec_enet_private *fep = netdev_priv(dev); 2724 int i; 2725 2726 /* Disable MIB statistics counters */ 2727 writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT); 2728 2729 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2730 writel(0, fep->hwp + fec_stats[i].offset); 2731 2732 /* Don't disable MIB statistics counters */ 2733 writel(0, fep->hwp + FEC_MIB_CTRLSTAT); 2734 } 2735 2736 #else /* !defined(CONFIG_M5272) */ 2737 #define FEC_STATS_SIZE 0 2738 static inline void fec_enet_update_ethtool_stats(struct net_device *dev) 2739 { 2740 } 2741 2742 static inline void fec_enet_clear_ethtool_stats(struct net_device *dev) 2743 { 2744 } 2745 #endif /* !defined(CONFIG_M5272) */ 2746 2747 /* ITR clock source is enet system clock (clk_ahb). 2748 * TCTT unit is cycle_ns * 64 cycle 2749 * So, the ICTT value = X us / (cycle_ns * 64) 2750 */ 2751 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us) 2752 { 2753 struct fec_enet_private *fep = netdev_priv(ndev); 2754 2755 return us * (fep->itr_clk_rate / 64000) / 1000; 2756 } 2757 2758 /* Set threshold for interrupt coalescing */ 2759 static void fec_enet_itr_coal_set(struct net_device *ndev) 2760 { 2761 struct fec_enet_private *fep = netdev_priv(ndev); 2762 int rx_itr, tx_itr; 2763 2764 /* Must be greater than zero to avoid unpredictable behavior */ 2765 if (!fep->rx_time_itr || !fep->rx_pkts_itr || 2766 !fep->tx_time_itr || !fep->tx_pkts_itr) 2767 return; 2768 2769 /* Select enet system clock as Interrupt Coalescing 2770 * timer Clock Source 2771 */ 2772 rx_itr = FEC_ITR_CLK_SEL; 2773 tx_itr = FEC_ITR_CLK_SEL; 2774 2775 /* set ICFT and ICTT */ 2776 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr); 2777 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr)); 2778 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr); 2779 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr)); 2780 2781 rx_itr |= FEC_ITR_EN; 2782 tx_itr |= FEC_ITR_EN; 2783 2784 writel(tx_itr, fep->hwp + FEC_TXIC0); 2785 writel(rx_itr, fep->hwp + FEC_RXIC0); 2786 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) { 2787 writel(tx_itr, fep->hwp + FEC_TXIC1); 2788 writel(rx_itr, fep->hwp + FEC_RXIC1); 2789 writel(tx_itr, fep->hwp + FEC_TXIC2); 2790 writel(rx_itr, fep->hwp + FEC_RXIC2); 2791 } 2792 } 2793 2794 static int fec_enet_get_coalesce(struct net_device *ndev, 2795 struct ethtool_coalesce *ec, 2796 struct kernel_ethtool_coalesce *kernel_coal, 2797 struct netlink_ext_ack *extack) 2798 { 2799 struct fec_enet_private *fep = netdev_priv(ndev); 2800 2801 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 2802 return -EOPNOTSUPP; 2803 2804 ec->rx_coalesce_usecs = fep->rx_time_itr; 2805 ec->rx_max_coalesced_frames = fep->rx_pkts_itr; 2806 2807 ec->tx_coalesce_usecs = fep->tx_time_itr; 2808 ec->tx_max_coalesced_frames = fep->tx_pkts_itr; 2809 2810 return 0; 2811 } 2812 2813 static int fec_enet_set_coalesce(struct net_device *ndev, 2814 struct ethtool_coalesce *ec, 2815 struct kernel_ethtool_coalesce *kernel_coal, 2816 struct netlink_ext_ack *extack) 2817 { 2818 struct fec_enet_private *fep = netdev_priv(ndev); 2819 struct device *dev = &fep->pdev->dev; 2820 unsigned int cycle; 2821 2822 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 2823 return -EOPNOTSUPP; 2824 2825 if (ec->rx_max_coalesced_frames > 255) { 2826 dev_err(dev, "Rx coalesced frames exceed hardware limitation\n"); 2827 return -EINVAL; 2828 } 2829 2830 if (ec->tx_max_coalesced_frames > 255) { 2831 dev_err(dev, "Tx coalesced frame exceed hardware limitation\n"); 2832 return -EINVAL; 2833 } 2834 2835 cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs); 2836 if (cycle > 0xFFFF) { 2837 dev_err(dev, "Rx coalesced usec exceed hardware limitation\n"); 2838 return -EINVAL; 2839 } 2840 2841 cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs); 2842 if (cycle > 0xFFFF) { 2843 dev_err(dev, "Tx coalesced usec exceed hardware limitation\n"); 2844 return -EINVAL; 2845 } 2846 2847 fep->rx_time_itr = ec->rx_coalesce_usecs; 2848 fep->rx_pkts_itr = ec->rx_max_coalesced_frames; 2849 2850 fep->tx_time_itr = ec->tx_coalesce_usecs; 2851 fep->tx_pkts_itr = ec->tx_max_coalesced_frames; 2852 2853 fec_enet_itr_coal_set(ndev); 2854 2855 return 0; 2856 } 2857 2858 static int fec_enet_get_tunable(struct net_device *netdev, 2859 const struct ethtool_tunable *tuna, 2860 void *data) 2861 { 2862 struct fec_enet_private *fep = netdev_priv(netdev); 2863 int ret = 0; 2864 2865 switch (tuna->id) { 2866 case ETHTOOL_RX_COPYBREAK: 2867 *(u32 *)data = fep->rx_copybreak; 2868 break; 2869 default: 2870 ret = -EINVAL; 2871 break; 2872 } 2873 2874 return ret; 2875 } 2876 2877 static int fec_enet_set_tunable(struct net_device *netdev, 2878 const struct ethtool_tunable *tuna, 2879 const void *data) 2880 { 2881 struct fec_enet_private *fep = netdev_priv(netdev); 2882 int ret = 0; 2883 2884 switch (tuna->id) { 2885 case ETHTOOL_RX_COPYBREAK: 2886 fep->rx_copybreak = *(u32 *)data; 2887 break; 2888 default: 2889 ret = -EINVAL; 2890 break; 2891 } 2892 2893 return ret; 2894 } 2895 2896 /* LPI Sleep Ts count base on tx clk (clk_ref). 2897 * The lpi sleep cnt value = X us / (cycle_ns). 2898 */ 2899 static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us) 2900 { 2901 struct fec_enet_private *fep = netdev_priv(ndev); 2902 2903 return us * (fep->clk_ref_rate / 1000) / 1000; 2904 } 2905 2906 static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable) 2907 { 2908 struct fec_enet_private *fep = netdev_priv(ndev); 2909 struct ethtool_eee *p = &fep->eee; 2910 unsigned int sleep_cycle, wake_cycle; 2911 int ret = 0; 2912 2913 if (enable) { 2914 ret = phy_init_eee(ndev->phydev, false); 2915 if (ret) 2916 return ret; 2917 2918 sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer); 2919 wake_cycle = sleep_cycle; 2920 } else { 2921 sleep_cycle = 0; 2922 wake_cycle = 0; 2923 } 2924 2925 p->tx_lpi_enabled = enable; 2926 p->eee_enabled = enable; 2927 p->eee_active = enable; 2928 2929 writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP); 2930 writel(wake_cycle, fep->hwp + FEC_LPI_WAKE); 2931 2932 return 0; 2933 } 2934 2935 static int 2936 fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata) 2937 { 2938 struct fec_enet_private *fep = netdev_priv(ndev); 2939 struct ethtool_eee *p = &fep->eee; 2940 2941 if (!(fep->quirks & FEC_QUIRK_HAS_EEE)) 2942 return -EOPNOTSUPP; 2943 2944 if (!netif_running(ndev)) 2945 return -ENETDOWN; 2946 2947 edata->eee_enabled = p->eee_enabled; 2948 edata->eee_active = p->eee_active; 2949 edata->tx_lpi_timer = p->tx_lpi_timer; 2950 edata->tx_lpi_enabled = p->tx_lpi_enabled; 2951 2952 return phy_ethtool_get_eee(ndev->phydev, edata); 2953 } 2954 2955 static int 2956 fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata) 2957 { 2958 struct fec_enet_private *fep = netdev_priv(ndev); 2959 struct ethtool_eee *p = &fep->eee; 2960 int ret = 0; 2961 2962 if (!(fep->quirks & FEC_QUIRK_HAS_EEE)) 2963 return -EOPNOTSUPP; 2964 2965 if (!netif_running(ndev)) 2966 return -ENETDOWN; 2967 2968 p->tx_lpi_timer = edata->tx_lpi_timer; 2969 2970 if (!edata->eee_enabled || !edata->tx_lpi_enabled || 2971 !edata->tx_lpi_timer) 2972 ret = fec_enet_eee_mode_set(ndev, false); 2973 else 2974 ret = fec_enet_eee_mode_set(ndev, true); 2975 2976 if (ret) 2977 return ret; 2978 2979 return phy_ethtool_set_eee(ndev->phydev, edata); 2980 } 2981 2982 static void 2983 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2984 { 2985 struct fec_enet_private *fep = netdev_priv(ndev); 2986 2987 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) { 2988 wol->supported = WAKE_MAGIC; 2989 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0; 2990 } else { 2991 wol->supported = wol->wolopts = 0; 2992 } 2993 } 2994 2995 static int 2996 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2997 { 2998 struct fec_enet_private *fep = netdev_priv(ndev); 2999 3000 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET)) 3001 return -EINVAL; 3002 3003 if (wol->wolopts & ~WAKE_MAGIC) 3004 return -EINVAL; 3005 3006 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC); 3007 if (device_may_wakeup(&ndev->dev)) 3008 fep->wol_flag |= FEC_WOL_FLAG_ENABLE; 3009 else 3010 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE); 3011 3012 return 0; 3013 } 3014 3015 static const struct ethtool_ops fec_enet_ethtool_ops = { 3016 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 3017 ETHTOOL_COALESCE_MAX_FRAMES, 3018 .get_drvinfo = fec_enet_get_drvinfo, 3019 .get_regs_len = fec_enet_get_regs_len, 3020 .get_regs = fec_enet_get_regs, 3021 .nway_reset = phy_ethtool_nway_reset, 3022 .get_link = ethtool_op_get_link, 3023 .get_coalesce = fec_enet_get_coalesce, 3024 .set_coalesce = fec_enet_set_coalesce, 3025 #ifndef CONFIG_M5272 3026 .get_pauseparam = fec_enet_get_pauseparam, 3027 .set_pauseparam = fec_enet_set_pauseparam, 3028 .get_strings = fec_enet_get_strings, 3029 .get_ethtool_stats = fec_enet_get_ethtool_stats, 3030 .get_sset_count = fec_enet_get_sset_count, 3031 #endif 3032 .get_ts_info = fec_enet_get_ts_info, 3033 .get_tunable = fec_enet_get_tunable, 3034 .set_tunable = fec_enet_set_tunable, 3035 .get_wol = fec_enet_get_wol, 3036 .set_wol = fec_enet_set_wol, 3037 .get_eee = fec_enet_get_eee, 3038 .set_eee = fec_enet_set_eee, 3039 .get_link_ksettings = phy_ethtool_get_link_ksettings, 3040 .set_link_ksettings = phy_ethtool_set_link_ksettings, 3041 .self_test = net_selftest, 3042 }; 3043 3044 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 3045 { 3046 struct fec_enet_private *fep = netdev_priv(ndev); 3047 struct phy_device *phydev = ndev->phydev; 3048 3049 if (!netif_running(ndev)) 3050 return -EINVAL; 3051 3052 if (!phydev) 3053 return -ENODEV; 3054 3055 if (fep->bufdesc_ex) { 3056 bool use_fec_hwts = !phy_has_hwtstamp(phydev); 3057 3058 if (cmd == SIOCSHWTSTAMP) { 3059 if (use_fec_hwts) 3060 return fec_ptp_set(ndev, rq); 3061 fec_ptp_disable_hwts(ndev); 3062 } else if (cmd == SIOCGHWTSTAMP) { 3063 if (use_fec_hwts) 3064 return fec_ptp_get(ndev, rq); 3065 } 3066 } 3067 3068 return phy_mii_ioctl(phydev, rq, cmd); 3069 } 3070 3071 static void fec_enet_free_buffers(struct net_device *ndev) 3072 { 3073 struct fec_enet_private *fep = netdev_priv(ndev); 3074 unsigned int i; 3075 struct sk_buff *skb; 3076 struct fec_enet_priv_tx_q *txq; 3077 struct fec_enet_priv_rx_q *rxq; 3078 unsigned int q; 3079 3080 for (q = 0; q < fep->num_rx_queues; q++) { 3081 rxq = fep->rx_queue[q]; 3082 for (i = 0; i < rxq->bd.ring_size; i++) 3083 page_pool_release_page(rxq->page_pool, rxq->rx_skb_info[i].page); 3084 3085 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq)) 3086 xdp_rxq_info_unreg(&rxq->xdp_rxq); 3087 page_pool_destroy(rxq->page_pool); 3088 rxq->page_pool = NULL; 3089 } 3090 3091 for (q = 0; q < fep->num_tx_queues; q++) { 3092 txq = fep->tx_queue[q]; 3093 for (i = 0; i < txq->bd.ring_size; i++) { 3094 kfree(txq->tx_bounce[i]); 3095 txq->tx_bounce[i] = NULL; 3096 skb = txq->tx_skbuff[i]; 3097 txq->tx_skbuff[i] = NULL; 3098 dev_kfree_skb(skb); 3099 } 3100 } 3101 } 3102 3103 static void fec_enet_free_queue(struct net_device *ndev) 3104 { 3105 struct fec_enet_private *fep = netdev_priv(ndev); 3106 int i; 3107 struct fec_enet_priv_tx_q *txq; 3108 3109 for (i = 0; i < fep->num_tx_queues; i++) 3110 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) { 3111 txq = fep->tx_queue[i]; 3112 dma_free_coherent(&fep->pdev->dev, 3113 txq->bd.ring_size * TSO_HEADER_SIZE, 3114 txq->tso_hdrs, 3115 txq->tso_hdrs_dma); 3116 } 3117 3118 for (i = 0; i < fep->num_rx_queues; i++) 3119 kfree(fep->rx_queue[i]); 3120 for (i = 0; i < fep->num_tx_queues; i++) 3121 kfree(fep->tx_queue[i]); 3122 } 3123 3124 static int fec_enet_alloc_queue(struct net_device *ndev) 3125 { 3126 struct fec_enet_private *fep = netdev_priv(ndev); 3127 int i; 3128 int ret = 0; 3129 struct fec_enet_priv_tx_q *txq; 3130 3131 for (i = 0; i < fep->num_tx_queues; i++) { 3132 txq = kzalloc(sizeof(*txq), GFP_KERNEL); 3133 if (!txq) { 3134 ret = -ENOMEM; 3135 goto alloc_failed; 3136 } 3137 3138 fep->tx_queue[i] = txq; 3139 txq->bd.ring_size = TX_RING_SIZE; 3140 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size; 3141 3142 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS; 3143 txq->tx_wake_threshold = 3144 (txq->bd.ring_size - txq->tx_stop_threshold) / 2; 3145 3146 txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev, 3147 txq->bd.ring_size * TSO_HEADER_SIZE, 3148 &txq->tso_hdrs_dma, 3149 GFP_KERNEL); 3150 if (!txq->tso_hdrs) { 3151 ret = -ENOMEM; 3152 goto alloc_failed; 3153 } 3154 } 3155 3156 for (i = 0; i < fep->num_rx_queues; i++) { 3157 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]), 3158 GFP_KERNEL); 3159 if (!fep->rx_queue[i]) { 3160 ret = -ENOMEM; 3161 goto alloc_failed; 3162 } 3163 3164 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE; 3165 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size; 3166 } 3167 return ret; 3168 3169 alloc_failed: 3170 fec_enet_free_queue(ndev); 3171 return ret; 3172 } 3173 3174 static int 3175 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue) 3176 { 3177 struct fec_enet_private *fep = netdev_priv(ndev); 3178 struct fec_enet_priv_rx_q *rxq; 3179 dma_addr_t phys_addr; 3180 struct bufdesc *bdp; 3181 struct page *page; 3182 int i, err; 3183 3184 rxq = fep->rx_queue[queue]; 3185 bdp = rxq->bd.base; 3186 3187 err = fec_enet_create_page_pool(fep, rxq, rxq->bd.ring_size); 3188 if (err < 0) { 3189 netdev_err(ndev, "%s failed queue %d (%d)\n", __func__, queue, err); 3190 return err; 3191 } 3192 3193 for (i = 0; i < rxq->bd.ring_size; i++) { 3194 page = page_pool_dev_alloc_pages(rxq->page_pool); 3195 if (!page) 3196 goto err_alloc; 3197 3198 phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM; 3199 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr); 3200 3201 rxq->rx_skb_info[i].page = page; 3202 rxq->rx_skb_info[i].offset = FEC_ENET_XDP_HEADROOM; 3203 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 3204 3205 if (fep->bufdesc_ex) { 3206 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 3207 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 3208 } 3209 3210 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 3211 } 3212 3213 /* Set the last buffer to wrap. */ 3214 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 3215 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 3216 return 0; 3217 3218 err_alloc: 3219 fec_enet_free_buffers(ndev); 3220 return -ENOMEM; 3221 } 3222 3223 static int 3224 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue) 3225 { 3226 struct fec_enet_private *fep = netdev_priv(ndev); 3227 unsigned int i; 3228 struct bufdesc *bdp; 3229 struct fec_enet_priv_tx_q *txq; 3230 3231 txq = fep->tx_queue[queue]; 3232 bdp = txq->bd.base; 3233 for (i = 0; i < txq->bd.ring_size; i++) { 3234 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); 3235 if (!txq->tx_bounce[i]) 3236 goto err_alloc; 3237 3238 bdp->cbd_sc = cpu_to_fec16(0); 3239 bdp->cbd_bufaddr = cpu_to_fec32(0); 3240 3241 if (fep->bufdesc_ex) { 3242 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 3243 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT); 3244 } 3245 3246 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 3247 } 3248 3249 /* Set the last buffer to wrap. */ 3250 bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 3251 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 3252 3253 return 0; 3254 3255 err_alloc: 3256 fec_enet_free_buffers(ndev); 3257 return -ENOMEM; 3258 } 3259 3260 static int fec_enet_alloc_buffers(struct net_device *ndev) 3261 { 3262 struct fec_enet_private *fep = netdev_priv(ndev); 3263 unsigned int i; 3264 3265 for (i = 0; i < fep->num_rx_queues; i++) 3266 if (fec_enet_alloc_rxq_buffers(ndev, i)) 3267 return -ENOMEM; 3268 3269 for (i = 0; i < fep->num_tx_queues; i++) 3270 if (fec_enet_alloc_txq_buffers(ndev, i)) 3271 return -ENOMEM; 3272 return 0; 3273 } 3274 3275 static int 3276 fec_enet_open(struct net_device *ndev) 3277 { 3278 struct fec_enet_private *fep = netdev_priv(ndev); 3279 int ret; 3280 bool reset_again; 3281 3282 ret = pm_runtime_resume_and_get(&fep->pdev->dev); 3283 if (ret < 0) 3284 return ret; 3285 3286 pinctrl_pm_select_default_state(&fep->pdev->dev); 3287 ret = fec_enet_clk_enable(ndev, true); 3288 if (ret) 3289 goto clk_enable; 3290 3291 /* During the first fec_enet_open call the PHY isn't probed at this 3292 * point. Therefore the phy_reset_after_clk_enable() call within 3293 * fec_enet_clk_enable() fails. As we need this reset in order to be 3294 * sure the PHY is working correctly we check if we need to reset again 3295 * later when the PHY is probed 3296 */ 3297 if (ndev->phydev && ndev->phydev->drv) 3298 reset_again = false; 3299 else 3300 reset_again = true; 3301 3302 /* I should reset the ring buffers here, but I don't yet know 3303 * a simple way to do that. 3304 */ 3305 3306 ret = fec_enet_alloc_buffers(ndev); 3307 if (ret) 3308 goto err_enet_alloc; 3309 3310 /* Init MAC prior to mii bus probe */ 3311 fec_restart(ndev); 3312 3313 /* Call phy_reset_after_clk_enable() again if it failed during 3314 * phy_reset_after_clk_enable() before because the PHY wasn't probed. 3315 */ 3316 if (reset_again) 3317 fec_enet_phy_reset_after_clk_enable(ndev); 3318 3319 /* Probe and connect to PHY when open the interface */ 3320 ret = fec_enet_mii_probe(ndev); 3321 if (ret) 3322 goto err_enet_mii_probe; 3323 3324 if (fep->quirks & FEC_QUIRK_ERR006687) 3325 imx6q_cpuidle_fec_irqs_used(); 3326 3327 if (fep->quirks & FEC_QUIRK_HAS_PMQOS) 3328 cpu_latency_qos_add_request(&fep->pm_qos_req, 0); 3329 3330 napi_enable(&fep->napi); 3331 phy_start(ndev->phydev); 3332 netif_tx_start_all_queues(ndev); 3333 3334 device_set_wakeup_enable(&ndev->dev, fep->wol_flag & 3335 FEC_WOL_FLAG_ENABLE); 3336 3337 return 0; 3338 3339 err_enet_mii_probe: 3340 fec_enet_free_buffers(ndev); 3341 err_enet_alloc: 3342 fec_enet_clk_enable(ndev, false); 3343 clk_enable: 3344 pm_runtime_mark_last_busy(&fep->pdev->dev); 3345 pm_runtime_put_autosuspend(&fep->pdev->dev); 3346 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3347 return ret; 3348 } 3349 3350 static int 3351 fec_enet_close(struct net_device *ndev) 3352 { 3353 struct fec_enet_private *fep = netdev_priv(ndev); 3354 3355 phy_stop(ndev->phydev); 3356 3357 if (netif_device_present(ndev)) { 3358 napi_disable(&fep->napi); 3359 netif_tx_disable(ndev); 3360 fec_stop(ndev); 3361 } 3362 3363 phy_disconnect(ndev->phydev); 3364 3365 if (fep->quirks & FEC_QUIRK_ERR006687) 3366 imx6q_cpuidle_fec_irqs_unused(); 3367 3368 fec_enet_update_ethtool_stats(ndev); 3369 3370 fec_enet_clk_enable(ndev, false); 3371 if (fep->quirks & FEC_QUIRK_HAS_PMQOS) 3372 cpu_latency_qos_remove_request(&fep->pm_qos_req); 3373 3374 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3375 pm_runtime_mark_last_busy(&fep->pdev->dev); 3376 pm_runtime_put_autosuspend(&fep->pdev->dev); 3377 3378 fec_enet_free_buffers(ndev); 3379 3380 return 0; 3381 } 3382 3383 /* Set or clear the multicast filter for this adaptor. 3384 * Skeleton taken from sunlance driver. 3385 * The CPM Ethernet implementation allows Multicast as well as individual 3386 * MAC address filtering. Some of the drivers check to make sure it is 3387 * a group multicast address, and discard those that are not. I guess I 3388 * will do the same for now, but just remove the test if you want 3389 * individual filtering as well (do the upper net layers want or support 3390 * this kind of feature?). 3391 */ 3392 3393 #define FEC_HASH_BITS 6 /* #bits in hash */ 3394 3395 static void set_multicast_list(struct net_device *ndev) 3396 { 3397 struct fec_enet_private *fep = netdev_priv(ndev); 3398 struct netdev_hw_addr *ha; 3399 unsigned int crc, tmp; 3400 unsigned char hash; 3401 unsigned int hash_high = 0, hash_low = 0; 3402 3403 if (ndev->flags & IFF_PROMISC) { 3404 tmp = readl(fep->hwp + FEC_R_CNTRL); 3405 tmp |= 0x8; 3406 writel(tmp, fep->hwp + FEC_R_CNTRL); 3407 return; 3408 } 3409 3410 tmp = readl(fep->hwp + FEC_R_CNTRL); 3411 tmp &= ~0x8; 3412 writel(tmp, fep->hwp + FEC_R_CNTRL); 3413 3414 if (ndev->flags & IFF_ALLMULTI) { 3415 /* Catch all multicast addresses, so set the 3416 * filter to all 1's 3417 */ 3418 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 3419 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3420 3421 return; 3422 } 3423 3424 /* Add the addresses in hash register */ 3425 netdev_for_each_mc_addr(ha, ndev) { 3426 /* calculate crc32 value of mac address */ 3427 crc = ether_crc_le(ndev->addr_len, ha->addr); 3428 3429 /* only upper 6 bits (FEC_HASH_BITS) are used 3430 * which point to specific bit in the hash registers 3431 */ 3432 hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f; 3433 3434 if (hash > 31) 3435 hash_high |= 1 << (hash - 32); 3436 else 3437 hash_low |= 1 << hash; 3438 } 3439 3440 writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 3441 writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3442 } 3443 3444 /* Set a MAC change in hardware. */ 3445 static int 3446 fec_set_mac_address(struct net_device *ndev, void *p) 3447 { 3448 struct fec_enet_private *fep = netdev_priv(ndev); 3449 struct sockaddr *addr = p; 3450 3451 if (addr) { 3452 if (!is_valid_ether_addr(addr->sa_data)) 3453 return -EADDRNOTAVAIL; 3454 eth_hw_addr_set(ndev, addr->sa_data); 3455 } 3456 3457 /* Add netif status check here to avoid system hang in below case: 3458 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx; 3459 * After ethx down, fec all clocks are gated off and then register 3460 * access causes system hang. 3461 */ 3462 if (!netif_running(ndev)) 3463 return 0; 3464 3465 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | 3466 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), 3467 fep->hwp + FEC_ADDR_LOW); 3468 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), 3469 fep->hwp + FEC_ADDR_HIGH); 3470 return 0; 3471 } 3472 3473 #ifdef CONFIG_NET_POLL_CONTROLLER 3474 /** 3475 * fec_poll_controller - FEC Poll controller function 3476 * @dev: The FEC network adapter 3477 * 3478 * Polled functionality used by netconsole and others in non interrupt mode 3479 * 3480 */ 3481 static void fec_poll_controller(struct net_device *dev) 3482 { 3483 int i; 3484 struct fec_enet_private *fep = netdev_priv(dev); 3485 3486 for (i = 0; i < FEC_IRQ_NUM; i++) { 3487 if (fep->irq[i] > 0) { 3488 disable_irq(fep->irq[i]); 3489 fec_enet_interrupt(fep->irq[i], dev); 3490 enable_irq(fep->irq[i]); 3491 } 3492 } 3493 } 3494 #endif 3495 3496 static inline void fec_enet_set_netdev_features(struct net_device *netdev, 3497 netdev_features_t features) 3498 { 3499 struct fec_enet_private *fep = netdev_priv(netdev); 3500 netdev_features_t changed = features ^ netdev->features; 3501 3502 netdev->features = features; 3503 3504 /* Receive checksum has been changed */ 3505 if (changed & NETIF_F_RXCSUM) { 3506 if (features & NETIF_F_RXCSUM) 3507 fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 3508 else 3509 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; 3510 } 3511 } 3512 3513 static int fec_set_features(struct net_device *netdev, 3514 netdev_features_t features) 3515 { 3516 struct fec_enet_private *fep = netdev_priv(netdev); 3517 netdev_features_t changed = features ^ netdev->features; 3518 3519 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) { 3520 napi_disable(&fep->napi); 3521 netif_tx_lock_bh(netdev); 3522 fec_stop(netdev); 3523 fec_enet_set_netdev_features(netdev, features); 3524 fec_restart(netdev); 3525 netif_tx_wake_all_queues(netdev); 3526 netif_tx_unlock_bh(netdev); 3527 napi_enable(&fep->napi); 3528 } else { 3529 fec_enet_set_netdev_features(netdev, features); 3530 } 3531 3532 return 0; 3533 } 3534 3535 static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb) 3536 { 3537 struct vlan_ethhdr *vhdr; 3538 unsigned short vlan_TCI = 0; 3539 3540 if (skb->protocol == htons(ETH_P_ALL)) { 3541 vhdr = (struct vlan_ethhdr *)(skb->data); 3542 vlan_TCI = ntohs(vhdr->h_vlan_TCI); 3543 } 3544 3545 return vlan_TCI; 3546 } 3547 3548 static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb, 3549 struct net_device *sb_dev) 3550 { 3551 struct fec_enet_private *fep = netdev_priv(ndev); 3552 u16 vlan_tag; 3553 3554 if (!(fep->quirks & FEC_QUIRK_HAS_AVB)) 3555 return netdev_pick_tx(ndev, skb, NULL); 3556 3557 vlan_tag = fec_enet_get_raw_vlan_tci(skb); 3558 if (!vlan_tag) 3559 return vlan_tag; 3560 3561 return fec_enet_vlan_pri_to_queue[vlan_tag >> 13]; 3562 } 3563 3564 static const struct net_device_ops fec_netdev_ops = { 3565 .ndo_open = fec_enet_open, 3566 .ndo_stop = fec_enet_close, 3567 .ndo_start_xmit = fec_enet_start_xmit, 3568 .ndo_select_queue = fec_enet_select_queue, 3569 .ndo_set_rx_mode = set_multicast_list, 3570 .ndo_validate_addr = eth_validate_addr, 3571 .ndo_tx_timeout = fec_timeout, 3572 .ndo_set_mac_address = fec_set_mac_address, 3573 .ndo_eth_ioctl = fec_enet_ioctl, 3574 #ifdef CONFIG_NET_POLL_CONTROLLER 3575 .ndo_poll_controller = fec_poll_controller, 3576 #endif 3577 .ndo_set_features = fec_set_features, 3578 }; 3579 3580 static const unsigned short offset_des_active_rxq[] = { 3581 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2 3582 }; 3583 3584 static const unsigned short offset_des_active_txq[] = { 3585 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2 3586 }; 3587 3588 /* 3589 * XXX: We need to clean up on failure exits here. 3590 * 3591 */ 3592 static int fec_enet_init(struct net_device *ndev) 3593 { 3594 struct fec_enet_private *fep = netdev_priv(ndev); 3595 struct bufdesc *cbd_base; 3596 dma_addr_t bd_dma; 3597 int bd_size; 3598 unsigned int i; 3599 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) : 3600 sizeof(struct bufdesc); 3601 unsigned dsize_log2 = __fls(dsize); 3602 int ret; 3603 3604 WARN_ON(dsize != (1 << dsize_log2)); 3605 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64) 3606 fep->rx_align = 0xf; 3607 fep->tx_align = 0xf; 3608 #else 3609 fep->rx_align = 0x3; 3610 fep->tx_align = 0x3; 3611 #endif 3612 fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT; 3613 fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT; 3614 fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT; 3615 fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT; 3616 3617 /* Check mask of the streaming and coherent API */ 3618 ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32)); 3619 if (ret < 0) { 3620 dev_warn(&fep->pdev->dev, "No suitable DMA available\n"); 3621 return ret; 3622 } 3623 3624 ret = fec_enet_alloc_queue(ndev); 3625 if (ret) 3626 return ret; 3627 3628 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize; 3629 3630 /* Allocate memory for buffer descriptors. */ 3631 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma, 3632 GFP_KERNEL); 3633 if (!cbd_base) { 3634 ret = -ENOMEM; 3635 goto free_queue_mem; 3636 } 3637 3638 /* Get the Ethernet address */ 3639 ret = fec_get_mac(ndev); 3640 if (ret) 3641 goto free_queue_mem; 3642 3643 /* make sure MAC we just acquired is programmed into the hw */ 3644 fec_set_mac_address(ndev, NULL); 3645 3646 /* Set receive and transmit descriptor base. */ 3647 for (i = 0; i < fep->num_rx_queues; i++) { 3648 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i]; 3649 unsigned size = dsize * rxq->bd.ring_size; 3650 3651 rxq->bd.qid = i; 3652 rxq->bd.base = cbd_base; 3653 rxq->bd.cur = cbd_base; 3654 rxq->bd.dma = bd_dma; 3655 rxq->bd.dsize = dsize; 3656 rxq->bd.dsize_log2 = dsize_log2; 3657 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i]; 3658 bd_dma += size; 3659 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 3660 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 3661 } 3662 3663 for (i = 0; i < fep->num_tx_queues; i++) { 3664 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i]; 3665 unsigned size = dsize * txq->bd.ring_size; 3666 3667 txq->bd.qid = i; 3668 txq->bd.base = cbd_base; 3669 txq->bd.cur = cbd_base; 3670 txq->bd.dma = bd_dma; 3671 txq->bd.dsize = dsize; 3672 txq->bd.dsize_log2 = dsize_log2; 3673 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i]; 3674 bd_dma += size; 3675 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 3676 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 3677 } 3678 3679 3680 /* The FEC Ethernet specific entries in the device structure */ 3681 ndev->watchdog_timeo = TX_TIMEOUT; 3682 ndev->netdev_ops = &fec_netdev_ops; 3683 ndev->ethtool_ops = &fec_enet_ethtool_ops; 3684 3685 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); 3686 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi); 3687 3688 if (fep->quirks & FEC_QUIRK_HAS_VLAN) 3689 /* enable hw VLAN support */ 3690 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; 3691 3692 if (fep->quirks & FEC_QUIRK_HAS_CSUM) { 3693 netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS); 3694 3695 /* enable hw accelerator */ 3696 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM 3697 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO); 3698 fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 3699 } 3700 3701 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) { 3702 fep->tx_align = 0; 3703 fep->rx_align = 0x3f; 3704 } 3705 3706 ndev->hw_features = ndev->features; 3707 3708 fec_restart(ndev); 3709 3710 if (fep->quirks & FEC_QUIRK_MIB_CLEAR) 3711 fec_enet_clear_ethtool_stats(ndev); 3712 else 3713 fec_enet_update_ethtool_stats(ndev); 3714 3715 return 0; 3716 3717 free_queue_mem: 3718 fec_enet_free_queue(ndev); 3719 return ret; 3720 } 3721 3722 #ifdef CONFIG_OF 3723 static int fec_reset_phy(struct platform_device *pdev) 3724 { 3725 int err, phy_reset; 3726 bool active_high = false; 3727 int msec = 1, phy_post_delay = 0; 3728 struct device_node *np = pdev->dev.of_node; 3729 3730 if (!np) 3731 return 0; 3732 3733 err = of_property_read_u32(np, "phy-reset-duration", &msec); 3734 /* A sane reset duration should not be longer than 1s */ 3735 if (!err && msec > 1000) 3736 msec = 1; 3737 3738 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0); 3739 if (phy_reset == -EPROBE_DEFER) 3740 return phy_reset; 3741 else if (!gpio_is_valid(phy_reset)) 3742 return 0; 3743 3744 err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay); 3745 /* valid reset duration should be less than 1s */ 3746 if (!err && phy_post_delay > 1000) 3747 return -EINVAL; 3748 3749 active_high = of_property_read_bool(np, "phy-reset-active-high"); 3750 3751 err = devm_gpio_request_one(&pdev->dev, phy_reset, 3752 active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, 3753 "phy-reset"); 3754 if (err) { 3755 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err); 3756 return err; 3757 } 3758 3759 if (msec > 20) 3760 msleep(msec); 3761 else 3762 usleep_range(msec * 1000, msec * 1000 + 1000); 3763 3764 gpio_set_value_cansleep(phy_reset, !active_high); 3765 3766 if (!phy_post_delay) 3767 return 0; 3768 3769 if (phy_post_delay > 20) 3770 msleep(phy_post_delay); 3771 else 3772 usleep_range(phy_post_delay * 1000, 3773 phy_post_delay * 1000 + 1000); 3774 3775 return 0; 3776 } 3777 #else /* CONFIG_OF */ 3778 static int fec_reset_phy(struct platform_device *pdev) 3779 { 3780 /* 3781 * In case of platform probe, the reset has been done 3782 * by machine code. 3783 */ 3784 return 0; 3785 } 3786 #endif /* CONFIG_OF */ 3787 3788 static void 3789 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx) 3790 { 3791 struct device_node *np = pdev->dev.of_node; 3792 3793 *num_tx = *num_rx = 1; 3794 3795 if (!np || !of_device_is_available(np)) 3796 return; 3797 3798 /* parse the num of tx and rx queues */ 3799 of_property_read_u32(np, "fsl,num-tx-queues", num_tx); 3800 3801 of_property_read_u32(np, "fsl,num-rx-queues", num_rx); 3802 3803 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) { 3804 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n", 3805 *num_tx); 3806 *num_tx = 1; 3807 return; 3808 } 3809 3810 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) { 3811 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n", 3812 *num_rx); 3813 *num_rx = 1; 3814 return; 3815 } 3816 3817 } 3818 3819 static int fec_enet_get_irq_cnt(struct platform_device *pdev) 3820 { 3821 int irq_cnt = platform_irq_count(pdev); 3822 3823 if (irq_cnt > FEC_IRQ_NUM) 3824 irq_cnt = FEC_IRQ_NUM; /* last for pps */ 3825 else if (irq_cnt == 2) 3826 irq_cnt = 1; /* last for pps */ 3827 else if (irq_cnt <= 0) 3828 irq_cnt = 1; /* At least 1 irq is needed */ 3829 return irq_cnt; 3830 } 3831 3832 static void fec_enet_get_wakeup_irq(struct platform_device *pdev) 3833 { 3834 struct net_device *ndev = platform_get_drvdata(pdev); 3835 struct fec_enet_private *fep = netdev_priv(ndev); 3836 3837 if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2) 3838 fep->wake_irq = fep->irq[2]; 3839 else 3840 fep->wake_irq = fep->irq[0]; 3841 } 3842 3843 static int fec_enet_init_stop_mode(struct fec_enet_private *fep, 3844 struct device_node *np) 3845 { 3846 struct device_node *gpr_np; 3847 u32 out_val[3]; 3848 int ret = 0; 3849 3850 gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0); 3851 if (!gpr_np) 3852 return 0; 3853 3854 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val, 3855 ARRAY_SIZE(out_val)); 3856 if (ret) { 3857 dev_dbg(&fep->pdev->dev, "no stop mode property\n"); 3858 goto out; 3859 } 3860 3861 fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np); 3862 if (IS_ERR(fep->stop_gpr.gpr)) { 3863 dev_err(&fep->pdev->dev, "could not find gpr regmap\n"); 3864 ret = PTR_ERR(fep->stop_gpr.gpr); 3865 fep->stop_gpr.gpr = NULL; 3866 goto out; 3867 } 3868 3869 fep->stop_gpr.reg = out_val[1]; 3870 fep->stop_gpr.bit = out_val[2]; 3871 3872 out: 3873 of_node_put(gpr_np); 3874 3875 return ret; 3876 } 3877 3878 static int 3879 fec_probe(struct platform_device *pdev) 3880 { 3881 struct fec_enet_private *fep; 3882 struct fec_platform_data *pdata; 3883 phy_interface_t interface; 3884 struct net_device *ndev; 3885 int i, irq, ret = 0; 3886 const struct of_device_id *of_id; 3887 static int dev_id; 3888 struct device_node *np = pdev->dev.of_node, *phy_node; 3889 int num_tx_qs; 3890 int num_rx_qs; 3891 char irq_name[8]; 3892 int irq_cnt; 3893 struct fec_devinfo *dev_info; 3894 3895 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs); 3896 3897 /* Init network device */ 3898 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) + 3899 FEC_STATS_SIZE, num_tx_qs, num_rx_qs); 3900 if (!ndev) 3901 return -ENOMEM; 3902 3903 SET_NETDEV_DEV(ndev, &pdev->dev); 3904 3905 /* setup board info structure */ 3906 fep = netdev_priv(ndev); 3907 3908 of_id = of_match_device(fec_dt_ids, &pdev->dev); 3909 if (of_id) 3910 pdev->id_entry = of_id->data; 3911 dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data; 3912 if (dev_info) 3913 fep->quirks = dev_info->quirks; 3914 3915 fep->netdev = ndev; 3916 fep->num_rx_queues = num_rx_qs; 3917 fep->num_tx_queues = num_tx_qs; 3918 3919 #if !defined(CONFIG_M5272) 3920 /* default enable pause frame auto negotiation */ 3921 if (fep->quirks & FEC_QUIRK_HAS_GBIT) 3922 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; 3923 #endif 3924 3925 /* Select default pin state */ 3926 pinctrl_pm_select_default_state(&pdev->dev); 3927 3928 fep->hwp = devm_platform_ioremap_resource(pdev, 0); 3929 if (IS_ERR(fep->hwp)) { 3930 ret = PTR_ERR(fep->hwp); 3931 goto failed_ioremap; 3932 } 3933 3934 fep->pdev = pdev; 3935 fep->dev_id = dev_id++; 3936 3937 platform_set_drvdata(pdev, ndev); 3938 3939 if ((of_machine_is_compatible("fsl,imx6q") || 3940 of_machine_is_compatible("fsl,imx6dl")) && 3941 !of_property_read_bool(np, "fsl,err006687-workaround-present")) 3942 fep->quirks |= FEC_QUIRK_ERR006687; 3943 3944 ret = fec_enet_ipc_handle_init(fep); 3945 if (ret) 3946 goto failed_ipc_init; 3947 3948 if (of_get_property(np, "fsl,magic-packet", NULL)) 3949 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET; 3950 3951 ret = fec_enet_init_stop_mode(fep, np); 3952 if (ret) 3953 goto failed_stop_mode; 3954 3955 phy_node = of_parse_phandle(np, "phy-handle", 0); 3956 if (!phy_node && of_phy_is_fixed_link(np)) { 3957 ret = of_phy_register_fixed_link(np); 3958 if (ret < 0) { 3959 dev_err(&pdev->dev, 3960 "broken fixed-link specification\n"); 3961 goto failed_phy; 3962 } 3963 phy_node = of_node_get(np); 3964 } 3965 fep->phy_node = phy_node; 3966 3967 ret = of_get_phy_mode(pdev->dev.of_node, &interface); 3968 if (ret) { 3969 pdata = dev_get_platdata(&pdev->dev); 3970 if (pdata) 3971 fep->phy_interface = pdata->phy; 3972 else 3973 fep->phy_interface = PHY_INTERFACE_MODE_MII; 3974 } else { 3975 fep->phy_interface = interface; 3976 } 3977 3978 ret = fec_enet_parse_rgmii_delay(fep, np); 3979 if (ret) 3980 goto failed_rgmii_delay; 3981 3982 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 3983 if (IS_ERR(fep->clk_ipg)) { 3984 ret = PTR_ERR(fep->clk_ipg); 3985 goto failed_clk; 3986 } 3987 3988 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 3989 if (IS_ERR(fep->clk_ahb)) { 3990 ret = PTR_ERR(fep->clk_ahb); 3991 goto failed_clk; 3992 } 3993 3994 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb); 3995 3996 /* enet_out is optional, depends on board */ 3997 fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out"); 3998 if (IS_ERR(fep->clk_enet_out)) { 3999 ret = PTR_ERR(fep->clk_enet_out); 4000 goto failed_clk; 4001 } 4002 4003 fep->ptp_clk_on = false; 4004 mutex_init(&fep->ptp_clk_mutex); 4005 4006 /* clk_ref is optional, depends on board */ 4007 fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref"); 4008 if (IS_ERR(fep->clk_ref)) { 4009 ret = PTR_ERR(fep->clk_ref); 4010 goto failed_clk; 4011 } 4012 fep->clk_ref_rate = clk_get_rate(fep->clk_ref); 4013 4014 /* clk_2x_txclk is optional, depends on board */ 4015 if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) { 4016 fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk"); 4017 if (IS_ERR(fep->clk_2x_txclk)) 4018 fep->clk_2x_txclk = NULL; 4019 } 4020 4021 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX; 4022 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); 4023 if (IS_ERR(fep->clk_ptp)) { 4024 fep->clk_ptp = NULL; 4025 fep->bufdesc_ex = false; 4026 } 4027 4028 ret = fec_enet_clk_enable(ndev, true); 4029 if (ret) 4030 goto failed_clk; 4031 4032 ret = clk_prepare_enable(fep->clk_ipg); 4033 if (ret) 4034 goto failed_clk_ipg; 4035 ret = clk_prepare_enable(fep->clk_ahb); 4036 if (ret) 4037 goto failed_clk_ahb; 4038 4039 fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy"); 4040 if (!IS_ERR(fep->reg_phy)) { 4041 ret = regulator_enable(fep->reg_phy); 4042 if (ret) { 4043 dev_err(&pdev->dev, 4044 "Failed to enable phy regulator: %d\n", ret); 4045 goto failed_regulator; 4046 } 4047 } else { 4048 if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) { 4049 ret = -EPROBE_DEFER; 4050 goto failed_regulator; 4051 } 4052 fep->reg_phy = NULL; 4053 } 4054 4055 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT); 4056 pm_runtime_use_autosuspend(&pdev->dev); 4057 pm_runtime_get_noresume(&pdev->dev); 4058 pm_runtime_set_active(&pdev->dev); 4059 pm_runtime_enable(&pdev->dev); 4060 4061 ret = fec_reset_phy(pdev); 4062 if (ret) 4063 goto failed_reset; 4064 4065 irq_cnt = fec_enet_get_irq_cnt(pdev); 4066 if (fep->bufdesc_ex) 4067 fec_ptp_init(pdev, irq_cnt); 4068 4069 ret = fec_enet_init(ndev); 4070 if (ret) 4071 goto failed_init; 4072 4073 for (i = 0; i < irq_cnt; i++) { 4074 snprintf(irq_name, sizeof(irq_name), "int%d", i); 4075 irq = platform_get_irq_byname_optional(pdev, irq_name); 4076 if (irq < 0) 4077 irq = platform_get_irq(pdev, i); 4078 if (irq < 0) { 4079 ret = irq; 4080 goto failed_irq; 4081 } 4082 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt, 4083 0, pdev->name, ndev); 4084 if (ret) 4085 goto failed_irq; 4086 4087 fep->irq[i] = irq; 4088 } 4089 4090 /* Decide which interrupt line is wakeup capable */ 4091 fec_enet_get_wakeup_irq(pdev); 4092 4093 ret = fec_enet_mii_init(pdev); 4094 if (ret) 4095 goto failed_mii_init; 4096 4097 /* Carrier starts down, phylib will bring it up */ 4098 netif_carrier_off(ndev); 4099 fec_enet_clk_enable(ndev, false); 4100 pinctrl_pm_select_sleep_state(&pdev->dev); 4101 4102 ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN; 4103 4104 ret = register_netdev(ndev); 4105 if (ret) 4106 goto failed_register; 4107 4108 device_init_wakeup(&ndev->dev, fep->wol_flag & 4109 FEC_WOL_HAS_MAGIC_PACKET); 4110 4111 if (fep->bufdesc_ex && fep->ptp_clock) 4112 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); 4113 4114 fep->rx_copybreak = COPYBREAK_DEFAULT; 4115 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work); 4116 4117 pm_runtime_mark_last_busy(&pdev->dev); 4118 pm_runtime_put_autosuspend(&pdev->dev); 4119 4120 return 0; 4121 4122 failed_register: 4123 fec_enet_mii_remove(fep); 4124 failed_mii_init: 4125 failed_irq: 4126 failed_init: 4127 fec_ptp_stop(pdev); 4128 failed_reset: 4129 pm_runtime_put_noidle(&pdev->dev); 4130 pm_runtime_disable(&pdev->dev); 4131 if (fep->reg_phy) 4132 regulator_disable(fep->reg_phy); 4133 failed_regulator: 4134 clk_disable_unprepare(fep->clk_ahb); 4135 failed_clk_ahb: 4136 clk_disable_unprepare(fep->clk_ipg); 4137 failed_clk_ipg: 4138 fec_enet_clk_enable(ndev, false); 4139 failed_clk: 4140 failed_rgmii_delay: 4141 if (of_phy_is_fixed_link(np)) 4142 of_phy_deregister_fixed_link(np); 4143 of_node_put(phy_node); 4144 failed_stop_mode: 4145 failed_ipc_init: 4146 failed_phy: 4147 dev_id--; 4148 failed_ioremap: 4149 free_netdev(ndev); 4150 4151 return ret; 4152 } 4153 4154 static int 4155 fec_drv_remove(struct platform_device *pdev) 4156 { 4157 struct net_device *ndev = platform_get_drvdata(pdev); 4158 struct fec_enet_private *fep = netdev_priv(ndev); 4159 struct device_node *np = pdev->dev.of_node; 4160 int ret; 4161 4162 ret = pm_runtime_resume_and_get(&pdev->dev); 4163 if (ret < 0) 4164 return ret; 4165 4166 cancel_work_sync(&fep->tx_timeout_work); 4167 fec_ptp_stop(pdev); 4168 unregister_netdev(ndev); 4169 fec_enet_mii_remove(fep); 4170 if (fep->reg_phy) 4171 regulator_disable(fep->reg_phy); 4172 4173 if (of_phy_is_fixed_link(np)) 4174 of_phy_deregister_fixed_link(np); 4175 of_node_put(fep->phy_node); 4176 4177 clk_disable_unprepare(fep->clk_ahb); 4178 clk_disable_unprepare(fep->clk_ipg); 4179 pm_runtime_put_noidle(&pdev->dev); 4180 pm_runtime_disable(&pdev->dev); 4181 4182 free_netdev(ndev); 4183 return 0; 4184 } 4185 4186 static int __maybe_unused fec_suspend(struct device *dev) 4187 { 4188 struct net_device *ndev = dev_get_drvdata(dev); 4189 struct fec_enet_private *fep = netdev_priv(ndev); 4190 int ret; 4191 4192 rtnl_lock(); 4193 if (netif_running(ndev)) { 4194 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) 4195 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON; 4196 phy_stop(ndev->phydev); 4197 napi_disable(&fep->napi); 4198 netif_tx_lock_bh(ndev); 4199 netif_device_detach(ndev); 4200 netif_tx_unlock_bh(ndev); 4201 fec_stop(ndev); 4202 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) { 4203 fec_irqs_disable(ndev); 4204 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 4205 } else { 4206 fec_irqs_disable_except_wakeup(ndev); 4207 if (fep->wake_irq > 0) { 4208 disable_irq(fep->wake_irq); 4209 enable_irq_wake(fep->wake_irq); 4210 } 4211 fec_enet_stop_mode(fep, true); 4212 } 4213 /* It's safe to disable clocks since interrupts are masked */ 4214 fec_enet_clk_enable(ndev, false); 4215 4216 fep->rpm_active = !pm_runtime_status_suspended(dev); 4217 if (fep->rpm_active) { 4218 ret = pm_runtime_force_suspend(dev); 4219 if (ret < 0) { 4220 rtnl_unlock(); 4221 return ret; 4222 } 4223 } 4224 } 4225 rtnl_unlock(); 4226 4227 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) 4228 regulator_disable(fep->reg_phy); 4229 4230 /* SOC supply clock to phy, when clock is disabled, phy link down 4231 * SOC control phy regulator, when regulator is disabled, phy link down 4232 */ 4233 if (fep->clk_enet_out || fep->reg_phy) 4234 fep->link = 0; 4235 4236 return 0; 4237 } 4238 4239 static int __maybe_unused fec_resume(struct device *dev) 4240 { 4241 struct net_device *ndev = dev_get_drvdata(dev); 4242 struct fec_enet_private *fep = netdev_priv(ndev); 4243 int ret; 4244 int val; 4245 4246 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) { 4247 ret = regulator_enable(fep->reg_phy); 4248 if (ret) 4249 return ret; 4250 } 4251 4252 rtnl_lock(); 4253 if (netif_running(ndev)) { 4254 if (fep->rpm_active) 4255 pm_runtime_force_resume(dev); 4256 4257 ret = fec_enet_clk_enable(ndev, true); 4258 if (ret) { 4259 rtnl_unlock(); 4260 goto failed_clk; 4261 } 4262 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) { 4263 fec_enet_stop_mode(fep, false); 4264 if (fep->wake_irq) { 4265 disable_irq_wake(fep->wake_irq); 4266 enable_irq(fep->wake_irq); 4267 } 4268 4269 val = readl(fep->hwp + FEC_ECNTRL); 4270 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 4271 writel(val, fep->hwp + FEC_ECNTRL); 4272 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON; 4273 } else { 4274 pinctrl_pm_select_default_state(&fep->pdev->dev); 4275 } 4276 fec_restart(ndev); 4277 netif_tx_lock_bh(ndev); 4278 netif_device_attach(ndev); 4279 netif_tx_unlock_bh(ndev); 4280 napi_enable(&fep->napi); 4281 phy_init_hw(ndev->phydev); 4282 phy_start(ndev->phydev); 4283 } 4284 rtnl_unlock(); 4285 4286 return 0; 4287 4288 failed_clk: 4289 if (fep->reg_phy) 4290 regulator_disable(fep->reg_phy); 4291 return ret; 4292 } 4293 4294 static int __maybe_unused fec_runtime_suspend(struct device *dev) 4295 { 4296 struct net_device *ndev = dev_get_drvdata(dev); 4297 struct fec_enet_private *fep = netdev_priv(ndev); 4298 4299 clk_disable_unprepare(fep->clk_ahb); 4300 clk_disable_unprepare(fep->clk_ipg); 4301 4302 return 0; 4303 } 4304 4305 static int __maybe_unused fec_runtime_resume(struct device *dev) 4306 { 4307 struct net_device *ndev = dev_get_drvdata(dev); 4308 struct fec_enet_private *fep = netdev_priv(ndev); 4309 int ret; 4310 4311 ret = clk_prepare_enable(fep->clk_ahb); 4312 if (ret) 4313 return ret; 4314 ret = clk_prepare_enable(fep->clk_ipg); 4315 if (ret) 4316 goto failed_clk_ipg; 4317 4318 return 0; 4319 4320 failed_clk_ipg: 4321 clk_disable_unprepare(fep->clk_ahb); 4322 return ret; 4323 } 4324 4325 static const struct dev_pm_ops fec_pm_ops = { 4326 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume) 4327 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL) 4328 }; 4329 4330 static struct platform_driver fec_driver = { 4331 .driver = { 4332 .name = DRIVER_NAME, 4333 .pm = &fec_pm_ops, 4334 .of_match_table = fec_dt_ids, 4335 .suppress_bind_attrs = true, 4336 }, 4337 .id_table = fec_devtype, 4338 .probe = fec_probe, 4339 .remove = fec_drv_remove, 4340 }; 4341 4342 module_platform_driver(fec_driver); 4343 4344 MODULE_LICENSE("GPL"); 4345