linux/drivers/net/ethernet/8390/ax88796.c
<<
>>
Prefs
   1/* drivers/net/ethernet/8390/ax88796.c
   2 *
   3 * Copyright 2005,2007 Simtec Electronics
   4 *      Ben Dooks <ben@simtec.co.uk>
   5 *
   6 * Asix AX88796 10/100 Ethernet controller support
   7 *      Based on ne.c, by Donald Becker, et-al.
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License version 2 as
  11 * published by the Free Software Foundation.
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/kernel.h>
  16#include <linux/errno.h>
  17#include <linux/isapnp.h>
  18#include <linux/interrupt.h>
  19#include <linux/io.h>
  20#include <linux/platform_device.h>
  21#include <linux/delay.h>
  22#include <linux/timer.h>
  23#include <linux/netdevice.h>
  24#include <linux/etherdevice.h>
  25#include <linux/ethtool.h>
  26#include <linux/mdio-bitbang.h>
  27#include <linux/phy.h>
  28#include <linux/eeprom_93cx6.h>
  29#include <linux/slab.h>
  30
  31#include <net/ax88796.h>
  32
  33
  34/* Rename the lib8390.c functions to show that they are in this driver */
  35#define __ei_open ax_ei_open
  36#define __ei_close ax_ei_close
  37#define __ei_poll ax_ei_poll
  38#define __ei_start_xmit ax_ei_start_xmit
  39#define __ei_tx_timeout ax_ei_tx_timeout
  40#define __ei_get_stats ax_ei_get_stats
  41#define __ei_set_multicast_list ax_ei_set_multicast_list
  42#define __ei_interrupt ax_ei_interrupt
  43#define ____alloc_ei_netdev ax__alloc_ei_netdev
  44#define __NS8390_init ax_NS8390_init
  45
  46/* force unsigned long back to 'void __iomem *' */
  47#define ax_convert_addr(_a) ((void __force __iomem *)(_a))
  48
  49#define ei_inb(_a) readb(ax_convert_addr(_a))
  50#define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
  51
  52#define ei_inb_p(_a) ei_inb(_a)
  53#define ei_outb_p(_v, _a) ei_outb(_v, _a)
  54
  55/* define EI_SHIFT() to take into account our register offsets */
  56#define EI_SHIFT(x) (ei_local->reg_offset[(x)])
  57
  58/* Ensure we have our RCR base value */
  59#define AX88796_PLATFORM
  60
  61static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
  62
  63#include "lib8390.c"
  64
  65#define DRV_NAME "ax88796"
  66#define DRV_VERSION "1.00"
  67
  68/* from ne.c */
  69#define NE_CMD          EI_SHIFT(0x00)
  70#define NE_RESET        EI_SHIFT(0x1f)
  71#define NE_DATAPORT     EI_SHIFT(0x10)
  72
  73#define NE1SM_START_PG  0x20    /* First page of TX buffer */
  74#define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
  75#define NESM_START_PG   0x40    /* First page of TX buffer */
  76#define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
  77
  78#define AX_GPOC_PPDSET  BIT(6)
  79
  80/* device private data */
  81
  82struct ax_device {
  83        struct mii_bus *mii_bus;
  84        struct mdiobb_ctrl bb_ctrl;
  85        void __iomem *addr_memr;
  86        u8 reg_memr;
  87        int link;
  88        int speed;
  89        int duplex;
  90
  91        void __iomem *map2;
  92        const struct ax_plat_data *plat;
  93
  94        unsigned char running;
  95        unsigned char resume_open;
  96        unsigned int irqflags;
  97
  98        u32 reg_offsets[0x20];
  99};
 100
 101static inline struct ax_device *to_ax_dev(struct net_device *dev)
 102{
 103        struct ei_device *ei_local = netdev_priv(dev);
 104        return (struct ax_device *)(ei_local + 1);
 105}
 106
 107/*
 108 * ax_initial_check
 109 *
 110 * do an initial probe for the card to check whether it exists
 111 * and is functional
 112 */
 113static int ax_initial_check(struct net_device *dev)
 114{
 115        struct ei_device *ei_local = netdev_priv(dev);
 116        void __iomem *ioaddr = ei_local->mem;
 117        int reg0;
 118        int regd;
 119
 120        reg0 = ei_inb(ioaddr);
 121        if (reg0 == 0xFF)
 122                return -ENODEV;
 123
 124        ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
 125        regd = ei_inb(ioaddr + 0x0d);
 126        ei_outb(0xff, ioaddr + 0x0d);
 127        ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
 128        ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
 129        if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
 130                ei_outb(reg0, ioaddr);
 131                ei_outb(regd, ioaddr + 0x0d);   /* Restore the old values. */
 132                return -ENODEV;
 133        }
 134
 135        return 0;
 136}
 137
 138/*
 139 * Hard reset the card. This used to pause for the same period that a
 140 * 8390 reset command required, but that shouldn't be necessary.
 141 */
 142static void ax_reset_8390(struct net_device *dev)
 143{
 144        struct ei_device *ei_local = netdev_priv(dev);
 145        unsigned long reset_start_time = jiffies;
 146        void __iomem *addr = (void __iomem *)dev->base_addr;
 147
 148        netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
 149
 150        ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
 151
 152        ei_local->txing = 0;
 153        ei_local->dmaing = 0;
 154
 155        /* This check _should_not_ be necessary, omit eventually. */
 156        while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
 157                if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
 158                        netdev_warn(dev, "%s: did not complete.\n", __func__);
 159                        break;
 160                }
 161        }
 162
 163        ei_outb(ENISR_RESET, addr + EN0_ISR);   /* Ack intr. */
 164}
 165
 166/* Wrapper for __ei_interrupt for platforms that have a platform-specific
 167 * way to find out whether the interrupt request might be caused by
 168 * the ax88796 chip.
 169 */
 170static irqreturn_t ax_ei_interrupt_filtered(int irq, void *dev_id)
 171{
 172        struct net_device *dev = dev_id;
 173        struct ax_device *ax = to_ax_dev(dev);
 174        struct platform_device *pdev = to_platform_device(dev->dev.parent);
 175
 176        if (!ax->plat->check_irq(pdev))
 177                return IRQ_NONE;
 178
 179        return ax_ei_interrupt(irq, dev_id);
 180}
 181
 182static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
 183                            int ring_page)
 184{
 185        struct ei_device *ei_local = netdev_priv(dev);
 186        void __iomem *nic_base = ei_local->mem;
 187
 188        /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 189        if (ei_local->dmaing) {
 190                netdev_err(dev, "DMAing conflict in %s "
 191                        "[DMAstat:%d][irqlock:%d].\n",
 192                        __func__,
 193                        ei_local->dmaing, ei_local->irqlock);
 194                return;
 195        }
 196
 197        ei_local->dmaing |= 0x01;
 198        ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
 199        ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
 200        ei_outb(0, nic_base + EN0_RCNTHI);
 201        ei_outb(0, nic_base + EN0_RSARLO);              /* On page boundary */
 202        ei_outb(ring_page, nic_base + EN0_RSARHI);
 203        ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 204
 205        if (ei_local->word16)
 206                ioread16_rep(nic_base + NE_DATAPORT, hdr,
 207                             sizeof(struct e8390_pkt_hdr) >> 1);
 208        else
 209                ioread8_rep(nic_base + NE_DATAPORT, hdr,
 210                            sizeof(struct e8390_pkt_hdr));
 211
 212        ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
 213        ei_local->dmaing &= ~0x01;
 214
 215        le16_to_cpus(&hdr->count);
 216}
 217
 218
 219/*
 220 * Block input and output, similar to the Crynwr packet driver. If
 221 * you are porting to a new ethercard, look at the packet driver
 222 * source for hints. The NEx000 doesn't share the on-board packet
 223 * memory -- you have to put the packet out through the "remote DMA"
 224 * dataport using ei_outb.
 225 */
 226static void ax_block_input(struct net_device *dev, int count,
 227                           struct sk_buff *skb, int ring_offset)
 228{
 229        struct ei_device *ei_local = netdev_priv(dev);
 230        void __iomem *nic_base = ei_local->mem;
 231        char *buf = skb->data;
 232
 233        if (ei_local->dmaing) {
 234                netdev_err(dev,
 235                        "DMAing conflict in %s "
 236                        "[DMAstat:%d][irqlock:%d].\n",
 237                        __func__,
 238                        ei_local->dmaing, ei_local->irqlock);
 239                return;
 240        }
 241
 242        ei_local->dmaing |= 0x01;
 243
 244        ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
 245        ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
 246        ei_outb(count >> 8, nic_base + EN0_RCNTHI);
 247        ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
 248        ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
 249        ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 250
 251        if (ei_local->word16) {
 252                ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
 253                if (count & 0x01)
 254                        buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
 255
 256        } else {
 257                ioread8_rep(nic_base + NE_DATAPORT, buf, count);
 258        }
 259
 260        ei_local->dmaing &= ~1;
 261}
 262
 263static void ax_block_output(struct net_device *dev, int count,
 264                            const unsigned char *buf, const int start_page)
 265{
 266        struct ei_device *ei_local = netdev_priv(dev);
 267        void __iomem *nic_base = ei_local->mem;
 268        unsigned long dma_start;
 269
 270        /*
 271         * Round the count up for word writes. Do we need to do this?
 272         * What effect will an odd byte count have on the 8390?  I
 273         * should check someday.
 274         */
 275        if (ei_local->word16 && (count & 0x01))
 276                count++;
 277
 278        /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 279        if (ei_local->dmaing) {
 280                netdev_err(dev, "DMAing conflict in %s."
 281                        "[DMAstat:%d][irqlock:%d]\n",
 282                        __func__,
 283                       ei_local->dmaing, ei_local->irqlock);
 284                return;
 285        }
 286
 287        ei_local->dmaing |= 0x01;
 288        /* We should already be in page 0, but to be safe... */
 289        ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
 290
 291        ei_outb(ENISR_RDC, nic_base + EN0_ISR);
 292
 293        /* Now the normal output. */
 294        ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
 295        ei_outb(count >> 8, nic_base + EN0_RCNTHI);
 296        ei_outb(0x00, nic_base + EN0_RSARLO);
 297        ei_outb(start_page, nic_base + EN0_RSARHI);
 298
 299        ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
 300        if (ei_local->word16)
 301                iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
 302        else
 303                iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
 304
 305        dma_start = jiffies;
 306
 307        while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
 308                if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
 309                        netdev_warn(dev, "timeout waiting for Tx RDC.\n");
 310                        ax_reset_8390(dev);
 311                        ax_NS8390_init(dev, 1);
 312                        break;
 313                }
 314        }
 315
 316        ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
 317        ei_local->dmaing &= ~0x01;
 318}
 319
 320/* definitions for accessing MII/EEPROM interface */
 321
 322#define AX_MEMR                 EI_SHIFT(0x14)
 323#define AX_MEMR_MDC             BIT(0)
 324#define AX_MEMR_MDIR            BIT(1)
 325#define AX_MEMR_MDI             BIT(2)
 326#define AX_MEMR_MDO             BIT(3)
 327#define AX_MEMR_EECS            BIT(4)
 328#define AX_MEMR_EEI             BIT(5)
 329#define AX_MEMR_EEO             BIT(6)
 330#define AX_MEMR_EECLK           BIT(7)
 331
 332static void ax_handle_link_change(struct net_device *dev)
 333{
 334        struct ax_device  *ax = to_ax_dev(dev);
 335        struct phy_device *phy_dev = dev->phydev;
 336        int status_change = 0;
 337
 338        if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
 339                             (ax->duplex != phy_dev->duplex))) {
 340
 341                ax->speed = phy_dev->speed;
 342                ax->duplex = phy_dev->duplex;
 343                status_change = 1;
 344        }
 345
 346        if (phy_dev->link != ax->link) {
 347                if (!phy_dev->link) {
 348                        ax->speed = 0;
 349                        ax->duplex = -1;
 350                }
 351                ax->link = phy_dev->link;
 352
 353                status_change = 1;
 354        }
 355
 356        if (status_change)
 357                phy_print_status(phy_dev);
 358}
 359
 360static int ax_mii_probe(struct net_device *dev)
 361{
 362        struct ax_device  *ax = to_ax_dev(dev);
 363        struct phy_device *phy_dev = NULL;
 364        int ret;
 365
 366        /* find the first phy */
 367        phy_dev = phy_find_first(ax->mii_bus);
 368        if (!phy_dev) {
 369                netdev_err(dev, "no PHY found\n");
 370                return -ENODEV;
 371        }
 372
 373        ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
 374                                 PHY_INTERFACE_MODE_MII);
 375        if (ret) {
 376                netdev_err(dev, "Could not attach to PHY\n");
 377                return ret;
 378        }
 379
 380        phy_set_max_speed(phy_dev, SPEED_100);
 381
 382        netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
 383                    phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
 384
 385        return 0;
 386}
 387
 388static void ax_phy_switch(struct net_device *dev, int on)
 389{
 390        struct ei_device *ei_local = netdev_priv(dev);
 391        struct ax_device *ax = to_ax_dev(dev);
 392
 393        u8 reg_gpoc =  ax->plat->gpoc_val;
 394
 395        if (!!on)
 396                reg_gpoc &= ~AX_GPOC_PPDSET;
 397        else
 398                reg_gpoc |= AX_GPOC_PPDSET;
 399
 400        ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
 401}
 402
 403static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
 404{
 405        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 406
 407        if (level)
 408                ax->reg_memr |= AX_MEMR_MDC;
 409        else
 410                ax->reg_memr &= ~AX_MEMR_MDC;
 411
 412        ei_outb(ax->reg_memr, ax->addr_memr);
 413}
 414
 415static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
 416{
 417        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 418
 419        if (output)
 420                ax->reg_memr &= ~AX_MEMR_MDIR;
 421        else
 422                ax->reg_memr |= AX_MEMR_MDIR;
 423
 424        ei_outb(ax->reg_memr, ax->addr_memr);
 425}
 426
 427static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
 428{
 429        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 430
 431        if (value)
 432                ax->reg_memr |= AX_MEMR_MDO;
 433        else
 434                ax->reg_memr &= ~AX_MEMR_MDO;
 435
 436        ei_outb(ax->reg_memr, ax->addr_memr);
 437}
 438
 439static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
 440{
 441        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 442        int reg_memr = ei_inb(ax->addr_memr);
 443
 444        return reg_memr & AX_MEMR_MDI ? 1 : 0;
 445}
 446
 447static const struct mdiobb_ops bb_ops = {
 448        .owner = THIS_MODULE,
 449        .set_mdc = ax_bb_mdc,
 450        .set_mdio_dir = ax_bb_dir,
 451        .set_mdio_data = ax_bb_set_data,
 452        .get_mdio_data = ax_bb_get_data,
 453};
 454
 455static int ax_mii_init(struct net_device *dev)
 456{
 457        struct platform_device *pdev = to_platform_device(dev->dev.parent);
 458        struct ei_device *ei_local = netdev_priv(dev);
 459        struct ax_device *ax = to_ax_dev(dev);
 460        int err;
 461
 462        ax->bb_ctrl.ops = &bb_ops;
 463        ax->addr_memr = ei_local->mem + AX_MEMR;
 464        ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
 465        if (!ax->mii_bus) {
 466                err = -ENOMEM;
 467                goto out;
 468        }
 469
 470        ax->mii_bus->name = "ax88796_mii_bus";
 471        ax->mii_bus->parent = dev->dev.parent;
 472        snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
 473                 pdev->name, pdev->id);
 474
 475        err = mdiobus_register(ax->mii_bus);
 476        if (err)
 477                goto out_free_mdio_bitbang;
 478
 479        return 0;
 480
 481 out_free_mdio_bitbang:
 482        free_mdio_bitbang(ax->mii_bus);
 483 out:
 484        return err;
 485}
 486
 487static int ax_open(struct net_device *dev)
 488{
 489        struct ax_device *ax = to_ax_dev(dev);
 490        int ret;
 491
 492        netdev_dbg(dev, "open\n");
 493
 494        ret = ax_mii_init(dev);
 495        if (ret)
 496                goto failed_mii;
 497
 498        if (ax->plat->check_irq)
 499                ret = request_irq(dev->irq, ax_ei_interrupt_filtered,
 500                                  ax->irqflags, dev->name, dev);
 501        else
 502                ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
 503                                  dev->name, dev);
 504        if (ret)
 505                goto failed_request_irq;
 506
 507        /* turn the phy on (if turned off) */
 508        ax_phy_switch(dev, 1);
 509
 510        ret = ax_mii_probe(dev);
 511        if (ret)
 512                goto failed_mii_probe;
 513        phy_start(dev->phydev);
 514
 515        ret = ax_ei_open(dev);
 516        if (ret)
 517                goto failed_ax_ei_open;
 518
 519        ax->running = 1;
 520
 521        return 0;
 522
 523 failed_ax_ei_open:
 524        phy_disconnect(dev->phydev);
 525 failed_mii_probe:
 526        ax_phy_switch(dev, 0);
 527        free_irq(dev->irq, dev);
 528 failed_request_irq:
 529        /* unregister mdiobus */
 530        mdiobus_unregister(ax->mii_bus);
 531        free_mdio_bitbang(ax->mii_bus);
 532 failed_mii:
 533        return ret;
 534}
 535
 536static int ax_close(struct net_device *dev)
 537{
 538        struct ax_device *ax = to_ax_dev(dev);
 539
 540        netdev_dbg(dev, "close\n");
 541
 542        ax->running = 0;
 543        wmb();
 544
 545        ax_ei_close(dev);
 546
 547        /* turn the phy off */
 548        ax_phy_switch(dev, 0);
 549        phy_disconnect(dev->phydev);
 550
 551        free_irq(dev->irq, dev);
 552
 553        mdiobus_unregister(ax->mii_bus);
 554        free_mdio_bitbang(ax->mii_bus);
 555        return 0;
 556}
 557
 558static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
 559{
 560        struct phy_device *phy_dev = dev->phydev;
 561
 562        if (!netif_running(dev))
 563                return -EINVAL;
 564
 565        if (!phy_dev)
 566                return -ENODEV;
 567
 568        return phy_mii_ioctl(phy_dev, req, cmd);
 569}
 570
 571/* ethtool ops */
 572
 573static void ax_get_drvinfo(struct net_device *dev,
 574                           struct ethtool_drvinfo *info)
 575{
 576        struct platform_device *pdev = to_platform_device(dev->dev.parent);
 577
 578        strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
 579        strlcpy(info->version, DRV_VERSION, sizeof(info->version));
 580        strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
 581}
 582
 583static u32 ax_get_msglevel(struct net_device *dev)
 584{
 585        struct ei_device *ei_local = netdev_priv(dev);
 586
 587        return ei_local->msg_enable;
 588}
 589
 590static void ax_set_msglevel(struct net_device *dev, u32 v)
 591{
 592        struct ei_device *ei_local = netdev_priv(dev);
 593
 594        ei_local->msg_enable = v;
 595}
 596
 597static const struct ethtool_ops ax_ethtool_ops = {
 598        .get_drvinfo            = ax_get_drvinfo,
 599        .get_link               = ethtool_op_get_link,
 600        .get_ts_info            = ethtool_op_get_ts_info,
 601        .get_msglevel           = ax_get_msglevel,
 602        .set_msglevel           = ax_set_msglevel,
 603        .get_link_ksettings     = phy_ethtool_get_link_ksettings,
 604        .set_link_ksettings     = phy_ethtool_set_link_ksettings,
 605};
 606
 607#ifdef CONFIG_AX88796_93CX6
 608static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
 609{
 610        struct ei_device *ei_local = eeprom->data;
 611        u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 612
 613        eeprom->reg_data_in = reg & AX_MEMR_EEI;
 614        eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
 615        eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
 616        eeprom->reg_chip_select = reg & AX_MEMR_EECS;
 617}
 618
 619static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
 620{
 621        struct ei_device *ei_local = eeprom->data;
 622        u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 623
 624        reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
 625
 626        if (eeprom->reg_data_in)
 627                reg |= AX_MEMR_EEI;
 628        if (eeprom->reg_data_clock)
 629                reg |= AX_MEMR_EECLK;
 630        if (eeprom->reg_chip_select)
 631                reg |= AX_MEMR_EECS;
 632
 633        ei_outb(reg, ei_local->mem + AX_MEMR);
 634        udelay(10);
 635}
 636#endif
 637
 638static const struct net_device_ops ax_netdev_ops = {
 639        .ndo_open               = ax_open,
 640        .ndo_stop               = ax_close,
 641        .ndo_do_ioctl           = ax_ioctl,
 642
 643        .ndo_start_xmit         = ax_ei_start_xmit,
 644        .ndo_tx_timeout         = ax_ei_tx_timeout,
 645        .ndo_get_stats          = ax_ei_get_stats,
 646        .ndo_set_rx_mode        = ax_ei_set_multicast_list,
 647        .ndo_validate_addr      = eth_validate_addr,
 648        .ndo_set_mac_address    = eth_mac_addr,
 649#ifdef CONFIG_NET_POLL_CONTROLLER
 650        .ndo_poll_controller    = ax_ei_poll,
 651#endif
 652};
 653
 654/* setup code */
 655
 656static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
 657{
 658        void __iomem *ioaddr = ei_local->mem;
 659        struct ax_device *ax = to_ax_dev(dev);
 660
 661        /* Select page 0 */
 662        ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
 663
 664        /* set to byte access */
 665        ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
 666        ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
 667}
 668
 669/*
 670 * ax_init_dev
 671 *
 672 * initialise the specified device, taking care to note the MAC
 673 * address it may already have (if configured), ensure
 674 * the device is ready to be used by lib8390.c and registerd with
 675 * the network layer.
 676 */
 677static int ax_init_dev(struct net_device *dev)
 678{
 679        struct ei_device *ei_local = netdev_priv(dev);
 680        struct ax_device *ax = to_ax_dev(dev);
 681        void __iomem *ioaddr = ei_local->mem;
 682        unsigned int start_page;
 683        unsigned int stop_page;
 684        int ret;
 685        int i;
 686
 687        ret = ax_initial_check(dev);
 688        if (ret)
 689                goto err_out;
 690
 691        /* setup goes here */
 692
 693        ax_initial_setup(dev, ei_local);
 694
 695        /* read the mac from the card prom if we need it */
 696
 697        if (ax->plat->flags & AXFLG_HAS_EEPROM) {
 698                unsigned char SA_prom[32];
 699
 700                ei_outb(6, ioaddr + EN0_RCNTLO);
 701                ei_outb(0, ioaddr + EN0_RCNTHI);
 702                ei_outb(0, ioaddr + EN0_RSARLO);
 703                ei_outb(0, ioaddr + EN0_RSARHI);
 704                ei_outb(E8390_RREAD + E8390_START, ioaddr + NE_CMD);
 705                for (i = 0; i < sizeof(SA_prom); i += 2) {
 706                        SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
 707                        SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
 708                }
 709                ei_outb(ENISR_RDC, ioaddr + EN0_ISR);   /* Ack intr. */
 710
 711                if (ax->plat->wordlength == 2)
 712                        for (i = 0; i < 16; i++)
 713                                SA_prom[i] = SA_prom[i+i];
 714
 715                memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
 716        }
 717
 718#ifdef CONFIG_AX88796_93CX6
 719        if (ax->plat->flags & AXFLG_HAS_93CX6) {
 720                unsigned char mac_addr[ETH_ALEN];
 721                struct eeprom_93cx6 eeprom;
 722
 723                eeprom.data = ei_local;
 724                eeprom.register_read = ax_eeprom_register_read;
 725                eeprom.register_write = ax_eeprom_register_write;
 726                eeprom.width = PCI_EEPROM_WIDTH_93C56;
 727
 728                eeprom_93cx6_multiread(&eeprom, 0,
 729                                       (__le16 __force *)mac_addr,
 730                                       sizeof(mac_addr) >> 1);
 731
 732                memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
 733        }
 734#endif
 735        if (ax->plat->wordlength == 2) {
 736                /* We must set the 8390 for word mode. */
 737                ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
 738                start_page = NESM_START_PG;
 739                stop_page = NESM_STOP_PG;
 740        } else {
 741                start_page = NE1SM_START_PG;
 742                stop_page = NE1SM_STOP_PG;
 743        }
 744
 745        /* load the mac-address from the device */
 746        if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
 747                ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
 748                        ei_local->mem + E8390_CMD); /* 0x61 */
 749                for (i = 0; i < ETH_ALEN; i++)
 750                        dev->dev_addr[i] =
 751                                ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
 752        }
 753
 754        if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
 755            ax->plat->mac_addr)
 756                memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
 757
 758        if (!is_valid_ether_addr(dev->dev_addr)) {
 759                eth_hw_addr_random(dev);
 760                dev_info(&dev->dev, "Using random MAC address: %pM\n",
 761                         dev->dev_addr);
 762        }
 763
 764        ax_reset_8390(dev);
 765
 766        ei_local->name = "AX88796";
 767        ei_local->tx_start_page = start_page;
 768        ei_local->stop_page = stop_page;
 769        ei_local->word16 = (ax->plat->wordlength == 2);
 770        ei_local->rx_start_page = start_page + TX_PAGES;
 771
 772#ifdef PACKETBUF_MEMSIZE
 773        /* Allow the packet buffer size to be overridden by know-it-alls. */
 774        ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
 775#endif
 776
 777        ei_local->reset_8390 = &ax_reset_8390;
 778        if (ax->plat->block_input)
 779                ei_local->block_input = ax->plat->block_input;
 780        else
 781                ei_local->block_input = &ax_block_input;
 782        if (ax->plat->block_output)
 783                ei_local->block_output = ax->plat->block_output;
 784        else
 785                ei_local->block_output = &ax_block_output;
 786        ei_local->get_8390_hdr = &ax_get_8390_hdr;
 787        ei_local->priv = 0;
 788
 789        dev->netdev_ops = &ax_netdev_ops;
 790        dev->ethtool_ops = &ax_ethtool_ops;
 791
 792        ax_NS8390_init(dev, 0);
 793
 794        ret = register_netdev(dev);
 795        if (ret)
 796                goto err_out;
 797
 798        netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
 799                    ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
 800                    dev->dev_addr);
 801
 802        return 0;
 803
 804 err_out:
 805        return ret;
 806}
 807
 808static int ax_remove(struct platform_device *pdev)
 809{
 810        struct net_device *dev = platform_get_drvdata(pdev);
 811        struct ei_device *ei_local = netdev_priv(dev);
 812        struct ax_device *ax = to_ax_dev(dev);
 813        struct resource *mem;
 814
 815        unregister_netdev(dev);
 816
 817        iounmap(ei_local->mem);
 818        mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 819        release_mem_region(mem->start, resource_size(mem));
 820
 821        if (ax->map2) {
 822                iounmap(ax->map2);
 823                mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 824                release_mem_region(mem->start, resource_size(mem));
 825        }
 826
 827        platform_set_drvdata(pdev, NULL);
 828        free_netdev(dev);
 829
 830        return 0;
 831}
 832
 833/*
 834 * ax_probe
 835 *
 836 * This is the entry point when the platform device system uses to
 837 * notify us of a new device to attach to. Allocate memory, find the
 838 * resources and information passed, and map the necessary registers.
 839 */
 840static int ax_probe(struct platform_device *pdev)
 841{
 842        struct net_device *dev;
 843        struct ei_device *ei_local;
 844        struct ax_device *ax;
 845        struct resource *irq, *mem, *mem2;
 846        unsigned long mem_size, mem2_size = 0;
 847        int ret = 0;
 848
 849        dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
 850        if (dev == NULL)
 851                return -ENOMEM;
 852
 853        /* ok, let's setup our device */
 854        SET_NETDEV_DEV(dev, &pdev->dev);
 855        ei_local = netdev_priv(dev);
 856        ax = to_ax_dev(dev);
 857
 858        ax->plat = dev_get_platdata(&pdev->dev);
 859        platform_set_drvdata(pdev, dev);
 860
 861        ei_local->rxcr_base = ax->plat->rcr_val;
 862
 863        /* find the platform resources */
 864        irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 865        if (!irq) {
 866                dev_err(&pdev->dev, "no IRQ specified\n");
 867                ret = -ENXIO;
 868                goto exit_mem;
 869        }
 870
 871        dev->irq = irq->start;
 872        ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
 873
 874        if (irq->flags &  IORESOURCE_IRQ_SHAREABLE)
 875                ax->irqflags |= IRQF_SHARED;
 876
 877        mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 878        if (!mem) {
 879                dev_err(&pdev->dev, "no MEM specified\n");
 880                ret = -ENXIO;
 881                goto exit_mem;
 882        }
 883
 884        mem_size = resource_size(mem);
 885
 886        /*
 887         * setup the register offsets from either the platform data or
 888         * by using the size of the resource provided
 889         */
 890        if (ax->plat->reg_offsets)
 891                ei_local->reg_offset = ax->plat->reg_offsets;
 892        else {
 893                ei_local->reg_offset = ax->reg_offsets;
 894                for (ret = 0; ret < 0x18; ret++)
 895                        ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
 896        }
 897
 898        if (!request_mem_region(mem->start, mem_size, pdev->name)) {
 899                dev_err(&pdev->dev, "cannot reserve registers\n");
 900                ret = -ENXIO;
 901                goto exit_mem;
 902        }
 903
 904        ei_local->mem = ioremap(mem->start, mem_size);
 905        dev->base_addr = (unsigned long)ei_local->mem;
 906
 907        if (ei_local->mem == NULL) {
 908                dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
 909
 910                ret = -ENXIO;
 911                goto exit_req;
 912        }
 913
 914        /* look for reset area */
 915        mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 916        if (!mem2) {
 917                if (!ax->plat->reg_offsets) {
 918                        for (ret = 0; ret < 0x20; ret++)
 919                                ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
 920                }
 921        } else {
 922                mem2_size = resource_size(mem2);
 923
 924                if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
 925                        dev_err(&pdev->dev, "cannot reserve registers\n");
 926                        ret = -ENXIO;
 927                        goto exit_mem1;
 928                }
 929
 930                ax->map2 = ioremap(mem2->start, mem2_size);
 931                if (!ax->map2) {
 932                        dev_err(&pdev->dev, "cannot map reset register\n");
 933                        ret = -ENXIO;
 934                        goto exit_mem2;
 935                }
 936
 937                ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
 938        }
 939
 940        /* got resources, now initialise and register device */
 941        ret = ax_init_dev(dev);
 942        if (!ret)
 943                return 0;
 944
 945        if (!ax->map2)
 946                goto exit_mem1;
 947
 948        iounmap(ax->map2);
 949
 950 exit_mem2:
 951        if (mem2)
 952                release_mem_region(mem2->start, mem2_size);
 953
 954 exit_mem1:
 955        iounmap(ei_local->mem);
 956
 957 exit_req:
 958        release_mem_region(mem->start, mem_size);
 959
 960 exit_mem:
 961        platform_set_drvdata(pdev, NULL);
 962        free_netdev(dev);
 963
 964        return ret;
 965}
 966
 967/* suspend and resume */
 968
 969#ifdef CONFIG_PM
 970static int ax_suspend(struct platform_device *dev, pm_message_t state)
 971{
 972        struct net_device *ndev = platform_get_drvdata(dev);
 973        struct ax_device *ax = to_ax_dev(ndev);
 974
 975        ax->resume_open = ax->running;
 976
 977        netif_device_detach(ndev);
 978        ax_close(ndev);
 979
 980        return 0;
 981}
 982
 983static int ax_resume(struct platform_device *pdev)
 984{
 985        struct net_device *ndev = platform_get_drvdata(pdev);
 986        struct ax_device *ax = to_ax_dev(ndev);
 987
 988        ax_initial_setup(ndev, netdev_priv(ndev));
 989        ax_NS8390_init(ndev, ax->resume_open);
 990        netif_device_attach(ndev);
 991
 992        if (ax->resume_open)
 993                ax_open(ndev);
 994
 995        return 0;
 996}
 997
 998#else
 999#define ax_suspend NULL
1000#define ax_resume NULL
1001#endif
1002
1003static struct platform_driver axdrv = {
1004        .driver = {
1005                .name           = "ax88796",
1006        },
1007        .probe          = ax_probe,
1008        .remove         = ax_remove,
1009        .suspend        = ax_suspend,
1010        .resume         = ax_resume,
1011};
1012
1013module_platform_driver(axdrv);
1014
1015MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1016MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1017MODULE_LICENSE("GPL v2");
1018MODULE_ALIAS("platform:ax88796");
1019