linux-bk/drivers/net/apne.c
<<
>>
Prefs
   1/*
   2 * Amiga Linux/68k 8390 based PCMCIA Ethernet Driver for the Amiga 1200
   3 *
   4 * (C) Copyright 1997 Alain Malek
   5 *                    (Alain.Malek@cryogen.com)
   6 *
   7 * ----------------------------------------------------------------------------
   8 *
   9 * This program is based on
  10 *
  11 * ne.c:       A general non-shared-memory NS8390 ethernet driver for linux
  12 *             Written 1992-94 by Donald Becker.
  13 *
  14 * 8390.c:     A general NS8390 ethernet driver core for linux.
  15 *             Written 1992-94 by Donald Becker.
  16 *
  17 * cnetdevice: A Sana-II ethernet driver for AmigaOS
  18 *             Written by Bruce Abbott (bhabbott@inhb.co.nz)
  19 *
  20 * ----------------------------------------------------------------------------
  21 *
  22 * This file is subject to the terms and conditions of the GNU General Public
  23 * License.  See the file COPYING in the main directory of the Linux
  24 * distribution for more details.
  25 *
  26 * ----------------------------------------------------------------------------
  27 *
  28 */
  29
  30
  31#include <linux/module.h>
  32#include <linux/kernel.h>
  33#include <linux/errno.h>
  34#include <linux/pci.h>
  35#include <linux/init.h>
  36#include <linux/delay.h>
  37#include <linux/netdevice.h>
  38#include <linux/etherdevice.h>
  39
  40#include <asm/system.h>
  41#include <asm/io.h>
  42#include <asm/setup.h>
  43#include <asm/amigaints.h>
  44#include <asm/amigahw.h>
  45#include <asm/amigayle.h>
  46#include <asm/amipcmcia.h>
  47
  48#include "8390.h"
  49
  50/* ---- No user-serviceable parts below ---- */
  51
  52#define NE_BASE  (dev->base_addr)
  53#define NE_CMD                  0x00
  54#define NE_DATAPORT             0x10            /* NatSemi-defined port window offset. */
  55#define NE_RESET                0x1f            /* Issue a read to reset, a write to clear. */
  56#define NE_IO_EXTENT            0x20
  57
  58#define NE_EN0_ISR              0x07
  59#define NE_EN0_DCFG             0x0e
  60
  61#define NE_EN0_RSARLO           0x08
  62#define NE_EN0_RSARHI           0x09
  63#define NE_EN0_RCNTLO           0x0a
  64#define NE_EN0_RXCR             0x0c
  65#define NE_EN0_TXCR             0x0d
  66#define NE_EN0_RCNTHI           0x0b
  67#define NE_EN0_IMR              0x0f
  68
  69#define NE1SM_START_PG  0x20    /* First page of TX buffer */
  70#define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
  71#define NESM_START_PG   0x40    /* First page of TX buffer */
  72#define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
  73
  74
  75int apne_probe(struct net_device *dev);
  76static int apne_probe1(struct net_device *dev, int ioaddr);
  77
  78static int apne_open(struct net_device *dev);
  79static int apne_close(struct net_device *dev);
  80
  81static void apne_reset_8390(struct net_device *dev);
  82static void apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
  83                          int ring_page);
  84static void apne_block_input(struct net_device *dev, int count,
  85                                                                struct sk_buff *skb, int ring_offset);
  86static void apne_block_output(struct net_device *dev, const int count,
  87                                                        const unsigned char *buf, const int start_page);
  88static irqreturn_t apne_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  89
  90static int init_pcmcia(void);
  91
  92/* IO base address used for nic */
  93
  94#define IOBASE 0x300
  95
  96/*
  97   use MANUAL_CONFIG and MANUAL_OFFSET for enabling IO by hand
  98   you can find the values to use by looking at the cnet.device
  99   config file example (the default values are for the CNET40BC card)
 100*/
 101
 102/*
 103#define MANUAL_CONFIG 0x20
 104#define MANUAL_OFFSET 0x3f8
 105
 106#define MANUAL_HWADDR0 0x00
 107#define MANUAL_HWADDR1 0x12
 108#define MANUAL_HWADDR2 0x34
 109#define MANUAL_HWADDR3 0x56
 110#define MANUAL_HWADDR4 0x78
 111#define MANUAL_HWADDR5 0x9a
 112*/
 113
 114static const char version[] =
 115    "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
 116
 117static int apne_owned;  /* signal if card already owned */
 118
 119int __init apne_probe(struct net_device *dev)
 120{
 121#ifndef MANUAL_CONFIG
 122        char tuple[8];
 123#endif
 124
 125        if (apne_owned)
 126                return -ENODEV;
 127
 128        SET_MODULE_OWNER(dev);
 129
 130        if ( !(AMIGAHW_PRESENT(PCMCIA)) )
 131                return (-ENODEV);
 132                                
 133        printk("Looking for PCMCIA ethernet card : ");
 134                                        
 135        /* check if a card is inserted */
 136        if (!(PCMCIA_INSERTED)) {
 137                printk("NO PCMCIA card inserted\n");
 138                return (-ENODEV);
 139        }
 140                                                                                                
 141        /* disable pcmcia irq for readtuple */
 142        pcmcia_disable_irq();
 143
 144#ifndef MANUAL_CONFIG
 145        if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
 146                (tuple[2] != CISTPL_FUNCID_NETWORK)) {
 147                printk("not an ethernet card\n");
 148                return (-ENODEV);
 149        }
 150#endif
 151
 152        printk("ethernet PCMCIA card inserted\n");
 153
 154        if (init_pcmcia())
 155                return apne_probe1(dev, IOBASE);
 156        else
 157                return (-ENODEV);
 158
 159}
 160
 161static int __init apne_probe1(struct net_device *dev, int ioaddr)
 162{
 163    int i;
 164    unsigned char SA_prom[32];
 165    int wordlength = 2;
 166    const char *name = NULL;
 167    int start_page, stop_page;
 168#ifndef MANUAL_HWADDR0
 169    int neX000, ctron;
 170#endif
 171    static unsigned version_printed;
 172 
 173    if (ei_debug  &&  version_printed++ == 0)
 174        printk(version);
 175
 176    printk("PCMCIA NE*000 ethercard probe");
 177
 178    /* Reset card. Who knows what dain-bramaged state it was left in. */
 179    {   unsigned long reset_start_time = jiffies;
 180
 181        outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
 182
 183        while ((inb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
 184                if (jiffies - reset_start_time > 2*HZ/100) {
 185                        printk(" not found (no reset ack).\n");
 186                        return -ENODEV;
 187                }
 188
 189        outb(0xff, ioaddr + NE_EN0_ISR);                /* Ack all intr. */
 190    }
 191
 192#ifndef MANUAL_HWADDR0
 193
 194    /* Read the 16 bytes of station address PROM.
 195       We must first initialize registers, similar to NS8390_init(eifdev, 0).
 196       We can't reliably read the SAPROM address without this.
 197       (I learned the hard way!). */
 198    {
 199        struct {unsigned long value, offset; } program_seq[] = {
 200            {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
 201            {0x48,      NE_EN0_DCFG},   /* Set byte-wide (0x48) access. */
 202            {0x00,      NE_EN0_RCNTLO}, /* Clear the count regs. */
 203            {0x00,      NE_EN0_RCNTHI},
 204            {0x00,      NE_EN0_IMR},    /* Mask completion irq. */
 205            {0xFF,      NE_EN0_ISR},
 206            {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20  Set to monitor */
 207            {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02  and loopback mode. */
 208            {32,        NE_EN0_RCNTLO},
 209            {0x00,      NE_EN0_RCNTHI},
 210            {0x00,      NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
 211            {0x00,      NE_EN0_RSARHI},
 212            {E8390_RREAD+E8390_START, NE_CMD},
 213        };
 214        for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
 215            outb(program_seq[i].value, ioaddr + program_seq[i].offset);
 216        }
 217
 218    }
 219    for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
 220        SA_prom[i] = inb(ioaddr + NE_DATAPORT);
 221        SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
 222        if (SA_prom[i] != SA_prom[i+1])
 223            wordlength = 1;
 224    }
 225
 226    /*  At this point, wordlength *only* tells us if the SA_prom is doubled
 227        up or not because some broken PCI cards don't respect the byte-wide
 228        request in program_seq above, and hence don't have doubled up values. 
 229        These broken cards would otherwise be detected as an ne1000.  */
 230
 231    if (wordlength == 2)
 232        for (i = 0; i < 16; i++)
 233                SA_prom[i] = SA_prom[i+i];
 234    
 235    if (wordlength == 2) {
 236        /* We must set the 8390 for word mode. */
 237        outb(0x49, ioaddr + NE_EN0_DCFG);
 238        start_page = NESM_START_PG;
 239        stop_page = NESM_STOP_PG;
 240    } else {
 241        start_page = NE1SM_START_PG;
 242        stop_page = NE1SM_STOP_PG;
 243    }
 244
 245    neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
 246    ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
 247
 248    /* Set up the rest of the parameters. */
 249    if (neX000) {
 250        name = (wordlength == 2) ? "NE2000" : "NE1000";
 251    } else if (ctron) {
 252        name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
 253        start_page = 0x01;
 254        stop_page = (wordlength == 2) ? 0x40 : 0x20;
 255    } else {
 256        printk(" not found.\n");
 257        return -ENXIO;
 258
 259    }
 260
 261#else
 262    wordlength = 2;
 263    /* We must set the 8390 for word mode. */
 264    outb(0x49, ioaddr + NE_EN0_DCFG);
 265    start_page = NESM_START_PG;
 266    stop_page = NESM_STOP_PG;
 267
 268    SA_prom[0] = MANUAL_HWADDR0;
 269    SA_prom[1] = MANUAL_HWADDR1;
 270    SA_prom[2] = MANUAL_HWADDR2;
 271    SA_prom[3] = MANUAL_HWADDR3;
 272    SA_prom[4] = MANUAL_HWADDR4;
 273    SA_prom[5] = MANUAL_HWADDR5;
 274    name = "NE2000";
 275#endif
 276
 277    dev->base_addr = ioaddr;
 278
 279    /* Install the Interrupt handler */
 280    i = request_irq(IRQ_AMIGA_PORTS, apne_interrupt, SA_SHIRQ, dev->name, dev);
 281    if (i) return i;
 282
 283    /* Allocate dev->priv and fill in 8390 specific dev fields. */
 284    if (ethdev_init(dev)) {
 285        printk (" unable to get memory for dev->priv.\n");
 286        free_irq(IRQ_AMIGA_PORTS, dev);
 287        return -ENOMEM;
 288    }
 289
 290    for(i = 0; i < ETHER_ADDR_LEN; i++) {
 291        printk(" %2.2x", SA_prom[i]);
 292        dev->dev_addr[i] = SA_prom[i];
 293    }
 294
 295    printk("\n%s: %s found.\n", dev->name, name);
 296
 297    ei_status.name = name;
 298    ei_status.tx_start_page = start_page;
 299    ei_status.stop_page = stop_page;
 300    ei_status.word16 = (wordlength == 2);
 301
 302    ei_status.rx_start_page = start_page + TX_PAGES;
 303
 304    ei_status.reset_8390 = &apne_reset_8390;
 305    ei_status.block_input = &apne_block_input;
 306    ei_status.block_output = &apne_block_output;
 307    ei_status.get_8390_hdr = &apne_get_8390_hdr;
 308    dev->open = &apne_open;
 309    dev->stop = &apne_close;
 310    NS8390_init(dev, 0);
 311
 312    pcmcia_ack_int(pcmcia_get_intreq());                /* ack PCMCIA int req */
 313    pcmcia_enable_irq();
 314
 315    apne_owned = 1;
 316
 317    return 0;
 318}
 319
 320static int
 321apne_open(struct net_device *dev)
 322{
 323    ei_open(dev);
 324    return 0;
 325}
 326
 327static int
 328apne_close(struct net_device *dev)
 329{
 330    if (ei_debug > 1)
 331        printk("%s: Shutting down ethercard.\n", dev->name);
 332    ei_close(dev);
 333    return 0;
 334}
 335
 336/* Hard reset the card.  This used to pause for the same period that a
 337   8390 reset command required, but that shouldn't be necessary. */
 338static void
 339apne_reset_8390(struct net_device *dev)
 340{
 341    unsigned long reset_start_time = jiffies;
 342
 343    init_pcmcia();
 344
 345    if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
 346
 347    outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
 348
 349    ei_status.txing = 0;
 350    ei_status.dmaing = 0;
 351
 352    /* This check _should_not_ be necessary, omit eventually. */
 353    while ((inb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
 354        if (jiffies - reset_start_time > 2*HZ/100) {
 355            printk("%s: ne_reset_8390() did not complete.\n", dev->name);
 356            break;
 357        }
 358    outb(ENISR_RESET, NE_BASE + NE_EN0_ISR);    /* Ack intr. */
 359}
 360
 361/* Grab the 8390 specific header. Similar to the block_input routine, but
 362   we don't need to be concerned with ring wrap as the header will be at
 363   the start of a page, so we optimize accordingly. */
 364
 365static void
 366apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
 367{
 368
 369    int nic_base = dev->base_addr;
 370    int cnt;
 371    char *ptrc;
 372    short *ptrs;
 373
 374    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 375    if (ei_status.dmaing) {
 376        printk("%s: DMAing conflict in ne_get_8390_hdr "
 377           "[DMAstat:%d][irqlock:%d][intr:%d].\n",
 378           dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
 379        return;
 380    }
 381
 382    ei_status.dmaing |= 0x01;
 383    outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
 384    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
 385    outb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
 386    outb(0, nic_base + NE_EN0_RCNTHI);
 387    outb(0, nic_base + NE_EN0_RSARLO);          /* On page boundary */
 388    outb(ring_page, nic_base + NE_EN0_RSARHI);
 389    outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 390
 391    if (ei_status.word16) {
 392        ptrs = (short*)hdr;
 393        for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
 394            *ptrs++ = inw(NE_BASE + NE_DATAPORT);
 395    } else {
 396        ptrc = (char*)hdr;
 397        for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
 398            *ptrc++ = inb(NE_BASE + NE_DATAPORT);
 399    }
 400
 401    outb(ENISR_RDC, nic_base + NE_EN0_ISR);     /* Ack intr. */
 402    ei_status.dmaing &= ~0x01;
 403
 404    le16_to_cpus(&hdr->count);
 405}
 406
 407/* Block input and output, similar to the Crynwr packet driver.  If you
 408   are porting to a new ethercard, look at the packet driver source for hints.
 409   The NEx000 doesn't share the on-board packet memory -- you have to put
 410   the packet out through the "remote DMA" dataport using outb. */
 411
 412static void
 413apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
 414{
 415    int nic_base = dev->base_addr;
 416    char *buf = skb->data;
 417    char *ptrc;
 418    short *ptrs;
 419    int cnt;
 420
 421    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 422    if (ei_status.dmaing) {
 423        printk("%s: DMAing conflict in ne_block_input "
 424           "[DMAstat:%d][irqlock:%d][intr:%d].\n",
 425           dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
 426        return;
 427    }
 428    ei_status.dmaing |= 0x01;
 429    outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
 430    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
 431    outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
 432    outb(count >> 8, nic_base + NE_EN0_RCNTHI);
 433    outb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
 434    outb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
 435    outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 436    if (ei_status.word16) {
 437      ptrs = (short*)buf;
 438      for (cnt = 0; cnt < (count>>1); cnt++)
 439        *ptrs++ = inw(NE_BASE + NE_DATAPORT);
 440      if (count & 0x01) {
 441        buf[count-1] = inb(NE_BASE + NE_DATAPORT);
 442      }
 443    } else {
 444      ptrc = (char*)buf;
 445      for (cnt = 0; cnt < count; cnt++)
 446        *ptrc++ = inb(NE_BASE + NE_DATAPORT);
 447    }
 448
 449    outb(ENISR_RDC, nic_base + NE_EN0_ISR);     /* Ack intr. */
 450    ei_status.dmaing &= ~0x01;
 451}
 452
 453static void
 454apne_block_output(struct net_device *dev, int count,
 455                const unsigned char *buf, const int start_page)
 456{
 457    int nic_base = NE_BASE;
 458    unsigned long dma_start;
 459    char *ptrc;
 460    short *ptrs;
 461    int cnt;
 462
 463    /* Round the count up for word writes.  Do we need to do this?
 464       What effect will an odd byte count have on the 8390?
 465       I should check someday. */
 466    if (ei_status.word16 && (count & 0x01))
 467      count++;
 468
 469    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 470    if (ei_status.dmaing) {
 471        printk("%s: DMAing conflict in ne_block_output."
 472           "[DMAstat:%d][irqlock:%d][intr:%d]\n",
 473           dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
 474        return;
 475    }
 476    ei_status.dmaing |= 0x01;
 477    /* We should already be in page 0, but to be safe... */
 478    outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
 479
 480    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
 481
 482   /* Now the normal output. */
 483    outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
 484    outb(count >> 8,   nic_base + NE_EN0_RCNTHI);
 485    outb(0x00, nic_base + NE_EN0_RSARLO);
 486    outb(start_page, nic_base + NE_EN0_RSARHI);
 487
 488    outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
 489    if (ei_status.word16) {
 490        ptrs = (short*)buf;
 491        for (cnt = 0; cnt < count>>1; cnt++)
 492            outw(*ptrs++, NE_BASE+NE_DATAPORT);
 493    } else {
 494        ptrc = (char*)buf;
 495        for (cnt = 0; cnt < count; cnt++)
 496            outb(*ptrc++, NE_BASE + NE_DATAPORT);
 497    }
 498
 499    dma_start = jiffies;
 500
 501    while ((inb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
 502        if (jiffies - dma_start > 2*HZ/100) {           /* 20ms */
 503                printk("%s: timeout waiting for Tx RDC.\n", dev->name);
 504                apne_reset_8390(dev);
 505                NS8390_init(dev,1);
 506                break;
 507        }
 508
 509    outb(ENISR_RDC, nic_base + NE_EN0_ISR);     /* Ack intr. */
 510    ei_status.dmaing &= ~0x01;
 511    return;
 512}
 513
 514static irqreturn_t apne_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 515{
 516    unsigned char pcmcia_intreq;
 517
 518    if (!(gayle.inten & GAYLE_IRQ_IRQ))
 519        return IRQ_NONE;
 520
 521    pcmcia_intreq = pcmcia_get_intreq();
 522
 523    if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
 524        pcmcia_ack_int(pcmcia_intreq);
 525        return IRQ_NONE;
 526    }
 527    if (ei_debug > 3)
 528        printk("pcmcia intreq = %x\n", pcmcia_intreq);
 529    pcmcia_disable_irq();                       /* to get rid of the sti() within ei_interrupt */
 530    ei_interrupt(irq, dev_id, regs);
 531    pcmcia_ack_int(pcmcia_get_intreq());
 532    pcmcia_enable_irq();
 533    return IRQ_HANDLED;
 534}
 535
 536#ifdef MODULE
 537static struct net_device apne_dev;
 538
 539int init_module(void)
 540{
 541        int err;
 542
 543        apne_dev.init = apne_probe;
 544        if ((err = register_netdev(&apne_dev))) {
 545                if (err == -EIO)
 546                        printk("No PCMCIA NEx000 ethernet card found.\n");
 547                return (err);
 548        }
 549        return (0);
 550}
 551
 552void cleanup_module(void)
 553{
 554        unregister_netdev(&apne_dev);
 555
 556        pcmcia_disable_irq();
 557
 558        free_irq(IRQ_AMIGA_PORTS, &apne_dev);
 559
 560        pcmcia_reset();
 561
 562        apne_owned = 0;
 563}
 564
 565#endif
 566
 567static int init_pcmcia(void)
 568{
 569        u_char config;
 570#ifndef MANUAL_CONFIG
 571        u_char tuple[32];
 572        int offset_len;
 573#endif
 574        u_long offset;
 575
 576        pcmcia_reset();
 577        pcmcia_program_voltage(PCMCIA_0V);
 578        pcmcia_access_speed(PCMCIA_SPEED_250NS);
 579        pcmcia_write_enable();
 580
 581#ifdef MANUAL_CONFIG
 582        config = MANUAL_CONFIG;
 583#else
 584        /* get and write config byte to enable IO port */
 585
 586        if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
 587                return 0;
 588
 589        config = tuple[2] & 0x3f;
 590#endif
 591#ifdef MANUAL_OFFSET
 592        offset = MANUAL_OFFSET;
 593#else
 594        if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
 595                return 0;
 596
 597        offset_len = (tuple[2] & 0x3) + 1;
 598        offset = 0;
 599        while(offset_len--) {
 600                offset = (offset << 8) | tuple[4+offset_len];
 601        }
 602#endif
 603
 604        out_8(GAYLE_ATTRIBUTE+offset, config);
 605
 606        return 1;
 607}
 608
 609MODULE_LICENSE("GPL");
 610
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.