3c589_cs.c 27.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*======================================================================

    A PCMCIA ethernet driver for the 3com 3c589 card.
4

Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9
    Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net

    3c589_cs.c 1.162 2001/10/13 00:08:50

    The network driver code is based on Donald Becker's 3c589 code:
10

Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16
    Written 1994 by Donald Becker.
    Copyright 1993 United States Government as represented by the
    Director, National Security Agency.  This software may be used and
    distributed according to the terms of the GNU General Public License,
    incorporated herein by reference.
    Donald Becker may be reached at becker@scyld.com
17

18
    Updated for 2.5.x by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21

======================================================================*/

22 23
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Linus Torvalds's avatar
Linus Torvalds committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#define DRV_NAME	"3c589_cs"
#define DRV_VERSION	"1.162-ac"

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/in.h>
#include <linux/delay.h>
#include <linux/ethtool.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/ioport.h>
#include <linux/bitops.h>
44
#include <linux/jiffies.h>
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/cisreg.h>
#include <pcmcia/ciscode.h>
#include <pcmcia/ds.h>

#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/system.h>

/* To minimize the size of the driver source I only define operating
   constants if they are used several times.  You'll need the manual
   if you want to understand driver details. */
/* Offsets from base I/O address. */
#define EL3_DATA	0x00
#define EL3_TIMER	0x0a
#define EL3_CMD		0x0e
#define EL3_STATUS	0x0e

#define EEPROM_READ	0x0080
#define EEPROM_BUSY	0x8000

#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)

/* The top five bits written to EL3_CMD are a command, the lower
   11 bits are the parameter, if applicable. */
enum c509cmd {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	TotalReset	= 0<<11,
	SelectWindow	= 1<<11,
	StartCoax	= 2<<11,
	RxDisable	= 3<<11,
	RxEnable	= 4<<11,
	RxReset		= 5<<11,
	RxDiscard	= 8<<11,
	TxEnable	= 9<<11,
	TxDisable	= 10<<11,
	TxReset		= 11<<11,
	FakeIntr	= 12<<11,
	AckIntr		= 13<<11,
	SetIntrEnb	= 14<<11,
	SetStatusEnb	= 15<<11,
	SetRxFilter	= 16<<11,
	SetRxThreshold	= 17<<11,
	SetTxThreshold	= 18<<11,
	SetTxStart	= 19<<11,
	StatsEnable	= 21<<11,
	StatsDisable	= 22<<11,
	StopCoax	= 23<<11
Linus Torvalds's avatar
Linus Torvalds committed
94 95 96
};

enum c509status {
97 98 99 100 101 102 103 104 105
	IntLatch	= 0x0001,
	AdapterFailure	= 0x0002,
	TxComplete	= 0x0004,
	TxAvailable	= 0x0008,
	RxComplete	= 0x0010,
	RxEarly		= 0x0020,
	IntReq		= 0x0040,
	StatsFull	= 0x0080,
	CmdBusy		= 0x1000
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109
};

/* The SetRxFilter command accepts the following classes: */
enum RxFilter {
110 111 112 113
	RxStation	= 1,
	RxMulticast	= 2,
	RxBroadcast	= 4,
	RxProm		= 8
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118
};

/* Register window 1 offsets, the window used in normal operation. */
#define TX_FIFO		0x00
#define RX_FIFO		0x00
119 120
#define RX_STATUS	0x08
#define TX_STATUS	0x0B
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127 128 129 130 131
#define TX_FREE		0x0C	/* Remaining free bytes in Tx buffer. */

#define WN0_IRQ		0x08	/* Window 0: Set IRQ line in bits 12-15. */
#define WN4_MEDIA	0x0A	/* Window 4: Various transcvr/media bits. */
#define MEDIA_TP	0x00C0	/* Enable link beat and jabber for 10baseT. */
#define MEDIA_LED	0x0001	/* Enable link light on 3C589E cards. */

/* Time in jiffies before concluding Tx hung */
#define TX_TIMEOUT	((400*HZ)/1000)

struct el3_private {
132
	struct pcmcia_device	*p_dev;
133 134 135 136 137 138
	/* For transceiver monitoring */
	struct timer_list	media;
	u16			media_status;
	u16			fast_poll;
	unsigned long		last_irq;
	spinlock_t		lock;
Linus Torvalds's avatar
Linus Torvalds committed
139 140
};

141
static const char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

/*====================================================================*/

/* Module parameters */

MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
MODULE_LICENSE("GPL");

#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)

/* Special hook for setting if_port when module is loaded */
INT_MODULE_PARM(if_port, 0);


/*====================================================================*/

159
static int tc589_config(struct pcmcia_device *link);
160
static void tc589_release(struct pcmcia_device *link);
Linus Torvalds's avatar
Linus Torvalds committed
161

