linux/drivers/net/ethernet/aeroflex/greth.c
<<
>>
Prefs
   1/*
   2 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
   3 *
   4 * 2005-2010 (c) Aeroflex Gaisler AB
   5 *
   6 * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
   7 * available in the GRLIB VHDL IP core library.
   8 *
   9 * Full documentation of both cores can be found here:
  10 * http://www.gaisler.com/products/grlib/grip.pdf
  11 *
  12 * The Gigabit version supports scatter/gather DMA, any alignment of
  13 * buffers and checksum offloading.
  14 *
  15 * This program is free software; you can redistribute it and/or modify it
  16 * under the terms of the GNU General Public License as published by the
  17 * Free Software Foundation; either version 2 of the License, or (at your
  18 * option) any later version.
  19 *
  20 * Contributors: Kristoffer Glembo
  21 *               Daniel Hellstrom
  22 *               Marko Isomaki
  23 */
  24
  25#include <linux/dma-mapping.h>
  26#include <linux/module.h>
  27#include <linux/uaccess.h>
  28#include <linux/interrupt.h>
  29#include <linux/netdevice.h>
  30#include <linux/etherdevice.h>
  31#include <linux/ethtool.h>
  32#include <linux/skbuff.h>
  33#include <linux/io.h>
  34#include <linux/crc32.h>
  35#include <linux/mii.h>
  36#include <linux/of_device.h>
  37#include <linux/of_net.h>
  38#include <linux/of_platform.h>
  39#include <linux/slab.h>
  40#include <asm/cacheflush.h>
  41#include <asm/byteorder.h>
  42
  43#ifdef CONFIG_SPARC
  44#include <asm/idprom.h>
  45#endif
  46
  47#include "greth.h"
  48
  49#define GRETH_DEF_MSG_ENABLE      \
  50        (NETIF_MSG_DRV          | \
  51         NETIF_MSG_PROBE        | \
  52         NETIF_MSG_LINK         | \
  53         NETIF_MSG_IFDOWN       | \
  54         NETIF_MSG_IFUP         | \
  55         NETIF_MSG_RX_ERR       | \
  56         NETIF_MSG_TX_ERR)
  57
  58static int greth_debug = -1;    /* -1 == use GRETH_DEF_MSG_ENABLE as value */
  59module_param(greth_debug, int, 0);
  60MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
  61
  62/* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
  63static int macaddr[6];
  64module_param_array(macaddr, int, NULL, 0);
  65MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
  66
  67static int greth_edcl = 1;
  68module_param(greth_edcl, int, 0);
  69MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
  70
  71static int greth_open(struct net_device *dev);
  72static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
  73           struct net_device *dev);
  74static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
  75           struct net_device *dev);
  76static int greth_rx(struct net_device *dev, int limit);
  77static int greth_rx_gbit(struct net_device *dev, int limit);
  78static void greth_clean_tx(struct net_device *dev);
  79static void greth_clean_tx_gbit(struct net_device *dev);
  80static irqreturn_t greth_interrupt(int irq, void *dev_id);
  81static int greth_close(struct net_device *dev);
  82static int greth_set_mac_add(struct net_device *dev, void *p);
  83static void greth_set_multicast_list(struct net_device *dev);
  84
  85#define GRETH_REGLOAD(a)            (be32_to_cpu(__raw_readl(&(a))))
  86#define GRETH_REGSAVE(a, v)         (__raw_writel(cpu_to_be32(v), &(a)))
  87#define GRETH_REGORIN(a, v)         (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
  88#define GRETH_REGANDIN(a, v)        (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
  89
  90#define NEXT_TX(N)      (((N) + 1) & GRETH_TXBD_NUM_MASK)
  91#define SKIP_TX(N, C)   (((N) + C) & GRETH_TXBD_NUM_MASK)
  92#define NEXT_RX(N)      (((N) + 1) & GRETH_RXBD_NUM_MASK)
  93
  94static void greth_print_rx_packet(void *addr, int len)
  95{
  96        print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
  97                        addr, len, true);
  98}
  99
 100static void greth_print_tx_packet(struct sk_buff *skb)
 101{
 102        int i;
 103        int length;
 104
 105        if (skb_shinfo(skb)->nr_frags == 0)
 106                length = skb->len;
 107        else
 108                length = skb_headlen(skb);
 109
 110        print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
 111                        skb->data, length, true);
 112
 113        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 114
 115                print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
 116                               skb_frag_address(&skb_shinfo(skb)->frags[i]),
 117                               skb_shinfo(skb)->frags[i].size, true);
 118        }
 119}
 120
 121static inline void greth_enable_tx(struct greth_private *greth)
 122{
 123        wmb();
 124        GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
 125}
 126
 127static inline void greth_enable_tx_and_irq(struct greth_private *greth)
 128{
 129        wmb(); /* BDs must been written to memory before enabling TX */
 130        GRETH_REGORIN(greth->regs->control, GRETH_TXEN | GRETH_TXI);
 131}
 132
 133static inline void greth_disable_tx(struct greth_private *greth)
 134{
 135        GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
 136}
 137
 138static inline void greth_enable_rx(struct greth_private *greth)
 139{
 140        wmb();
 141        GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
 142}
 143
 144static inline void greth_disable_rx(struct greth_private *greth)
 145{
 146        GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
 147}
 148
 149static inline void greth_enable_irqs(struct greth_private *greth)
 150{
 151        GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
 152}
 153
 154static inline void greth_disable_irqs(struct greth_private *greth)
 155{
 156        GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
 157}
 158
 159static inline void greth_write_bd(u32 *bd, u32 val)
 160{
 161        __raw_writel(cpu_to_be32(val), bd);
 162}
 163
 164static inline u32 greth_read_bd(u32 *bd)
 165{
 166        return be32_to_cpu(__raw_readl(bd));
 167}
 168
 169static void greth_clean_rings(struct greth_private *greth)
 170{
 171        int i;
 172        struct greth_bd *rx_bdp = greth->rx_bd_base;
 173        struct greth_bd *tx_bdp = greth->tx_bd_base;
 174
 175        if (greth->gbit_mac) {
 176
 177                /* Free and unmap RX buffers */
 178                for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
 179                        if (greth->rx_skbuff[i] != NULL) {
 180                                dev_kfree_skb(greth->rx_skbuff[i]);
 181                                dma_unmap_single(greth->dev,
 182                                                 greth_read_bd(&rx_bdp->addr),
 183                                                 MAX_FRAME_SIZE+NET_IP_ALIGN,
 184                                                 DMA_FROM_DEVICE);
 185                        }
 186                }
 187
 188                /* TX buffers */
 189                while (greth->tx_free < GRETH_TXBD_NUM) {
 190
 191                        struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
 192                        int nr_frags = skb_shinfo(skb)->nr_frags;
 193                        tx_bdp = greth->tx_bd_base + greth->tx_last;
 194                        greth->tx_last = NEXT_TX(greth->tx_last);
 195
 196                        dma_unmap_single(greth->dev,
 197                                         greth_read_bd(&tx_bdp->addr),
 198                                         skb_headlen(skb),
 199                                         DMA_TO_DEVICE);
 200
 201                        for (i = 0; i < nr_frags; i++) {
 202                                skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 203                                tx_bdp = greth->tx_bd_base + greth->tx_last;
 204
 205                                dma_unmap_page(greth->dev,
 206                                               greth_read_bd(&tx_bdp->addr),
 207                                               skb_frag_size(frag),
 208                                               DMA_TO_DEVICE);
 209
 210                                greth->tx_last = NEXT_TX(greth->tx_last);
 211                        }
 212                        greth->tx_free += nr_frags+1;
 213                        dev_kfree_skb(skb);
 214                }
 215
 216
 217        } else { /* 10/100 Mbps MAC */
 218
 219                for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
 220                        kfree(greth->rx_bufs[i]);
 221                        dma_unmap_single(greth->dev,
 222                                         greth_read_bd(&rx_bdp->addr),
 223                                         MAX_FRAME_SIZE,
 224                                         DMA_FROM_DEVICE);
 225                }
 226                for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
 227                        kfree(greth->tx_bufs[i]);
 228                        dma_unmap_single(greth->dev,
 229                                         greth_read_bd(&tx_bdp->addr),
 230                                         MAX_FRAME_SIZE,
 231                                         DMA_TO_DEVICE);
 232                }
 233        }
 234}
 235
 236static int greth_init_rings(struct greth_private *greth)
 237{
 238        struct sk_buff *skb;
 239        struct greth_bd *rx_bd, *tx_bd;
 240        u32 dma_addr;
 241        int i;
 242
 243        rx_bd = greth->rx_bd_base;
 244        tx_bd = greth->tx_bd_base;
 245
 246        /* Initialize descriptor rings and buffers */
 247        if (greth->gbit_mac) {
 248
 249                for (i = 0; i < GRETH_RXBD_NUM; i++) {
 250                        skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
 251                        if (skb == NULL) {
 252                                if (netif_msg_ifup(greth))
 253                                        dev_err(greth->dev, "Error allocating DMA ring.\n");
 254                                goto cleanup;
 255                        }
 256                        skb_reserve(skb, NET_IP_ALIGN);
 257                        dma_addr = dma_map_single(greth->dev,
 258                                                  skb->data,
 259                                                  MAX_FRAME_SIZE+NET_IP_ALIGN,
 260                                                  DMA_FROM_DEVICE);
 261
 262                        if (dma_mapping_error(greth->dev, dma_addr)) {
 263                                if (netif_msg_ifup(greth))
 264                                        dev_err(greth->dev, "Could not create initial DMA mapping\n");
 265                                goto cleanup;
 266                        }
 267                        greth->rx_skbuff[i] = skb;
 268                        greth_write_bd(&rx_bd[i].addr, dma_addr);
 269                        greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
 270                }
 271
 272        } else {
 273
 274                /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
 275                for (i = 0; i < GRETH_RXBD_NUM; i++) {
 276
 277                        greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
 278
 279                        if (greth->rx_bufs[i] == NULL) {
 280                                if (netif_msg_ifup(greth))
 281                                        dev_err(greth->dev, "Error allocating DMA ring.\n");
 282                                goto cleanup;
 283                        }
 284
 285                        dma_addr = dma_map_single(greth->dev,
 286                                                  greth->rx_bufs[i],
 287                                                  MAX_FRAME_SIZE,
 288                                                  DMA_FROM_DEVICE);
 289
 290                        if (dma_mapping_error(greth->dev, dma_addr)) {
 291                                if (netif_msg_ifup(greth))
 292                                        dev_err(greth->dev, "Could not create initial DMA mapping\n");
 293                                goto cleanup;
 294                        }
 295                        greth_write_bd(&rx_bd[i].addr, dma_addr);
 296                        greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
 297                }
 298                for (i = 0; i < GRETH_TXBD_NUM; i++) {
 299
 300                        greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
 301
 302                        if (greth->tx_bufs[i] == NULL) {
 303                                if (netif_msg_ifup(greth))
 304                                        dev_err(greth->dev, "Error allocating DMA ring.\n");
 305                                goto cleanup;
 306                        }
 307
 308                        dma_addr = dma_map_single(greth->dev,
 309                                                  greth->tx_bufs[i],
 310                                                  MAX_FRAME_SIZE,
 311                                                  DMA_TO_DEVICE);
 312
 313                        if (dma_mapping_error(greth->dev, dma_addr)) {
 314                                if (netif_msg_ifup(greth))
 315                                        dev_err(greth->dev, "Could not create initial DMA mapping\n");
 316                                goto cleanup;
 317                        }
 318                        greth_write_bd(&tx_bd[i].addr, dma_addr);
 319                        greth_write_bd(&tx_bd[i].stat, 0);
 320                }
 321        }
 322        greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
 323                       greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
 324
 325        /* Initialize pointers. */
 326        greth->rx_cur = 0;
 327        greth->tx_next = 0;
 328        greth->tx_last = 0;
 329        greth->tx_free = GRETH_TXBD_NUM;
 330
 331        /* Initialize descriptor base address */
 332        GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
 333        GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
 334
 335        return 0;
 336
 337cleanup:
 338        greth_clean_rings(greth);
 339        return -ENOMEM;
 340}
 341
 342static int greth_open(struct net_device *dev)
 343{
 344        struct greth_private *greth = netdev_priv(dev);
 345        int err;
 346
 347        err = greth_init_rings(greth);
 348        if (err) {
 349                if (netif_msg_ifup(greth))
 350                        dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
 351                return err;
 352        }
 353
 354        err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
 355        if (err) {
 356                if (netif_msg_ifup(greth))
 357                        dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
 358                greth_clean_rings(greth);
 359                return err;
 360        }
 361
 362        if (netif_msg_ifup(greth))
 363                dev_dbg(&dev->dev, " starting queue\n");
 364        netif_start_queue(dev);
 365
 366        GRETH_REGSAVE(greth->regs->status, 0xFF);
 367
 368        napi_enable(&greth->napi);
 369
 370        greth_enable_irqs(greth);
 371        greth_enable_tx(greth);
 372        greth_enable_rx(greth);
 373        return 0;
 374
 375}
 376
 377static int greth_close(struct net_device *dev)
 378{
 379        struct greth_private *greth = netdev_priv(dev);
 380
 381        napi_disable(&greth->napi);
 382
 383        greth_disable_irqs(greth);
 384        greth_disable_tx(greth);
 385        greth_disable_rx(greth);
 386
 387        netif_stop_queue(dev);
 388
 389        free_irq(greth->irq, (void *) dev);
 390
 391        greth_clean_rings(greth);
 392
 393        return 0;
 394}
 395
 396static netdev_tx_t
 397greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
 398{
 399        struct greth_private *greth = netdev_priv(dev);
 400        struct greth_bd *bdp;
 401        int err = NETDEV_TX_OK;
 402        u32 status, dma_addr, ctrl;
 403        unsigned long flags;
 404
 405        /* Clean TX Ring */
 406        greth_clean_tx(greth->netdev);
 407
 408        if (unlikely(greth->tx_free <= 0)) {
 409                spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
 410                ctrl = GRETH_REGLOAD(greth->regs->control);
 411                /* Enable TX IRQ only if not already in poll() routine */
 412                if (ctrl & GRETH_RXI)
 413                        GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
 414                netif_stop_queue(dev);
 415                spin_unlock_irqrestore(&greth->devlock, flags);
 416                return NETDEV_TX_BUSY;
 417        }
 418
 419        if (netif_msg_pktdata(greth))
 420                greth_print_tx_packet(skb);
 421
 422
 423        if (unlikely(skb->len > MAX_FRAME_SIZE)) {
 424                dev->stats.tx_errors++;
 425                goto out;
 426        }
 427
 428        bdp = greth->tx_bd_base + greth->tx_next;
 429        dma_addr = greth_read_bd(&bdp->addr);
 430
 431        memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
 432
 433        dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
 434
 435        status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
 436        greth->tx_bufs_length[greth->tx_next] = skb->len & GRETH_BD_LEN;
 437
 438        /* Wrap around descriptor ring */
 439        if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
 440                status |= GRETH_BD_WR;
 441        }
 442
 443        greth->tx_next = NEXT_TX(greth->tx_next);
 444        greth->tx_free--;
 445
 446        /* Write descriptor control word and enable transmission */
 447        greth_write_bd(&bdp->stat, status);
 448        spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
 449        greth_enable_tx(greth);
 450        spin_unlock_irqrestore(&greth->devlock, flags);
 451
 452out:
 453        dev_kfree_skb(skb);
 454        return err;
 455}
 456
 457static inline u16 greth_num_free_bds(u16 tx_last, u16 tx_next)
 458{
 459        if (tx_next < tx_last)
 460                return (tx_last - tx_next) - 1;
 461        else
 462                return GRETH_TXBD_NUM - (tx_next - tx_last) - 1;
 463}
 464
 465static netdev_tx_t
 466greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
 467{
 468        struct greth_private *greth = netdev_priv(dev);
 469        struct greth_bd *bdp;
 470        u32 status, dma_addr;
 471        int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
 472        unsigned long flags;
 473        u16 tx_last;
 474
 475        nr_frags = skb_shinfo(skb)->nr_frags;
 476        tx_last = greth->tx_last;
 477        rmb(); /* tx_last is updated by the poll task */
 478
 479        if (greth_num_free_bds(tx_last, greth->tx_next) < nr_frags + 1) {
 480                netif_stop_queue(dev);
 481                err = NETDEV_TX_BUSY;
 482                goto out;
 483        }
 484
 485        if (netif_msg_pktdata(greth))
 486                greth_print_tx_packet(skb);
 487
 488        if (unlikely(skb->len > MAX_FRAME_SIZE)) {
 489                dev->stats.tx_errors++;
 490                goto out;
 491        }
 492
 493        /* Save skb pointer. */
 494        greth->tx_skbuff[greth->tx_next] = skb;
 495
 496        /* Linear buf */
 497        if (nr_frags != 0)
 498                status = GRETH_TXBD_MORE;
 499        else
 500                status = GRETH_BD_IE;
 501
 502        if (skb->ip_summed == CHECKSUM_PARTIAL)
 503                status |= GRETH_TXBD_CSALL;
 504        status |= skb_headlen(skb) & GRETH_BD_LEN;
 505        if (greth->tx_next == GRETH_TXBD_NUM_MASK)
 506                status |= GRETH_BD_WR;
 507
 508
 509        bdp = greth->tx_bd_base + greth->tx_next;
 510        greth_write_bd(&bdp->stat, status);
 511        dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
 512
 513        if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
 514                goto map_error;
 515
 516        greth_write_bd(&bdp->addr, dma_addr);
 517
 518        curr_tx = NEXT_TX(greth->tx_next);
 519
 520        /* Frags */
 521        for (i = 0; i < nr_frags; i++) {
 522                skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 523                greth->tx_skbuff[curr_tx] = NULL;
 524                bdp = greth->tx_bd_base + curr_tx;
 525
 526                status = GRETH_BD_EN;
 527                if (skb->ip_summed == CHECKSUM_PARTIAL)
 528                        status |= GRETH_TXBD_CSALL;
 529                status |= skb_frag_size(frag) & GRETH_BD_LEN;
 530
 531                /* Wrap around descriptor ring */
 532                if (curr_tx == GRETH_TXBD_NUM_MASK)
 533                        status |= GRETH_BD_WR;
 534
 535                /* More fragments left */
 536                if (i < nr_frags - 1)
 537                        status |= GRETH_TXBD_MORE;
 538                else
 539                        status |= GRETH_BD_IE; /* enable IRQ on last fragment */
 540
 541                greth_write_bd(&bdp->stat, status);
 542
 543                dma_addr = skb_frag_dma_map(greth->dev, frag, 0, skb_frag_size(frag),
 544                                            DMA_TO_DEVICE);
 545
 546                if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
 547                        goto frag_map_error;
 548
 549                greth_write_bd(&bdp->addr, dma_addr);
 550
 551                curr_tx = NEXT_TX(curr_tx);
 552        }
 553
 554        wmb();
 555
 556        /* Enable the descriptor chain by enabling the first descriptor */
 557        bdp = greth->tx_bd_base + greth->tx_next;
 558        greth_write_bd(&bdp->stat,
 559                       greth_read_bd(&bdp->stat) | GRETH_BD_EN);
 560
 561        spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
 562        greth->tx_next = curr_tx;
 563        greth_enable_tx_and_irq(greth);
 564        spin_unlock_irqrestore(&greth->devlock, flags);
 565
 566        return NETDEV_TX_OK;
 567
 568frag_map_error:
 569        /* Unmap SKB mappings that succeeded and disable descriptor */
 570        for (i = 0; greth->tx_next + i != curr_tx; i++) {
 571                bdp = greth->tx_bd_base + greth->tx_next + i;
 572                dma_unmap_single(greth->dev,
 573                                 greth_read_bd(&bdp->addr),
 574                                 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
 575                                 DMA_TO_DEVICE);
 576                greth_write_bd(&bdp->stat, 0);
 577        }
 578map_error:
 579        if (net_ratelimit())
 580                dev_warn(greth->dev, "Could not create TX DMA mapping\n");
 581        dev_kfree_skb(skb);
 582out:
 583        return err;
 584}
 585
 586static irqreturn_t greth_interrupt(int irq, void *dev_id)
 587{
 588        struct net_device *dev = dev_id;
 589        struct greth_private *greth;
 590        u32 status, ctrl;
 591        irqreturn_t retval = IRQ_NONE;
 592
 593        greth = netdev_priv(dev);
 594
 595        spin_lock(&greth->devlock);
 596
 597        /* Get the interrupt events that caused us to be here. */
 598        status = GRETH_REGLOAD(greth->regs->status);
 599
 600        /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
 601         * set regardless of whether IRQ is enabled or not. Especially
 602         * important when shared IRQ.
 603         */
 604        ctrl = GRETH_REGLOAD(greth->regs->control);
 605
 606        /* Handle rx and tx interrupts through poll */
 607        if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
 608            ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
 609                retval = IRQ_HANDLED;
 610
 611                /* Disable interrupts and schedule poll() */
 612                greth_disable_irqs(greth);
 613                napi_schedule(&greth->napi);
 614        }
 615
 616        spin_unlock(&greth->devlock);
 617
 618        return retval;
 619}
 620
 621static void greth_clean_tx(struct net_device *dev)
 622{
 623        struct greth_private *greth;
 624        struct greth_bd *bdp;
 625        u32 stat;
 626
 627        greth = netdev_priv(dev);
 628
 629        while (1) {
 630                bdp = greth->tx_bd_base + greth->tx_last;
 631                GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
 632                mb();
 633                stat = greth_read_bd(&bdp->stat);
 634
 635                if (unlikely(stat & GRETH_BD_EN))
 636                        break;
 637
 638                if (greth->tx_free == GRETH_TXBD_NUM)
 639                        break;
 640
 641                /* Check status for errors */
 642                if (unlikely(stat & GRETH_TXBD_STATUS)) {
 643                        dev->stats.tx_errors++;
 644                        if (stat & GRETH_TXBD_ERR_AL)
 645                                dev->stats.tx_aborted_errors++;
 646                        if (stat & GRETH_TXBD_ERR_UE)
 647                                dev->stats.tx_fifo_errors++;
 648                }
 649                dev->stats.tx_packets++;
 650                dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
 651                greth->tx_last = NEXT_TX(greth->tx_last);
 652                greth->tx_free++;
 653        }
 654
 655        if (greth->tx_free > 0) {
 656                netif_wake_queue(dev);
 657        }
 658}
 659
 660static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
 661{
 662        /* Check status for errors */
 663        if (unlikely(stat & GRETH_TXBD_STATUS)) {
 664                dev->stats.tx_errors++;
 665                if (stat & GRETH_TXBD_ERR_AL)
 666                        dev->stats.tx_aborted_errors++;
 667                if (stat & GRETH_TXBD_ERR_UE)
 668                        dev->stats.tx_fifo_errors++;
 669                if (stat & GRETH_TXBD_ERR_LC)
 670                        dev->stats.tx_aborted_errors++;
 671        }
 672        dev->stats.tx_packets++;
 673}
 674
 675static void greth_clean_tx_gbit(struct net_device *dev)
 676{
 677        struct greth_private *greth;
 678        struct greth_bd *bdp, *bdp_last_frag;
 679        struct sk_buff *skb = NULL;
 680        u32 stat;
 681        int nr_frags, i;
 682        u16 tx_last;
 683
 684        greth = netdev_priv(dev);
 685        tx_last = greth->tx_last;
 686
 687        while (tx_last != greth->tx_next) {
 688
 689                skb = greth->tx_skbuff[tx_last];
 690
 691                nr_frags = skb_shinfo(skb)->nr_frags;
 692
 693                /* We only clean fully completed SKBs */
 694                bdp_last_frag = greth->tx_bd_base + SKIP_TX(tx_last, nr_frags);
 695
 696                GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
 697                mb();
 698                stat = greth_read_bd(&bdp_last_frag->stat);
 699
 700                if (stat & GRETH_BD_EN)
 701                        break;
 702
 703                greth->tx_skbuff[tx_last] = NULL;
 704
 705                greth_update_tx_stats(dev, stat);
 706                dev->stats.tx_bytes += skb->len;
 707
 708                bdp = greth->tx_bd_base + tx_last;
 709
 710                tx_last = NEXT_TX(tx_last);
 711
 712                dma_unmap_single(greth->dev,
 713                                 greth_read_bd(&bdp->addr),
 714                                 skb_headlen(skb),
 715                                 DMA_TO_DEVICE);
 716
 717                for (i = 0; i < nr_frags; i++) {
 718                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 719                        bdp = greth->tx_bd_base + tx_last;
 720
 721                        dma_unmap_page(greth->dev,
 722                                       greth_read_bd(&bdp->addr),
 723                                       skb_frag_size(frag),
 724                                       DMA_TO_DEVICE);
 725
 726                        tx_last = NEXT_TX(tx_last);
 727                }
 728                dev_kfree_skb(skb);
 729        }
 730        if (skb) { /* skb is set only if the above while loop was entered */
 731                wmb();
 732                greth->tx_last = tx_last;
 733
 734                if (netif_queue_stopped(dev) &&
 735                    (greth_num_free_bds(tx_last, greth->tx_next) >
 736                    (MAX_SKB_FRAGS+1)))
 737                        netif_wake_queue(dev);
 738        }
 739}
 740
 741static int greth_rx(struct net_device *dev, int limit)
 742{
 743        struct greth_private *greth;
 744        struct greth_bd *bdp;
 745        struct sk_buff *skb;
 746        int pkt_len;
 747        int bad, count;
 748        u32 status, dma_addr;
 749        unsigned long flags;
 750
 751        greth = netdev_priv(dev);
 752
 753        for (count = 0; count < limit; ++count) {
 754
 755                bdp = greth->rx_bd_base + greth->rx_cur;
 756                GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
 757                mb();
 758                status = greth_read_bd(&bdp->stat);
 759
 760                if (unlikely(status & GRETH_BD_EN)) {
 761                        break;
 762                }
 763
 764                dma_addr = greth_read_bd(&bdp->addr);
 765                bad = 0;
 766
 767                /* Check status for errors. */
 768                if (unlikely(status & GRETH_RXBD_STATUS)) {
 769                        if (status & GRETH_RXBD_ERR_FT) {
 770                                dev->stats.rx_length_errors++;
 771                                bad = 1;
 772                        }
 773                        if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
 774                                dev->stats.rx_frame_errors++;
 775                                bad = 1;
 776                        }
 777                        if (status & GRETH_RXBD_ERR_CRC) {
 778                                dev->stats.rx_crc_errors++;
 779                                bad = 1;
 780                        }
 781                }
 782                if (unlikely(bad)) {
 783                        dev->stats.rx_errors++;
 784
 785                } else {
 786
 787                        pkt_len = status & GRETH_BD_LEN;
 788
 789                        skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
 790
 791                        if (unlikely(skb == NULL)) {
 792
 793                                if (net_ratelimit())
 794                                        dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
 795
 796                                dev->stats.rx_dropped++;
 797
 798                        } else {
 799                                skb_reserve(skb, NET_IP_ALIGN);
 800
 801                                dma_sync_single_for_cpu(greth->dev,
 802                                                        dma_addr,
 803                                                        pkt_len,
 804                                                        DMA_FROM_DEVICE);
 805
 806                                if (netif_msg_pktdata(greth))
 807                                        greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
 808
 809                                skb_put_data(skb, phys_to_virt(dma_addr),
 810                                             pkt_len);
 811
 812                                skb->protocol = eth_type_trans(skb, dev);
 813                                dev->stats.rx_bytes += pkt_len;
 814                                dev->stats.rx_packets++;
 815                                netif_receive_skb(skb);
 816                        }
 817                }
 818
 819                status = GRETH_BD_EN | GRETH_BD_IE;
 820                if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
 821                        status |= GRETH_BD_WR;
 822                }
 823
 824                wmb();
 825                greth_write_bd(&bdp->stat, status);
 826
 827                dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
 828
 829                spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
 830                greth_enable_rx(greth);
 831                spin_unlock_irqrestore(&greth->devlock, flags);
 832
 833                greth->rx_cur = NEXT_RX(greth->rx_cur);
 834        }
 835
 836        return count;
 837}
 838
 839static inline int hw_checksummed(u32 status)
 840{
 841
 842        if (status & GRETH_RXBD_IP_FRAG)
 843                return 0;
 844
 845        if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
 846                return 0;
 847
 848        if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
 849                return 0;
 850
 851        if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
 852                return 0;
 853
 854        return 1;
 855}
 856
 857static int greth_rx_gbit(struct net_device *dev, int limit)
 858{
 859        struct greth_private *greth;
 860        struct greth_bd *bdp;
 861        struct sk_buff *skb, *newskb;
 862        int pkt_len;
 863        int bad, count = 0;
 864        u32 status, dma_addr;
 865        unsigned long flags;
 866
 867        greth = netdev_priv(dev);
 868
 869        for (count = 0; count < limit; ++count) {
 870
 871                bdp = greth->rx_bd_base + greth->rx_cur;
 872                skb = greth->rx_skbuff[greth->rx_cur];
 873                GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
 874                mb();
 875                status = greth_read_bd(&bdp->stat);
 876                bad = 0;
 877
 878                if (status & GRETH_BD_EN)
 879                        break;
 880
 881                /* Check status for errors. */
 882                if (unlikely(status & GRETH_RXBD_STATUS)) {
 883
 884                        if (status & GRETH_RXBD_ERR_FT) {
 885                                dev->stats.rx_length_errors++;
 886                                bad = 1;
 887                        } else if (status &
 888                                   (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
 889                                dev->stats.rx_frame_errors++;
 890                                bad = 1;
 891                        } else if (status & GRETH_RXBD_ERR_CRC) {
 892                                dev->stats.rx_crc_errors++;
 893                                bad = 1;
 894                        }
 895                }
 896
 897                /* Allocate new skb to replace current, not needed if the
 898                 * current skb can be reused */
 899                if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
 900                        skb_reserve(newskb, NET_IP_ALIGN);
 901
 902                        dma_addr = dma_map_single(greth->dev,
 903                                                      newskb->data,
 904                                                      MAX_FRAME_SIZE + NET_IP_ALIGN,
 905                                                      DMA_FROM_DEVICE);
 906
 907                        if (!dma_mapping_error(greth->dev, dma_addr)) {
 908                                /* Process the incoming frame. */
 909                                pkt_len = status & GRETH_BD_LEN;
 910
 911                                dma_unmap_single(greth->dev,
 912                                                 greth_read_bd(&bdp->addr),
 913                                                 MAX_FRAME_SIZE + NET_IP_ALIGN,
 914                                                 DMA_FROM_DEVICE);
 915
 916                                if (netif_msg_pktdata(greth))
 917                                        greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
 918
 919                                skb_put(skb, pkt_len);
 920
 921                                if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
 922                                        skb->ip_summed = CHECKSUM_UNNECESSARY;
 923                                else
 924                                        skb_checksum_none_assert(skb);
 925
 926                                skb->protocol = eth_type_trans(skb, dev);
 927                                dev->stats.rx_packets++;
 928                                dev->stats.rx_bytes += pkt_len;
 929                                netif_receive_skb(skb);
 930
 931                                greth->rx_skbuff[greth->rx_cur] = newskb;
 932                                greth_write_bd(&bdp->addr, dma_addr);
 933                        } else {
 934                                if (net_ratelimit())
 935                                        dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
 936                                dev_kfree_skb(newskb);
 937                                /* reusing current skb, so it is a drop */
 938                                dev->stats.rx_dropped++;
 939                        }
 940                } else if (bad) {
 941                        /* Bad Frame transfer, the skb is reused */
 942                        dev->stats.rx_dropped++;
 943                } else {
 944                        /* Failed Allocating a new skb. This is rather stupid
 945                         * but the current "filled" skb is reused, as if
 946                         * transfer failure. One could argue that RX descriptor
 947                         * table handling should be divided into cleaning and
 948                         * filling as the TX part of the driver
 949                         */
 950                        if (net_ratelimit())
 951                                dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
 952                        /* reusing current skb, so it is a drop */
 953                        dev->stats.rx_dropped++;
 954                }
 955
 956                status = GRETH_BD_EN | GRETH_BD_IE;
 957                if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
 958                        status |= GRETH_BD_WR;
 959                }
 960
 961                wmb();
 962                greth_write_bd(&bdp->stat, status);
 963                spin_lock_irqsave(&greth->devlock, flags);
 964                greth_enable_rx(greth);
 965                spin_unlock_irqrestore(&greth->devlock, flags);
 966                greth->rx_cur = NEXT_RX(greth->rx_cur);
 967        }
 968
 969        return count;
 970
 971}
 972
 973static int greth_poll(struct napi_struct *napi, int budget)
 974{
 975        struct greth_private *greth;
 976        int work_done = 0;
 977        unsigned long flags;
 978        u32 mask, ctrl;
 979        greth = container_of(napi, struct greth_private, napi);
 980
 981restart_txrx_poll:
 982        if (greth->gbit_mac) {
 983                greth_clean_tx_gbit(greth->netdev);
 984                work_done += greth_rx_gbit(greth->netdev, budget - work_done);
 985        } else {
 986                if (netif_queue_stopped(greth->netdev))
 987                        greth_clean_tx(greth->netdev);
 988                work_done += greth_rx(greth->netdev, budget - work_done);
 989        }
 990
 991        if (work_done < budget) {
 992
 993                spin_lock_irqsave(&greth->devlock, flags);
 994
 995                ctrl = GRETH_REGLOAD(greth->regs->control);
 996                if ((greth->gbit_mac && (greth->tx_last != greth->tx_next)) ||
 997                    (!greth->gbit_mac && netif_queue_stopped(greth->netdev))) {
 998                        GRETH_REGSAVE(greth->regs->control,
 999                                        ctrl | GRETH_TXI | GRETH_RXI);
1000                        mask = GRETH_INT_RX | GRETH_INT_RE |
1001                               GRETH_INT_TX | GRETH_INT_TE;
1002                } else {
1003                        GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
1004                        mask = GRETH_INT_RX | GRETH_INT_RE;
1005                }
1006
1007                if (GRETH_REGLOAD(greth->regs->status) & mask) {
1008                        GRETH_REGSAVE(greth->regs->control, ctrl);
1009                        spin_unlock_irqrestore(&greth->devlock, flags);
1010                        goto restart_txrx_poll;
1011                } else {
1012                        napi_complete_done(napi, work_done);
1013                        spin_unlock_irqrestore(&greth->devlock, flags);
1014                }
1015        }
1016
1017        return work_done;
1018}
1019
1020static int greth_set_mac_add(struct net_device *dev, void *p)
1021{
1022        struct sockaddr *addr = p;
1023        struct greth_private *greth;
1024        struct greth_regs *regs;
1025
1026        greth = netdev_priv(dev);
1027        regs = greth->regs;
1028
1029        if (!is_valid_ether_addr(addr->sa_data))
1030                return -EADDRNOTAVAIL;
1031
1032        memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1033        GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1034        GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1035                      dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1036
1037        return 0;
1038}
1039
1040static u32 greth_hash_get_index(__u8 *addr)
1041{
1042        return (ether_crc(6, addr)) & 0x3F;
1043}
1044
1045static void greth_set_hash_filter(struct net_device *dev)
1046{
1047        struct netdev_hw_addr *ha;
1048        struct greth_private *greth = netdev_priv(dev);
1049        struct greth_regs *regs = greth->regs;
1050        u32 mc_filter[2];
1051        unsigned int bitnr;
1052
1053        mc_filter[0] = mc_filter[1] = 0;
1054
1055        netdev_for_each_mc_addr(ha, dev) {
1056                bitnr = greth_hash_get_index(ha->addr);
1057                mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1058        }
1059
1060        GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1061        GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1062}
1063
1064static void greth_set_multicast_list(struct net_device *dev)
1065{
1066        int cfg;
1067        struct greth_private *greth = netdev_priv(dev);
1068        struct greth_regs *regs = greth->regs;
1069
1070        cfg = GRETH_REGLOAD(regs->control);
1071        if (dev->flags & IFF_PROMISC)
1072                cfg |= GRETH_CTRL_PR;
1073        else
1074                cfg &= ~GRETH_CTRL_PR;
1075
1076        if (greth->multicast) {
1077                if (dev->flags & IFF_ALLMULTI) {
1078                        GRETH_REGSAVE(regs->hash_msb, -1);
1079                        GRETH_REGSAVE(regs->hash_lsb, -1);
1080                        cfg |= GRETH_CTRL_MCEN;
1081                        GRETH_REGSAVE(regs->control, cfg);
1082                        return;
1083                }
1084
1085                if (netdev_mc_empty(dev)) {
1086                        cfg &= ~GRETH_CTRL_MCEN;
1087                        GRETH_REGSAVE(regs->control, cfg);
1088                        return;
1089                }
1090
1091                /* Setup multicast filter */
1092                greth_set_hash_filter(dev);
1093                cfg |= GRETH_CTRL_MCEN;
1094        }
1095        GRETH_REGSAVE(regs->control, cfg);
1096}
1097
1098static u32 greth_get_msglevel(struct net_device *dev)
1099{
1100        struct greth_private *greth = netdev_priv(dev);
1101        return greth->msg_enable;
1102}
1103
1104static void greth_set_msglevel(struct net_device *dev, u32 value)
1105{
1106        struct greth_private *greth = netdev_priv(dev);
1107        greth->msg_enable = value;
1108}
1109
1110static int greth_get_regs_len(struct net_device *dev)
1111{
1112        return sizeof(struct greth_regs);
1113}
1114
1115static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1116{
1117        struct greth_private *greth = netdev_priv(dev);
1118
1119        strlcpy(info->driver, dev_driver_string(greth->dev),
1120                sizeof(info->driver));
1121        strlcpy(info->version, "revision: 1.0", sizeof(info->version));
1122        strlcpy(info->bus_info, greth->dev->bus->name, sizeof(info->bus_info));
1123        strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1124}
1125
1126static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1127{
1128        int i;
1129        struct greth_private *greth = netdev_priv(dev);
1130        u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1131        u32 *buff = p;
1132
1133        for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1134                buff[i] = greth_read_bd(&greth_regs[i]);
1135}
1136
1137static const struct ethtool_ops greth_ethtool_ops = {
1138        .get_msglevel           = greth_get_msglevel,
1139        .set_msglevel           = greth_set_msglevel,
1140        .get_drvinfo            = greth_get_drvinfo,
1141        .get_regs_len           = greth_get_regs_len,
1142        .get_regs               = greth_get_regs,
1143        .get_link               = ethtool_op_get_link,
1144        .get_link_ksettings     = phy_ethtool_get_link_ksettings,
1145        .set_link_ksettings     = phy_ethtool_set_link_ksettings,
1146};
1147
1148static struct net_device_ops greth_netdev_ops = {
1149        .ndo_open               = greth_open,
1150        .ndo_stop               = greth_close,
1151        .ndo_start_xmit         = greth_start_xmit,
1152        .ndo_set_mac_address    = greth_set_mac_add,
1153        .ndo_validate_addr      = eth_validate_addr,
1154};
1155
1156static inline int wait_for_mdio(struct greth_private *greth)
1157{
1158        unsigned long timeout = jiffies + 4*HZ/100;
1159        while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1160                if (time_after(jiffies, timeout))
1161                        return 0;
1162        }
1163        return 1;
1164}
1165
1166static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1167{
1168        struct greth_private *greth = bus->priv;
1169        int data;
1170
1171        if (!wait_for_mdio(greth))
1172                return -EBUSY;
1173
1174        GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1175
1176        if (!wait_for_mdio(greth))
1177                return -EBUSY;
1178
1179        if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1180                data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1181                return data;
1182
1183        } else {
1184                return -1;
1185        }
1186}
1187
1188static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1189{
1190        struct greth_private *greth = bus->priv;
1191
1192        if (!wait_for_mdio(greth))
1193                return -EBUSY;
1194
1195        GRETH_REGSAVE(greth->regs->mdio,
1196                      ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1197
1198        if (!wait_for_mdio(greth))
1199                return -EBUSY;
1200
1201        return 0;
1202}
1203
1204static void greth_link_change(struct net_device *dev)
1205{
1206        struct greth_private *greth = netdev_priv(dev);
1207        struct phy_device *phydev = dev->phydev;
1208        unsigned long flags;
1209        int status_change = 0;
1210        u32 ctrl;
1211
1212        spin_lock_irqsave(&greth->devlock, flags);
1213
1214        if (phydev->link) {
1215
1216                if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1217                        ctrl = GRETH_REGLOAD(greth->regs->control) &
1218                               ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1219
1220                        if (phydev->duplex)
1221                                ctrl |= GRETH_CTRL_FD;
1222
1223                        if (phydev->speed == SPEED_100)
1224                                ctrl |= GRETH_CTRL_SP;
1225                        else if (phydev->speed == SPEED_1000)
1226                                ctrl |= GRETH_CTRL_GB;
1227
1228                        GRETH_REGSAVE(greth->regs->control, ctrl);
1229                        greth->speed = phydev->speed;
1230                        greth->duplex = phydev->duplex;
1231                        status_change = 1;
1232                }
1233        }
1234
1235        if (phydev->link != greth->link) {
1236                if (!phydev->link) {
1237                        greth->speed = 0;
1238                        greth->duplex = -1;
1239                }
1240                greth->link = phydev->link;
1241
1242                status_change = 1;
1243        }
1244
1245        spin_unlock_irqrestore(&greth->devlock, flags);
1246
1247        if (status_change) {
1248                if (phydev->link)
1249                        pr_debug("%s: link up (%d/%s)\n",
1250                                dev->name, phydev->speed,
1251                                DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1252                else
1253                        pr_debug("%s: link down\n", dev->name);
1254        }
1255}
1256
1257static int greth_mdio_probe(struct net_device *dev)
1258{
1259        struct greth_private *greth = netdev_priv(dev);
1260        struct phy_device *phy = NULL;
1261        int ret;
1262
1263        /* Find the first PHY */
1264        phy = phy_find_first(greth->mdio);
1265
1266        if (!phy) {
1267                if (netif_msg_probe(greth))
1268                        dev_err(&dev->dev, "no PHY found\n");
1269                return -ENXIO;
1270        }
1271
1272        ret = phy_connect_direct(dev, phy, &greth_link_change,
1273                                 greth->gbit_mac ? PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII);
1274        if (ret) {
1275                if (netif_msg_ifup(greth))
1276                        dev_err(&dev->dev, "could not attach to PHY\n");
1277                return ret;
1278        }
1279
1280        if (greth->gbit_mac)
1281                phy_set_max_speed(phy, SPEED_1000);
1282        else
1283                phy_set_max_speed(phy, SPEED_100);
1284
1285        linkmode_copy(phy->advertising, phy->supported);
1286
1287        greth->link = 0;
1288        greth->speed = 0;
1289        greth->duplex = -1;
1290
1291        return 0;
1292}
1293
1294static int greth_mdio_init(struct greth_private *greth)
1295{
1296        int ret;
1297        unsigned long timeout;
1298        struct net_device *ndev = greth->netdev;
1299
1300        greth->mdio = mdiobus_alloc();
1301        if (!greth->mdio) {
1302                return -ENOMEM;
1303        }
1304
1305        greth->mdio->name = "greth-mdio";
1306        snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1307        greth->mdio->read = greth_mdio_read;
1308        greth->mdio->write = greth_mdio_write;
1309        greth->mdio->priv = greth;
1310
1311        ret = mdiobus_register(greth->mdio);
1312        if (ret) {
1313                goto error;
1314        }
1315
1316        ret = greth_mdio_probe(greth->netdev);
1317        if (ret) {
1318                if (netif_msg_probe(greth))
1319                        dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1320                goto unreg_mdio;
1321        }
1322
1323        phy_start(ndev->phydev);
1324
1325        /* If Ethernet debug link is used make autoneg happen right away */
1326        if (greth->edcl && greth_edcl == 1) {
1327                phy_start_aneg(ndev->phydev);
1328                timeout = jiffies + 6*HZ;
1329                while (!phy_aneg_done(ndev->phydev) &&
1330                       time_before(jiffies, timeout)) {
1331                }
1332                phy_read_status(ndev->phydev);
1333                greth_link_change(greth->netdev);
1334        }
1335
1336        return 0;
1337
1338unreg_mdio:
1339        mdiobus_unregister(greth->mdio);
1340error:
1341        mdiobus_free(greth->mdio);
1342        return ret;
1343}
1344
1345/* Initialize the GRETH MAC */
1346static int greth_of_probe(struct platform_device *ofdev)
1347{
1348        struct net_device *dev;
1349        struct greth_private *greth;
1350        struct greth_regs *regs;
1351
1352        int i;
1353        int err;
1354        int tmp;
1355        unsigned long timeout;
1356
1357        dev = alloc_etherdev(sizeof(struct greth_private));
1358
1359        if (dev == NULL)
1360                return -ENOMEM;
1361
1362        greth = netdev_priv(dev);
1363        greth->netdev = dev;
1364        greth->dev = &ofdev->dev;
1365
1366        if (greth_debug > 0)
1367                greth->msg_enable = greth_debug;
1368        else
1369                greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1370
1371        spin_lock_init(&greth->devlock);
1372
1373        greth->regs = of_ioremap(&ofdev->resource[0], 0,
1374                                 resource_size(&ofdev->resource[0]),
1375                                 "grlib-greth regs");
1376
1377        if (greth->regs == NULL) {
1378                if (netif_msg_probe(greth))
1379                        dev_err(greth->dev, "ioremap failure.\n");
1380                err = -EIO;
1381                goto error1;
1382        }
1383
1384        regs = greth->regs;
1385        greth->irq = ofdev->archdata.irqs[0];
1386
1387        dev_set_drvdata(greth->dev, dev);
1388        SET_NETDEV_DEV(dev, greth->dev);
1389
1390        if (netif_msg_probe(greth))
1391                dev_dbg(greth->dev, "resetting controller.\n");
1392
1393        /* Reset the controller. */
1394        GRETH_REGSAVE(regs->control, GRETH_RESET);
1395
1396        /* Wait for MAC to reset itself */
1397        timeout = jiffies + HZ/100;
1398        while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1399                if (time_after(jiffies, timeout)) {
1400                        err = -EIO;
1401                        if (netif_msg_probe(greth))
1402                                dev_err(greth->dev, "timeout when waiting for reset.\n");
1403                        goto error2;
1404                }
1405        }
1406
1407        /* Get default PHY address  */
1408        greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1409
1410        /* Check if we have GBIT capable MAC */
1411        tmp = GRETH_REGLOAD(regs->control);
1412        greth->gbit_mac = (tmp >> 27) & 1;
1413
1414        /* Check for multicast capability */
1415        greth->multicast = (tmp >> 25) & 1;
1416
1417        greth->edcl = (tmp >> 31) & 1;
1418
1419        /* If we have EDCL we disable the EDCL speed-duplex FSM so
1420         * it doesn't interfere with the software */
1421        if (greth->edcl != 0)
1422                GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1423
1424        /* Check if MAC can handle MDIO interrupts */
1425        greth->mdio_int_en = (tmp >> 26) & 1;
1426
1427        err = greth_mdio_init(greth);
1428        if (err) {
1429                if (netif_msg_probe(greth))
1430                        dev_err(greth->dev, "failed to register MDIO bus\n");
1431                goto error2;
1432        }
1433
1434        /* Allocate TX descriptor ring in coherent memory */
1435        greth->tx_bd_base = dma_zalloc_coherent(greth->dev, 1024,
1436                                                &greth->tx_bd_base_phys,
1437                                                GFP_KERNEL);
1438        if (!greth->tx_bd_base) {
1439                err = -ENOMEM;
1440                goto error3;
1441        }
1442
1443        /* Allocate RX descriptor ring in coherent memory */
1444        greth->rx_bd_base = dma_zalloc_coherent(greth->dev, 1024,
1445                                                &greth->rx_bd_base_phys,
1446                                                GFP_KERNEL);
1447        if (!greth->rx_bd_base) {
1448                err = -ENOMEM;
1449                goto error4;
1450        }
1451
1452        /* Get MAC address from: module param, OF property or ID prom */
1453        for (i = 0; i < 6; i++) {
1454                if (macaddr[i] != 0)
1455                        break;
1456        }
1457        if (i == 6) {
1458                u8 addr[ETH_ALEN];
1459
1460                err = of_get_mac_address(ofdev->dev.of_node, addr);
1461                if (!err) {
1462                        for (i = 0; i < 6; i++)
1463                                macaddr[i] = (unsigned int) addr[i];
1464                } else {
1465#ifdef CONFIG_SPARC
1466                        for (i = 0; i < 6; i++)
1467                                macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1468#endif
1469                }
1470        }
1471
1472        for (i = 0; i < 6; i++)
1473                dev->dev_addr[i] = macaddr[i];
1474
1475        macaddr[5]++;
1476
1477        if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1478                if (netif_msg_probe(greth))
1479                        dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1480                err = -EINVAL;
1481                goto error5;
1482        }
1483
1484        GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1485        GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1486                      dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1487
1488        /* Clear all pending interrupts except PHY irq */
1489        GRETH_REGSAVE(regs->status, 0xFF);
1490
1491        if (greth->gbit_mac) {
1492                dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1493                        NETIF_F_RXCSUM;
1494                dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1495                greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1496        }
1497
1498        if (greth->multicast) {
1499                greth_netdev_ops.ndo_set_rx_mode = greth_set_multicast_list;
1500                dev->flags |= IFF_MULTICAST;
1501        } else {
1502                dev->flags &= ~IFF_MULTICAST;
1503        }
1504
1505        dev->netdev_ops = &greth_netdev_ops;
1506        dev->ethtool_ops = &greth_ethtool_ops;
1507
1508        err = register_netdev(dev);
1509        if (err) {
1510                if (netif_msg_probe(greth))
1511                        dev_err(greth->dev, "netdevice registration failed.\n");
1512                goto error5;
1513        }
1514
1515        /* setup NAPI */
1516        netif_napi_add(dev, &greth->napi, greth_poll, 64);
1517
1518        return 0;
1519
1520error5:
1521        dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1522error4:
1523        dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1524error3:
1525        mdiobus_unregister(greth->mdio);
1526error2:
1527        of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1528error1:
1529        free_netdev(dev);
1530        return err;
1531}
1532
1533static int greth_of_remove(struct platform_device *of_dev)
1534{
1535        struct net_device *ndev = platform_get_drvdata(of_dev);
1536        struct greth_private *greth = netdev_priv(ndev);
1537
1538        /* Free descriptor areas */
1539        dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1540
1541        dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1542
1543        if (ndev->phydev)
1544                phy_stop(ndev->phydev);
1545        mdiobus_unregister(greth->mdio);
1546
1547        unregister_netdev(ndev);
1548        free_netdev(ndev);
1549
1550        of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1551
1552        return 0;
1553}
1554
1555static const struct of_device_id greth_of_match[] = {
1556        {
1557         .name = "GAISLER_ETHMAC",
1558         },
1559        {
1560         .name = "01_01d",
1561         },
1562        {},
1563};
1564
1565MODULE_DEVICE_TABLE(of, greth_of_match);
1566
1567static struct platform_driver greth_of_driver = {
1568        .driver = {
1569                .name = "grlib-greth",
1570                .of_match_table = greth_of_match,
1571        },
1572        .probe = greth_of_probe,
1573        .remove = greth_of_remove,
1574};
1575
1576module_platform_driver(greth_of_driver);
1577
1578MODULE_AUTHOR("Aeroflex Gaisler AB.");
1579MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1580MODULE_LICENSE("GPL");
1581