linux/drivers/net/ethernet/freescale/fec_mpc52xx.c
<<
>>
Prefs
   1/*
   2 * Driver for the MPC5200 Fast Ethernet Controller
   3 *
   4 * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
   5 * now maintained by Sylvain Munaut <tnt@246tNt.com>
   6 *
   7 * Copyright (C) 2007  Domen Puncer, Telargo, Inc.
   8 * Copyright (C) 2007  Sylvain Munaut <tnt@246tNt.com>
   9 * Copyright (C) 2003-2004  MontaVista, Software, Inc.
  10 *
  11 * This file is licensed under the terms of the GNU General Public License
  12 * version 2. This program is licensed "as is" without any warranty of any
  13 * kind, whether express or implied.
  14 *
  15 */
  16
  17#include <linux/dma-mapping.h>
  18#include <linux/module.h>
  19
  20#include <linux/kernel.h>
  21#include <linux/types.h>
  22#include <linux/spinlock.h>
  23#include <linux/slab.h>
  24#include <linux/errno.h>
  25#include <linux/init.h>
  26#include <linux/interrupt.h>
  27#include <linux/crc32.h>
  28#include <linux/hardirq.h>
  29#include <linux/delay.h>
  30#include <linux/of_device.h>
  31#include <linux/of_mdio.h>
  32#include <linux/of_net.h>
  33#include <linux/of_platform.h>
  34
  35#include <linux/netdevice.h>
  36#include <linux/etherdevice.h>
  37#include <linux/ethtool.h>
  38#include <linux/skbuff.h>
  39
  40#include <asm/io.h>
  41#include <asm/delay.h>
  42#include <asm/mpc52xx.h>
  43
  44#include <linux/fsl/bestcomm/bestcomm.h>
  45#include <linux/fsl/bestcomm/fec.h>
  46
  47#include "fec_mpc52xx.h"
  48
  49#define DRIVER_NAME "mpc52xx-fec"
  50
  51/* Private driver data structure */
  52struct mpc52xx_fec_priv {
  53        struct net_device *ndev;
  54        int duplex;
  55        int speed;
  56        int r_irq;
  57        int t_irq;
  58        struct mpc52xx_fec __iomem *fec;
  59        struct bcom_task *rx_dmatsk;
  60        struct bcom_task *tx_dmatsk;
  61        spinlock_t lock;
  62        int msg_enable;
  63
  64        /* MDIO link details */
  65        unsigned int mdio_speed;
  66        struct device_node *phy_node;
  67        struct phy_device *phydev;
  68        enum phy_state link;
  69        int seven_wire_mode;
  70};
  71
  72
  73static irqreturn_t mpc52xx_fec_interrupt(int, void *);
  74static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
  75static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
  76static void mpc52xx_fec_stop(struct net_device *dev);
  77static void mpc52xx_fec_start(struct net_device *dev);
  78static void mpc52xx_fec_reset(struct net_device *dev);
  79
  80#define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  81                NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
  82static int debug = -1;  /* the above default */
  83module_param(debug, int, 0);
  84MODULE_PARM_DESC(debug, "debugging messages level");
  85
  86static void mpc52xx_fec_tx_timeout(struct net_device *dev)
  87{
  88        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
  89        unsigned long flags;
  90
  91        dev_warn(&dev->dev, "transmit timed out\n");
  92
  93        spin_lock_irqsave(&priv->lock, flags);
  94        mpc52xx_fec_reset(dev);
  95        dev->stats.tx_errors++;
  96        spin_unlock_irqrestore(&priv->lock, flags);
  97
  98        netif_wake_queue(dev);
  99}
 100
 101static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
 102{
 103        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 104        struct mpc52xx_fec __iomem *fec = priv->fec;
 105
 106        out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
 107        out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
 108}
 109
 110static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
 111{
 112        struct sockaddr *sock = addr;
 113
 114        memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
 115
 116        mpc52xx_fec_set_paddr(dev, sock->sa_data);
 117        return 0;
 118}
 119
 120static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
 121{
 122        while (!bcom_queue_empty(s)) {
 123                struct bcom_fec_bd *bd;
 124                struct sk_buff *skb;
 125
 126                skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
 127                dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
 128                                 DMA_FROM_DEVICE);
 129                kfree_skb(skb);
 130        }
 131}
 132
 133static void
 134mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
 135{
 136        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 137        struct bcom_fec_bd *bd;
 138
 139        bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
 140        bd->status = FEC_RX_BUFFER_SIZE;
 141        bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
 142                                    FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
 143        bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
 144}
 145
 146static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
 147{
 148        struct sk_buff *skb;
 149
 150        while (!bcom_queue_full(rxtsk)) {
 151                skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
 152                if (!skb)
 153                        return -EAGAIN;
 154
 155                /* zero out the initial receive buffers to aid debugging */
 156                memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
 157                mpc52xx_fec_rx_submit(dev, skb);
 158        }
 159        return 0;
 160}
 161
 162/* based on generic_adjust_link from fs_enet-main.c */
 163static void mpc52xx_fec_adjust_link(struct net_device *dev)
 164{
 165        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 166        struct phy_device *phydev = priv->phydev;
 167        int new_state = 0;
 168
 169        if (phydev->link != PHY_DOWN) {
 170                if (phydev->duplex != priv->duplex) {
 171                        struct mpc52xx_fec __iomem *fec = priv->fec;
 172                        u32 rcntrl;
 173                        u32 tcntrl;
 174
 175                        new_state = 1;
 176                        priv->duplex = phydev->duplex;
 177
 178                        rcntrl = in_be32(&fec->r_cntrl);
 179                        tcntrl = in_be32(&fec->x_cntrl);
 180
 181                        rcntrl &= ~FEC_RCNTRL_DRT;
 182                        tcntrl &= ~FEC_TCNTRL_FDEN;
 183                        if (phydev->duplex == DUPLEX_FULL)
 184                                tcntrl |= FEC_TCNTRL_FDEN;      /* FD enable */
 185                        else
 186                                rcntrl |= FEC_RCNTRL_DRT;       /* disable Rx on Tx (HD) */
 187
 188                        out_be32(&fec->r_cntrl, rcntrl);
 189                        out_be32(&fec->x_cntrl, tcntrl);
 190                }
 191
 192                if (phydev->speed != priv->speed) {
 193                        new_state = 1;
 194                        priv->speed = phydev->speed;
 195                }
 196
 197                if (priv->link == PHY_DOWN) {
 198                        new_state = 1;
 199                        priv->link = phydev->link;
 200                }
 201
 202        } else if (priv->link) {
 203                new_state = 1;
 204                priv->link = PHY_DOWN;
 205                priv->speed = 0;
 206                priv->duplex = -1;
 207        }
 208
 209        if (new_state && netif_msg_link(priv))
 210                phy_print_status(phydev);
 211}
 212
 213static int mpc52xx_fec_open(struct net_device *dev)
 214{
 215        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 216        int err = -EBUSY;
 217
 218        if (priv->phy_node) {
 219                priv->phydev = of_phy_connect(priv->ndev, priv->phy_node,
 220                                              mpc52xx_fec_adjust_link, 0, 0);
 221                if (!priv->phydev) {
 222                        dev_err(&dev->dev, "of_phy_connect failed\n");
 223                        return -ENODEV;
 224                }
 225                phy_start(priv->phydev);
 226        }
 227
 228        if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
 229                        DRIVER_NAME "_ctrl", dev)) {
 230                dev_err(&dev->dev, "ctrl interrupt request failed\n");
 231                goto free_phy;
 232        }
 233        if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
 234                        DRIVER_NAME "_rx", dev)) {
 235                dev_err(&dev->dev, "rx interrupt request failed\n");
 236                goto free_ctrl_irq;
 237        }
 238        if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
 239                        DRIVER_NAME "_tx", dev)) {
 240                dev_err(&dev->dev, "tx interrupt request failed\n");
 241                goto free_2irqs;
 242        }
 243
 244        bcom_fec_rx_reset(priv->rx_dmatsk);
 245        bcom_fec_tx_reset(priv->tx_dmatsk);
 246
 247        err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
 248        if (err) {
 249                dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
 250                goto free_irqs;
 251        }
 252
 253        bcom_enable(priv->rx_dmatsk);
 254        bcom_enable(priv->tx_dmatsk);
 255
 256        mpc52xx_fec_start(dev);
 257
 258        netif_start_queue(dev);
 259
 260        return 0;
 261
 262 free_irqs:
 263        free_irq(priv->t_irq, dev);
 264 free_2irqs:
 265        free_irq(priv->r_irq, dev);
 266 free_ctrl_irq:
 267        free_irq(dev->irq, dev);
 268 free_phy:
 269        if (priv->phydev) {
 270                phy_stop(priv->phydev);
 271                phy_disconnect(priv->phydev);
 272                priv->phydev = NULL;
 273        }
 274
 275        return err;
 276}
 277
 278static int mpc52xx_fec_close(struct net_device *dev)
 279{
 280        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 281
 282        netif_stop_queue(dev);
 283
 284        mpc52xx_fec_stop(dev);
 285
 286        mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
 287
 288        free_irq(dev->irq, dev);
 289        free_irq(priv->r_irq, dev);
 290        free_irq(priv->t_irq, dev);
 291
 292        if (priv->phydev) {
 293                /* power down phy */
 294                phy_stop(priv->phydev);
 295                phy_disconnect(priv->phydev);
 296                priv->phydev = NULL;
 297        }
 298
 299        return 0;
 300}
 301
 302/* This will only be invoked if your driver is _not_ in XOFF state.
 303 * What this means is that you need not check it, and that this
 304 * invariant will hold if you make sure that the netif_*_queue()
 305 * calls are done at the proper times.
 306 */
 307static int mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
 308{
 309        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 310        struct bcom_fec_bd *bd;
 311        unsigned long flags;
 312
 313        if (bcom_queue_full(priv->tx_dmatsk)) {
 314                if (net_ratelimit())
 315                        dev_err(&dev->dev, "transmit queue overrun\n");
 316                return NETDEV_TX_BUSY;
 317        }
 318
 319        spin_lock_irqsave(&priv->lock, flags);
 320
 321        bd = (struct bcom_fec_bd *)
 322                bcom_prepare_next_buffer(priv->tx_dmatsk);
 323
 324        bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
 325        bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
 326                                    DMA_TO_DEVICE);
 327
 328        skb_tx_timestamp(skb);
 329        bcom_submit_next_buffer(priv->tx_dmatsk, skb);
 330        spin_unlock_irqrestore(&priv->lock, flags);
 331
 332        if (bcom_queue_full(priv->tx_dmatsk)) {
 333                netif_stop_queue(dev);
 334        }
 335
 336        return NETDEV_TX_OK;
 337}
 338
 339#ifdef CONFIG_NET_POLL_CONTROLLER
 340static void mpc52xx_fec_poll_controller(struct net_device *dev)
 341{
 342        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 343
 344        disable_irq(priv->t_irq);
 345        mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
 346        enable_irq(priv->t_irq);
 347        disable_irq(priv->r_irq);
 348        mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
 349        enable_irq(priv->r_irq);
 350}
 351#endif
 352
 353
 354/* This handles BestComm transmit task interrupts
 355 */
 356static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
 357{
 358        struct net_device *dev = dev_id;
 359        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 360
 361        spin_lock(&priv->lock);
 362        while (bcom_buffer_done(priv->tx_dmatsk)) {
 363                struct sk_buff *skb;
 364                struct bcom_fec_bd *bd;
 365                skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
 366                                (struct bcom_bd **)&bd);
 367                dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
 368                                 DMA_TO_DEVICE);
 369
 370                dev_kfree_skb_irq(skb);
 371        }
 372        spin_unlock(&priv->lock);
 373
 374        netif_wake_queue(dev);
 375
 376        return IRQ_HANDLED;
 377}
 378
 379static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
 380{
 381        struct net_device *dev = dev_id;
 382        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 383        struct sk_buff *rskb; /* received sk_buff */
 384        struct sk_buff *skb;  /* new sk_buff to enqueue in its place */
 385        struct bcom_fec_bd *bd;
 386        u32 status, physaddr;
 387        int length;
 388
 389        spin_lock(&priv->lock);
 390
 391        while (bcom_buffer_done(priv->rx_dmatsk)) {
 392
 393                rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
 394                                            (struct bcom_bd **)&bd);
 395                physaddr = bd->skb_pa;
 396
 397                /* Test for errors in received frame */
 398                if (status & BCOM_FEC_RX_BD_ERRORS) {
 399                        /* Drop packet and reuse the buffer */
 400                        mpc52xx_fec_rx_submit(dev, rskb);
 401                        dev->stats.rx_dropped++;
 402                        continue;
 403                }
 404
 405                /* skbs are allocated on open, so now we allocate a new one,
 406                 * and remove the old (with the packet) */
 407                skb = netdev_alloc_skb(dev, FEC_RX_BUFFER_SIZE);
 408                if (!skb) {
 409                        /* Can't get a new one : reuse the same & drop pkt */
 410                        dev_notice(&dev->dev, "Low memory - dropped packet.\n");
 411                        mpc52xx_fec_rx_submit(dev, rskb);
 412                        dev->stats.rx_dropped++;
 413                        continue;
 414                }
 415
 416                /* Enqueue the new sk_buff back on the hardware */
 417                mpc52xx_fec_rx_submit(dev, skb);
 418
 419                /* Process the received skb - Drop the spin lock while
 420                 * calling into the network stack */
 421                spin_unlock(&priv->lock);
 422
 423                dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
 424                                 DMA_FROM_DEVICE);
 425                length = status & BCOM_FEC_RX_BD_LEN_MASK;
 426                skb_put(rskb, length - 4);      /* length without CRC32 */
 427                rskb->protocol = eth_type_trans(rskb, dev);
 428                if (!skb_defer_rx_timestamp(rskb))
 429                        netif_rx(rskb);
 430
 431                spin_lock(&priv->lock);
 432        }
 433
 434        spin_unlock(&priv->lock);
 435
 436        return IRQ_HANDLED;
 437}
 438
 439static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
 440{
 441        struct net_device *dev = dev_id;
 442        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 443        struct mpc52xx_fec __iomem *fec = priv->fec;
 444        u32 ievent;
 445
 446        ievent = in_be32(&fec->ievent);
 447
 448        ievent &= ~FEC_IEVENT_MII;      /* mii is handled separately */
 449        if (!ievent)
 450                return IRQ_NONE;
 451
 452        out_be32(&fec->ievent, ievent);         /* clear pending events */
 453
 454        /* on fifo error, soft-reset fec */
 455        if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
 456
 457                if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
 458                        dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
 459                if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
 460                        dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
 461
 462                spin_lock(&priv->lock);
 463                mpc52xx_fec_reset(dev);
 464                spin_unlock(&priv->lock);
 465
 466                return IRQ_HANDLED;
 467        }
 468
 469        if (ievent & ~FEC_IEVENT_TFINT)
 470                dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
 471
 472        return IRQ_HANDLED;
 473}
 474
 475/*
 476 * Get the current statistics.
 477 * This may be called with the card open or closed.
 478 */
 479static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
 480{
 481        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 482        struct net_device_stats *stats = &dev->stats;
 483        struct mpc52xx_fec __iomem *fec = priv->fec;
 484
 485        stats->rx_bytes = in_be32(&fec->rmon_r_octets);
 486        stats->rx_packets = in_be32(&fec->rmon_r_packets);
 487        stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
 488                in_be32(&fec->rmon_r_undersize) +
 489                in_be32(&fec->rmon_r_oversize) +
 490                in_be32(&fec->rmon_r_frag) +
 491                in_be32(&fec->rmon_r_jab);
 492
 493        stats->tx_bytes = in_be32(&fec->rmon_t_octets);
 494        stats->tx_packets = in_be32(&fec->rmon_t_packets);
 495        stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
 496                in_be32(&fec->rmon_t_undersize) +
 497                in_be32(&fec->rmon_t_oversize) +
 498                in_be32(&fec->rmon_t_frag) +
 499                in_be32(&fec->rmon_t_jab);
 500
 501        stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
 502        stats->collisions = in_be32(&fec->rmon_t_col);
 503
 504        /* detailed rx_errors: */
 505        stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
 506                                        + in_be32(&fec->rmon_r_oversize)
 507                                        + in_be32(&fec->rmon_r_frag)
 508                                        + in_be32(&fec->rmon_r_jab);
 509        stats->rx_over_errors = in_be32(&fec->r_macerr);
 510        stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
 511        stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
 512        stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
 513        stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
 514
 515        /* detailed tx_errors: */
 516        stats->tx_aborted_errors = 0;
 517        stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
 518        stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
 519        stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
 520        stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
 521
 522        return stats;
 523}
 524
 525/*
 526 * Read MIB counters in order to reset them,
 527 * then zero all the stats fields in memory
 528 */
 529static void mpc52xx_fec_reset_stats(struct net_device *dev)
 530{
 531        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 532        struct mpc52xx_fec __iomem *fec = priv->fec;
 533
 534        out_be32(&fec->mib_control, FEC_MIB_DISABLE);
 535        memset_io(&fec->rmon_t_drop, 0,
 536                   offsetof(struct mpc52xx_fec, reserved10) -
 537                   offsetof(struct mpc52xx_fec, rmon_t_drop));
 538        out_be32(&fec->mib_control, 0);
 539
 540        memset(&dev->stats, 0, sizeof(dev->stats));
 541}
 542
 543/*
 544 * Set or clear the multicast filter for this adaptor.
 545 */
 546static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
 547{
 548        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 549        struct mpc52xx_fec __iomem *fec = priv->fec;
 550        u32 rx_control;
 551
 552        rx_control = in_be32(&fec->r_cntrl);
 553
 554        if (dev->flags & IFF_PROMISC) {
 555                rx_control |= FEC_RCNTRL_PROM;
 556                out_be32(&fec->r_cntrl, rx_control);
 557        } else {
 558                rx_control &= ~FEC_RCNTRL_PROM;
 559                out_be32(&fec->r_cntrl, rx_control);
 560
 561                if (dev->flags & IFF_ALLMULTI) {
 562                        out_be32(&fec->gaddr1, 0xffffffff);
 563                        out_be32(&fec->gaddr2, 0xffffffff);
 564                } else {
 565                        u32 crc;
 566                        struct netdev_hw_addr *ha;
 567                        u32 gaddr1 = 0x00000000;
 568                        u32 gaddr2 = 0x00000000;
 569
 570                        netdev_for_each_mc_addr(ha, dev) {
 571                                crc = ether_crc_le(6, ha->addr) >> 26;
 572                                if (crc >= 32)
 573                                        gaddr1 |= 1 << (crc-32);
 574                                else
 575                                        gaddr2 |= 1 << crc;
 576                        }
 577                        out_be32(&fec->gaddr1, gaddr1);
 578                        out_be32(&fec->gaddr2, gaddr2);
 579                }
 580        }
 581}
 582
 583/**
 584 * mpc52xx_fec_hw_init
 585 * @dev: network device
 586 *
 587 * Setup various hardware setting, only needed once on start
 588 */
 589static void mpc52xx_fec_hw_init(struct net_device *dev)
 590{
 591        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 592        struct mpc52xx_fec __iomem *fec = priv->fec;
 593        int i;
 594
 595        /* Whack a reset.  We should wait for this. */
 596        out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
 597        for (i = 0; i < FEC_RESET_DELAY; ++i) {
 598                if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
 599                        break;
 600                udelay(1);
 601        }
 602        if (i == FEC_RESET_DELAY)
 603                dev_err(&dev->dev, "FEC Reset timeout!\n");
 604
 605        /* set pause to 0x20 frames */
 606        out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
 607
 608        /* high service request will be deasserted when there's < 7 bytes in fifo
 609         * low service request will be deasserted when there's < 4*7 bytes in fifo
 610         */
 611        out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
 612        out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
 613
 614        /* alarm when <= x bytes in FIFO */
 615        out_be32(&fec->rfifo_alarm, 0x0000030c);
 616        out_be32(&fec->tfifo_alarm, 0x00000100);
 617
 618        /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
 619        out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
 620
 621        /* enable crc generation */
 622        out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
 623        out_be32(&fec->iaddr1, 0x00000000);     /* No individual filter */
 624        out_be32(&fec->iaddr2, 0x00000000);     /* No individual filter */
 625
 626        /* set phy speed.
 627         * this can't be done in phy driver, since it needs to be called
 628         * before fec stuff (even on resume) */
 629        out_be32(&fec->mii_speed, priv->mdio_speed);
 630}
 631
 632/**
 633 * mpc52xx_fec_start
 634 * @dev: network device
 635 *
 636 * This function is called to start or restart the FEC during a link
 637 * change.  This happens on fifo errors or when switching between half
 638 * and full duplex.
 639 */
 640static void mpc52xx_fec_start(struct net_device *dev)
 641{
 642        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 643        struct mpc52xx_fec __iomem *fec = priv->fec;
 644        u32 rcntrl;
 645        u32 tcntrl;
 646        u32 tmp;
 647
 648        /* clear sticky error bits */
 649        tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
 650        out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
 651        out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
 652
 653        /* FIFOs will reset on mpc52xx_fec_enable */
 654        out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
 655
 656        /* Set station address. */
 657        mpc52xx_fec_set_paddr(dev, dev->dev_addr);
 658
 659        mpc52xx_fec_set_multicast_list(dev);
 660
 661        /* set max frame len, enable flow control, select mii mode */
 662        rcntrl = FEC_RX_BUFFER_SIZE << 16;      /* max frame length */
 663        rcntrl |= FEC_RCNTRL_FCE;
 664
 665        if (!priv->seven_wire_mode)
 666                rcntrl |= FEC_RCNTRL_MII_MODE;
 667
 668        if (priv->duplex == DUPLEX_FULL)
 669                tcntrl = FEC_TCNTRL_FDEN;       /* FD enable */
 670        else {
 671                rcntrl |= FEC_RCNTRL_DRT;       /* disable Rx on Tx (HD) */
 672                tcntrl = 0;
 673        }
 674        out_be32(&fec->r_cntrl, rcntrl);
 675        out_be32(&fec->x_cntrl, tcntrl);
 676
 677        /* Clear any outstanding interrupt. */
 678        out_be32(&fec->ievent, 0xffffffff);
 679
 680        /* Enable interrupts we wish to service. */
 681        out_be32(&fec->imask, FEC_IMASK_ENABLE);
 682
 683        /* And last, enable the transmit and receive processing. */
 684        out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
 685        out_be32(&fec->r_des_active, 0x01000000);
 686}
 687
 688/**
 689 * mpc52xx_fec_stop
 690 * @dev: network device
 691 *
 692 * stop all activity on fec and empty dma buffers
 693 */
 694static void mpc52xx_fec_stop(struct net_device *dev)
 695{
 696        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 697        struct mpc52xx_fec __iomem *fec = priv->fec;
 698        unsigned long timeout;
 699
 700        /* disable all interrupts */
 701        out_be32(&fec->imask, 0);
 702
 703        /* Disable the rx task. */
 704        bcom_disable(priv->rx_dmatsk);
 705
 706        /* Wait for tx queue to drain, but only if we're in process context */
 707        if (!in_interrupt()) {
 708                timeout = jiffies + msecs_to_jiffies(2000);
 709                while (time_before(jiffies, timeout) &&
 710                                !bcom_queue_empty(priv->tx_dmatsk))
 711                        msleep(100);
 712
 713                if (time_after_eq(jiffies, timeout))
 714                        dev_err(&dev->dev, "queues didn't drain\n");
 715#if 1
 716                if (time_after_eq(jiffies, timeout)) {
 717                        dev_err(&dev->dev, "  tx: index: %i, outdex: %i\n",
 718                                        priv->tx_dmatsk->index,
 719                                        priv->tx_dmatsk->outdex);
 720                        dev_err(&dev->dev, "  rx: index: %i, outdex: %i\n",
 721                                        priv->rx_dmatsk->index,
 722                                        priv->rx_dmatsk->outdex);
 723                }
 724#endif
 725        }
 726
 727        bcom_disable(priv->tx_dmatsk);
 728
 729        /* Stop FEC */
 730        out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
 731}
 732
 733/* reset fec and bestcomm tasks */
 734static void mpc52xx_fec_reset(struct net_device *dev)
 735{
 736        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 737        struct mpc52xx_fec __iomem *fec = priv->fec;
 738
 739        mpc52xx_fec_stop(dev);
 740
 741        out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
 742        out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
 743
 744        mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
 745
 746        mpc52xx_fec_hw_init(dev);
 747
 748        bcom_fec_rx_reset(priv->rx_dmatsk);
 749        bcom_fec_tx_reset(priv->tx_dmatsk);
 750
 751        mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
 752
 753        bcom_enable(priv->rx_dmatsk);
 754        bcom_enable(priv->tx_dmatsk);
 755
 756        mpc52xx_fec_start(dev);
 757
 758        netif_wake_queue(dev);
 759}
 760
 761
 762/* ethtool interface */
 763
 764static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 765{
 766        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 767
 768        if (!priv->phydev)
 769                return -ENODEV;
 770
 771        return phy_ethtool_gset(priv->phydev, cmd);
 772}
 773
 774static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 775{
 776        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 777
 778        if (!priv->phydev)
 779                return -ENODEV;
 780
 781        return phy_ethtool_sset(priv->phydev, cmd);
 782}
 783
 784static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
 785{
 786        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 787        return priv->msg_enable;
 788}
 789
 790static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
 791{
 792        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 793        priv->msg_enable = level;
 794}
 795
 796static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
 797        .get_settings = mpc52xx_fec_get_settings,
 798        .set_settings = mpc52xx_fec_set_settings,
 799        .get_link = ethtool_op_get_link,
 800        .get_msglevel = mpc52xx_fec_get_msglevel,
 801        .set_msglevel = mpc52xx_fec_set_msglevel,
 802        .get_ts_info = ethtool_op_get_ts_info,
 803};
 804
 805
 806static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 807{
 808        struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 809
 810        if (!priv->phydev)
 811                return -ENOTSUPP;
 812
 813        return phy_mii_ioctl(priv->phydev, rq, cmd);
 814}
 815
 816static const struct net_device_ops mpc52xx_fec_netdev_ops = {
 817        .ndo_open = mpc52xx_fec_open,
 818        .ndo_stop = mpc52xx_fec_close,
 819        .ndo_start_xmit = mpc52xx_fec_start_xmit,
 820        .ndo_set_rx_mode = mpc52xx_fec_set_multicast_list,
 821        .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
 822        .ndo_validate_addr = eth_validate_addr,
 823        .ndo_do_ioctl = mpc52xx_fec_ioctl,
 824        .ndo_change_mtu = eth_change_mtu,
 825        .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
 826        .ndo_get_stats = mpc52xx_fec_get_stats,
 827#ifdef CONFIG_NET_POLL_CONTROLLER
 828        .ndo_poll_controller = mpc52xx_fec_poll_controller,
 829#endif
 830};
 831
 832/* ======================================================================== */
 833/* OF Driver                                                                */
 834/* ======================================================================== */
 835
 836static int mpc52xx_fec_probe(struct platform_device *op)
 837{
 838        int rv;
 839        struct net_device *ndev;
 840        struct mpc52xx_fec_priv *priv = NULL;
 841        struct resource mem;
 842        const u32 *prop;
 843        int prop_size;
 844        struct device_node *np = op->dev.of_node;
 845        const char *mac_addr;
 846
 847        phys_addr_t rx_fifo;
 848        phys_addr_t tx_fifo;
 849
 850        /* Get the ether ndev & it's private zone */
 851        ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
 852        if (!ndev)
 853                return -ENOMEM;
 854
 855        priv = netdev_priv(ndev);
 856        priv->ndev = ndev;
 857
 858        /* Reserve FEC control zone */
 859        rv = of_address_to_resource(np, 0, &mem);
 860        if (rv) {
 861                printk(KERN_ERR DRIVER_NAME ": "
 862                                "Error while parsing device node resource\n" );
 863                goto err_netdev;
 864        }
 865        if (resource_size(&mem) < sizeof(struct mpc52xx_fec)) {
 866                printk(KERN_ERR DRIVER_NAME
 867                       " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
 868                       (unsigned long)resource_size(&mem),
 869                       sizeof(struct mpc52xx_fec));
 870                rv = -EINVAL;
 871                goto err_netdev;
 872        }
 873
 874        if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
 875                                DRIVER_NAME)) {
 876                rv = -EBUSY;
 877                goto err_netdev;
 878        }
 879
 880        /* Init ether ndev with what we have */
 881        ndev->netdev_ops        = &mpc52xx_fec_netdev_ops;
 882        ndev->ethtool_ops       = &mpc52xx_fec_ethtool_ops;
 883        ndev->watchdog_timeo    = FEC_WATCHDOG_TIMEOUT;
 884        ndev->base_addr         = mem.start;
 885        SET_NETDEV_DEV(ndev, &op->dev);
 886
 887        spin_lock_init(&priv->lock);
 888
 889        /* ioremap the zones */
 890        priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
 891
 892        if (!priv->fec) {
 893                rv = -ENOMEM;
 894                goto err_mem_region;
 895        }
 896
 897        /* Bestcomm init */
 898        rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
 899        tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
 900
 901        priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
 902        priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
 903
 904        if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
 905                printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
 906                rv = -ENOMEM;
 907                goto err_rx_tx_dmatsk;
 908        }
 909
 910        /* Get the IRQ we need one by one */
 911                /* Control */
 912        ndev->irq = irq_of_parse_and_map(np, 0);
 913
 914                /* RX */
 915        priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
 916
 917                /* TX */
 918        priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
 919
 920        /*
 921         * MAC address init:
 922         *
 923         * First try to read MAC address from DT
 924         */
 925        mac_addr = of_get_mac_address(np);
 926        if (mac_addr) {
 927                memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
 928        } else {
 929                struct mpc52xx_fec __iomem *fec = priv->fec;
 930
 931                /*
 932                 * If the MAC addresse is not provided via DT then read
 933                 * it back from the controller regs
 934                 */
 935                *(u32 *)(&ndev->dev_addr[0]) = in_be32(&fec->paddr1);
 936                *(u16 *)(&ndev->dev_addr[4]) = in_be32(&fec->paddr2) >> 16;
 937        }
 938
 939        /*
 940         * Check if the MAC address is valid, if not get a random one
 941         */
 942        if (!is_valid_ether_addr(ndev->dev_addr)) {
 943                eth_hw_addr_random(ndev);
 944                dev_warn(&ndev->dev, "using random MAC address %pM\n",
 945                         ndev->dev_addr);
 946        }
 947
 948        priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
 949
 950        /*
 951         * Link mode configuration
 952         */
 953
 954        /* Start with safe defaults for link connection */
 955        priv->speed = 100;
 956        priv->duplex = DUPLEX_HALF;
 957        priv->mdio_speed = ((mpc5xxx_get_bus_frequency(np) >> 20) / 5) << 1;
 958
 959        /* The current speed preconfigures the speed of the MII link */
 960        prop = of_get_property(np, "current-speed", &prop_size);
 961        if (prop && (prop_size >= sizeof(u32) * 2)) {
 962                priv->speed = prop[0];
 963                priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
 964        }
 965
 966        /* If there is a phy handle, then get the PHY node */
 967        priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
 968
 969        /* the 7-wire property means don't use MII mode */
 970        if (of_find_property(np, "fsl,7-wire-mode", NULL)) {
 971                priv->seven_wire_mode = 1;
 972                dev_info(&ndev->dev, "using 7-wire PHY mode\n");
 973        }
 974
 975        /* Hardware init */
 976        mpc52xx_fec_hw_init(ndev);
 977        mpc52xx_fec_reset_stats(ndev);
 978
 979        rv = register_netdev(ndev);
 980        if (rv < 0)
 981                goto err_node;
 982
 983        /* We're done ! */
 984        dev_set_drvdata(&op->dev, ndev);
 985        printk(KERN_INFO "%s: %s MAC %pM\n",
 986               ndev->name, op->dev.of_node->full_name, ndev->dev_addr);
 987
 988        return 0;
 989
 990err_node:
 991        of_node_put(priv->phy_node);
 992        irq_dispose_mapping(ndev->irq);
 993err_rx_tx_dmatsk:
 994        if (priv->rx_dmatsk)
 995                bcom_fec_rx_release(priv->rx_dmatsk);
 996        if (priv->tx_dmatsk)
 997                bcom_fec_tx_release(priv->tx_dmatsk);
 998        iounmap(priv->fec);
 999err_mem_region:
