1 /*======================================================================
3 A PCMCIA ethernet driver for the 3com 3c589 card.
5 Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
7 3c589_cs.c 1.162 2001/10/13 00:08:50
9 The network driver code is based on Donald Becker's 3c589 code:
11 Written 1994 by Donald Becker.
12 Copyright 1993 United States Government as represented by the
13 Director, National Security Agency. This software may be used and
14 distributed according to the terms of the GNU General Public License,
15 incorporated herein by reference.
16 Donald Becker may be reached at becker@scyld.com
18 Updated for 2.5.x by Alan Cox <alan@redhat.com>
20 ======================================================================*/
22 #define DRV_NAME "3c589_cs"
23 #define DRV_VERSION "1.162-ac"
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
34 #include <linux/delay.h>
35 #include <linux/ethtool.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/if_arp.h>
40 #include <linux/ioport.h>
42 #include <pcmcia/version.h>
43 #include <pcmcia/cs_types.h>
44 #include <pcmcia/cs.h>
45 #include <pcmcia/cistpl.h>
46 #include <pcmcia/cisreg.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
50 #include <asm/uaccess.h>
52 #include <asm/system.h>
53 #include <asm/bitops.h>
55 /* To minimize the size of the driver source I only define operating
56 constants if they are used several times. You'll need the manual
57 if you want to understand driver details. */
58 /* Offsets from base I/O address. */
60 #define EL3_TIMER 0x0a
62 #define EL3_STATUS 0x0e
64 #define EEPROM_READ 0x0080
65 #define EEPROM_BUSY 0x8000
67 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
69 /* The top five bits written to EL3_CMD are a command, the lower
70 11 bits are the parameter, if applicable. */
72 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
73 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
74 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
75 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
76 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
77 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
78 StatsDisable = 22<<11, StopCoax = 23<<11,
82 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
83 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
84 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000
87 /* The SetRxFilter command accepts the following classes: */
89 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
92 /* Register window 1 offsets, the window used in normal operation. */
95 #define RX_STATUS 0x08
96 #define TX_STATUS 0x0B
97 #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
99 #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
100 #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
101 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
102 #define MEDIA_LED 0x0001 /* Enable link light on 3C589E cards. */
104 /* Time in jiffies before concluding Tx hung */
105 #define TX_TIMEOUT ((400*HZ)/1000)
110 struct net_device_stats stats;
111 /* For transceiver monitoring */
112 struct timer_list media;
115 unsigned long last_irq;
119 static char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
121 /*====================================================================*/
123 /* Module parameters */
125 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
126 MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
127 MODULE_LICENSE("GPL");
129 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
131 /* Special hook for setting if_port when module is loaded */
132 INT_MODULE_PARM(if_port, 0);
134 /* Bit map of interrupts to choose from */
135 INT_MODULE_PARM(irq_mask, 0xdeb8);
136 static int irq_list[4] = { -1 };
137 MODULE_PARM(irq_list, "1-4i");
140 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
141 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
142 static char *version =
143 DRV_NAME ".c " DRV_VERSION " 2001/10/13 00:08:50 (David Hinds)";
145 #define DEBUG(n, args...)
148 /*====================================================================*/
150 static void tc589_config(dev_link_t *link);
151 static void tc589_release(dev_link_t *link);
152 static int tc589_event(event_t event, int priority,
153 event_callback_args_t *args);
155 static u16 read_eeprom(ioaddr_t ioaddr, int index);
156 static void tc589_reset(struct net_device *dev);
157 static void media_check(unsigned long arg);
158 static int el3_config(struct net_device *dev, struct ifmap *map);
159 static int el3_open(struct net_device *dev);
160 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
161 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
162 static void update_stats(struct net_device *dev);
163 static struct net_device_stats *el3_get_stats(struct net_device *dev);
164 static int el3_rx(struct net_device *dev);
165 static int el3_close(struct net_device *dev);
166 static void el3_tx_timeout(struct net_device *dev);
167 static void set_multicast_list(struct net_device *dev);
168 static struct ethtool_ops netdev_ethtool_ops;
170 static dev_info_t dev_info = "3c589_cs";
172 static dev_link_t *tc589_attach(void);
173 static void tc589_detach(dev_link_t *);
175 static dev_link_t *dev_list;
177 /*======================================================================
179 tc589_attach() creates an "instance" of the driver, allocating
180 local data structures for one device. The device is registered
183 ======================================================================*/
185 static dev_link_t *tc589_attach(void)
187 struct el3_private *lp;
188 client_reg_t client_reg;
190 struct net_device *dev;
193 DEBUG(0, "3c589_attach()\n");
195 /* Create new ethernet device */
196 dev = alloc_etherdev(sizeof(struct el3_private));
199 lp = netdev_priv(dev);
203 spin_lock_init(&lp->lock);
204 link->io.NumPorts1 = 16;
205 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
206 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
207 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
208 if (irq_list[0] == -1)
209 link->irq.IRQInfo2 = irq_mask;
211 for (i = 0; i < 4; i++)
212 link->irq.IRQInfo2 |= 1 << irq_list[i];
213 link->irq.Handler = &el3_interrupt;
214 link->irq.Instance = dev;
215 link->conf.Attributes = CONF_ENABLE_IRQ;
217 link->conf.IntType = INT_MEMORY_AND_IO;
218 link->conf.ConfigIndex = 1;
219 link->conf.Present = PRESENT_OPTION;
221 /* The EL3-specific entries in the device structure. */
222 SET_MODULE_OWNER(dev);
223 dev->hard_start_xmit = &el3_start_xmit;
224 dev->set_config = &el3_config;
225 dev->get_stats = &el3_get_stats;
226 dev->set_multicast_list = &set_multicast_list;
227 dev->open = &el3_open;
228 dev->stop = &el3_close;
229 #ifdef HAVE_TX_TIMEOUT
230 dev->tx_timeout = el3_tx_timeout;
231 dev->watchdog_timeo = TX_TIMEOUT;
233 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
235 /* Register with Card Services */
236 link->next = dev_list;
238 client_reg.dev_info = &dev_info;
239 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
240 client_reg.EventMask =
241 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
242 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
243 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
244 client_reg.event_handler = &tc589_event;
245 client_reg.Version = 0x0210;
246 client_reg.event_callback_args.client_data = link;
247 ret = pcmcia_register_client(&link->handle, &client_reg);
249 cs_error(link->handle, RegisterClient, ret);
257 /*======================================================================
259 This deletes a driver "instance". The device is de-registered
260 with Card Services. If it has been released, all local data
261 structures are freed. Otherwise, the structures will be freed
262 when the device is released.
264 ======================================================================*/
266 static void tc589_detach(dev_link_t *link)
268 struct net_device *dev = link->priv;
271 DEBUG(0, "3c589_detach(0x%p)\n", link);
273 /* Locate device structure */
274 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
275 if (*linkp == link) break;
279 if (link->state & DEV_CONFIG)
283 pcmcia_deregister_client(link->handle);
285 /* Unlink device structure, free bits */
288 unregister_netdev(dev);
292 /*======================================================================
294 tc589_config() is scheduled to run after a CARD_INSERTION event
295 is received, to configure the PCMCIA socket, and to make the
296 ethernet device available to the system.
298 ======================================================================*/
300 #define CS_CHECK(fn, ret) \
301 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
303 static void tc589_config(dev_link_t *link)
305 client_handle_t handle = link->handle;
306 struct net_device *dev = link->priv;
307 struct el3_private *lp = netdev_priv(dev);
310 u16 buf[32], *phys_addr;
311 int last_fn, last_ret, i, j, multi = 0, fifo;
313 char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
315 DEBUG(0, "3c589_config(0x%p)\n", link);
317 phys_addr = (u16 *)dev->dev_addr;
318 tuple.Attributes = 0;
319 tuple.DesiredTuple = CISTPL_CONFIG;
320 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
321 tuple.TupleData = (cisdata_t *)buf;
322 tuple.TupleDataMax = sizeof(buf);
323 tuple.TupleOffset = 0;
324 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
325 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
326 link->conf.ConfigBase = parse.config.base;
327 link->conf.Present = parse.config.rmask[0];
329 /* Is this a 3c562? */
330 tuple.DesiredTuple = CISTPL_MANFID;
331 tuple.Attributes = TUPLE_RETURN_COMMON;
332 if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
333 (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
334 if (le16_to_cpu(buf[0]) != MANFID_3COM)
335 printk(KERN_INFO "3c589_cs: hmmm, is this really a "
337 multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
341 link->state |= DEV_CONFIG;
343 /* For the 3c562, the base address must be xx00-xx7f */
344 link->io.IOAddrLines = 16;
345 for (i = j = 0; j < 0x400; j += 0x10) {
346 if (multi && (j & 0x80)) continue;
347 link->io.BasePort1 = j ^ 0x300;
348 i = pcmcia_request_io(link->handle, &link->io);
349 if (i == CS_SUCCESS) break;
351 if (i != CS_SUCCESS) {
352 cs_error(link->handle, RequestIO, i);
355 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
356 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
358 dev->irq = link->irq.AssignedIRQ;
359 dev->base_addr = link->io.BasePort1;
360 ioaddr = dev->base_addr;
363 /* The 3c589 has an extra EEPROM for configuration info, including
364 the hardware address. The 3c562 puts the address in the CIS. */
365 tuple.DesiredTuple = 0x88;
366 if (pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) {
367 pcmcia_get_tuple_data(handle, &tuple);
368 for (i = 0; i < 3; i++)
369 phys_addr[i] = htons(buf[i]);
371 for (i = 0; i < 3; i++)
372 phys_addr[i] = htons(read_eeprom(ioaddr, i));
373 if (phys_addr[0] == 0x6060) {
374 printk(KERN_ERR "3c589_cs: IO port conflict at 0x%03lx"
375 "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
380 /* The address and resource configuration register aren't loaded from
381 the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
382 outw(0x3f00, ioaddr + 8);
385 /* The if_port symbol can be set when the module is loaded */
386 if ((if_port >= 0) && (if_port <= 3))
387 dev->if_port = if_port;
389 printk(KERN_ERR "3c589_cs: invalid if_port requested\n");
391 link->dev = &lp->node;
392 link->state &= ~DEV_CONFIG_PENDING;
394 if (register_netdev(dev) != 0) {
395 printk(KERN_ERR "3c589_cs: register_netdev() failed\n");
400 strcpy(lp->node.dev_name, dev->name);
402 printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
403 dev->name, (multi ? "562" : "589"), dev->base_addr,
405 for (i = 0; i < 6; i++)
406 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
407 printk(KERN_INFO " %dK FIFO split %s Rx:Tx, %s xcvr\n",
408 (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
409 if_names[dev->if_port]);
413 cs_error(link->handle, last_fn, last_ret);
420 /*======================================================================
422 After a card is removed, tc589_release() will unregister the net
423 device, and release the PCMCIA configuration. If the device is
424 still open, this will be postponed until it is closed.
426 ======================================================================*/
428 static void tc589_release(dev_link_t *link)
430 DEBUG(0, "3c589_release(0x%p)\n", link);
432 pcmcia_release_configuration(link->handle);
433 pcmcia_release_io(link->handle, &link->io);
434 pcmcia_release_irq(link->handle, &link->irq);
436 link->state &= ~DEV_CONFIG;
439 /*======================================================================
441 The card status event handler. Mostly, this schedules other
442 stuff to run after an event is received. A CARD_REMOVAL event
443 also sets some flags to discourage the net drivers from trying
444 to talk to the card any more.
446 ======================================================================*/
448 static int tc589_event(event_t event, int priority,
449 event_callback_args_t *args)
451 dev_link_t *link = args->client_data;
452 struct net_device *dev = link->priv;
454 DEBUG(1, "3c589_event(0x%06x)\n", event);
457 case CS_EVENT_CARD_REMOVAL:
458 link->state &= ~DEV_PRESENT;
459 if (link->state & DEV_CONFIG) {
460 netif_device_detach(dev);
464 case CS_EVENT_CARD_INSERTION:
465 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
468 case CS_EVENT_PM_SUSPEND:
469 link->state |= DEV_SUSPEND;
470 /* Fall through... */
471 case CS_EVENT_RESET_PHYSICAL:
472 if (link->state & DEV_CONFIG) {
474 netif_device_detach(dev);
475 pcmcia_release_configuration(link->handle);
478 case CS_EVENT_PM_RESUME:
479 link->state &= ~DEV_SUSPEND;
480 /* Fall through... */
481 case CS_EVENT_CARD_RESET:
482 if (link->state & DEV_CONFIG) {
483 pcmcia_request_configuration(link->handle, &link->conf);
486 netif_device_attach(dev);
494 /*====================================================================*/
497 Use this for commands that may take time to finish
499 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
502 outw(cmd, dev->base_addr + EL3_CMD);
504 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
506 printk(KERN_WARNING "%s: command 0x%04x did not complete!\n",
511 Read a word from the EEPROM using the regular EEPROM access register.
512 Assume that we are in register window zero.
514 static u16 read_eeprom(ioaddr_t ioaddr, int index)
517 outw(EEPROM_READ + index, ioaddr + 10);
518 /* Reading the eeprom takes 162 us */
519 for (i = 1620; i >= 0; i--)
520 if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
522 return inw(ioaddr + 12);
526 Set transceiver type, perhaps to something other than what the user
527 specified in dev->if_port.
529 static void tc589_set_xcvr(struct net_device *dev, int if_port)
531 struct el3_private *lp = netdev_priv(dev);
532 ioaddr_t ioaddr = dev->base_addr;
536 case 0: case 1: outw(0, ioaddr + 6); break;
537 case 2: outw(3<<14, ioaddr + 6); break;
538 case 3: outw(1<<14, ioaddr + 6); break;
540 /* On PCMCIA, this just turns on the LED */
541 outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
542 /* 10baseT interface, enable link beat and jabber check. */
544 outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
547 lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
549 lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
552 static void dump_status(struct net_device *dev)
554 ioaddr_t ioaddr = dev->base_addr;
556 printk(KERN_INFO " irq status %04x, rx status %04x, tx status "
557 "%02x tx free %04x\n", inw(ioaddr+EL3_STATUS),
558 inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
559 inw(ioaddr+TX_FREE));
561 printk(KERN_INFO " diagnostics: fifo %04x net %04x ethernet %04x"
562 " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
563 inw(ioaddr+0x08), inw(ioaddr+0x0a));
567 /* Reset and restore all of the 3c589 registers. */
568 static void tc589_reset(struct net_device *dev)
570 ioaddr_t ioaddr = dev->base_addr;
574 outw(0x0001, ioaddr + 4); /* Activate board. */
575 outw(0x3f00, ioaddr + 8); /* Set the IRQ line. */
577 /* Set the station address in window 2. */
579 for (i = 0; i < 6; i++)
580 outb(dev->dev_addr[i], ioaddr + i);
582 tc589_set_xcvr(dev, dev->if_port);
584 /* Switch to the stats window, and clear all stats by reading. */
585 outw(StatsDisable, ioaddr + EL3_CMD);
587 for (i = 0; i < 9; i++)
592 /* Switch to register set 1 for normal use. */
595 /* Accept b-cast and phys addr only. */
596 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
597 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
598 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
599 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
600 /* Allow status bits to be seen. */
601 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
602 /* Ack all pending events, and set active indicator mask. */
603 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
605 outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
606 | AdapterFailure, ioaddr + EL3_CMD);
609 static void netdev_get_drvinfo(struct net_device *dev,
610 struct ethtool_drvinfo *info)
612 strcpy(info->driver, DRV_NAME);
613 strcpy(info->version, DRV_VERSION);
614 sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
618 static u32 netdev_get_msglevel(struct net_device *dev)
623 static void netdev_set_msglevel(struct net_device *dev, u32 level)
627 #endif /* PCMCIA_DEBUG */
629 static struct ethtool_ops netdev_ethtool_ops = {
630 .get_drvinfo = netdev_get_drvinfo,
632 .get_msglevel = netdev_get_msglevel,
633 .set_msglevel = netdev_set_msglevel,
634 #endif /* PCMCIA_DEBUG */
637 static int el3_config(struct net_device *dev, struct ifmap *map)
639 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
640 if (map->port <= 3) {
641 dev->if_port = map->port;
642 printk(KERN_INFO "%s: switched to %s port\n",
643 dev->name, if_names[dev->if_port]);
644 tc589_set_xcvr(dev, dev->if_port);
651 static int el3_open(struct net_device *dev)
653 struct el3_private *lp = netdev_priv(dev);
654 dev_link_t *link = &lp->link;
660 netif_start_queue(dev);
663 init_timer(&lp->media);
664 lp->media.function = &media_check;
665 lp->media.data = (unsigned long) dev;
666 lp->media.expires = jiffies + HZ;
667 add_timer(&lp->media);
669 DEBUG(1, "%s: opened, status %4.4x.\n",
670 dev->name, inw(dev->base_addr + EL3_STATUS));
675 static void el3_tx_timeout(struct net_device *dev)
677 struct el3_private *lp = netdev_priv(dev);
678 ioaddr_t ioaddr = dev->base_addr;
680 printk(KERN_WARNING "%s: Transmit timed out!\n", dev->name);
682 lp->stats.tx_errors++;
683 dev->trans_start = jiffies;
684 /* Issue TX_RESET and TX_START commands. */
685 tc589_wait_for_completion(dev, TxReset);
686 outw(TxEnable, ioaddr + EL3_CMD);
687 netif_wake_queue(dev);
690 static void pop_tx_status(struct net_device *dev)
692 struct el3_private *lp = netdev_priv(dev);
693 ioaddr_t ioaddr = dev->base_addr;
696 /* Clear the Tx status stack. */
697 for (i = 32; i > 0; i--) {
698 u_char tx_status = inb(ioaddr + TX_STATUS);
699 if (!(tx_status & 0x84)) break;
700 /* reset transmitter on jabber error or underrun */
701 if (tx_status & 0x30)
702 tc589_wait_for_completion(dev, TxReset);
703 if (tx_status & 0x38) {
704 DEBUG(1, "%s: transmit error: status 0x%02x\n",
705 dev->name, tx_status);
706 outw(TxEnable, ioaddr + EL3_CMD);
707 lp->stats.tx_aborted_errors++;
709 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
713 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
715 ioaddr_t ioaddr = dev->base_addr;
716 struct el3_private *priv = netdev_priv(dev);
718 DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
719 "status %4.4x.\n", dev->name, (long)skb->len,
720 inw(ioaddr + EL3_STATUS));
722 priv->stats.tx_bytes += skb->len;
724 /* Put out the doubleword header... */
725 outw(skb->len, ioaddr + TX_FIFO);
726 outw(0x00, ioaddr + TX_FIFO);
727 /* ... and the packet rounded to a doubleword. */
728 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
730 dev->trans_start = jiffies;
731 if (inw(ioaddr + TX_FREE) <= 1536) {
732 netif_stop_queue(dev);
733 /* Interrupt us when the FIFO has room for max-sized packet. */
734 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
743 /* The EL3 interrupt handler. */
744 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
746 struct net_device *dev = (struct net_device *) dev_id;
747 struct el3_private *lp = netdev_priv(dev);
748 ioaddr_t ioaddr, status;
749 int i = 0, handled = 1;
751 if (!netif_device_present(dev))
754 ioaddr = dev->base_addr;
756 DEBUG(3, "%s: interrupt, status %4.4x.\n",
757 dev->name, inw(ioaddr + EL3_STATUS));
759 spin_lock(&lp->lock);
760 while ((status = inw(ioaddr + EL3_STATUS)) &
761 (IntLatch | RxComplete | StatsFull)) {
762 if ((status & 0xe000) != 0x2000) {
763 DEBUG(1, "%s: interrupt from dead card\n", dev->name);
768 if (status & RxComplete)
771 if (status & TxAvailable) {
772 DEBUG(3, " TX room bit was handled.\n");
773 /* There's room in the FIFO for a full-sized packet. */
774 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
775 netif_wake_queue(dev);
778 if (status & TxComplete)
781 if (status & (AdapterFailure | RxEarly | StatsFull)) {
782 /* Handle all uncommon interrupts. */
783 if (status & StatsFull) /* Empty statistics. */
785 if (status & RxEarly) { /* Rx early is unused. */
787 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
789 if (status & AdapterFailure) {
792 fifo_diag = inw(ioaddr + 4);
794 printk(KERN_WARNING "%s: adapter failure, FIFO diagnostic"
795 " register %04x.\n", dev->name, fifo_diag);
796 if (fifo_diag & 0x0400) {
798 tc589_wait_for_completion(dev, TxReset);
799 outw(TxEnable, ioaddr + EL3_CMD);
801 if (fifo_diag & 0x2000) {
803 tc589_wait_for_completion(dev, RxReset);
804 set_multicast_list(dev);
805 outw(RxEnable, ioaddr + EL3_CMD);
807 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
812 printk(KERN_ERR "%s: infinite loop in interrupt, "
813 "status %4.4x.\n", dev->name, status);
814 /* Clear all interrupts */
815 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
818 /* Acknowledge the IRQ. */
819 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
822 lp->last_irq = jiffies;
823 spin_unlock(&lp->lock);
824 DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
825 dev->name, inw(ioaddr + EL3_STATUS));
826 return IRQ_RETVAL(handled);
829 static void media_check(unsigned long arg)
831 struct net_device *dev = (struct net_device *)(arg);
832 struct el3_private *lp = netdev_priv(dev);
833 ioaddr_t ioaddr = dev->base_addr;
837 if (!netif_device_present(dev)) goto reschedule;
840 /* Check for pending interrupt with expired latency timer: with
841 this, we can limp along even if the interrupt is blocked */
842 if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
843 (inb(ioaddr + EL3_TIMER) == 0xff)) {
845 printk(KERN_WARNING "%s: interrupt(s) dropped!\n", dev->name);
846 el3_interrupt(dev->irq, lp, NULL);
851 lp->media.expires = jiffies + HZ/100;
852 add_timer(&lp->media);
856 /* lp->lock guards the EL3 window. Window should always be 1 except
857 when the lock is held */
858 spin_lock_irqsave(&lp->lock, flags);
860 media = inw(ioaddr+WN4_MEDIA) & 0xc810;
862 /* Ignore collisions unless we've had no irq's recently */
863 if (jiffies - lp->last_irq < HZ) {
866 /* Try harder to detect carrier errors */
868 outw(StatsDisable, ioaddr + EL3_CMD);
869 errs = inb(ioaddr + 0);
870 outw(StatsEnable, ioaddr + EL3_CMD);
871 lp->stats.tx_carrier_errors += errs;
872 if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
875 if (media != lp->media_status) {
876 if ((media & lp->media_status & 0x8000) &&
877 ((lp->media_status ^ media) & 0x0800))
878 printk(KERN_INFO "%s: %s link beat\n", dev->name,
879 (lp->media_status & 0x0800 ? "lost" : "found"));
880 else if ((media & lp->media_status & 0x4000) &&
881 ((lp->media_status ^ media) & 0x0010))
882 printk(KERN_INFO "%s: coax cable %s\n", dev->name,
883 (lp->media_status & 0x0010 ? "ok" : "problem"));
884 if (dev->if_port == 0) {
885 if (media & 0x8000) {
887 printk(KERN_INFO "%s: flipped to 10baseT\n",
890 tc589_set_xcvr(dev, 2);
891 } else if (media & 0x4000) {
893 tc589_set_xcvr(dev, 1);
895 printk(KERN_INFO "%s: flipped to 10base2\n",
899 lp->media_status = media;
903 spin_unlock_irqrestore(&lp->lock, flags);
906 lp->media.expires = jiffies + HZ;
907 add_timer(&lp->media);
910 static struct net_device_stats *el3_get_stats(struct net_device *dev)
912 struct el3_private *lp = netdev_priv(dev);
914 dev_link_t *link = &lp->link;
917 spin_lock_irqsave(&lp->lock, flags);
919 spin_unlock_irqrestore(&lp->lock, flags);
925 Update statistics. We change to register window 6, so this should be run
926 single-threaded if the device is active. This is expected to be a rare
927 operation, and it's simpler for the rest of the driver to assume that
928 window 1 is always valid rather than use a special window-state variable.
930 Caller must hold the lock for this
932 static void update_stats(struct net_device *dev)
934 struct el3_private *lp = netdev_priv(dev);
935 ioaddr_t ioaddr = dev->base_addr;
937 DEBUG(2, "%s: updating the statistics.\n", dev->name);
938 /* Turn off statistics updates while reading. */
939 outw(StatsDisable, ioaddr + EL3_CMD);
940 /* Switch to the stats window, and read everything. */
942 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
943 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
944 /* Multiple collisions. */ inb(ioaddr + 2);
945 lp->stats.collisions += inb(ioaddr + 3);
946 lp->stats.tx_window_errors += inb(ioaddr + 4);
947 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
948 lp->stats.tx_packets += inb(ioaddr + 6);
949 /* Rx packets */ inb(ioaddr + 7);
950 /* Tx deferrals */ inb(ioaddr + 8);
951 /* Rx octets */ inw(ioaddr + 10);
952 /* Tx octets */ inw(ioaddr + 12);
954 /* Back to window 1, and turn statistics back on. */
956 outw(StatsEnable, ioaddr + EL3_CMD);
959 static int el3_rx(struct net_device *dev)
961 struct el3_private *lp = netdev_priv(dev);
962 ioaddr_t ioaddr = dev->base_addr;
966 DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
967 dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
968 while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
969 (--worklimit >= 0)) {
970 if (rx_status & 0x4000) { /* Error, update stats. */
971 short error = rx_status & 0x3800;
972 lp->stats.rx_errors++;
974 case 0x0000: lp->stats.rx_over_errors++; break;
975 case 0x0800: lp->stats.rx_length_errors++; break;
976 case 0x1000: lp->stats.rx_frame_errors++; break;
977 case 0x1800: lp->stats.rx_length_errors++; break;
978 case 0x2000: lp->stats.rx_frame_errors++; break;
979 case 0x2800: lp->stats.rx_crc_errors++; break;
982 short pkt_len = rx_status & 0x7ff;
985 skb = dev_alloc_skb(pkt_len+5);
987 DEBUG(3, " Receiving packet size %d status %4.4x.\n",
992 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
994 skb->protocol = eth_type_trans(skb, dev);
996 dev->last_rx = jiffies;
997 lp->stats.rx_packets++;
998 lp->stats.rx_bytes += pkt_len;
1000 DEBUG(1, "%s: couldn't allocate a sk_buff of"
1001 " size %d.\n", dev->name, pkt_len);
1002 lp->stats.rx_dropped++;
1005 /* Pop the top of the Rx FIFO */
1006 tc589_wait_for_completion(dev, RxDiscard);
1009 printk(KERN_WARNING "%s: too much work in el3_rx!\n", dev->name);
1013 static void set_multicast_list(struct net_device *dev)
1015 struct el3_private *lp = netdev_priv(dev);
1016 dev_link_t *link = &lp->link;
1017 ioaddr_t ioaddr = dev->base_addr;
1018 u16 opts = SetRxFilter | RxStation | RxBroadcast;
1020 if (!(DEV_OK(link))) return;
1021 if (dev->flags & IFF_PROMISC)
1022 opts |= RxMulticast | RxProm;
1023 else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1024 opts |= RxMulticast;
1025 outw(opts, ioaddr + EL3_CMD);
1028 static int el3_close(struct net_device *dev)
1030 struct el3_private *lp = netdev_priv(dev);
1031 dev_link_t *link = &lp->link;
1032 ioaddr_t ioaddr = dev->base_addr;
1034 DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1037 /* Turn off statistics ASAP. We update lp->stats below. */
1038 outw(StatsDisable, ioaddr + EL3_CMD);
1040 /* Disable the receiver and transmitter. */
1041 outw(RxDisable, ioaddr + EL3_CMD);
1042 outw(TxDisable, ioaddr + EL3_CMD);
1044 if (dev->if_port == 2)
1045 /* Turn off thinnet power. Green! */
1046 outw(StopCoax, ioaddr + EL3_CMD);
1047 else if (dev->if_port == 1) {
1048 /* Disable link beat and jabber */
1050 outw(0, ioaddr + WN4_MEDIA);
1053 /* Switching back to window 0 disables the IRQ. */
1055 /* But we explicitly zero the IRQ line select anyway. */
1056 outw(0x0f00, ioaddr + WN0_IRQ);
1058 /* Check if the card still exists */
1059 if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1064 netif_stop_queue(dev);
1065 del_timer_sync(&lp->media);
1070 static struct pcmcia_driver tc589_driver = {
1071 .owner = THIS_MODULE,
1075 .attach = tc589_attach,
1076 .detach = tc589_detach,
1079 static int __init init_tc589(void)
1081 return pcmcia_register_driver(&tc589_driver);
1084 static void __exit exit_tc589(void)
1086 pcmcia_unregister_driver(&tc589_driver);
1087 while (dev_list != NULL)
1088 tc589_detach(dev_list);
1091 module_init(init_tc589);
1092 module_exit(exit_tc589);