162
static u16 read_eeprom(unsigned int ioaddr, int index);
Linus Torvalds's avatar
Linus Torvalds committed
163 164 165 166
static void tc589_reset(struct net_device *dev);
static void media_check(unsigned long arg);
static int el3_config(struct net_device *dev, struct ifmap *map);
static int el3_open(struct net_device *dev);
167 168
static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
					struct net_device *dev);
169
static irqreturn_t el3_interrupt(int irq, void *dev_id);
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174
static void update_stats(struct net_device *dev);
static struct net_device_stats *el3_get_stats(struct net_device *dev);
static int el3_rx(struct net_device *dev);
static int el3_close(struct net_device *dev);
static void el3_tx_timeout(struct net_device *dev);
175
static void set_rx_mode(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
176
static void set_multicast_list(struct net_device *dev);
177
static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds's avatar
Linus Torvalds committed
178

179
static void tc589_detach(struct pcmcia_device *p_dev);
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184 185 186 187 188

/*======================================================================

    tc589_attach() creates an "instance" of the driver, allocating
    local data structures for one device.  The device is registered
    with Card Services.

======================================================================*/

189
static const struct net_device_ops el3_netdev_ops = {
190 191
	.ndo_open		= el3_open,
	.ndo_stop		= el3_close,
192
	.ndo_start_xmit		= el3_start_xmit,
193
	.ndo_tx_timeout		= el3_tx_timeout,
194 195 196 197
	.ndo_set_config		= el3_config,
	.ndo_get_stats		= el3_get_stats,
	.ndo_set_multicast_list = set_multicast_list,
	.ndo_change_mtu		= eth_change_mtu,
198
	.ndo_set_mac_address	= eth_mac_addr,
199 200 201
	.ndo_validate_addr	= eth_validate_addr,
};

202
static int tc589_probe(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206
{
    struct el3_private *lp;
    struct net_device *dev;

207
    dev_dbg(&link->dev, "3c589_attach()\n");
208

Linus Torvalds's avatar
Linus Torvalds committed
209 210 211
    /* Create new ethernet device */
    dev = alloc_etherdev(sizeof(struct el3_private));
    if (!dev)
212
	 return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
213 214
    lp = netdev_priv(dev);
    link->priv = dev;
215
    lp->p_dev = link;
Linus Torvalds's avatar
Linus Torvalds committed
216 217

    spin_lock_init(&lp->lock);
218 219
    link->resource[0]->end = 16;
    link->resource[0]->flags |= IO_DATA_PATH_WIDTH_16;
220

Linus Torvalds's avatar
Linus Torvalds committed
221 222 223
    link->conf.Attributes = CONF_ENABLE_IRQ;
    link->conf.IntType = INT_MEMORY_AND_IO;
    link->conf.ConfigIndex = 1;
224

225
    dev->netdev_ops = &el3_netdev_ops;
Linus Torvalds's avatar
Linus Torvalds committed
226
    dev->watchdog_timeo = TX_TIMEOUT;
227

Linus Torvalds's avatar
Linus Torvalds committed
228 229
    SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);

230
    return tc589_config(link);
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235 236 237 238 239 240 241
} /* tc589_attach */

/*======================================================================

    This deletes a driver "instance".  The device is de-registered
    with Card Services.  If it has been released, all local data
    structures are freed.  Otherwise, the structures will be freed
    when the device is released.

======================================================================*/

242
static void tc589_detach(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
243 244
{
    struct net_device *dev = link->priv;
245

246
    dev_dbg(&link->dev, "3c589_detach\n");
Linus Torvalds's avatar
Linus Torvalds committed
247

248
    unregister_netdev(dev);
Linus Torvalds's avatar
Linus Torvalds committed
249

250
    tc589_release(link);
251

Linus Torvalds's avatar
Linus Torvalds committed
252 253 254 255 256 257 258 259
    free_netdev(dev);
} /* tc589_detach */

/*======================================================================

    tc589_config() is scheduled to run after a CARD_INSERTION event
    is received, to configure the PCMCIA socket, and to make the
    ethernet device available to the system.
260

Linus Torvalds's avatar
Linus Torvalds committed
261 262
======================================================================*/

263
static int tc589_config(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
264 265
{
    struct net_device *dev = link->priv;
266
    __be16 *phys_addr;
267
    int ret, i, j, multi = 0, fifo;
268
    unsigned int ioaddr;
Linus Torvalds's avatar
Linus Torvalds committed
269
    char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
270 271
    u8 *buf;
    size_t len;
272

273
    dev_dbg(&link->dev, "3c589_config\n");
Linus Torvalds's avatar
Linus Torvalds committed
274

275
    phys_addr = (__be16 *)dev->dev_addr;
276 277
    /* Is this a 3c562? */
    if (link->manf_id != MANFID_3COM)
278
	    dev_info(&link->dev, "hmmm, is this really a 3Com card??\n");
279
    multi = (link->card_id == PRODID_3COM_3C562);
Linus Torvalds's avatar
Linus Torvalds committed
280

281 282
    link->io_lines = 16;

Linus Torvalds's avatar
Linus Torvalds committed
283 284 285
    /* For the 3c562, the base address must be xx00-xx7f */
    for (i = j = 0; j < 0x400; j += 0x10) {
	if (multi && (j & 0x80)) continue;
286 287
	link->resource[0]->start = j ^ 0x300;
	i = pcmcia_request_io(link);
288 289
	if (i == 0)
		break;
Linus Torvalds's avatar
Linus Torvalds committed
290
    }
291
    if (i != 0)
Linus Torvalds's avatar
Linus Torvalds committed
292
	goto failed;
293

294
    ret = pcmcia_request_irq(link, el3_interrupt);
295 296 297 298 299 300
    if (ret)
	    goto failed;

    ret = pcmcia_request_configuration(link, &link->conf);
    if (ret)
	    goto failed;
301

302
    dev->irq = link->irq;
303
    dev->base_addr = link->resource[0]->start;
Linus Torvalds's avatar
Linus Torvalds committed
304 305 306 307 308
    ioaddr = dev->base_addr;
    EL3WINDOW(0);

    /* The 3c589 has an extra EEPROM for configuration info, including
       the hardware address.  The 3c562 puts the address in the CIS. */
309 310 311 312 313
    len = pcmcia_get_tuple(link, 0x88, &buf);
    if (buf && len >= 6) {
	    for (i = 0; i < 3; i++)
		    phys_addr[i] = htons(le16_to_cpu(buf[i*2]));
	    kfree(buf);
Linus Torvalds's avatar
Linus Torvalds committed
314
    } else {
315
	kfree(buf); /* 0 < len < 6 */
Linus Torvalds's avatar
Linus Torvalds committed
316 317
	for (i = 0; i < 3; i++)
	    phys_addr[i] = htons(read_eeprom(ioaddr, i));
318
	if (phys_addr[0] == htons(0x6060)) {
319 320
	    dev_err(&link->dev, "IO port conflict at 0x%03lx-0x%03lx\n",
		    dev->base_addr, dev->base_addr+15);
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324 325 326 327 328 329 330 331 332 333
	    goto failed;
	}
    }

    /* The address and resource configuration register aren't loaded from
       the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
    outw(0x3f00, ioaddr + 8);
    fifo = inl(ioaddr);

    /* The if_port symbol can be set when the module is loaded */
    if ((if_port >= 0) && (if_port <= 3))
	dev->if_port = if_port;
    else
334
	dev_err(&link->dev, "invalid if_port requested\n");
335

336
    SET_NETDEV_DEV(dev, &link->dev);
Linus Torvalds's avatar
Linus Torvalds committed
337 338

    if (register_netdev(dev) != 0) {
339
	    dev_err(&link->dev, "register_netdev() failed\n");
Linus Torvalds's avatar
Linus Torvalds committed
340 341 342
	goto failed;
    }

343 344 345 346 347 348
    netdev_info(dev, "3Com 3c%s, io %#3lx, irq %d, hw_addr %pM\n",
		(multi ? "562" : "589"), dev->base_addr, dev->irq,
		dev->dev_addr);
    netdev_info(dev, "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
		(fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
		if_names[dev->if_port]);
349
    return 0;
Linus Torvalds's avatar
Linus Torvalds committed
350 351 352

failed:
    tc589_release(link);
353
    return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
354 355 356 357 358 359 360
} /* tc589_config */

/*======================================================================

    After a card is removed, tc589_release() will unregister the net
    device, and release the PCMCIA configuration.  If the device is
    still open, this will be postponed until it is closed.
361

Linus Torvalds's avatar
Linus Torvalds committed
362 363
======================================================================*/

364
static void tc589_release(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
365
{
366
	pcmcia_disable_device(link);
Linus Torvalds's avatar
Linus Torvalds committed
367 368
}

369
static int tc589_suspend(struct pcmcia_device *link)
370 371 372
{
	struct net_device *dev = link->priv;

373
	if (link->open)
374
		netif_device_detach(dev);
375 376 377 378

	return 0;
}

379
static int tc589_resume(struct pcmcia_device *link)
380 381 382
{
	struct net_device *dev = link->priv;

383
	if (link->open) {
384 385
		tc589_reset(dev);
		netif_device_attach(dev);
386 387 388 389 390
	}

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
391 392 393 394 395 396 397 398 399 400 401 402
/*====================================================================*/

/*
  Use this for commands that may take time to finish
*/
static void tc589_wait_for_completion(struct net_device *dev, int cmd)
{
    int i = 100;
    outw(cmd, dev->base_addr + EL3_CMD);
    while (--i > 0)
	if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
    if (i == 0)
403
	netdev_warn(dev, "command 0x%04x did not complete!\n", cmd);
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408 409
}

/*
  Read a word from the EEPROM using the regular EEPROM access register.
  Assume that we are in register window zero.
*/
410
static u16 read_eeprom(unsigned int ioaddr, int index)
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
{
    int i;
    outw(EEPROM_READ + index, ioaddr + 10);
    /* Reading the eeprom takes 162 us */
    for (i = 1620; i >= 0; i--)
	if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
	    break;
    return inw(ioaddr + 12);
}

/*
  Set transceiver type, perhaps to something other than what the user
  specified in dev->if_port.
*/
static void tc589_set_xcvr(struct net_device *dev, int if_port)
{
    struct el3_private *lp = netdev_priv(dev);
428
    unsigned int ioaddr = dev->base_addr;
429

Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    EL3WINDOW(0);
    switch (if_port) {
    case 0: case 1: outw(0, ioaddr + 6); break;
    case 2: outw(3<<14, ioaddr + 6); break;
    case 3: outw(1<<14, ioaddr + 6); break;
    }
    /* On PCMCIA, this just turns on the LED */
    outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
    /* 10baseT interface, enable link beat and jabber check. */
    EL3WINDOW(4);
    outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
    EL3WINDOW(1);
    if (if_port == 2)
	lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
    else
	lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
}

static void dump_status(struct net_device *dev)
{
450
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
451
    EL3WINDOW(1);
452 453 454
    netdev_info(dev, "  irq status %04x, rx status %04x, tx status %02x  tx free %04x\n",
		inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS),
		inb(ioaddr+TX_STATUS), inw(ioaddr+TX_FREE));
Linus Torvalds's avatar
Linus Torvalds committed
455
    EL3WINDOW(4);
456 457 458
    netdev_info(dev, "  diagnostics: fifo %04x net %04x ethernet %04x media %04x\n",
		inw(ioaddr+0x04), inw(ioaddr+0x06), inw(ioaddr+0x08),
		inw(ioaddr+0x0a));
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462 463 464
    EL3WINDOW(1);
}

/* Reset and restore all of the 3c589 registers. */
static void tc589_reset(struct net_device *dev)
{
465
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
466
    int i;
467

Linus Torvalds's avatar
Linus Torvalds committed
468
    EL3WINDOW(0);
469
    outw(0x0001, ioaddr + 4);			/* Activate board. */
Linus Torvalds's avatar
Linus Torvalds committed
470
    outw(0x3f00, ioaddr + 8);			/* Set the IRQ line. */
471

Linus Torvalds's avatar
Linus Torvalds committed
472 473 474 475 476 477
    /* Set the station address in window 2. */
    EL3WINDOW(2);
    for (i = 0; i < 6; i++)
	outb(dev->dev_addr[i], ioaddr + i);

    tc589_set_xcvr(dev, dev->if_port);
478

Linus Torvalds's avatar
Linus Torvalds committed
479 480 481 482 483 484 485
    /* Switch to the stats window, and clear all stats by reading. */
    outw(StatsDisable, ioaddr + EL3_CMD);
    EL3WINDOW(6);
    for (i = 0; i < 9; i++)
	inb(ioaddr+i);
    inw(ioaddr + 10);
    inw(ioaddr + 12);
486

Linus Torvalds's avatar
Linus Torvalds committed
487 488 489
    /* Switch to register set 1 for normal use. */
    EL3WINDOW(1);

490
    set_rx_mode(dev);
Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
    outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
    outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
    /* Allow status bits to be seen. */
    outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
    /* Ack all pending events, and set active indicator mask. */
    outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
	 ioaddr + EL3_CMD);
    outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
	 | AdapterFailure, ioaddr + EL3_CMD);
}

static void netdev_get_drvinfo(struct net_device *dev,
			       struct ethtool_drvinfo *info)
{
	strcpy(info->driver, DRV_NAME);
	strcpy(info->version, DRV_VERSION);
	sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
}

511
static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
512 513 514 515 516 517 518 519
	.get_drvinfo		= netdev_get_drvinfo,
};

static int el3_config(struct net_device *dev, struct ifmap *map)
{
    if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
	if (map->port <= 3) {
	    dev->if_port = map->port;
520
	    netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
Linus Torvalds's avatar
Linus Torvalds committed
521 522 523 524 525 526 527 528 529 530
	    tc589_set_xcvr(dev, dev->if_port);
	} else
	    return -EINVAL;
    }
    return 0;
}

static int el3_open(struct net_device *dev)
{
    struct el3_private *lp = netdev_priv(dev);
531
    struct pcmcia_device *link = lp->p_dev;
532

533
    if (!pcmcia_dev_present(link))
Linus Torvalds's avatar
Linus Torvalds committed
534 535 536 537
	return -ENODEV;

    link->open++;
    netif_start_queue(dev);
538

Linus Torvalds's avatar
Linus Torvalds committed
539 540 541 542 543 544 545
    tc589_reset(dev);
    init_timer(&lp->media);
    lp->media.function = &media_check;
    lp->media.data = (unsigned long) dev;
    lp->media.expires = jiffies + HZ;
    add_timer(&lp->media);

546
    dev_dbg(&link->dev, "%s: opened, status %4.4x.\n",
Linus Torvalds's avatar
Linus Torvalds committed
547
	  dev->name, inw(dev->base_addr + EL3_STATUS));
548

Linus Torvalds's avatar
Linus Torvalds committed
549 550 551 552 553
    return 0;
}

static void el3_tx_timeout(struct net_device *dev)
{
554
    unsigned int ioaddr = dev->base_addr;
555 556

    netdev_warn(dev, "Transmit timed out!\n");
Linus Torvalds's avatar
Linus Torvalds committed
557
    dump_status(dev);
558
    dev->stats.tx_errors++;
Eric Dumazet's avatar
Eric Dumazet committed
559
    dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565 566 567
    /* Issue TX_RESET and TX_START commands. */
    tc589_wait_for_completion(dev, TxReset);
    outw(TxEnable, ioaddr + EL3_CMD);
    netif_wake_queue(dev);
}

static void pop_tx_status(struct net_device *dev)
{
568
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
569
    int i;
570

Linus Torvalds's avatar
Linus Torvalds committed
571 572 573 574 575 576
    /* Clear the Tx status stack. */
    for (i = 32; i > 0; i--) {
	u_char tx_status = inb(ioaddr + TX_STATUS);
	if (!(tx_status & 0x84)) break;
	/* reset transmitter on jabber error or underrun */
	if (tx_status & 0x30)
577
		tc589_wait_for_completion(dev, TxReset);
Linus Torvalds's avatar
Linus Torvalds committed
578
	if (tx_status & 0x38) {
579 580 581
		netdev_dbg(dev, "transmit error: status 0x%02x\n", tx_status);
		outw(TxEnable, ioaddr + EL3_CMD);
		dev->stats.tx_aborted_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
582 583 584 585 586
	}
	outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
    }
}

587 588
static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
					struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
589
{
590
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
591
    struct el3_private *priv = netdev_priv(dev);
Komuro's avatar
Komuro committed
592
    unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
593

594 595
    netdev_dbg(dev, "el3_start_xmit(length = %ld) called, status %4.4x.\n",
	       (long)skb->len, inw(ioaddr + EL3_STATUS));
Linus Torvalds's avatar
Linus Torvalds committed
596

597
    spin_lock_irqsave(&priv->lock, flags);
Komuro's avatar
Komuro committed
598

599
    dev->stats.tx_bytes += skb->len;
Linus Torvalds's avatar
Linus Torvalds committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613

    /* Put out the doubleword header... */
    outw(skb->len, ioaddr + TX_FIFO);
    outw(0x00, ioaddr + TX_FIFO);
    /* ... and the packet rounded to a doubleword. */
    outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);

    if (inw(ioaddr + TX_FREE) <= 1536) {
	netif_stop_queue(dev);
	/* Interrupt us when the FIFO has room for max-sized packet. */
	outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
    }

    pop_tx_status(dev);
614
    spin_unlock_irqrestore(&priv->lock, flags);
615
    dev_kfree_skb(skb);
616

617
    return NETDEV_TX_OK;
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620
}

