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        /* mask with MAC supported features */
 381        phy_dev->supported &= PHY_BASIC_FEATURES;
 382        phy_dev->advertising = phy_dev->supported;
 383
 384        netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
 385                    phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
 386
 387        return 0;
 388}
 389
 390static void ax_phy_switch(struct net_device *dev, int on)
 391{
 392        struct ei_device *ei_local = netdev_priv(dev);
 393        struct ax_device *ax = to_ax_dev(dev);
 394
 395        u8 reg_gpoc =  ax->plat->gpoc_val;
 396
 397        if (!!on)
 398                reg_gpoc &= ~AX_GPOC_PPDSET;
 399        else
 400                reg_gpoc |= AX_GPOC_PPDSET;
 401
 402        ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
 403}
 404
 405static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
 406{
 407        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 408
 409        if (level)
 410                ax->reg_memr |= AX_MEMR_MDC;
 411        else
 412                ax->reg_memr &= ~AX_MEMR_MDC;
 413
 414        ei_outb(ax->reg_memr, ax->addr_memr);
 415}
 416
 417static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
 418{
 419        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 420
 421        if (output)
 422                ax->reg_memr &= ~AX_MEMR_MDIR;
 423        else
 424                ax->reg_memr |= AX_MEMR_MDIR;
 425
 426        ei_outb(ax->reg_memr, ax->addr_memr);
 427}
 428
 429static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
 430{
 431        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 432
 433        if (value)
 434                ax->reg_memr |= AX_MEMR_MDO;
 435        else
 436                ax->reg_memr &= ~AX_MEMR_MDO;
 437
 438        ei_outb(ax->reg_memr, ax->addr_memr);
 439}
 440
 441static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
 442{
 443        struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 444        int reg_memr = ei_inb(ax->addr_memr);
 445
 446        return reg_memr & AX_MEMR_MDI ? 1 : 0;
 447}
 448
 449static const struct mdiobb_ops bb_ops = {
 450        .owner = THIS_MODULE,
 451        .set_mdc = ax_bb_mdc,
 452        .set_mdio_dir = ax_bb_dir,
 453        .set_mdio_data = ax_bb_set_data,
 454        .get_mdio_data = ax_bb_get_data,
 455};
 456
 457static int ax_mii_init(struct net_device *dev)
 458{
 459        struct platform_device *pdev = to_platform_device(dev->dev.parent);
 460        struct ei_device *ei_local = netdev_priv(dev);
 461        struct ax_device *ax = to_ax_dev(dev);
 462        int err;
 463
 464        ax->bb_ctrl.ops = &bb_ops;
 465        ax->addr_memr = ei_local->mem + AX_MEMR;
 466        ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
 467        if (!ax->mii_bus) {
 468                err = -ENOMEM;
 469                goto out;
 470        }
 471
 472        ax->mii_bus->name = "ax88796_mii_bus";
 473        ax->mii_bus->parent = dev->dev.parent;
 474        snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
 475                 pdev->name, pdev->id);
 476
 477        err = mdiobus_register(ax->mii_bus);
 478        if (err)
 479                goto out_free_mdio_bitbang;
 480
 481        return 0;
 482
 483 out_free_mdio_bitbang:
 484        free_mdio_bitbang(ax->mii_bus);
 485 out:
 486        return err;
 487}
 488
 489static int ax_open(struct net_device *dev)
 490{
 491        struct ax_device *ax = to_ax_dev(dev);
 492        int ret;
 493
 494        netdev_dbg(dev, "open\n");
 495
 496        ret = ax_mii_init(dev);
 497        if (ret)
 498                goto failed_mii;
 499
 500        if (ax->plat->check_irq)
 501                ret = request_irq(dev->irq, ax_ei_interrupt_filtered,
 502                                  ax->irqflags, dev->name, dev);
 503        else
 504                ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
 505                                  dev->name, dev);
 506        if (ret)
 507                goto failed_request_irq;
 508
 509        /* turn the phy on (if turned off) */
 510        ax_phy_switch(dev, 1);
 511
 512        ret = ax_mii_probe(dev);
 513        if (ret)
 514                goto failed_mii_probe;
 515        phy_start(dev->phydev);
 516
 517        ret = ax_ei_open(dev);
 518        if (ret)
 519                goto failed_ax_ei_open;
 520
 521        ax->running = 1;
 522
 523        return 0;
 524
 525 failed_ax_ei_open:
 526        phy_disconnect(dev->phydev);
 527 failed_mii_probe:
 528        ax_phy_switch(dev, 0);
 529        free_irq(dev->irq, dev);
 530 failed_request_irq:
 531        /* unregister mdiobus */
 532        mdiobus_unregister(ax->mii_bus);
 533        free_mdio_bitbang(ax->mii_bus);
 534 failed_mii:
 535        return ret;
 536}
 537
 538static int ax_close(struct net_device *dev)
 539{
 540        struct ax_device *ax = to_ax_dev(dev);
 541
 542        netdev_dbg(dev, "close\n");
 543
 544        ax->running = 0;
 545        wmb();
 546
 547        ax_ei_close(dev);
 548
 549        /* turn the phy off */
 550        ax_phy_switch(dev, 0);
 551        phy_disconnect(dev->phydev);
 552
 553        free_irq(dev->irq, dev);
 554
 555        mdiobus_unregister(ax->mii_bus);
 556        free_mdio_bitbang(ax->mii_bus);
 557        return 0;
 558}
 559
 560static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
 561{
 562        struct phy_device *phy_dev = dev->phydev;
 563
 564        if (!netif_running(dev))
 565                return -EINVAL;
 566
 567        if (!phy_dev)
 568                return -ENODEV;
 569
 570        return phy_mii_ioctl(phy_dev, req, cmd);
 571}
 572
 573/* ethtool ops */
 574
 575static void ax_get_drvinfo(struct net_device *dev,
 576                           struct ethtool_drvinfo *info)
 577{
 578        struct platform_device *pdev = to_platform_device(dev->dev.parent);
 579
 580        strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
 581        strlcpy(info->version, DRV_VERSION, sizeof(info->version));
 582        strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
 583}
 584
 585static u32 ax_get_msglevel(struct net_device *dev)
 586{
 587        struct ei_device *ei_local = netdev_priv(dev);
 588
 589        return ei_local->msg_enable;
 590}
 591
 592static void ax_set_msglevel(struct net_device *dev, u32 v)
 593{
 594        struct ei_device *ei_local = netdev_priv(dev);
 595
 596        ei_local->msg_enable = v;
 597}
 598
 599static const struct ethtool_ops ax_ethtool_ops = {
 600        .get_drvinfo            = ax_get_drvinfo,
 601        .get_link               = ethtool_op_get_link,
 602        .get_ts_info            = ethtool_op_get_ts_info,
 603        .get_msglevel           = ax_get_msglevel,
 604        .set_msglevel           = ax_set_msglevel,
 605        .get_link_ksettings     = phy_ethtool_get_link_ksettings,
 606        .set_link_ksettings     = phy_ethtool_set_link_ksettings,
 607};
 608
 609#ifdef CONFIG_AX88796_93CX6
 610static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
 611{
 612        struct ei_device *ei_local = eeprom->data;
 613        u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 614
 615        eeprom->reg_data_in = reg & AX_MEMR_EEI;
 616        eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
 617        eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
 618        eeprom->reg_chip_select = reg & AX_MEMR_EECS;
 619}
 620
 621static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
 622{
 623        struct ei_device *ei_local = eeprom->data;
 624        u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 625
 626        reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
 627
 628        if (eeprom->reg_data_in)
 629                reg |= AX_MEMR_EEI;
 630        if (eeprom->reg_data_clock)
 631                reg |= AX_MEMR_EECLK;
 632        if (eeprom->reg_chip_select)
 633                reg |= AX_MEMR_EECS;
 634
 635        ei_outb(reg, ei_local->mem + AX_MEMR);
 636        udelay(10);
 637}
 638#endif
 639
 640static const struct net_device_ops ax_netdev_ops = {
 641        .ndo_open               = ax_open,
 642        .ndo_stop               = ax_close,
 643        .ndo_do_ioctl           = ax_ioctl,
 644
 645        .ndo_start_xmit         = ax_ei_start_xmit,
 646        .ndo_tx_timeout         = ax_ei_tx_timeout,
 647        .ndo_get_stats          = ax_ei_get_stats,
 648        .ndo_set_rx_mode        = ax_ei_set_multicast_list,
 649        .ndo_validate_addr      = eth_validate_addr,
 650        .ndo_set_mac_address    = eth_mac_addr,
 651#ifdef CONFIG_NET_POLL_CONTROLLER
 652        .ndo_poll_controller    = ax_ei_poll,
 653#endif
 654};
 655
 656/* setup code */
 657
 658static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
 659{
 660        void __iomem *ioaddr = ei_local->mem;
 661        struct ax_device *ax = to_ax_dev(dev);
 662
 663        /* Select page 0 */
 664        ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
 665
 666        /* set to byte access */
 667        ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
 668        ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
 669}
 670
 671/*
 672 * ax_init_dev
 673 *
 674 * initialise the specified device, taking care to note the MAC
 675 * address it may already have (if configured), ensure
 676 * the device is ready to be used by lib8390.c and registerd with
 677 * the network layer.
 678 */
 679static int ax_init_dev(struct net_device *dev)
 680{
 681        struct ei_device *ei_local = netdev_priv(dev);
 682        struct ax_device *ax = to_ax_dev(dev);
 683        void __iomem *ioaddr = ei_local->mem;
 684        unsigned int start_page;
 685        unsigned int stop_page;
 686        int ret;
 687        int i;
 688
 689        ret = ax_initial_check(dev);
 690        if (ret)
 691                goto err_out;
 692
 693        /* setup goes here */
 694
 695        ax_initial_setup(dev, ei_local);
 696
 697        /* read the mac from the card prom if we need it */
 698
 699        if (ax->plat->flags & AXFLG_HAS_EEPROM) {
 700                unsigned char SA_prom[32];
 701
 702                ei_outb(6, ioaddr + EN0_RCNTLO);
 703                ei_outb(0, ioaddr + EN0_RCNTHI);
 704                ei_outb(0, ioaddr + EN0_RSARLO);
 705                ei_outb(0, ioaddr + EN0_RSARHI);
 706                ei_outb(E8390_RREAD + E8390_START, ioaddr + NE_CMD);
 707                for (i = 0; i < sizeof(SA_prom); i += 2) {
 708                        SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
 709                        SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
 710                }
 711                ei_outb(ENISR_RDC, ioaddr + EN0_ISR);   /* Ack intr. */
 712
 713                if (ax->plat->wordlength == 2)
 714                        for (i = 0; i < 16; i++)
 715                                SA_prom[i] = SA_prom[i+i];
 716
 717                memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
 718        }
 719
 720#ifdef CONFIG_AX88796_93CX6
 721        if (ax->plat->flags & AXFLG_HAS_93CX6) {
 722                unsigned char mac_addr[ETH_ALEN];
 723                struct eeprom_93cx6 eeprom;
 724
 725                eeprom.data = ei_local;
 726                eeprom.register_read = ax_eeprom_register_read;
 727                eeprom.register_write = ax_eeprom_register_write;
 728                eeprom.width = PCI_EEPROM_WIDTH_93C56;
 729
 730                eeprom_93cx6_multiread(&eeprom, 0,
 731                                       (__le16 __force *)mac_addr,
 732                                       sizeof(mac_addr) >> 1);
 733
 734                memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
 735        }
 736#endif
 737        if (ax->plat->wordlength == 2) {
 738                /* We must set the 8390 for word mode. */
 739                ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
 740                start_page = NESM_START_PG;
 741                stop_page = NESM_STOP_PG;
 742        } else {
 743                start_page = NE1SM_START_PG;
 744                stop_page = NE1SM_STOP_PG;
 745        }
 746
 747        /* load the mac-address from the device */
 748        if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
 749                ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
 750                        ei_local->mem + E8390_CMD); /* 0x61 */
 751                for (i = 0; i < ETH_ALEN; i++)
 752                        dev->dev_addr[i] =
 753                                ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
 754        }
 755
 756        if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
 757            ax->plat->mac_addr)
 758                memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
 759
 760        if (!is_valid_ether_addr(dev->dev_addr)) {
 761                eth_hw_addr_random(dev);
 762                dev_info(&dev->dev, "Using random MAC address: %pM\n",
 763                         dev->dev_addr);
 764        }
 765
 766        ax_reset_8390(dev);
 767
 768        ei_local->name = "AX88796";
 769        ei_local->tx_start_page = start_page;
 770        ei_local->stop_page = stop_page;
 771        ei_local->word16 = (ax->plat->wordlength == 2);
 772        ei_local->rx_start_page = start_page + TX_PAGES;
 773
 774#ifdef PACKETBUF_MEMSIZE
 775        /* Allow the packet buffer size to be overridden by know-it-alls. */
 776        ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
 777#endif
 778
 779        ei_local->reset_8390 = &ax_reset_8390;
 780        if (ax->plat->block_input)
 781                ei_local->block_input = ax->plat->block_input;
 782        else
 783                ei_local->block_input = &ax_block_input;
 784        if (ax->plat->block_output)
 785                ei_local->block_output = ax->plat->block_output;
 786        else
 787                ei_local->block_output = &ax_block_output;
 788        ei_local->get_8390_hdr = &ax_get_8390_hdr;
 789        ei_local->priv = 0;
 790
 791        dev->netdev_ops = &ax_netdev_ops;
 792        dev->ethtool_ops = &ax_ethtool_ops;
 793
 794        ax_NS8390_init(dev, 0);
 795
 796        ret = register_netdev(dev);
 797        if (ret)
 798                goto err_out;
 799
 800        netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
 801                    ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
 802                    dev->dev_addr);
 803
 804        return 0;
 805
 806 err_out:
 807        return ret;
 808}
 809
 810static int ax_remove(struct platform_device *pdev)
 811{
 812        struct net_device *dev = platform_get_drvdata(pdev);
 813        struct ei_device *ei_local = netdev_priv(dev);
 814        struct ax_device *ax = to_ax_dev(dev);
 815        struct resource *mem;
 816
 817        unregister_netdev(dev);
 818
 819        iounmap(ei_local->mem);
 820        mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 821        release_mem_region(mem->start, resource_size(mem));
 822
 823        if (ax->map2) {
 824                iounmap(ax->map2);
 825                mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 826                release_mem_region(mem->start, resource_size(mem));
 827        }
 828
 829        platform_set_drvdata(pdev, NULL);
 830        free_netdev(dev);
 831
 832        return 0;
 833}
 834
 835/*
 836 * ax_probe
 837 *
 838 * This is the entry point when the platform device system uses to
 839 * notify us of a new device to attach to. Allocate memory, find the
 840 * resources and information passed, and map the necessary registers.
 841 */
 842static int ax_probe(struct platform_device *pdev)
 843{
 844        struct net_device *dev;
 845        struct ei_device *ei_local;
 846        struct ax_device *ax;
 847        struct resource *irq, *mem, *mem2;
 848        unsigned long mem_size, mem2_size = 0;
 849        int ret = 0;
 850
 851        dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
 852        if (dev == NULL)
 853                return -ENOMEM;
 854
 855        /* ok, let's setup our device */
 856        SET_NETDEV_DEV(dev, &pdev->dev);
 857        ei_local = netdev_priv(dev);
 858        ax = to_ax_dev(dev);
 859
 860        ax->plat = dev_get_platdata(&pdev->dev);
 861        platform_set_drvdata(pdev, dev);
 862
 863        ei_local->rxcr_base = ax->plat->rcr_val;
 864
 865        /* find the platform resources */
 866        irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 867        if (!irq) {
 868                dev_err(&pdev->dev, "no IRQ specified\n");
 869                ret = -ENXIO;
 870                goto exit_mem;
 871        }
 872
 873        dev->irq = irq->start;
 874        ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
 875
 876        if (irq->flags &  IORESOURCE_IRQ_SHAREABLE)
 877                ax->irqflags |= IRQF_SHARED;
 878
 879        mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 880        if (!mem) {
 881                dev_err(&pdev->dev, "no MEM specified\n");
 882                ret = -ENXIO;
 883                goto exit_mem;
 884        }
 885
 886        mem_size = resource_size(mem);
 887
 888        /*
 889         * setup the register offsets from either the platform data or
 890         * by using the size of the resource provided
 891         */
 892        if (ax->plat->reg_offsets)
 893                ei_local->reg_offset = ax->plat->reg_offsets;
 894        else {
 895                ei_local->reg_offset = ax->reg_offsets;
 896                for (ret = 0; ret < 0x18; ret++)
 897                        ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
 898        }
 899
 900        if (!request_mem_region(mem->start, mem_size, pdev->name)) {
 901                dev_err(&pdev->dev, "cannot reserve registers\n");
 902                ret = -ENXIO;
 903                goto exit_mem;
 904        }
 905
 906        ei_local->mem = ioremap(mem->start, mem_size);
 907        dev->base_addr = (unsigned long)ei_local->mem;
 908
 909        if (ei_local->mem == NULL) {
 910                dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
 911
 912                ret = -ENXIO;
 913                goto exit_req;
 914        }
 915
 916        /* look for reset area */
 917        mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 918        if (!mem2) {
 919                if (!ax->plat->reg_offsets) {
 920                        for (ret = 0; ret < 0x20; ret++)
 921                                ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
 922                }
 923        } else {
 924                mem2_size = resource_size(mem2);
 925
 926                if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
 927                        dev_err(&pdev->dev, "cannot reserve registers\n");
 928                        ret = -ENXIO;
 929                        goto exit_mem1;
 930                }
 931
 932                ax->map2 = ioremap(mem2->start, mem2_size);
 933                if (!ax->map2) {
 934                        dev_err(&pdev->dev, "cannot map reset register\n");
 935                        ret = -ENXIO;
 936                        goto exit_mem2;
 937                }
 938
 939                ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
 940        }
 941
 942        /* got resources, now initialise and register device */
 943        ret = ax_init_dev(dev);
 944        if (!ret)
 945                return 0;
 946
 947        if (!ax->map2)
 948                goto exit_mem1;
 949
 950        iounmap(ax->map2);
 951
 952 exit_mem2:
 953        if (mem2)
 954                release_mem_region(mem2->start, mem2_size);
 955
 956 exit_mem1:
 957        iounmap(ei_local->mem);
 958
 959 exit_req:
 960        release_mem_region(mem->start, mem_size);
 961
 962 exit_mem:
 963        platform_set_drvdata(pdev, NULL);
 964        free_netdev(dev);
 965
 966        return ret;
 967}
 968
 969/* suspend and resume */
 970
 971#ifdef CONFIG_PM
 972static int ax_suspend(struct platform_device *dev, pm_message_t state)
 973{
 974        struct net_device *ndev = platform_get_drvdata(dev);
 975        struct ax_device *ax = to_ax_dev(ndev);
 976
 977        ax->resume_open = ax->running;
 978
 979        netif_device_detach(ndev);
 980        ax_close(ndev);
 981
 982        return 0;
 983}
 984
 985static int ax_resume(struct platform_device *pdev)
 986{
 987        struct net_device *ndev = platform_get_drvdata(pdev);
 988        struct ax_device *ax = to_ax_dev(ndev);
 989
 990        ax_initial_setup(ndev, netdev_priv(ndev));
 991        ax_NS8390_init(ndev, ax->resume_open);
 992        netif_device_attach(ndev);
 993
 994        if (ax->resume_open)
 995                ax_open(ndev);
 996
 997        return 0;
 998}
 999
1000#else
1001#define ax_suspend NULL
1002#define ax_resume NULL
1003#endif
1004
1005static struct platform_driver axdrv = {
1006        .driver = {
1007                .name           = "ax88796",
1008        },
1009        .probe          = ax_probe,
1010        .remove         = ax_remove,
1011        .suspend        = ax_suspend,
1012        .resume         = ax_resume,
1013};
1014
1015module_platform_driver(axdrv);
1016
1017MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1018MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1019MODULE_LICENSE("GPL v2");
1020MODULE_ALIAS("platform:ax88796");
1021