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;
280 unregister_netdev(dev);
282 if (link->state & DEV_CONFIG)
286 pcmcia_deregister_client(link->handle);
288 /* Unlink device structure, free bits */
293 /*======================================================================
295 tc589_config() is scheduled to run after a CARD_INSERTION event
296 is received, to configure the PCMCIA socket, and to make the
297 ethernet device available to the system.
299 ======================================================================*/
301 #define CS_CHECK(fn, ret) \
302 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
304 static void tc589_config(dev_link_t *link)
306 client_handle_t handle = link->handle;
307 struct net_device *dev = link->priv;
308 struct el3_private *lp = netdev_priv(dev);
311 u16 buf[32], *phys_addr;
312 int last_fn, last_ret, i, j, multi = 0, fifo;
314 char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
316 DEBUG(0, "3c589_config(0x%p)\n", link);
318 phys_addr = (u16 *)dev->dev_addr;
319 tuple.Attributes = 0;
320 tuple.DesiredTuple = CISTPL_CONFIG;
321 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
322 tuple.TupleData = (cisdata_t *)buf;
323 tuple.TupleDataMax = sizeof(buf);
324 tuple.TupleOffset = 0;
325 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
326 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
327 link->conf.ConfigBase = parse.config.base;
328 link->conf.Present = parse.config.rmask[0];
330 /* Is this a 3c562? */
331 tuple.DesiredTuple = CISTPL_MANFID;
332 tuple.Attributes = TUPLE_RETURN_COMMON;
333 if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
334 (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
335 if (le16_to_cpu(buf[0]) != MANFID_3COM)
336 printk(KERN_INFO "3c589_cs: hmmm, is this really a "
338 multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
342 link->state |= DEV_CONFIG;
344 /* For the 3c562, the base address must be xx00-xx7f */
345 link->io.IOAddrLines = 16;
346 for (i = j = 0; j < 0x400; j += 0x10) {
347 if (multi && (j & 0x80)) continue;
348 link->io.BasePort1 = j ^ 0x300;
349 i = pcmcia_request_io(link->handle, &link->io);
350 if (i == CS_SUCCESS) break;
352 if (i != CS_SUCCESS) {
353 cs_error(link->handle, RequestIO, i);
356 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
357 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
359 dev->irq = link->irq.AssignedIRQ;
360 dev->base_addr = link->io.BasePort1;
361 ioaddr = dev->base_addr;
364 /* The 3c589 has an extra EEPROM for configuration info, including
365 the hardware address. The 3c562 puts the address in the CIS. */
366 tuple.DesiredTuple = 0x88;
367 if (pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) {
368 pcmcia_get_tuple_data(handle, &tuple);
369 for (i = 0; i < 3; i++)
370 phys_addr[i] = htons(buf[i]);
372 for (i = 0; i < 3; i++)
373 phys_addr[i] = htons(read_eeprom(ioaddr, i));
374 if (phys_addr[0] == 0x6060) {
375 printk(KERN_ERR "3c589_cs: IO port conflict at 0x%03lx"
376 "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
381 /* The address and resource configuration register aren't loaded from
382 the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
383 outw(0x3f00, ioaddr + 8);
386 /* The if_port symbol can be set when the module is loaded */
387 if ((if_port >= 0) && (if_port <= 3))
388 dev->if_port = if_port;
390 printk(KERN_ERR "3c589_cs: invalid if_port requested\n");
392 link->dev = &lp->node;
393 link->state &= ~DEV_CONFIG_PENDING;
395 if (register_netdev(dev) != 0) {
396 printk(KERN_ERR "3c589_cs: register_netdev() failed\n");
401 strcpy(lp->node.dev_name, dev->name);
403 printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
404 dev->name, (multi ? "562" : "589"), dev->base_addr,
406 for (i = 0; i < 6; i++)
407 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
408 printk(KERN_INFO " %dK FIFO split %s Rx:Tx, %s xcvr\n",
409 (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
410 if_names[dev->if_port]);
414 cs_error(link->handle, last_fn, last_ret);
421 /*======================================================================
423 After a card is removed, tc589_release() will unregister the net
424 device, and release the PCMCIA configuration. If the device is
425 still open, this will be postponed until it is closed.
427 ======================================================================*/
429 static void tc589_release(dev_link_t *link)
431 DEBUG(0, "3c589_release(0x%p)\n", link);
433 pcmcia_release_configuration(link->handle);
434 pcmcia_release_io(link->handle, &link->io);
435 pcmcia_release_irq(link->handle, &link->irq);
437 link->state &= ~DEV_CONFIG;
440 /*======================================================================
442 The card status event handler. Mostly, this schedules other
443 stuff to run after an event is received. A CARD_REMOVAL event
444 also sets some flags to discourage the net drivers from trying
445 to talk to the card any more.
447 ======================================================================*/
449 static int tc589_event(event_t event, int priority,
450 event_callback_args_t *args)
452 dev_link_t *link = args->client_data;
453 struct net_device *dev = link->priv;
455 DEBUG(1, "3c589_event(0x%06x)\n", event);
458 case CS_EVENT_CARD_REMOVAL:
459 link->state &= ~DEV_PRESENT;
460 if (link->state & DEV_CONFIG)
461 netif_device_detach(dev);
463 case CS_EVENT_CARD_INSERTION:
464 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
467 case CS_EVENT_PM_SUSPEND:
468 link->state |= DEV_SUSPEND;
469 /* Fall through... */
470 case CS_EVENT_RESET_PHYSICAL:
471 if (link->state & DEV_CONFIG) {
473 netif_device_detach(dev);
474 pcmcia_release_configuration(link->handle);
477 case CS_EVENT_PM_RESUME:
478 link->state &= ~DEV_SUSPEND;
479 /* Fall through... */
480 case CS_EVENT_CARD_RESET:
481 if (link->state & DEV_CONFIG) {
482 pcmcia_request_configuration(link->handle, &link->conf);
485 netif_device_attach(dev);
493 /*====================================================================*/
496 Use this for commands that may take time to finish
498 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
501 outw(cmd, dev->base_addr + EL3_CMD);
503 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
505 printk(KERN_WARNING "%s: command 0x%04x did not complete!\n",
510 Read a word from the EEPROM using the regular EEPROM access register.
511 Assume that we are in register window zero.
513 static u16 read_eeprom(ioaddr_t ioaddr, int index)
516 outw(EEPROM_READ + index, ioaddr + 10);
517 /* Reading the eeprom takes 162 us */
518 for (i = 1620; i >= 0; i--)
519 if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
521 return inw(ioaddr + 12);
525 Set transceiver type, perhaps to something other than what the user
526 specified in dev->if_port.
528 static void tc589_set_xcvr(struct net_device *dev, int if_port)
530 struct el3_private *lp = netdev_priv(dev);
531 ioaddr_t ioaddr = dev->base_addr;
535 case 0: case 1: outw(0, ioaddr + 6); break;
536 case 2: outw(3<<14, ioaddr + 6); break;
537 case 3: outw(1<<14, ioaddr + 6); break;
539 /* On PCMCIA, this just turns on the LED */
540 outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
541 /* 10baseT interface, enable link beat and jabber check. */
543 outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
546 lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
548 lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
551 static void dump_status(struct net_device *dev)
553 ioaddr_t ioaddr = dev->base_addr;
555 printk(KERN_INFO " irq status %04x, rx status %04x, tx status "
556 "%02x tx free %04x\n", inw(ioaddr+EL3_STATUS),
557 inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
558 inw(ioaddr+TX_FREE));
560 printk(KERN_INFO " diagnostics: fifo %04x net %04x ethernet %04x"
561 " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
562 inw(ioaddr+0x08), inw(ioaddr+0x0a));
566 /* Reset and restore all of the 3c589 registers. */
567 static void tc589_reset(struct net_device *dev)
569 ioaddr_t ioaddr = dev->base_addr;
573 outw(0x0001, ioaddr + 4); /* Activate board. */
574 outw(0x3f00, ioaddr + 8); /* Set the IRQ line. */
576 /* Set the station address in window 2. */
578 for (i = 0; i < 6; i++)
579 outb(dev->dev_addr[i], ioaddr + i);
581 tc589_set_xcvr(dev, dev->if_port);
583 /* Switch to the stats window, and clear all stats by reading. */
584 outw(StatsDisable, ioaddr + EL3_CMD);
586 for (i = 0; i < 9; i++)
591 /* Switch to register set 1 for normal use. */
594 /* Accept b-cast and phys addr only. */
595 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
596 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
597 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
598 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
599 /* Allow status bits to be seen. */
600 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
601 /* Ack all pending events, and set active indicator mask. */
602 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
604 outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
605 | AdapterFailure, ioaddr + EL3_CMD);
608 static void netdev_get_drvinfo(struct net_device *dev,
609 struct ethtool_drvinfo *info)
611 strcpy(info->driver, DRV_NAME);
612 strcpy(info->version, DRV_VERSION);
613 sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
617 static u32 netdev_get_msglevel(struct net_device *dev)
622 static void netdev_set_msglevel(struct net_device *dev, u32 level)
626 #endif /* PCMCIA_DEBUG */
628 static struct ethtool_ops netdev_ethtool_ops = {
629 .get_drvinfo = netdev_get_drvinfo,
631 .get_msglevel = netdev_get_msglevel,
632 .set_msglevel = netdev_set_msglevel,
633 #endif /* PCMCIA_DEBUG */
636 static int el3_config(struct net_device *dev, struct ifmap *map)
638 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
639 if (map->port <= 3) {
640 dev->if_port = map->port;
641 printk(KERN_INFO "%s: switched to %s port\n",
642 dev->name, if_names[dev->if_port]);
643 tc589_set_xcvr(dev, dev->if_port);
650 static int el3_open(struct net_device *dev)
652 struct el3_private *lp = netdev_priv(dev);
653 dev_link_t *link = &lp->link;
659 netif_start_queue(dev);
662 init_timer(&lp->media);
663 lp->media.function = &media_check;
664 lp->media.data = (unsigned long) dev;
665 lp->media.expires = jiffies + HZ;
666 add_timer(&lp->media);
668 DEBUG(1, "%s: opened, status %4.4x.\n",
669 dev->name, inw(dev->base_addr + EL3_STATUS));
674 static void el3_tx_timeout(struct net_device *dev)
676 struct el3_private *lp = netdev_priv(dev);
677 ioaddr_t ioaddr = dev->base_addr;
679 printk(KERN_WARNING "%s: Transmit timed out!\n", dev->name);
681 lp->stats.tx_errors++;
682 dev->trans_start = jiffies;
683 /* Issue TX_RESET and TX_START commands. */
684 tc589_wait_for_completion(dev, TxReset);
685 outw(TxEnable, ioaddr + EL3_CMD);
686 netif_wake_queue(dev);
689 static void pop_tx_status(struct net_device *dev)
691 struct el3_private *lp = netdev_priv(dev);
692 ioaddr_t ioaddr = dev->base_addr;
695 /* Clear the Tx status stack. */
696 for (i = 32; i > 0; i--) {
697 u_char tx_status = inb(ioaddr + TX_STATUS);
698 if (!(tx_status & 0x84)) break;
699 /* reset transmitter on jabber error or underrun */
700 if (tx_status & 0x30)
701 tc589_wait_for_completion(dev, TxReset);
702 if (tx_status & 0x38) {
703 DEBUG(1, "%s: transmit error: status 0x%02x\n",
704 dev->name, tx_status);
705 outw(TxEnable, ioaddr + EL3_CMD);
706 lp->stats.tx_aborted_errors++;
708 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
712 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
714 ioaddr_t ioaddr = dev->base_addr;
715 struct el3_private *priv = netdev_priv(dev);
717 DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
718 "status %4.4x.\n", dev->name, (long)skb->len,
719 inw(ioaddr + EL3_STATUS));
721 priv->stats.tx_bytes += skb->len;
723 /* Put out the doubleword header... */
724 outw(skb->len, ioaddr + TX_FIFO);
725 outw(0x00, ioaddr + TX_FIFO);
726 /* ... and the packet rounded to a doubleword. */
727 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
729 dev->trans_start = jiffies;
730 if (inw(ioaddr + TX_FREE) <= 1536) {
731 netif_stop_queue(dev);
732 /* Interrupt us when the FIFO has room for max-sized packet. */
733 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
742 /* The EL3 interrupt handler. */
743 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
745 struct net_device *dev = (struct net_device *) dev_id;
746 struct el3_private *lp = netdev_priv(dev);
747 ioaddr_t ioaddr, status;
748 int i = 0, handled = 1;
750 if (!netif_device_present(dev))
753 ioaddr = dev->base_addr;
755 DEBUG(3, "%s: interrupt, status %4.4x.\n",
756 dev->name, inw(ioaddr + EL3_STATUS));
758 spin_lock(&lp->lock);
759 while ((status = inw(ioaddr + EL3_STATUS)) &
760 (IntLatch | RxComplete | StatsFull)) {
761 if ((status & 0xe000) != 0x2000) {
762 DEBUG(1, "%s: interrupt from dead card\n", dev->name);
767 if (status & RxComplete)
770 if (status & TxAvailable) {
771 DEBUG(3, " TX room bit was handled.\n");
772 /* There's room in the FIFO for a full-sized packet. */
773 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
774 netif_wake_queue(dev);
777 if (status & TxComplete)
780 if (status & (AdapterFailure | RxEarly | StatsFull)) {
781 /* Handle all uncommon interrupts. */
782 if (status & StatsFull) /* Empty statistics. */
784 if (status & RxEarly) { /* Rx early is unused. */
786 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
788 if (status & AdapterFailure) {
791 fifo_diag = inw(ioaddr + 4);
793 printk(KERN_WARNING "%s: adapter failure, FIFO diagnostic"
794 " register %04x.\n", dev->name, fifo_diag);
795 if (fifo_diag & 0x0400) {
797 tc589_wait_for_completion(dev, TxReset);
798 outw(TxEnable, ioaddr + EL3_CMD);
800 if (fifo_diag & 0x2000) {
802 tc589_wait_for_completion(dev, RxReset);
803 set_multicast_list(dev);
804 outw(RxEnable, ioaddr + EL3_CMD);
806 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
811 printk(KERN_ERR "%s: infinite loop in interrupt, "
812 "status %4.4x.\n", dev->name, status);
813 /* Clear all interrupts */
814 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
817 /* Acknowledge the IRQ. */
818 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
821 lp->last_irq = jiffies;
822 spin_unlock(&lp->lock);
823 DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
824 dev->name, inw(ioaddr + EL3_STATUS));
825 return IRQ_RETVAL(handled);
828 static void media_check(unsigned long arg)
830 struct net_device *dev = (struct net_device *)(arg);
831 struct el3_private *lp = netdev_priv(dev);
832 ioaddr_t ioaddr = dev->base_addr;
836 if (!netif_device_present(dev)) goto reschedule;
839 /* Check for pending interrupt with expired latency timer: with
840 this, we can limp along even if the interrupt is blocked */
841 if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
842 (inb(ioaddr + EL3_TIMER) == 0xff)) {
844 printk(KERN_WARNING "%s: interrupt(s) dropped!\n", dev->name);
845 el3_interrupt(dev->irq, lp, NULL);
850 lp->media.expires = jiffies + HZ/100;
851 add_timer(&lp->media);
855 /* lp->lock guards the EL3 window. Window should always be 1 except
856 when the lock is held */
857 spin_lock_irqsave(&lp->lock, flags);
859 media = inw(ioaddr+WN4_MEDIA) & 0xc810;
861 /* Ignore collisions unless we've had no irq's recently */
862 if (jiffies - lp->last_irq < HZ) {
865 /* Try harder to detect carrier errors */
867 outw(StatsDisable, ioaddr + EL3_CMD);
868 errs = inb(ioaddr + 0);
869 outw(StatsEnable, ioaddr + EL3_CMD);
870 lp->stats.tx_carrier_errors += errs;
871 if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
874 if (media != lp->media_status) {
875 if ((media & lp->media_status & 0x8000) &&
876 ((lp->media_status ^ media) & 0x0800))
877 printk(KERN_INFO "%s: %s link beat\n", dev->name,
878 (lp->media_status & 0x0800 ? "lost" : "found"));
879 else if ((media & lp->media_status & 0x4000) &&
880 ((lp->media_status ^ media) & 0x0010))
881 printk(KERN_INFO "%s: coax cable %s\n", dev->name,
882 (lp->media_status & 0x0010 ? "ok" : "problem"));
883 if (dev->if_port == 0) {
884 if (media & 0x8000) {
886 printk(KERN_INFO "%s: flipped to 10baseT\n",
889 tc589_set_xcvr(dev, 2);
890 } else if (media & 0x4000) {
892 tc589_set_xcvr(dev, 1);
894 printk(KERN_INFO "%s: flipped to 10base2\n",
898 lp->media_status = media;
902 spin_unlock_irqrestore(&lp->lock, flags);
905 lp->media.expires = jiffies + HZ;
906 add_timer(&lp->media);
909 static struct net_device_stats *el3_get_stats(struct net_device *dev)
911 struct el3_private *lp = netdev_priv(dev);
913 dev_link_t *link = &lp->link;
916 spin_lock_irqsave(&lp->lock, flags);
918 spin_unlock_irqrestore(&lp->lock, flags);
924 Update statistics. We change to register window 6, so this should be run
925 single-threaded if the device is active. This is expected to be a rare
926 operation, and it's simpler for the rest of the driver to assume that
927 window 1 is always valid rather than use a special window-state variable.
929 Caller must hold the lock for this
931 static void update_stats(struct net_device *dev)
933 struct el3_private *lp = netdev_priv(dev);
934 ioaddr_t ioaddr = dev->base_addr;
936 DEBUG(2, "%s: updating the statistics.\n", dev->name);
937 /* Turn off statistics updates while reading. */
938 outw(StatsDisable, ioaddr + EL3_CMD);
939 /* Switch to the stats window, and read everything. */
941 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
942 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
943 /* Multiple collisions. */ inb(ioaddr + 2);
944 lp->stats.collisions += inb(ioaddr + 3);
945 lp->stats.tx_window_errors += inb(ioaddr + 4);
946 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
947 lp->stats.tx_packets += inb(ioaddr + 6);
948 /* Rx packets */ inb(ioaddr + 7);
949 /* Tx deferrals */ inb(ioaddr + 8);
950 /* Rx octets */ inw(ioaddr + 10);
951 /* Tx octets */ inw(ioaddr + 12);
953 /* Back to window 1, and turn statistics back on. */
955 outw(StatsEnable, ioaddr + EL3_CMD);
958 static int el3_rx(struct net_device *dev)
960 struct el3_private *lp = netdev_priv(dev);
961 ioaddr_t ioaddr = dev->base_addr;
965 DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
966 dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
967 while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
968 (--worklimit >= 0)) {
969 if (rx_status & 0x4000) { /* Error, update stats. */
970 short error = rx_status & 0x3800;
971 lp->stats.rx_errors++;
973 case 0x0000: lp->stats.rx_over_errors++; break;
974 case 0x0800: lp->stats.rx_length_errors++; break;
975 case 0x1000: lp->stats.rx_frame_errors++; break;
976 case 0x1800: lp->stats.rx_length_errors++; break;
977 case 0x2000: lp->stats.rx_frame_errors++; break;
978 case 0x2800: lp->stats.rx_crc_errors++; break;
981 short pkt_len = rx_status & 0x7ff;
984 skb = dev_alloc_skb(pkt_len+5);
986 DEBUG(3, " Receiving packet size %d status %4.4x.\n",
991 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
993 skb->protocol = eth_type_trans(skb, dev);
995 dev->last_rx = jiffies;
996 lp->stats.rx_packets++;
997 lp->stats.rx_bytes += pkt_len;
999 DEBUG(1, "%s: couldn't allocate a sk_buff of"
1000 " size %d.\n", dev->name, pkt_len);
1001 lp->stats.rx_dropped++;
1004 /* Pop the top of the Rx FIFO */
1005 tc589_wait_for_completion(dev, RxDiscard);
1008 printk(KERN_WARNING "%s: too much work in el3_rx!\n", dev->name);
1012 static void set_multicast_list(struct net_device *dev)
1014 struct el3_private *lp = netdev_priv(dev);
1015 dev_link_t *link = &lp->link;
1016 ioaddr_t ioaddr = dev->base_addr;
1017 u16 opts = SetRxFilter | RxStation | RxBroadcast;
1019 if (!(DEV_OK(link))) return;
1020 if (dev->flags & IFF_PROMISC)
1021 opts |= RxMulticast | RxProm;
1022 else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1023 opts |= RxMulticast;
1024 outw(opts, ioaddr + EL3_CMD);
1027 static int el3_close(struct net_device *dev)
1029 struct el3_private *lp = netdev_priv(dev);
1030 dev_link_t *link = &lp->link;
1031 ioaddr_t ioaddr = dev->base_addr;
1033 DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1036 /* Turn off statistics ASAP. We update lp->stats below. */
1037 outw(StatsDisable, ioaddr + EL3_CMD);
1039 /* Disable the receiver and transmitter. */
1040 outw(RxDisable, ioaddr + EL3_CMD);
1041 outw(TxDisable, ioaddr + EL3_CMD);
1043 if (dev->if_port == 2)
1044 /* Turn off thinnet power. Green! */
1045 outw(StopCoax, ioaddr + EL3_CMD);
1046 else if (dev->if_port == 1) {
1047 /* Disable link beat and jabber */
1049 outw(0, ioaddr + WN4_MEDIA);
1052 /* Switching back to window 0 disables the IRQ. */
1054 /* But we explicitly zero the IRQ line select anyway. */
1055 outw(0x0f00, ioaddr + WN0_IRQ);
1057 /* Check if the card still exists */
1058 if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1063 netif_stop_queue(dev);
1064 del_timer_sync(&lp->media);
1069 static struct pcmcia_driver tc589_driver = {
1070 .owner = THIS_MODULE,
1074 .attach = tc589_attach,
1075 .detach = tc589_detach,
1078 static int __init init_tc589(void)
1080 return pcmcia_register_driver(&tc589_driver);
1083 static void __exit exit_tc589(void)
1085 pcmcia_unregister_driver(&tc589_driver);
1086 while (dev_list != NULL)
1087 tc589_detach(dev_list);
1090 module_init(init_tc589);
1091 module_exit(exit_tc589);