/* The EL3 interrupt handler. */
621
static irqreturn_t el3_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
622 623 624
{
    struct net_device *dev = (struct net_device *) dev_id;
    struct el3_private *lp = netdev_priv(dev);
625
    unsigned int ioaddr;
Linus Torvalds's avatar
Linus Torvalds committed
626 627
    __u16 status;
    int i = 0, handled = 1;
628

Linus Torvalds's avatar
Linus Torvalds committed
629 630 631 632 633
    if (!netif_device_present(dev))
	return IRQ_NONE;

    ioaddr = dev->base_addr;

634
    netdev_dbg(dev, "interrupt, status %4.4x.\n", inw(ioaddr + EL3_STATUS));
Linus Torvalds's avatar
Linus Torvalds committed
635

636
    spin_lock(&lp->lock);
Linus Torvalds's avatar
Linus Torvalds committed
637 638 639
    while ((status = inw(ioaddr + EL3_STATUS)) &
	(IntLatch | RxComplete | StatsFull)) {
	if ((status & 0xe000) != 0x2000) {
640 641 642
		netdev_dbg(dev, "interrupt from dead card\n");
		handled = 0;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
643 644
	}
	if (status & RxComplete)
645
		el3_rx(dev);
Linus Torvalds's avatar
Linus Torvalds committed
646
	if (status & TxAvailable) {
647 648 649 650
		netdev_dbg(dev, "    TX room bit was handled.\n");
		/* There's room in the FIFO for a full-sized packet. */
		outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
		netif_wake_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
651 652
	}
	if (status & TxComplete)
653
		pop_tx_status(dev);
Linus Torvalds's avatar
Linus Torvalds committed
654 655 656 657 658 659 660 661 662 663 664 665 666
	if (status & (AdapterFailure | RxEarly | StatsFull)) {
	    /* Handle all uncommon interrupts. */
	    if (status & StatsFull)		/* Empty statistics. */
		update_stats(dev);
	    if (status & RxEarly) {		/* Rx early is unused. */
		el3_rx(dev);
		outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
	    }
	    if (status & AdapterFailure) {
		u16 fifo_diag;
		EL3WINDOW(4);
		fifo_diag = inw(ioaddr + 4);
		EL3WINDOW(1);
667 668
		netdev_warn(dev, "adapter failure, FIFO diagnostic register %04x.\n",
			    fifo_diag);
Linus Torvalds's avatar
Linus Torvalds committed
669 670 671 672 673 674 675 676
		if (fifo_diag & 0x0400) {
		    /* Tx overrun */
		    tc589_wait_for_completion(dev, TxReset);
		    outw(TxEnable, ioaddr + EL3_CMD);
		}
		if (fifo_diag & 0x2000) {
		    /* Rx underrun */
		    tc589_wait_for_completion(dev, RxReset);
677
		    set_rx_mode(dev);
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680 681 682 683
		    outw(RxEnable, ioaddr + EL3_CMD);
		}
		outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
	    }
	}
	if (++i > 10) {
684 685 686 687 688
		netdev_err(dev, "infinite loop in interrupt, status %4.4x.\n",
			   status);
		/* Clear all interrupts */
		outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691 692 693
	}
	/* Acknowledge the IRQ. */
	outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
    }
    lp->last_irq = jiffies;
694 695 696
    spin_unlock(&lp->lock);
    netdev_dbg(dev, "exiting interrupt, status %4.4x.\n",
	       inw(ioaddr + EL3_STATUS));
Linus Torvalds's avatar
Linus Torvalds committed
697 698 699 700 701 702 703
    return IRQ_RETVAL(handled);
}

