1 /* 3c574.c: A PCMCIA ethernet driver for the 3com 3c574 "RoadRunner".
4 Donald Becker, becker@scyld.com, (driver core) and
5 David Hinds, dahinds@users.sourceforge.net (from his PC card code).
6 Locking fixes (C) Copyright 2003 Red Hat Inc
8 This software may be used and distributed according to the terms of
9 the GNU General Public License, incorporated herein by reference.
11 This driver derives from Donald Becker's 3c509 core, which has the
13 Copyright 1993 United States Government as represented by the
14 Director, National Security Agency.
22 I. Board Compatibility
24 This device driver is designed for the 3Com 3c574 PC card Fast Ethernet
27 II. Board-specific settings
29 None -- PC cards are autoconfigured.
33 The 3c574 uses a Boomerang-style interface, without the bus-master capability.
34 See the Boomerang driver and documentation for most details.
36 IV. Notes and chip documentation.
38 Two added registers are used to enhance PIO performance, RunnerRdCtrl and
39 RunnerWrCtrl. These are 11 bit down-counters that are preloaded with the
40 count of word (16 bits) reads or writes the driver is about to do to the Rx
41 or Tx FIFO. The chip is then able to hide the internal-PCI-bus to PC-card
42 translation latency by buffering the I/O operations with an 8 word FIFO.
43 Note: No other chip accesses are permitted when this buffer is used.
45 A second enhancement is that both attribute and common memory space
46 0x0800-0x0fff can translated to the PIO FIFO. Thus memory operations (faster
47 with *some* PCcard bridges) may be used instead of I/O operations.
48 This is enabled by setting the 0x10 bit in the PCMCIA LAN COR.
50 Some slow PC card bridges work better if they never see a WAIT signal.
51 This is configured by setting the 0x20 bit in the PCMCIA LAN COR.
52 Only do this after testing that it is reliable and improves performance.
54 The upper five bits of RunnerRdCtrl are used to window into PCcard
55 configuration space registers. Window 0 is the regular Boomerang/Odie
56 register set, 1-5 are various PC card control registers, and 16-31 are
57 the (reversed!) CIS table.
59 A final note: writing the InternalConfig register in window 3 with an
60 invalid ramWidth is Very Bad.
64 http://www.scyld.com/expert/NWay.html
65 http://www.national.com/pf/DP/DP83840.html
67 Thanks to Terry Murphy of 3Com for providing development information for
68 earlier 3Com products.
72 #include <linux/module.h>
73 #include <linux/kernel.h>
74 #include <linux/init.h>
75 #include <linux/slab.h>
76 #include <linux/string.h>
77 #include <linux/timer.h>
78 #include <linux/interrupt.h>
80 #include <linux/delay.h>
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/if_arp.h>
85 #include <linux/ioport.h>
86 #include <linux/ethtool.h>
88 #include <pcmcia/version.h>
89 #include <pcmcia/cs_types.h>
90 #include <pcmcia/cs.h>
91 #include <pcmcia/cistpl.h>
92 #include <pcmcia/cisreg.h>
93 #include <pcmcia/ciscode.h>
94 #include <pcmcia/ds.h>
95 #include <pcmcia/mem_op.h>
97 #include <asm/uaccess.h>
99 #include <asm/system.h>
100 #include <asm/bitops.h>
102 /*====================================================================*/
104 /* Module parameters */
106 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
107 MODULE_DESCRIPTION("3Com 3c574 series PCMCIA ethernet driver");
108 MODULE_LICENSE("GPL");
110 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
112 /* Now-standard PC card module parameters. */
113 INT_MODULE_PARM(irq_mask, 0xdeb8);
114 static int irq_list[4] = { -1 };
115 MODULE_PARM(irq_list, "1-4i");
117 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
118 INT_MODULE_PARM(max_interrupt_work, 32);
120 /* Force full duplex modes? */
121 INT_MODULE_PARM(full_duplex, 0);
123 /* Autodetect link polarity reversal? */
124 INT_MODULE_PARM(auto_polarity, 1);
127 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
128 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
129 static char *version =
130 "3c574_cs.c 1.65ac1 2003/04/07 Donald Becker/David Hinds, becker@scyld.com.\n";
132 #define DEBUG(n, args...)
135 /*====================================================================*/
137 /* Time in jiffies before concluding the transmitter is hung. */
138 #define TX_TIMEOUT ((800*HZ)/1000)
140 /* To minimize the size of the driver source and make the driver more
141 readable not all constants are symbolically defined.
142 You'll need the manual if you want to understand driver details anyway. */
143 /* Offsets from base I/O address. */
144 #define EL3_DATA 0x00
146 #define EL3_STATUS 0x0e
148 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
150 /* The top five bits written to EL3_CMD are a command, the lower
151 11 bits are the parameter, if applicable. */
153 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
154 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
155 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
156 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
157 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
158 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
159 StatsDisable = 22<<11, StopCoax = 23<<11,
163 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
164 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
165 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000 };
167 /* The SetRxFilter command accepts the following classes: */
169 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
173 Wn0EepromCmd = 10, Wn0EepromData = 12, /* EEPROM command/address, data. */
174 IntrStatus=0x0E, /* Valid in all windows. */
176 /* These assumes the larger EEPROM. */
177 enum Win0_EEPROM_cmds {
178 EEPROM_Read = 0x200, EEPROM_WRITE = 0x100, EEPROM_ERASE = 0x300,
179 EEPROM_EWENB = 0x30, /* Enable erasing/writing for 10 msec. */
180 EEPROM_EWDIS = 0x00, /* Disable EWENB before 10 msec timeout. */
183 /* Register window 1 offsets, the window used in normal operation.
184 On the "Odie" this window is always mapped at offsets 0x10-0x1f.
185 Except for TxFree, which is overlapped by RunnerWrCtrl. */
187 TX_FIFO = 0x10, RX_FIFO = 0x10, RxErrors = 0x14,
188 RxStatus = 0x18, Timer=0x1A, TxStatus = 0x1B,
189 TxFree = 0x0C, /* Remaining free bytes in Tx buffer. */
190 RunnerRdCtrl = 0x16, RunnerWrCtrl = 0x1c,
193 enum Window3 { /* Window 3: MAC/config bits. */
194 Wn3_Config=0, Wn3_MAC_Ctrl=6, Wn3_Options=8,
198 struct w3_config_fields {
199 unsigned int ram_size:3, ram_width:1, ram_speed:2, rom_size:2;
201 unsigned int ram_split:2, pad18:2, xcvr:3, pad21:1, autoselect:1;
206 enum Window4 { /* Window 4: Xcvr/media bits. */
207 Wn4_FIFODiag = 4, Wn4_NetDiag = 6, Wn4_PhysicalMgmt=8, Wn4_Media = 10,
210 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
215 struct net_device_stats stats;
216 u16 advertising, partner; /* NWay media advertisement */
217 unsigned char phys; /* MII device address */
218 unsigned int autoselect:1, default_media:3; /* Read from the EEPROM/Wn3_Config. */
219 /* for transceiver monitoring */
220 struct timer_list media;
221 unsigned short media_status;
222 unsigned short fast_poll;
223 unsigned long last_irq;
224 spinlock_t window_lock; /* Guards the Window selection */
227 /* Set iff a MII transceiver on any interface requires mdio preamble.
228 This only set with the original DP83840 on older 3c905 boards, so the extra
229 code size of a per-interface flag is not worthwhile. */
230 static char mii_preamble_required = 0;
232 /* Index of functions. */
234 static void tc574_config(dev_link_t *link);
235 static void tc574_release(dev_link_t *link);
236 static int tc574_event(event_t event, int priority,
237 event_callback_args_t *args);
239 static void mdio_sync(ioaddr_t ioaddr, int bits);
240 static int mdio_read(ioaddr_t ioaddr, int phy_id, int location);
241 static void mdio_write(ioaddr_t ioaddr, int phy_id, int location, int value);
242 static unsigned short read_eeprom(ioaddr_t ioaddr, int index);
243 static void tc574_wait_for_completion(struct net_device *dev, int cmd);
245 static void tc574_reset(struct net_device *dev);
246 static void media_check(unsigned long arg);
247 static int el3_open(struct net_device *dev);
248 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
249 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
250 static void update_stats(struct net_device *dev);
251 static struct net_device_stats *el3_get_stats(struct net_device *dev);
252 static int el3_rx(struct net_device *dev, int worklimit);
253 static int el3_close(struct net_device *dev);
254 static void el3_tx_timeout(struct net_device *dev);
255 static int el3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
256 static void set_rx_mode(struct net_device *dev);
258 static dev_info_t dev_info = "3c574_cs";
260 static dev_link_t *tc574_attach(void);
261 static void tc574_detach(dev_link_t *);
263 static dev_link_t *dev_list;
265 static void flush_stale_links(void)
267 dev_link_t *link, *next;
268 for (link = dev_list; link; link = next) {
270 if (link->state & DEV_STALE_LINK)
276 tc574_attach() creates an "instance" of the driver, allocating
277 local data structures for one device. The device is registered
281 static dev_link_t *tc574_attach(void)
283 struct el3_private *lp;
284 client_reg_t client_reg;
286 struct net_device *dev;
289 DEBUG(0, "3c574_attach()\n");
292 /* Create the PC card device object. */
293 dev = alloc_etherdev(sizeof(struct el3_private));
300 spin_lock_init(&lp->window_lock);
301 link->io.NumPorts1 = 32;
302 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
303 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
304 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
305 if (irq_list[0] == -1)
306 link->irq.IRQInfo2 = irq_mask;
308 for (i = 0; i < 4; i++)
309 link->irq.IRQInfo2 |= 1 << irq_list[i];
310 link->irq.Handler = &el3_interrupt;
311 link->irq.Instance = dev;
312 link->conf.Attributes = CONF_ENABLE_IRQ;
314 link->conf.IntType = INT_MEMORY_AND_IO;
315 link->conf.ConfigIndex = 1;
316 link->conf.Present = PRESENT_OPTION;
318 /* The EL3-specific entries in the device structure. */
319 dev->hard_start_xmit = &el3_start_xmit;
320 dev->get_stats = &el3_get_stats;
321 dev->do_ioctl = &el3_ioctl;
322 dev->set_multicast_list = &set_rx_mode;
323 dev->open = &el3_open;
324 dev->stop = &el3_close;
325 #ifdef HAVE_TX_TIMEOUT
326 dev->tx_timeout = el3_tx_timeout;
327 dev->watchdog_timeo = TX_TIMEOUT;
330 /* Register with Card Services */
331 link->next = dev_list;
333 client_reg.dev_info = &dev_info;
334 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
335 client_reg.EventMask =
336 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
337 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
338 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
339 client_reg.event_handler = &tc574_event;
340 client_reg.Version = 0x0210;
341 client_reg.event_callback_args.client_data = link;
342 ret = CardServices(RegisterClient, &link->handle, &client_reg);
344 cs_error(link->handle, RegisterClient, ret);
354 This deletes a driver "instance". The device is de-registered
355 with Card Services. If it has been released, all local data
356 structures are freed. Otherwise, the structures will be freed
357 when the device is released.
361 static void tc574_detach(dev_link_t *link)
363 struct net_device *dev = link->priv;
366 DEBUG(0, "3c574_detach(0x%p)\n", link);
368 /* Locate device structure */
369 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
370 if (*linkp == link) break;
374 if (link->state & DEV_CONFIG) {
376 if (link->state & DEV_STALE_CONFIG) {
377 link->state |= DEV_STALE_LINK;
383 CardServices(DeregisterClient, link->handle);
385 /* Unlink device structure, free bits */
388 unregister_netdev(dev);
394 tc574_config() is scheduled to run after a CARD_INSERTION event
395 is received, to configure the PCMCIA socket, and to make the
396 ethernet device available to the system.
399 #define CS_CHECK(fn, args...) \
400 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
402 static void tc574_config(dev_link_t *link)
404 client_handle_t handle = link->handle;
405 struct net_device *dev = link->priv;
406 struct el3_private *lp = dev->priv;
409 unsigned short buf[32];
410 int last_fn, last_ret, i, j;
415 phys_addr = (u16 *)dev->dev_addr;
417 DEBUG(0, "3c574_config(0x%p)\n", link);
419 tuple.Attributes = 0;
420 tuple.DesiredTuple = CISTPL_CONFIG;
421 CS_CHECK(GetFirstTuple, handle, &tuple);
422 tuple.TupleData = (cisdata_t *)buf;
423 tuple.TupleDataMax = 64;
424 tuple.TupleOffset = 0;
425 CS_CHECK(GetTupleData, handle, &tuple);
426 CS_CHECK(ParseTuple, handle, &tuple, &parse);
427 link->conf.ConfigBase = parse.config.base;
428 link->conf.Present = parse.config.rmask[0];
431 link->state |= DEV_CONFIG;
433 link->io.IOAddrLines = 16;
434 for (i = j = 0; j < 0x400; j += 0x20) {
435 link->io.BasePort1 = j ^ 0x300;
436 i = CardServices(RequestIO, link->handle, &link->io);
437 if (i == CS_SUCCESS) break;
439 if (i != CS_SUCCESS) {
440 cs_error(link->handle, RequestIO, i);
443 CS_CHECK(RequestIRQ, link->handle, &link->irq);
444 CS_CHECK(RequestConfiguration, link->handle, &link->conf);
446 dev->irq = link->irq.AssignedIRQ;
447 dev->base_addr = link->io.BasePort1;
449 if (register_netdev(dev) != 0) {
450 printk(KERN_NOTICE "3c574_cs: register_netdev() failed\n");
454 ioaddr = dev->base_addr;
455 strcpy(lp->node.dev_name, dev->name);
456 link->dev = &lp->node;
457 link->state &= ~DEV_CONFIG_PENDING;
459 /* The 3c574 normally uses an EEPROM for configuration info, including
460 the hardware address. The future products may include a modem chip
461 and put the address in the CIS. */
462 tuple.DesiredTuple = 0x88;
463 if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) {
464 CardServices(GetTupleData, handle, &tuple);
465 for (i = 0; i < 3; i++)
466 phys_addr[i] = htons(buf[i]);
469 for (i = 0; i < 3; i++)
470 phys_addr[i] = htons(read_eeprom(ioaddr, i + 10));
471 if (phys_addr[0] == 0x6060) {
472 printk(KERN_NOTICE "3c574_cs: IO port conflict at 0x%03lx"
473 "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
477 tuple.DesiredTuple = CISTPL_VERS_1;
478 if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS &&
479 CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS &&
480 CardServices(ParseTuple, handle, &tuple, &parse) == CS_SUCCESS) {
481 cardname = parse.version_1.str + parse.version_1.ofs[1];
483 cardname = "3Com 3c574";
485 printk(KERN_INFO "%s: %s at io %#3lx, irq %d, hw_addr ",
486 dev->name, cardname, dev->base_addr, dev->irq);
488 for (i = 0; i < 6; i++)
489 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : ".\n"));
492 u_char mcr, *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
493 union wn3_config config;
494 outw(2<<11, ioaddr + RunnerRdCtrl);
495 mcr = inb(ioaddr + 2);
496 outw(0<<11, ioaddr + RunnerRdCtrl);
497 printk(KERN_INFO " ASIC rev %d,", mcr>>3);
499 config.i = inl(ioaddr + Wn3_Config);
500 printk(" %dK FIFO split %s Rx:Tx, %sMII interface.\n",
501 8 << config.u.ram_size, ram_split[config.u.ram_split],
502 config.u.autoselect ? "autoselect " : "");
503 lp->default_media = config.u.xcvr;
504 lp->autoselect = config.u.autoselect;
507 init_timer(&lp->media);
512 /* Roadrunner only: Turn on the MII transceiver */
513 outw(0x8040, ioaddr + Wn3_Options);
515 outw(0xc040, ioaddr + Wn3_Options);
516 tc574_wait_for_completion(dev, TxReset);
517 tc574_wait_for_completion(dev, RxReset);
519 outw(0x8040, ioaddr + Wn3_Options);
522 for (phy = 1; phy <= 32; phy++) {
524 mdio_sync(ioaddr, 32);
525 mii_status = mdio_read(ioaddr, phy & 0x1f, 1);
526 if (mii_status != 0xffff) {
527 lp->phys = phy & 0x1f;
528 DEBUG(0, " MII transceiver at index %d, status %x.\n",
530 if ((mii_status & 0x0040) == 0)
531 mii_preamble_required = 1;
536 printk(KERN_NOTICE " No MII transceivers found!\n");
539 i = mdio_read(ioaddr, lp->phys, 16) | 0x40;
540 mdio_write(ioaddr, lp->phys, 16, i);
541 lp->advertising = mdio_read(ioaddr, lp->phys, 4);
543 /* Only advertise the FD media types. */
544 lp->advertising &= ~0x02a0;
545 mdio_write(ioaddr, lp->phys, 4, lp->advertising);
552 cs_error(link->handle, last_fn, last_ret);
560 After a card is removed, tc574_release() will unregister the net
561 device, and release the PCMCIA configuration. If the device is
562 still open, this will be postponed until it is closed.
565 static void tc574_release(dev_link_t *link)
567 DEBUG(0, "3c574_release(0x%p)\n", link);
570 DEBUG(1, "3c574_cs: release postponed, '%s' still open\n",
571 link->dev->dev_name);
572 link->state |= DEV_STALE_CONFIG;
576 CardServices(ReleaseConfiguration, link->handle);
577 CardServices(ReleaseIO, link->handle, &link->io);
578 CardServices(ReleaseIRQ, link->handle, &link->irq);
580 link->state &= ~DEV_CONFIG;
582 } /* tc574_release */
585 The card status event handler. Mostly, this schedules other
586 stuff to run after an event is received. A CARD_REMOVAL event
587 also sets some flags to discourage the net drivers from trying
588 to talk to the card any more.
591 static int tc574_event(event_t event, int priority,
592 event_callback_args_t *args)
594 dev_link_t *link = args->client_data;
595 struct net_device *dev = link->priv;
597 DEBUG(1, "3c574_event(0x%06x)\n", event);
600 case CS_EVENT_CARD_REMOVAL:
601 link->state &= ~DEV_PRESENT;
602 if (link->state & DEV_CONFIG) {
603 netif_device_detach(dev);
607 case CS_EVENT_CARD_INSERTION:
608 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
611 case CS_EVENT_PM_SUSPEND:
612 link->state |= DEV_SUSPEND;
613 /* Fall through... */
614 case CS_EVENT_RESET_PHYSICAL:
615 if (link->state & DEV_CONFIG) {
617 netif_device_detach(dev);
618 CardServices(ReleaseConfiguration, link->handle);
621 case CS_EVENT_PM_RESUME:
622 link->state &= ~DEV_SUSPEND;
623 /* Fall through... */
624 case CS_EVENT_CARD_RESET:
625 if (link->state & DEV_CONFIG) {
626 CardServices(RequestConfiguration, link->handle, &link->conf);
629 netif_device_attach(dev);
637 static void dump_status(struct net_device *dev)
639 ioaddr_t ioaddr = dev->base_addr;
641 printk(KERN_INFO " irq status %04x, rx status %04x, tx status "
642 "%02x, tx free %04x\n", inw(ioaddr+EL3_STATUS),
643 inw(ioaddr+RxStatus), inb(ioaddr+TxStatus),
646 printk(KERN_INFO " diagnostics: fifo %04x net %04x ethernet %04x"
647 " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
648 inw(ioaddr+0x08), inw(ioaddr+0x0a));
653 Use this for commands that may take time to finish
655 static void tc574_wait_for_completion(struct net_device *dev, int cmd)
658 outw(cmd, dev->base_addr + EL3_CMD);
660 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
662 printk(KERN_NOTICE "%s: command 0x%04x did not complete!\n", dev->name, cmd);
665 /* Read a word from the EEPROM using the regular EEPROM access register.
666 Assume that we are in register window zero.
668 static unsigned short read_eeprom(ioaddr_t ioaddr, int index)
671 outw(EEPROM_Read + index, ioaddr + Wn0EepromCmd);
672 /* Pause for at least 162 usec for the read to take place. */
673 for (timer = 1620; timer >= 0; timer--) {
674 if ((inw(ioaddr + Wn0EepromCmd) & 0x8000) == 0)
677 return inw(ioaddr + Wn0EepromData);
680 /* MII transceiver control section.
681 Read and write the MII registers using software-generated serial
682 MDIO protocol. See the MII specifications or DP83840A data sheet
684 The maxium data clock rate is 2.5 Mhz. The timing is easily met by the
685 slow PC card interface. */
687 #define MDIO_SHIFT_CLK 0x01
688 #define MDIO_DIR_WRITE 0x04
689 #define MDIO_DATA_WRITE0 (0x00 | MDIO_DIR_WRITE)
690 #define MDIO_DATA_WRITE1 (0x02 | MDIO_DIR_WRITE)
691 #define MDIO_DATA_READ 0x02
692 #define MDIO_ENB_IN 0x00
694 /* Generate the preamble required for initial synchronization and
695 a few older transceivers. */
696 static void mdio_sync(ioaddr_t ioaddr, int bits)
698 int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
700 /* Establish sync by sending at least 32 logic ones. */
701 while (-- bits >= 0) {
702 outw(MDIO_DATA_WRITE1, mdio_addr);
703 outw(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
707 static int mdio_read(ioaddr_t ioaddr, int phy_id, int location)
710 int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
711 unsigned int retval = 0;
712 int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
714 if (mii_preamble_required)
715 mdio_sync(ioaddr, 32);
717 /* Shift the read command bits out. */
718 for (i = 14; i >= 0; i--) {
719 int dataval = (read_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
720 outw(dataval, mdio_addr);
721 outw(dataval | MDIO_SHIFT_CLK, mdio_addr);
723 /* Read the two transition, 16 data, and wire-idle bits. */
724 for (i = 19; i > 0; i--) {
725 outw(MDIO_ENB_IN, mdio_addr);
726 retval = (retval << 1) | ((inw(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
727 outw(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
729 return (retval>>1) & 0xffff;
732 static void mdio_write(ioaddr_t ioaddr, int phy_id, int location, int value)
734 int write_cmd = 0x50020000 | (phy_id << 23) | (location << 18) | value;
735 int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
738 if (mii_preamble_required)
739 mdio_sync(ioaddr, 32);
741 /* Shift the command bits out. */
742 for (i = 31; i >= 0; i--) {
743 int dataval = (write_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
744 outw(dataval, mdio_addr);
745 outw(dataval | MDIO_SHIFT_CLK, mdio_addr);
747 /* Leave the interface idle. */
748 for (i = 1; i >= 0; i--) {
749 outw(MDIO_ENB_IN, mdio_addr);
750 outw(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
756 /* Reset and restore all of the 3c574 registers. */
757 static void tc574_reset(struct net_device *dev)
759 struct el3_private *lp = (struct el3_private *)dev->priv;
760 int i, ioaddr = dev->base_addr;
763 tc574_wait_for_completion(dev, TotalReset|0x10);
765 spin_lock_irqsave(&lp->window_lock, flags);
766 /* Clear any transactions in progress. */
767 outw(0, ioaddr + RunnerWrCtrl);
768 outw(0, ioaddr + RunnerRdCtrl);
770 /* Set the station address and mask. */
772 for (i = 0; i < 6; i++)
773 outb(dev->dev_addr[i], ioaddr + i);
777 /* Reset config options */
779 outb((dev->mtu > 1500 ? 0x40 : 0), ioaddr + Wn3_MAC_Ctrl);
780 outl((lp->autoselect ? 0x01000000 : 0) | 0x0062001b,
781 ioaddr + Wn3_Config);
782 /* Roadrunner only: Turn on the MII transceiver. */
783 outw(0x8040, ioaddr + Wn3_Options);
785 outw(0xc040, ioaddr + Wn3_Options);
787 spin_unlock_irqrestore(&lp->window_lock, flags);
789 tc574_wait_for_completion(dev, TxReset);
790 tc574_wait_for_completion(dev, RxReset);
792 spin_lock_irqsave(&lp->window_lock, flags);
794 outw(0x8040, ioaddr + Wn3_Options);
796 /* Switch to the stats window, and clear all stats by reading. */
797 outw(StatsDisable, ioaddr + EL3_CMD);
799 for (i = 0; i < 10; i++)
807 /* .. enable any extra statistics bits.. */
808 outw(0x0040, ioaddr + Wn4_NetDiag);
811 spin_unlock_irqrestore(&lp->window_lock, flags);
813 /* .. re-sync MII and re-fill what NWay is advertising. */
814 mdio_sync(ioaddr, 32);
815 mdio_write(ioaddr, lp->phys, 4, lp->advertising);
816 if (!auto_polarity) {
817 /* works for TDK 78Q2120 series MII's */
818 int i = mdio_read(ioaddr, lp->phys, 16) | 0x20;
819 mdio_write(ioaddr, lp->phys, 16, i);
822 spin_lock_irqsave(&lp->window_lock, flags);
823 /* Switch to register set 1 for normal use, just for TxFree. */
825 spin_unlock_irqrestore(&lp->window_lock, flags);
826 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
827 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
828 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
829 /* Allow status bits to be seen. */
830 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
831 /* Ack all pending events, and set active indicator mask. */
832 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
834 outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
835 | AdapterFailure | RxEarly, ioaddr + EL3_CMD);
838 static int el3_open(struct net_device *dev)
840 struct el3_private *lp = (struct el3_private *)dev->priv;
841 dev_link_t *link = &lp->link;
847 netif_start_queue(dev);
850 lp->media.function = &media_check;
851 lp->media.data = (unsigned long) dev;
852 lp->media.expires = jiffies + HZ;
853 add_timer(&lp->media);
855 DEBUG(2, "%s: opened, status %4.4x.\n",
856 dev->name, inw(dev->base_addr + EL3_STATUS));
861 static void el3_tx_timeout(struct net_device *dev)
863 struct el3_private *lp = (struct el3_private *)dev->priv;
864 ioaddr_t ioaddr = dev->base_addr;
866 printk(KERN_NOTICE "%s: Transmit timed out!\n", dev->name);
868 lp->stats.tx_errors++;
869 dev->trans_start = jiffies;
870 /* Issue TX_RESET and TX_START commands. */
871 tc574_wait_for_completion(dev, TxReset);
872 outw(TxEnable, ioaddr + EL3_CMD);
873 netif_wake_queue(dev);
876 static void pop_tx_status(struct net_device *dev)
878 struct el3_private *lp = (struct el3_private *)dev->priv;
879 ioaddr_t ioaddr = dev->base_addr;
882 /* Clear the Tx status stack. */
883 for (i = 32; i > 0; i--) {
884 u_char tx_status = inb(ioaddr + TxStatus);
885 if (!(tx_status & 0x84))
887 /* reset transmitter on jabber error or underrun */
888 if (tx_status & 0x30)
889 tc574_wait_for_completion(dev, TxReset);
890 if (tx_status & 0x38) {
891 DEBUG(1, "%s: transmit error: status 0x%02x\n",
892 dev->name, tx_status);
893 outw(TxEnable, ioaddr + EL3_CMD);
894 lp->stats.tx_aborted_errors++;
896 outb(0x00, ioaddr + TxStatus); /* Pop the status stack. */
900 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
902 ioaddr_t ioaddr = dev->base_addr;
903 struct el3_private *lp = (struct el3_private *)dev->priv;
906 DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
907 "status %4.4x.\n", dev->name, (long)skb->len,
908 inw(ioaddr + EL3_STATUS));
910 spin_lock_irqsave(&lp->window_lock, flags);
911 outw(skb->len, ioaddr + TX_FIFO);
912 outw(0, ioaddr + TX_FIFO);
913 outsl(ioaddr + TX_FIFO, skb->data, (skb->len+3)>>2);
915 dev->trans_start = jiffies;
917 /* TxFree appears only in Window 1, not offset 0x1c. */
918 if (inw(ioaddr + TxFree) <= 1536) {
919 netif_stop_queue(dev);
920 /* Interrupt us when the FIFO has room for max-sized packet.
921 The threshold is in units of dwords. */
922 outw(SetTxThreshold + (1536>>2), ioaddr + EL3_CMD);
926 spin_unlock_irqrestore(&lp->window_lock, flags);
931 /* The EL3 interrupt handler. */
932 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
934 struct net_device *dev = (struct net_device *) dev_id;
935 struct el3_private *lp = dev->priv;
936 ioaddr_t ioaddr, status;
937 int work_budget = max_interrupt_work;
940 if (!netif_device_present(dev))
942 ioaddr = dev->base_addr;
944 DEBUG(3, "%s: interrupt, status %4.4x.\n",
945 dev->name, inw(ioaddr + EL3_STATUS));
947 spin_lock(&lp->window_lock);
949 while ((status = inw(ioaddr + EL3_STATUS)) &
950 (IntLatch | RxComplete | RxEarly | StatsFull)) {
951 if (!netif_device_present(dev) ||
952 ((status & 0xe000) != 0x2000)) {
953 DEBUG(1, "%s: Interrupt from dead card\n", dev->name);
959 if (status & RxComplete)
960 work_budget = el3_rx(dev, work_budget);
962 if (status & TxAvailable) {
963 DEBUG(3, " TX room bit was handled.\n");
964 /* There's room in the FIFO for a full-sized packet. */
965 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
966 netif_wake_queue(dev);
969 if (status & TxComplete)
972 if (status & (AdapterFailure | RxEarly | StatsFull)) {
973 /* Handle all uncommon interrupts. */
974 if (status & StatsFull)
976 if (status & RxEarly) {
977 work_budget = el3_rx(dev, work_budget);
978 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
980 if (status & AdapterFailure) {
983 fifo_diag = inw(ioaddr + Wn4_FIFODiag);
985 printk(KERN_NOTICE "%s: adapter failure, FIFO diagnostic"
986 " register %04x.\n", dev->name, fifo_diag);
987 if (fifo_diag & 0x0400) {
989 tc574_wait_for_completion(dev, TxReset);
990 outw(TxEnable, ioaddr + EL3_CMD);
992 if (fifo_diag & 0x2000) {
994 tc574_wait_for_completion(dev, RxReset);
996 outw(RxEnable, ioaddr + EL3_CMD);
998 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
1002 if (--work_budget < 0) {
1003 DEBUG(0, "%s: Too much work in interrupt, "
1004 "status %4.4x.\n", dev->name, status);
1005 /* Clear all interrupts */
1006 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
1009 /* Acknowledge the IRQ. */
1010 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
1013 DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
1014 dev->name, inw(ioaddr + EL3_STATUS));
1016 spin_unlock(&lp->window_lock);
1017 return IRQ_RETVAL(handled);
1021 This timer serves two purposes: to check for missed interrupts
1022 (and as a last resort, poll the NIC for events), and to monitor
1023 the MII, reporting changes in cable status.
1025 static void media_check(unsigned long arg)
1027 struct net_device *dev = (struct net_device *) arg;
1028 struct el3_private *lp = dev->priv;
1029 ioaddr_t ioaddr = dev->base_addr;
1030 unsigned long flags;
1031 unsigned short /* cable, */ media, partner;
1033 if (!netif_device_present(dev))
1036 /* Check for pending interrupt with expired latency timer: with
1037 this, we can limp along even if the interrupt is blocked */
1038 if ((inw(ioaddr + EL3_STATUS) & IntLatch) && (inb(ioaddr + Timer) == 0xff)) {
1040 printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1041 el3_interrupt(dev->irq, lp, NULL);
1044 if (lp->fast_poll) {
1046 lp->media.expires = jiffies + 2*HZ/100;
1047 add_timer(&lp->media);
1051 spin_lock_irqsave(&lp->window_lock, flags);
1053 media = mdio_read(ioaddr, lp->phys, 1);
1054 partner = mdio_read(ioaddr, lp->phys, 5);
1057 if (media != lp->media_status) {
1058 if ((media ^ lp->media_status) & 0x0004)
1059 printk(KERN_INFO "%s: %s link beat\n", dev->name,
1060 (lp->media_status & 0x0004) ? "lost" : "found");
1061 if ((media ^ lp->media_status) & 0x0020) {
1063 if (lp->media_status & 0x0020) {
1064 printk(KERN_INFO "%s: autonegotiation restarted\n",
1066 } else if (partner) {
1067 partner &= lp->advertising;
1068 lp->partner = partner;
1069 printk(KERN_INFO "%s: autonegotiation complete: "
1070 "%sbaseT-%cD selected\n", dev->name,
1071 ((partner & 0x0180) ? "100" : "10"),
1072 ((partner & 0x0140) ? 'F' : 'H'));
1074 printk(KERN_INFO "%s: link partner did not autonegotiate\n",
1079 outb((partner & 0x0140 ? 0x20 : 0) |
1080 (dev->mtu > 1500 ? 0x40 : 0), ioaddr + Wn3_MAC_Ctrl);
1085 printk(KERN_INFO "%s: remote fault detected\n",
1088 printk(KERN_INFO "%s: jabber detected\n", dev->name);
1089 lp->media_status = media;
1091 spin_unlock_irqrestore(&lp->window_lock, flags);
1094 lp->media.expires = jiffies + HZ;
1095 add_timer(&lp->media);
1098 static struct net_device_stats *el3_get_stats(struct net_device *dev)
1100 struct el3_private *lp = (struct el3_private *)dev->priv;
1102 if (netif_device_present(dev))
1107 /* Update statistics.
1108 Suprisingly this need not be run single-threaded, but it effectively is.
1109 The counters clear when read, so the adds must merely be atomic.
1111 static void update_stats(struct net_device *dev)
1113 struct el3_private *lp = (struct el3_private *)dev->priv;
1114 ioaddr_t ioaddr = dev->base_addr;
1115 unsigned long flags;
1118 DEBUG(2, "%s: updating the statistics.\n", dev->name);
1120 if (inw(ioaddr+EL3_STATUS) == 0xffff) /* No card. */
1123 spin_lock_irqsave(&lp->window_lock, flags);
1125 /* Unlike the 3c509 we need not turn off stats updates while reading. */
1126 /* Switch to the stats window, and read everything. */
1128 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
1129 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
1130 /* Multiple collisions. */ inb(ioaddr + 2);
1131 lp->stats.collisions += inb(ioaddr + 3);
1132 lp->stats.tx_window_errors += inb(ioaddr + 4);
1133 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
1134 lp->stats.tx_packets += inb(ioaddr + 6);
1135 up = inb(ioaddr + 9);
1136 lp->stats.tx_packets += (up&0x30) << 4;
1137 /* Rx packets */ inb(ioaddr + 7);
1138 /* Tx deferrals */ inb(ioaddr + 8);
1139 rx = inw(ioaddr + 10);
1140 tx = inw(ioaddr + 12);
1143 /* BadSSD */ inb(ioaddr + 12);
1144 up = inb(ioaddr + 13);
1146 lp->stats.tx_bytes += tx + ((up & 0xf0) << 12);
1149 spin_unlock_irqrestore(&lp->window_lock, flags);
1152 static int el3_rx(struct net_device *dev, int worklimit)
1154 struct el3_private *lp = (struct el3_private *)dev->priv;
1155 ioaddr_t ioaddr = dev->base_addr;
1158 DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
1159 dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RxStatus));
1160 while (!((rx_status = inw(ioaddr + RxStatus)) & 0x8000) &&
1161 (--worklimit >= 0)) {
1162 if (rx_status & 0x4000) { /* Error, update stats. */
1163 short error = rx_status & 0x3800;
1164 lp->stats.rx_errors++;
1166 case 0x0000: lp->stats.rx_over_errors++; break;
1167 case 0x0800: lp->stats.rx_length_errors++; break;
1168 case 0x1000: lp->stats.rx_frame_errors++; break;
1169 case 0x1800: lp->stats.rx_length_errors++; break;
1170 case 0x2000: lp->stats.rx_frame_errors++; break;
1171 case 0x2800: lp->stats.rx_crc_errors++; break;
1174 short pkt_len = rx_status & 0x7ff;
1175 struct sk_buff *skb;
1177 skb = dev_alloc_skb(pkt_len+5);
1179 DEBUG(3, " Receiving packet size %d status %4.4x.\n",
1180 pkt_len, rx_status);
1183 skb_reserve(skb, 2);
1184 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
1186 skb->protocol = eth_type_trans(skb, dev);
1188 dev->last_rx = jiffies;
1189 lp->stats.rx_packets++;
1190 lp->stats.rx_bytes += pkt_len;
1192 DEBUG(1, "%s: couldn't allocate a sk_buff of"
1193 " size %d.\n", dev->name, pkt_len);
1194 lp->stats.rx_dropped++;
1197 tc574_wait_for_completion(dev, RxDiscard);
1203 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1207 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1211 case ETHTOOL_GDRVINFO: {
1212 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1213 strncpy(info.driver, "3c574_cs", sizeof(info.driver)-1);
1214 if (copy_to_user(useraddr, &info, sizeof(info)))
1223 /* Provide ioctl() calls to examine the MII xcvr state. */
1224 static int el3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1226 struct el3_private *lp = (struct el3_private *)dev->priv;
1227 ioaddr_t ioaddr = dev->base_addr;
1228 u16 *data = (u16 *)&rq->ifr_data;
1229 int phy = lp->phys & 0x1f;
1231 DEBUG(2, "%s: In ioct(%-.6s, %#4.4x) %4.4x %4.4x %4.4x %4.4x.\n",
1232 dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1233 data[0], data[1], data[2], data[3]);
1237 return netdev_ethtool_ioctl(dev, (void *)rq->ifr_data);
1238 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
1240 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
1243 unsigned long flags;
1245 spin_lock_irqsave(&lp->window_lock, flags);
1246 saved_window = inw(ioaddr + EL3_CMD) >> 13;
1248 data[3] = mdio_read(ioaddr, data[0] & 0x1f, data[1] & 0x1f);
1249 EL3WINDOW(saved_window);
1250 spin_unlock_irqrestore(&lp->window_lock, flags);
1253 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
1256 unsigned long flags;
1258 if (!capable(CAP_NET_ADMIN))
1260 spin_lock_irqsave(&lp->window_lock, flags);
1261 saved_window = inw(ioaddr + EL3_CMD) >> 13;
1263 mdio_write(ioaddr, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1264 EL3WINDOW(saved_window);
1265 spin_unlock_irqrestore(&lp->window_lock, flags);
1273 /* The Odie chip has a 64 bin multicast filter, but the bit layout is not
1274 documented. Until it is we revert to receiving all multicast frames when
1275 any multicast reception is desired.
1276 Note: My other drivers emit a log message whenever promiscuous mode is
1277 entered to help detect password sniffers. This is less desirable on
1278 typical PC card machines, so we omit the message.
1281 static void set_rx_mode(struct net_device *dev)
1283 ioaddr_t ioaddr = dev->base_addr;
1285 if (dev->flags & IFF_PROMISC)
1286 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1288 else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1289 outw(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
1291 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1294 static int el3_close(struct net_device *dev)
1296 ioaddr_t ioaddr = dev->base_addr;
1297 struct el3_private *lp = dev->priv;
1298 dev_link_t *link = &lp->link;
1300 DEBUG(2, "%s: shutting down ethercard.\n", dev->name);
1303 /* Turn off statistics ASAP. We update lp->stats below. */
1304 outw(StatsDisable, ioaddr + EL3_CMD);
1306 /* Disable the receiver and transmitter. */
1307 outw(RxDisable, ioaddr + EL3_CMD);
1308 outw(TxDisable, ioaddr + EL3_CMD);
1310 /* Note: Switching to window 0 may disable the IRQ. */
1317 netif_stop_queue(dev);
1318 del_timer_sync(&lp->media);
1319 if (link->state & DEV_STALE_CONFIG)
1320 tc574_release(link);
1324 static struct pcmcia_driver tc574_driver = {
1325 .owner = THIS_MODULE,
1329 .attach = tc574_attach,
1330 .detach = tc574_detach,
1333 static int __init init_tc574(void)
1335 return pcmcia_register_driver(&tc574_driver);
1338 static void __exit exit_tc574(void)
1340 pcmcia_unregister_driver(&tc574_driver);
1341 while (dev_list != NULL)
1342 tc574_detach(dev_list);
1345 module_init(init_tc574);
1346 module_exit(exit_tc574);