linux-bk/drivers/net/pcmcia/xirc2ps_cs.c
<<
>>
Prefs
   1/* [xirc2ps_cs.c wk 03.11.99] (1.40 1999/11/18 00:06:03)
   2 * Xircom CreditCard Ethernet Adapter IIps driver
   3 * Xircom Realport 10/100 (RE-100) driver 
   4 *
   5 * This driver supports various Xircom CreditCard Ethernet adapters
   6 * including the CE2, CE IIps, RE-10, CEM28, CEM33, CE33, CEM56,
   7 * CE3-100, CE3B, RE-100, REM10BT, and REM56G-100.
   8 *
   9 * 2000-09-24 <psheer@icon.co.za> The Xircom CE3B-100 may not
  10 * autodetect the media properly. In this case use the
  11 * if_port=1 (for 10BaseT) or if_port=4 (for 100BaseT) options
  12 * to force the media type.
  13 * 
  14 * Written originally by Werner Koch based on David Hinds' skeleton of the
  15 * PCMCIA driver.
  16 *
  17 * Copyright (c) 1997,1998 Werner Koch (dd9jn)
  18 *
  19 * This driver is free software; you can redistribute it and/or modify
  20 * it under the terms of the GNU General Public License as published by
  21 * the Free Software Foundation; either version 2 of the License, or
  22 * (at your option) any later version.
  23 *
  24 * It is distributed in the hope that it will be useful,
  25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27 * GNU General Public License for more details.
  28 *
  29 * You should have received a copy of the GNU General Public License
  30 * along with this program; if not, write to the Free Software
  31 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  32 *
  33 *
  34 * ALTERNATIVELY, this driver may be distributed under the terms of
  35 * the following license, in which case the provisions of this license
  36 * are required INSTEAD OF the GNU General Public License.  (This clause
  37 * is necessary due to a potential bad interaction between the GPL and
  38 * the restrictions contained in a BSD-style copyright.)
  39 *
  40 * Redistribution and use in source and binary forms, with or without
  41 * modification, are permitted provided that the following conditions
  42 * are met:
  43 * 1. Redistributions of source code must retain the above copyright
  44 *    notice, and the entire permission notice in its entirety,
  45 *    including the disclaimer of warranties.
  46 * 2. Redistributions in binary form must reproduce the above copyright
  47 *    notice, this list of conditions and the following disclaimer in the
  48 *    documentation and/or other materials provided with the distribution.
  49 * 3. The name of the author may not be used to endorse or promote
  50 *    products derived from this software without specific prior
  51 *    written permission.
  52 *
  53 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  56 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  61 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  63 * OF THE POSSIBILITY OF SUCH DAMAGE.
  64 */
  65
  66#include <linux/module.h>
  67#include <linux/kernel.h>
  68#include <linux/init.h>
  69#include <linux/ptrace.h>
  70#include <linux/slab.h>
  71#include <linux/string.h>
  72#include <linux/timer.h>
  73#include <linux/interrupt.h>
  74#include <linux/in.h>
  75#include <linux/delay.h>
  76#include <linux/ethtool.h>
  77#include <linux/netdevice.h>
  78#include <linux/etherdevice.h>
  79#include <linux/skbuff.h>
  80#include <linux/if_arp.h>
  81#include <linux/ioport.h>
  82
  83#include <pcmcia/version.h>
  84#include <pcmcia/cs_types.h>
  85#include <pcmcia/cs.h>
  86#include <pcmcia/cistpl.h>
  87#include <pcmcia/cisreg.h>
  88#include <pcmcia/ciscode.h>
  89
  90#include <asm/io.h>
  91#include <asm/system.h>
  92#include <asm/bitops.h>
  93#include <asm/uaccess.h>
  94
  95#ifndef MANFID_COMPAQ
  96  #define MANFID_COMPAQ            0x0138
  97  #define MANFID_COMPAQ2           0x0183  /* is this correct? */
  98#endif
  99
 100#include <pcmcia/ds.h>
 101
 102/* Time in jiffies before concluding Tx hung */
 103#define TX_TIMEOUT      ((400*HZ)/1000)
 104
 105/****************
 106 * Some constants used to access the hardware
 107 */
 108
 109/* Register offsets and value constans */
 110#define XIRCREG_CR  0   /* Command register (wr) */
 111enum xirc_cr {
 112    TransmitPacket = 0x01,
 113    SoftReset = 0x02,
 114    EnableIntr = 0x04,
 115    ForceIntr  = 0x08,
 116    ClearTxFIFO = 0x10,
 117    ClearRxOvrun = 0x20,
 118    RestartTx    = 0x40
 119};
 120#define XIRCREG_ESR 0   /* Ethernet status register (rd) */
 121enum xirc_esr {
 122    FullPktRcvd = 0x01, /* full packet in receive buffer */
 123    PktRejected = 0x04, /* a packet has been rejected */
 124    TxPktPend = 0x08,   /* TX Packet Pending */
 125    IncorPolarity = 0x10,
 126    MediaSelect = 0x20  /* set if TP, clear if AUI */
 127};
 128#define XIRCREG_PR  1   /* Page Register select */
 129#define XIRCREG_EDP 4   /* Ethernet Data Port Register */
 130#define XIRCREG_ISR 6   /* Ethernet Interrupt Status Register */
 131enum xirc_isr {
 132    TxBufOvr = 0x01,    /* TX Buffer Overflow */
 133    PktTxed  = 0x02,    /* Packet Transmitted */
 134    MACIntr  = 0x04,    /* MAC Interrupt occurred */
 135    TxResGrant = 0x08,  /* Tx Reservation Granted */
 136    RxFullPkt = 0x20,   /* Rx Full Packet */
 137    RxPktRej  = 0x40,   /* Rx Packet Rejected */
 138    ForcedIntr= 0x80    /* Forced Interrupt */
 139};
 140#define XIRCREG1_IMR0 12 /* Ethernet Interrupt Mask Register (on page 1)*/
 141#define XIRCREG1_IMR1 13
 142#define XIRCREG0_TSO  8  /* Transmit Space Open Register (on page 0)*/
 143#define XIRCREG0_TRS  10 /* Transmit reservation Size Register (page 0)*/
 144#define XIRCREG0_DO   12 /* Data Offset Register (page 0) (wr) */
 145#define XIRCREG0_RSR  12 /* Receive Status Register (page 0) (rd) */
 146enum xirc_rsr {
 147    PhyPkt = 0x01,      /* set:physical packet, clear: multicast packet */
 148    BrdcstPkt = 0x02,   /* set if it is a broadcast packet */
 149    PktTooLong = 0x04,  /* set if packet length > 1518 */
 150    AlignErr = 0x10,    /* incorrect CRC and last octet not complete */
 151    CRCErr = 0x20,      /* incorrect CRC and last octet is complete */
 152    PktRxOk = 0x80      /* received ok */
 153};
 154#define XIRCREG0_PTR 13 /* packets transmitted register (rd) */
 155#define XIRCREG0_RBC 14 /* receive byte count regsister (rd) */
 156#define XIRCREG1_ECR 14 /* ethernet configurationn register */
 157enum xirc_ecr {
 158    FullDuplex = 0x04,  /* enable full duplex mode */
 159    LongTPMode = 0x08,  /* adjust for longer lengths of TP cable */
 160    DisablePolCor = 0x10,/* disable auto polarity correction */
 161    DisableLinkPulse = 0x20, /* disable link pulse generation */
 162    DisableAutoTx = 0x40, /* disable auto-transmit */
 163};
 164#define XIRCREG2_RBS 8  /* receive buffer start register */
 165#define XIRCREG2_LED 10 /* LED Configuration register */
 166/* values for the leds:    Bits 2-0 for led 1
 167 *  0 disabled             Bits 5-3 for led 2
 168 *  1 collision
 169 *  2 noncollision
 170 *  3 link_detected
 171 *  4 incor_polarity
 172 *  5 jabber
 173 *  6 auto_assertion
 174 *  7 rx_tx_activity
 175 */
 176#define XIRCREG2_MSR 12 /* Mohawk specific register */
 177
 178#define XIRCREG4_GPR0 8 /* General Purpose Register 0 */
 179#define XIRCREG4_GPR1 9 /* General Purpose Register 1 */
 180#define XIRCREG2_GPR2 13 /* General Purpose Register 2 (page2!)*/
 181#define XIRCREG4_BOV 10 /* Bonding Version Register */
 182#define XIRCREG4_LMA 12 /* Local Memory Address Register */
 183#define XIRCREG4_LMD 14 /* Local Memory Data Port */
 184/* MAC register can only by accessed with 8 bit operations */
 185#define XIRCREG40_CMD0 8    /* Command Register (wr) */
 186enum xirc_cmd {             /* Commands */
 187    Transmit = 0x01,
 188    EnableRecv = 0x04,
 189    DisableRecv = 0x08,
 190    Abort = 0x10,
 191    Online = 0x20,
 192    IntrAck = 0x40,
 193    Offline = 0x80
 194};
 195#define XIRCREG5_RHSA0  10  /* Rx Host Start Address */
 196#define XIRCREG40_RXST0 9   /* Receive Status Register */
 197#define XIRCREG40_TXST0 11  /* Transmit Status Register 0 */
 198#define XIRCREG40_TXST1 12  /* Transmit Status Register 10 */
 199#define XIRCREG40_RMASK0 13  /* Receive Mask Register */
 200#define XIRCREG40_TMASK0 14  /* Transmit Mask Register 0 */
 201#define XIRCREG40_TMASK1 15  /* Transmit Mask Register 0 */
 202#define XIRCREG42_SWC0  8   /* Software Configuration 0 */
 203#define XIRCREG42_SWC1  9   /* Software Configuration 1 */
 204#define XIRCREG42_BOC   10  /* Back-Off Configuration */
 205#define XIRCREG44_TDR0  8   /* Time Domain Reflectometry 0 */
 206#define XIRCREG44_TDR1  9   /* Time Domain Reflectometry 1 */
 207#define XIRCREG44_RXBC_LO 10 /* Rx Byte Count 0 (rd) */
 208#define XIRCREG44_RXBC_HI 11 /* Rx Byte Count 1 (rd) */
 209#define XIRCREG45_REV    15 /* Revision Register (rd) */
 210#define XIRCREG50_IA    8   /* Individual Address (8-13) */
 211
 212static char *if_names[] = { "Auto", "10BaseT", "10Base2", "AUI", "100BaseT" };
 213
 214/****************
 215 * All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
 216 * you do not define PCMCIA_DEBUG at all, all the debug code will be
 217 * left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
 218 * be present but disabled -- but it can then be enabled for specific
 219 * modules at load time with a 'pc_debug=#' option to insmod.
 220 */
 221#ifdef PCMCIA_DEBUG
 222static int pc_debug = PCMCIA_DEBUG;
 223MODULE_PARM(pc_debug, "i");
 224#define DEBUG(n, args...) if (pc_debug>(n)) printk(KDBG_XIRC args)
 225#else
 226#define DEBUG(n, args...)
 227#endif
 228
 229#define KDBG_XIRC KERN_DEBUG   "xirc2ps_cs: "
 230#define KERR_XIRC KERN_ERR     "xirc2ps_cs: "
 231#define KWRN_XIRC KERN_WARNING "xirc2ps_cs: "
 232#define KNOT_XIRC KERN_NOTICE  "xirc2ps_cs: "
 233#define KINF_XIRC KERN_INFO    "xirc2ps_cs: "
 234
 235/* card types */
 236#define XIR_UNKNOWN  0  /* unknown: not supported */
 237#define XIR_CE       1  /* (prodid 1) different hardware: not supported */
 238#define XIR_CE2      2  /* (prodid 2) */
 239#define XIR_CE3      3  /* (prodid 3) */
 240#define XIR_CEM      4  /* (prodid 1) different hardware: not supported */
 241#define XIR_CEM2     5  /* (prodid 2) */
 242#define XIR_CEM3     6  /* (prodid 3) */
 243#define XIR_CEM33    7  /* (prodid 4) */
 244#define XIR_CEM56M   8  /* (prodid 5) */
 245#define XIR_CEM56    9  /* (prodid 6) */
 246#define XIR_CM28    10  /* (prodid 3) modem only: not supported here */
 247#define XIR_CM33    11  /* (prodid 4) modem only: not supported here */
 248#define XIR_CM56    12  /* (prodid 5) modem only: not supported here */
 249#define XIR_CG      13  /* (prodid 1) GSM modem only: not supported */
 250#define XIR_CBE     14  /* (prodid 1) cardbus ethernet: not supported */
 251/*====================================================================*/
 252
 253/* Module parameters */
 254
 255MODULE_DESCRIPTION("Xircom PCMCIA ethernet driver");
 256MODULE_LICENSE("Dual MPL/GPL");
 257
 258#define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
 259
 260static int irq_list[4] = { -1 };
 261MODULE_PARM(irq_list, "1-4i");
 262INT_MODULE_PARM(irq_mask,       0xdeb8);
 263INT_MODULE_PARM(if_port,        0);
 264INT_MODULE_PARM(full_duplex,    0);
 265INT_MODULE_PARM(do_sound,       1);
 266INT_MODULE_PARM(lockup_hack,    0);  /* anti lockup hack */
 267
 268/*====================================================================*/
 269
 270/* We do not process more than these number of bytes during one
 271 * interrupt. (Of course we receive complete packets, so this is not
 272 * an exact value).
 273 * Something between 2000..22000; first value gives best interrupt latency,
 274 * the second enables the usage of the complete on-chip buffer. We use the
 275 * high value as the initial value.
 276 */
 277static unsigned maxrx_bytes = 22000;
 278
 279/* MII management prototypes */
 280static void mii_idle(ioaddr_t ioaddr);
 281static void mii_putbit(ioaddr_t ioaddr, unsigned data);
 282static int  mii_getbit(ioaddr_t ioaddr);
 283static void mii_wbits(ioaddr_t ioaddr, unsigned data, int len);
 284static unsigned mii_rd(ioaddr_t ioaddr, u_char phyaddr, u_char phyreg);
 285static void mii_wr(ioaddr_t ioaddr, u_char phyaddr, u_char phyreg,
 286                   unsigned data, int len);
 287
 288/*
 289 * The event() function is this driver's Card Services event handler.
 290 * It will be called by Card Services when an appropriate card status
 291 * event is received.  The config() and release() entry points are
 292 * used to configure or release a socket, in response to card insertion
 293 * and ejection events.  They are invoked from the event handler.
 294 */
 295
 296static int has_ce2_string(dev_link_t * link);
 297static void xirc2ps_config(dev_link_t * link);
 298static void xirc2ps_release(dev_link_t * link);
 299static int xirc2ps_event(event_t event, int priority,
 300                         event_callback_args_t * args);
 301
 302/****************
 303 * The attach() and detach() entry points are used to create and destroy
 304 * "instances" of the driver, where each instance represents everything
 305 * needed to manage one actual PCMCIA card.
 306 */
 307
 308static dev_link_t *xirc2ps_attach(void);
 309static void xirc2ps_detach(dev_link_t *);
 310
 311/****************
 312 * You'll also need to prototype all the functions that will actually
 313 * be used to talk to your device.  See 'pcmem_cs' for a good example
 314 * of a fully self-sufficient driver; the other drivers rely more or
 315 * less on other parts of the kernel.
 316 */
 317
 318static irqreturn_t xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 319
 320/*
 321 * The dev_info variable is the "key" that is used to match up this
 322 * device driver with appropriate cards, through the card configuration
 323 * database.
 324 */
 325
 326static dev_info_t dev_info = "xirc2ps_cs";
 327
 328/****************
 329 * A linked list of "instances" of the device.  Each actual
 330 * PCMCIA card corresponds to one device instance, and is described
 331 * by one dev_link_t structure (defined in ds.h).
 332 *
 333 * You may not want to use a linked list for this -- for example, the
 334 * memory card driver uses an array of dev_link_t pointers, where minor
 335 * device numbers are used to derive the corresponding array index.
 336 */
 337
 338static dev_link_t *dev_list;
 339
 340/****************
 341 * A dev_link_t structure has fields for most things that are needed
 342 * to keep track of a socket, but there will usually be some device
 343 * specific information that also needs to be kept track of.  The
 344 * 'priv' pointer in a dev_link_t structure can be used to point to
 345 * a device-specific private data structure, like this.
 346 *
 347 * A driver needs to provide a dev_node_t structure for each device
 348 * on a card.  In some cases, there is only one device per card (for
 349 * example, ethernet cards, modems).  In other cases, there may be
 350 * many actual or logical devices (SCSI adapters, memory cards with
 351 * multiple partitions).  The dev_node_t structures need to be kept
 352 * in a linked list starting at the 'dev' field of a dev_link_t
 353 * structure.  We allocate them in the card's private data structure,
 354 * because they generally can't be allocated dynamically.
 355 */
 356
 357typedef struct local_info_t {
 358    dev_link_t link;
 359    dev_node_t node;
 360    struct net_device_stats stats;
 361    int card_type;
 362    int probe_port;
 363    int silicon; /* silicon revision. 0=old CE2, 1=Scipper, 4=Mohawk */
 364    int mohawk;  /* a CE3 type card */
 365    int dingo;   /* a CEM56 type card */
 366    int new_mii; /* has full 10baseT/100baseT MII */
 367    int modem;   /* is a multi function card (i.e with a modem) */
 368    caddr_t dingo_ccr; /* only used for CEM56 cards */
 369    unsigned last_ptr_value; /* last packets transmitted value */
 370    const char *manf_str;
 371} local_info_t;
 372
 373/****************
 374 * Some more prototypes
 375 */
 376static int do_start_xmit(struct sk_buff *skb, struct net_device *dev);
 377static void do_tx_timeout(struct net_device *dev);
 378static struct net_device_stats *do_get_stats(struct net_device *dev);
 379static void set_addresses(struct net_device *dev);
 380static void set_multicast_list(struct net_device *dev);
 381static int set_card_type(dev_link_t *link, const void *s);
 382static int do_config(struct net_device *dev, struct ifmap *map);
 383static int do_open(struct net_device *dev);
 384static int do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 385static struct ethtool_ops netdev_ethtool_ops;
 386static void hardreset(struct net_device *dev);
 387static void do_reset(struct net_device *dev, int full);
 388static int init_mii(struct net_device *dev);
 389static void do_powerdown(struct net_device *dev);
 390static int do_stop(struct net_device *dev);
 391
 392/*=============== Helper functions =========================*/
 393static int
 394get_tuple_data(int fn, client_handle_t handle, tuple_t *tuple)
 395{
 396    int err;
 397
 398    if ((err=CardServices(fn, handle, tuple)))
 399        return err;
 400    return CardServices(GetTupleData, handle, tuple);
 401}
 402
 403static int
 404get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
 405{
 406    int err;
 407
 408    if ((err=get_tuple_data(fn, handle, tuple)))
 409        return err;
 410    return CardServices(ParseTuple, handle, tuple, parse);
 411}
 412
 413#define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
 414#define next_tuple(a, b, c)  get_tuple(GetNextTuple, a, b, c)
 415
 416#define SelectPage(pgnr)   outb((pgnr), ioaddr + XIRCREG_PR)
 417#define GetByte(reg)       ((unsigned)inb(ioaddr + (reg)))
 418#define GetWord(reg)       ((unsigned)inw(ioaddr + (reg)))
 419#define PutByte(reg,value) outb((value), ioaddr+(reg))
 420#define PutWord(reg,value) outw((value), ioaddr+(reg))
 421
 422#define Wait(n) do { \
 423        set_current_state(TASK_UNINTERRUPTIBLE); \
 424        schedule_timeout(n); \
 425} while (0)
 426
 427/*====== Functions used for debugging =================================*/
 428#if defined(PCMCIA_DEBUG) && 0 /* reading regs may change system status */
 429static void
 430PrintRegisters(struct net_device *dev)
 431{
 432    ioaddr_t ioaddr = dev->base_addr;
 433
 434    if (pc_debug > 1) {
 435        int i, page;
 436
 437        printk(KDBG_XIRC "Register  common: ");
 438        for (i = 0; i < 8; i++)
 439            printk(" %2.2x", GetByte(i));
 440        printk("\n");
 441        for (page = 0; page <= 8; page++) {
 442            printk(KDBG_XIRC "Register page %2x: ", page);
 443            SelectPage(page);
 444            for (i = 8; i < 16; i++)
 445                printk(" %2.2x", GetByte(i));
 446            printk("\n");
 447        }
 448        for (page=0x40 ; page <= 0x5f; page++) {
 449            if (page == 0x43 || (page >= 0x46 && page <= 0x4f)
 450                || (page >= 0x51 && page <=0x5e))
 451                continue;
 452            printk(KDBG_XIRC "Register page %2x: ", page);
 453            SelectPage(page);
 454            for (i = 8; i < 16; i++)
 455                printk(" %2.2x", GetByte(i));
 456            printk("\n");
 457        }
 458    }
 459}
 460#endif /* PCMCIA_DEBUG */
 461
 462/*============== MII Management functions ===============*/
 463
 464/****************
 465 * Turn around for read
 466 */
 467static void
 468mii_idle(ioaddr_t ioaddr)
 469{
 470    PutByte(XIRCREG2_GPR2, 0x04|0); /* drive MDCK low */
 471    udelay(1);
 472    PutByte(XIRCREG2_GPR2, 0x04|1); /* and drive MDCK high */
 473    udelay(1);
 474}
 475
 476/****************
 477 * Write a bit to MDI/O
 478 */
 479static void
 480mii_putbit(ioaddr_t ioaddr, unsigned data)
 481{
 482  #if 1
 483    if (data) {
 484        PutByte(XIRCREG2_GPR2, 0x0c|2|0); /* set MDIO */
 485        udelay(1);
 486        PutByte(XIRCREG2_GPR2, 0x0c|2|1); /* and drive MDCK high */
 487        udelay(1);
 488    } else {
 489        PutByte(XIRCREG2_GPR2, 0x0c|0|0); /* clear MDIO */
 490        udelay(1);
 491        PutByte(XIRCREG2_GPR2, 0x0c|0|1); /* and drive MDCK high */
 492        udelay(1);
 493    }
 494  #else
 495    if (data) {
 496        PutWord(XIRCREG2_GPR2-1, 0x0e0e);
 497        udelay(1);
 498        PutWord(XIRCREG2_GPR2-1, 0x0f0f);
 499        udelay(1);
 500    } else {
 501        PutWord(XIRCREG2_GPR2-1, 0x0c0c);
 502        udelay(1);
 503        PutWord(XIRCREG2_GPR2-1, 0x0d0d);
 504        udelay(1);
 505    }
 506  #endif
 507}
 508
 509/****************
 510 * Get a bit from MDI/O
 511 */
 512static int
 513mii_getbit(ioaddr_t ioaddr)
 514{
 515    unsigned d;
 516
 517    PutByte(XIRCREG2_GPR2, 4|0); /* drive MDCK low */
 518    udelay(1);
 519    d = GetByte(XIRCREG2_GPR2); /* read MDIO */
 520    PutByte(XIRCREG2_GPR2, 4|1); /* drive MDCK high again */
 521    udelay(1);
 522    return d & 0x20; /* read MDIO */
 523}
 524
 525static void
 526mii_wbits(ioaddr_t ioaddr, unsigned data, int len)
 527{
 528    unsigned m = 1 << (len-1);
 529    for (; m; m >>= 1)
 530        mii_putbit(ioaddr, data & m);
 531}
 532
 533static unsigned
 534mii_rd(ioaddr_t ioaddr, u_char phyaddr, u_char phyreg)
 535{
 536    int i;
 537    unsigned data=0, m;
 538
 539    SelectPage(2);
 540    for (i=0; i < 32; i++)              /* 32 bit preamble */
 541        mii_putbit(ioaddr, 1);
 542    mii_wbits(ioaddr, 0x06, 4);         /* Start and opcode for read */
 543    mii_wbits(ioaddr, phyaddr, 5);      /* PHY address to be accessed */
 544    mii_wbits(ioaddr, phyreg, 5);       /* PHY register to read */
 545    mii_idle(ioaddr);                   /* turn around */
 546    mii_getbit(ioaddr);
 547
 548    for (m = 1<<15; m; m >>= 1)
 549        if (mii_getbit(ioaddr))
 550            data |= m;
 551    mii_idle(ioaddr);
 552    return data;
 553}
 554
 555static void
 556mii_wr(ioaddr_t ioaddr, u_char phyaddr, u_char phyreg, unsigned data, int len)
 557{
 558    int i;
 559
 560    SelectPage(2);
 561    for (i=0; i < 32; i++)              /* 32 bit preamble */
 562        mii_putbit(ioaddr, 1);
 563    mii_wbits(ioaddr, 0x05, 4);         /* Start and opcode for write */
 564    mii_wbits(ioaddr, phyaddr, 5);      /* PHY address to be accessed */
 565    mii_wbits(ioaddr, phyreg, 5);       /* PHY Register to write */
 566    mii_putbit(ioaddr, 1);              /* turn around */
 567    mii_putbit(ioaddr, 0);
 568    mii_wbits(ioaddr, data, len);       /* And write the data */
 569    mii_idle(ioaddr);
 570}
 571
 572/*============= Main bulk of functions  =========================*/
 573
 574/****************
 575 * xirc2ps_attach() creates an "instance" of the driver, allocating
 576 * local data structures for one device.  The device is registered
 577 * with Card Services.
 578 *
 579 * The dev_link structure is initialized, but we don't actually
 580 * configure the card at this point -- we wait until we receive a
 581 * card insertion event.
 582 */
 583
 584static dev_link_t *
 585xirc2ps_attach(void)
 586{
 587    client_reg_t client_reg;
 588    dev_link_t *link;
 589    struct net_device *dev;
 590    local_info_t *local;
 591    int err;
 592
 593    DEBUG(0, "attach()\n");
 594
 595    /* Allocate the device structure */
 596    dev = alloc_etherdev(sizeof(local_info_t));
 597    if (!dev)
 598            return NULL;
 599    local = dev->priv;
 600    link = &local->link;
 601    link->priv = dev;
 602
 603    /* General socket configuration */
 604    link->conf.Attributes = CONF_ENABLE_IRQ;
 605    link->conf.Vcc = 50;
 606    link->conf.IntType = INT_MEMORY_AND_IO;
 607    link->conf.ConfigIndex = 1;
 608    link->conf.Present = PRESENT_OPTION;
 609    link->irq.Handler = xirc2ps_interrupt;
 610    link->irq.Instance = dev;
 611
 612    /* Fill in card specific entries */
 613    SET_MODULE_OWNER(dev);
 614    dev->hard_start_xmit = &do_start_xmit;
 615    dev->set_config = &do_config;
 616    dev->get_stats = &do_get_stats;
 617    dev->do_ioctl = &do_ioctl;
 618    SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
 619    dev->set_multicast_list = &set_multicast_list;
 620    dev->open = &do_open;
 621    dev->stop = &do_stop;
 622#ifdef HAVE_TX_TIMEOUT
 623    dev->tx_timeout = do_tx_timeout;
 624    dev->watchdog_timeo = TX_TIMEOUT;
 625#endif
 626
 627    /* Register with Card Services */
 628    link->next = dev_list;
 629    dev_list = link;
 630    client_reg.dev_info = &dev_info;
 631    client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
 632    client_reg.EventMask =
 633        CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
 634        CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
 635        CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
 636    client_reg.event_handler = &xirc2ps_event;
 637    client_reg.Version = 0x0210;
 638    client_reg.event_callback_args.client_data = link;
 639    if ((err = CardServices(RegisterClient, &link->handle, &client_reg))) {
 640        cs_error(link->handle, RegisterClient, err);
 641        xirc2ps_detach(link);
 642        return NULL;
 643    }
 644
 645    return link;
 646} /* xirc2ps_attach */
 647
 648/****************
 649 *  This deletes a driver "instance".  The device is de-registered
 650 *  with Card Services.  If it has been released, all local data
 651 *  structures are freed.  Otherwise, the structures will be freed
 652 *  when the device is released.
 653 */
 654
 655static void
 656xirc2ps_detach(dev_link_t * link)
 657{
 658    struct net_device *dev = link->priv;
 659    dev_link_t **linkp;
 660
 661    DEBUG(0, "detach(0x%p)\n", link);
 662
 663    /* Locate device structure */
 664    for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
 665        if (*linkp == link)
 666            break;
 667    if (!*linkp) {
 668        DEBUG(0, "detach(0x%p): dev_link lost\n", link);
 669        return;
 670    }
 671
 672    /*
 673     * If the device is currently configured and active, we won't
 674     * actually delete it yet.  Instead, it is marked so that when
 675     * the release() function is called, that will trigger a proper
 676     * detach().
 677     */
 678    if (link->state & DEV_CONFIG)
 679        xirc2ps_release(link);
 680
 681    /* Break the link with Card Services */
 682    if (link->handle)
 683        CardServices(DeregisterClient, link->handle);
 684
 685    /* Unlink device structure, free it */
 686    *linkp = link->next;
 687    if (link->dev) {
 688        unregister_netdev(dev);
 689        free_netdev(dev);
 690    } else
 691        kfree(dev);
 692
 693} /* xirc2ps_detach */
 694
 695/****************
 696 * Detect the type of the card. s is the buffer with the data of tuple 0x20
 697 * Returns: 0 := not supported
 698 *                     mediaid=11 and prodid=47
 699 * Media-Id bits:
 700 *  Ethernet        0x01
 701 *  Tokenring       0x02
 702 *  Arcnet          0x04
 703 *  Wireless        0x08
 704 *  Modem           0x10
 705 *  GSM only        0x20
 706 * Prod-Id bits:
 707 *  Pocket          0x10
 708 *  External        0x20
 709 *  Creditcard      0x40
 710 *  Cardbus         0x80
 711 *
 712 */
 713static int
 714set_card_type(dev_link_t *link, const void *s)
 715{
 716    struct net_device *dev = link->priv;
 717    local_info_t *local = dev->priv;
 718  #ifdef PCMCIA_DEBUG
 719    unsigned cisrev = ((const unsigned char *)s)[2];
 720  #endif
 721    unsigned mediaid= ((const unsigned char *)s)[3];
 722    unsigned prodid = ((const unsigned char *)s)[4];
 723
 724    DEBUG(0, "cisrev=%02x mediaid=%02x prodid=%02x\n",
 725          cisrev, mediaid, prodid);
 726
 727    local->mohawk = 0;
 728    local->dingo = 0;
 729    local->modem = 0;
 730    local->card_type = XIR_UNKNOWN;
 731    if (!(prodid & 0x40)) {
 732        printk(KNOT_XIRC "Ooops: Not a creditcard\n");
 733        return 0;
 734    }
 735    if (!(mediaid & 0x01)) {
 736        printk(KNOT_XIRC "Not an Ethernet card\n");
 737        return 0;
 738    }
 739    if (mediaid & 0x10) {
 740        local->modem = 1;
 741        switch(prodid & 15) {
 742          case 1: local->card_type = XIR_CEM   ; break;
 743          case 2: local->card_type = XIR_CEM2  ; break;
 744          case 3: local->card_type = XIR_CEM3  ; break;
 745          case 4: local->card_type = XIR_CEM33 ; break;
 746          case 5: local->card_type = XIR_CEM56M;
 747                  local->mohawk = 1;
 748                  break;
 749          case 6:
 750          case 7: /* 7 is the RealPort 10/56 */
 751                  local->card_type = XIR_CEM56 ;
 752                  local->mohawk = 1;
 753                  local->dingo = 1;
 754                  break;
 755        }
 756    } else {
 757        switch(prodid & 15) {
 758          case 1: local->card_type = has_ce2_string(link)? XIR_CE2 : XIR_CE ;
 759                  break;
 760          case 2: local->card_type = XIR_CE2; break;
 761          case 3: local->card_type = XIR_CE3;
 762                  local->mohawk = 1;
 763                  break;
 764        }
 765    }
 766    if (local->card_type == XIR_CE || local->card_type == XIR_CEM) {
 767        printk(KNOT_XIRC "Sorry, this is an old CE card\n");
 768        return 0;
 769    }
 770    if (local->card_type == XIR_UNKNOWN)
 771        printk(KNOT_XIRC "unknown card (mediaid=%02x prodid=%02x)\n",
 772               mediaid, prodid);
 773
 774    return 1;
 775}
 776
 777/****************
 778 * There are some CE2 cards out which claim to be a CE card.
 779 * This function looks for a "CE2" in the 3rd version field.
 780 * Returns: true if this is a CE2
 781 */
 782static int
 783has_ce2_string(dev_link_t * link)
 784{
 785    client_handle_t handle = link->handle;
 786    tuple_t tuple;
 787    cisparse_t parse;
 788    u_char buf[256];
 789
 790    tuple.Attributes = 0;
 791    tuple.TupleData = buf;
 792    tuple.TupleDataMax = 254;
 793    tuple.TupleOffset = 0;
 794    tuple.DesiredTuple = CISTPL_VERS_1;
 795    if (!first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 2) {
 796        if (strstr(parse.version_1.str + parse.version_1.ofs[2], "CE2"))
 797            return 1;
 798    }
 799    return 0;
 800}
 801
 802/****************
 803 * xirc2ps_config() is scheduled to run after a CARD_INSERTION event
 804 * is received, to configure the PCMCIA socket, and to make the
 805 * ethernet device available to the system.
 806 */
 807static void
 808xirc2ps_config(dev_link_t * link)
 809{
 810    client_handle_t handle = link->handle;
 811    struct net_device *dev = link->priv;
 812    local_info_t *local = dev->priv;
 813    tuple_t tuple;
 814    cisparse_t parse;
 815    ioaddr_t ioaddr;
 816    int err, i;
 817    u_char buf[64];
 818    cistpl_lan_node_id_t *node_id = (cistpl_lan_node_id_t*)parse.funce.data;
 819    cistpl_cftable_entry_t *cf = &parse.cftable_entry;
 820
 821    local->dingo_ccr = 0;
 822
 823    DEBUG(0, "config(0x%p)\n", link);
 824
 825    /*
 826     * This reads the card's CONFIG tuple to find its configuration
 827     * registers.
 828     */
 829    tuple.Attributes = 0;
 830    tuple.TupleData = buf;
 831    tuple.TupleDataMax = 64;
 832    tuple.TupleOffset = 0;
 833
 834    /* Is this a valid  card */
 835    tuple.DesiredTuple = CISTPL_MANFID;
 836    if ((err=first_tuple(handle, &tuple, &parse))) {
 837        printk(KNOT_XIRC "manfid not found in CIS\n");
 838        goto failure;
 839    }
 840
 841    switch(parse.manfid.manf) {
 842      case MANFID_XIRCOM:
 843        local->manf_str = "Xircom";
 844        break;
 845      case MANFID_ACCTON:
 846        local->manf_str = "Accton";
 847        break;
 848      case MANFID_COMPAQ:
 849      case MANFID_COMPAQ2:
 850        local->manf_str = "Compaq";
 851        break;
 852      case MANFID_INTEL:
 853        local->manf_str = "Intel";
 854        break;
 855      case MANFID_TOSHIBA:
 856        local->manf_str = "Toshiba";
 857        break;
 858      default:
 859        printk(KNOT_XIRC "Unknown Card Manufacturer ID: 0x%04x\n",
 860               (unsigned)parse.manfid.manf);
 861        goto failure;
 862    }
 863    DEBUG(0, "found %s card\n", local->manf_str);
 864
 865    if (!set_card_type(link, buf)) {
 866        printk(KNOT_XIRC "this card is not supported\n");
 867        goto failure;
 868    }
 869
 870    /* get configuration stuff */
 871    tuple.DesiredTuple = CISTPL_CONFIG;
 872    if ((err=first_tuple(handle, &tuple, &parse)))
 873        goto cis_error;
 874    link->conf.ConfigBase = parse.config.base;
 875    link->conf.Present =    parse.config.rmask[0];
 876
 877    /* get the ethernet address from the CIS */
 878    tuple.DesiredTuple = CISTPL_FUNCE;
 879    for (err = first_tuple(handle, &tuple, &parse); !err;
 880                             err = next_tuple(handle, &tuple, &parse)) {
 881        /* Once I saw two CISTPL_FUNCE_LAN_NODE_ID entries:
 882         * the first one with a length of zero the second correct -
 883         * so I skip all entries with length 0 */
 884        if (parse.funce.type == CISTPL_FUNCE_LAN_NODE_ID
 885            && ((cistpl_lan_node_id_t *)parse.funce.data)->nb)
 886            break;
 887    }
 888    if (err) { /* not found: try to get the node-id from tuple 0x89 */
 889        tuple.DesiredTuple = 0x89;  /* data layout looks like tuple 0x22 */
 890        if (!(err = get_tuple_data(GetFirstTuple, handle, &tuple))) {
 891            if (tuple.TupleDataLen == 8 && *buf == CISTPL_FUNCE_LAN_NODE_ID)
 892                memcpy(&parse, buf, 8);
 893            else
 894                err = -1;
 895        }
 896    }
 897    if (err) { /* another try   (James Lehmer's CE2 version 4.1)*/
 898        tuple.DesiredTuple = CISTPL_FUNCE;
 899        for (err = first_tuple(handle, &tuple, &parse); !err;
 900                                 err = next_tuple(handle, &tuple, &parse)) {
 901            if (parse.funce.type == 0x02 && parse.funce.data[0] == 1
 902                && parse.funce.data[1] == 6 && tuple.TupleDataLen == 13) {
 903                buf[1] = 4;
 904                memcpy(&parse, buf+1, 8);
 905                break;
 906            }
 907        }
 908    }
 909    if (err) {
 910        printk(KNOT_XIRC "node-id not found in CIS\n");
 911        goto failure;
 912    }
 913    node_id = (cistpl_lan_node_id_t *)parse.funce.data;
 914    if (node_id->nb != 6) {
 915        printk(KNOT_XIRC "malformed node-id in CIS\n");
 916        goto failure;
 917    }
 918    for (i=0; i < 6; i++)
 919        dev->dev_addr[i] = node_id->id[i];
 920
 921    /* Configure card */
 922    link->state |= DEV_CONFIG;
 923
 924    link->io.IOAddrLines =10;
 925    link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
 926    link->irq.Attributes = IRQ_HANDLE_PRESENT;
 927    link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
 928    if (irq_list[0] == -1)
 929        link->irq.IRQInfo2 = irq_mask;
 930    else {
 931        for (i = 0; i < 4; i++)
 932            link->irq.IRQInfo2 |= 1 << irq_list[i];
 933    }
 934    if (local->modem) {
 935        int pass;
 936
 937        if (do_sound) {
 938            link->conf.Attributes |= CONF_ENABLE_SPKR;
 939            link->conf.Status |= CCSR_AUDIO_ENA;
 940        }
 941        link->irq.Attributes |= IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED ;
 942        link->io.NumPorts2 = 8;
 943        link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
 944        if (local->dingo) {
 945            /* Take the Modem IO port from the CIS and scan for a free
 946             * Ethernet port */
 947            link->io.NumPorts1 = 16; /* no Mako stuff anymore */
 948            tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
 949            for (err = first_tuple(handle, &tuple, &parse); !err;
 950                                 err = next_tuple(handle, &tuple, &parse)) {
 951                if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8) {
 952                    for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
 953                        link->conf.ConfigIndex = cf->index ;
 954                        link->io.BasePort2 = cf->io.win[0].base;
 955                        link->io.BasePort1 = ioaddr;
 956                        if (!(err=CardServices(RequestIO, link->handle,
 957                                                                &link->io)))
 958                            goto port_found;
 959                    }
 960                }
 961            }
 962        } else {
 963            link->io.NumPorts1 = 18;
 964            /* We do 2 passes here: The first one uses the regular mapping and
 965             * the second tries again, thereby considering that the 32 ports are
 966             * mirrored every 32 bytes. Actually we use a mirrored port for
 967             * the Mako if (on the first pass) the COR bit 5 is set.
 968             */
 969            for (pass=0; pass < 2; pass++) {
 970                tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
 971                for (err = first_tuple(handle, &tuple, &parse); !err;
 972                                     err = next_tuple(handle, &tuple, &parse)){
 973                    if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8){
 974                        link->conf.ConfigIndex = cf->index ;
 975                        link->io.BasePort2 = cf->io.win[0].base;
 976                        link->io.BasePort1 = link->io.BasePort2
 977                                    + (pass ? (cf->index & 0x20 ? -24:8)
 978                                            : (cf->index & 0x20 ?   8:-24));
 979                        if (!(err=CardServices(RequestIO, link->handle,
 980                                                                &link->io)))
 981                            goto port_found;
 982                    }
 983                }
 984            }
 985            /* if special option:
 986             * try to configure as Ethernet only.
 987             * .... */
 988        }
 989        printk(KNOT_XIRC "no ports available\n");
 990    } else {
 991        link->irq.Attributes |= IRQ_TYPE_EXCLUSIVE;
 992        link->io.NumPorts1 = 16;
 993        for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
 994            link->io.BasePort1 = ioaddr;
 995            if (!(err=CardServices(RequestIO, link->handle, &link->io)))
 996                goto port_found;
 997        }
 998        link->io.BasePort1 = 0; /* let CS decide */
 999        if ((err=CardServices(RequestIO, link->handle, &link->io))) {
1000            cs_error(link->handle, RequestIO, err);
1001            goto config_error;
1002        }
1003    }
1004  port_found:
1005    if (err)
1006         goto config_error;
1007
1008    /****************
1009     * Now allocate an interrupt line.  Note that this does not
1010     * actually assign a handler to the interrupt.
1011     */
1012    if ((err=CardServices(RequestIRQ, link->handle, &link->irq))) {
1013        cs_error(link->handle, RequestIRQ, err);
1014        goto config_error;
1015    }
1016
1017    /****************
1018     * This actually configures the PCMCIA socket -- setting up
1019     * the I/O windows and the interrupt mapping.
1020     */
1021    if ((err=CardServices(RequestConfiguration,
1022                          link->handle, &link->conf))) {
1023        cs_error(link->handle, RequestConfiguration, err);
1024        goto config_error;
1025    }
1026
1027    if (local->dingo) {
1028        conf_reg_t reg;
1029        win_req_t req;
1030        memreq_t mem;
1031
1032        /* Reset the modem's BAR to the correct value
1033         * This is necessary because in the RequestConfiguration call,
1034         * the base address of the ethernet port (BasePort1) is written
1035         * to the BAR registers of the modem.
1036         */
1037        reg.Action = CS_WRITE;
1038        reg.Offset = CISREG_IOBASE_0;
1039        reg.Value = link->io.BasePort2 & 0xff;
1040        if ((err = CardServices(AccessConfigurationRegister, link->handle,
1041                                &reg))) {
1042            cs_error(link->handle, AccessConfigurationRegister, err);
1043            goto config_error;
1044        }
1045        reg.Action = CS_WRITE;
1046        reg.Offset = CISREG_IOBASE_1;
1047        reg.Value = (link->io.BasePort2 >> 8) & 0xff;
1048        if ((err = CardServices(AccessConfigurationRegister, link->handle,
1049                                &reg))) {
1050            cs_error(link->handle, AccessConfigurationRegister, err);
1051            goto config_error;
1052        }
1053
1054        /* There is no config entry for the Ethernet part which
1055         * is at 0x0800. So we allocate a window into the attribute
1056         * memory and write direct to the CIS registers
1057         */
1058        req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
1059        req.Base = req.Size = 0;
1060        req.AccessSpeed = 0;
1061        link->win = (window_handle_t)link->handle;
1062        if ((err = CardServices(RequestWindow, &link->win, &req))) {
1063            cs_error(link->handle, RequestWindow, err);
1064            goto config_error;
1065        }
1066        local->dingo_ccr = ioremap(req.Base,0x1000) + 0x0800;
1067        mem.CardOffset = 0x0;
1068        mem.Page = 0;
1069        if ((err = CardServices(MapMemPage, link->win, &mem))) {
1070            cs_error(link->handle, MapMemPage, err);
1071            goto config_error;
1072        }
1073
1074        /* Setup the CCRs; there are no infos in the CIS about the Ethernet
1075         * part.
1076         */
1077        writeb(0x47, local->dingo_ccr + CISREG_COR);
1078        ioaddr = link->io.BasePort1;
1079        writeb(ioaddr & 0xff      , local->dingo_ccr + CISREG_IOBASE_0);
1080        writeb((ioaddr >> 8)&0xff , local->dingo_ccr + CISREG_IOBASE_1);
1081
1082      #if 0
1083        {
1084            u_char tmp;
1085            printk(KERN_INFO "ECOR:");
1086            for (i=0; i < 7; i++) {
1087                tmp = readb(local->dingo_ccr + i*2);
1088                printk(" %02x", tmp);
1089            }
1090            printk("\n");
1091            printk(KERN_INFO "DCOR:");
1092            for (i=0; i < 4; i++) {
1093                tmp = readb(local->dingo_ccr + 0x20 + i*2);
1094                printk(" %02x", tmp);
1095            }
1096            printk("\n");
1097            printk(KERN_INFO "SCOR:");
1098            for (i=0; i < 10; i++) {
1099                tmp = readb(local->dingo_ccr + 0x40 + i*2);
1100                printk(" %02x", tmp);
1101            }
1102            printk("\n");
1103        }
1104      #endif
1105
1106        writeb(0x01, local->dingo_ccr + 0x20);
1107        writeb(0x0c, local->dingo_ccr + 0x22);
1108        writeb(0x00, local->dingo_ccr + 0x24);
1109        writeb(0x00, local->dingo_ccr + 0x26);
1110        writeb(0x00, local->dingo_ccr + 0x28);
1111    }
1112
1113    /* The if_port symbol can be set when the module is loaded */
1114    local->probe_port=0;
1115    if (!if_port) {
1116        local->probe_port = dev->if_port = 1;
1117    } else if ((if_port >= 1 && if_port <= 2) ||
1118               (local->mohawk && if_port==4))
1119        dev->if_port = if_port;
1120    else
1121        printk(KNOT_XIRC "invalid if_port requested\n");
1122
1123    /* we can now register the device with the net subsystem */
1124    dev->irq = link->irq.AssignedIRQ;
1125    dev->base_addr = link->io.BasePort1;
1126    if ((err=register_netdev(dev))) {
1127        printk(KNOT_XIRC "register_netdev() failed\n");
1128        goto config_error;
1129    }
1130
1131    strcpy(local->node.dev_name, dev->name);
1132    link->dev = &local->node;
1133    link->state &= ~DEV_CONFIG_PENDING;
1134
1135    if (local->dingo)
1136        do_reset(dev, 1); /* a kludge to make the cem56 work */
1137
1138    /* give some infos about the hardware */
1139    printk(KERN_INFO "%s: %s: port %#3lx, irq %d, hwaddr",
1140         dev->name, local->manf_str,(u_long)dev->base_addr, (int)dev->irq);
1141    for (i = 0; i < 6; i++)
1142        printk("%c%02X", i?':':' ', dev->dev_addr[i]);
1143    printk("\n");
1144
1145    return;
1146
1147  config_error:
1148    link->state &= ~DEV_CONFIG_PENDING;
1149    xirc2ps_release(link);
1150    return;
1151
1152  cis_error:
1153    printk(KNOT_XIRC "unable to parse CIS\n");
1154  failure:
1155    link->state &= ~DEV_CONFIG_PENDING;
1156} /* xirc2ps_config */
1157
1158/****************
1159 * After a card is removed, xirc2ps_release() will unregister the net
1160 * device, and release the PCMCIA configuration.  If the device is
1161 * still open, this will be postponed until it is closed.
1162 */
1163static void
1164xirc2ps_release(dev_link_t *link)
1165{
1166
1167    DEBUG(0, "release(0x%p)\n", link);
1168
1169    if (link->win) {
1170        struct net_device *dev = link->priv;
1171        local_info_t *local = dev->priv;
1172        if (local->dingo)
1173            iounmap(local->dingo_ccr - 0x0800);
1174        CardServices(ReleaseWindow, link->win);
1175    }
1176    CardServices(ReleaseConfiguration, link->handle);
1177    CardServices(ReleaseIO, link->handle, &link->io);
1178    CardServices(ReleaseIRQ, link->handle, &link->irq);
1179    link->state &= ~DEV_CONFIG;
1180
1181} /* xirc2ps_release */
1182
1183/*====================================================================*/
1184
1185/****************
1186 * The card status event handler.  Mostly, this schedules other
1187 * stuff to run after an event is received.  A CARD_REMOVAL event
1188 * also sets some flags to discourage the net drivers from trying
1189 * to talk to the card any more.
1190 *
1191 * When a CARD_REMOVAL event is received, we immediately set a flag
1192 * to block future accesses to this device.  All the functions that
1193 * actually access the device should check this flag to make sure
1194 * the card is still present.
1195 */
1196
1197static int
1198xirc2ps_event(event_t event, int priority,
1199              event_callback_args_t * args)
1200{
1201    dev_link_t *link = args->client_data;
1202    struct net_device *dev = link->priv;
1203
1204    DEBUG(0, "event(%d)\n", (int)event);
1205
1206    switch (event) {
1207    case CS_EVENT_REGISTRATION_COMPLETE:
1208        DEBUG(0, "registration complete\n");
1209        break;
1210    case CS_EVENT_CARD_REMOVAL:
1211        link->state &= ~DEV_PRESENT;
1212        if (link->state & DEV_CONFIG) {
1213            netif_device_detach(dev);
1214            xirc2ps_release(link);
1215        }
1216        break;
1217    case CS_EVENT_CARD_INSERTION:
1218        link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1219        xirc2ps_config(link);
1220        break;
1221    case CS_EVENT_PM_SUSPEND:
1222        link->state |= DEV_SUSPEND;
1223        /* Fall through... */
1224    case CS_EVENT_RESET_PHYSICAL:
1225        if (link->state & DEV_CONFIG) {
1226            if (link->open) {
1227                netif_device_detach(dev);
1228                do_powerdown(dev);
1229            }
1230            CardServices(ReleaseConfiguration, link->handle);
1231        }
1232        break;
1233    case CS_EVENT_PM_RESUME:
1234        link->state &= ~DEV_SUSPEND;
1235        /* Fall through... */
1236    case CS_EVENT_CARD_RESET:
1237        if (link->state & DEV_CONFIG) {
1238            CardServices(RequestConfiguration, link->handle, &link->conf);
1239            if (link->open) {
1240                do_reset(dev,1);
1241                netif_device_attach(dev);
1242            }
1243        }
1244        break;
1245    }
1246    return 0;
1247} /* xirc2ps_event */
1248
1249/*====================================================================*/
1250
1251/****************
1252 * This is the Interrupt service route.
1253 */
1254static irqreturn_t
1255xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1256{
1257    struct net_device *dev = (struct net_device *)dev_id;
1258    local_info_t *lp = dev->priv;
1259    ioaddr_t ioaddr;
1260    u_char saved_page;
1261    unsigned bytes_rcvd;
1262    unsigned int_status, eth_status, rx_status, tx_status;
1263    unsigned rsr, pktlen;
1264    ulong start_ticks = jiffies; /* fixme: jiffies rollover every 497 days
1265                                  * is this something to worry about?
1266                                  * -- on a laptop?
1267                                  */
1268
1269    if (!netif_device_present(dev))
1270        return IRQ_HANDLED;
1271
1272    ioaddr = dev->base_addr;
1273    if (lp->mohawk) { /* must disable the interrupt */
1274        PutByte(XIRCREG_CR, 0);
1275    }
1276
1277    DEBUG(6, "%s: interrupt %d at %#x.\n", dev->name, irq, ioaddr);
1278
1279    saved_page = GetByte(XIRCREG_PR);
1280    /* Read the ISR to see whats the cause for the interrupt.
1281     * This also clears the interrupt flags on CE2 cards
1282     */
1283    int_status = GetByte(XIRCREG_ISR);
1284    bytes_rcvd = 0;
1285  loop_entry:
1286    if (int_status == 0xff) { /* card may be ejected */
1287        DEBUG(3, "%s: interrupt %d for dead card\n", dev->name, irq);
1288        goto leave;
1289    }
1290    eth_status = GetByte(XIRCREG_ESR);
1291
1292    SelectPage(0x40);
1293    rx_status  = GetByte(XIRCREG40_RXST0);
1294    PutByte(XIRCREG40_RXST0, (~rx_status & 0xff));
1295    tx_status = GetByte(XIRCREG40_TXST0);
1296    tx_status |= GetByte(XIRCREG40_TXST1) << 8;
1297    PutByte(XIRCREG40_TXST0, 0);
1298    PutByte(XIRCREG40_TXST1, 0);
1299
1300    DEBUG(3, "%s: ISR=%#2.2x ESR=%#2.2x RSR=%#2.2x TSR=%#4.4x\n",
1301          dev->name, int_status, eth_status, rx_status, tx_status);
1302
1303    /***** receive section ******/
1304    SelectPage(0);
1305    while (eth_status & FullPktRcvd) {
1306        rsr = GetByte(XIRCREG0_RSR);
1307        if (bytes_rcvd > maxrx_bytes && (rsr & PktRxOk)) {
1308            /* too many bytes received during this int, drop the rest of the
1309             * packets */
1310            lp->stats.rx_dropped++;
1311            DEBUG(2, "%s: RX drop, too much done\n", dev->name);
1312        } else if (rsr & PktRxOk) {
1313            struct sk_buff *skb;
1314
1315            pktlen = GetWord(XIRCREG0_RBC);
1316            bytes_rcvd += pktlen;
1317
1318            DEBUG(5, "rsr=%#02x packet_length=%u\n", rsr, pktlen);
1319
1320            skb = dev_alloc_skb(pktlen+3); /* 1 extra so we can use insw */
1321            if (!skb) {
1322                printk(KNOT_XIRC "low memory, packet dropped (size=%u)\n",
1323                       pktlen);
1324                lp->stats.rx_dropped++;
1325            } else { /* okay get the packet */
1326                skb_reserve(skb, 2);
1327                if (lp->silicon == 0 ) { /* work around a hardware bug */
1328                    unsigned rhsa; /* receive start address */
1329
1330                    SelectPage(5);
1331                    rhsa = GetWord(XIRCREG5_RHSA0);
1332                    SelectPage(0);
1333                    rhsa += 3; /* skip control infos */
1334                    if (rhsa >= 0x8000)
1335                        rhsa = 0;
1336                    if (rhsa + pktlen > 0x8000) {
1337                        unsigned i;
1338                        u_char *buf = skb_put(skb, pktlen);
1339                        for (i=0; i < pktlen ; i++, rhsa++) {
1340                            buf[i] = GetByte(XIRCREG_EDP);
1341                            if (rhsa == 0x8000) {
1342                                rhsa = 0;
1343                                i--;
1344                            }
1345                        }
1346                    } else {
1347                        insw(ioaddr+XIRCREG_EDP,
1348                                skb_put(skb, pktlen), (pktlen+1)>>1);
1349                    }
1350                }
1351              #if 0
1352                else if (lp->mohawk) {
1353                    /* To use this 32 bit access we should use
1354                     * a manual optimized loop
1355                     * Also the words are swapped, we can get more
1356                     * performance by using 32 bit access and swapping
1357                     * the words in a register. Will need this for cardbus
1358                     *
1359                     * Note: don't forget to change the ALLOC_SKB to .. +3
1360                     */
1361                    unsigned i;
1362                    u_long *p = skb_put(skb, pktlen);
1363                    register u_long a;
1364                    ioaddr_t edpreg = ioaddr+XIRCREG_EDP-2;
1365                    for (i=0; i < len ; i += 4, p++) {
1366                        a = inl(edpreg);
1367                        __asm__("rorl $16,%0\n\t"
1368                                :"=q" (a)
1369                                : "0" (a));
1370                        *p = a;
1371                    }
1372                }
1373              #endif
1374                else {
1375                    insw(ioaddr+XIRCREG_EDP, skb_put(skb, pktlen),
1376                            (pktlen+1)>>1);
1377                }
1378                skb->protocol = eth_type_trans(skb, dev);
1379                skb->dev = dev;
1380                netif_rx(skb);
1381                dev->last_rx = jiffies;
1382                lp->stats.rx_packets++;
1383                lp->stats.rx_bytes += pktlen;
1384                if (!(rsr & PhyPkt))
1385                    lp->stats.multicast++;
1386            }
1387        } else { /* bad packet */
1388            DEBUG(5, "rsr=%#02x\n", rsr);
1389        }
1390        if (rsr & PktTooLong) {
1391            lp->stats.rx_frame_errors++;
1392            DEBUG(3, "%s: Packet too long\n", dev->name);
1393        }
1394        if (rsr & CRCErr) {
1395            lp->stats.rx_crc_errors++;
1396            DEBUG(3, "%s: CRC error\n", dev->name);
1397        }
1398        if (rsr & AlignErr) {
1399            lp->stats.rx_fifo_errors++; /* okay ? */
1400            DEBUG(3, "%s: Alignment error\n", dev->name);
1401        }
1402
1403        /* clear the received/dropped/error packet */
1404        PutWord(XIRCREG0_DO, 0x8000); /* issue cmd: skip_rx_packet */
1405
1406        /* get the new ethernet status */
1407        eth_status = GetByte(XIRCREG_ESR);
1408    }
1409    if (rx_status & 0x10) { /* Receive overrun */
1410        lp->stats.rx_over_errors++;
1411        PutByte(XIRCREG_CR, ClearRxOvrun);
1412        DEBUG(3, "receive overrun cleared\n");
1413    }
1414
1415    /***** transmit section ******/
1416    if (int_status & PktTxed) {
1417        unsigned n, nn;
1418
1419        n = lp->last_ptr_value;
1420        nn = GetByte(XIRCREG0_PTR);
1421        lp->last_ptr_value = nn;
1422        if (nn < n) /* rollover */
1423            lp->stats.tx_packets += 256 - n;
1424        else if (n == nn) { /* happens sometimes - don't know why */
1425            DEBUG(0, "PTR not changed?\n");
1426        } else
1427            lp->stats.tx_packets += lp->last_ptr_value - n;
1428        netif_wake_queue(dev);
1429    }
1430    if (tx_status & 0x0002) {   /* Execessive collissions */
1431        DEBUG(0, "tx restarted due to execssive collissions\n");
1432        PutByte(XIRCREG_CR, RestartTx);  /* restart transmitter process */
1433    }
1434    if (tx_status & 0x0040)
1435        lp->stats.tx_aborted_errors++;
1436
1437    /* recalculate our work chunk so that we limit the duration of this
1438     * ISR to about 1/10 of a second.
1439     * Calculate only if we received a reasonable amount of bytes.
1440     */
1441    if (bytes_rcvd > 1000) {
1442        u_long duration = jiffies - start_ticks;
1443
1444        if (duration >= HZ/10) { /* if more than about 1/10 second */
1445            maxrx_bytes = (bytes_rcvd * (HZ/10)) / duration;
1446            if (maxrx_bytes < 2000)
1447                maxrx_bytes = 2000;
1448            else if (maxrx_bytes > 22000)
1449                maxrx_bytes = 22000;
1450            DEBUG(1, "set maxrx=%u (rcvd=%u ticks=%lu)\n",
1451                  maxrx_bytes, bytes_rcvd, duration);
1452        } else if (!duration && maxrx_bytes < 22000) {
1453            /* now much faster */
1454            maxrx_bytes += 2000;
1455            if (maxrx_bytes > 22000)
1456                maxrx_bytes = 22000;
1457            DEBUG(1, "set maxrx=%u\n", maxrx_bytes);
1458        }
1459    }
1460
1461  leave:
1462    if (lockup_hack) {
1463        if (int_status != 0xff && (int_status = GetByte(XIRCREG_ISR)) != 0)
1464            goto loop_entry;
1465    }
1466    SelectPage(saved_page);
1467    PutByte(XIRCREG_CR, EnableIntr);  /* re-enable interrupts */
1468    /* Instead of dropping packets during a receive, we could
1469     * force an interrupt with this command:
1470     *    PutByte(XIRCREG_CR, EnableIntr|ForceIntr);
1471     */
1472    return IRQ_HANDLED;
1473} /* xirc2ps_interrupt */
1474
1475/*====================================================================*/
1476
1477static void
1478do_tx_timeout(struct net_device *dev)
1479{
1480    local_info_t *lp = dev->priv;
1481    printk(KERN_NOTICE "%s: transmit timed out\n", dev->name);
1482    lp->stats.tx_errors++;
1483    /* reset the card */
1484    do_reset(dev,1);
1485    dev->trans_start = jiffies;
1486    netif_wake_queue(dev);
1487}
1488
1489static int
1490do_start_xmit(struct sk_buff *skb, struct net_device *dev)
1491{
1492    local_info_t *lp = dev->priv;
1493    ioaddr_t ioaddr = dev->base_addr;
1494    int okay;
1495    unsigned freespace;
1496    unsigned pktlen = skb? skb->len : 0;
1497
1498    DEBUG(1, "do_start_xmit(skb=%p, dev=%p) len=%u\n",
1499          skb, dev, pktlen);
1500
1501
1502    /* adjust the packet length to min. required
1503     * and hope that the buffer is large enough
1504     * to provide some random data.
1505     * fixme: For Mohawk we can change this by sending
1506     * a larger packetlen than we actually have; the chip will
1507     * pad this in his buffer with random bytes
1508     */
1509    if (pktlen < ETH_ZLEN)
1510    {
1511        skb = skb_padto(skb, ETH_ZLEN);
1512        if (skb == NULL)
1513                return 0;
1514        pktlen = ETH_ZLEN;
1515    }
1516
1517    netif_stop_queue(dev);
1518    SelectPage(0);
1519    PutWord(XIRCREG0_TRS, (u_short)pktlen+2);
1520    freespace = GetWord(XIRCREG0_TSO);
1521    okay = freespace & 0x8000;
1522    freespace &= 0x7fff;
1523    /* TRS doesn't work - (indeed it is eliminated with sil-rev 1) */
1524    okay = pktlen +2 < freespace;
1525    DEBUG(2 + (okay ? 2 : 0), "%s: avail. tx space=%u%s\n",
1526          dev->name, freespace, okay ? " (okay)":" (not enough)");
1527    if (!okay) { /* not enough space */
1528        return 1;  /* upper layer may decide to requeue this packet */
1529    }
1530    /* send the packet */
1531    PutWord(XIRCREG_EDP, (u_short)pktlen);
1532    outsw(ioaddr+XIRCREG_EDP, skb->data, pktlen>>1);
1533    if (pktlen & 1)
1534        PutByte(XIRCREG_EDP, skb->data[pktlen-1]);
1535
1536    if (lp->mohawk)
1537        PutByte(XIRCREG_CR, TransmitPacket|EnableIntr);
1538
1539    dev_kfree_skb (skb);
1540    dev->trans_start = jiffies;
1541    lp->stats.tx_bytes += pktlen;
1542    netif_start_queue(dev);
1543    return 0;
1544}
1545
1546static struct net_device_stats *
1547do_get_stats(struct net_device *dev)
1548{
1549    local_info_t *lp = dev->priv;
1550
1551    /*  lp->stats.rx_missed_errors = GetByte(?) */
1552    return &lp->stats;
1553}
1554
1555/****************
1556 * Set all addresses: This first one is the individual address,
1557 * the next 9 addresses are taken from the multicast list and
1558 * the rest is filled with the individual address.
1559 */
1560static void
1561set_addresses(struct net_device *dev)
1562{
1563    ioaddr_t ioaddr = dev->base_addr;
1564    local_info_t *lp = dev->priv;
1565    struct dev_mc_list *dmi = dev->mc_list;
1566    char *addr;
1567    int i,j,k,n;
1568
1569    SelectPage(k=0x50);
1570    for (i=0,j=8,n=0; ; i++, j++) {
1571        if (i > 5) {
1572            if (++n > 9)
1573                break;
1574            i = 0;
1575        }
1576        if (j > 15) {
1577            j = 8;
1578            k++;
1579            SelectPage(k);
1580        }
1581
1582        if (n && n <= dev->mc_count && dmi) {
1583            addr = dmi->dmi_addr;
1584            dmi = dmi->next;
1585        } else
1586            addr = dev->dev_addr;
1587
1588        if (lp->mohawk)
1589            PutByte(j, addr[5-i]);
1590        else
1591            PutByte(j, addr[i]);
1592    }
1593    SelectPage(0);
1594}
1595
1596/****************
1597 * Set or clear the multicast filter for this adaptor.
1598 * We can filter up to 9 addresses, if more are requested we set
1599 * multicast promiscuous mode.
1600 */
1601
1602static void
1603set_multicast_list(struct net_device *dev)
1604{
1605    ioaddr_t ioaddr = dev->base_addr;
1606
1607    SelectPage(0x42);
1608    if (dev->flags & IFF_PROMISC) { /* snoop */
1609        PutByte(XIRCREG42_SWC1, 0x06); /* set MPE and PME */
1610    } else if (dev->mc_count > 9 || (dev->flags & IFF_ALLMULTI)) {
1611        PutByte(XIRCREG42_SWC1, 0x06); /* set MPE */
1612    } else if (dev->mc_count) {
1613        /* the chip can filter 9 addresses perfectly */
1614        PutByte(XIRCREG42_SWC1, 0x00);
1615        SelectPage(0x40);
1616        PutByte(XIRCREG40_CMD0, Offline);
1617        set_addresses(dev);
1618        SelectPage(0x40);
1619        PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1620    } else { /* standard usage */
1621        PutByte(XIRCREG42_SWC1, 0x00);
1622    }
1623    SelectPage(0);
1624}
1625
1626static int
1627do_config(struct net_device *dev, struct ifmap *map)
1628{
1629    local_info_t *local = dev->priv;
1630
1631    DEBUG(0, "do_config(%p)\n", dev);
1632    if (map->port != 255 && map->port != dev->if_port) {
1633        if (map->port > 4)
1634            return -EINVAL;
1635        if (!map->port) {
1636            local->probe_port = 1;
1637            dev->if_port = 1;
1638        } else {
1639            local->probe_port = 0;
1640            dev->if_port = map->port;
1641        }
1642        printk(KERN_INFO "%s: switching to %s port\n",
1643               dev->name, if_names[dev->if_port]);
1644        do_reset(dev,1);  /* not the fine way :-) */
1645    }
1646    return 0;
1647}
1648
1649/****************
1650 * Open the driver
1651 */
1652static int
1653do_open(struct net_device *dev)
1654{
1655    local_info_t *lp = dev->priv;
1656    dev_link_t *link = &lp->link;
1657
1658    DEBUG(0, "do_open(%p)\n", dev);
1659
1660    /* Check that the PCMCIA card is still here. */
1661    /* Physical device present signature. */
1662    if (!DEV_OK(link))
1663        return -ENODEV;
1664
1665    /* okay */
1666    link->open++;
1667
1668    netif_start_queue(dev);
1669    do_reset(dev,1);
1670
1671    return 0;
1672}
1673
1674static void netdev_get_drvinfo(struct net_device *dev,
1675                               struct ethtool_drvinfo *info)
1676{
1677        strcpy(info->driver, "xirc2ps_cs");
1678}
1679
1680static struct ethtool_ops netdev_ethtool_ops = {
1681        .get_drvinfo            = netdev_get_drvinfo,
1682};
1683
1684static int
1685do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1686{
1687    local_info_t *local = dev->priv;
1688    ioaddr_t ioaddr = dev->base_addr;
1689    u16 *data = (u16 *)&rq->ifr_data;
1690
1691    DEBUG(1, "%s: ioctl(%-.6s, %#04x) %04x %04x %04x %04x\n",
1692          dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1693          data[0], data[1], data[2], data[3]);
1694
1695    if (!local->mohawk)
1696        return -EOPNOTSUPP;
1697
1698    switch(cmd) {
1699      case SIOCGMIIPHY:         /* Get the address of the PHY in use. */
1700        data[0] = 0;            /* we have only this address */
1701        /* fall trough */
1702      case SIOCGMIIREG:         /* Read the specified MII register. */
1703        data[3] = mii_rd(ioaddr, data[0] & 0x1f, data[1] & 0x1f);
1704        break;
1705      case SIOCSMIIREG:         /* Write the specified MII register */
1706        if (!capable(CAP_NET_ADMIN))
1707            return -EPERM;
1708        mii_wr(ioaddr, data[0] & 0x1f, data[1] & 0x1f, data[2], 16);
1709        break;
1710      default:
1711        return -EOPNOTSUPP;
1712    }
1713    return 0;
1714}
1715
1716static void
1717hardreset(struct net_device *dev)
1718{
1719    local_info_t *local = dev->priv;
1720    ioaddr_t ioaddr = dev->base_addr;
1721
1722    SelectPage(4);
1723    udelay(1);
1724    PutByte(XIRCREG4_GPR1, 0);       /* clear bit 0: power down */
1725    Wait(HZ/25);                     /* wait 40 msec */
1726    if (local->mohawk)
1727        PutByte(XIRCREG4_GPR1, 1);       /* set bit 0: power up */
1728    else
1729        PutByte(XIRCREG4_GPR1, 1 | 4);   /* set bit 0: power up, bit 2: AIC */
1730    Wait(HZ/50);                     /* wait 20 msec */
1731}
1732
1733static void
1734do_reset(struct net_device *dev, int full)
1735{
1736    local_info_t *local = dev->priv;
1737    ioaddr_t ioaddr = dev->base_addr;
1738    unsigned value;
1739
1740    DEBUG(0, "%s: do_reset(%p,%d)\n", dev? dev->name:"eth?", dev, full);
1741
1742    hardreset(dev);
1743    PutByte(XIRCREG_CR, SoftReset); /* set */
1744    Wait(HZ/50);                     /* wait 20 msec */
1745    PutByte(XIRCREG_CR, 0);          /* clear */
1746    Wait(HZ/25);                     /* wait 40 msec */
1747    if (local->mohawk) {
1748        SelectPage(4);
1749        /* set pin GP1 and GP2 to output  (0x0c)
1750         * set GP1 to low to power up the ML6692 (0x00)
1751         * set GP2 to high to power up the 10Mhz chip  (0x02)
1752         */
1753        PutByte(XIRCREG4_GPR0, 0x0e);
1754    }
1755
1756    /* give the circuits some time to power up */
1757    Wait(HZ/2);         /* about 500ms */
1758
1759    local->last_ptr_value = 0;
1760    local->silicon = local->mohawk ? (GetByte(XIRCREG4_BOV) & 0x70) >> 4
1761                                   : (GetByte(XIRCREG4_BOV) & 0x30) >> 4;
1762
1763    if (local->probe_port) {
1764        if (!local->mohawk) {
1765            SelectPage(4);
1766            PutByte(XIRCREG4_GPR0, 4);
1767            local->probe_port = 0;
1768        }
1769    } else if (dev->if_port == 2) { /* enable 10Base2 */
1770        SelectPage(0x42);
1771        PutByte(XIRCREG42_SWC1, 0xC0);
1772    } else { /* enable 10BaseT */
1773        SelectPage(0x42);
1774        PutByte(XIRCREG42_SWC1, 0x80);
1775    }
1776    Wait(HZ/25);                     /* wait 40 msec to let it complete */
1777
1778  #ifdef PCMCIA_DEBUG
1779    if (pc_debug) {
1780        SelectPage(0);
1781        value = GetByte(XIRCREG_ESR);    /* read the ESR */
1782        printk(KERN_DEBUG "%s: ESR is: %#02x\n", dev->name, value);
1783    }
1784  #endif
1785
1786    /* setup the ECR */
1787    SelectPage(1);
1788    PutByte(XIRCREG1_IMR0, 0xff); /* allow all ints */
1789    PutByte(XIRCREG1_IMR1, 1    ); /* and Set TxUnderrunDetect */
1790    value = GetByte(XIRCREG1_ECR);
1791  #if 0
1792    if (local->mohawk)
1793        value |= DisableLinkPulse;
1794    PutByte(XIRCREG1_ECR, value);
1795  #endif
1796    DEBUG(0, "%s: ECR is: %#02x\n", dev->name, value);
1797
1798    SelectPage(0x42);
1799    PutByte(XIRCREG42_SWC0, 0x20); /* disable source insertion */
1800
1801    if (local->silicon != 1) {
1802        /* set the local memory dividing line.
1803         * The comments in the sample code say that this is only
1804         * settable with the scipper version 2 which is revision 0.
1805         * Always for CE3 cards
1806         */
1807        SelectPage(2);
1808        PutWord(XIRCREG2_RBS, 0x2000);
1809    }
1810
1811    if (full)
1812        set_addresses(dev);
1813
1814    /* Hardware workaround:
1815     * The receive byte pointer after reset is off by 1 so we need
1816     * to move the offset pointer back to 0.
1817     */
1818    SelectPage(0);
1819    PutWord(XIRCREG0_DO, 0x2000); /* change offset command, off=0 */
1820
1821    /* setup MAC IMRs and clear status registers */
1822    SelectPage(0x40);                /* Bit 7 ... bit 0 */
1823    PutByte(XIRCREG40_RMASK0, 0xff); /* ROK, RAB, rsv, RO, CRC, AE, PTL, MP */
1824    PutByte(XIRCREG40_TMASK0, 0xff); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1825    PutByte(XIRCREG40_TMASK1, 0xb0); /* rsv, rsv, PTD, EXT, rsv,rsv,rsv, rsv*/
1826    PutByte(XIRCREG40_RXST0,  0x00); /* ROK, RAB, REN, RO, CRC, AE, PTL, MP */
1827    PutByte(XIRCREG40_TXST0,  0x00); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1828    PutByte(XIRCREG40_TXST1,  0x00); /* TEN, rsv, PTD, EXT, retry_counter:4  */
1829
1830    if (full && local->mohawk && init_mii(dev)) {
1831        if (dev->if_port == 4 || local->dingo || local->new_mii) {
1832            printk(KERN_INFO "%s: MII selected\n", dev->name);
1833            SelectPage(2);
1834            PutByte(XIRCREG2_MSR, GetByte(XIRCREG2_MSR) | 0x08);
1835            Wait(HZ/50);
1836        } else {
1837            printk(KERN_INFO "%s: MII detected; using 10mbs\n",
1838                   dev->name);
1839            SelectPage(0x42);
1840            if (dev->if_port == 2) /* enable 10Base2 */
1841                PutByte(XIRCREG42_SWC1, 0xC0);
1842            else  /* enable 10BaseT */
1843                PutByte(XIRCREG42_SWC1, 0x80);
1844            Wait(HZ/25);        /* wait 40 msec to let it complete */
1845        }
1846        if (full_duplex)
1847            PutByte(XIRCREG1_ECR, GetByte(XIRCREG1_ECR | FullDuplex));
1848    } else {  /* No MII */
1849        SelectPage(0);
1850        value = GetByte(XIRCREG_ESR);    /* read the ESR */
1851        dev->if_port = (value & MediaSelect) ? 1 : 2;
1852    }
1853
1854    /* configure the LEDs */
1855    SelectPage(2);
1856    if (dev->if_port == 1 || dev->if_port == 4) /* TP: Link and Activity */
1857        PutByte(XIRCREG2_LED, 0x3b);
1858    else                              /* Coax: Not-Collision and Activity */
1859        PutByte(XIRCREG2_LED, 0x3a);
1860
1861    if (local->dingo)
1862        PutByte(0x0b, 0x04); /* 100 Mbit LED */
1863
1864    /* enable receiver and put the mac online */
1865    if (full) {
1866        SelectPage(0x40);
1867        PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1868    }
1869
1870    /* setup Ethernet IMR and enable interrupts */
1871    SelectPage(1);
1872    PutByte(XIRCREG1_IMR0, 0xff);
1873    udelay(1);
1874    SelectPage(0);
1875    PutByte(XIRCREG_CR, EnableIntr);
1876    if (local->modem && !local->dingo) { /* do some magic */
1877        if (!(GetByte(0x10) & 0x01))
1878            PutByte(0x10, 0x11); /* unmask master-int bit */
1879    }
1880
1881    if (full)
1882        printk(KERN_INFO "%s: media %s, silicon revision %d\n",
1883               dev->name, if_names[dev->if_port], local->silicon);
1884    /* We should switch back to page 0 to avoid a bug in revision 0
1885     * where regs with offset below 8 can't be read after an access
1886     * to the MAC registers */
1887    SelectPage(0);
1888}
1889
1890/****************
1891 * Initialize the Media-Independent-Interface
1892 * Returns: True if we have a good MII
1893 */
1894static int
1895init_mii(struct net_device *dev)
1896{
1897    local_info_t *local = dev->priv;
1898    ioaddr_t ioaddr = dev->base_addr;
1899    unsigned control, status, linkpartner;
1900    int i;
1901
1902    if (if_port == 4 || if_port == 1) { /* force 100BaseT or 10BaseT */
1903        dev->if_port = if_port;
1904        local->probe_port = 0;
1905        return 1;
1906    }
1907
1908    status = mii_rd(ioaddr,  0, 1);
1909    if ((status & 0xff00) != 0x7800)
1910        return 0; /* No MII */
1911
1912    local->new_mii = (mii_rd(ioaddr, 0, 2) != 0xffff);
1913    
1914    if (local->probe_port)
1915        control = 0x1000; /* auto neg */
1916    else if (dev->if_port == 4)
1917        control = 0x2000; /* no auto neg, 100mbs mode */
1918    else
1919        control = 0x0000; /* no auto neg, 10mbs mode */
1920    mii_wr(ioaddr,  0, 0, control, 16);
1921    udelay(100);
1922    control = mii_rd(ioaddr, 0, 0);
1923
1924    if (control & 0x0400) {
1925        printk(KERN_NOTICE "%s can't take PHY out of isolation mode\n",
1926               dev->name);
1927        local->probe_port = 0;
1928        return 0;
1929    }
1930
1931    if (local->probe_port) {
1932        /* according to the DP83840A specs the auto negotiation process
1933         * may take up to 3.5 sec, so we use this also for our ML6692
1934         * Fixme: Better to use a timer here!
1935         */
1936        for (i=0; i < 35; i++) {
1937            Wait(HZ/10);         /* wait 100 msec */
1938            status = mii_rd(ioaddr,  0, 1);
1939            if ((status & 0x0020) && (status & 0x0004))
1940                break;
1941        }
1942
1943        if (!(status & 0x0020)) {
1944            printk(KERN_INFO "%s: autonegotiation failed;"
1945                   " using 10mbs\n", dev->name);
1946            if (!local->new_mii) {
1947                control = 0x0000;
1948                mii_wr(ioaddr,  0, 0, control, 16);
1949                udelay(100);
1950                SelectPage(0);
1951                dev->if_port = (GetByte(XIRCREG_ESR) & MediaSelect) ? 1 : 2;
1952            }
1953        } else {
1954            linkpartner = mii_rd(ioaddr, 0, 5);
1955            printk(KERN_INFO "%s: MII link partner: %04x\n",
1956                   dev->name, linkpartner);
1957            if (linkpartner & 0x0080) {
1958                dev->if_port = 4;
1959            } else
1960                dev->if_port = 1;
1961        }
1962    }
1963
1964    return 1;
1965}
1966
1967static void
1968do_powerdown(struct net_device *dev)
1969{
1970
1971    ioaddr_t ioaddr = dev->base_addr;
1972
1973    DEBUG(0, "do_powerdown(%p)\n", dev);
1974
1975    SelectPage(4);
1976    PutByte(XIRCREG4_GPR1, 0);       /* clear bit 0: power down */
1977    SelectPage(0);
1978}
1979
1980static int
1981do_stop(struct net_device *dev)
1982{
1983    ioaddr_t ioaddr = dev->base_addr;
1984    local_info_t *lp = dev->priv;
1985    dev_link_t *link = &lp->link;
1986
1987    DEBUG(0, "do_stop(%p)\n", dev);
1988
1989    if (!link)
1990        return -ENODEV;
1991
1992    netif_stop_queue(dev);
1993
1994    SelectPage(0);
1995    PutByte(XIRCREG_CR, 0);  /* disable interrupts */
1996    SelectPage(0x01);
1997    PutByte(XIRCREG1_IMR0, 0x00); /* forbid all ints */
1998    SelectPage(4);
1999    PutByte(XIRCREG4_GPR1, 0);  /* clear bit 0: power down */
2000    SelectPage(0);
2001
2002    link->open--;
2003    return 0;
2004}
2005
2006static struct pcmcia_driver xirc2ps_cs_driver = {
2007        .owner          = THIS_MODULE,
2008        .drv            = {
2009                .name   = "xirc2ps_cs",
2010        },
2011        .attach         = xirc2ps_attach,
2012        .detach         = xirc2ps_detach,
2013};
2014
2015static int __init
2016init_xirc2ps_cs(void)
2017{
2018        return pcmcia_register_driver(&xirc2ps_cs_driver);
2019}
2020
2021static void __exit
2022exit_xirc2ps_cs(void)
2023{
2024        pcmcia_unregister_driver(&xirc2ps_cs_driver);
2025
2026        while (dev_list)
2027                xirc2ps_detach(dev_list);
2028}
2029
2030module_init(init_xirc2ps_cs);
2031module_exit(exit_xirc2ps_cs);
2032
2033#ifndef MODULE
2034static int __init setup_xirc2ps_cs(char *str)
2035{
2036        /* irq, irq_mask, if_port, full_duplex, do_sound, lockup_hack
2037         * [,irq2 [,irq3 [,irq4]]]
2038         */
2039        int ints[10] = { -1 };
2040
2041        str = get_options(str, 9, ints);
2042
2043#define MAYBE_SET(X,Y) if (ints[0] >= Y && ints[Y] != -1) { X = ints[Y]; }
2044        MAYBE_SET(irq_list[0], 1);
2045        MAYBE_SET(irq_mask, 2);
2046        MAYBE_SET(if_port, 3);
2047        MAYBE_SET(full_duplex, 4);
2048        MAYBE_SET(do_sound, 5);
2049        MAYBE_SET(lockup_hack, 6);
2050        MAYBE_SET(irq_list[1], 7);
2051        MAYBE_SET(irq_list[2], 8);
2052        MAYBE_SET(irq_list[3], 9);
2053#undef  MAYBE_SET
2054
2055        return 0;
2056}
2057
2058__setup("xirc2ps_cs=", setup_xirc2ps_cs);
2059#endif
2060
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.