static void media_check(unsigned long arg)
{
    struct net_device *dev = (struct net_device *)(arg);
    struct el3_private *lp = netdev_priv(dev);
704
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
705 706 707 708 709 710 711 712 713 714
    u16 media, errs;
    unsigned long flags;

    if (!netif_device_present(dev)) goto reschedule;

    /* Check for pending interrupt with expired latency timer: with
       this, we can limp along even if the interrupt is blocked */
    if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
	(inb(ioaddr + EL3_TIMER) == 0xff)) {
	if (!lp->fast_poll)
715
		netdev_warn(dev, "interrupt(s) dropped!\n");
716 717

	local_irq_save(flags);
Komuro's avatar
Komuro committed
718
	el3_interrupt(dev->irq, dev);
719 720
	local_irq_restore(flags);

Linus Torvalds's avatar
Linus Torvalds committed
721 722 723 724 725 726 727 728 729 730 731
	lp->fast_poll = HZ;
    }
    if (lp->fast_poll) {
	lp->fast_poll--;
	lp->media.expires = jiffies + HZ/100;
	add_timer(&lp->media);
	return;
    }

    /* lp->lock guards the EL3 window. Window should always be 1 except
       when the lock is held */
732
    spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
733 734 735 736
    EL3WINDOW(4);
    media = inw(ioaddr+WN4_MEDIA) & 0xc810;

    /* Ignore collisions unless we've had no irq's recently */
