1 /* 2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx. 3 * 4 * Copyright (c) 2003 Intracom S.A. 5 * by Pantelis Antoniou <panto@intracom.gr> 6 * 7 * 2005 (c) MontaVista Software, Inc. 8 * Vitaly Bordug <vbordug@ru.mvista.com> 9 * 10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com> 11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se> 12 * 13 * This file is licensed under the terms of the GNU General Public License 14 * version 2. This program is licensed "as is" without any warranty of any 15 * kind, whether express or implied. 16 */ 17 18 #include <linux/module.h> 19 #include <linux/kernel.h> 20 #include <linux/types.h> 21 #include <linux/string.h> 22 #include <linux/ptrace.h> 23 #include <linux/errno.h> 24 #include <linux/ioport.h> 25 #include <linux/slab.h> 26 #include <linux/interrupt.h> 27 #include <linux/delay.h> 28 #include <linux/netdevice.h> 29 #include <linux/etherdevice.h> 30 #include <linux/skbuff.h> 31 #include <linux/spinlock.h> 32 #include <linux/mii.h> 33 #include <linux/ethtool.h> 34 #include <linux/bitops.h> 35 #include <linux/fs.h> 36 #include <linux/platform_device.h> 37 #include <linux/phy.h> 38 #include <linux/of.h> 39 #include <linux/of_mdio.h> 40 #include <linux/of_platform.h> 41 #include <linux/of_gpio.h> 42 #include <linux/of_net.h> 43 44 #include <linux/vmalloc.h> 45 #include <asm/pgtable.h> 46 #include <asm/irq.h> 47 #include <asm/uaccess.h> 48 49 #include "fs_enet.h" 50 51 /*************************************************/ 52 53 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>"); 54 MODULE_DESCRIPTION("Freescale Ethernet Driver"); 55 MODULE_LICENSE("GPL"); 56 MODULE_VERSION(DRV_MODULE_VERSION); 57 58 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */ 59 module_param(fs_enet_debug, int, 0); 60 MODULE_PARM_DESC(fs_enet_debug, 61 "Freescale bitmapped debugging message enable value"); 62 63 #ifdef CONFIG_NET_POLL_CONTROLLER 64 static void fs_enet_netpoll(struct net_device *dev); 65 #endif 66 67 static void fs_set_multicast_list(struct net_device *dev) 68 { 69 struct fs_enet_private *fep = netdev_priv(dev); 70 71 (*fep->ops->set_multicast_list)(dev); 72 } 73 74 static void skb_align(struct sk_buff *skb, int align) 75 { 76 int off = ((unsigned long)skb->data) & (align - 1); 77 78 if (off) 79 skb_reserve(skb, align - off); 80 } 81 82 /* NAPI receive function */ 83 static int fs_enet_rx_napi(struct napi_struct *napi, int budget) 84 { 85 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi); 86 struct net_device *dev = fep->ndev; 87 const struct fs_platform_info *fpi = fep->fpi; 88 cbd_t __iomem *bdp; 89 struct sk_buff *skb, *skbn, *skbt; 90 int received = 0; 91 u16 pkt_len, sc; 92 int curidx; 93 94 /* 95 * First, grab all of the stats for the incoming packet. 96 * These get messed up if we get called due to a busy condition. 97 */ 98 bdp = fep->cur_rx; 99 100 /* clear RX status bits for napi*/ 101 (*fep->ops->napi_clear_rx_event)(dev); 102 103 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { 104 curidx = bdp - fep->rx_bd_base; 105 106 /* 107 * Since we have allocated space to hold a complete frame, 108 * the last indicator should be set. 109 */ 110 if ((sc & BD_ENET_RX_LAST) == 0) 111 dev_warn(fep->dev, "rcv is not +last\n"); 112 113 /* 114 * Check for errors. 115 */ 116 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | 117 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { 118 fep->stats.rx_errors++; 119 /* Frame too long or too short. */ 120 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) 121 fep->stats.rx_length_errors++; 122 /* Frame alignment */ 123 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) 124 fep->stats.rx_frame_errors++; 125 /* CRC Error */ 126 if (sc & BD_ENET_RX_CR) 127 fep->stats.rx_crc_errors++; 128 /* FIFO overrun */ 129 if (sc & BD_ENET_RX_OV) 130 fep->stats.rx_crc_errors++; 131 132 skb = fep->rx_skbuff[curidx]; 133 134 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 135 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 136 DMA_FROM_DEVICE); 137 138 skbn = skb; 139 140 } else { 141 skb = fep->rx_skbuff[curidx]; 142 143 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 144 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 145 DMA_FROM_DEVICE); 146 147 /* 148 * Process the incoming frame. 149 */ 150 fep->stats.rx_packets++; 151 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ 152 fep->stats.rx_bytes += pkt_len + 4; 153 154 if (pkt_len <= fpi->rx_copybreak) { 155 /* +2 to make IP header L1 cache aligned */ 156 skbn = netdev_alloc_skb(dev, pkt_len + 2); 157 if (skbn != NULL) { 158 skb_reserve(skbn, 2); /* align IP header */ 159 skb_copy_from_linear_data(skb, 160 skbn->data, pkt_len); 161 /* swap */ 162 skbt = skb; 163 skb = skbn; 164 skbn = skbt; 165 } 166 } else { 167 skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE); 168 169 if (skbn) 170 skb_align(skbn, ENET_RX_ALIGN); 171 } 172 173 if (skbn != NULL) { 174 skb_put(skb, pkt_len); /* Make room */ 175 skb->protocol = eth_type_trans(skb, dev); 176 received++; 177 netif_receive_skb(skb); 178 } else { 179 fep->stats.rx_dropped++; 180 skbn = skb; 181 } 182 } 183 184 fep->rx_skbuff[curidx] = skbn; 185 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, 186 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 187 DMA_FROM_DEVICE)); 188 CBDW_DATLEN(bdp, 0); 189 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); 190 191 /* 192 * Update BD pointer to next entry. 193 */ 194 if ((sc & BD_ENET_RX_WRAP) == 0) 195 bdp++; 196 else 197 bdp = fep->rx_bd_base; 198 199 (*fep->ops->rx_bd_done)(dev); 200 201 if (received >= budget) 202 break; 203 } 204 205 fep->cur_rx = bdp; 206 207 if (received < budget) { 208 /* done */ 209 napi_complete(napi); 210 (*fep->ops->napi_enable_rx)(dev); 211 } 212 return received; 213 } 214 215 /* non NAPI receive function */ 216 static int fs_enet_rx_non_napi(struct net_device *dev) 217 { 218 struct fs_enet_private *fep = netdev_priv(dev); 219 const struct fs_platform_info *fpi = fep->fpi; 220 cbd_t __iomem *bdp; 221 struct sk_buff *skb, *skbn, *skbt; 222 int received = 0; 223 u16 pkt_len, sc; 224 int curidx; 225 /* 226 * First, grab all of the stats for the incoming packet. 227 * These get messed up if we get called due to a busy condition. 228 */ 229 bdp = fep->cur_rx; 230 231 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { 232 233 curidx = bdp - fep->rx_bd_base; 234 235 /* 236 * Since we have allocated space to hold a complete frame, 237 * the last indicator should be set. 238 */ 239 if ((sc & BD_ENET_RX_LAST) == 0) 240 dev_warn(fep->dev, "rcv is not +last\n"); 241 242 /* 243 * Check for errors. 244 */ 245 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | 246 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { 247 fep->stats.rx_errors++; 248 /* Frame too long or too short. */ 249 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) 250 fep->stats.rx_length_errors++; 251 /* Frame alignment */ 252 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) 253 fep->stats.rx_frame_errors++; 254 /* CRC Error */ 255 if (sc & BD_ENET_RX_CR) 256 fep->stats.rx_crc_errors++; 257 /* FIFO overrun */ 258 if (sc & BD_ENET_RX_OV) 259 fep->stats.rx_crc_errors++; 260 261 skb = fep->rx_skbuff[curidx]; 262 263 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 264 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 265 DMA_FROM_DEVICE); 266 267 skbn = skb; 268 269 } else { 270 271 skb = fep->rx_skbuff[curidx]; 272 273 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 274 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 275 DMA_FROM_DEVICE); 276 277 /* 278 * Process the incoming frame. 279 */ 280 fep->stats.rx_packets++; 281 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ 282 fep->stats.rx_bytes += pkt_len + 4; 283 284 if (pkt_len <= fpi->rx_copybreak) { 285 /* +2 to make IP header L1 cache aligned */ 286 skbn = netdev_alloc_skb(dev, pkt_len + 2); 287 if (skbn != NULL) { 288 skb_reserve(skbn, 2); /* align IP header */ 289 skb_copy_from_linear_data(skb, 290 skbn->data, pkt_len); 291 /* swap */ 292 skbt = skb; 293 skb = skbn; 294 skbn = skbt; 295 } 296 } else { 297 skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE); 298 299 if (skbn) 300 skb_align(skbn, ENET_RX_ALIGN); 301 } 302 303 if (skbn != NULL) { 304 skb_put(skb, pkt_len); /* Make room */ 305 skb->protocol = eth_type_trans(skb, dev); 306 received++; 307 netif_rx(skb); 308 } else { 309 fep->stats.rx_dropped++; 310 skbn = skb; 311 } 312 } 313 314 fep->rx_skbuff[curidx] = skbn; 315 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, 316 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 317 DMA_FROM_DEVICE)); 318 CBDW_DATLEN(bdp, 0); 319 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); 320 321 /* 322 * Update BD pointer to next entry. 323 */ 324 if ((sc & BD_ENET_RX_WRAP) == 0) 325 bdp++; 326 else 327 bdp = fep->rx_bd_base; 328 329 (*fep->ops->rx_bd_done)(dev); 330 } 331 332 fep->cur_rx = bdp; 333 334 return 0; 335 } 336 337 static void fs_enet_tx(struct net_device *dev) 338 { 339 struct fs_enet_private *fep = netdev_priv(dev); 340 cbd_t __iomem *bdp; 341 struct sk_buff *skb; 342 int dirtyidx, do_wake, do_restart; 343 u16 sc; 344 345 spin_lock(&fep->tx_lock); 346 bdp = fep->dirty_tx; 347 348 do_wake = do_restart = 0; 349 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) { 350 dirtyidx = bdp - fep->tx_bd_base; 351 352 if (fep->tx_free == fep->tx_ring) 353 break; 354 355 skb = fep->tx_skbuff[dirtyidx]; 356 357 /* 358 * Check for errors. 359 */ 360 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC | 361 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) { 362 363 if (sc & BD_ENET_TX_HB) /* No heartbeat */ 364 fep->stats.tx_heartbeat_errors++; 365 if (sc & BD_ENET_TX_LC) /* Late collision */ 366 fep->stats.tx_window_errors++; 367 if (sc & BD_ENET_TX_RL) /* Retrans limit */ 368 fep->stats.tx_aborted_errors++; 369 if (sc & BD_ENET_TX_UN) /* Underrun */ 370 fep->stats.tx_fifo_errors++; 371 if (sc & BD_ENET_TX_CSL) /* Carrier lost */ 372 fep->stats.tx_carrier_errors++; 373 374 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) { 375 fep->stats.tx_errors++; 376 do_restart = 1; 377 } 378 } else 379 fep->stats.tx_packets++; 380 381 if (sc & BD_ENET_TX_READY) { 382 dev_warn(fep->dev, 383 "HEY! Enet xmit interrupt and TX_READY.\n"); 384 } 385 386 /* 387 * Deferred means some collisions occurred during transmit, 388 * but we eventually sent the packet OK. 389 */ 390 if (sc & BD_ENET_TX_DEF) 391 fep->stats.collisions++; 392 393 /* unmap */ 394 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 395 skb->len, DMA_TO_DEVICE); 396 397 /* 398 * Free the sk buffer associated with this last transmit. 399 */ 400 dev_kfree_skb_irq(skb); 401 fep->tx_skbuff[dirtyidx] = NULL; 402 403 /* 404 * Update pointer to next buffer descriptor to be transmitted. 405 */ 406 if ((sc & BD_ENET_TX_WRAP) == 0) 407 bdp++; 408 else 409 bdp = fep->tx_bd_base; 410 411 /* 412 * Since we have freed up a buffer, the ring is no longer 413 * full. 414 */ 415 if (!fep->tx_free++) 416 do_wake = 1; 417 } 418 419 fep->dirty_tx = bdp; 420 421 if (do_restart) 422 (*fep->ops->tx_restart)(dev); 423 424 spin_unlock(&fep->tx_lock); 425 426 if (do_wake) 427 netif_wake_queue(dev); 428 } 429 430 /* 431 * The interrupt handler. 432 * This is called from the MPC core interrupt. 433 */ 434 static irqreturn_t 435 fs_enet_interrupt(int irq, void *dev_id) 436 { 437 struct net_device *dev = dev_id; 438 struct fs_enet_private *fep; 439 const struct fs_platform_info *fpi; 440 u32 int_events; 441 u32 int_clr_events; 442 int nr, napi_ok; 443 int handled; 444 445 fep = netdev_priv(dev); 446 fpi = fep->fpi; 447 448 nr = 0; 449 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) { 450 nr++; 451 452 int_clr_events = int_events; 453 if (fpi->use_napi) 454 int_clr_events &= ~fep->ev_napi_rx; 455 456 (*fep->ops->clear_int_events)(dev, int_clr_events); 457 458 if (int_events & fep->ev_err) 459 (*fep->ops->ev_error)(dev, int_events); 460 461 if (int_events & fep->ev_rx) { 462 if (!fpi->use_napi) 463 fs_enet_rx_non_napi(dev); 464 else { 465 napi_ok = napi_schedule_prep(&fep->napi); 466 467 (*fep->ops->napi_disable_rx)(dev); 468 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx); 469 470 /* NOTE: it is possible for FCCs in NAPI mode */ 471 /* to submit a spurious interrupt while in poll */ 472 if (napi_ok) 473 __napi_schedule(&fep->napi); 474 } 475 } 476 477 if (int_events & fep->ev_tx) 478 fs_enet_tx(dev); 479 } 480 481 handled = nr > 0; 482 return IRQ_RETVAL(handled); 483 } 484 485 void fs_init_bds(struct net_device *dev) 486 { 487 struct fs_enet_private *fep = netdev_priv(dev); 488 cbd_t __iomem *bdp; 489 struct sk_buff *skb; 490 int i; 491 492 fs_cleanup_bds(dev); 493 494 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base; 495 fep->tx_free = fep->tx_ring; 496 fep->cur_rx = fep->rx_bd_base; 497 498 /* 499 * Initialize the receive buffer descriptors. 500 */ 501 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { 502 skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE); 503 if (skb == NULL) 504 break; 505 506 skb_align(skb, ENET_RX_ALIGN); 507 fep->rx_skbuff[i] = skb; 508 CBDW_BUFADDR(bdp, 509 dma_map_single(fep->dev, skb->data, 510 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 511 DMA_FROM_DEVICE)); 512 CBDW_DATLEN(bdp, 0); /* zero */ 513 CBDW_SC(bdp, BD_ENET_RX_EMPTY | 514 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP)); 515 } 516 /* 517 * if we failed, fillup remainder 518 */ 519 for (; i < fep->rx_ring; i++, bdp++) { 520 fep->rx_skbuff[i] = NULL; 521 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP); 522 } 523 524 /* 525 * ...and the same for transmit. 526 */ 527 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { 528 fep->tx_skbuff[i] = NULL; 529 CBDW_BUFADDR(bdp, 0); 530 CBDW_DATLEN(bdp, 0); 531 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP); 532 } 533 } 534 535 void fs_cleanup_bds(struct net_device *dev) 536 { 537 struct fs_enet_private *fep = netdev_priv(dev); 538 struct sk_buff *skb; 539 cbd_t __iomem *bdp; 540 int i; 541 542 /* 543 * Reset SKB transmit buffers. 544 */ 545 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { 546 if ((skb = fep->tx_skbuff[i]) == NULL) 547 continue; 548 549 /* unmap */ 550 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 551 skb->len, DMA_TO_DEVICE); 552 553 fep->tx_skbuff[i] = NULL; 554 dev_kfree_skb(skb); 555 } 556 557 /* 558 * Reset SKB receive buffers 559 */ 560 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { 561 if ((skb = fep->rx_skbuff[i]) == NULL) 562 continue; 563 564 /* unmap */ 565 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), 566 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), 567 DMA_FROM_DEVICE); 568 569 fep->rx_skbuff[i] = NULL; 570 571 dev_kfree_skb(skb); 572 } 573 } 574 575 /**********************************************************************************/ 576 577 #ifdef CONFIG_FS_ENET_MPC5121_FEC 578 /* 579 * MPC5121 FEC requeries 4-byte alignment for TX data buffer! 580 */ 581 static struct sk_buff *tx_skb_align_workaround(struct net_device *dev, 582 struct sk_buff *skb) 583 { 584 struct sk_buff *new_skb; 585 586 /* Alloc new skb */ 587 new_skb = netdev_alloc_skb(dev, skb->len + 4); 588 if (!new_skb) 589 return NULL; 590 591 /* Make sure new skb is properly aligned */ 592 skb_align(new_skb, 4); 593 594 /* Copy data to new skb ... */ 595 skb_copy_from_linear_data(skb, new_skb->data, skb->len); 596 skb_put(new_skb, skb->len); 597 598 /* ... and free an old one */ 599 dev_kfree_skb_any(skb); 600 601 return new_skb; 602 } 603 #endif 604 605 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev) 606 { 607 struct fs_enet_private *fep = netdev_priv(dev); 608 cbd_t __iomem *bdp; 609 int curidx; 610 u16 sc; 611 unsigned long flags; 612 613 #ifdef CONFIG_FS_ENET_MPC5121_FEC 614 if (((unsigned long)skb->data) & 0x3) { 615 skb = tx_skb_align_workaround(dev, skb); 616 if (!skb) { 617 /* 618 * We have lost packet due to memory allocation error 619 * in tx_skb_align_workaround(). Hopefully original 620 * skb is still valid, so try transmit it later. 621 */ 622 return NETDEV_TX_BUSY; 623 } 624 } 625 #endif 626 spin_lock_irqsave(&fep->tx_lock, flags); 627 628 /* 629 * Fill in a Tx ring entry 630 */ 631 bdp = fep->cur_tx; 632 633 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) { 634 netif_stop_queue(dev); 635 spin_unlock_irqrestore(&fep->tx_lock, flags); 636 637 /* 638 * Ooops. All transmit buffers are full. Bail out. 639 * This should not happen, since the tx queue should be stopped. 640 */ 641 dev_warn(fep->dev, "tx queue full!.\n"); 642 return NETDEV_TX_BUSY; 643 } 644 645 curidx = bdp - fep->tx_bd_base; 646 /* 647 * Clear all of the status flags. 648 */ 649 CBDC_SC(bdp, BD_ENET_TX_STATS); 650 651 /* 652 * Save skb pointer. 653 */ 654 fep->tx_skbuff[curidx] = skb; 655 656 fep->stats.tx_bytes += skb->len; 657 658 /* 659 * Push the data cache so the CPM does not get stale memory data. 660 */ 661 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, 662 skb->data, skb->len, DMA_TO_DEVICE)); 663 CBDW_DATLEN(bdp, skb->len); 664 665 /* 666 * If this was the last BD in the ring, start at the beginning again. 667 */ 668 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0) 669 fep->cur_tx++; 670 else 671 fep->cur_tx = fep->tx_bd_base; 672 673 if (!--fep->tx_free) 674 netif_stop_queue(dev); 675 676 /* Trigger transmission start */ 677 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR | 678 BD_ENET_TX_LAST | BD_ENET_TX_TC; 679 680 /* note that while FEC does not have this bit 681 * it marks it as available for software use 682 * yay for hw reuse :) */ 683 if (skb->len <= 60) 684 sc |= BD_ENET_TX_PAD; 685 CBDS_SC(bdp, sc); 686 687 skb_tx_timestamp(skb); 688 689 (*fep->ops->tx_kickstart)(dev); 690 691 spin_unlock_irqrestore(&fep->tx_lock, flags); 692 693 return NETDEV_TX_OK; 694 } 695 696 static void fs_timeout(struct net_device *dev) 697 { 698 struct fs_enet_private *fep = netdev_priv(dev); 699 unsigned long flags; 700 int wake = 0; 701 702 fep->stats.tx_errors++; 703 704 spin_lock_irqsave(&fep->lock, flags); 705 706 if (dev->flags & IFF_UP) { 707 phy_stop(fep->phydev); 708 (*fep->ops->stop)(dev); 709 (*fep->ops->restart)(dev); 710 phy_start(fep->phydev); 711 } 712 713 phy_start(fep->phydev); 714 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY); 715 spin_unlock_irqrestore(&fep->lock, flags); 716 717 if (wake) 718 netif_wake_queue(dev); 719 } 720 721 /*----------------------------------------------------------------------------- 722 * generic link-change handler - should be sufficient for most cases 723 *-----------------------------------------------------------------------------*/ 724 static void generic_adjust_link(struct net_device *dev) 725 { 726 struct fs_enet_private *fep = netdev_priv(dev); 727 struct phy_device *phydev = fep->phydev; 728 int new_state = 0; 729 730 if (phydev->link) { 731 /* adjust to duplex mode */ 732 if (phydev->duplex != fep->oldduplex) { 733 new_state = 1; 734 fep->oldduplex = phydev->duplex; 735 } 736 737 if (phydev->speed != fep->oldspeed) { 738 new_state = 1; 739 fep->oldspeed = phydev->speed; 740 } 741 742 if (!fep->oldlink) { 743 new_state = 1; 744 fep->oldlink = 1; 745 } 746 747 if (new_state) 748 fep->ops->restart(dev); 749 } else if (fep->oldlink) { 750 new_state = 1; 751 fep->oldlink = 0; 752 fep->oldspeed = 0; 753 fep->oldduplex = -1; 754 } 755 756 if (new_state && netif_msg_link(fep)) 757 phy_print_status(phydev); 758 } 759 760 761 static void fs_adjust_link(struct net_device *dev) 762 { 763 struct fs_enet_private *fep = netdev_priv(dev); 764 unsigned long flags; 765 766 spin_lock_irqsave(&fep->lock, flags); 767 768 if(fep->ops->adjust_link) 769 fep->ops->adjust_link(dev); 770 else 771 generic_adjust_link(dev); 772 773 spin_unlock_irqrestore(&fep->lock, flags); 774 } 775 776 static int fs_init_phy(struct net_device *dev) 777 { 778 struct fs_enet_private *fep = netdev_priv(dev); 779 struct phy_device *phydev; 780 phy_interface_t iface; 781 782 fep->oldlink = 0; 783 fep->oldspeed = 0; 784 fep->oldduplex = -1; 785 786 iface = fep->fpi->use_rmii ? 787 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII; 788 789 phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0, 790 iface); 791 if (!phydev) { 792 phydev = of_phy_connect_fixed_link(dev, &fs_adjust_link, 793 iface); 794 } 795 if (!phydev) { 796 dev_err(&dev->dev, "Could not attach to PHY\n"); 797 return -ENODEV; 798 } 799 800 fep->phydev = phydev; 801 802 return 0; 803 } 804 805 static int fs_enet_open(struct net_device *dev) 806 { 807 struct fs_enet_private *fep = netdev_priv(dev); 808 int r; 809 int err; 810 811 /* to initialize the fep->cur_rx,... */ 812 /* not doing this, will cause a crash in fs_enet_rx_napi */ 813 fs_init_bds(fep->ndev); 814 815 if (fep->fpi->use_napi) 816 napi_enable(&fep->napi); 817 818 /* Install our interrupt handler. */ 819 r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED, 820 "fs_enet-mac", dev); 821 if (r != 0) { 822 dev_err(fep->dev, "Could not allocate FS_ENET IRQ!"); 823 if (fep->fpi->use_napi) 824 napi_disable(&fep->napi); 825 return -EINVAL; 826 } 827 828 err = fs_init_phy(dev); 829 if (err) { 830 free_irq(fep->interrupt, dev); 831 if (fep->fpi->use_napi) 832 napi_disable(&fep->napi); 833 return err; 834 } 835 phy_start(fep->phydev); 836 837 netif_start_queue(dev); 838 839 return 0; 840 } 841 842 static int fs_enet_close(struct net_device *dev) 843 { 844 struct fs_enet_private *fep = netdev_priv(dev); 845 unsigned long flags; 846 847 netif_stop_queue(dev); 848 netif_carrier_off(dev); 849 if (fep->fpi->use_napi) 850 napi_disable(&fep->napi); 851 phy_stop(fep->phydev); 852 853 spin_lock_irqsave(&fep->lock, flags); 854 spin_lock(&fep->tx_lock); 855 (*fep->ops->stop)(dev); 856 spin_unlock(&fep->tx_lock); 857 spin_unlock_irqrestore(&fep->lock, flags); 858 859 /* release any irqs */ 860 phy_disconnect(fep->phydev); 861 fep->phydev = NULL; 862 free_irq(fep->interrupt, dev); 863 864 return 0; 865 } 866 867 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev) 868 { 869 struct fs_enet_private *fep = netdev_priv(dev); 870 return &fep->stats; 871 } 872 873 /*************************************************************************/ 874 875 static void fs_get_drvinfo(struct net_device *dev, 876 struct ethtool_drvinfo *info) 877 { 878 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 879 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); 880 } 881 882 static int fs_get_regs_len(struct net_device *dev) 883 { 884 struct fs_enet_private *fep = netdev_priv(dev); 885 886 return (*fep->ops->get_regs_len)(dev); 887 } 888 889 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs, 890 void *p) 891 { 892 struct fs_enet_private *fep = netdev_priv(dev); 893 unsigned long flags; 894 int r, len; 895 896 len = regs->len; 897 898 spin_lock_irqsave(&fep->lock, flags); 899 r = (*fep->ops->get_regs)(dev, p, &len); 900 spin_unlock_irqrestore(&fep->lock, flags); 901 902 if (r == 0) 903 regs->version = 0; 904 } 905 906 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 907 { 908 struct fs_enet_private *fep = netdev_priv(dev); 909 910 if (!fep->phydev) 911 return -ENODEV; 912 913 return phy_ethtool_gset(fep->phydev, cmd); 914 } 915 916 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 917 { 918 struct fs_enet_private *fep = netdev_priv(dev); 919 920 if (!fep->phydev) 921 return -ENODEV; 922 923 return phy_ethtool_sset(fep->phydev, cmd); 924 } 925 926 static int fs_nway_reset(struct net_device *dev) 927 { 928 return 0; 929 } 930 931 static u32 fs_get_msglevel(struct net_device *dev) 932 { 933 struct fs_enet_private *fep = netdev_priv(dev); 934 return fep->msg_enable; 935 } 936 937 static void fs_set_msglevel(struct net_device *dev, u32 value) 938 { 939 struct fs_enet_private *fep = netdev_priv(dev); 940 fep->msg_enable = value; 941 } 942 943 static const struct ethtool_ops fs_ethtool_ops = { 944 .get_drvinfo = fs_get_drvinfo, 945 .get_regs_len = fs_get_regs_len, 946 .get_settings = fs_get_settings, 947 .set_settings = fs_set_settings, 948 .nway_reset = fs_nway_reset, 949 .get_link = ethtool_op_get_link, 950 .get_msglevel = fs_get_msglevel, 951 .set_msglevel = fs_set_msglevel, 952 .get_regs = fs_get_regs, 953 .get_ts_info = ethtool_op_get_ts_info, 954 }; 955 956 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 957 { 958 struct fs_enet_private *fep = netdev_priv(dev); 959 960 if (!netif_running(dev)) 961 return -EINVAL; 962 963 return phy_mii_ioctl(fep->phydev, rq, cmd); 964 } 965 966 extern int fs_mii_connect(struct net_device *dev); 967 extern void fs_mii_disconnect(struct net_device *dev); 968 969 /**************************************************************************************/ 970 971 #ifdef CONFIG_FS_ENET_HAS_FEC 972 #define IS_FEC(match) ((match)->data == &fs_fec_ops) 973 #else 974 #define IS_FEC(match) 0 975 #endif 976 977 static const struct net_device_ops fs_enet_netdev_ops = { 978 .ndo_open = fs_enet_open, 979 .ndo_stop = fs_enet_close, 980 .ndo_get_stats = fs_enet_get_stats, 981 .ndo_start_xmit = fs_enet_start_xmit, 982 .ndo_tx_timeout = fs_timeout, 983 .ndo_set_rx_mode = fs_set_multicast_list, 984 .ndo_do_ioctl = fs_ioctl, 985 .ndo_validate_addr = eth_validate_addr, 986 .ndo_set_mac_address = eth_mac_addr, 987 .ndo_change_mtu = eth_change_mtu, 988 #ifdef CONFIG_NET_POLL_CONTROLLER 989 .ndo_poll_controller = fs_enet_netpoll, 990 #endif 991 }; 992 993 static struct of_device_id fs_enet_match[]; 994 static int fs_enet_probe(struct platform_device *ofdev) 995 { 996 const struct of_device_id *match; 997 struct net_device *ndev; 998 struct fs_enet_private *fep; 999 struct fs_platform_info *fpi; 1000 const u32 *data; 1001 struct clk *clk; 1002 int err; 1003 const u8 *mac_addr; 1004 const char *phy_connection_type; 1005 int privsize, len, ret = -ENODEV; 1006 1007 match = of_match_device(fs_enet_match, &ofdev->dev); 1008 if (!match) 1009 return -EINVAL; 1010 1011 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL); 1012 if (!fpi) 1013 return -ENOMEM; 1014 1015 if (!IS_FEC(match)) { 1016 data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len); 1017 if (!data || len != 4) 1018 goto out_free_fpi; 1019 1020 fpi->cp_command = *data; 1021 } 1022 1023 fpi->rx_ring = 32; 1024 fpi->tx_ring = 32; 1025 fpi->rx_copybreak = 240; 1026 fpi->use_napi = 1; 1027 fpi->napi_weight = 17; 1028 fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0); 1029 if ((!fpi->phy_node) && (!of_get_property(ofdev->dev.of_node, "fixed-link", 1030 NULL))) 1031 goto out_free_fpi; 1032 1033 if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) { 1034 phy_connection_type = of_get_property(ofdev->dev.of_node, 1035 "phy-connection-type", NULL); 1036 if (phy_connection_type && !strcmp("rmii", phy_connection_type)) 1037 fpi->use_rmii = 1; 1038 } 1039 1040 /* make clock lookup non-fatal (the driver is shared among platforms), 1041 * but require enable to succeed when a clock was specified/found, 1042 * keep a reference to the clock upon successful acquisition 1043 */ 1044 clk = devm_clk_get(&ofdev->dev, "per"); 1045 if (!IS_ERR(clk)) { 1046 err = clk_prepare_enable(clk); 1047 if (err) { 1048 ret = err; 1049 goto out_free_fpi; 1050 } 1051 fpi->clk_per = clk; 1052 } 1053 1054 privsize = sizeof(*fep) + 1055 sizeof(struct sk_buff **) * 1056 (fpi->rx_ring + fpi->tx_ring); 1057 1058 ndev = alloc_etherdev(privsize); 1059 if (!ndev) { 1060 ret = -ENOMEM; 1061 goto out_put; 1062 } 1063 1064 SET_NETDEV_DEV(ndev, &ofdev->dev); 1065 platform_set_drvdata(ofdev, ndev); 1066 1067 fep = netdev_priv(ndev); 1068 fep->dev = &ofdev->dev; 1069 fep->ndev = ndev; 1070 fep->fpi = fpi; 1071 fep->ops = match->data; 1072 1073 ret = fep->ops->setup_data(ndev); 1074 if (ret) 1075 goto out_free_dev; 1076 1077 fep->rx_skbuff = (struct sk_buff **)&fep[1]; 1078 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring; 1079 1080 spin_lock_init(&fep->lock); 1081 spin_lock_init(&fep->tx_lock); 1082 1083 mac_addr = of_get_mac_address(ofdev->dev.of_node); 1084 if (mac_addr) 1085 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN); 1086 1087 ret = fep->ops->allocate_bd(ndev); 1088 if (ret) 1089 goto out_cleanup_data; 1090 1091 fep->rx_bd_base = fep->ring_base; 1092 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring; 1093 1094 fep->tx_ring = fpi->tx_ring; 1095 fep->rx_ring = fpi->rx_ring; 1096 1097 ndev->netdev_ops = &fs_enet_netdev_ops; 1098 ndev->watchdog_timeo = 2 * HZ; 1099 if (fpi->use_napi) 1100 netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi, 1101 fpi->napi_weight); 1102 1103 ndev->ethtool_ops = &fs_ethtool_ops; 1104 1105 init_timer(&fep->phy_timer_list); 1106 1107 netif_carrier_off(ndev); 1108 1109 ret = register_netdev(ndev); 1110 if (ret) 1111 goto out_free_bd; 1112 1113 pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr); 1114 1115 return 0; 1116 1117 out_free_bd: 1118 fep->ops->free_bd(ndev); 1119 out_cleanup_data: 1120 fep->ops->cleanup_data(ndev); 1121 out_free_dev: 1122 free_netdev(ndev); 1123 out_put: 1124 of_node_put(fpi->phy_node); 1125 if (fpi->clk_per) 1126 clk_disable_unprepare(fpi->clk_per); 1127 out_free_fpi: 1128 kfree(fpi); 1129 return ret; 1130 } 1131 1132 static int fs_enet_remove(struct platform_device *ofdev) 1133 { 1134 struct net_device *ndev = platform_get_drvdata(ofdev); 1135 struct fs_enet_private *fep = netdev_priv(ndev); 1136 1137 unregister_netdev(ndev); 1138 1139 fep->ops->free_bd(ndev); 1140 fep->ops->cleanup_data(ndev); 1141 dev_set_drvdata(fep->dev, NULL); 1142 of_node_put(fep->fpi->phy_node); 1143 if (fep->fpi->clk_per) 1144 clk_disable_unprepare(fep->fpi->clk_per); 1145 free_netdev(ndev); 1146 return 0; 1147 } 1148 1149 static struct of_device_id fs_enet_match[] = { 1150 #ifdef CONFIG_FS_ENET_HAS_SCC 1151 { 1152 .compatible = "fsl,cpm1-scc-enet", 1153 .data = (void *)&fs_scc_ops, 1154 }, 1155 { 1156 .compatible = "fsl,cpm2-scc-enet", 1157 .data = (void *)&fs_scc_ops, 1158 }, 1159 #endif 1160 #ifdef CONFIG_FS_ENET_HAS_FCC 1161 { 1162 .compatible = "fsl,cpm2-fcc-enet", 1163 .data = (void *)&fs_fcc_ops, 1164 }, 1165 #endif 1166 #ifdef CONFIG_FS_ENET_HAS_FEC 1167 #ifdef CONFIG_FS_ENET_MPC5121_FEC 1168 { 1169 .compatible = "fsl,mpc5121-fec", 1170 .data = (void *)&fs_fec_ops, 1171 }, 1172 { 1173 .compatible = "fsl,mpc5125-fec", 1174 .data = (void *)&fs_fec_ops, 1175 }, 1176 #else 1177 { 1178 .compatible = "fsl,pq1-fec-enet", 1179 .data = (void *)&fs_fec_ops, 1180 }, 1181 #endif 1182 #endif 1183 {} 1184 }; 1185 MODULE_DEVICE_TABLE(of, fs_enet_match); 1186 1187 static struct platform_driver fs_enet_driver = { 1188 .driver = { 1189 .owner = THIS_MODULE, 1190 .name = "fs_enet", 1191 .of_match_table = fs_enet_match, 1192 }, 1193 .probe = fs_enet_probe, 1194 .remove = fs_enet_remove, 1195 }; 1196 1197 #ifdef CONFIG_NET_POLL_CONTROLLER 1198 static void fs_enet_netpoll(struct net_device *dev) 1199 { 1200 disable_irq(dev->irq); 1201 fs_enet_interrupt(dev->irq, dev); 1202 enable_irq(dev->irq); 1203 } 1204 #endif 1205 1206 module_platform_driver(fs_enet_driver); 1207