linux/drivers/net/ethernet/cadence/at91_ether.c
<<
>>
Prefs
   1/*
   2 * Ethernet driver for the Atmel AT91RM9200 (Thunder)
   3 *
   4 *  Copyright (C) 2003 SAN People (Pty) Ltd
   5 *
   6 * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc.
   7 * Initial version by Rick Bronson 01/11/2003
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version
  12 * 2 of the License, or (at your option) any later version.
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/interrupt.h>
  18#include <linux/netdevice.h>
  19#include <linux/etherdevice.h>
  20#include <linux/skbuff.h>
  21#include <linux/dma-mapping.h>
  22#include <linux/ethtool.h>
  23#include <linux/platform_data/macb.h>
  24#include <linux/platform_device.h>
  25#include <linux/clk.h>
  26#include <linux/gfp.h>
  27#include <linux/phy.h>
  28#include <linux/io.h>
  29#include <linux/of.h>
  30#include <linux/of_device.h>
  31#include <linux/of_net.h>
  32#include <linux/pinctrl/consumer.h>
  33
  34#include "macb.h"
  35
  36/* 1518 rounded up */
  37#define MAX_RBUFF_SZ    0x600
  38/* max number of receive buffers */
  39#define MAX_RX_DESCR    9
  40
  41/* Initialize and start the Receiver and Transmit subsystems */
  42static int at91ether_start(struct net_device *dev)
  43{
  44        struct macb *lp = netdev_priv(dev);
  45        dma_addr_t addr;
  46        u32 ctl;
  47        int i;
  48
  49        lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  50                                        MAX_RX_DESCR * sizeof(struct macb_dma_desc),
  51                                        &lp->rx_ring_dma, GFP_KERNEL);
  52        if (!lp->rx_ring) {
  53                netdev_err(dev, "unable to alloc rx ring DMA buffer\n");
  54                return -ENOMEM;
  55        }
  56
  57        lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  58                                        MAX_RX_DESCR * MAX_RBUFF_SZ,
  59                                        &lp->rx_buffers_dma, GFP_KERNEL);
  60        if (!lp->rx_buffers) {
  61                netdev_err(dev, "unable to alloc rx data DMA buffer\n");
  62
  63                dma_free_coherent(&lp->pdev->dev,
  64                                        MAX_RX_DESCR * sizeof(struct macb_dma_desc),
  65                                        lp->rx_ring, lp->rx_ring_dma);
  66                lp->rx_ring = NULL;
  67                return -ENOMEM;
  68        }
  69
  70        addr = lp->rx_buffers_dma;
  71        for (i = 0; i < MAX_RX_DESCR; i++) {
  72                lp->rx_ring[i].addr = addr;
  73                lp->rx_ring[i].ctrl = 0;
  74                addr += MAX_RBUFF_SZ;
  75        }
  76
  77        /* Set the Wrap bit on the last descriptor */
  78        lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
  79
  80        /* Reset buffer index */
  81        lp->rx_tail = 0;
  82
  83        /* Program address of descriptor list in Rx Buffer Queue register */
  84        macb_writel(lp, RBQP, lp->rx_ring_dma);
  85
  86        /* Enable Receive and Transmit */
  87        ctl = macb_readl(lp, NCR);
  88        macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  89
  90        return 0;
  91}
  92
  93/* Open the ethernet interface */
  94static int at91ether_open(struct net_device *dev)
  95{
  96        struct macb *lp = netdev_priv(dev);
  97        u32 ctl;
  98        int ret;
  99
 100        /* Clear internal statistics */
 101        ctl = macb_readl(lp, NCR);
 102        macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
 103
 104        macb_set_hwaddr(lp);
 105
 106        ret = at91ether_start(dev);
 107        if (ret)
 108                return ret;
 109
 110        /* Enable MAC interrupts */
 111        macb_writel(lp, IER, MACB_BIT(RCOMP)    |
 112                             MACB_BIT(RXUBR)    |
 113                             MACB_BIT(ISR_TUND) |
 114                             MACB_BIT(ISR_RLE)  |
 115                             MACB_BIT(TCOMP)    |
 116                             MACB_BIT(ISR_ROVR) |
 117                             MACB_BIT(HRESP));
 118
 119        /* schedule a link state check */
 120        phy_start(lp->phy_dev);
 121
 122        netif_start_queue(dev);
 123
 124        return 0;
 125}
 126
 127/* Close the interface */
 128static int at91ether_close(struct net_device *dev)
 129{
 130        struct macb *lp = netdev_priv(dev);
 131        u32 ctl;
 132
 133        /* Disable Receiver and Transmitter */
 134        ctl = macb_readl(lp, NCR);
 135        macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
 136
 137        /* Disable MAC interrupts */
 138        macb_writel(lp, IDR, MACB_BIT(RCOMP)    |
 139                             MACB_BIT(RXUBR)    |
 140                             MACB_BIT(ISR_TUND) |
 141                             MACB_BIT(ISR_RLE)  |
 142                             MACB_BIT(TCOMP)    |
 143                             MACB_BIT(ISR_ROVR) |
 144                             MACB_BIT(HRESP));
 145
 146        netif_stop_queue(dev);
 147
 148        dma_free_coherent(&lp->pdev->dev,
 149                                MAX_RX_DESCR * sizeof(struct macb_dma_desc),
 150                                lp->rx_ring, lp->rx_ring_dma);
 151        lp->rx_ring = NULL;
 152
 153        dma_free_coherent(&lp->pdev->dev,
 154                                MAX_RX_DESCR * MAX_RBUFF_SZ,
 155                                lp->rx_buffers, lp->rx_buffers_dma);
 156        lp->rx_buffers = NULL;
 157
 158        return 0;
 159}
 160
 161/* Transmit packet */
 162static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
 163{
 164        struct macb *lp = netdev_priv(dev);
 165
 166        if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
 167                netif_stop_queue(dev);
 168
 169                /* Store packet information (to free when Tx completed) */
 170                lp->skb = skb;
 171                lp->skb_length = skb->len;
 172                lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
 173                                                        DMA_TO_DEVICE);
 174
 175                /* Set address of the data in the Transmit Address register */
 176                macb_writel(lp, TAR, lp->skb_physaddr);
 177                /* Set length of the packet in the Transmit Control register */
 178                macb_writel(lp, TCR, skb->len);
 179
 180        } else {
 181                netdev_err(dev, "%s called, but device is busy!\n", __func__);
 182                return NETDEV_TX_BUSY;
 183        }
 184
 185        return NETDEV_TX_OK;
 186}
 187
 188/* Extract received frame from buffer descriptors and sent to upper layers.
 189 * (Called from interrupt context)
 190 */
 191static void at91ether_rx(struct net_device *dev)
 192{
 193        struct macb *lp = netdev_priv(dev);
 194        unsigned char *p_recv;
 195        struct sk_buff *skb;
 196        unsigned int pktlen;
 197
 198        while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
 199                p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
 200                pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
 201                skb = netdev_alloc_skb(dev, pktlen + 2);
 202                if (skb) {
 203                        skb_reserve(skb, 2);
 204                        memcpy(skb_put(skb, pktlen), p_recv, pktlen);
 205
 206                        skb->protocol = eth_type_trans(skb, dev);
 207                        lp->stats.rx_packets++;
 208                        lp->stats.rx_bytes += pktlen;
 209                        netif_rx(skb);
 210                } else {
 211                        lp->stats.rx_dropped++;
 212                        netdev_notice(dev, "Memory squeeze, dropping packet.\n");
 213                }
 214
 215                if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
 216                        lp->stats.multicast++;
 217
 218                /* reset ownership bit */
 219                lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
 220
 221                /* wrap after last buffer */
 222                if (lp->rx_tail == MAX_RX_DESCR - 1)
 223                        lp->rx_tail = 0;
 224                else
 225                        lp->rx_tail++;
 226        }
 227}
 228
 229/* MAC interrupt handler */
 230static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
 231{
 232        struct net_device *dev = dev_id;
 233        struct macb *lp = netdev_priv(dev);
 234        u32 intstatus, ctl;
 235
 236        /* MAC Interrupt Status register indicates what interrupts are pending.
 237         * It is automatically cleared once read.
 238         */
 239        intstatus = macb_readl(lp, ISR);
 240
 241        /* Receive complete */
 242        if (intstatus & MACB_BIT(RCOMP))
 243                at91ether_rx(dev);
 244
 245        /* Transmit complete */
 246        if (intstatus & MACB_BIT(TCOMP)) {
 247                /* The TCOM bit is set even if the transmission failed */
 248                if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
 249                        lp->stats.tx_errors++;
 250
 251                if (lp->skb) {
 252                        dev_kfree_skb_irq(lp->skb);
 253                        lp->skb = NULL;
 254                        dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
 255                        lp->stats.tx_packets++;
 256                        lp->stats.tx_bytes += lp->skb_length;
 257                }
 258                netif_wake_queue(dev);
 259        }
 260
 261        /* Work-around for EMAC Errata section 41.3.1 */
 262        if (intstatus & MACB_BIT(RXUBR)) {
 263                ctl = macb_readl(lp, NCR);
 264                macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
 265                macb_writel(lp, NCR, ctl | MACB_BIT(RE));
 266        }
 267
 268        if (intstatus & MACB_BIT(ISR_ROVR))
 269                netdev_err(dev, "ROVR error\n");
 270
 271        return IRQ_HANDLED;
 272}
 273
 274#ifdef CONFIG_NET_POLL_CONTROLLER
 275static void at91ether_poll_controller(struct net_device *dev)
 276{
 277        unsigned long flags;
 278
 279        local_irq_save(flags);
 280        at91ether_interrupt(dev->irq, dev);
 281        local_irq_restore(flags);
 282}
 283#endif
 284
 285static const struct net_device_ops at91ether_netdev_ops = {
 286        .ndo_open               = at91ether_open,
 287        .ndo_stop               = at91ether_close,
 288        .ndo_start_xmit         = at91ether_start_xmit,
 289        .ndo_get_stats          = macb_get_stats,
 290        .ndo_set_rx_mode        = macb_set_rx_mode,
 291        .ndo_set_mac_address    = eth_mac_addr,
 292        .ndo_do_ioctl           = macb_ioctl,
 293        .ndo_validate_addr      = eth_validate_addr,
 294        .ndo_change_mtu         = eth_change_mtu,
 295#ifdef CONFIG_NET_POLL_CONTROLLER
 296        .ndo_poll_controller    = at91ether_poll_controller,
 297#endif
 298};
 299
 300#if defined(CONFIG_OF)
 301static const struct of_device_id at91ether_dt_ids[] = {
 302        { .compatible = "cdns,at91rm9200-emac" },
 303        { .compatible = "cdns,emac" },
 304        { /* sentinel */ }
 305};
 306
 307MODULE_DEVICE_TABLE(of, at91ether_dt_ids);
 308
 309static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
 310{
 311        struct device_node *np = pdev->dev.of_node;
 312
 313        if (np)
 314                return of_get_phy_mode(np);
 315
 316        return -ENODEV;
 317}
 318
 319static int at91ether_get_hwaddr_dt(struct macb *bp)
 320{
 321        struct device_node *np = bp->pdev->dev.of_node;
 322
 323        if (np) {
 324                const char *mac = of_get_mac_address(np);
 325                if (mac) {
 326                        memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
 327                        return 0;
 328                }
 329        }
 330
 331        return -ENODEV;
 332}
 333#else
 334static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
 335{
 336        return -ENODEV;
 337}
 338static int at91ether_get_hwaddr_dt(struct macb *bp)
 339{
 340        return -ENODEV;
 341}
 342#endif
 343
 344/* Detect MAC & PHY and perform ethernet interface initialization */
 345static int __init at91ether_probe(struct platform_device *pdev)
 346{
 347        struct macb_platform_data *board_data = pdev->dev.platform_data;
 348        struct resource *regs;
 349        struct net_device *dev;
 350        struct phy_device *phydev;
 351        struct pinctrl *pinctrl;
 352        struct macb *lp;
 353        int res;
 354        u32 reg;
 355
 356        regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 357        if (!regs)
 358                return -ENOENT;
 359
 360        pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
 361        if (IS_ERR(pinctrl)) {
 362                res = PTR_ERR(pinctrl);
 363                if (res == -EPROBE_DEFER)
 364                        return res;
 365
 366                dev_warn(&pdev->dev, "No pinctrl provided\n");
 367        }
 368
 369        dev = alloc_etherdev(sizeof(struct macb));
 370        if (!dev)
 371                return -ENOMEM;
 372
 373        lp = netdev_priv(dev);
 374        lp->pdev = pdev;
 375        lp->dev = dev;
 376        spin_lock_init(&lp->lock);
 377
 378        /* physical base address */
 379        dev->base_addr = regs->start;
 380        lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
 381        if (!lp->regs) {
 382                res = -ENOMEM;
 383                goto err_free_dev;
 384        }
 385
 386        /* Clock */
 387        lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
 388        if (IS_ERR(lp->pclk)) {
 389                res = PTR_ERR(lp->pclk);
 390                goto err_free_dev;
 391        }
 392        clk_enable(lp->pclk);
 393
 394        /* Install the interrupt handler */
 395        dev->irq = platform_get_irq(pdev, 0);
 396        res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
 397        if (res)
 398                goto err_disable_clock;
 399
 400        ether_setup(dev);
 401        dev->netdev_ops = &at91ether_netdev_ops;
 402        dev->ethtool_ops = &macb_ethtool_ops;
 403        platform_set_drvdata(pdev, dev);
 404        SET_NETDEV_DEV(dev, &pdev->dev);
 405
 406        res = at91ether_get_hwaddr_dt(lp);
 407        if (res < 0)
 408                macb_get_hwaddr(lp);
 409
 410        res = at91ether_get_phy_mode_dt(pdev);
 411        if (res < 0) {
 412                if (board_data && board_data->is_rmii)
 413                        lp->phy_interface = PHY_INTERFACE_MODE_RMII;
 414                else
 415                        lp->phy_interface = PHY_INTERFACE_MODE_MII;
 416        } else {
 417                lp->phy_interface = res;
 418        }
 419
 420        macb_writel(lp, NCR, 0);
 421
 422        reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
 423        if (lp->phy_interface == PHY_INTERFACE_MODE_RMII)
 424                reg |= MACB_BIT(RM9200_RMII);
 425
 426        macb_writel(lp, NCFGR, reg);
 427
 428        /* Register the network interface */
 429        res = register_netdev(dev);
 430        if (res)
 431                goto err_disable_clock;
 432
 433        if (macb_mii_init(lp) != 0)
 434                goto err_out_unregister_netdev;
 435
 436        /* will be enabled in open() */
 437        netif_carrier_off(dev);
 438
 439        phydev = lp->phy_dev;
 440        netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
 441                                phydev->drv->name, dev_name(&phydev->dev),
 442                                phydev->irq);
 443
 444        /* Display ethernet banner */
 445        netdev_info(dev, "AT91 ethernet at 0x%08lx int=%d (%pM)\n",
 446                                dev->base_addr, dev->irq, dev->dev_addr);
 447
 448        return 0;
 449
 450err_out_unregister_netdev:
 451        unregister_netdev(dev);
 452err_disable_clock:
 453        clk_disable(lp->pclk);
 454err_free_dev:
 455        free_netdev(dev);
 456        return res;
 457}
 458
 459static int at91ether_remove(struct platform_device *pdev)
 460{
 461        struct net_device *dev = platform_get_drvdata(pdev);
 462        struct macb *lp = netdev_priv(dev);
 463
 464        if (lp->phy_dev)
 465                phy_disconnect(lp->phy_dev);
 466
 467        mdiobus_unregister(lp->mii_bus);
 468        kfree(lp->mii_bus->irq);
 469        mdiobus_free(lp->mii_bus);
 470        unregister_netdev(dev);
 471        clk_disable(lp->pclk);
 472        free_netdev(dev);
 473        platform_set_drvdata(pdev, NULL);
 474
 475        return 0;
 476}
 477
 478#ifdef CONFIG_PM
 479static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
 480{
 481        struct net_device *net_dev = platform_get_drvdata(pdev);
 482        struct macb *lp = netdev_priv(net_dev);
 483
 484        if (netif_running(net_dev)) {
 485                netif_stop_queue(net_dev);
 486                netif_device_detach(net_dev);
 487
 488                clk_disable(lp->pclk);
 489        }
 490        return 0;
 491}
 492
 493static int at91ether_resume(struct platform_device *pdev)
 494{
 495        struct net_device *net_dev = platform_get_drvdata(pdev);
 496        struct macb *lp = netdev_priv(net_dev);
 497
 498        if (netif_running(net_dev)) {
 499                clk_enable(lp->pclk);
 500
 501                netif_device_attach(net_dev);
 502                netif_start_queue(net_dev);
 503        }
 504        return 0;
 505}
 506#else
 507#define at91ether_suspend       NULL
 508#define at91ether_resume        NULL
 509#endif
 510
 511static struct platform_driver at91ether_driver = {
 512        .remove         = at91ether_remove,
 513        .suspend        = at91ether_suspend,
 514        .resume         = at91ether_resume,
 515        .driver         = {
 516                .name   = "at91_ether",
 517                .owner  = THIS_MODULE,
 518                .of_match_table = of_match_ptr(at91ether_dt_ids),
 519        },
 520};
 521
 522static int __init at91ether_init(void)
 523{
 524        return platform_driver_probe(&at91ether_driver, at91ether_probe);
 525}
 526
 527static void __exit at91ether_exit(void)
 528{
 529        platform_driver_unregister(&at91ether_driver);
 530}
 531
 532module_init(at91ether_init)
 533module_exit(at91ether_exit)
 534
 535MODULE_LICENSE("GPL");
 536MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
 537MODULE_AUTHOR("Andrew Victor");
 538MODULE_ALIAS("platform:at91_ether");
 539