737
    if (time_before(jiffies, lp->last_irq + HZ)) {
Linus Torvalds's avatar
Linus Torvalds committed
738 739 740 741 742 743 744
	media &= ~0x0010;
    } else {
	/* Try harder to detect carrier errors */
	EL3WINDOW(6);
	outw(StatsDisable, ioaddr + EL3_CMD);
	errs = inb(ioaddr + 0);
	outw(StatsEnable, ioaddr + EL3_CMD);
745
	dev->stats.tx_carrier_errors += errs;
Linus Torvalds's avatar
Linus Torvalds committed
746 747 748 749 750 751
	if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
    }

    if (media != lp->media_status) {
	if ((media & lp->media_status & 0x8000) &&
	    ((lp->media_status ^ media) & 0x0800))
752 753
		netdev_info(dev, "%s link beat\n",
			    (lp->media_status & 0x0800 ? "lost" : "found"));
Linus Torvalds's avatar
Linus Torvalds committed
754 755
	else if ((media & lp->media_status & 0x4000) &&
		 ((lp->media_status ^ media) & 0x0010))
756 757
		netdev_info(dev, "coax cable %s\n",
			    (lp->media_status & 0x0010 ? "ok" : "problem"));
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760
	if (dev->if_port == 0) {
	    if (media & 0x8000) {
		if (media & 0x0800)
761
			netdev_info(dev, "flipped to 10baseT\n");
Linus Torvalds's avatar
Linus Torvalds committed
762
		else
763
			tc589_set_xcvr(dev, 2);
Linus Torvalds's avatar
Linus Torvalds committed
764 765 766 767
	    } else if (media & 0x4000) {
		if (media & 0x0010)
		    tc589_set_xcvr(dev, 1);
		else
768
		    netdev_info(dev, "flipped to 10base2\n");
Linus Torvalds's avatar
Linus Torvalds committed
769 770 771 772
	    }
	}
	lp->media_status = media;
    }