1000        release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
1001err_netdev:
1002        free_netdev(ndev);
1003
1004        return rv;
1005}
1006
1007static int
1008mpc52xx_fec_remove(struct platform_device *op)
1009{
1010        struct net_device *ndev;
1011        struct mpc52xx_fec_priv *priv;
1012
1013        ndev = dev_get_drvdata(&op->dev);
1014        priv = netdev_priv(ndev);
1015
1016        unregister_netdev(ndev);
1017
1018        if (priv->phy_node)
1019                of_node_put(priv->phy_node);
1020        priv->phy_node = NULL;
1021
1022        irq_dispose_mapping(ndev->irq);
1023
1024        bcom_fec_rx_release(priv->rx_dmatsk);
1025        bcom_fec_tx_release(priv->tx_dmatsk);
1026
1027        iounmap(priv->fec);
1028
1029        release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
1030
1031        free_netdev(ndev);
1032
1033        dev_set_drvdata(&op->dev, NULL);
1034        return 0;
1035}
1036
1037#ifdef CONFIG_PM
1038static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
1039{
1040        struct net_device *dev = dev_get_drvdata(&op->dev);
1041
1042        if (netif_running(dev))
1043                mpc52xx_fec_close(dev);
1044
1045        return 0;
1046}
1047
1048static int mpc52xx_fec_of_resume(struct platform_device *op)
1049{
1050        struct net_device *dev = dev_get_drvdata(&op->dev);
1051
1052        mpc52xx_fec_hw_init(dev);
1053        mpc52xx_fec_reset_stats(dev);
1054
1055        if (netif_running(dev))
1056                mpc52xx_fec_open(dev);
1057
1058        return 0;
1059}
1060#endif
1061
1062static struct of_device_id mpc52xx_fec_match[] = {
1063        { .compatible = "fsl,mpc5200b-fec", },
1064        { .compatible = "fsl,mpc5200-fec", },
1065        { .compatible = "mpc5200-fec", },
1066        { }
1067};
1068
1069MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1070
1071static struct platform_driver mpc52xx_fec_driver = {
1072        .driver = {
1073                .name = DRIVER_NAME,
1074                .owner = THIS_MODULE,
1075                .of_match_table = mpc52xx_fec_match,
1076        },
1077        .probe          = mpc52xx_fec_probe,
1078        .remove         = mpc52xx_fec_remove,
1079#ifdef CONFIG_PM
1080        .suspend        = mpc52xx_fec_of_suspend,
1081        .resume         = mpc52xx_fec_of_resume,
1082#endif
1083};
1084
1085
1086/* ======================================================================== */
1087/* Module                                                                   */
1088/* ======================================================================== */
1089
1090static int __init
1091mpc52xx_fec_init(void)
1092{
1093#ifdef CONFIG_FEC_MPC52xx_MDIO
1094        int ret;
1095        ret = platform_driver_register(&mpc52xx_fec_mdio_driver);
1096        if (ret) {
1097                printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
1098                return ret;
1099        }
1100#endif
1101        return platform_driver_register(&mpc52xx_fec_driver);
1102}
1103
1104static void __exit
1105mpc52xx_fec_exit(void)
1106{
1107        platform_driver_unregister(&mpc52xx_fec_driver);
1108#ifdef CONFIG_FEC_MPC52xx_MDIO
1109        platform_driver_unregister(&mpc52xx_fec_mdio_driver);
1110#endif
1111}
1112
1113
1114module_init(mpc52xx_fec_init);
1115module_exit(mpc52xx_fec_exit);
1116
1117MODULE_LICENSE("GPL");
1118MODULE_AUTHOR("Dale Farnsworth");
1119MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");
1120