1 /* 2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx. 3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net) 4 * 5 * Right now, I am very wasteful with the buffers. I allocate memory 6 * pages and then divide them into 2K frame buffers. This way I know I 7 * have buffers large enough to hold one frame within one buffer descriptor. 8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which 9 * will be much more memory efficient and will easily handle lots of 10 * small packets. 11 * 12 * Much better multiple PHY support by Magnus Damm. 13 * Copyright (c) 2000 Ericsson Radio Systems AB. 14 * 15 * Support for FEC controller of ColdFire processors. 16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com) 17 * 18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be) 19 * Copyright (c) 2004-2006 Macq Electronique SA. 20 * 21 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. 22 */ 23 24 #include <linux/module.h> 25 #include <linux/kernel.h> 26 #include <linux/string.h> 27 #include <linux/ptrace.h> 28 #include <linux/errno.h> 29 #include <linux/ioport.h> 30 #include <linux/slab.h> 31 #include <linux/interrupt.h> 32 #include <linux/delay.h> 33 #include <linux/netdevice.h> 34 #include <linux/etherdevice.h> 35 #include <linux/skbuff.h> 36 #include <linux/in.h> 37 #include <linux/ip.h> 38 #include <net/ip.h> 39 #include <net/tso.h> 40 #include <linux/tcp.h> 41 #include <linux/udp.h> 42 #include <linux/icmp.h> 43 #include <linux/spinlock.h> 44 #include <linux/workqueue.h> 45 #include <linux/bitops.h> 46 #include <linux/io.h> 47 #include <linux/irq.h> 48 #include <linux/clk.h> 49 #include <linux/platform_device.h> 50 #include <linux/phy.h> 51 #include <linux/fec.h> 52 #include <linux/of.h> 53 #include <linux/of_device.h> 54 #include <linux/of_gpio.h> 55 #include <linux/of_mdio.h> 56 #include <linux/of_net.h> 57 #include <linux/regulator/consumer.h> 58 #include <linux/if_vlan.h> 59 #include <linux/pinctrl/consumer.h> 60 #include <linux/prefetch.h> 61 62 #include <asm/cacheflush.h> 63 64 #include "fec.h" 65 66 static void set_multicast_list(struct net_device *ndev); 67 static void fec_enet_itr_coal_init(struct net_device *ndev); 68 69 #define DRIVER_NAME "fec" 70 71 #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0)) 72 73 /* Pause frame feild and FIFO threshold */ 74 #define FEC_ENET_FCE (1 << 5) 75 #define FEC_ENET_RSEM_V 0x84 76 #define FEC_ENET_RSFL_V 16 77 #define FEC_ENET_RAEM_V 0x8 78 #define FEC_ENET_RAFL_V 0x8 79 #define FEC_ENET_OPD_V 0xFFF0 80 81 static struct platform_device_id fec_devtype[] = { 82 { 83 /* keep it for coldfire */ 84 .name = DRIVER_NAME, 85 .driver_data = 0, 86 }, { 87 .name = "imx25-fec", 88 .driver_data = FEC_QUIRK_USE_GASKET, 89 }, { 90 .name = "imx27-fec", 91 .driver_data = 0, 92 }, { 93 .name = "imx28-fec", 94 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME, 95 }, { 96 .name = "imx6q-fec", 97 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 98 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 99 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358, 100 }, { 101 .name = "mvf600-fec", 102 .driver_data = FEC_QUIRK_ENET_MAC, 103 }, { 104 .name = "imx6sx-fec", 105 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 106 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 107 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 108 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE, 109 }, { 110 /* sentinel */ 111 } 112 }; 113 MODULE_DEVICE_TABLE(platform, fec_devtype); 114 115 enum imx_fec_type { 116 IMX25_FEC = 1, /* runs on i.mx25/50/53 */ 117 IMX27_FEC, /* runs on i.mx27/35/51 */ 118 IMX28_FEC, 119 IMX6Q_FEC, 120 MVF600_FEC, 121 IMX6SX_FEC, 122 }; 123 124 static const struct of_device_id fec_dt_ids[] = { 125 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, 126 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, 127 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, 128 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, 129 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, 130 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], }, 131 { /* sentinel */ } 132 }; 133 MODULE_DEVICE_TABLE(of, fec_dt_ids); 134 135 static unsigned char macaddr[ETH_ALEN]; 136 module_param_array(macaddr, byte, NULL, 0); 137 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); 138 139 #if defined(CONFIG_M5272) 140 /* 141 * Some hardware gets it MAC address out of local flash memory. 142 * if this is non-zero then assume it is the address to get MAC from. 143 */ 144 #if defined(CONFIG_NETtel) 145 #define FEC_FLASHMAC 0xf0006006 146 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) 147 #define FEC_FLASHMAC 0xf0006000 148 #elif defined(CONFIG_CANCam) 149 #define FEC_FLASHMAC 0xf0020000 150 #elif defined (CONFIG_M5272C3) 151 #define FEC_FLASHMAC (0xffe04000 + 4) 152 #elif defined(CONFIG_MOD5272) 153 #define FEC_FLASHMAC 0xffc0406b 154 #else 155 #define FEC_FLASHMAC 0 156 #endif 157 #endif /* CONFIG_M5272 */ 158 159 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets. 160 */ 161 #define PKT_MAXBUF_SIZE 1522 162 #define PKT_MINBUF_SIZE 64 163 #define PKT_MAXBLR_SIZE 1536 164 165 /* FEC receive acceleration */ 166 #define FEC_RACC_IPDIS (1 << 1) 167 #define FEC_RACC_PRODIS (1 << 2) 168 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS) 169 170 /* 171 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame 172 * size bits. Other FEC hardware does not, so we need to take that into 173 * account when setting it. 174 */ 175 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 176 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) 177 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) 178 #else 179 #define OPT_FRAME_SIZE 0 180 #endif 181 182 /* FEC MII MMFR bits definition */ 183 #define FEC_MMFR_ST (1 << 30) 184 #define FEC_MMFR_OP_READ (2 << 28) 185 #define FEC_MMFR_OP_WRITE (1 << 28) 186 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23) 187 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18) 188 #define FEC_MMFR_TA (2 << 16) 189 #define FEC_MMFR_DATA(v) (v & 0xffff) 190 191 #define FEC_MII_TIMEOUT 30000 /* us */ 192 193 /* Transmitter timeout */ 194 #define TX_TIMEOUT (2 * HZ) 195 196 #define FEC_PAUSE_FLAG_AUTONEG 0x1 197 #define FEC_PAUSE_FLAG_ENABLE 0x2 198 199 #define COPYBREAK_DEFAULT 256 200 201 #define TSO_HEADER_SIZE 128 202 /* Max number of allowed TCP segments for software TSO */ 203 #define FEC_MAX_TSO_SEGS 100 204 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 205 206 #define IS_TSO_HEADER(txq, addr) \ 207 ((addr >= txq->tso_hdrs_dma) && \ 208 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE)) 209 210 static int mii_cnt; 211 212 static inline 213 struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, 214 struct fec_enet_private *fep, 215 int queue_id) 216 { 217 struct bufdesc *new_bd = bdp + 1; 218 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1; 219 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id]; 220 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id]; 221 struct bufdesc_ex *ex_base; 222 struct bufdesc *base; 223 int ring_size; 224 225 if (bdp >= txq->tx_bd_base) { 226 base = txq->tx_bd_base; 227 ring_size = txq->tx_ring_size; 228 ex_base = (struct bufdesc_ex *)txq->tx_bd_base; 229 } else { 230 base = rxq->rx_bd_base; 231 ring_size = rxq->rx_ring_size; 232 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base; 233 } 234 235 if (fep->bufdesc_ex) 236 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ? 237 ex_base : ex_new_bd); 238 else 239 return (new_bd >= (base + ring_size)) ? 240 base : new_bd; 241 } 242 243 static inline 244 struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, 245 struct fec_enet_private *fep, 246 int queue_id) 247 { 248 struct bufdesc *new_bd = bdp - 1; 249 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1; 250 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id]; 251 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id]; 252 struct bufdesc_ex *ex_base; 253 struct bufdesc *base; 254 int ring_size; 255 256 if (bdp >= txq->tx_bd_base) { 257 base = txq->tx_bd_base; 258 ring_size = txq->tx_ring_size; 259 ex_base = (struct bufdesc_ex *)txq->tx_bd_base; 260 } else { 261 base = rxq->rx_bd_base; 262 ring_size = rxq->rx_ring_size; 263 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base; 264 } 265 266 if (fep->bufdesc_ex) 267 return (struct bufdesc *)((ex_new_bd < ex_base) ? 268 (ex_new_bd + ring_size) : ex_new_bd); 269 else 270 return (new_bd < base) ? (new_bd + ring_size) : new_bd; 271 } 272 273 static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp, 274 struct fec_enet_private *fep) 275 { 276 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size; 277 } 278 279 static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep, 280 struct fec_enet_priv_tx_q *txq) 281 { 282 int entries; 283 284 entries = ((const char *)txq->dirty_tx - 285 (const char *)txq->cur_tx) / fep->bufdesc_size - 1; 286 287 return entries > 0 ? entries : entries + txq->tx_ring_size; 288 } 289 290 static void *swap_buffer(void *bufaddr, int len) 291 { 292 int i; 293 unsigned int *buf = bufaddr; 294 295 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++) 296 *buf = cpu_to_be32(*buf); 297 298 return bufaddr; 299 } 300 301 static void fec_dump(struct net_device *ndev) 302 { 303 struct fec_enet_private *fep = netdev_priv(ndev); 304 struct bufdesc *bdp; 305 struct fec_enet_priv_tx_q *txq; 306 int index = 0; 307 308 netdev_info(ndev, "TX ring dump\n"); 309 pr_info("Nr SC addr len SKB\n"); 310 311 txq = fep->tx_queue[0]; 312 bdp = txq->tx_bd_base; 313 314 do { 315 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n", 316 index, 317 bdp == txq->cur_tx ? 'S' : ' ', 318 bdp == txq->dirty_tx ? 'H' : ' ', 319 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen, 320 txq->tx_skbuff[index]); 321 bdp = fec_enet_get_nextdesc(bdp, fep, 0); 322 index++; 323 } while (bdp != txq->tx_bd_base); 324 } 325 326 static inline bool is_ipv4_pkt(struct sk_buff *skb) 327 { 328 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4; 329 } 330 331 static int 332 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) 333 { 334 /* Only run for packets requiring a checksum. */ 335 if (skb->ip_summed != CHECKSUM_PARTIAL) 336 return 0; 337 338 if (unlikely(skb_cow_head(skb, 0))) 339 return -1; 340 341 if (is_ipv4_pkt(skb)) 342 ip_hdr(skb)->check = 0; 343 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; 344 345 return 0; 346 } 347 348 static int 349 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq, 350 struct sk_buff *skb, 351 struct net_device *ndev) 352 { 353 struct fec_enet_private *fep = netdev_priv(ndev); 354 const struct platform_device_id *id_entry = 355 platform_get_device_id(fep->pdev); 356 struct bufdesc *bdp = txq->cur_tx; 357 struct bufdesc_ex *ebdp; 358 int nr_frags = skb_shinfo(skb)->nr_frags; 359 unsigned short queue = skb_get_queue_mapping(skb); 360 int frag, frag_len; 361 unsigned short status; 362 unsigned int estatus = 0; 363 skb_frag_t *this_frag; 364 unsigned int index; 365 void *bufaddr; 366 dma_addr_t addr; 367 int i; 368 369 for (frag = 0; frag < nr_frags; frag++) { 370 this_frag = &skb_shinfo(skb)->frags[frag]; 371 bdp = fec_enet_get_nextdesc(bdp, fep, queue); 372 ebdp = (struct bufdesc_ex *)bdp; 373 374 status = bdp->cbd_sc; 375 status &= ~BD_ENET_TX_STATS; 376 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 377 frag_len = skb_shinfo(skb)->frags[frag].size; 378 379 /* Handle the last BD specially */ 380 if (frag == nr_frags - 1) { 381 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 382 if (fep->bufdesc_ex) { 383 estatus |= BD_ENET_TX_INT; 384 if (unlikely(skb_shinfo(skb)->tx_flags & 385 SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 386 estatus |= BD_ENET_TX_TS; 387 } 388 } 389 390 if (fep->bufdesc_ex) { 391 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) 392 estatus |= FEC_TX_BD_FTYPE(queue); 393 if (skb->ip_summed == CHECKSUM_PARTIAL) 394 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 395 ebdp->cbd_bdu = 0; 396 ebdp->cbd_esc = estatus; 397 } 398 399 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset; 400 401 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep); 402 if (((unsigned long) bufaddr) & fep->tx_align || 403 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { 404 memcpy(txq->tx_bounce[index], bufaddr, frag_len); 405 bufaddr = txq->tx_bounce[index]; 406 407 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) 408 swap_buffer(bufaddr, frag_len); 409 } 410 411 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len, 412 DMA_TO_DEVICE); 413 if (dma_mapping_error(&fep->pdev->dev, addr)) { 414 dev_kfree_skb_any(skb); 415 if (net_ratelimit()) 416 netdev_err(ndev, "Tx DMA memory map failed\n"); 417 goto dma_mapping_error; 418 } 419 420 bdp->cbd_bufaddr = addr; 421 bdp->cbd_datlen = frag_len; 422 bdp->cbd_sc = status; 423 } 424 425 txq->cur_tx = bdp; 426 427 return 0; 428 429 dma_mapping_error: 430 bdp = txq->cur_tx; 431 for (i = 0; i < frag; i++) { 432 bdp = fec_enet_get_nextdesc(bdp, fep, queue); 433 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, 434 bdp->cbd_datlen, DMA_TO_DEVICE); 435 } 436 return NETDEV_TX_OK; 437 } 438 439 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, 440 struct sk_buff *skb, struct net_device *ndev) 441 { 442 struct fec_enet_private *fep = netdev_priv(ndev); 443 const struct platform_device_id *id_entry = 444 platform_get_device_id(fep->pdev); 445 int nr_frags = skb_shinfo(skb)->nr_frags; 446 struct bufdesc *bdp, *last_bdp; 447 void *bufaddr; 448 dma_addr_t addr; 449 unsigned short status; 450 unsigned short buflen; 451 unsigned short queue; 452 unsigned int estatus = 0; 453 unsigned int index; 454 int entries_free; 455 int ret; 456 457 entries_free = fec_enet_get_free_txdesc_num(fep, txq); 458 if (entries_free < MAX_SKB_FRAGS + 1) { 459 dev_kfree_skb_any(skb); 460 if (net_ratelimit()) 461 netdev_err(ndev, "NOT enough BD for SG!\n"); 462 return NETDEV_TX_OK; 463 } 464 465 /* Protocol checksum off-load for TCP and UDP. */ 466 if (fec_enet_clear_csum(skb, ndev)) { 467 dev_kfree_skb_any(skb); 468 return NETDEV_TX_OK; 469 } 470 471 /* Fill in a Tx ring entry */ 472 bdp = txq->cur_tx; 473 status = bdp->cbd_sc; 474 status &= ~BD_ENET_TX_STATS; 475 476 /* Set buffer length and buffer pointer */ 477 bufaddr = skb->data; 478 buflen = skb_headlen(skb); 479 480 queue = skb_get_queue_mapping(skb); 481 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep); 482 if (((unsigned long) bufaddr) & fep->tx_align || 483 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { 484 memcpy(txq->tx_bounce[index], skb->data, buflen); 485 bufaddr = txq->tx_bounce[index]; 486 487 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) 488 swap_buffer(bufaddr, buflen); 489 } 490 491 /* Push the data cache so the CPM does not get stale memory data. */ 492 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE); 493 if (dma_mapping_error(&fep->pdev->dev, addr)) { 494 dev_kfree_skb_any(skb); 495 if (net_ratelimit()) 496 netdev_err(ndev, "Tx DMA memory map failed\n"); 497 return NETDEV_TX_OK; 498 } 499 500 if (nr_frags) { 501 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev); 502 if (ret) 503 return ret; 504 } else { 505 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 506 if (fep->bufdesc_ex) { 507 estatus = BD_ENET_TX_INT; 508 if (unlikely(skb_shinfo(skb)->tx_flags & 509 SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 510 estatus |= BD_ENET_TX_TS; 511 } 512 } 513 514 if (fep->bufdesc_ex) { 515 516 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 517 518 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 519 fep->hwts_tx_en)) 520 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 521 522 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) 523 estatus |= FEC_TX_BD_FTYPE(queue); 524 525 if (skb->ip_summed == CHECKSUM_PARTIAL) 526 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 527 528 ebdp->cbd_bdu = 0; 529 ebdp->cbd_esc = estatus; 530 } 531 532 last_bdp = txq->cur_tx; 533 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep); 534 /* Save skb pointer */ 535 txq->tx_skbuff[index] = skb; 536 537 bdp->cbd_datlen = buflen; 538 bdp->cbd_bufaddr = addr; 539 540 /* Send it on its way. Tell FEC it's ready, interrupt when done, 541 * it's the last BD of the frame, and to put the CRC on the end. 542 */ 543 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); 544 bdp->cbd_sc = status; 545 546 /* If this was the last BD in the ring, start at the beginning again. */ 547 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue); 548 549 skb_tx_timestamp(skb); 550 551 txq->cur_tx = bdp; 552 553 /* Trigger transmission start */ 554 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue)); 555 556 return 0; 557 } 558 559 static int 560 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb, 561 struct net_device *ndev, 562 struct bufdesc *bdp, int index, char *data, 563 int size, bool last_tcp, bool is_last) 564 { 565 struct fec_enet_private *fep = netdev_priv(ndev); 566 const struct platform_device_id *id_entry = 567 platform_get_device_id(fep->pdev); 568 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 569 unsigned short queue = skb_get_queue_mapping(skb); 570 unsigned short status; 571 unsigned int estatus = 0; 572 dma_addr_t addr; 573 574 status = bdp->cbd_sc; 575 status &= ~BD_ENET_TX_STATS; 576 577 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 578 579 if (((unsigned long) data) & fep->tx_align || 580 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { 581 memcpy(txq->tx_bounce[index], data, size); 582 data = txq->tx_bounce[index]; 583 584 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) 585 swap_buffer(data, size); 586 } 587 588 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE); 589 if (dma_mapping_error(&fep->pdev->dev, addr)) { 590 dev_kfree_skb_any(skb); 591 if (net_ratelimit()) 592 netdev_err(ndev, "Tx DMA memory map failed\n"); 593 return NETDEV_TX_BUSY; 594 } 595 596 bdp->cbd_datlen = size; 597 bdp->cbd_bufaddr = addr; 598 599 if (fep->bufdesc_ex) { 600 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) 601 estatus |= FEC_TX_BD_FTYPE(queue); 602 if (skb->ip_summed == CHECKSUM_PARTIAL) 603 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 604 ebdp->cbd_bdu = 0; 605 ebdp->cbd_esc = estatus; 606 } 607 608 /* Handle the last BD specially */ 609 if (last_tcp) 610 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC); 611 if (is_last) { 612 status |= BD_ENET_TX_INTR; 613 if (fep->bufdesc_ex) 614 ebdp->cbd_esc |= BD_ENET_TX_INT; 615 } 616 617 bdp->cbd_sc = status; 618 619 return 0; 620 } 621 622 static int 623 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq, 624 struct sk_buff *skb, struct net_device *ndev, 625 struct bufdesc *bdp, int index) 626 { 627 struct fec_enet_private *fep = netdev_priv(ndev); 628 const struct platform_device_id *id_entry = 629 platform_get_device_id(fep->pdev); 630 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 631 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 632 unsigned short queue = skb_get_queue_mapping(skb); 633 void *bufaddr; 634 unsigned long dmabuf; 635 unsigned short status; 636 unsigned int estatus = 0; 637 638 status = bdp->cbd_sc; 639 status &= ~BD_ENET_TX_STATS; 640 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 641 642 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 643 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE; 644 if (((unsigned long)bufaddr) & fep->tx_align || 645 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) { 646 memcpy(txq->tx_bounce[index], skb->data, hdr_len); 647 bufaddr = txq->tx_bounce[index]; 648 649 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) 650 swap_buffer(bufaddr, hdr_len); 651 652 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr, 653 hdr_len, DMA_TO_DEVICE); 654 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) { 655 dev_kfree_skb_any(skb); 656 if (net_ratelimit()) 657 netdev_err(ndev, "Tx DMA memory map failed\n"); 658 return NETDEV_TX_BUSY; 659 } 660 } 661 662 bdp->cbd_bufaddr = dmabuf; 663 bdp->cbd_datlen = hdr_len; 664 665 if (fep->bufdesc_ex) { 666 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) 667 estatus |= FEC_TX_BD_FTYPE(queue); 668 if (skb->ip_summed == CHECKSUM_PARTIAL) 669 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 670 ebdp->cbd_bdu = 0; 671 ebdp->cbd_esc = estatus; 672 } 673 674 bdp->cbd_sc = status; 675 676 return 0; 677 } 678 679 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq, 680 struct sk_buff *skb, 681 struct net_device *ndev) 682 { 683 struct fec_enet_private *fep = netdev_priv(ndev); 684 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 685 int total_len, data_left; 686 struct bufdesc *bdp = txq->cur_tx; 687 unsigned short queue = skb_get_queue_mapping(skb); 688 struct tso_t tso; 689 unsigned int index = 0; 690 int ret; 691 const struct platform_device_id *id_entry = 692 platform_get_device_id(fep->pdev); 693 694 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) { 695 dev_kfree_skb_any(skb); 696 if (net_ratelimit()) 697 netdev_err(ndev, "NOT enough BD for TSO!\n"); 698 return NETDEV_TX_OK; 699 } 700 701 /* Protocol checksum off-load for TCP and UDP. */ 702 if (fec_enet_clear_csum(skb, ndev)) { 703 dev_kfree_skb_any(skb); 704 return NETDEV_TX_OK; 705 } 706 707 /* Initialize the TSO handler, and prepare the first payload */ 708 tso_start(skb, &tso); 709 710 total_len = skb->len - hdr_len; 711 while (total_len > 0) { 712 char *hdr; 713 714 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep); 715 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 716 total_len -= data_left; 717 718 /* prepare packet headers: MAC + IP + TCP */ 719 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 720 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 721 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index); 722 if (ret) 723 goto err_release; 724 725 while (data_left > 0) { 726 int size; 727 728 size = min_t(int, tso.size, data_left); 729 bdp = fec_enet_get_nextdesc(bdp, fep, queue); 730 index = fec_enet_get_bd_index(txq->tx_bd_base, 731 bdp, fep); 732 ret = fec_enet_txq_put_data_tso(txq, skb, ndev, 733 bdp, index, 734 tso.data, size, 735 size == data_left, 736 total_len == 0); 737 if (ret) 738 goto err_release; 739 740 data_left -= size; 741 tso_build_data(skb, &tso, size); 742 } 743 744 bdp = fec_enet_get_nextdesc(bdp, fep, queue); 745 } 746 747 /* Save skb pointer */ 748 txq->tx_skbuff[index] = skb; 749 750 skb_tx_timestamp(skb); 751 txq->cur_tx = bdp; 752 753 /* Trigger transmission start */ 754 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) || 755 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) || 756 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) || 757 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) || 758 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue))) 759 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue)); 760 761 return 0; 762 763 err_release: 764 /* TODO: Release all used data descriptors for TSO */ 765 return ret; 766 } 767 768 static netdev_tx_t 769 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) 770 { 771 struct fec_enet_private *fep = netdev_priv(ndev); 772 int entries_free; 773 unsigned short queue; 774 struct fec_enet_priv_tx_q *txq; 775 struct netdev_queue *nq; 776 int ret; 777 778 queue = skb_get_queue_mapping(skb); 779 txq = fep->tx_queue[queue]; 780 nq = netdev_get_tx_queue(ndev, queue); 781 782 if (skb_is_gso(skb)) 783 ret = fec_enet_txq_submit_tso(txq, skb, ndev); 784 else 785 ret = fec_enet_txq_submit_skb(txq, skb, ndev); 786 if (ret) 787 return ret; 788 789 entries_free = fec_enet_get_free_txdesc_num(fep, txq); 790 if (entries_free <= txq->tx_stop_threshold) 791 netif_tx_stop_queue(nq); 792 793 return NETDEV_TX_OK; 794 } 795 796 /* Init RX & TX buffer descriptors 797 */ 798 static void fec_enet_bd_init(struct net_device *dev) 799 { 800 struct fec_enet_private *fep = netdev_priv(dev); 801 struct fec_enet_priv_tx_q *txq; 802 struct fec_enet_priv_rx_q *rxq; 803 struct bufdesc *bdp; 804 unsigned int i; 805 unsigned int q; 806 807 for (q = 0; q < fep->num_rx_queues; q++) { 808 /* Initialize the receive buffer descriptors. */ 809 rxq = fep->rx_queue[q]; 810 bdp = rxq->rx_bd_base; 811 812 for (i = 0; i < rxq->rx_ring_size; i++) { 813 814 /* Initialize the BD for every fragment in the page. */ 815 if (bdp->cbd_bufaddr) 816 bdp->cbd_sc = BD_ENET_RX_EMPTY; 817 else 818 bdp->cbd_sc = 0; 819 bdp = fec_enet_get_nextdesc(bdp, fep, q); 820 } 821 822 /* Set the last buffer to wrap */ 823 bdp = fec_enet_get_prevdesc(bdp, fep, q); 824 bdp->cbd_sc |= BD_SC_WRAP; 825 826 rxq->cur_rx = rxq->rx_bd_base; 827 } 828 829 for (q = 0; q < fep->num_tx_queues; q++) { 830 /* ...and the same for transmit */ 831 txq = fep->tx_queue[q]; 832 bdp = txq->tx_bd_base; 833 txq->cur_tx = bdp; 834 835 for (i = 0; i < txq->tx_ring_size; i++) { 836 /* Initialize the BD for every fragment in the page. */ 837 bdp->cbd_sc = 0; 838 if (txq->tx_skbuff[i]) { 839 dev_kfree_skb_any(txq->tx_skbuff[i]); 840 txq->tx_skbuff[i] = NULL; 841 } 842 bdp->cbd_bufaddr = 0; 843 bdp = fec_enet_get_nextdesc(bdp, fep, q); 844 } 845 846 /* Set the last buffer to wrap */ 847 bdp = fec_enet_get_prevdesc(bdp, fep, q); 848 bdp->cbd_sc |= BD_SC_WRAP; 849 txq->dirty_tx = bdp; 850 } 851 } 852 853 static void fec_enet_active_rxring(struct net_device *ndev) 854 { 855 struct fec_enet_private *fep = netdev_priv(ndev); 856 int i; 857 858 for (i = 0; i < fep->num_rx_queues; i++) 859 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i)); 860 } 861 862 static void fec_enet_enable_ring(struct net_device *ndev) 863 { 864 struct fec_enet_private *fep = netdev_priv(ndev); 865 struct fec_enet_priv_tx_q *txq; 866 struct fec_enet_priv_rx_q *rxq; 867 int i; 868 869 for (i = 0; i < fep->num_rx_queues; i++) { 870 rxq = fep->rx_queue[i]; 871 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i)); 872 873 /* enable DMA1/2 */ 874 if (i) 875 writel(RCMR_MATCHEN | RCMR_CMP(i), 876 fep->hwp + FEC_RCMR(i)); 877 } 878 879 for (i = 0; i < fep->num_tx_queues; i++) { 880 txq = fep->tx_queue[i]; 881 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i)); 882 883 /* enable DMA1/2 */ 884 if (i) 885 writel(DMA_CLASS_EN | IDLE_SLOPE(i), 886 fep->hwp + FEC_DMA_CFG(i)); 887 } 888 } 889 890 static void fec_enet_reset_skb(struct net_device *ndev) 891 { 892 struct fec_enet_private *fep = netdev_priv(ndev); 893 struct fec_enet_priv_tx_q *txq; 894 int i, j; 895 896 for (i = 0; i < fep->num_tx_queues; i++) { 897 txq = fep->tx_queue[i]; 898 899 for (j = 0; j < txq->tx_ring_size; j++) { 900 if (txq->tx_skbuff[j]) { 901 dev_kfree_skb_any(txq->tx_skbuff[j]); 902 txq->tx_skbuff[j] = NULL; 903 } 904 } 905 } 906 } 907 908 /* 909 * This function is called to start or restart the FEC during a link 910 * change, transmit timeout, or to reconfigure the FEC. The network 911 * packet processing for this device must be stopped before this call. 912 */ 913 static void 914 fec_restart(struct net_device *ndev) 915 { 916 struct fec_enet_private *fep = netdev_priv(ndev); 917 const struct platform_device_id *id_entry = 918 platform_get_device_id(fep->pdev); 919 u32 val; 920 u32 temp_mac[2]; 921 u32 rcntl = OPT_FRAME_SIZE | 0x04; 922 u32 ecntl = 0x2; /* ETHEREN */ 923 924 /* Whack a reset. We should wait for this. 925 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 926 * instead of reset MAC itself. 927 */ 928 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) { 929 writel(0, fep->hwp + FEC_ECNTRL); 930 } else { 931 writel(1, fep->hwp + FEC_ECNTRL); 932 udelay(10); 933 } 934 935 /* 936 * enet-mac reset will reset mac address registers too, 937 * so need to reconfigure it. 938 */ 939 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { 940 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); 941 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW); 942 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH); 943 } 944 945 /* Clear any outstanding interrupt. */ 946 writel(0xffc00000, fep->hwp + FEC_IEVENT); 947 948 /* Set maximum receive buffer size. */ 949 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE); 950 951 fec_enet_bd_init(ndev); 952 953 fec_enet_enable_ring(ndev); 954 955 /* Reset tx SKB buffers. */ 956 fec_enet_reset_skb(ndev); 957 958 /* Enable MII mode */ 959 if (fep->full_duplex == DUPLEX_FULL) { 960 /* FD enable */ 961 writel(0x04, fep->hwp + FEC_X_CNTRL); 962 } else { 963 /* No Rcv on Xmit */ 964 rcntl |= 0x02; 965 writel(0x0, fep->hwp + FEC_X_CNTRL); 966 } 967 968 /* Set MII speed */ 969 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 970 971 #if !defined(CONFIG_M5272) 972 /* set RX checksum */ 973 val = readl(fep->hwp + FEC_RACC); 974 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) 975 val |= FEC_RACC_OPTIONS; 976 else 977 val &= ~FEC_RACC_OPTIONS; 978 writel(val, fep->hwp + FEC_RACC); 979 #endif 980 981 /* 982 * The phy interface and speed need to get configured 983 * differently on enet-mac. 984 */ 985 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { 986 /* Enable flow control and length check */ 987 rcntl |= 0x40000000 | 0x00000020; 988 989 /* RGMII, RMII or MII */ 990 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII) 991 rcntl |= (1 << 6); 992 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 993 rcntl |= (1 << 8); 994 else 995 rcntl &= ~(1 << 8); 996 997 /* 1G, 100M or 10M */ 998 if (fep->phy_dev) { 999 if (fep->phy_dev->speed == SPEED_1000) 1000 ecntl |= (1 << 5); 1001 else if (fep->phy_dev->speed == SPEED_100) 1002 rcntl &= ~(1 << 9); 1003 else 1004 rcntl |= (1 << 9); 1005 } 1006 } else { 1007 #ifdef FEC_MIIGSK_ENR 1008 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) { 1009 u32 cfgr; 1010 /* disable the gasket and wait */ 1011 writel(0, fep->hwp + FEC_MIIGSK_ENR); 1012 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) 1013 udelay(1); 1014 1015 /* 1016 * configure the gasket: 1017 * RMII, 50 MHz, no loopback, no echo 1018 * MII, 25 MHz, no loopback, no echo 1019 */ 1020 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1021 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; 1022 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10) 1023 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; 1024 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); 1025 1026 /* re-enable the gasket */ 1027 writel(2, fep->hwp + FEC_MIIGSK_ENR); 1028 } 1029 #endif 1030 } 1031 1032 #if !defined(CONFIG_M5272) 1033 /* enable pause frame*/ 1034 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || 1035 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && 1036 fep->phy_dev && fep->phy_dev->pause)) { 1037 rcntl |= FEC_ENET_FCE; 1038 1039 /* set FIFO threshold parameter to reduce overrun */ 1040 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); 1041 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); 1042 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); 1043 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); 1044 1045 /* OPD */ 1046 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); 1047 } else { 1048 rcntl &= ~FEC_ENET_FCE; 1049 } 1050 #endif /* !defined(CONFIG_M5272) */ 1051 1052 writel(rcntl, fep->hwp + FEC_R_CNTRL); 1053 1054 /* Setup multicast filter. */ 1055 set_multicast_list(ndev); 1056 #ifndef CONFIG_M5272 1057 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); 1058 writel(0, fep->hwp + FEC_HASH_TABLE_LOW); 1059 #endif 1060 1061 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { 1062 /* enable ENET endian swap */ 1063 ecntl |= (1 << 8); 1064 /* enable ENET store and forward mode */ 1065 writel(1 << 8, fep->hwp + FEC_X_WMRK); 1066 } 1067 1068 if (fep->bufdesc_ex) 1069 ecntl |= (1 << 4); 1070 1071 #ifndef CONFIG_M5272 1072 /* Enable the MIB statistic event counters */ 1073 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT); 1074 #endif 1075 1076 /* And last, enable the transmit and receive processing */ 1077 writel(ecntl, fep->hwp + FEC_ECNTRL); 1078 fec_enet_active_rxring(ndev); 1079 1080 if (fep->bufdesc_ex) 1081 fec_ptp_start_cyclecounter(ndev); 1082 1083 /* Enable interrupts we wish to service */ 1084 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1085 1086 /* Init the interrupt coalescing */ 1087 fec_enet_itr_coal_init(ndev); 1088 1089 } 1090 1091 static void 1092 fec_stop(struct net_device *ndev) 1093 { 1094 struct fec_enet_private *fep = netdev_priv(ndev); 1095 const struct platform_device_id *id_entry = 1096 platform_get_device_id(fep->pdev); 1097 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8); 1098 1099 /* We cannot expect a graceful transmit stop without link !!! */ 1100 if (fep->link) { 1101 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ 1102 udelay(10); 1103 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) 1104 netdev_err(ndev, "Graceful transmit stop did not complete!\n"); 1105 } 1106 1107 /* Whack a reset. We should wait for this. 1108 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1109 * instead of reset MAC itself. 1110 */ 1111 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) { 1112 writel(0, fep->hwp + FEC_ECNTRL); 1113 } else { 1114 writel(1, fep->hwp + FEC_ECNTRL); 1115 udelay(10); 1116 } 1117 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1118 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1119 1120 /* We have to keep ENET enabled to have MII interrupt stay working */ 1121 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) { 1122 writel(2, fep->hwp + FEC_ECNTRL); 1123 writel(rmii_mode, fep->hwp + FEC_R_CNTRL); 1124 } 1125 } 1126 1127 1128 static void 1129 fec_timeout(struct net_device *ndev) 1130 { 1131 struct fec_enet_private *fep = netdev_priv(ndev); 1132 1133 fec_dump(ndev); 1134 1135 ndev->stats.tx_errors++; 1136 1137 schedule_work(&fep->tx_timeout_work); 1138 } 1139 1140 static void fec_enet_timeout_work(struct work_struct *work) 1141 { 1142 struct fec_enet_private *fep = 1143 container_of(work, struct fec_enet_private, tx_timeout_work); 1144 struct net_device *ndev = fep->netdev; 1145 1146 rtnl_lock(); 1147 if (netif_device_present(ndev) || netif_running(ndev)) { 1148 napi_disable(&fep->napi); 1149 netif_tx_lock_bh(ndev); 1150 fec_restart(ndev); 1151 netif_wake_queue(ndev); 1152 netif_tx_unlock_bh(ndev); 1153 napi_enable(&fep->napi); 1154 } 1155 rtnl_unlock(); 1156 } 1157 1158 static void 1159 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts, 1160 struct skb_shared_hwtstamps *hwtstamps) 1161 { 1162 unsigned long flags; 1163 u64 ns; 1164 1165 spin_lock_irqsave(&fep->tmreg_lock, flags); 1166 ns = timecounter_cyc2time(&fep->tc, ts); 1167 spin_unlock_irqrestore(&fep->tmreg_lock, flags); 1168 1169 memset(hwtstamps, 0, sizeof(*hwtstamps)); 1170 hwtstamps->hwtstamp = ns_to_ktime(ns); 1171 } 1172 1173 static void 1174 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id) 1175 { 1176 struct fec_enet_private *fep; 1177 struct bufdesc *bdp; 1178 unsigned short status; 1179 struct sk_buff *skb; 1180 struct fec_enet_priv_tx_q *txq; 1181 struct netdev_queue *nq; 1182 int index = 0; 1183 int entries_free; 1184 1185 fep = netdev_priv(ndev); 1186 1187 queue_id = FEC_ENET_GET_QUQUE(queue_id); 1188 1189 txq = fep->tx_queue[queue_id]; 1190 /* get next bdp of dirty_tx */ 1191 nq = netdev_get_tx_queue(ndev, queue_id); 1192 bdp = txq->dirty_tx; 1193 1194 /* get next bdp of dirty_tx */ 1195 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id); 1196 1197 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) { 1198 1199 /* current queue is empty */ 1200 if (bdp == txq->cur_tx) 1201 break; 1202 1203 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep); 1204 1205 skb = txq->tx_skbuff[index]; 1206 txq->tx_skbuff[index] = NULL; 1207 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr)) 1208 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, 1209 bdp->cbd_datlen, DMA_TO_DEVICE); 1210 bdp->cbd_bufaddr = 0; 1211 if (!skb) { 1212 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id); 1213 continue; 1214 } 1215 1216 /* Check for errors. */ 1217 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | 1218 BD_ENET_TX_RL | BD_ENET_TX_UN | 1219 BD_ENET_TX_CSL)) { 1220 ndev->stats.tx_errors++; 1221 if (status & BD_ENET_TX_HB) /* No heartbeat */ 1222 ndev->stats.tx_heartbeat_errors++; 1223 if (status & BD_ENET_TX_LC) /* Late collision */ 1224 ndev->stats.tx_window_errors++; 1225 if (status & BD_ENET_TX_RL) /* Retrans limit */ 1226 ndev->stats.tx_aborted_errors++; 1227 if (status & BD_ENET_TX_UN) /* Underrun */ 1228 ndev->stats.tx_fifo_errors++; 1229 if (status & BD_ENET_TX_CSL) /* Carrier lost */ 1230 ndev->stats.tx_carrier_errors++; 1231 } else { 1232 ndev->stats.tx_packets++; 1233 ndev->stats.tx_bytes += skb->len; 1234 } 1235 1236 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) && 1237 fep->bufdesc_ex) { 1238 struct skb_shared_hwtstamps shhwtstamps; 1239 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1240 1241 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps); 1242 skb_tstamp_tx(skb, &shhwtstamps); 1243 } 1244 1245 /* Deferred means some collisions occurred during transmit, 1246 * but we eventually sent the packet OK. 1247 */ 1248 if (status & BD_ENET_TX_DEF) 1249 ndev->stats.collisions++; 1250 1251 /* Free the sk buffer associated with this last transmit */ 1252 dev_kfree_skb_any(skb); 1253 1254 txq->dirty_tx = bdp; 1255 1256 /* Update pointer to next buffer descriptor to be transmitted */ 1257 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id); 1258 1259 /* Since we have freed up a buffer, the ring is no longer full 1260 */ 1261 if (netif_queue_stopped(ndev)) { 1262 entries_free = fec_enet_get_free_txdesc_num(fep, txq); 1263 if (entries_free >= txq->tx_wake_threshold) 1264 netif_tx_wake_queue(nq); 1265 } 1266 } 1267 1268 /* ERR006538: Keep the transmitter going */ 1269 if (bdp != txq->cur_tx && 1270 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0) 1271 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id)); 1272 } 1273 1274 static void 1275 fec_enet_tx(struct net_device *ndev) 1276 { 1277 struct fec_enet_private *fep = netdev_priv(ndev); 1278 u16 queue_id; 1279 /* First process class A queue, then Class B and Best Effort queue */ 1280 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) { 1281 clear_bit(queue_id, &fep->work_tx); 1282 fec_enet_tx_queue(ndev, queue_id); 1283 } 1284 return; 1285 } 1286 1287 static int 1288 fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb) 1289 { 1290 struct fec_enet_private *fep = netdev_priv(ndev); 1291 int off; 1292 1293 off = ((unsigned long)skb->data) & fep->rx_align; 1294 if (off) 1295 skb_reserve(skb, fep->rx_align + 1 - off); 1296 1297 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data, 1298 FEC_ENET_RX_FRSIZE - fep->rx_align, 1299 DMA_FROM_DEVICE); 1300 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) { 1301 if (net_ratelimit()) 1302 netdev_err(ndev, "Rx DMA memory map failed\n"); 1303 return -ENOMEM; 1304 } 1305 1306 return 0; 1307 } 1308 1309 static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb, 1310 struct bufdesc *bdp, u32 length) 1311 { 1312 struct fec_enet_private *fep = netdev_priv(ndev); 1313 struct sk_buff *new_skb; 1314 1315 if (length > fep->rx_copybreak) 1316 return false; 1317 1318 new_skb = netdev_alloc_skb(ndev, length); 1319 if (!new_skb) 1320 return false; 1321 1322 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr, 1323 FEC_ENET_RX_FRSIZE - fep->rx_align, 1324 DMA_FROM_DEVICE); 1325 memcpy(new_skb->data, (*skb)->data, length); 1326 *skb = new_skb; 1327 1328 return true; 1329 } 1330 1331 /* During a receive, the cur_rx points to the current incoming buffer. 1332 * When we update through the ring, if the next incoming buffer has 1333 * not been given to the system, we just set the empty indicator, 1334 * effectively tossing the packet. 1335 */ 1336 static int 1337 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) 1338 { 1339 struct fec_enet_private *fep = netdev_priv(ndev); 1340 const struct platform_device_id *id_entry = 1341 platform_get_device_id(fep->pdev); 1342 struct fec_enet_priv_rx_q *rxq; 1343 struct bufdesc *bdp; 1344 unsigned short status; 1345 struct sk_buff *skb_new = NULL; 1346 struct sk_buff *skb; 1347 ushort pkt_len; 1348 __u8 *data; 1349 int pkt_received = 0; 1350 struct bufdesc_ex *ebdp = NULL; 1351 bool vlan_packet_rcvd = false; 1352 u16 vlan_tag; 1353 int index = 0; 1354 bool is_copybreak; 1355 1356 #ifdef CONFIG_M532x 1357 flush_cache_all(); 1358 #endif 1359 queue_id = FEC_ENET_GET_QUQUE(queue_id); 1360 rxq = fep->rx_queue[queue_id]; 1361 1362 /* First, grab all of the stats for the incoming packet. 1363 * These get messed up if we get called due to a busy condition. 1364 */ 1365 bdp = rxq->cur_rx; 1366 1367 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) { 1368 1369 if (pkt_received >= budget) 1370 break; 1371 pkt_received++; 1372 1373 /* Since we have allocated space to hold a complete frame, 1374 * the last indicator should be set. 1375 */ 1376 if ((status & BD_ENET_RX_LAST) == 0) 1377 netdev_err(ndev, "rcv is not +last\n"); 1378 1379 1380 /* Check for errors. */ 1381 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | 1382 BD_ENET_RX_CR | BD_ENET_RX_OV)) { 1383 ndev->stats.rx_errors++; 1384 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) { 1385 /* Frame too long or too short. */ 1386 ndev->stats.rx_length_errors++; 1387 } 1388 if (status & BD_ENET_RX_NO) /* Frame alignment */ 1389 ndev->stats.rx_frame_errors++; 1390 if (status & BD_ENET_RX_CR) /* CRC Error */ 1391 ndev->stats.rx_crc_errors++; 1392 if (status & BD_ENET_RX_OV) /* FIFO overrun */ 1393 ndev->stats.rx_fifo_errors++; 1394 } 1395 1396 /* Report late collisions as a frame error. 1397 * On this error, the BD is closed, but we don't know what we 1398 * have in the buffer. So, just drop this frame on the floor. 1399 */ 1400 if (status & BD_ENET_RX_CL) { 1401 ndev->stats.rx_errors++; 1402 ndev->stats.rx_frame_errors++; 1403 goto rx_processing_done; 1404 } 1405 1406 /* Process the incoming frame. */ 1407 ndev->stats.rx_packets++; 1408 pkt_len = bdp->cbd_datlen; 1409 ndev->stats.rx_bytes += pkt_len; 1410 1411 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep); 1412 skb = rxq->rx_skbuff[index]; 1413 1414 /* The packet length includes FCS, but we don't want to 1415 * include that when passing upstream as it messes up 1416 * bridging applications. 1417 */ 1418 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4); 1419 if (!is_copybreak) { 1420 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); 1421 if (unlikely(!skb_new)) { 1422 ndev->stats.rx_dropped++; 1423 goto rx_processing_done; 1424 } 1425 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, 1426 FEC_ENET_RX_FRSIZE - fep->rx_align, 1427 DMA_FROM_DEVICE); 1428 } 1429 1430 prefetch(skb->data - NET_IP_ALIGN); 1431 skb_put(skb, pkt_len - 4); 1432 data = skb->data; 1433 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) 1434 swap_buffer(data, pkt_len); 1435 1436 /* Extract the enhanced buffer descriptor */ 1437 ebdp = NULL; 1438 if (fep->bufdesc_ex) 1439 ebdp = (struct bufdesc_ex *)bdp; 1440 1441 /* If this is a VLAN packet remove the VLAN Tag */ 1442 vlan_packet_rcvd = false; 1443 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) && 1444 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) { 1445 /* Push and remove the vlan tag */ 1446 struct vlan_hdr *vlan_header = 1447 (struct vlan_hdr *) (data + ETH_HLEN); 1448 vlan_tag = ntohs(vlan_header->h_vlan_TCI); 1449 1450 vlan_packet_rcvd = true; 1451 1452 skb_copy_to_linear_data_offset(skb, VLAN_HLEN, 1453 data, (2 * ETH_ALEN)); 1454 skb_pull(skb, VLAN_HLEN); 1455 } 1456 1457 skb->protocol = eth_type_trans(skb, ndev); 1458 1459 /* Get receive timestamp from the skb */ 1460 if (fep->hwts_rx_en && fep->bufdesc_ex) 1461 fec_enet_hwtstamp(fep, ebdp->ts, 1462 skb_hwtstamps(skb)); 1463 1464 if (fep->bufdesc_ex && 1465 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { 1466 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) { 1467 /* don't check it */ 1468 skb->ip_summed = CHECKSUM_UNNECESSARY; 1469 } else { 1470 skb_checksum_none_assert(skb); 1471 } 1472 } 1473 1474 /* Handle received VLAN packets */ 1475 if (vlan_packet_rcvd) 1476 __vlan_hwaccel_put_tag(skb, 1477 htons(ETH_P_8021Q), 1478 vlan_tag); 1479 1480 napi_gro_receive(&fep->napi, skb); 1481 1482 if (is_copybreak) { 1483 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr, 1484 FEC_ENET_RX_FRSIZE - fep->rx_align, 1485 DMA_FROM_DEVICE); 1486 } else { 1487 rxq->rx_skbuff[index] = skb_new; 1488 fec_enet_new_rxbdp(ndev, bdp, skb_new); 1489 } 1490 1491 rx_processing_done: 1492 /* Clear the status flags for this buffer */ 1493 status &= ~BD_ENET_RX_STATS; 1494 1495 /* Mark the buffer empty */ 1496 status |= BD_ENET_RX_EMPTY; 1497 bdp->cbd_sc = status; 1498 1499 if (fep->bufdesc_ex) { 1500 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1501 1502 ebdp->cbd_esc = BD_ENET_RX_INT; 1503 ebdp->cbd_prot = 0; 1504 ebdp->cbd_bdu = 0; 1505 } 1506 1507 /* Update BD pointer to next entry */ 1508 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id); 1509 1510 /* Doing this here will keep the FEC running while we process 1511 * incoming frames. On a heavily loaded network, we should be 1512 * able to keep up at the expense of system resources. 1513 */ 1514 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id)); 1515 } 1516 rxq->cur_rx = bdp; 1517 return pkt_received; 1518 } 1519 1520 static int 1521 fec_enet_rx(struct net_device *ndev, int budget) 1522 { 1523 int pkt_received = 0; 1524 u16 queue_id; 1525 struct fec_enet_private *fep = netdev_priv(ndev); 1526 1527 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) { 1528 clear_bit(queue_id, &fep->work_rx); 1529 pkt_received += fec_enet_rx_queue(ndev, 1530 budget - pkt_received, queue_id); 1531 } 1532 return pkt_received; 1533 } 1534 1535 static bool 1536 fec_enet_collect_events(struct fec_enet_private *fep, uint int_events) 1537 { 1538 if (int_events == 0) 1539 return false; 1540 1541 if (int_events & FEC_ENET_RXF) 1542 fep->work_rx |= (1 << 2); 1543 if (int_events & FEC_ENET_RXF_1) 1544 fep->work_rx |= (1 << 0); 1545 if (int_events & FEC_ENET_RXF_2) 1546 fep->work_rx |= (1 << 1); 1547 1548 if (int_events & FEC_ENET_TXF) 1549 fep->work_tx |= (1 << 2); 1550 if (int_events & FEC_ENET_TXF_1) 1551 fep->work_tx |= (1 << 0); 1552 if (int_events & FEC_ENET_TXF_2) 1553 fep->work_tx |= (1 << 1); 1554 1555 return true; 1556 } 1557 1558 static irqreturn_t 1559 fec_enet_interrupt(int irq, void *dev_id) 1560 { 1561 struct net_device *ndev = dev_id; 1562 struct fec_enet_private *fep = netdev_priv(ndev); 1563 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF; 1564 uint int_events; 1565 irqreturn_t ret = IRQ_NONE; 1566 1567 int_events = readl(fep->hwp + FEC_IEVENT); 1568 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT); 1569 fec_enet_collect_events(fep, int_events); 1570 1571 if (int_events & napi_mask) { 1572 ret = IRQ_HANDLED; 1573 1574 /* Disable the NAPI interrupts */ 1575 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK); 1576 napi_schedule(&fep->napi); 1577 } 1578 1579 if (int_events & FEC_ENET_MII) { 1580 ret = IRQ_HANDLED; 1581 complete(&fep->mdio_done); 1582 } 1583 1584 if (fep->ptp_clock) 1585 fec_ptp_check_pps_event(fep); 1586 1587 return ret; 1588 } 1589 1590 static int fec_enet_rx_napi(struct napi_struct *napi, int budget) 1591 { 1592 struct net_device *ndev = napi->dev; 1593 struct fec_enet_private *fep = netdev_priv(ndev); 1594 int pkts; 1595 1596 /* 1597 * Clear any pending transmit or receive interrupts before 1598 * processing the rings to avoid racing with the hardware. 1599 */ 1600 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT); 1601 1602 pkts = fec_enet_rx(ndev, budget); 1603 1604 fec_enet_tx(ndev); 1605 1606 if (pkts < budget) { 1607 napi_complete(napi); 1608 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1609 } 1610 return pkts; 1611 } 1612 1613 /* ------------------------------------------------------------------------- */ 1614 static void fec_get_mac(struct net_device *ndev) 1615 { 1616 struct fec_enet_private *fep = netdev_priv(ndev); 1617 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev); 1618 unsigned char *iap, tmpaddr[ETH_ALEN]; 1619 1620 /* 1621 * try to get mac address in following order: 1622 * 1623 * 1) module parameter via kernel command line in form 1624 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 1625 */ 1626 iap = macaddr; 1627 1628 /* 1629 * 2) from device tree data 1630 */ 1631 if (!is_valid_ether_addr(iap)) { 1632 struct device_node *np = fep->pdev->dev.of_node; 1633 if (np) { 1634 const char *mac = of_get_mac_address(np); 1635 if (mac) 1636 iap = (unsigned char *) mac; 1637 } 1638 } 1639 1640 /* 1641 * 3) from flash or fuse (via platform data) 1642 */ 1643 if (!is_valid_ether_addr(iap)) { 1644 #ifdef CONFIG_M5272 1645 if (FEC_FLASHMAC) 1646 iap = (unsigned char *)FEC_FLASHMAC; 1647 #else 1648 if (pdata) 1649 iap = (unsigned char *)&pdata->mac; 1650 #endif 1651 } 1652 1653 /* 1654 * 4) FEC mac registers set by bootloader 1655 */ 1656 if (!is_valid_ether_addr(iap)) { 1657 *((__be32 *) &tmpaddr[0]) = 1658 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW)); 1659 *((__be16 *) &tmpaddr[4]) = 1660 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); 1661 iap = &tmpaddr[0]; 1662 } 1663 1664 /* 1665 * 5) random mac address 1666 */ 1667 if (!is_valid_ether_addr(iap)) { 1668 /* Report it and use a random ethernet address instead */ 1669 netdev_err(ndev, "Invalid MAC address: %pM\n", iap); 1670 eth_hw_addr_random(ndev); 1671 netdev_info(ndev, "Using random MAC address: %pM\n", 1672 ndev->dev_addr); 1673 return; 1674 } 1675 1676 memcpy(ndev->dev_addr, iap, ETH_ALEN); 1677 1678 /* Adjust MAC if using macaddr */ 1679 if (iap == macaddr) 1680 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id; 1681 } 1682 1683 /* ------------------------------------------------------------------------- */ 1684 1685 /* 1686 * Phy section 1687 */ 1688 static void fec_enet_adjust_link(struct net_device *ndev) 1689 { 1690 struct fec_enet_private *fep = netdev_priv(ndev); 1691 struct phy_device *phy_dev = fep->phy_dev; 1692 int status_change = 0; 1693 1694 /* Prevent a state halted on mii error */ 1695 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) { 1696 phy_dev->state = PHY_RESUMING; 1697 return; 1698 } 1699 1700 /* 1701 * If the netdev is down, or is going down, we're not interested 1702 * in link state events, so just mark our idea of the link as down 1703 * and ignore the event. 1704 */ 1705 if (!netif_running(ndev) || !netif_device_present(ndev)) { 1706 fep->link = 0; 1707 } else if (phy_dev->link) { 1708 if (!fep->link) { 1709 fep->link = phy_dev->link; 1710 status_change = 1; 1711 } 1712 1713 if (fep->full_duplex != phy_dev->duplex) { 1714 fep->full_duplex = phy_dev->duplex; 1715 status_change = 1; 1716 } 1717 1718 if (phy_dev->speed != fep->speed) { 1719 fep->speed = phy_dev->speed; 1720 status_change = 1; 1721 } 1722 1723 /* if any of the above changed restart the FEC */ 1724 if (status_change) { 1725 napi_disable(&fep->napi); 1726 netif_tx_lock_bh(ndev); 1727 fec_restart(ndev); 1728 netif_wake_queue(ndev); 1729 netif_tx_unlock_bh(ndev); 1730 napi_enable(&fep->napi); 1731 } 1732 } else { 1733 if (fep->link) { 1734 napi_disable(&fep->napi); 1735 netif_tx_lock_bh(ndev); 1736 fec_stop(ndev); 1737 netif_tx_unlock_bh(ndev); 1738 napi_enable(&fep->napi); 1739 fep->link = phy_dev->link; 1740 status_change = 1; 1741 } 1742 } 1743 1744 if (status_change) 1745 phy_print_status(phy_dev); 1746 } 1747 1748 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum) 1749 { 1750 struct fec_enet_private *fep = bus->priv; 1751 unsigned long time_left; 1752 1753 fep->mii_timeout = 0; 1754 init_completion(&fep->mdio_done); 1755 1756 /* start a read op */ 1757 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ | 1758 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | 1759 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); 1760 1761 /* wait for end of transfer */ 1762 time_left = wait_for_completion_timeout(&fep->mdio_done, 1763 usecs_to_jiffies(FEC_MII_TIMEOUT)); 1764 if (time_left == 0) { 1765 fep->mii_timeout = 1; 1766 netdev_err(fep->netdev, "MDIO read timeout\n"); 1767 return -ETIMEDOUT; 1768 } 1769 1770 /* return value */ 1771 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); 1772 } 1773 1774 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum, 1775 u16 value) 1776 { 1777 struct fec_enet_private *fep = bus->priv; 1778 unsigned long time_left; 1779 1780 fep->mii_timeout = 0; 1781 init_completion(&fep->mdio_done); 1782 1783 /* start a write op */ 1784 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE | 1785 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) | 1786 FEC_MMFR_TA | FEC_MMFR_DATA(value), 1787 fep->hwp + FEC_MII_DATA); 1788 1789 /* wait for end of transfer */ 1790 time_left = wait_for_completion_timeout(&fep->mdio_done, 1791 usecs_to_jiffies(FEC_MII_TIMEOUT)); 1792 if (time_left == 0) { 1793 fep->mii_timeout = 1; 1794 netdev_err(fep->netdev, "MDIO write timeout\n"); 1795 return -ETIMEDOUT; 1796 } 1797 1798 return 0; 1799 } 1800 1801 static int fec_enet_clk_enable(struct net_device *ndev, bool enable) 1802 { 1803 struct fec_enet_private *fep = netdev_priv(ndev); 1804 int ret; 1805 1806 if (enable) { 1807 ret = clk_prepare_enable(fep->clk_ahb); 1808 if (ret) 1809 return ret; 1810 ret = clk_prepare_enable(fep->clk_ipg); 1811 if (ret) 1812 goto failed_clk_ipg; 1813 if (fep->clk_enet_out) { 1814 ret = clk_prepare_enable(fep->clk_enet_out); 1815 if (ret) 1816 goto failed_clk_enet_out; 1817 } 1818 if (fep->clk_ptp) { 1819 mutex_lock(&fep->ptp_clk_mutex); 1820 ret = clk_prepare_enable(fep->clk_ptp); 1821 if (ret) { 1822 mutex_unlock(&fep->ptp_clk_mutex); 1823 goto failed_clk_ptp; 1824 } else { 1825 fep->ptp_clk_on = true; 1826 } 1827 mutex_unlock(&fep->ptp_clk_mutex); 1828 } 1829 if (fep->clk_ref) { 1830 ret = clk_prepare_enable(fep->clk_ref); 1831 if (ret) 1832 goto failed_clk_ref; 1833 } 1834 } else { 1835 clk_disable_unprepare(fep->clk_ahb); 1836 clk_disable_unprepare(fep->clk_ipg); 1837 if (fep->clk_enet_out) 1838 clk_disable_unprepare(fep->clk_enet_out); 1839 if (fep->clk_ptp) { 1840 mutex_lock(&fep->ptp_clk_mutex); 1841 clk_disable_unprepare(fep->clk_ptp); 1842 fep->ptp_clk_on = false; 1843 mutex_unlock(&fep->ptp_clk_mutex); 1844 } 1845 if (fep->clk_ref) 1846 clk_disable_unprepare(fep->clk_ref); 1847 } 1848 1849 return 0; 1850 1851 failed_clk_ref: 1852 if (fep->clk_ref) 1853 clk_disable_unprepare(fep->clk_ref); 1854 failed_clk_ptp: 1855 if (fep->clk_enet_out) 1856 clk_disable_unprepare(fep->clk_enet_out); 1857 failed_clk_enet_out: 1858 clk_disable_unprepare(fep->clk_ipg); 1859 failed_clk_ipg: 1860 clk_disable_unprepare(fep->clk_ahb); 1861 1862 return ret; 1863 } 1864 1865 static int fec_enet_mii_probe(struct net_device *ndev) 1866 { 1867 struct fec_enet_private *fep = netdev_priv(ndev); 1868 const struct platform_device_id *id_entry = 1869 platform_get_device_id(fep->pdev); 1870 struct phy_device *phy_dev = NULL; 1871 char mdio_bus_id[MII_BUS_ID_SIZE]; 1872 char phy_name[MII_BUS_ID_SIZE + 3]; 1873 int phy_id; 1874 int dev_id = fep->dev_id; 1875 1876 fep->phy_dev = NULL; 1877 1878 if (fep->phy_node) { 1879 phy_dev = of_phy_connect(ndev, fep->phy_node, 1880 &fec_enet_adjust_link, 0, 1881 fep->phy_interface); 1882 } else { 1883 /* check for attached phy */ 1884 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { 1885 if ((fep->mii_bus->phy_mask & (1 << phy_id))) 1886 continue; 1887 if (fep->mii_bus->phy_map[phy_id] == NULL) 1888 continue; 1889 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0) 1890 continue; 1891 if (dev_id--) 1892 continue; 1893 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); 1894 break; 1895 } 1896 1897 if (phy_id >= PHY_MAX_ADDR) { 1898 netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); 1899 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); 1900 phy_id = 0; 1901 } 1902 1903 snprintf(phy_name, sizeof(phy_name), 1904 PHY_ID_FMT, mdio_bus_id, phy_id); 1905 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 1906 fep->phy_interface); 1907 } 1908 1909 if (IS_ERR(phy_dev)) { 1910 netdev_err(ndev, "could not attach to PHY\n"); 1911 return PTR_ERR(phy_dev); 1912 } 1913 1914 /* mask with MAC supported features */ 1915 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) { 1916 phy_dev->supported &= PHY_GBIT_FEATURES; 1917 phy_dev->supported &= ~SUPPORTED_1000baseT_Half; 1918 #if !defined(CONFIG_M5272) 1919 phy_dev->supported |= SUPPORTED_Pause; 1920 #endif 1921 } 1922 else 1923 phy_dev->supported &= PHY_BASIC_FEATURES; 1924 1925 phy_dev->advertising = phy_dev->supported; 1926 1927 fep->phy_dev = phy_dev; 1928 fep->link = 0; 1929 fep->full_duplex = 0; 1930 1931 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n", 1932 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev), 1933 fep->phy_dev->irq); 1934 1935 return 0; 1936 } 1937 1938 static int fec_enet_mii_init(struct platform_device *pdev) 1939 { 1940 static struct mii_bus *fec0_mii_bus; 1941 struct net_device *ndev = platform_get_drvdata(pdev); 1942 struct fec_enet_private *fep = netdev_priv(ndev); 1943 const struct platform_device_id *id_entry = 1944 platform_get_device_id(fep->pdev); 1945 struct device_node *node; 1946 int err = -ENXIO, i; 1947 1948 /* 1949 * The dual fec interfaces are not equivalent with enet-mac. 1950 * Here are the differences: 1951 * 1952 * - fec0 supports MII & RMII modes while fec1 only supports RMII 1953 * - fec0 acts as the 1588 time master while fec1 is slave 1954 * - external phys can only be configured by fec0 1955 * 1956 * That is to say fec1 can not work independently. It only works 1957 * when fec0 is working. The reason behind this design is that the 1958 * second interface is added primarily for Switch mode. 1959 * 1960 * Because of the last point above, both phys are attached on fec0 1961 * mdio interface in board design, and need to be configured by 1962 * fec0 mii_bus. 1963 */ 1964 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) { 1965 /* fec1 uses fec0 mii_bus */ 1966 if (mii_cnt && fec0_mii_bus) { 1967 fep->mii_bus = fec0_mii_bus; 1968 mii_cnt++; 1969 return 0; 1970 } 1971 return -ENOENT; 1972 } 1973 1974 fep->mii_timeout = 0; 1975 1976 /* 1977 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed) 1978 * 1979 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while 1980 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 1981 * Reference Manual has an error on this, and gets fixed on i.MX6Q 1982 * document. 1983 */ 1984 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000); 1985 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) 1986 fep->phy_speed--; 1987 fep->phy_speed <<= 1; 1988 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1989 1990 fep->mii_bus = mdiobus_alloc(); 1991 if (fep->mii_bus == NULL) { 1992 err = -ENOMEM; 1993 goto err_out; 1994 } 1995 1996 fep->mii_bus->name = "fec_enet_mii_bus"; 1997 fep->mii_bus->read = fec_enet_mdio_read; 1998 fep->mii_bus->write = fec_enet_mdio_write; 1999 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 2000 pdev->name, fep->dev_id + 1); 2001 fep->mii_bus->priv = fep; 2002 fep->mii_bus->parent = &pdev->dev; 2003 2004 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); 2005 if (!fep->mii_bus->irq) { 2006 err = -ENOMEM; 2007 goto err_out_free_mdiobus; 2008 } 2009 2010 for (i = 0; i < PHY_MAX_ADDR; i++) 2011 fep->mii_bus->irq[i] = PHY_POLL; 2012 2013 node = of_get_child_by_name(pdev->dev.of_node, "mdio"); 2014 if (node) { 2015 err = of_mdiobus_register(fep->mii_bus, node); 2016 of_node_put(node); 2017 } else { 2018 err = mdiobus_register(fep->mii_bus); 2019 } 2020 2021 if (err) 2022 goto err_out_free_mdio_irq; 2023 2024 mii_cnt++; 2025 2026 /* save fec0 mii_bus */ 2027 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) 2028 fec0_mii_bus = fep->mii_bus; 2029 2030 return 0; 2031 2032 err_out_free_mdio_irq: 2033 kfree(fep->mii_bus->irq); 2034 err_out_free_mdiobus: 2035 mdiobus_free(fep->mii_bus); 2036 err_out: 2037 return err; 2038 } 2039 2040 static void fec_enet_mii_remove(struct fec_enet_private *fep) 2041 { 2042 if (--mii_cnt == 0) { 2043 mdiobus_unregister(fep->mii_bus); 2044 kfree(fep->mii_bus->irq); 2045 mdiobus_free(fep->mii_bus); 2046 } 2047 } 2048 2049 static int fec_enet_get_settings(struct net_device *ndev, 2050 struct ethtool_cmd *cmd) 2051 { 2052 struct fec_enet_private *fep = netdev_priv(ndev); 2053 struct phy_device *phydev = fep->phy_dev; 2054 2055 if (!phydev) 2056 return -ENODEV; 2057 2058 return phy_ethtool_gset(phydev, cmd); 2059 } 2060 2061 static int fec_enet_set_settings(struct net_device *ndev, 2062 struct ethtool_cmd *cmd) 2063 { 2064 struct fec_enet_private *fep = netdev_priv(ndev); 2065 struct phy_device *phydev = fep->phy_dev; 2066 2067 if (!phydev) 2068 return -ENODEV; 2069 2070 return phy_ethtool_sset(phydev, cmd); 2071 } 2072 2073 static void fec_enet_get_drvinfo(struct net_device *ndev, 2074 struct ethtool_drvinfo *info) 2075 { 2076 struct fec_enet_private *fep = netdev_priv(ndev); 2077 2078 strlcpy(info->driver, fep->pdev->dev.driver->name, 2079 sizeof(info->driver)); 2080 strlcpy(info->version, "Revision: 1.0", sizeof(info->version)); 2081 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); 2082 } 2083 2084 static int fec_enet_get_ts_info(struct net_device *ndev, 2085 struct ethtool_ts_info *info) 2086 { 2087 struct fec_enet_private *fep = netdev_priv(ndev); 2088 2089 if (fep->bufdesc_ex) { 2090 2091 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 2092 SOF_TIMESTAMPING_RX_SOFTWARE | 2093 SOF_TIMESTAMPING_SOFTWARE | 2094 SOF_TIMESTAMPING_TX_HARDWARE | 2095 SOF_TIMESTAMPING_RX_HARDWARE | 2096 SOF_TIMESTAMPING_RAW_HARDWARE; 2097 if (fep->ptp_clock) 2098 info->phc_index = ptp_clock_index(fep->ptp_clock); 2099 else 2100 info->phc_index = -1; 2101 2102 info->tx_types = (1 << HWTSTAMP_TX_OFF) | 2103 (1 << HWTSTAMP_TX_ON); 2104 2105 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 2106 (1 << HWTSTAMP_FILTER_ALL); 2107 return 0; 2108 } else { 2109 return ethtool_op_get_ts_info(ndev, info); 2110 } 2111 } 2112 2113 #if !defined(CONFIG_M5272) 2114 2115 static void fec_enet_get_pauseparam(struct net_device *ndev, 2116 struct ethtool_pauseparam *pause) 2117 { 2118 struct fec_enet_private *fep = netdev_priv(ndev); 2119 2120 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; 2121 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; 2122 pause->rx_pause = pause->tx_pause; 2123 } 2124 2125 static int fec_enet_set_pauseparam(struct net_device *ndev, 2126 struct ethtool_pauseparam *pause) 2127 { 2128 struct fec_enet_private *fep = netdev_priv(ndev); 2129 2130 if (!fep->phy_dev) 2131 return -ENODEV; 2132 2133 if (pause->tx_pause != pause->rx_pause) { 2134 netdev_info(ndev, 2135 "hardware only support enable/disable both tx and rx"); 2136 return -EINVAL; 2137 } 2138 2139 fep->pause_flag = 0; 2140 2141 /* tx pause must be same as rx pause */ 2142 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; 2143 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; 2144 2145 if (pause->rx_pause || pause->autoneg) { 2146 fep->phy_dev->supported |= ADVERTISED_Pause; 2147 fep->phy_dev->advertising |= ADVERTISED_Pause; 2148 } else { 2149 fep->phy_dev->supported &= ~ADVERTISED_Pause; 2150 fep->phy_dev->advertising &= ~ADVERTISED_Pause; 2151 } 2152 2153 if (pause->autoneg) { 2154 if (netif_running(ndev)) 2155 fec_stop(ndev); 2156 phy_start_aneg(fep->phy_dev); 2157 } 2158 if (netif_running(ndev)) { 2159 napi_disable(&fep->napi); 2160 netif_tx_lock_bh(ndev); 2161 fec_restart(ndev); 2162 netif_wake_queue(ndev); 2163 netif_tx_unlock_bh(ndev); 2164 napi_enable(&fep->napi); 2165 } 2166 2167 return 0; 2168 } 2169 2170 static const struct fec_stat { 2171 char name[ETH_GSTRING_LEN]; 2172 u16 offset; 2173 } fec_stats[] = { 2174 /* RMON TX */ 2175 { "tx_dropped", RMON_T_DROP }, 2176 { "tx_packets", RMON_T_PACKETS }, 2177 { "tx_broadcast", RMON_T_BC_PKT }, 2178 { "tx_multicast", RMON_T_MC_PKT }, 2179 { "tx_crc_errors", RMON_T_CRC_ALIGN }, 2180 { "tx_undersize", RMON_T_UNDERSIZE }, 2181 { "tx_oversize", RMON_T_OVERSIZE }, 2182 { "tx_fragment", RMON_T_FRAG }, 2183 { "tx_jabber", RMON_T_JAB }, 2184 { "tx_collision", RMON_T_COL }, 2185 { "tx_64byte", RMON_T_P64 }, 2186 { "tx_65to127byte", RMON_T_P65TO127 }, 2187 { "tx_128to255byte", RMON_T_P128TO255 }, 2188 { "tx_256to511byte", RMON_T_P256TO511 }, 2189 { "tx_512to1023byte", RMON_T_P512TO1023 }, 2190 { "tx_1024to2047byte", RMON_T_P1024TO2047 }, 2191 { "tx_GTE2048byte", RMON_T_P_GTE2048 }, 2192 { "tx_octets", RMON_T_OCTETS }, 2193 2194 /* IEEE TX */ 2195 { "IEEE_tx_drop", IEEE_T_DROP }, 2196 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK }, 2197 { "IEEE_tx_1col", IEEE_T_1COL }, 2198 { "IEEE_tx_mcol", IEEE_T_MCOL }, 2199 { "IEEE_tx_def", IEEE_T_DEF }, 2200 { "IEEE_tx_lcol", IEEE_T_LCOL }, 2201 { "IEEE_tx_excol", IEEE_T_EXCOL }, 2202 { "IEEE_tx_macerr", IEEE_T_MACERR }, 2203 { "IEEE_tx_cserr", IEEE_T_CSERR }, 2204 { "IEEE_tx_sqe", IEEE_T_SQE }, 2205 { "IEEE_tx_fdxfc", IEEE_T_FDXFC }, 2206 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK }, 2207 2208 /* RMON RX */ 2209 { "rx_packets", RMON_R_PACKETS }, 2210 { "rx_broadcast", RMON_R_BC_PKT }, 2211 { "rx_multicast", RMON_R_MC_PKT }, 2212 { "rx_crc_errors", RMON_R_CRC_ALIGN }, 2213 { "rx_undersize", RMON_R_UNDERSIZE }, 2214 { "rx_oversize", RMON_R_OVERSIZE }, 2215 { "rx_fragment", RMON_R_FRAG }, 2216 { "rx_jabber", RMON_R_JAB }, 2217 { "rx_64byte", RMON_R_P64 }, 2218 { "rx_65to127byte", RMON_R_P65TO127 }, 2219 { "rx_128to255byte", RMON_R_P128TO255 }, 2220 { "rx_256to511byte", RMON_R_P256TO511 }, 2221 { "rx_512to1023byte", RMON_R_P512TO1023 }, 2222 { "rx_1024to2047byte", RMON_R_P1024TO2047 }, 2223 { "rx_GTE2048byte", RMON_R_P_GTE2048 }, 2224 { "rx_octets", RMON_R_OCTETS }, 2225 2226 /* IEEE RX */ 2227 { "IEEE_rx_drop", IEEE_R_DROP }, 2228 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK }, 2229 { "IEEE_rx_crc", IEEE_R_CRC }, 2230 { "IEEE_rx_align", IEEE_R_ALIGN }, 2231 { "IEEE_rx_macerr", IEEE_R_MACERR }, 2232 { "IEEE_rx_fdxfc", IEEE_R_FDXFC }, 2233 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK }, 2234 }; 2235 2236 static void fec_enet_get_ethtool_stats(struct net_device *dev, 2237 struct ethtool_stats *stats, u64 *data) 2238 { 2239 struct fec_enet_private *fep = netdev_priv(dev); 2240 int i; 2241 2242 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2243 data[i] = readl(fep->hwp + fec_stats[i].offset); 2244 } 2245 2246 static void fec_enet_get_strings(struct net_device *netdev, 2247 u32 stringset, u8 *data) 2248 { 2249 int i; 2250 switch (stringset) { 2251 case ETH_SS_STATS: 2252 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2253 memcpy(data + i * ETH_GSTRING_LEN, 2254 fec_stats[i].name, ETH_GSTRING_LEN); 2255 break; 2256 } 2257 } 2258 2259 static int fec_enet_get_sset_count(struct net_device *dev, int sset) 2260 { 2261 switch (sset) { 2262 case ETH_SS_STATS: 2263 return ARRAY_SIZE(fec_stats); 2264 default: 2265 return -EOPNOTSUPP; 2266 } 2267 } 2268 #endif /* !defined(CONFIG_M5272) */ 2269 2270 static int fec_enet_nway_reset(struct net_device *dev) 2271 { 2272 struct fec_enet_private *fep = netdev_priv(dev); 2273 struct phy_device *phydev = fep->phy_dev; 2274 2275 if (!phydev) 2276 return -ENODEV; 2277 2278 return genphy_restart_aneg(phydev); 2279 } 2280 2281 /* ITR clock source is enet system clock (clk_ahb). 2282 * TCTT unit is cycle_ns * 64 cycle 2283 * So, the ICTT value = X us / (cycle_ns * 64) 2284 */ 2285 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us) 2286 { 2287 struct fec_enet_private *fep = netdev_priv(ndev); 2288 2289 return us * (fep->itr_clk_rate / 64000) / 1000; 2290 } 2291 2292 /* Set threshold for interrupt coalescing */ 2293 static void fec_enet_itr_coal_set(struct net_device *ndev) 2294 { 2295 struct fec_enet_private *fep = netdev_priv(ndev); 2296 const struct platform_device_id *id_entry = 2297 platform_get_device_id(fep->pdev); 2298 int rx_itr, tx_itr; 2299 2300 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB)) 2301 return; 2302 2303 /* Must be greater than zero to avoid unpredictable behavior */ 2304 if (!fep->rx_time_itr || !fep->rx_pkts_itr || 2305 !fep->tx_time_itr || !fep->tx_pkts_itr) 2306 return; 2307 2308 /* Select enet system clock as Interrupt Coalescing 2309 * timer Clock Source 2310 */ 2311 rx_itr = FEC_ITR_CLK_SEL; 2312 tx_itr = FEC_ITR_CLK_SEL; 2313 2314 /* set ICFT and ICTT */ 2315 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr); 2316 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr)); 2317 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr); 2318 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr)); 2319 2320 rx_itr |= FEC_ITR_EN; 2321 tx_itr |= FEC_ITR_EN; 2322 2323 writel(tx_itr, fep->hwp + FEC_TXIC0); 2324 writel(rx_itr, fep->hwp + FEC_RXIC0); 2325 writel(tx_itr, fep->hwp + FEC_TXIC1); 2326 writel(rx_itr, fep->hwp + FEC_RXIC1); 2327 writel(tx_itr, fep->hwp + FEC_TXIC2); 2328 writel(rx_itr, fep->hwp + FEC_RXIC2); 2329 } 2330 2331 static int 2332 fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec) 2333 { 2334 struct fec_enet_private *fep = netdev_priv(ndev); 2335 const struct platform_device_id *id_entry = 2336 platform_get_device_id(fep->pdev); 2337 2338 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB)) 2339 return -EOPNOTSUPP; 2340 2341 ec->rx_coalesce_usecs = fep->rx_time_itr; 2342 ec->rx_max_coalesced_frames = fep->rx_pkts_itr; 2343 2344 ec->tx_coalesce_usecs = fep->tx_time_itr; 2345 ec->tx_max_coalesced_frames = fep->tx_pkts_itr; 2346 2347 return 0; 2348 } 2349 2350 static int 2351 fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec) 2352 { 2353 struct fec_enet_private *fep = netdev_priv(ndev); 2354 const struct platform_device_id *id_entry = 2355 platform_get_device_id(fep->pdev); 2356 2357 unsigned int cycle; 2358 2359 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB)) 2360 return -EOPNOTSUPP; 2361 2362 if (ec->rx_max_coalesced_frames > 255) { 2363 pr_err("Rx coalesced frames exceed hardware limiation"); 2364 return -EINVAL; 2365 } 2366 2367 if (ec->tx_max_coalesced_frames > 255) { 2368 pr_err("Tx coalesced frame exceed hardware limiation"); 2369 return -EINVAL; 2370 } 2371 2372 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr); 2373 if (cycle > 0xFFFF) { 2374 pr_err("Rx coalesed usec exceeed hardware limiation"); 2375 return -EINVAL; 2376 } 2377 2378 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr); 2379 if (cycle > 0xFFFF) { 2380 pr_err("Rx coalesed usec exceeed hardware limiation"); 2381 return -EINVAL; 2382 } 2383 2384 fep->rx_time_itr = ec->rx_coalesce_usecs; 2385 fep->rx_pkts_itr = ec->rx_max_coalesced_frames; 2386 2387 fep->tx_time_itr = ec->tx_coalesce_usecs; 2388 fep->tx_pkts_itr = ec->tx_max_coalesced_frames; 2389 2390 fec_enet_itr_coal_set(ndev); 2391 2392 return 0; 2393 } 2394 2395 static void fec_enet_itr_coal_init(struct net_device *ndev) 2396 { 2397 struct ethtool_coalesce ec; 2398 2399 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT; 2400 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT; 2401 2402 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT; 2403 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT; 2404 2405 fec_enet_set_coalesce(ndev, &ec); 2406 } 2407 2408 static int fec_enet_get_tunable(struct net_device *netdev, 2409 const struct ethtool_tunable *tuna, 2410 void *data) 2411 { 2412 struct fec_enet_private *fep = netdev_priv(netdev); 2413 int ret = 0; 2414 2415 switch (tuna->id) { 2416 case ETHTOOL_RX_COPYBREAK: 2417 *(u32 *)data = fep->rx_copybreak; 2418 break; 2419 default: 2420 ret = -EINVAL; 2421 break; 2422 } 2423 2424 return ret; 2425 } 2426 2427 static int fec_enet_set_tunable(struct net_device *netdev, 2428 const struct ethtool_tunable *tuna, 2429 const void *data) 2430 { 2431 struct fec_enet_private *fep = netdev_priv(netdev); 2432 int ret = 0; 2433 2434 switch (tuna->id) { 2435 case ETHTOOL_RX_COPYBREAK: 2436 fep->rx_copybreak = *(u32 *)data; 2437 break; 2438 default: 2439 ret = -EINVAL; 2440 break; 2441 } 2442 2443 return ret; 2444 } 2445 2446 static const struct ethtool_ops fec_enet_ethtool_ops = { 2447 .get_settings = fec_enet_get_settings, 2448 .set_settings = fec_enet_set_settings, 2449 .get_drvinfo = fec_enet_get_drvinfo, 2450 .nway_reset = fec_enet_nway_reset, 2451 .get_link = ethtool_op_get_link, 2452 .get_coalesce = fec_enet_get_coalesce, 2453 .set_coalesce = fec_enet_set_coalesce, 2454 #ifndef CONFIG_M5272 2455 .get_pauseparam = fec_enet_get_pauseparam, 2456 .set_pauseparam = fec_enet_set_pauseparam, 2457 .get_strings = fec_enet_get_strings, 2458 .get_ethtool_stats = fec_enet_get_ethtool_stats, 2459 .get_sset_count = fec_enet_get_sset_count, 2460 #endif 2461 .get_ts_info = fec_enet_get_ts_info, 2462 .get_tunable = fec_enet_get_tunable, 2463 .set_tunable = fec_enet_set_tunable, 2464 }; 2465 2466 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 2467 { 2468 struct fec_enet_private *fep = netdev_priv(ndev); 2469 struct phy_device *phydev = fep->phy_dev; 2470 2471 if (!netif_running(ndev)) 2472 return -EINVAL; 2473 2474 if (!phydev) 2475 return -ENODEV; 2476 2477 if (fep->bufdesc_ex) { 2478 if (cmd == SIOCSHWTSTAMP) 2479 return fec_ptp_set(ndev, rq); 2480 if (cmd == SIOCGHWTSTAMP) 2481 return fec_ptp_get(ndev, rq); 2482 } 2483 2484 return phy_mii_ioctl(phydev, rq, cmd); 2485 } 2486 2487 static void fec_enet_free_buffers(struct net_device *ndev) 2488 { 2489 struct fec_enet_private *fep = netdev_priv(ndev); 2490 unsigned int i; 2491 struct sk_buff *skb; 2492 struct bufdesc *bdp; 2493 struct fec_enet_priv_tx_q *txq; 2494 struct fec_enet_priv_rx_q *rxq; 2495 unsigned int q; 2496 2497 for (q = 0; q < fep->num_rx_queues; q++) { 2498 rxq = fep->rx_queue[q]; 2499 bdp = rxq->rx_bd_base; 2500 for (i = 0; i < rxq->rx_ring_size; i++) { 2501 skb = rxq->rx_skbuff[i]; 2502 rxq->rx_skbuff[i] = NULL; 2503 if (skb) { 2504 dma_unmap_single(&fep->pdev->dev, 2505 bdp->cbd_bufaddr, 2506 FEC_ENET_RX_FRSIZE - fep->rx_align, 2507 DMA_FROM_DEVICE); 2508 dev_kfree_skb(skb); 2509 } 2510 bdp = fec_enet_get_nextdesc(bdp, fep, q); 2511 } 2512 } 2513 2514 for (q = 0; q < fep->num_tx_queues; q++) { 2515 txq = fep->tx_queue[q]; 2516 bdp = txq->tx_bd_base; 2517 for (i = 0; i < txq->tx_ring_size; i++) { 2518 kfree(txq->tx_bounce[i]); 2519 txq->tx_bounce[i] = NULL; 2520 skb = txq->tx_skbuff[i]; 2521 txq->tx_skbuff[i] = NULL; 2522 dev_kfree_skb(skb); 2523 } 2524 } 2525 } 2526 2527 static void fec_enet_free_queue(struct net_device *ndev) 2528 { 2529 struct fec_enet_private *fep = netdev_priv(ndev); 2530 int i; 2531 struct fec_enet_priv_tx_q *txq; 2532 2533 for (i = 0; i < fep->num_tx_queues; i++) 2534 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) { 2535 txq = fep->tx_queue[i]; 2536 dma_free_coherent(NULL, 2537 txq->tx_ring_size * TSO_HEADER_SIZE, 2538 txq->tso_hdrs, 2539 txq->tso_hdrs_dma); 2540 } 2541 2542 for (i = 0; i < fep->num_rx_queues; i++) 2543 if (fep->rx_queue[i]) 2544 kfree(fep->rx_queue[i]); 2545 2546 for (i = 0; i < fep->num_tx_queues; i++) 2547 if (fep->tx_queue[i]) 2548 kfree(fep->tx_queue[i]); 2549 } 2550 2551 static int fec_enet_alloc_queue(struct net_device *ndev) 2552 { 2553 struct fec_enet_private *fep = netdev_priv(ndev); 2554 int i; 2555 int ret = 0; 2556 struct fec_enet_priv_tx_q *txq; 2557 2558 for (i = 0; i < fep->num_tx_queues; i++) { 2559 txq = kzalloc(sizeof(*txq), GFP_KERNEL); 2560 if (!txq) { 2561 ret = -ENOMEM; 2562 goto alloc_failed; 2563 } 2564 2565 fep->tx_queue[i] = txq; 2566 txq->tx_ring_size = TX_RING_SIZE; 2567 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size; 2568 2569 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS; 2570 txq->tx_wake_threshold = 2571 (txq->tx_ring_size - txq->tx_stop_threshold) / 2; 2572 2573 txq->tso_hdrs = dma_alloc_coherent(NULL, 2574 txq->tx_ring_size * TSO_HEADER_SIZE, 2575 &txq->tso_hdrs_dma, 2576 GFP_KERNEL); 2577 if (!txq->tso_hdrs) { 2578 ret = -ENOMEM; 2579 goto alloc_failed; 2580 } 2581 } 2582 2583 for (i = 0; i < fep->num_rx_queues; i++) { 2584 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]), 2585 GFP_KERNEL); 2586 if (!fep->rx_queue[i]) { 2587 ret = -ENOMEM; 2588 goto alloc_failed; 2589 } 2590 2591 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE; 2592 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size; 2593 } 2594 return ret; 2595 2596 alloc_failed: 2597 fec_enet_free_queue(ndev); 2598 return ret; 2599 } 2600 2601 static int 2602 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue) 2603 { 2604 struct fec_enet_private *fep = netdev_priv(ndev); 2605 unsigned int i; 2606 struct sk_buff *skb; 2607 struct bufdesc *bdp; 2608 struct fec_enet_priv_rx_q *rxq; 2609 2610 rxq = fep->rx_queue[queue]; 2611 bdp = rxq->rx_bd_base; 2612 for (i = 0; i < rxq->rx_ring_size; i++) { 2613 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE); 2614 if (!skb) 2615 goto err_alloc; 2616 2617 if (fec_enet_new_rxbdp(ndev, bdp, skb)) { 2618 dev_kfree_skb(skb); 2619 goto err_alloc; 2620 } 2621 2622 rxq->rx_skbuff[i] = skb; 2623 bdp->cbd_sc = BD_ENET_RX_EMPTY; 2624 2625 if (fep->bufdesc_ex) { 2626 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 2627 ebdp->cbd_esc = BD_ENET_RX_INT; 2628 } 2629 2630 bdp = fec_enet_get_nextdesc(bdp, fep, queue); 2631 } 2632 2633 /* Set the last buffer to wrap. */ 2634 bdp = fec_enet_get_prevdesc(bdp, fep, queue); 2635 bdp->cbd_sc |= BD_SC_WRAP; 2636 return 0; 2637 2638 err_alloc: 2639 fec_enet_free_buffers(ndev); 2640 return -ENOMEM; 2641 } 2642 2643 static int 2644 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue) 2645 { 2646 struct fec_enet_private *fep = netdev_priv(ndev); 2647 unsigned int i; 2648 struct bufdesc *bdp; 2649 struct fec_enet_priv_tx_q *txq; 2650 2651 txq = fep->tx_queue[queue]; 2652 bdp = txq->tx_bd_base; 2653 for (i = 0; i < txq->tx_ring_size; i++) { 2654 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); 2655 if (!txq->tx_bounce[i]) 2656 goto err_alloc; 2657 2658 bdp->cbd_sc = 0; 2659 bdp->cbd_bufaddr = 0; 2660 2661 if (fep->bufdesc_ex) { 2662 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 2663 ebdp->cbd_esc = BD_ENET_TX_INT; 2664 } 2665 2666 bdp = fec_enet_get_nextdesc(bdp, fep, queue); 2667 } 2668 2669 /* Set the last buffer to wrap. */ 2670 bdp = fec_enet_get_prevdesc(bdp, fep, queue); 2671 bdp->cbd_sc |= BD_SC_WRAP; 2672 2673 return 0; 2674 2675 err_alloc: 2676 fec_enet_free_buffers(ndev); 2677 return -ENOMEM; 2678 } 2679 2680 static int fec_enet_alloc_buffers(struct net_device *ndev) 2681 { 2682 struct fec_enet_private *fep = netdev_priv(ndev); 2683 unsigned int i; 2684 2685 for (i = 0; i < fep->num_rx_queues; i++) 2686 if (fec_enet_alloc_rxq_buffers(ndev, i)) 2687 return -ENOMEM; 2688 2689 for (i = 0; i < fep->num_tx_queues; i++) 2690 if (fec_enet_alloc_txq_buffers(ndev, i)) 2691 return -ENOMEM; 2692 return 0; 2693 } 2694 2695 static int 2696 fec_enet_open(struct net_device *ndev) 2697 { 2698 struct fec_enet_private *fep = netdev_priv(ndev); 2699 int ret; 2700 2701 pinctrl_pm_select_default_state(&fep->pdev->dev); 2702 ret = fec_enet_clk_enable(ndev, true); 2703 if (ret) 2704 return ret; 2705 2706 /* I should reset the ring buffers here, but I don't yet know 2707 * a simple way to do that. 2708 */ 2709 2710 ret = fec_enet_alloc_buffers(ndev); 2711 if (ret) 2712 goto err_enet_alloc; 2713 2714 /* Probe and connect to PHY when open the interface */ 2715 ret = fec_enet_mii_probe(ndev); 2716 if (ret) 2717 goto err_enet_mii_probe; 2718 2719 fec_restart(ndev); 2720 napi_enable(&fep->napi); 2721 phy_start(fep->phy_dev); 2722 netif_tx_start_all_queues(ndev); 2723 2724 return 0; 2725 2726 err_enet_mii_probe: 2727 fec_enet_free_buffers(ndev); 2728 err_enet_alloc: 2729 fec_enet_clk_enable(ndev, false); 2730 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 2731 return ret; 2732 } 2733 2734 static int 2735 fec_enet_close(struct net_device *ndev) 2736 { 2737 struct fec_enet_private *fep = netdev_priv(ndev); 2738 2739 phy_stop(fep->phy_dev); 2740 2741 if (netif_device_present(ndev)) { 2742 napi_disable(&fep->napi); 2743 netif_tx_disable(ndev); 2744 fec_stop(ndev); 2745 } 2746 2747 phy_disconnect(fep->phy_dev); 2748 fep->phy_dev = NULL; 2749 2750 fec_enet_clk_enable(ndev, false); 2751 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 2752 fec_enet_free_buffers(ndev); 2753 2754 return 0; 2755 } 2756 2757 /* Set or clear the multicast filter for this adaptor. 2758 * Skeleton taken from sunlance driver. 2759 * The CPM Ethernet implementation allows Multicast as well as individual 2760 * MAC address filtering. Some of the drivers check to make sure it is 2761 * a group multicast address, and discard those that are not. I guess I 2762 * will do the same for now, but just remove the test if you want 2763 * individual filtering as well (do the upper net layers want or support 2764 * this kind of feature?). 2765 */ 2766 2767 #define HASH_BITS 6 /* #bits in hash */ 2768 #define CRC32_POLY 0xEDB88320 2769 2770 static void set_multicast_list(struct net_device *ndev) 2771 { 2772 struct fec_enet_private *fep = netdev_priv(ndev); 2773 struct netdev_hw_addr *ha; 2774 unsigned int i, bit, data, crc, tmp; 2775 unsigned char hash; 2776 2777 if (ndev->flags & IFF_PROMISC) { 2778 tmp = readl(fep->hwp + FEC_R_CNTRL); 2779 tmp |= 0x8; 2780 writel(tmp, fep->hwp + FEC_R_CNTRL); 2781 return; 2782 } 2783 2784 tmp = readl(fep->hwp + FEC_R_CNTRL); 2785 tmp &= ~0x8; 2786 writel(tmp, fep->hwp + FEC_R_CNTRL); 2787 2788 if (ndev->flags & IFF_ALLMULTI) { 2789 /* Catch all multicast addresses, so set the 2790 * filter to all 1's 2791 */ 2792 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 2793 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 2794 2795 return; 2796 } 2797 2798 /* Clear filter and add the addresses in hash register 2799 */ 2800 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 2801 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 2802 2803 netdev_for_each_mc_addr(ha, ndev) { 2804 /* calculate crc32 value of mac address */ 2805 crc = 0xffffffff; 2806 2807 for (i = 0; i < ndev->addr_len; i++) { 2808 data = ha->addr[i]; 2809 for (bit = 0; bit < 8; bit++, data >>= 1) { 2810 crc = (crc >> 1) ^ 2811 (((crc ^ data) & 1) ? CRC32_POLY : 0); 2812 } 2813 } 2814 2815 /* only upper 6 bits (HASH_BITS) are used 2816 * which point to specific bit in he hash registers 2817 */ 2818 hash = (crc >> (32 - HASH_BITS)) & 0x3f; 2819 2820 if (hash > 31) { 2821 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 2822 tmp |= 1 << (hash - 32); 2823 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 2824 } else { 2825 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW); 2826 tmp |= 1 << hash; 2827 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 2828 } 2829 } 2830 } 2831 2832 /* Set a MAC change in hardware. */ 2833 static int 2834 fec_set_mac_address(struct net_device *ndev, void *p) 2835 { 2836 struct fec_enet_private *fep = netdev_priv(ndev); 2837 struct sockaddr *addr = p; 2838 2839 if (addr) { 2840 if (!is_valid_ether_addr(addr->sa_data)) 2841 return -EADDRNOTAVAIL; 2842 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len); 2843 } 2844 2845 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | 2846 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), 2847 fep->hwp + FEC_ADDR_LOW); 2848 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), 2849 fep->hwp + FEC_ADDR_HIGH); 2850 return 0; 2851 } 2852 2853 #ifdef CONFIG_NET_POLL_CONTROLLER 2854 /** 2855 * fec_poll_controller - FEC Poll controller function 2856 * @dev: The FEC network adapter 2857 * 2858 * Polled functionality used by netconsole and others in non interrupt mode 2859 * 2860 */ 2861 static void fec_poll_controller(struct net_device *dev) 2862 { 2863 int i; 2864 struct fec_enet_private *fep = netdev_priv(dev); 2865 2866 for (i = 0; i < FEC_IRQ_NUM; i++) { 2867 if (fep->irq[i] > 0) { 2868 disable_irq(fep->irq[i]); 2869 fec_enet_interrupt(fep->irq[i], dev); 2870 enable_irq(fep->irq[i]); 2871 } 2872 } 2873 } 2874 #endif 2875 2876 #define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM 2877 static inline void fec_enet_set_netdev_features(struct net_device *netdev, 2878 netdev_features_t features) 2879 { 2880 struct fec_enet_private *fep = netdev_priv(netdev); 2881 netdev_features_t changed = features ^ netdev->features; 2882 2883 netdev->features = features; 2884 2885 /* Receive checksum has been changed */ 2886 if (changed & NETIF_F_RXCSUM) { 2887 if (features & NETIF_F_RXCSUM) 2888 fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 2889 else 2890 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; 2891 } 2892 } 2893 2894 static int fec_set_features(struct net_device *netdev, 2895 netdev_features_t features) 2896 { 2897 struct fec_enet_private *fep = netdev_priv(netdev); 2898 netdev_features_t changed = features ^ netdev->features; 2899 2900 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) { 2901 napi_disable(&fep->napi); 2902 netif_tx_lock_bh(netdev); 2903 fec_stop(netdev); 2904 fec_enet_set_netdev_features(netdev, features); 2905 fec_restart(netdev); 2906 netif_tx_wake_all_queues(netdev); 2907 netif_tx_unlock_bh(netdev); 2908 napi_enable(&fep->napi); 2909 } else { 2910 fec_enet_set_netdev_features(netdev, features); 2911 } 2912 2913 return 0; 2914 } 2915 2916 static const struct net_device_ops fec_netdev_ops = { 2917 .ndo_open = fec_enet_open, 2918 .ndo_stop = fec_enet_close, 2919 .ndo_start_xmit = fec_enet_start_xmit, 2920 .ndo_set_rx_mode = set_multicast_list, 2921 .ndo_change_mtu = eth_change_mtu, 2922 .ndo_validate_addr = eth_validate_addr, 2923 .ndo_tx_timeout = fec_timeout, 2924 .ndo_set_mac_address = fec_set_mac_address, 2925 .ndo_do_ioctl = fec_enet_ioctl, 2926 #ifdef CONFIG_NET_POLL_CONTROLLER 2927 .ndo_poll_controller = fec_poll_controller, 2928 #endif 2929 .ndo_set_features = fec_set_features, 2930 }; 2931 2932 /* 2933 * XXX: We need to clean up on failure exits here. 2934 * 2935 */ 2936 static int fec_enet_init(struct net_device *ndev) 2937 { 2938 struct fec_enet_private *fep = netdev_priv(ndev); 2939 const struct platform_device_id *id_entry = 2940 platform_get_device_id(fep->pdev); 2941 struct fec_enet_priv_tx_q *txq; 2942 struct fec_enet_priv_rx_q *rxq; 2943 struct bufdesc *cbd_base; 2944 dma_addr_t bd_dma; 2945 int bd_size; 2946 unsigned int i; 2947 2948 #if defined(CONFIG_ARM) 2949 fep->rx_align = 0xf; 2950 fep->tx_align = 0xf; 2951 #else 2952 fep->rx_align = 0x3; 2953 fep->tx_align = 0x3; 2954 #endif 2955 2956 fec_enet_alloc_queue(ndev); 2957 2958 if (fep->bufdesc_ex) 2959 fep->bufdesc_size = sizeof(struct bufdesc_ex); 2960 else 2961 fep->bufdesc_size = sizeof(struct bufdesc); 2962 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * 2963 fep->bufdesc_size; 2964 2965 /* Allocate memory for buffer descriptors. */ 2966 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma, 2967 GFP_KERNEL); 2968 if (!cbd_base) { 2969 return -ENOMEM; 2970 } 2971 2972 memset(cbd_base, 0, bd_size); 2973 2974 /* Get the Ethernet address */ 2975 fec_get_mac(ndev); 2976 /* make sure MAC we just acquired is programmed into the hw */ 2977 fec_set_mac_address(ndev, NULL); 2978 2979 /* Set receive and transmit descriptor base. */ 2980 for (i = 0; i < fep->num_rx_queues; i++) { 2981 rxq = fep->rx_queue[i]; 2982 rxq->index = i; 2983 rxq->rx_bd_base = (struct bufdesc *)cbd_base; 2984 rxq->bd_dma = bd_dma; 2985 if (fep->bufdesc_ex) { 2986 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size; 2987 cbd_base = (struct bufdesc *) 2988 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size); 2989 } else { 2990 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size; 2991 cbd_base += rxq->rx_ring_size; 2992 } 2993 } 2994 2995 for (i = 0; i < fep->num_tx_queues; i++) { 2996 txq = fep->tx_queue[i]; 2997 txq->index = i; 2998 txq->tx_bd_base = (struct bufdesc *)cbd_base; 2999 txq->bd_dma = bd_dma; 3000 if (fep->bufdesc_ex) { 3001 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size; 3002 cbd_base = (struct bufdesc *) 3003 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size); 3004 } else { 3005 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size; 3006 cbd_base += txq->tx_ring_size; 3007 } 3008 } 3009 3010 3011 /* The FEC Ethernet specific entries in the device structure */ 3012 ndev->watchdog_timeo = TX_TIMEOUT; 3013 ndev->netdev_ops = &fec_netdev_ops; 3014 ndev->ethtool_ops = &fec_enet_ethtool_ops; 3015 3016 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); 3017 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT); 3018 3019 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN) 3020 /* enable hw VLAN support */ 3021 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; 3022 3023 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) { 3024 ndev->gso_max_segs = FEC_MAX_TSO_SEGS; 3025 3026 /* enable hw accelerator */ 3027 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM 3028 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO); 3029 fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 3030 } 3031 3032 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) { 3033 fep->tx_align = 0; 3034 fep->rx_align = 0x3f; 3035 } 3036 3037 ndev->hw_features = ndev->features; 3038 3039 fec_restart(ndev); 3040 3041 return 0; 3042 } 3043 3044 #ifdef CONFIG_OF 3045 static void fec_reset_phy(struct platform_device *pdev) 3046 { 3047 int err, phy_reset; 3048 int msec = 1; 3049 struct device_node *np = pdev->dev.of_node; 3050 3051 if (!np) 3052 return; 3053 3054 of_property_read_u32(np, "phy-reset-duration", &msec); 3055 /* A sane reset duration should not be longer than 1s */ 3056 if (msec > 1000) 3057 msec = 1; 3058 3059 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0); 3060 if (!gpio_is_valid(phy_reset)) 3061 return; 3062 3063 err = devm_gpio_request_one(&pdev->dev, phy_reset, 3064 GPIOF_OUT_INIT_LOW, "phy-reset"); 3065 if (err) { 3066 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err); 3067 return; 3068 } 3069 msleep(msec); 3070 gpio_set_value(phy_reset, 1); 3071 } 3072 #else /* CONFIG_OF */ 3073 static void fec_reset_phy(struct platform_device *pdev) 3074 { 3075 /* 3076 * In case of platform probe, the reset has been done 3077 * by machine code. 3078 */ 3079 } 3080 #endif /* CONFIG_OF */ 3081 3082 static void 3083 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx) 3084 { 3085 struct device_node *np = pdev->dev.of_node; 3086 int err; 3087 3088 *num_tx = *num_rx = 1; 3089 3090 if (!np || !of_device_is_available(np)) 3091 return; 3092 3093 /* parse the num of tx and rx queues */ 3094 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx); 3095 if (err) 3096 *num_tx = 1; 3097 3098 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx); 3099 if (err) 3100 *num_rx = 1; 3101 3102 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) { 3103 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n", 3104 *num_tx); 3105 *num_tx = 1; 3106 return; 3107 } 3108 3109 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) { 3110 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n", 3111 *num_rx); 3112 *num_rx = 1; 3113 return; 3114 } 3115 3116 } 3117 3118 static int 3119 fec_probe(struct platform_device *pdev) 3120 { 3121 struct fec_enet_private *fep; 3122 struct fec_platform_data *pdata; 3123 struct net_device *ndev; 3124 int i, irq, ret = 0; 3125 struct resource *r; 3126 const struct of_device_id *of_id; 3127 static int dev_id; 3128 struct device_node *np = pdev->dev.of_node, *phy_node; 3129 int num_tx_qs; 3130 int num_rx_qs; 3131 3132 of_id = of_match_device(fec_dt_ids, &pdev->dev); 3133 if (of_id) 3134 pdev->id_entry = of_id->data; 3135 3136 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs); 3137 3138 /* Init network device */ 3139 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private), 3140 num_tx_qs, num_rx_qs); 3141 if (!ndev) 3142 return -ENOMEM; 3143 3144 SET_NETDEV_DEV(ndev, &pdev->dev); 3145 3146 /* setup board info structure */ 3147 fep = netdev_priv(ndev); 3148 3149 fep->num_rx_queues = num_rx_qs; 3150 fep->num_tx_queues = num_tx_qs; 3151 3152 #if !defined(CONFIG_M5272) 3153 /* default enable pause frame auto negotiation */ 3154 if (pdev->id_entry && 3155 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT)) 3156 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; 3157 #endif 3158 3159 /* Select default pin state */ 3160 pinctrl_pm_select_default_state(&pdev->dev); 3161 3162 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3163 fep->hwp = devm_ioremap_resource(&pdev->dev, r); 3164 if (IS_ERR(fep->hwp)) { 3165 ret = PTR_ERR(fep->hwp); 3166 goto failed_ioremap; 3167 } 3168 3169 fep->pdev = pdev; 3170 fep->dev_id = dev_id++; 3171 3172 fep->bufdesc_ex = 0; 3173 3174 platform_set_drvdata(pdev, ndev); 3175 3176 phy_node = of_parse_phandle(np, "phy-handle", 0); 3177 if (!phy_node && of_phy_is_fixed_link(np)) { 3178 ret = of_phy_register_fixed_link(np); 3179 if (ret < 0) { 3180 dev_err(&pdev->dev, 3181 "broken fixed-link specification\n"); 3182 goto failed_phy; 3183 } 3184 phy_node = of_node_get(np); 3185 } 3186 fep->phy_node = phy_node; 3187 3188 ret = of_get_phy_mode(pdev->dev.of_node); 3189 if (ret < 0) { 3190 pdata = dev_get_platdata(&pdev->dev); 3191 if (pdata) 3192 fep->phy_interface = pdata->phy; 3193 else 3194 fep->phy_interface = PHY_INTERFACE_MODE_MII; 3195 } else { 3196 fep->phy_interface = ret; 3197 } 3198 3199 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 3200 if (IS_ERR(fep->clk_ipg)) { 3201 ret = PTR_ERR(fep->clk_ipg); 3202 goto failed_clk; 3203 } 3204 3205 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 3206 if (IS_ERR(fep->clk_ahb)) { 3207 ret = PTR_ERR(fep->clk_ahb); 3208 goto failed_clk; 3209 } 3210 3211 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb); 3212 3213 /* enet_out is optional, depends on board */ 3214 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out"); 3215 if (IS_ERR(fep->clk_enet_out)) 3216 fep->clk_enet_out = NULL; 3217 3218 fep->ptp_clk_on = false; 3219 mutex_init(&fep->ptp_clk_mutex); 3220 3221 /* clk_ref is optional, depends on board */ 3222 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref"); 3223 if (IS_ERR(fep->clk_ref)) 3224 fep->clk_ref = NULL; 3225 3226 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); 3227 fep->bufdesc_ex = 3228 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX; 3229 if (IS_ERR(fep->clk_ptp)) { 3230 fep->clk_ptp = NULL; 3231 fep->bufdesc_ex = 0; 3232 } 3233 3234 ret = fec_enet_clk_enable(ndev, true); 3235 if (ret) 3236 goto failed_clk; 3237 3238 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy"); 3239 if (!IS_ERR(fep->reg_phy)) { 3240 ret = regulator_enable(fep->reg_phy); 3241 if (ret) { 3242 dev_err(&pdev->dev, 3243 "Failed to enable phy regulator: %d\n", ret); 3244 goto failed_regulator; 3245 } 3246 } else { 3247 fep->reg_phy = NULL; 3248 } 3249 3250 fec_reset_phy(pdev); 3251 3252 if (fep->bufdesc_ex) 3253 fec_ptp_init(pdev); 3254 3255 ret = fec_enet_init(ndev); 3256 if (ret) 3257 goto failed_init; 3258 3259 for (i = 0; i < FEC_IRQ_NUM; i++) { 3260 irq = platform_get_irq(pdev, i); 3261 if (irq < 0) { 3262 if (i) 3263 break; 3264 ret = irq; 3265 goto failed_irq; 3266 } 3267 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt, 3268 0, pdev->name, ndev); 3269 if (ret) 3270 goto failed_irq; 3271 } 3272 3273 init_completion(&fep->mdio_done); 3274 ret = fec_enet_mii_init(pdev); 3275 if (ret) 3276 goto failed_mii_init; 3277 3278 /* Carrier starts down, phylib will bring it up */ 3279 netif_carrier_off(ndev); 3280 fec_enet_clk_enable(ndev, false); 3281 pinctrl_pm_select_sleep_state(&pdev->dev); 3282 3283 ret = register_netdev(ndev); 3284 if (ret) 3285 goto failed_register; 3286 3287 if (fep->bufdesc_ex && fep->ptp_clock) 3288 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); 3289 3290 fep->rx_copybreak = COPYBREAK_DEFAULT; 3291 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work); 3292 return 0; 3293 3294 failed_register: 3295 fec_enet_mii_remove(fep); 3296 failed_mii_init: 3297 failed_irq: 3298 failed_init: 3299 if (fep->reg_phy) 3300 regulator_disable(fep->reg_phy); 3301 failed_regulator: 3302 fec_enet_clk_enable(ndev, false); 3303 failed_clk: 3304 failed_phy: 3305 of_node_put(phy_node); 3306 failed_ioremap: 3307 free_netdev(ndev); 3308 3309 return ret; 3310 } 3311 3312 static int 3313 fec_drv_remove(struct platform_device *pdev) 3314 { 3315 struct net_device *ndev = platform_get_drvdata(pdev); 3316 struct fec_enet_private *fep = netdev_priv(ndev); 3317 3318 cancel_delayed_work_sync(&fep->time_keep); 3319 cancel_work_sync(&fep->tx_timeout_work); 3320 unregister_netdev(ndev); 3321 fec_enet_mii_remove(fep); 3322 if (fep->reg_phy) 3323 regulator_disable(fep->reg_phy); 3324 if (fep->ptp_clock) 3325 ptp_clock_unregister(fep->ptp_clock); 3326 fec_enet_clk_enable(ndev, false); 3327 of_node_put(fep->phy_node); 3328 free_netdev(ndev); 3329 3330 return 0; 3331 } 3332 3333 static int __maybe_unused fec_suspend(struct device *dev) 3334 { 3335 struct net_device *ndev = dev_get_drvdata(dev); 3336 struct fec_enet_private *fep = netdev_priv(ndev); 3337 3338 rtnl_lock(); 3339 if (netif_running(ndev)) { 3340 phy_stop(fep->phy_dev); 3341 napi_disable(&fep->napi); 3342 netif_tx_lock_bh(ndev); 3343 netif_device_detach(ndev); 3344 netif_tx_unlock_bh(ndev); 3345 fec_stop(ndev); 3346 } 3347 rtnl_unlock(); 3348 3349 fec_enet_clk_enable(ndev, false); 3350 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3351 3352 if (fep->reg_phy) 3353 regulator_disable(fep->reg_phy); 3354 3355 return 0; 3356 } 3357 3358 static int __maybe_unused fec_resume(struct device *dev) 3359 { 3360 struct net_device *ndev = dev_get_drvdata(dev); 3361 struct fec_enet_private *fep = netdev_priv(ndev); 3362 int ret; 3363 3364 if (fep->reg_phy) { 3365 ret = regulator_enable(fep->reg_phy); 3366 if (ret) 3367 return ret; 3368 } 3369 3370 pinctrl_pm_select_default_state(&fep->pdev->dev); 3371 ret = fec_enet_clk_enable(ndev, true); 3372 if (ret) 3373 goto failed_clk; 3374 3375 rtnl_lock(); 3376 if (netif_running(ndev)) { 3377 fec_restart(ndev); 3378 netif_tx_lock_bh(ndev); 3379 netif_device_attach(ndev); 3380 netif_tx_unlock_bh(ndev); 3381 napi_enable(&fep->napi); 3382 phy_start(fep->phy_dev); 3383 } 3384 rtnl_unlock(); 3385 3386 return 0; 3387 3388 failed_clk: 3389 if (fep->reg_phy) 3390 regulator_disable(fep->reg_phy); 3391 return ret; 3392 } 3393 3394 static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume); 3395 3396 static struct platform_driver fec_driver = { 3397 .driver = { 3398 .name = DRIVER_NAME, 3399 .owner = THIS_MODULE, 3400 .pm = &fec_pm_ops, 3401 .of_match_table = fec_dt_ids, 3402 }, 3403 .id_table = fec_devtype, 3404 .probe = fec_probe, 3405 .remove = fec_drv_remove, 3406 }; 3407 3408 module_platform_driver(fec_driver); 3409 3410 MODULE_ALIAS("platform:"DRIVER_NAME); 3411 MODULE_LICENSE("GPL"); 3412