773

Linus Torvalds's avatar
Linus Torvalds committed
774
    EL3WINDOW(1);
775
    spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
776 777 778 779 780 781 782 783 784 785

reschedule:
    lp->media.expires = jiffies + HZ;
    add_timer(&lp->media);
}

static struct net_device_stats *el3_get_stats(struct net_device *dev)
{
    struct el3_private *lp = netdev_priv(dev);
    unsigned long flags;
786
    struct pcmcia_device *link = lp->p_dev;
Linus Torvalds's avatar
Linus Torvalds committed
787

788
    if (pcmcia_dev_present(link)) {
789
	spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
790 791 792
	update_stats(dev);
	spin_unlock_irqrestore(&lp->lock, flags);
    }
793
    return &dev->stats;
Linus Torvalds's avatar
Linus Torvalds committed
794 795 796 797 798 799 800
}

/*
  Update statistics.  We change to register window 6, so this should be run
  single-threaded if the device is active. This is expected to be a rare
  operation, and it's simpler for the rest of the driver to assume that
  window 1 is always valid rather than use a special window-state variable.
801

Linus Torvalds's avatar
Linus Torvalds committed
802 803 804 805
  Caller must hold the lock for this
*/
static void update_stats(struct net_device *dev)
{
806
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
807

808
    netdev_dbg(dev, "updating the statistics.\n");
Linus Torvalds's avatar
Linus Torvalds committed
809 810 811 812
    /* Turn off statistics updates while reading. */
    outw(StatsDisable, ioaddr + EL3_CMD);
    /* Switch to the stats window, and read everything. */
    EL3WINDOW(6);
813
    dev->stats.tx_carrier_errors	+= inb(ioaddr + 0);
814
    dev->stats.tx_heartbeat_errors	+= inb(ioaddr + 1);
815
    /* Multiple collisions. */		inb(ioaddr + 2);
816 817 818 819
    dev->stats.collisions		+= inb(ioaddr + 3);
    dev->stats.tx_window_errors		+= inb(ioaddr + 4);
    dev->stats.rx_fifo_errors		+= inb(ioaddr + 5);
    dev->stats.tx_packets		+= inb(ioaddr + 6);
Linus Torvalds's avatar
Linus Torvalds committed
820 821 822 823
    /* Rx packets   */			inb(ioaddr + 7);
    /* Tx deferrals */			inb(ioaddr + 8);
    /* Rx octets */			inw(ioaddr + 10);
    /* Tx octets */			inw(ioaddr + 12);
824

Linus Torvalds's avatar
Linus Torvalds committed
825 826 827 828 829 830 831
    /* Back to window 1, and turn statistics back on. */
    EL3WINDOW(1);
    outw(StatsEnable, ioaddr + EL3_CMD);
}

