- pre2
[davej-history.git] / drivers / net / pcmcia / 3c589_cs.c
blob3cbba20b9050f6a21aa6d74c647ef34d806326e2
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.153 2000/06/12 21:27:25
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 Public License,
15 incorporated herein by reference.
16 Donald Becker may be reached at becker@cesdis1.gsfc.nasa.gov
18 ======================================================================*/
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/ptrace.h>
25 #include <linux/malloc.h>
26 #include <linux/string.h>
27 #include <linux/timer.h>
28 #include <linux/interrupt.h>
29 #include <linux/in.h>
30 #include <linux/delay.h>
31 #include <asm/io.h>
32 #include <asm/system.h>
33 #include <asm/bitops.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/if_arp.h>
39 #include <linux/ioport.h>
41 #include <pcmcia/version.h>
42 #include <pcmcia/cs_types.h>
43 #include <pcmcia/cs.h>
44 #include <pcmcia/cistpl.h>
45 #include <pcmcia/cisreg.h>
46 #include <pcmcia/ciscode.h>
47 #include <pcmcia/ds.h>
49 /* To minimize the size of the driver source I only define operating
50 constants if they are used several times. You'll need the manual
51 if you want to understand driver details. */
52 /* Offsets from base I/O address. */
53 #define EL3_DATA 0x00
54 #define EL3_TIMER 0x0a
55 #define EL3_CMD 0x0e
56 #define EL3_STATUS 0x0e
58 #define EEPROM_READ 0x0080
59 #define EEPROM_BUSY 0x8000
61 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
63 /* The top five bits written to EL3_CMD are a command, the lower
64 11 bits are the parameter, if applicable. */
65 enum c509cmd {
66 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
67 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
68 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
69 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
70 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
71 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
72 StatsDisable = 22<<11, StopCoax = 23<<11,
75 enum c509status {
76 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
77 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
78 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000
81 /* The SetRxFilter command accepts the following classes: */
82 enum RxFilter {
83 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
86 /* Register window 1 offsets, the window used in normal operation. */
87 #define TX_FIFO 0x00
88 #define RX_FIFO 0x00
89 #define RX_STATUS 0x08
90 #define TX_STATUS 0x0B
91 #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
93 #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
94 #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
95 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
96 #define MEDIA_LED 0x0001 /* Enable link light on 3C589E cards. */
98 /* Time in jiffies before concluding Tx hung */
99 #define TX_TIMEOUT ((400*HZ)/1000)
101 struct el3_private {
102 dev_link_t link;
103 struct net_device dev;
104 dev_node_t node;
105 struct net_device_stats stats;
106 /* For transceiver monitoring */
107 struct timer_list media;
108 u_short media_status;
109 u_short fast_poll;
110 u_long last_irq;
113 static char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
115 #ifdef PCMCIA_DEBUG
116 static int pc_debug = PCMCIA_DEBUG;
117 MODULE_PARM(pc_debug, "i");
118 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
119 static char *version =
120 "3c589_cs.c 1.153 2000/06/12 21:27:25 (David Hinds)";
121 #else
122 #define DEBUG(n, args...)
123 #endif
125 /*====================================================================*/
127 /* Parameters that can be set with 'insmod' */
129 /* Special hook for setting if_port when module is loaded */
130 static int if_port = 0;
132 /* Bit map of interrupts to choose from */
133 static u_int irq_mask = 0xdeb8;
134 static int irq_list[4] = { -1 };
136 MODULE_PARM(if_port, "i");
137 MODULE_PARM(irq_mask, "i");
138 MODULE_PARM(irq_list, "1-4i");
140 /*====================================================================*/
142 static void tc589_config(dev_link_t *link);
143 static void tc589_release(u_long arg);
144 static int tc589_event(event_t event, int priority,
145 event_callback_args_t *args);
147 static u_short read_eeprom(ioaddr_t ioaddr, int index);
148 static void tc589_reset(struct net_device *dev);
149 static void media_check(u_long arg);
150 static int el3_config(struct net_device *dev, struct ifmap *map);
151 static int el3_open(struct net_device *dev);
152 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
153 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
154 static void update_stats(struct net_device *dev);
155 static struct net_device_stats *el3_get_stats(struct net_device *dev);
156 static int el3_rx(struct net_device *dev);
157 static int el3_close(struct net_device *dev);
158 static void el3_tx_timeout(struct net_device *dev);
159 static void set_multicast_list(struct net_device *dev);
161 static dev_info_t dev_info = "3c589_cs";
163 static dev_link_t *tc589_attach(void);
164 static void tc589_detach(dev_link_t *);
166 static dev_link_t *dev_list = NULL;
168 /*======================================================================
170 This bit of code is used to avoid unregistering network devices
171 at inappropriate times. 2.2 and later kernels are fairly picky
172 about when this can happen.
174 ======================================================================*/
176 static void flush_stale_links(void)
178 dev_link_t *link, *next;
179 for (link = dev_list; link; link = next) {
180 next = link->next;
181 if (link->state & DEV_STALE_LINK)
182 tc589_detach(link);
186 /*====================================================================*/
188 static void cs_error(client_handle_t handle, int func, int ret)
190 error_info_t err = { func, ret };
191 CardServices(ReportError, handle, &err);
194 /*======================================================================
196 tc589_attach() creates an "instance" of the driver, allocating
197 local data structures for one device. The device is registered
198 with Card Services.
200 ======================================================================*/
202 static dev_link_t *tc589_attach(void)
204 struct el3_private *lp;
205 client_reg_t client_reg;
206 dev_link_t *link;
207 struct net_device *dev;
208 int i, ret;
210 DEBUG(0, "3c589_attach()\n");
211 flush_stale_links();
213 /* Create new ethernet device */
214 lp = kmalloc(sizeof(*lp), GFP_KERNEL);
215 if (!lp) return NULL;
216 memset(lp, 0, sizeof(*lp));
217 link = &lp->link; dev = &lp->dev;
218 link->priv = dev->priv = link->irq.Instance = lp;
220 link->release.function = &tc589_release;
221 link->release.data = (u_long)link;
222 link->io.NumPorts1 = 16;
223 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
224 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
225 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
226 if (irq_list[0] == -1)
227 link->irq.IRQInfo2 = irq_mask;
228 else
229 for (i = 0; i < 4; i++)
230 link->irq.IRQInfo2 |= 1 << irq_list[i];
231 link->irq.Handler = &el3_interrupt;
232 link->conf.Attributes = CONF_ENABLE_IRQ;
233 link->conf.Vcc = 50;
234 link->conf.IntType = INT_MEMORY_AND_IO;
235 link->conf.ConfigIndex = 1;
236 link->conf.Present = PRESENT_OPTION;
238 /* The EL3-specific entries in the device structure. */
239 dev->hard_start_xmit = &el3_start_xmit;
240 dev->set_config = &el3_config;
241 dev->get_stats = &el3_get_stats;
242 dev->set_multicast_list = &set_multicast_list;
243 ether_setup(dev);
244 dev->open = &el3_open;
245 dev->stop = &el3_close;
246 dev->tx_timeout = el3_tx_timeout;
247 dev->watchdog_timeo = TX_TIMEOUT;
249 /* Register with Card Services */
250 link->next = dev_list;
251 dev_list = link;
252 client_reg.dev_info = &dev_info;
253 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
254 client_reg.EventMask =
255 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
256 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
257 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
258 client_reg.event_handler = &tc589_event;
259 client_reg.Version = 0x0210;
260 client_reg.event_callback_args.client_data = link;
261 ret = CardServices(RegisterClient, &link->handle, &client_reg);
262 if (ret != 0) {
263 cs_error(link->handle, RegisterClient, ret);
264 tc589_detach(link);
265 return NULL;
268 return link;
269 } /* tc589_attach */
271 /*======================================================================
273 This deletes a driver "instance". The device is de-registered
274 with Card Services. If it has been released, all local data
275 structures are freed. Otherwise, the structures will be freed
276 when the device is released.
278 ======================================================================*/
280 static void tc589_detach(dev_link_t *link)
282 struct el3_private *lp = link->priv;
283 dev_link_t **linkp;
285 DEBUG(0, "3c589_detach(0x%p)\n", link);
287 /* Locate device structure */
288 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
289 if (*linkp == link) break;
290 if (*linkp == NULL)
291 return;
293 del_timer(&link->release);
294 if (link->state & DEV_CONFIG) {
295 tc589_release((u_long)link);
296 if (link->state & DEV_STALE_CONFIG) {
297 link->state |= DEV_STALE_LINK;
298 return;
302 if (link->handle)
303 CardServices(DeregisterClient, link->handle);
305 /* Unlink device structure, free bits */
306 *linkp = link->next;
307 if (link->dev)
308 unregister_netdev(&lp->dev);
309 kfree(lp);
311 } /* tc589_detach */
313 /*======================================================================
315 tc589_config() is scheduled to run after a CARD_INSERTION event
316 is received, to configure the PCMCIA socket, and to make the
317 ethernet device available to the system.
319 ======================================================================*/
321 #define CS_CHECK(fn, args...) \
322 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
324 static void tc589_config(dev_link_t *link)
326 client_handle_t handle = link->handle;
327 struct el3_private *lp = link->priv;
328 struct net_device *dev = &lp->dev;
329 tuple_t tuple;
330 cisparse_t parse;
331 u_short buf[32], *phys_addr;
332 int last_fn, last_ret, i, j, multi = 0;
333 ioaddr_t ioaddr;
334 char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
336 DEBUG(0, "3c589_config(0x%p)\n", link);
338 phys_addr = (u_short *)dev->dev_addr;
339 tuple.Attributes = 0;
340 tuple.DesiredTuple = CISTPL_CONFIG;
341 CS_CHECK(GetFirstTuple, handle, &tuple);
342 tuple.TupleData = (cisdata_t *)buf;
343 tuple.TupleDataMax = sizeof(buf);
344 tuple.TupleOffset = 0;
345 CS_CHECK(GetTupleData, handle, &tuple);
346 CS_CHECK(ParseTuple, handle, &tuple, &parse);
347 link->conf.ConfigBase = parse.config.base;
348 link->conf.Present = parse.config.rmask[0];
350 /* Is this a 3c562? */
351 tuple.DesiredTuple = CISTPL_MANFID;
352 tuple.Attributes = TUPLE_RETURN_COMMON;
353 if ((CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) &&
354 (CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS)) {
355 if (le16_to_cpu(buf[0]) != MANFID_3COM)
356 printk(KERN_INFO "3c589_cs: hmmm, is this really a "
357 "3Com card??\n");
358 multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
361 /* Configure card */
362 link->state |= DEV_CONFIG;
364 /* For the 3c562, the base address must be xx00-xx7f */
365 link->io.IOAddrLines = 16;
366 for (i = j = 0; j < 0x400; j += 0x10) {
367 if (multi && (j & 0x80)) continue;
368 link->io.BasePort1 = j ^ 0x300;
369 i = CardServices(RequestIO, link->handle, &link->io);
370 if (i == CS_SUCCESS) break;
372 if (i != CS_SUCCESS) {
373 cs_error(link->handle, RequestIO, i);
374 goto failed;
376 CS_CHECK(RequestIRQ, link->handle, &link->irq);
377 CS_CHECK(RequestConfiguration, link->handle, &link->conf);
379 dev->irq = link->irq.AssignedIRQ;
380 dev->base_addr = link->io.BasePort1;
381 if (register_netdev(dev) != 0) {
382 printk(KERN_NOTICE "3c589_cs: register_netdev() failed\n");
383 goto failed;
386 ioaddr = dev->base_addr;
387 EL3WINDOW(0);
389 /* The 3c589 has an extra EEPROM for configuration info, including
390 the hardware address. The 3c562 puts the address in the CIS. */
391 tuple.DesiredTuple = 0x88;
392 if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) {
393 CardServices(GetTupleData, handle, &tuple);
394 for (i = 0; i < 3; i++)
395 phys_addr[i] = htons(buf[i]);
396 } else {
397 for (i = 0; i < 3; i++)
398 phys_addr[i] = htons(read_eeprom(ioaddr, i));
399 if (phys_addr[0] == 0x6060) {
400 printk(KERN_NOTICE "3c589_cs: IO port conflict at 0x%03lx"
401 "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
402 goto failed;
406 strcpy(lp->node.dev_name, dev->name);
407 link->dev = &lp->node;
408 link->state &= ~DEV_CONFIG_PENDING;
410 /* The address and resource configuration register aren't loaded from
411 the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
412 outw(0x3f00, ioaddr + 8);
414 /* The if_port symbol can be set when the module is loaded */
415 if ((if_port >= 0) && (if_port <= 3))
416 dev->if_port = if_port;
417 else
418 printk(KERN_NOTICE "3c589_cs: invalid if_port requested\n");
420 printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
421 dev->name, (multi ? "562" : "589"), dev->base_addr,
422 dev->irq);
423 for (i = 0; i < 6; i++)
424 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
425 i = inl(ioaddr);
426 printk(KERN_INFO " %dK FIFO split %s Rx:Tx, %s xcvr\n",
427 (i & 7) ? 32 : 8, ram_split[(i >> 16) & 3],
428 if_names[dev->if_port]);
429 return;
431 cs_failed:
432 cs_error(link->handle, last_fn, last_ret);
433 failed:
434 tc589_release((u_long)link);
435 return;
437 } /* tc589_config */
439 /*======================================================================
441 After a card is removed, tc589_release() will unregister the net
442 device, and release the PCMCIA configuration. If the device is
443 still open, this will be postponed until it is closed.
445 ======================================================================*/
447 static void tc589_release(u_long arg)
449 dev_link_t *link = (dev_link_t *)arg;
451 DEBUG(0, "3c589_release(0x%p)\n", link);
453 if (link->open) {
454 DEBUG(1, "3c589_cs: release postponed, '%s' still open\n",
455 link->dev->dev_name);
456 link->state |= DEV_STALE_CONFIG;
457 return;
460 CardServices(ReleaseConfiguration, link->handle);
461 CardServices(ReleaseIO, link->handle, &link->io);
462 CardServices(ReleaseIRQ, link->handle, &link->irq);
464 link->state &= ~DEV_CONFIG;
466 } /* tc589_release */
468 /*======================================================================
470 The card status event handler. Mostly, this schedules other
471 stuff to run after an event is received. A CARD_REMOVAL event
472 also sets some flags to discourage the net drivers from trying
473 to talk to the card any more.
475 ======================================================================*/
477 static int tc589_event(event_t event, int priority,
478 event_callback_args_t *args)
480 dev_link_t *link = args->client_data;
481 struct el3_private *lp = link->priv;
482 struct net_device *dev = &lp->dev;
484 DEBUG(1, "3c589_event(0x%06x)\n", event);
486 switch (event) {
487 case CS_EVENT_CARD_REMOVAL:
488 link->state &= ~DEV_PRESENT;
489 if (link->state & DEV_CONFIG) {
490 netif_device_detach(dev);
491 mod_timer(&link->release, jiffies + HZ/20);
493 break;
494 case CS_EVENT_CARD_INSERTION:
495 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
496 tc589_config(link);
497 break;
498 case CS_EVENT_PM_SUSPEND:
499 link->state |= DEV_SUSPEND;
500 /* Fall through... */
501 case CS_EVENT_RESET_PHYSICAL:
502 if (link->state & DEV_CONFIG) {
503 if (link->open)
504 netif_device_detach(dev);
505 CardServices(ReleaseConfiguration, link->handle);
507 break;
508 case CS_EVENT_PM_RESUME:
509 link->state &= ~DEV_SUSPEND;
510 /* Fall through... */
511 case CS_EVENT_CARD_RESET:
512 if (link->state & DEV_CONFIG) {
513 CardServices(RequestConfiguration, link->handle, &link->conf);
514 if (link->open) {
515 tc589_reset(dev);
516 netif_device_attach(dev);
519 break;
521 return 0;
522 } /* tc589_event */
524 /*====================================================================*/
527 Use this for commands that may take time to finish
529 static void wait_for_completion(struct net_device *dev, int cmd)
531 int i = 100;
532 outw(cmd, dev->base_addr + EL3_CMD);
533 while (--i > 0)
534 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
535 if (i == 0)
536 printk(KERN_NOTICE "%s: command 0x%04x did not complete!\n",
537 dev->name, cmd);
541 Read a word from the EEPROM using the regular EEPROM access register.
542 Assume that we are in register window zero.
544 static u_short read_eeprom(ioaddr_t ioaddr, int index)
546 int i;
547 outw(EEPROM_READ + index, ioaddr + 10);
548 /* Reading the eeprom takes 162 us */
549 for (i = 1620; i >= 0; i--)
550 if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
551 break;
552 return inw(ioaddr + 12);
556 Set transceiver type, perhaps to something other than what the user
557 specified in dev->if_port.
559 static void tc589_set_xcvr(struct net_device *dev, int if_port)
561 struct el3_private *lp = (struct el3_private *)dev->priv;
562 ioaddr_t ioaddr = dev->base_addr;
564 EL3WINDOW(0);
565 switch (if_port) {
566 case 0: case 1: outw(0, ioaddr + 6); break;
567 case 2: outw(3<<14, ioaddr + 6); break;
568 case 3: outw(1<<14, ioaddr + 6); break;
570 /* On PCMCIA, this just turns on the LED */
571 outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
572 /* 10baseT interface, enable link beat and jabber check. */
573 EL3WINDOW(4);
574 outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
575 EL3WINDOW(1);
576 if (if_port == 2)
577 lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
578 else
579 lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
582 static void dump_status(struct net_device *dev)
584 ioaddr_t ioaddr = dev->base_addr;
585 EL3WINDOW(1);
586 printk(KERN_INFO " irq status %04x, rx status %04x, tx status "
587 "%02x tx free %04x\n", inw(ioaddr+EL3_STATUS),
588 inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
589 inw(ioaddr+TX_FREE));
590 EL3WINDOW(4);
591 printk(KERN_INFO " diagnostics: fifo %04x net %04x ethernet %04x"
592 " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
593 inw(ioaddr+0x08), inw(ioaddr+0x0a));
594 EL3WINDOW(1);
597 /* Reset and restore all of the 3c589 registers. */
598 static void tc589_reset(struct net_device *dev)
600 ioaddr_t ioaddr = dev->base_addr;
601 int i;
603 EL3WINDOW(0);
604 outw(0x0001, ioaddr + 4); /* Activate board. */
605 outw(0x3f00, ioaddr + 8); /* Set the IRQ line. */
607 /* Set the station address in window 2. */
608 EL3WINDOW(2);
609 for (i = 0; i < 6; i++)
610 outb(dev->dev_addr[i], ioaddr + i);
612 tc589_set_xcvr(dev, dev->if_port);
614 /* Switch to the stats window, and clear all stats by reading. */
615 outw(StatsDisable, ioaddr + EL3_CMD);
616 EL3WINDOW(6);
617 for (i = 0; i < 9; i++)
618 inb(ioaddr+i);
619 inw(ioaddr + 10);
620 inw(ioaddr + 12);
622 /* Switch to register set 1 for normal use. */
623 EL3WINDOW(1);
625 /* Accept b-cast and phys addr only. */
626 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
627 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
628 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
629 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
630 /* Allow status bits to be seen. */
631 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
632 /* Ack all pending events, and set active indicator mask. */
633 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
634 ioaddr + EL3_CMD);
635 outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
636 | AdapterFailure, ioaddr + EL3_CMD);
639 static int el3_config(struct net_device *dev, struct ifmap *map)
641 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
642 if (map->port <= 3) {
643 dev->if_port = map->port;
644 printk(KERN_INFO "%s: switched to %s port\n",
645 dev->name, if_names[dev->if_port]);
646 tc589_set_xcvr(dev, dev->if_port);
647 } else
648 return -EINVAL;
650 return 0;
653 static int el3_open(struct net_device *dev)
655 struct el3_private *lp = (struct el3_private *)dev->priv;
656 dev_link_t *link = &lp->link;
658 if (!DEV_OK(link))
659 return -ENODEV;
661 link->open++;
662 MOD_INC_USE_COUNT;
663 netif_start_queue(dev);
665 tc589_reset(dev);
666 lp->media.function = &media_check;
667 lp->media.data = (u_long)lp;
668 lp->media.expires = jiffies + HZ;
669 add_timer(&lp->media);
671 DEBUG(1, "%s: opened, status %4.4x.\n",
672 dev->name, inw(dev->base_addr + EL3_STATUS));
674 return 0;
677 static void el3_tx_timeout(struct net_device *dev)
679 struct el3_private *lp = (struct el3_private *)dev->priv;
680 ioaddr_t ioaddr = dev->base_addr;
682 printk(KERN_NOTICE "%s: Transmit timed out!\n", dev->name);
683 dump_status(dev);
684 lp->stats.tx_errors++;
685 dev->trans_start = jiffies;
686 /* Issue TX_RESET and TX_START commands. */
687 wait_for_completion(dev, TxReset);
688 outw(TxEnable, ioaddr + EL3_CMD);
689 netif_start_queue(dev);
692 static void pop_tx_status(struct net_device *dev)
694 struct el3_private *lp = (struct el3_private *)dev->priv;
695 ioaddr_t ioaddr = dev->base_addr;
696 int i;
698 /* Clear the Tx status stack. */
699 for (i = 32; i > 0; i--) {
700 u_char tx_status = inb(ioaddr + TX_STATUS);
701 if (!(tx_status & 0x84)) break;
702 /* reset transmitter on jabber error or underrun */
703 if (tx_status & 0x30)
704 wait_for_completion(dev, TxReset);
705 if (tx_status & 0x38) {
706 DEBUG(1, "%s: transmit error: status 0x%02x\n",
707 dev->name, tx_status);
708 outw(TxEnable, ioaddr + EL3_CMD);
709 lp->stats.tx_aborted_errors++;
711 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
715 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
717 ioaddr_t ioaddr = dev->base_addr;
719 DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
720 "status %4.4x.\n", dev->name, (long)skb->len,
721 inw(ioaddr + EL3_STATUS));
723 ((struct el3_private *)dev->priv)->stats.tx_bytes += skb->len;
725 /* Put out the doubleword header... */
726 outw(skb->len, ioaddr + TX_FIFO);
727 outw(0x00, ioaddr + TX_FIFO);
728 /* ... and the packet rounded to a doubleword. */
729 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
731 dev->trans_start = jiffies;
732 if (inw(ioaddr + TX_FREE) <= 1536) {
733 netif_stop_queue(dev);
734 /* Interrupt us when the FIFO has room for max-sized packet. */
735 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
738 dev_kfree_skb(skb);
739 pop_tx_status(dev);
741 return 0;
744 /* The EL3 interrupt handler. */
745 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
747 struct el3_private *lp = dev_id;
748 struct net_device *dev = &lp->dev;
749 ioaddr_t ioaddr, status;
750 int i = 0;
752 if (!netif_device_present(dev))
753 return;
754 ioaddr = dev->base_addr;
756 DEBUG(3, "%s: interrupt, status %4.4x.\n",
757 dev->name, inw(ioaddr + EL3_STATUS));
759 while ((status = inw(ioaddr + EL3_STATUS)) &
760 (IntLatch | RxComplete | StatsFull)) {
761 if (!netif_device_present(dev) ||
762 ((status & 0xe000) != 0x2000)) {
763 DEBUG(1, "%s: interrupt from dead card\n", dev->name);
764 break;
767 if (status & RxComplete)
768 el3_rx(dev);
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)
778 pop_tx_status(dev);
780 if (status & (AdapterFailure | RxEarly | StatsFull)) {
781 /* Handle all uncommon interrupts. */
782 if (status & StatsFull) /* Empty statistics. */
783 update_stats(dev);
784 if (status & RxEarly) { /* Rx early is unused. */
785 el3_rx(dev);
786 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
788 if (status & AdapterFailure) {
789 u16 fifo_diag;
790 EL3WINDOW(4);
791 fifo_diag = inw(ioaddr + 4);
792 EL3WINDOW(1);
793 printk(KERN_NOTICE "%s: adapter failure, FIFO diagnostic"
794 " register %04x.\n", dev->name, fifo_diag);
795 if (fifo_diag & 0x0400) {
796 /* Tx overrun */
797 wait_for_completion(dev, TxReset);
798 outw(TxEnable, ioaddr + EL3_CMD);
800 if (fifo_diag & 0x2000) {
801 /* Rx underrun */
802 wait_for_completion(dev, RxReset);
803 set_multicast_list(dev);
804 outw(RxEnable, ioaddr + EL3_CMD);
806 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
810 if (++i > 10) {
811 printk(KERN_NOTICE "%s: infinite loop in interrupt, "
812 "status %4.4x.\n", dev->name, status);
813 /* Clear all interrupts */
814 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
815 break;
817 /* Acknowledge the IRQ. */
818 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
821 lp->last_irq = jiffies;
822 DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
823 dev->name, inw(ioaddr + EL3_STATUS));
824 return;
827 static void media_check(u_long arg)
829 struct el3_private *lp = (struct el3_private *)(arg);
830 struct net_device *dev = &lp->dev;
831 ioaddr_t ioaddr = dev->base_addr;
832 u_short media, errs;
833 u_long flags;
835 if (!netif_device_present(dev)) goto reschedule;
837 EL3WINDOW(1);
838 /* Check for pending interrupt with expired latency timer: with
839 this, we can limp along even if the interrupt is blocked */
840 if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
841 (inb(ioaddr + EL3_TIMER) == 0xff)) {
842 if (!lp->fast_poll)
843 printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
844 el3_interrupt(dev->irq, lp, NULL);
845 lp->fast_poll = HZ;
847 if (lp->fast_poll) {
848 lp->fast_poll--;
849 lp->media.expires = jiffies + 1;
850 add_timer(&lp->media);
851 return;
854 save_flags(flags);
855 cli();
856 EL3WINDOW(4);
857 media = inw(ioaddr+WN4_MEDIA) & 0xc810;
859 /* Ignore collisions unless we've had no irq's recently */
860 if (jiffies - lp->last_irq < HZ) {
861 media &= ~0x0010;
862 } else {
863 /* Try harder to detect carrier errors */
864 EL3WINDOW(6);
865 outw(StatsDisable, ioaddr + EL3_CMD);
866 errs = inb(ioaddr + 0);
867 outw(StatsEnable, ioaddr + EL3_CMD);
868 lp->stats.tx_carrier_errors += errs;
869 if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
872 if (media != lp->media_status) {
873 if ((media & lp->media_status & 0x8000) &&
874 ((lp->media_status ^ media) & 0x0800))
875 printk(KERN_INFO "%s: %s link beat\n", dev->name,
876 (lp->media_status & 0x0800 ? "lost" : "found"));
877 else if ((media & lp->media_status & 0x4000) &&
878 ((lp->media_status ^ media) & 0x0010))
879 printk(KERN_INFO "%s: coax cable %s\n", dev->name,
880 (lp->media_status & 0x0010 ? "ok" : "problem"));
881 if (dev->if_port == 0) {
882 if (media & 0x8000) {
883 if (media & 0x0800)
884 printk(KERN_INFO "%s: flipped to 10baseT\n",
885 dev->name);
886 else
887 tc589_set_xcvr(dev, 2);
888 } else if (media & 0x4000) {
889 if (media & 0x0010)
890 tc589_set_xcvr(dev, 1);
891 else
892 printk(KERN_INFO "%s: flipped to 10base2\n",
893 dev->name);
896 lp->media_status = media;
899 EL3WINDOW(1);
900 restore_flags(flags);
902 reschedule:
903 lp->media.expires = jiffies + HZ;
904 add_timer(&lp->media);
907 static struct net_device_stats *el3_get_stats(struct net_device *dev)
909 struct el3_private *lp = (struct el3_private *)dev->priv;
910 unsigned long flags;
911 dev_link_t *link = &lp->link;
913 if (DEV_OK(link)) {
914 save_flags(flags);
915 cli();
916 update_stats(dev);
917 restore_flags(flags);
919 return &lp->stats;
923 Update statistics. We change to register window 6, so this should be run
924 single-threaded if the device is active. This is expected to be a rare
925 operation, and it's simpler for the rest of the driver to assume that
926 window 1 is always valid rather than use a special window-state variable.
928 static void update_stats(struct net_device *dev)
930 struct el3_private *lp = (struct el3_private *)dev->priv;
931 ioaddr_t ioaddr = dev->base_addr;
933 DEBUG(2, "%s: updating the statistics.\n", dev->name);
934 /* Turn off statistics updates while reading. */
935 outw(StatsDisable, ioaddr + EL3_CMD);
936 /* Switch to the stats window, and read everything. */
937 EL3WINDOW(6);
938 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
939 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
940 /* Multiple collisions. */ inb(ioaddr + 2);
941 lp->stats.collisions += inb(ioaddr + 3);
942 lp->stats.tx_window_errors += inb(ioaddr + 4);
943 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
944 lp->stats.tx_packets += inb(ioaddr + 6);
945 /* Rx packets */ inb(ioaddr + 7);
946 /* Tx deferrals */ inb(ioaddr + 8);
947 /* Rx octets */ inw(ioaddr + 10);
948 /* Tx octets */ inw(ioaddr + 12);
950 /* Back to window 1, and turn statistics back on. */
951 EL3WINDOW(1);
952 outw(StatsEnable, ioaddr + EL3_CMD);
955 static int el3_rx(struct net_device *dev)
957 struct el3_private *lp = (struct el3_private *)dev->priv;
958 ioaddr_t ioaddr = dev->base_addr;
959 int worklimit = 32;
960 short rx_status;
962 DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
963 dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
964 while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
965 (--worklimit >= 0)) {
966 if (rx_status & 0x4000) { /* Error, update stats. */
967 short error = rx_status & 0x3800;
968 lp->stats.rx_errors++;
969 switch (error) {
970 case 0x0000: lp->stats.rx_over_errors++; break;
971 case 0x0800: lp->stats.rx_length_errors++; break;
972 case 0x1000: lp->stats.rx_frame_errors++; break;
973 case 0x1800: lp->stats.rx_length_errors++; break;
974 case 0x2000: lp->stats.rx_frame_errors++; break;
975 case 0x2800: lp->stats.rx_crc_errors++; break;
977 } else {
978 short pkt_len = rx_status & 0x7ff;
979 struct sk_buff *skb;
981 skb = dev_alloc_skb(pkt_len+5);
983 DEBUG(3, " Receiving packet size %d status %4.4x.\n",
984 pkt_len, rx_status);
985 if (skb != NULL) {
986 skb->dev = dev;
988 skb_reserve(skb, 2);
989 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
990 (pkt_len+3)>>2);
991 skb->protocol = eth_type_trans(skb, dev);
993 netif_rx(skb);
994 lp->stats.rx_packets++;
995 lp->stats.rx_bytes += skb->len;
996 } else {
997 DEBUG(1, "%s: couldn't allocate a sk_buff of"
998 " size %d.\n", dev->name, pkt_len);
999 lp->stats.rx_dropped++;
1002 /* Pop the top of the Rx FIFO */
1003 wait_for_completion(dev, RxDiscard);
1005 if (worklimit == 0)
1006 printk(KERN_NOTICE "%s: too much work in el3_rx!\n", dev->name);
1007 return 0;
1010 /* Set or clear the multicast filter for this adapter.
1011 num_addrs == -1 Promiscuous mode, receive all packets
1012 num_addrs == 0 Normal mode, clear multicast list
1013 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
1014 best-effort filtering.
1016 static void set_multicast_list(struct net_device *dev)
1018 struct el3_private *lp = dev->priv;
1019 dev_link_t *link = &lp->link;
1020 ioaddr_t ioaddr = dev->base_addr;
1022 if (!(DEV_OK(link))) return;
1023 #ifdef PCMCIA_DEBUG
1024 if (pc_debug > 2) {
1025 static int old = 0;
1026 if (old != dev->mc_count) {
1027 old = dev->mc_count;
1028 DEBUG(0, "%s: setting Rx mode to %d addresses.\n",
1029 dev->name, old);
1032 #endif
1033 if (dev->flags & IFF_PROMISC)
1034 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1035 ioaddr + EL3_CMD);
1036 else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1037 outw(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
1038 else
1039 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1042 static int el3_close(struct net_device *dev)
1044 struct el3_private *lp = dev->priv;
1045 dev_link_t *link = &lp->link;
1046 ioaddr_t ioaddr = dev->base_addr;
1048 DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1050 if (DEV_OK(link)) {
1051 /* Turn off statistics ASAP. We update lp->stats below. */
1052 outw(StatsDisable, ioaddr + EL3_CMD);
1054 /* Disable the receiver and transmitter. */
1055 outw(RxDisable, ioaddr + EL3_CMD);
1056 outw(TxDisable, ioaddr + EL3_CMD);
1058 if (dev->if_port == 2)
1059 /* Turn off thinnet power. Green! */
1060 outw(StopCoax, ioaddr + EL3_CMD);
1061 else if (dev->if_port == 1) {
1062 /* Disable link beat and jabber */
1063 EL3WINDOW(4);
1064 outw(0, ioaddr + WN4_MEDIA);
1067 /* Switching back to window 0 disables the IRQ. */
1068 EL3WINDOW(0);
1069 /* But we explicitly zero the IRQ line select anyway. */
1070 outw(0x0f00, ioaddr + WN0_IRQ);
1072 /* Check if the card still exists */
1073 if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1074 update_stats(dev);
1077 link->open--;
1078 netif_stop_queue(dev);
1079 del_timer(&lp->media);
1080 if (link->state & DEV_STALE_CONFIG)
1081 mod_timer(&link->release, jiffies + HZ/20);
1083 MOD_DEC_USE_COUNT;
1085 return 0;
1088 /*====================================================================*/
1090 static int __init init_3c589_cs(void)
1092 servinfo_t serv;
1093 DEBUG(0, "%s\n", version);
1094 CardServices(GetCardServicesInfo, &serv);
1095 if (serv.Revision != CS_RELEASE_CODE) {
1096 printk(KERN_NOTICE "3c589_cs: Card Services release "
1097 "does not match!\n");
1098 return -1;
1100 register_pccard_driver(&dev_info, &tc589_attach, &tc589_detach);
1101 return 0;
1104 static void __exit exit_3c589_cs(void)
1106 DEBUG(0, "3c589_cs: unloading\n");
1107 unregister_pccard_driver(&dev_info);
1108 while (dev_list != NULL)
1109 tc589_detach(dev_list);
1112 module_init(init_3c589_cs);
1113 module_exit(exit_3c589_cs);