2  * Driver for the MPC5200 Fast Ethernet Controller
 
   4  * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
 
   5  * now maintained by Sylvain Munaut <tnt@246tNt.com>
 
   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.
 
  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.
 
  17 #include <linux/module.h>
 
  19 #include <linux/kernel.h>
 
  20 #include <linux/types.h>
 
  21 #include <linux/spinlock.h>
 
  22 #include <linux/errno.h>
 
  23 #include <linux/init.h>
 
  24 #include <linux/crc32.h>
 
  25 #include <linux/hardirq.h>
 
  26 #include <linux/delay.h>
 
  27 #include <linux/of_device.h>
 
  28 #include <linux/of_platform.h>
 
  30 #include <linux/netdevice.h>
 
  31 #include <linux/etherdevice.h>
 
  32 #include <linux/ethtool.h>
 
  33 #include <linux/skbuff.h>
 
  36 #include <asm/delay.h>
 
  37 #include <asm/mpc52xx.h>
 
  39 #include <sysdev/bestcomm/bestcomm.h>
 
  40 #include <sysdev/bestcomm/fec.h>
 
  42 #include "fec_mpc52xx.h"
 
  44 #define DRIVER_NAME "mpc52xx-fec"
 
  46 static irqreturn_t mpc52xx_fec_interrupt(int, void *);
 
  47 static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
 
  48 static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
 
  49 static void mpc52xx_fec_stop(struct net_device *dev);
 
  50 static void mpc52xx_fec_start(struct net_device *dev);
 
  51 static void mpc52xx_fec_reset(struct net_device *dev);
 
  53 static u8 mpc52xx_fec_mac_addr[6];
 
  54 module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);
 
  55 MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");
 
  57 #define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
 
  58                 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFDOWN )
 
  59 static int debug = -1;  /* the above default */
 
  60 module_param(debug, int, 0);
 
  61 MODULE_PARM_DESC(debug, "debugging messages level");
 
  63 static void mpc52xx_fec_tx_timeout(struct net_device *dev)
 
  65         dev_warn(&dev->dev, "transmit timed out\n");
 
  67         mpc52xx_fec_reset(dev);
 
  69         dev->stats.tx_errors++;
 
  71         netif_wake_queue(dev);
 
  74 static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
 
  76         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
  77         struct mpc52xx_fec __iomem *fec = priv->fec;
 
  79         out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
 
  80         out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
 
  83 static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac)
 
  85         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
  86         struct mpc52xx_fec __iomem *fec = priv->fec;
 
  88         *(u32 *)(&mac[0]) = in_be32(&fec->paddr1);
 
  89         *(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;
 
  92 static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
 
  94         struct sockaddr *sock = addr;
 
  96         memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
 
  98         mpc52xx_fec_set_paddr(dev, sock->sa_data);
 
 102 static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
 
 104         while (!bcom_queue_empty(s)) {
 
 105                 struct bcom_fec_bd *bd;
 
 108                 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
 
 109                 dma_unmap_single(&dev->dev, bd->skb_pa, skb->len, DMA_FROM_DEVICE);
 
 114 static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
 
 116         while (!bcom_queue_full(rxtsk)) {
 
 118                 struct bcom_fec_bd *bd;
 
 120                 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
 
 124                 /* zero out the initial receive buffers to aid debugging */
 
 125                 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
 
 127                 bd = (struct bcom_fec_bd *)bcom_prepare_next_buffer(rxtsk);
 
 129                 bd->status = FEC_RX_BUFFER_SIZE;
 
 130                 bd->skb_pa = dma_map_single(&dev->dev, skb->data,
 
 131                                 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
 
 133                 bcom_submit_next_buffer(rxtsk, skb);
 
 139 /* based on generic_adjust_link from fs_enet-main.c */
 
 140 static void mpc52xx_fec_adjust_link(struct net_device *dev)
 
 142         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 143         struct phy_device *phydev = priv->phydev;
 
 146         if (phydev->link != PHY_DOWN) {
 
 147                 if (phydev->duplex != priv->duplex) {
 
 148                         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 153                         priv->duplex = phydev->duplex;
 
 155                         rcntrl = in_be32(&fec->r_cntrl);
 
 156                         tcntrl = in_be32(&fec->x_cntrl);
 
 158                         rcntrl &= ~FEC_RCNTRL_DRT;
 
 159                         tcntrl &= ~FEC_TCNTRL_FDEN;
 
 160                         if (phydev->duplex == DUPLEX_FULL)
 
 161                                 tcntrl |= FEC_TCNTRL_FDEN;      /* FD enable */
 
 163                                 rcntrl |= FEC_RCNTRL_DRT;       /* disable Rx on Tx (HD) */
 
 165                         out_be32(&fec->r_cntrl, rcntrl);
 
 166                         out_be32(&fec->x_cntrl, tcntrl);
 
 169                 if (phydev->speed != priv->speed) {
 
 171                         priv->speed = phydev->speed;
 
 174                 if (priv->link == PHY_DOWN) {
 
 176                         priv->link = phydev->link;
 
 178                         netif_carrier_on(dev);
 
 179                         netif_start_queue(dev);
 
 182         } else if (priv->link) {
 
 184                 priv->link = PHY_DOWN;
 
 187                 netif_stop_queue(dev);
 
 188                 netif_carrier_off(dev);
 
 191         if (new_state && netif_msg_link(priv))
 
 192                 phy_print_status(phydev);
 
 195 static int mpc52xx_fec_init_phy(struct net_device *dev)
 
 197         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 198         struct phy_device *phydev;
 
 199         char phy_id[BUS_ID_SIZE];
 
 201         snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT,
 
 202                         (unsigned int)dev->base_addr, priv->phy_addr);
 
 204         priv->link = PHY_DOWN;
 
 208         phydev = phy_connect(dev, phy_id, &mpc52xx_fec_adjust_link, 0, PHY_INTERFACE_MODE_MII);
 
 209         if (IS_ERR(phydev)) {
 
 210                 dev_err(&dev->dev, "phy_connect failed\n");
 
 211                 return PTR_ERR(phydev);
 
 213         dev_info(&dev->dev, "attached phy %i to driver %s\n",
 
 214                         phydev->addr, phydev->drv->name);
 
 216         priv->phydev = phydev;
 
 221 static int mpc52xx_fec_phy_start(struct net_device *dev)
 
 223         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 229         err = mpc52xx_fec_init_phy(dev);
 
 231                 dev_err(&dev->dev, "mpc52xx_fec_init_phy failed\n");
 
 235         /* reset phy - this also wakes it from PDOWN */
 
 236         phy_write(priv->phydev, MII_BMCR, BMCR_RESET);
 
 237         phy_start(priv->phydev);
 
 242 static void mpc52xx_fec_phy_stop(struct net_device *dev)
 
 244         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 249         phy_disconnect(priv->phydev);
 
 251         phy_stop(priv->phydev);
 
 252         phy_write(priv->phydev, MII_BMCR, BMCR_PDOWN);
 
 255 static int mpc52xx_fec_phy_mii_ioctl(struct mpc52xx_fec_priv *priv,
 
 256                 struct mii_ioctl_data *mii_data, int cmd)
 
 261         return phy_mii_ioctl(priv->phydev, mii_data, cmd);
 
 264 static void mpc52xx_fec_phy_hw_init(struct mpc52xx_fec_priv *priv)
 
 266         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 271         out_be32(&fec->mii_speed, priv->phy_speed);
 
 274 static int mpc52xx_fec_open(struct net_device *dev)
 
 276         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 279         if (request_irq(dev->irq, &mpc52xx_fec_interrupt, IRQF_SHARED,
 
 280                         DRIVER_NAME "_ctrl", dev)) {
 
 281                 dev_err(&dev->dev, "ctrl interrupt request failed\n");
 
 284         if (request_irq(priv->r_irq, &mpc52xx_fec_rx_interrupt, 0,
 
 285                         DRIVER_NAME "_rx", dev)) {
 
 286                 dev_err(&dev->dev, "rx interrupt request failed\n");
 
 289         if (request_irq(priv->t_irq, &mpc52xx_fec_tx_interrupt, 0,
 
 290                         DRIVER_NAME "_tx", dev)) {
 
 291                 dev_err(&dev->dev, "tx interrupt request failed\n");
 
 295         bcom_fec_rx_reset(priv->rx_dmatsk);
 
 296         bcom_fec_tx_reset(priv->tx_dmatsk);
 
 298         err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
 
 300                 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
 
 304         err = mpc52xx_fec_phy_start(dev);
 
 308         bcom_enable(priv->rx_dmatsk);
 
 309         bcom_enable(priv->tx_dmatsk);
 
 311         mpc52xx_fec_start(dev);
 
 313         netif_start_queue(dev);
 
 318         mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
 
 321         free_irq(priv->t_irq, dev);
 
 323         free_irq(priv->r_irq, dev);
 
 325         free_irq(dev->irq, dev);
 
 331 static int mpc52xx_fec_close(struct net_device *dev)
 
 333         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 335         netif_stop_queue(dev);
 
 337         mpc52xx_fec_stop(dev);
 
 339         mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
 
 341         free_irq(dev->irq, dev);
 
 342         free_irq(priv->r_irq, dev);
 
 343         free_irq(priv->t_irq, dev);
 
 345         mpc52xx_fec_phy_stop(dev);
 
 350 /* This will only be invoked if your driver is _not_ in XOFF state.
 
 351  * What this means is that you need not check it, and that this
 
 352  * invariant will hold if you make sure that the netif_*_queue()
 
 353  * calls are done at the proper times.
 
 355 static int mpc52xx_fec_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
 357         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 358         struct bcom_fec_bd *bd;
 
 360         if (bcom_queue_full(priv->tx_dmatsk)) {
 
 362                         dev_err(&dev->dev, "transmit queue overrun\n");
 
 366         spin_lock_irq(&priv->lock);
 
 367         dev->trans_start = jiffies;
 
 369         bd = (struct bcom_fec_bd *)
 
 370                 bcom_prepare_next_buffer(priv->tx_dmatsk);
 
 372         bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
 
 373         bd->skb_pa = dma_map_single(&dev->dev, skb->data, skb->len, DMA_TO_DEVICE);
 
 375         bcom_submit_next_buffer(priv->tx_dmatsk, skb);
 
 377         if (bcom_queue_full(priv->tx_dmatsk)) {
 
 378                 netif_stop_queue(dev);
 
 381         spin_unlock_irq(&priv->lock);
 
 386 /* This handles BestComm transmit task interrupts
 
 388 static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
 
 390         struct net_device *dev = dev_id;
 
 391         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 393         spin_lock(&priv->lock);
 
 395         while (bcom_buffer_done(priv->tx_dmatsk)) {
 
 397                 struct bcom_fec_bd *bd;
 
 398                 skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
 
 399                                 (struct bcom_bd **)&bd);
 
 400                 dma_unmap_single(&dev->dev, bd->skb_pa, skb->len, DMA_TO_DEVICE);
 
 402                 dev_kfree_skb_irq(skb);
 
 405         netif_wake_queue(dev);
 
 407         spin_unlock(&priv->lock);
 
 412 static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
 
 414         struct net_device *dev = dev_id;
 
 415         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 417         while (bcom_buffer_done(priv->rx_dmatsk)) {
 
 419                 struct sk_buff *rskb;
 
 420                 struct bcom_fec_bd *bd;
 
 423                 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
 
 424                                 (struct bcom_bd **)&bd);
 
 425                 dma_unmap_single(&dev->dev, bd->skb_pa, rskb->len, DMA_FROM_DEVICE);
 
 427                 /* Test for errors in received frame */
 
 428                 if (status & BCOM_FEC_RX_BD_ERRORS) {
 
 429                         /* Drop packet and reuse the buffer */
 
 430                         bd = (struct bcom_fec_bd *)
 
 431                                 bcom_prepare_next_buffer(priv->rx_dmatsk);
 
 433                         bd->status = FEC_RX_BUFFER_SIZE;
 
 434                         bd->skb_pa = dma_map_single(&dev->dev, rskb->data,
 
 435                                         FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
 
 437                         bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
 
 439                         dev->stats.rx_dropped++;
 
 444                 /* skbs are allocated on open, so now we allocate a new one,
 
 445                  * and remove the old (with the packet) */
 
 446                 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
 
 448                         /* Process the received skb */
 
 449                         int length = status & BCOM_FEC_RX_BD_LEN_MASK;
 
 451                         skb_put(rskb, length - 4);      /* length without CRC32 */
 
 454                         rskb->protocol = eth_type_trans(rskb, dev);
 
 457                         dev->last_rx = jiffies;
 
 459                         /* Can't get a new one : reuse the same & drop pkt */
 
 460                         dev_notice(&dev->dev, "Memory squeeze, dropping packet.\n");
 
 461                         dev->stats.rx_dropped++;
 
 466                 bd = (struct bcom_fec_bd *)
 
 467                         bcom_prepare_next_buffer(priv->rx_dmatsk);
 
 469                 bd->status = FEC_RX_BUFFER_SIZE;
 
 470                 bd->skb_pa = dma_map_single(&dev->dev, skb->data,
 
 471                                 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
 
 473                 bcom_submit_next_buffer(priv->rx_dmatsk, skb);
 
 479 static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
 
 481         struct net_device *dev = dev_id;
 
 482         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 483         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 486         ievent = in_be32(&fec->ievent);
 
 488         ievent &= ~FEC_IEVENT_MII;      /* mii is handled separately */
 
 492         out_be32(&fec->ievent, ievent);         /* clear pending events */
 
 494         if (ievent & ~(FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
 
 495                 if (ievent & ~FEC_IEVENT_TFINT)
 
 496                         dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
 
 500         if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
 
 501                 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
 
 502         if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
 
 503                 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
 
 505         mpc52xx_fec_reset(dev);
 
 507         netif_wake_queue(dev);
 
 512  * Get the current statistics.
 
 513  * This may be called with the card open or closed.
 
 515 static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
 
 517         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 518         struct net_device_stats *stats = &dev->stats;
 
 519         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 521         stats->rx_bytes = in_be32(&fec->rmon_r_octets);
 
 522         stats->rx_packets = in_be32(&fec->rmon_r_packets);
 
 523         stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
 
 524                 in_be32(&fec->rmon_r_undersize) +
 
 525                 in_be32(&fec->rmon_r_oversize) +
 
 526                 in_be32(&fec->rmon_r_frag) +
 
 527                 in_be32(&fec->rmon_r_jab);
 
 529         stats->tx_bytes = in_be32(&fec->rmon_t_octets);
 
 530         stats->tx_packets = in_be32(&fec->rmon_t_packets);
 
 531         stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
 
 532                 in_be32(&fec->rmon_t_undersize) +
 
 533                 in_be32(&fec->rmon_t_oversize) +
 
 534                 in_be32(&fec->rmon_t_frag) +
 
 535                 in_be32(&fec->rmon_t_jab);
 
 537         stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
 
 538         stats->collisions = in_be32(&fec->rmon_t_col);
 
 540         /* detailed rx_errors: */
 
 541         stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
 
 542                                         + in_be32(&fec->rmon_r_oversize)
 
 543                                         + in_be32(&fec->rmon_r_frag)
 
 544                                         + in_be32(&fec->rmon_r_jab);
 
 545         stats->rx_over_errors = in_be32(&fec->r_macerr);
 
 546         stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
 
 547         stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
 
 548         stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
 
 549         stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
 
 551         /* detailed tx_errors: */
 
 552         stats->tx_aborted_errors = 0;
 
 553         stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
 
 554         stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
 
 555         stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
 
 556         stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
 
 562  * Read MIB counters in order to reset them,
 
 563  * then zero all the stats fields in memory
 
 565 static void mpc52xx_fec_reset_stats(struct net_device *dev)
 
 567         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 568         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 570         out_be32(&fec->mib_control, FEC_MIB_DISABLE);
 
 571         memset_io(&fec->rmon_t_drop, 0,
 
 572                    offsetof(struct mpc52xx_fec, reserved10) -
 
 573                    offsetof(struct mpc52xx_fec, rmon_t_drop));
 
 574         out_be32(&fec->mib_control, 0);
 
 576         memset(&dev->stats, 0, sizeof(dev->stats));
 
 580  * Set or clear the multicast filter for this adaptor.
 
 582 static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
 
 584         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 585         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 588         rx_control = in_be32(&fec->r_cntrl);
 
 590         if (dev->flags & IFF_PROMISC) {
 
 591                 rx_control |= FEC_RCNTRL_PROM;
 
 592                 out_be32(&fec->r_cntrl, rx_control);
 
 594                 rx_control &= ~FEC_RCNTRL_PROM;
 
 595                 out_be32(&fec->r_cntrl, rx_control);
 
 597                 if (dev->flags & IFF_ALLMULTI) {
 
 598                         out_be32(&fec->gaddr1, 0xffffffff);
 
 599                         out_be32(&fec->gaddr2, 0xffffffff);
 
 603                         struct dev_mc_list *dmi;
 
 604                         u32 gaddr1 = 0x00000000;
 
 605                         u32 gaddr2 = 0x00000000;
 
 608                         for (i=0; i<dev->mc_count; i++) {
 
 609                                 crc = ether_crc_le(6, dmi->dmi_addr) >> 26;
 
 611                                         gaddr1 |= 1 << (crc-32);
 
 616                         out_be32(&fec->gaddr1, gaddr1);
 
 617                         out_be32(&fec->gaddr2, gaddr2);
 
 623  * mpc52xx_fec_hw_init
 
 624  * @dev: network device
 
 626  * Setup various hardware setting, only needed once on start
 
 628 static void mpc52xx_fec_hw_init(struct net_device *dev)
 
 630         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 631         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 634         /* Whack a reset.  We should wait for this. */
 
 635         out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
 
 636         for (i = 0; i < FEC_RESET_DELAY; ++i) {
 
 637                 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
 
 641         if (i == FEC_RESET_DELAY)
 
 642                 dev_err(&dev->dev, "FEC Reset timeout!\n");
 
 644         /* set pause to 0x20 frames */
 
 645         out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
 
 647         /* high service request will be deasserted when there's < 7 bytes in fifo
 
 648          * low service request will be deasserted when there's < 4*7 bytes in fifo
 
 650         out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
 
 651         out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
 
 653         /* alarm when <= x bytes in FIFO */
 
 654         out_be32(&fec->rfifo_alarm, 0x0000030c);
 
 655         out_be32(&fec->tfifo_alarm, 0x00000100);
 
 657         /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
 
 658         out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
 
 660         /* enable crc generation */
 
 661         out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
 
 662         out_be32(&fec->iaddr1, 0x00000000);     /* No individual filter */
 
 663         out_be32(&fec->iaddr2, 0x00000000);     /* No individual filter */
 
 666          * this can't be done in phy driver, since it needs to be called
 
 667          * before fec stuff (even on resume) */
 
 668         mpc52xx_fec_phy_hw_init(priv);
 
 673  * @dev: network device
 
 675  * This function is called to start or restart the FEC during a link
 
 676  * change.  This happens on fifo errors or when switching between half
 
 679 static void mpc52xx_fec_start(struct net_device *dev)
 
 681         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 682         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 687         /* clear sticky error bits */
 
 688         tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
 
 689         out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
 
 690         out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
 
 692         /* FIFOs will reset on mpc52xx_fec_enable */
 
 693         out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
 
 695         /* Set station address. */
 
 696         mpc52xx_fec_set_paddr(dev, dev->dev_addr);
 
 698         mpc52xx_fec_set_multicast_list(dev);
 
 700         /* set max frame len, enable flow control, select mii mode */
 
 701         rcntrl = FEC_RX_BUFFER_SIZE << 16;      /* max frame length */
 
 702         rcntrl |= FEC_RCNTRL_FCE;
 
 705                 rcntrl |= FEC_RCNTRL_MII_MODE;
 
 707         if (priv->duplex == DUPLEX_FULL)
 
 708                 tcntrl = FEC_TCNTRL_FDEN;       /* FD enable */
 
 710                 rcntrl |= FEC_RCNTRL_DRT;       /* disable Rx on Tx (HD) */
 
 713         out_be32(&fec->r_cntrl, rcntrl);
 
 714         out_be32(&fec->x_cntrl, tcntrl);
 
 716         /* Clear any outstanding interrupt. */
 
 717         out_be32(&fec->ievent, 0xffffffff);
 
 719         /* Enable interrupts we wish to service. */
 
 720         out_be32(&fec->imask, FEC_IMASK_ENABLE);
 
 722         /* And last, enable the transmit and receive processing. */
 
 723         out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
 
 724         out_be32(&fec->r_des_active, 0x01000000);
 
 729  * @dev: network device
 
 731  * stop all activity on fec and empty dma buffers
 
 733 static void mpc52xx_fec_stop(struct net_device *dev)
 
 735         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 736         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 737         unsigned long timeout;
 
 739         /* disable all interrupts */
 
 740         out_be32(&fec->imask, 0);
 
 742         /* Disable the rx task. */
 
 743         bcom_disable(priv->rx_dmatsk);
 
 745         /* Wait for tx queue to drain, but only if we're in process context */
 
 746         if (!in_interrupt()) {
 
 747                 timeout = jiffies + msecs_to_jiffies(2000);
 
 748                 while (time_before(jiffies, timeout) &&
 
 749                                 !bcom_queue_empty(priv->tx_dmatsk))
 
 752                 if (time_after_eq(jiffies, timeout))
 
 753                         dev_err(&dev->dev, "queues didn't drain\n");
 
 755                 if (time_after_eq(jiffies, timeout)) {
 
 756                         dev_err(&dev->dev, "  tx: index: %i, outdex: %i\n",
 
 757                                         priv->tx_dmatsk->index,
 
 758                                         priv->tx_dmatsk->outdex);
 
 759                         dev_err(&dev->dev, "  rx: index: %i, outdex: %i\n",
 
 760                                         priv->rx_dmatsk->index,
 
 761                                         priv->rx_dmatsk->outdex);
 
 766         bcom_disable(priv->tx_dmatsk);
 
 769         out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
 
 774 /* reset fec and bestcomm tasks */
 
 775 static void mpc52xx_fec_reset(struct net_device *dev)
 
 777         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 778         struct mpc52xx_fec __iomem *fec = priv->fec;
 
 780         mpc52xx_fec_stop(dev);
 
 782         out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
 
 783         out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
 
 785         mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
 
 787         mpc52xx_fec_hw_init(dev);
 
 789         phy_stop(priv->phydev);
 
 790         phy_write(priv->phydev, MII_BMCR, BMCR_RESET);
 
 791         phy_start(priv->phydev);
 
 793         bcom_fec_rx_reset(priv->rx_dmatsk);
 
 794         bcom_fec_tx_reset(priv->tx_dmatsk);
 
 796         mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
 
 798         bcom_enable(priv->rx_dmatsk);
 
 799         bcom_enable(priv->tx_dmatsk);
 
 801         mpc52xx_fec_start(dev);
 
 805 /* ethtool interface */
 
 806 static void mpc52xx_fec_get_drvinfo(struct net_device *dev,
 
 807                 struct ethtool_drvinfo *info)
 
 809         strcpy(info->driver, DRIVER_NAME);
 
 812 static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 
 814         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 815         return phy_ethtool_gset(priv->phydev, cmd);
 
 818 static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 
 820         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 821         return phy_ethtool_sset(priv->phydev, cmd);
 
 824 static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
 
 826         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 827         return priv->msg_enable;
 
 830 static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
 
 832         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 833         priv->msg_enable = level;
 
 836 static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
 
 837         .get_drvinfo = mpc52xx_fec_get_drvinfo,
 
 838         .get_settings = mpc52xx_fec_get_settings,
 
 839         .set_settings = mpc52xx_fec_set_settings,
 
 840         .get_link = ethtool_op_get_link,
 
 841         .get_msglevel = mpc52xx_fec_get_msglevel,
 
 842         .set_msglevel = mpc52xx_fec_set_msglevel,
 
 846 static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 
 848         struct mpc52xx_fec_priv *priv = netdev_priv(dev);
 
 850         return mpc52xx_fec_phy_mii_ioctl(priv, if_mii(rq), cmd);
 
 853 /* ======================================================================== */
 
 855 /* ======================================================================== */
 
 858 mpc52xx_fec_probe(struct of_device *op, const struct of_device_id *match)
 
 861         struct net_device *ndev;
 
 862         struct mpc52xx_fec_priv *priv = NULL;
 
 869         /* Get the ether ndev & it's private zone */
 
 870         ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
 
 874         priv = netdev_priv(ndev);
 
 876         /* Reserve FEC control zone */
 
 877         rv = of_address_to_resource(op->node, 0, &mem);
 
 879                 printk(KERN_ERR DRIVER_NAME ": "
 
 880                                 "Error while parsing device node resource\n" );
 
 883         if ((mem.end - mem.start + 1) < sizeof(struct mpc52xx_fec)) {
 
 884                 printk(KERN_ERR DRIVER_NAME
 
 885                         " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
 
 886                         (unsigned long)(mem.end - mem.start + 1), sizeof(struct mpc52xx_fec));
 
 890         if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec), DRIVER_NAME))
 
 893         /* Init ether ndev with what we have */
 
 894         ndev->open              = mpc52xx_fec_open;
 
 895         ndev->stop              = mpc52xx_fec_close;
 
 896         ndev->hard_start_xmit   = mpc52xx_fec_hard_start_xmit;
 
 897         ndev->do_ioctl          = mpc52xx_fec_ioctl;
 
 898         ndev->ethtool_ops       = &mpc52xx_fec_ethtool_ops;
 
 899         ndev->get_stats         = mpc52xx_fec_get_stats;
 
 900         ndev->set_mac_address   = mpc52xx_fec_set_mac_address;
 
 901         ndev->set_multicast_list = mpc52xx_fec_set_multicast_list;
 
 902         ndev->tx_timeout        = mpc52xx_fec_tx_timeout;
 
 903         ndev->watchdog_timeo    = FEC_WATCHDOG_TIMEOUT;
 
 904         ndev->base_addr         = mem.start;
 
 906         priv->t_irq = priv->r_irq = ndev->irq = NO_IRQ; /* IRQ are free for now */
 
 908         spin_lock_init(&priv->lock);
 
 910         /* ioremap the zones */
 
 911         priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
 
 919         rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
 
 920         tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
 
 922         priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
 
 923         priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
 
 925         if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
 
 926                 printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
 
 931         /* Get the IRQ we need one by one */
 
 933         ndev->irq = irq_of_parse_and_map(op->node, 0);
 
 936         priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
 
 939         priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
 
 941         /* MAC address init */
 
 942         if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
 
 943                 memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
 
 945                 mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
 
 947         priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
 
 948         priv->duplex = DUPLEX_FULL;
 
 950         /* is the phy present in device tree? */
 
 951         ph = of_get_property(op->node, "phy-handle", NULL);
 
 953                 const unsigned int *prop;
 
 954                 struct device_node *phy_dn;
 
 957                 phy_dn = of_find_node_by_phandle(*ph);
 
 958                 prop = of_get_property(phy_dn, "reg", NULL);
 
 959                 priv->phy_addr = *prop;
 
 964                 priv->phy_speed = ((mpc52xx_find_ipb_freq(op->node) >> 20) / 5) << 1;
 
 966                 dev_info(&ndev->dev, "can't find \"phy-handle\" in device"
 
 967                                 " tree, using 7-wire mode\n");
 
 971         mpc52xx_fec_hw_init(ndev);
 
 973         mpc52xx_fec_reset_stats(ndev);
 
 975         SET_NETDEV_DEV(ndev, &op->dev);
 
 977         /* Register the new network device */
 
 978         rv = register_netdev(ndev);
 
 983         dev_set_drvdata(&op->dev, ndev);
 
 988         /* Error handling - free everything that might be allocated */
 
 991         irq_dispose_mapping(ndev->irq);
 
 994                 bcom_fec_rx_release(priv->rx_dmatsk);
 
 996                 bcom_fec_tx_release(priv->tx_dmatsk);
 
1001         release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
 
1009 mpc52xx_fec_remove(struct of_device *op)
 
1011         struct net_device *ndev;
 
1012         struct mpc52xx_fec_priv *priv;
 
1014         ndev = dev_get_drvdata(&op->dev);
 
1015         priv = netdev_priv(ndev);
 
1017         unregister_netdev(ndev);
 
1019         irq_dispose_mapping(ndev->irq);
 
1021         bcom_fec_rx_release(priv->rx_dmatsk);
 
1022         bcom_fec_tx_release(priv->tx_dmatsk);
 
1026         release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
 
1030         dev_set_drvdata(&op->dev, NULL);
 
1035 static int mpc52xx_fec_of_suspend(struct of_device *op, pm_message_t state)
 
1037         struct net_device *dev = dev_get_drvdata(&op->dev);
 
1039         if (netif_running(dev))
 
1040                 mpc52xx_fec_close(dev);
 
1045 static int mpc52xx_fec_of_resume(struct of_device *op)
 
1047         struct net_device *dev = dev_get_drvdata(&op->dev);
 
1049         mpc52xx_fec_hw_init(dev);
 
1050         mpc52xx_fec_reset_stats(dev);
 
1052         if (netif_running(dev))
 
1053                 mpc52xx_fec_open(dev);
 
1059 static struct of_device_id mpc52xx_fec_match[] = {
 
1060         { .type = "network", .compatible = "fsl,mpc5200-fec", },
 
1061         { .type = "network", .compatible = "mpc5200-fec", },
 
1065 MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
 
1067 static struct of_platform_driver mpc52xx_fec_driver = {
 
1068         .owner          = THIS_MODULE,
 
1069         .name           = DRIVER_NAME,
 
1070         .match_table    = mpc52xx_fec_match,
 
1071         .probe          = mpc52xx_fec_probe,
 
1072         .remove         = mpc52xx_fec_remove,
 
1074         .suspend        = mpc52xx_fec_of_suspend,
 
1075         .resume         = mpc52xx_fec_of_resume,
 
1080 /* ======================================================================== */
 
1082 /* ======================================================================== */
 
1085 mpc52xx_fec_init(void)
 
1087 #ifdef CONFIG_FEC_MPC52xx_MDIO
 
1089         ret = of_register_platform_driver(&mpc52xx_fec_mdio_driver);
 
1091                 printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
 
1095         return of_register_platform_driver(&mpc52xx_fec_driver);
 
1099 mpc52xx_fec_exit(void)
 
1101         of_unregister_platform_driver(&mpc52xx_fec_driver);
 
1102 #ifdef CONFIG_FEC_MPC52xx_MDIO
 
1103         of_unregister_platform_driver(&mpc52xx_fec_mdio_driver);
 
1108 module_init(mpc52xx_fec_init);
 
1109 module_exit(mpc52xx_fec_exit);
 
1111 MODULE_LICENSE("GPL");
 
1112 MODULE_AUTHOR("Dale Farnsworth");
 
1113 MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");