static int el3_rx(struct net_device *dev)
{
832
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
833 834
    int worklimit = 32;
    short rx_status;
835 836 837

    netdev_dbg(dev, "in rx_packet(), status %4.4x, rx_status %4.4x.\n",
	       inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
Linus Torvalds's avatar
Linus Torvalds committed
838
    while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
839 840
		    worklimit > 0) {
	worklimit--;
Linus Torvalds's avatar
Linus Torvalds committed
841 842
	if (rx_status & 0x4000) { /* Error, update stats. */
	    short error = rx_status & 0x3800;
843
	    dev->stats.rx_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
844
	    switch (error) {
845 846 847 848 849 850
	    case 0x0000:	dev->stats.rx_over_errors++; break;
	    case 0x0800:	dev->stats.rx_length_errors++; break;
	    case 0x1000:	dev->stats.rx_frame_errors++; break;
	    case 0x1800:	dev->stats.rx_length_errors++; break;
	    case 0x2000:	dev->stats.rx_frame_errors++; break;
	    case 0x2800:	dev->stats.rx_crc_errors++; break;
Linus Torvalds's avatar
Linus Torvalds committed
851 852 853 854
	    }
	} else {
	    short pkt_len = rx_status & 0x7ff;
	    struct sk_buff *skb;
855

Linus Torvalds's avatar
Linus Torvalds committed
856
	    skb = dev_alloc_skb(pkt_len+5);
857 858 859

	    netdev_dbg(dev, "    Receiving packet size %d status %4.4x.\n",
		       pkt_len, rx_status);
Linus Torvalds's avatar
Linus Torvalds committed
860 861 862 863 864 865
	    if (skb != NULL) {
		skb_reserve(skb, 2);
		insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
			(pkt_len+3)>>2);
		skb->protocol = eth_type_trans(skb, dev);
		netif_rx(skb);
866 867
		dev->stats.rx_packets++;
		dev->stats.rx_bytes += pkt_len;
Linus Torvalds's avatar
Linus Torvalds committed
868
	    } else {
869 870
		netdev_dbg(dev, "couldn't allocate a sk_buff of size %d.\n",
			   pkt_len);
871
		dev->stats.rx_dropped++;
Linus Torvalds's avatar
Linus Torvalds committed
872 873 874 875 876 877
	    }
	}
	/* Pop the top of the Rx FIFO */
	tc589_wait_for_completion(dev, RxDiscard);
    }
    if (worklimit == 0)
878
	netdev_warn(dev, "too much work in el3_rx!\n");
Linus Torvalds's avatar
Linus Torvalds committed
879 880 881
    return 0;
}

882
static void set_rx_mode(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
883
{
884
    unsigned int ioaddr = dev->base_addr;
Linus Torvalds's avatar
Linus Torvalds committed
885 886 887 888
    u16 opts = SetRxFilter | RxStation | RxBroadcast;

    if (dev->flags & IFF_PROMISC)
	opts |= RxMulticast | RxProm;
889
    else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI))
Linus Torvalds's avatar
Linus Torvalds committed
890 891 892 893
	opts |= RxMulticast;
    outw(opts, ioaddr + EL3_CMD);
}

894 895 896 897 898 899 900 901 902 903
static void set_multicast_list(struct net_device *dev)
{
	struct el3_private *priv = netdev_priv(dev);
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
	set_rx_mode(dev);
	spin_unlock_irqrestore(&priv->lock, flags);
}

Linus Torvalds's avatar
Linus Torvalds committed
904 905 906
static int el3_close(struct net_device *dev)
{
    struct el3_private *lp = netdev_priv(dev);
907
    struct pcmcia_device *link = lp->p_dev;
908
    unsigned int ioaddr = dev->base_addr;
909

910
    dev_dbg(&link->dev, "%s: shutting down ethercard.\n", dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
911

912
    if (pcmcia_dev_present(link)) {
913
	/* Turn off statistics ASAP.  We update dev->stats below. */
Linus Torvalds's avatar
Linus Torvalds committed
914
	outw(StatsDisable, ioaddr + EL3_CMD);
915

Linus Torvalds's avatar
Linus Torvalds committed
916 917 918
	/* Disable the receiver and transmitter. */
	outw(RxDisable, ioaddr + EL3_CMD);
	outw(TxDisable, ioaddr + EL3_CMD);
919

Linus Torvalds's avatar
Linus Torvalds committed
920 921 922 923 924 925 926 927
	if (dev->if_port == 2)
	    /* Turn off thinnet power.  Green! */
	    outw(StopCoax, ioaddr + EL3_CMD);
	else if (dev->if_port == 1) {
	    /* Disable link beat and jabber */
	    EL3WINDOW(4);
	    outw(0, ioaddr + WN4_MEDIA);
	}
928

Linus Torvalds's avatar
Linus Torvalds committed
929 930 931 932
	/* Switching back to window 0 disables the IRQ. */
	EL3WINDOW(0);
	/* But we explicitly zero the IRQ line select anyway. */
	outw(0x0f00, ioaddr + WN0_IRQ);
933

Linus Torvalds's avatar
Linus Torvalds committed
934 935 936 937 938 939 940 941
	/* Check if the card still exists */
	if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
	    update_stats(dev);
    }

    link->open--;
    netif_stop_queue(dev);
    del_timer_sync(&lp->media);
942

Linus Torvalds's avatar
Linus Torvalds committed
943 944 945
    return 0;
}

946 947 948 949 950
static struct pcmcia_device_id tc589_ids[] = {
	PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0101, 0x0562),
	PCMCIA_MFC_DEVICE_PROD_ID1(0, "Motorola MARQUIS", 0xf03e4e77),
	PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0589),
	PCMCIA_DEVICE_PROD_ID12("Farallon", "ENet", 0x58d93fc4, 0x992c2202),
951 952
	PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x0035, "cis/3CXEM556.cis"),
	PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x003d, "cis/3CXEM556.cis"),
953 954 955 956
	PCMCIA_DEVICE_NULL,
};
MODULE_DEVICE_TABLE(pcmcia, tc589_ids);

Linus Torvalds's avatar
Linus Torvalds committed
957 958 959 960 961
static struct pcmcia_driver tc589_driver = {
	.owner		= THIS_MODULE,
	.drv		= {
		.name	= "3c589_cs",
	},
962
	.probe		= tc589_probe,
963
	.remove		= tc589_detach,
964
	.id_table	= tc589_ids,
965 966
	.suspend	= tc589_suspend,
	.resume		= tc589_resume,
Linus Torvalds's avatar
Linus Torvalds committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980
};

static int __init init_tc589(void)
{
	return pcmcia_register_driver(&tc589_driver);
}

static void __exit exit_tc589(void)
{
	pcmcia_unregister_driver(&tc589_driver);
}

module_init(init_tc589);
module_exit(exit_tc589);