Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / drivers / net / pcmcia / 3c574_cs.c
1 /* 3c574.c: A PCMCIA ethernet driver for the 3com 3c574 "RoadRunner".
2
3         Written 1993-1998 by
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
7
8         This software may be used and distributed according to the terms of
9         the GNU General Public License, incorporated herein by reference.
10
11         This driver derives from Donald Becker's 3c509 core, which has the
12         following copyright:
13         Copyright 1993 United States Government as represented by the
14         Director, National Security Agency.
15         
16
17 */
18
19 /*
20                                 Theory of Operation
21
22 I. Board Compatibility
23
24 This device driver is designed for the 3Com 3c574 PC card Fast Ethernet
25 Adapter.
26
27 II. Board-specific settings
28
29 None -- PC cards are autoconfigured.
30
31 III. Driver operation
32
33 The 3c574 uses a Boomerang-style interface, without the bus-master capability.
34 See the Boomerang driver and documentation for most details.
35
36 IV. Notes and chip documentation.
37
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.
44
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.
49
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.
53
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.
58
59 A final note: writing the InternalConfig register in window 3 with an
60 invalid ramWidth is Very Bad.
61
62 V. References
63
64 http://www.scyld.com/expert/NWay.html
65 http://www.national.com/pf/DP/DP83840.html
66
67 Thanks to Terry Murphy of 3Com for providing development information for
68 earlier 3Com products.
69
70 */
71
72 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
73
74 #include <linux/module.h>
75 #include <linux/kernel.h>
76 #include <linux/init.h>
77 #include <linux/slab.h>
78 #include <linux/string.h>
79 #include <linux/timer.h>
80 #include <linux/interrupt.h>
81 #include <linux/in.h>
82 #include <linux/delay.h>
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/if_arp.h>
87 #include <linux/ioport.h>
88 #include <linux/bitops.h>
89 #include <linux/mii.h>
90
91 #include <pcmcia/cs.h>
92 #include <pcmcia/cistpl.h>
93 #include <pcmcia/cisreg.h>
94 #include <pcmcia/ciscode.h>
95 #include <pcmcia/ds.h>
96
97 #include <asm/uaccess.h>
98 #include <asm/io.h>
99 #include <asm/system.h>
100
101 /*====================================================================*/
102
103 /* Module parameters */
104
105 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
106 MODULE_DESCRIPTION("3Com 3c574 series PCMCIA ethernet driver");
107 MODULE_LICENSE("GPL");
108
109 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
110
111 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
112 INT_MODULE_PARM(max_interrupt_work, 32);
113
114 /* Force full duplex modes? */
115 INT_MODULE_PARM(full_duplex, 0);
116
117 /* Autodetect link polarity reversal? */
118 INT_MODULE_PARM(auto_polarity, 1);
119
120
121 /*====================================================================*/
122
123 /* Time in jiffies before concluding the transmitter is hung. */
124 #define TX_TIMEOUT  ((800*HZ)/1000)
125
126 /* To minimize the size of the driver source and make the driver more
127    readable not all constants are symbolically defined.
128    You'll need the manual if you want to understand driver details anyway. */
129 /* Offsets from base I/O address. */
130 #define EL3_DATA        0x00
131 #define EL3_CMD         0x0e
132 #define EL3_STATUS      0x0e
133
134 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
135
136 /* The top five bits written to EL3_CMD are a command, the lower
137    11 bits are the parameter, if applicable. */
138 enum el3_cmds {
139         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
140         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
141         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
142         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
143         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
144         SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
145         StatsDisable = 22<<11, StopCoax = 23<<11,
146 };
147
148 enum elxl_status {
149         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
150         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
151         IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000 };
152
153 /* The SetRxFilter command accepts the following classes: */
154 enum RxFilter {
155         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
156 };
157
158 enum Window0 {
159         Wn0EepromCmd = 10, Wn0EepromData = 12, /* EEPROM command/address, data. */
160         IntrStatus=0x0E,                /* Valid in all windows. */
161 };
162 /* These assumes the larger EEPROM. */
163 enum Win0_EEPROM_cmds {
164         EEPROM_Read = 0x200, EEPROM_WRITE = 0x100, EEPROM_ERASE = 0x300,
165         EEPROM_EWENB = 0x30,            /* Enable erasing/writing for 10 msec. */
166         EEPROM_EWDIS = 0x00,            /* Disable EWENB before 10 msec timeout. */
167 };
168
169 /* Register window 1 offsets, the window used in normal operation.
170    On the "Odie" this window is always mapped at offsets 0x10-0x1f.
171    Except for TxFree, which is overlapped by RunnerWrCtrl. */
172 enum Window1 {
173         TX_FIFO = 0x10,  RX_FIFO = 0x10,  RxErrors = 0x14,
174         RxStatus = 0x18,  Timer=0x1A, TxStatus = 0x1B,
175         TxFree = 0x0C, /* Remaining free bytes in Tx buffer. */
176         RunnerRdCtrl = 0x16, RunnerWrCtrl = 0x1c,
177 };
178
179 enum Window3 {                  /* Window 3: MAC/config bits. */
180         Wn3_Config=0, Wn3_MAC_Ctrl=6, Wn3_Options=8,
181 };
182 enum wn3_config {
183         Ram_size = 7,
184         Ram_width = 8,
185         Ram_speed = 0x30,
186         Rom_size = 0xc0,
187         Ram_split_shift = 16,
188         Ram_split = 3 << Ram_split_shift,
189         Xcvr_shift = 20,
190         Xcvr = 7 << Xcvr_shift,
191         Autoselect = 0x1000000,
192 };
193
194 enum Window4 {          /* Window 4: Xcvr/media bits. */
195         Wn4_FIFODiag = 4, Wn4_NetDiag = 6, Wn4_PhysicalMgmt=8, Wn4_Media = 10,
196 };
197
198 #define MEDIA_TP        0x00C0  /* Enable link beat and jabber for 10baseT. */
199
200 struct el3_private {
201         struct pcmcia_device    *p_dev;
202         u16 advertising, partner;               /* NWay media advertisement */
203         unsigned char phys;                     /* MII device address */
204         unsigned int autoselect:1, default_media:3;     /* Read from the EEPROM/Wn3_Config. */
205         /* for transceiver monitoring */
206         struct timer_list media;
207         unsigned short media_status;
208         unsigned short fast_poll;
209         unsigned long last_irq;
210         spinlock_t window_lock;                 /* Guards the Window selection */
211 };
212
213 /* Set iff a MII transceiver on any interface requires mdio preamble.
214    This only set with the original DP83840 on older 3c905 boards, so the extra
215    code size of a per-interface flag is not worthwhile. */
216 static char mii_preamble_required = 0;
217
218 /* Index of functions. */
219
220 static int tc574_config(struct pcmcia_device *link);
221 static void tc574_release(struct pcmcia_device *link);
222
223 static void mdio_sync(unsigned int ioaddr, int bits);
224 static int mdio_read(unsigned int ioaddr, int phy_id, int location);
225 static void mdio_write(unsigned int ioaddr, int phy_id, int location,
226                        int value);
227 static unsigned short read_eeprom(unsigned int ioaddr, int index);
228 static void tc574_wait_for_completion(struct net_device *dev, int cmd);
229
230 static void tc574_reset(struct net_device *dev);
231 static void media_check(unsigned long arg);
232 static int el3_open(struct net_device *dev);
233 static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
234                                         struct net_device *dev);
235 static irqreturn_t el3_interrupt(int irq, void *dev_id);
236 static void update_stats(struct net_device *dev);
237 static struct net_device_stats *el3_get_stats(struct net_device *dev);
238 static int el3_rx(struct net_device *dev, int worklimit);
239 static int el3_close(struct net_device *dev);
240 static void el3_tx_timeout(struct net_device *dev);
241 static int el3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
242 static void set_rx_mode(struct net_device *dev);
243 static void set_multicast_list(struct net_device *dev);
244
245 static void tc574_detach(struct pcmcia_device *p_dev);
246
247 /*
248         tc574_attach() creates an "instance" of the driver, allocating
249         local data structures for one device.  The device is registered
250         with Card Services.
251 */
252 static const struct net_device_ops el3_netdev_ops = {
253         .ndo_open               = el3_open,
254         .ndo_stop               = el3_close,
255         .ndo_start_xmit         = el3_start_xmit,
256         .ndo_tx_timeout         = el3_tx_timeout,
257         .ndo_get_stats          = el3_get_stats,
258         .ndo_do_ioctl           = el3_ioctl,
259         .ndo_set_multicast_list = set_multicast_list,
260         .ndo_change_mtu         = eth_change_mtu,
261         .ndo_set_mac_address    = eth_mac_addr,
262         .ndo_validate_addr      = eth_validate_addr,
263 };
264
265 static int tc574_probe(struct pcmcia_device *link)
266 {
267         struct el3_private *lp;
268         struct net_device *dev;
269
270         dev_dbg(&link->dev, "3c574_attach()\n");
271
272         /* Create the PC card device object. */
273         dev = alloc_etherdev(sizeof(struct el3_private));
274         if (!dev)
275                 return -ENOMEM;
276         lp = netdev_priv(dev);
277         link->priv = dev;
278         lp->p_dev = link;
279
280         spin_lock_init(&lp->window_lock);
281         link->resource[0]->end = 32;
282         link->resource[0]->flags |= IO_DATA_PATH_WIDTH_16;
283         link->conf.Attributes = CONF_ENABLE_IRQ;
284         link->conf.IntType = INT_MEMORY_AND_IO;
285         link->conf.ConfigIndex = 1;
286
287         dev->netdev_ops = &el3_netdev_ops;
288         dev->watchdog_timeo = TX_TIMEOUT;
289
290         return tc574_config(link);
291 } /* tc574_attach */
292
293 /*
294
295         This deletes a driver "instance".  The device is de-registered
296         with Card Services.  If it has been released, all local data
297         structures are freed.  Otherwise, the structures will be freed
298         when the device is released.
299
300 */
301
302 static void tc574_detach(struct pcmcia_device *link)
303 {
304         struct net_device *dev = link->priv;
305
306         dev_dbg(&link->dev, "3c574_detach()\n");
307
308         unregister_netdev(dev);
309
310         tc574_release(link);
311
312         free_netdev(dev);
313 } /* tc574_detach */
314
315 /*
316         tc574_config() is scheduled to run after a CARD_INSERTION event
317         is received, to configure the PCMCIA socket, and to make the
318         ethernet device available to the system.
319 */
320
321 static const char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
322
323 static int tc574_config(struct pcmcia_device *link)
324 {
325         struct net_device *dev = link->priv;
326         struct el3_private *lp = netdev_priv(dev);
327         int ret, i, j;
328         unsigned int ioaddr;
329         __be16 *phys_addr;
330         char *cardname;
331         __u32 config;
332         u8 *buf;
333         size_t len;
334
335         phys_addr = (__be16 *)dev->dev_addr;
336
337         dev_dbg(&link->dev, "3c574_config()\n");
338
339         link->io_lines = 16;
340
341         for (i = j = 0; j < 0x400; j += 0x20) {
342                 link->resource[0]->start = j ^ 0x300;
343                 i = pcmcia_request_io(link);
344                 if (i == 0)
345                         break;
346         }
347         if (i != 0)
348                 goto failed;
349
350         ret = pcmcia_request_irq(link, el3_interrupt);
351         if (ret)
352                 goto failed;
353
354         ret = pcmcia_request_configuration(link, &link->conf);
355         if (ret)
356                 goto failed;
357
358         dev->irq = link->irq;
359         dev->base_addr = link->resource[0]->start;
360
361         ioaddr = dev->base_addr;
362
363         /* The 3c574 normally uses an EEPROM for configuration info, including
364            the hardware address.  The future products may include a modem chip
365            and put the address in the CIS. */
366
367         len = pcmcia_get_tuple(link, 0x88, &buf);
368         if (buf && len >= 6) {
369                 for (i = 0; i < 3; i++)
370                         phys_addr[i] = htons(le16_to_cpu(buf[i * 2]));
371                 kfree(buf);
372         } else {
373                 kfree(buf); /* 0 < len < 6 */
374                 EL3WINDOW(0);
375                 for (i = 0; i < 3; i++)
376                         phys_addr[i] = htons(read_eeprom(ioaddr, i + 10));
377                 if (phys_addr[0] == htons(0x6060)) {
378                         pr_notice("IO port conflict at 0x%03lx-0x%03lx\n",
379                                   dev->base_addr, dev->base_addr+15);
380                         goto failed;
381                 }
382         }
383         if (link->prod_id[1])
384                 cardname = link->prod_id[1];
385         else
386                 cardname = "3Com 3c574";
387
388         {
389                 u_char mcr;
390                 outw(2<<11, ioaddr + RunnerRdCtrl);
391                 mcr = inb(ioaddr + 2);
392                 outw(0<<11, ioaddr + RunnerRdCtrl);
393                 pr_info("  ASIC rev %d,", mcr>>3);
394                 EL3WINDOW(3);
395                 config = inl(ioaddr + Wn3_Config);
396                 lp->default_media = (config & Xcvr) >> Xcvr_shift;
397                 lp->autoselect = config & Autoselect ? 1 : 0;
398         }
399
400         init_timer(&lp->media);
401
402         {
403                 int phy;
404                 
405                 /* Roadrunner only: Turn on the MII transceiver */
406                 outw(0x8040, ioaddr + Wn3_Options);
407                 mdelay(1);
408                 outw(0xc040, ioaddr + Wn3_Options);
409                 tc574_wait_for_completion(dev, TxReset);
410                 tc574_wait_for_completion(dev, RxReset);
411                 mdelay(1);
412                 outw(0x8040, ioaddr + Wn3_Options);
413                 
414                 EL3WINDOW(4);
415                 for (phy = 1; phy <= 32; phy++) {
416                         int mii_status;
417                         mdio_sync(ioaddr, 32);
418                         mii_status = mdio_read(ioaddr, phy & 0x1f, 1);
419                         if (mii_status != 0xffff) {
420                                 lp->phys = phy & 0x1f;
421                                 dev_dbg(&link->dev, "  MII transceiver at "
422                                         "index %d, status %x.\n",
423                                           phy, mii_status);
424                                 if ((mii_status & 0x0040) == 0)
425                                         mii_preamble_required = 1;
426                                 break;
427                         }
428                 }
429                 if (phy > 32) {
430                         pr_notice("  No MII transceivers found!\n");
431                         goto failed;
432                 }
433                 i = mdio_read(ioaddr, lp->phys, 16) | 0x40;
434                 mdio_write(ioaddr, lp->phys, 16, i);
435                 lp->advertising = mdio_read(ioaddr, lp->phys, 4);
436                 if (full_duplex) {
437                         /* Only advertise the FD media types. */
438                         lp->advertising &= ~0x02a0;
439                         mdio_write(ioaddr, lp->phys, 4, lp->advertising);
440                 }
441         }
442
443         SET_NETDEV_DEV(dev, &link->dev);
444
445         if (register_netdev(dev) != 0) {
446                 pr_notice("register_netdev() failed\n");
447                 goto failed;
448         }
449
450         netdev_info(dev, "%s at io %#3lx, irq %d, hw_addr %pM\n",
451                     cardname, dev->base_addr, dev->irq, dev->dev_addr);
452         netdev_info(dev, " %dK FIFO split %s Rx:Tx, %sMII interface.\n",
453                     8 << config & Ram_size,
454                     ram_split[(config & Ram_split) >> Ram_split_shift],
455                     config & Autoselect ? "autoselect " : "");
456
457         return 0;
458
459 failed:
460         tc574_release(link);
461         return -ENODEV;
462
463 } /* tc574_config */
464
465 /*
466         After a card is removed, tc574_release() will unregister the net
467         device, and release the PCMCIA configuration.  If the device is
468         still open, this will be postponed until it is closed.
469 */
470
471 static void tc574_release(struct pcmcia_device *link)
472 {
473         pcmcia_disable_device(link);
474 }
475
476 static int tc574_suspend(struct pcmcia_device *link)
477 {
478         struct net_device *dev = link->priv;
479
480         if (link->open)
481                 netif_device_detach(dev);
482
483         return 0;
484 }
485
486 static int tc574_resume(struct pcmcia_device *link)
487 {
488         struct net_device *dev = link->priv;
489
490         if (link->open) {
491                 tc574_reset(dev);
492                 netif_device_attach(dev);
493         }
494
495         return 0;
496 }
497
498 static void dump_status(struct net_device *dev)
499 {
500         unsigned int ioaddr = dev->base_addr;
501         EL3WINDOW(1);
502         netdev_info(dev, "  irq status %04x, rx status %04x, tx status %02x, tx free %04x\n",
503                     inw(ioaddr+EL3_STATUS),
504                     inw(ioaddr+RxStatus), inb(ioaddr+TxStatus),
505                     inw(ioaddr+TxFree));
506         EL3WINDOW(4);
507         netdev_info(dev, "  diagnostics: fifo %04x net %04x ethernet %04x media %04x\n",
508                     inw(ioaddr+0x04), inw(ioaddr+0x06),
509                     inw(ioaddr+0x08), inw(ioaddr+0x0a));
510         EL3WINDOW(1);
511 }
512
513 /*
514   Use this for commands that may take time to finish
515 */
516 static void tc574_wait_for_completion(struct net_device *dev, int cmd)
517 {
518         int i = 1500;
519         outw(cmd, dev->base_addr + EL3_CMD);
520         while (--i > 0)
521                 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
522         if (i == 0)
523                 netdev_notice(dev, "command 0x%04x did not complete!\n", cmd);
524 }
525
526 /* Read a word from the EEPROM using the regular EEPROM access register.
527    Assume that we are in register window zero.
528  */
529 static unsigned short read_eeprom(unsigned int ioaddr, int index)
530 {
531         int timer;
532         outw(EEPROM_Read + index, ioaddr + Wn0EepromCmd);
533         /* Pause for at least 162 usec for the read to take place. */
534         for (timer = 1620; timer >= 0; timer--) {
535                 if ((inw(ioaddr + Wn0EepromCmd) & 0x8000) == 0)
536                         break;
537         }
538         return inw(ioaddr + Wn0EepromData);
539 }
540
541 /* MII transceiver control section.
542    Read and write the MII registers using software-generated serial
543    MDIO protocol.  See the MII specifications or DP83840A data sheet
544    for details.
545    The maxium data clock rate is 2.5 Mhz.  The timing is easily met by the
546    slow PC card interface. */
547
548 #define MDIO_SHIFT_CLK  0x01
549 #define MDIO_DIR_WRITE  0x04
550 #define MDIO_DATA_WRITE0 (0x00 | MDIO_DIR_WRITE)
551 #define MDIO_DATA_WRITE1 (0x02 | MDIO_DIR_WRITE)
552 #define MDIO_DATA_READ  0x02
553 #define MDIO_ENB_IN             0x00
554
555 /* Generate the preamble required for initial synchronization and
556    a few older transceivers. */
557 static void mdio_sync(unsigned int ioaddr, int bits)
558 {
559         unsigned int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
560
561         /* Establish sync by sending at least 32 logic ones. */
562         while (-- bits >= 0) {
563                 outw(MDIO_DATA_WRITE1, mdio_addr);
564                 outw(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
565         }
566 }
567
568 static int mdio_read(unsigned int ioaddr, int phy_id, int location)
569 {
570         int i;
571         int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
572         unsigned int retval = 0;
573         unsigned int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
574
575         if (mii_preamble_required)
576                 mdio_sync(ioaddr, 32);
577
578         /* Shift the read command bits out. */
579         for (i = 14; i >= 0; i--) {
580                 int dataval = (read_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
581                 outw(dataval, mdio_addr);
582                 outw(dataval | MDIO_SHIFT_CLK, mdio_addr);
583         }
584         /* Read the two transition, 16 data, and wire-idle bits. */
585         for (i = 19; i > 0; i--) {
586                 outw(MDIO_ENB_IN, mdio_addr);
587                 retval = (retval << 1) | ((inw(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
588                 outw(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
589         }
590         return (retval>>1) & 0xffff;
591 }
592
593 static void mdio_write(unsigned int ioaddr, int phy_id, int location, int value)
594 {
595         int write_cmd = 0x50020000 | (phy_id << 23) | (location << 18) | value;
596         unsigned int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
597         int i;
598
599         if (mii_preamble_required)
600                 mdio_sync(ioaddr, 32);
601
602         /* Shift the command bits out. */
603         for (i = 31; i >= 0; i--) {
604                 int dataval = (write_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
605                 outw(dataval, mdio_addr);
606                 outw(dataval | MDIO_SHIFT_CLK, mdio_addr);
607         }
608         /* Leave the interface idle. */
609         for (i = 1; i >= 0; i--) {
610                 outw(MDIO_ENB_IN, mdio_addr);
611                 outw(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
612         }
613 }
614
615 /* Reset and restore all of the 3c574 registers. */
616 static void tc574_reset(struct net_device *dev)
617 {
618         struct el3_private *lp = netdev_priv(dev);
619         int i;
620         unsigned int ioaddr = dev->base_addr;
621         unsigned long flags;
622
623         tc574_wait_for_completion(dev, TotalReset|0x10);
624
625         spin_lock_irqsave(&lp->window_lock, flags);
626         /* Clear any transactions in progress. */
627         outw(0, ioaddr + RunnerWrCtrl);
628         outw(0, ioaddr + RunnerRdCtrl);
629
630         /* Set the station address and mask. */
631         EL3WINDOW(2);
632         for (i = 0; i < 6; i++)
633                 outb(dev->dev_addr[i], ioaddr + i);
634         for (; i < 12; i+=2)
635                 outw(0, ioaddr + i);
636
637         /* Reset config options */
638         EL3WINDOW(3);
639         outb((dev->mtu > 1500 ? 0x40 : 0), ioaddr + Wn3_MAC_Ctrl);
640         outl((lp->autoselect ? 0x01000000 : 0) | 0x0062001b,
641                  ioaddr + Wn3_Config);
642         /* Roadrunner only: Turn on the MII transceiver. */
643         outw(0x8040, ioaddr + Wn3_Options);
644         mdelay(1);
645         outw(0xc040, ioaddr + Wn3_Options);
646         EL3WINDOW(1);
647         spin_unlock_irqrestore(&lp->window_lock, flags);
648         
649         tc574_wait_for_completion(dev, TxReset);
650         tc574_wait_for_completion(dev, RxReset);
651         mdelay(1);
652         spin_lock_irqsave(&lp->window_lock, flags);
653         EL3WINDOW(3);
654         outw(0x8040, ioaddr + Wn3_Options);
655
656         /* Switch to the stats window, and clear all stats by reading. */
657         outw(StatsDisable, ioaddr + EL3_CMD);
658         EL3WINDOW(6);
659         for (i = 0; i < 10; i++)
660                 inb(ioaddr + i);
661         inw(ioaddr + 10);
662         inw(ioaddr + 12);
663         EL3WINDOW(4);
664         inb(ioaddr + 12);
665         inb(ioaddr + 13);
666
667         /* .. enable any extra statistics bits.. */
668         outw(0x0040, ioaddr + Wn4_NetDiag);
669         
670         EL3WINDOW(1);
671         spin_unlock_irqrestore(&lp->window_lock, flags);
672         
673         /* .. re-sync MII and re-fill what NWay is advertising. */
674         mdio_sync(ioaddr, 32);
675         mdio_write(ioaddr, lp->phys, 4, lp->advertising);
676         if (!auto_polarity) {
677                 /* works for TDK 78Q2120 series MII's */
678                 i = mdio_read(ioaddr, lp->phys, 16) | 0x20;
679                 mdio_write(ioaddr, lp->phys, 16, i);
680         }
681
682         spin_lock_irqsave(&lp->window_lock, flags);
683         /* Switch to register set 1 for normal use, just for TxFree. */
684         set_rx_mode(dev);
685         spin_unlock_irqrestore(&lp->window_lock, flags);
686         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
687         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
688         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
689         /* Allow status bits to be seen. */
690         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
691         /* Ack all pending events, and set active indicator mask. */
692         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
693                  ioaddr + EL3_CMD);
694         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
695                  | AdapterFailure | RxEarly, ioaddr + EL3_CMD);
696 }
697
698 static int el3_open(struct net_device *dev)
699 {
700         struct el3_private *lp = netdev_priv(dev);
701         struct pcmcia_device *link = lp->p_dev;
702
703         if (!pcmcia_dev_present(link))
704                 return -ENODEV;
705         
706         link->open++;
707         netif_start_queue(dev);
708         
709         tc574_reset(dev);
710         lp->media.function = media_check;
711         lp->media.data = (unsigned long) dev;
712         lp->media.expires = jiffies + HZ;
713         add_timer(&lp->media);
714         
715         dev_dbg(&link->dev, "%s: opened, status %4.4x.\n",
716                   dev->name, inw(dev->base_addr + EL3_STATUS));
717         
718         return 0;
719 }
720
721 static void el3_tx_timeout(struct net_device *dev)
722 {
723         unsigned int ioaddr = dev->base_addr;
724         
725         netdev_notice(dev, "Transmit timed out!\n");
726         dump_status(dev);
727         dev->stats.tx_errors++;
728         dev->trans_start = jiffies; /* prevent tx timeout */
729         /* Issue TX_RESET and TX_START commands. */
730         tc574_wait_for_completion(dev, TxReset);
731         outw(TxEnable, ioaddr + EL3_CMD);
732         netif_wake_queue(dev);
733 }
734
735 static void pop_tx_status(struct net_device *dev)
736 {
737         unsigned int ioaddr = dev->base_addr;
738         int i;
739     
740         /* Clear the Tx status stack. */
741         for (i = 32; i > 0; i--) {
742                 u_char tx_status = inb(ioaddr + TxStatus);
743                 if (!(tx_status & 0x84))
744                         break;
745                 /* reset transmitter on jabber error or underrun */
746                 if (tx_status & 0x30)
747                         tc574_wait_for_completion(dev, TxReset);
748                 if (tx_status & 0x38) {
749                         pr_debug("%s: transmit error: status 0x%02x\n",
750                                   dev->name, tx_status);
751                         outw(TxEnable, ioaddr + EL3_CMD);
752                         dev->stats.tx_aborted_errors++;
753                 }
754                 outb(0x00, ioaddr + TxStatus); /* Pop the status stack. */
755         }
756 }
757
758 static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
759                                         struct net_device *dev)
760 {
761         unsigned int ioaddr = dev->base_addr;
762         struct el3_private *lp = netdev_priv(dev);
763         unsigned long flags;
764
765         pr_debug("%s: el3_start_xmit(length = %ld) called, "
766                   "status %4.4x.\n", dev->name, (long)skb->len,
767                   inw(ioaddr + EL3_STATUS));
768
769         spin_lock_irqsave(&lp->window_lock, flags);
770
771         dev->stats.tx_bytes += skb->len;
772
773         /* Put out the doubleword header... */
774         outw(skb->len, ioaddr + TX_FIFO);
775         outw(0, ioaddr + TX_FIFO);
776         /* ... and the packet rounded to a doubleword. */
777         outsl(ioaddr + TX_FIFO, skb->data, (skb->len+3)>>2);
778
779         /* TxFree appears only in Window 1, not offset 0x1c. */
780         if (inw(ioaddr + TxFree) <= 1536) {
781                 netif_stop_queue(dev);
782                 /* Interrupt us when the FIFO has room for max-sized packet. 
783                    The threshold is in units of dwords. */
784                 outw(SetTxThreshold + (1536>>2), ioaddr + EL3_CMD);
785         }
786
787         pop_tx_status(dev);
788         spin_unlock_irqrestore(&lp->window_lock, flags);
789         dev_kfree_skb(skb);
790         return NETDEV_TX_OK;
791 }
792
793 /* The EL3 interrupt handler. */
794 static irqreturn_t el3_interrupt(int irq, void *dev_id)
795 {
796         struct net_device *dev = (struct net_device *) dev_id;
797         struct el3_private *lp = netdev_priv(dev);
798         unsigned int ioaddr;
799         unsigned status;
800         int work_budget = max_interrupt_work;
801         int handled = 0;
802
803         if (!netif_device_present(dev))
804                 return IRQ_NONE;
805         ioaddr = dev->base_addr;
806
807         pr_debug("%s: interrupt, status %4.4x.\n",
808                   dev->name, inw(ioaddr + EL3_STATUS));
809
810         spin_lock(&lp->window_lock);
811         
812         while ((status = inw(ioaddr + EL3_STATUS)) &
813                    (IntLatch | RxComplete | RxEarly | StatsFull)) {
814                 if (!netif_device_present(dev) ||
815                         ((status & 0xe000) != 0x2000)) {
816                         pr_debug("%s: Interrupt from dead card\n", dev->name);
817                         break;
818                 }
819
820                 handled = 1;
821
822                 if (status & RxComplete)
823                         work_budget = el3_rx(dev, work_budget);
824
825                 if (status & TxAvailable) {
826                         pr_debug("  TX room bit was handled.\n");
827                         /* There's room in the FIFO for a full-sized packet. */
828                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
829                         netif_wake_queue(dev);
830                 }
831
832                 if (status & TxComplete)
833                         pop_tx_status(dev);
834
835                 if (status & (AdapterFailure | RxEarly | StatsFull)) {
836                         /* Handle all uncommon interrupts. */
837                         if (status & StatsFull)
838                                 update_stats(dev);
839                         if (status & RxEarly) {
840                                 work_budget = el3_rx(dev, work_budget);
841                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
842                         }
843                         if (status & AdapterFailure) {
844                                 u16 fifo_diag;
845                                 EL3WINDOW(4);
846                                 fifo_diag = inw(ioaddr + Wn4_FIFODiag);
847                                 EL3WINDOW(1);
848                                 netdev_notice(dev, "adapter failure, FIFO diagnostic register %04x\n",
849                                               fifo_diag);
850                                 if (fifo_diag & 0x0400) {
851                                         /* Tx overrun */
852                                         tc574_wait_for_completion(dev, TxReset);
853                                         outw(TxEnable, ioaddr + EL3_CMD);
854                                 }
855                                 if (fifo_diag & 0x2000) {
856                                         /* Rx underrun */
857                                         tc574_wait_for_completion(dev, RxReset);
858                                         set_rx_mode(dev);
859                                         outw(RxEnable, ioaddr + EL3_CMD);
860                                 }
861                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
862                         }
863                 }
864
865                 if (--work_budget < 0) {
866                         pr_debug("%s: Too much work in interrupt, "
867                                   "status %4.4x.\n", dev->name, status);
868                         /* Clear all interrupts */
869                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
870                         break;
871                 }
872                 /* Acknowledge the IRQ. */
873                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
874         }
875
876         pr_debug("%s: exiting interrupt, status %4.4x.\n",
877                   dev->name, inw(ioaddr + EL3_STATUS));
878                   
879         spin_unlock(&lp->window_lock);
880         return IRQ_RETVAL(handled);
881 }
882
883 /*
884     This timer serves two purposes: to check for missed interrupts
885         (and as a last resort, poll the NIC for events), and to monitor
886         the MII, reporting changes in cable status.
887 */
888 static void media_check(unsigned long arg)
889 {
890         struct net_device *dev = (struct net_device *) arg;
891         struct el3_private *lp = netdev_priv(dev);
892         unsigned int ioaddr = dev->base_addr;
893         unsigned long flags;
894         unsigned short /* cable, */ media, partner;
895
896         if (!netif_device_present(dev))
897                 goto reschedule;
898         
899         /* Check for pending interrupt with expired latency timer: with
900            this, we can limp along even if the interrupt is blocked */
901         if ((inw(ioaddr + EL3_STATUS) & IntLatch) && (inb(ioaddr + Timer) == 0xff)) {
902                 if (!lp->fast_poll)
903                         netdev_info(dev, "interrupt(s) dropped!\n");
904
905                 local_irq_save(flags);
906                 el3_interrupt(dev->irq, dev);
907                 local_irq_restore(flags);
908
909                 lp->fast_poll = HZ;
910         }
911         if (lp->fast_poll) {
912                 lp->fast_poll--;
913                 lp->media.expires = jiffies + 2*HZ/100;
914                 add_timer(&lp->media);
915                 return;
916         }
917
918         spin_lock_irqsave(&lp->window_lock, flags);
919         EL3WINDOW(4);
920         media = mdio_read(ioaddr, lp->phys, 1);
921         partner = mdio_read(ioaddr, lp->phys, 5);
922         EL3WINDOW(1);
923         
924         if (media != lp->media_status) {
925                 if ((media ^ lp->media_status) & 0x0004)
926                         netdev_info(dev, "%s link beat\n",
927                                     (lp->media_status & 0x0004) ? "lost" : "found");
928                 if ((media ^ lp->media_status) & 0x0020) {
929                         lp->partner = 0;
930                         if (lp->media_status & 0x0020) {
931                                 netdev_info(dev, "autonegotiation restarted\n");
932                         } else if (partner) {
933                                 partner &= lp->advertising;
934                                 lp->partner = partner;
935                                 netdev_info(dev, "autonegotiation complete: "
936                                             "%dbaseT-%cD selected\n",
937                                             (partner & 0x0180) ? 100 : 10,
938                                             (partner & 0x0140) ? 'F' : 'H');
939                         } else {
940                                 netdev_info(dev, "link partner did not autonegotiate\n");
941                         }
942
943                         EL3WINDOW(3);
944                         outb((partner & 0x0140 ? 0x20 : 0) |
945                                  (dev->mtu > 1500 ? 0x40 : 0), ioaddr + Wn3_MAC_Ctrl);
946                         EL3WINDOW(1);
947
948                 }
949                 if (media & 0x0010)
950                         netdev_info(dev, "remote fault detected\n");
951                 if (media & 0x0002)
952                         netdev_info(dev, "jabber detected\n");
953                 lp->media_status = media;
954         }
955         spin_unlock_irqrestore(&lp->window_lock, flags);
956
957 reschedule:
958         lp->media.expires = jiffies + HZ;
959         add_timer(&lp->media);
960 }
961
962 static struct net_device_stats *el3_get_stats(struct net_device *dev)
963 {
964         struct el3_private *lp = netdev_priv(dev);
965
966         if (netif_device_present(dev)) {
967                 unsigned long flags;
968                 spin_lock_irqsave(&lp->window_lock, flags);
969                 update_stats(dev);
970                 spin_unlock_irqrestore(&lp->window_lock, flags);
971         }
972         return &dev->stats;
973 }
974
975 /*  Update statistics.
976         Suprisingly this need not be run single-threaded, but it effectively is.
977         The counters clear when read, so the adds must merely be atomic.
978  */
979 static void update_stats(struct net_device *dev)
980 {
981         unsigned int ioaddr = dev->base_addr;
982         u8 rx, tx, up;
983
984         pr_debug("%s: updating the statistics.\n", dev->name);
985
986         if (inw(ioaddr+EL3_STATUS) == 0xffff) /* No card. */
987                 return;
988                 
989         /* Unlike the 3c509 we need not turn off stats updates while reading. */
990         /* Switch to the stats window, and read everything. */
991         EL3WINDOW(6);
992         dev->stats.tx_carrier_errors            += inb(ioaddr + 0);
993         dev->stats.tx_heartbeat_errors          += inb(ioaddr + 1);
994         /* Multiple collisions. */              inb(ioaddr + 2);
995         dev->stats.collisions                   += inb(ioaddr + 3);
996         dev->stats.tx_window_errors             += inb(ioaddr + 4);
997         dev->stats.rx_fifo_errors               += inb(ioaddr + 5);
998         dev->stats.tx_packets                   += inb(ioaddr + 6);
999         up                                       = inb(ioaddr + 9);
1000         dev->stats.tx_packets                   += (up&0x30) << 4;
1001         /* Rx packets   */                         inb(ioaddr + 7);
1002         /* Tx deferrals */                         inb(ioaddr + 8);
1003         rx                                       = inw(ioaddr + 10);
1004         tx                                       = inw(ioaddr + 12);
1005
1006         EL3WINDOW(4);
1007         /* BadSSD */                               inb(ioaddr + 12);
1008         up                                       = inb(ioaddr + 13);
1009
1010         EL3WINDOW(1);
1011 }
1012
1013 static int el3_rx(struct net_device *dev, int worklimit)
1014 {
1015         unsigned int ioaddr = dev->base_addr;
1016         short rx_status;
1017         
1018         pr_debug("%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
1019                   dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RxStatus));
1020         while (!((rx_status = inw(ioaddr + RxStatus)) & 0x8000) &&
1021                         worklimit > 0) {
1022                 worklimit--;
1023                 if (rx_status & 0x4000) { /* Error, update stats. */
1024                         short error = rx_status & 0x3800;
1025                         dev->stats.rx_errors++;
1026                         switch (error) {
1027                         case 0x0000:    dev->stats.rx_over_errors++; break;
1028                         case 0x0800:    dev->stats.rx_length_errors++; break;
1029                         case 0x1000:    dev->stats.rx_frame_errors++; break;
1030                         case 0x1800:    dev->stats.rx_length_errors++; break;
1031                         case 0x2000:    dev->stats.rx_frame_errors++; break;
1032                         case 0x2800:    dev->stats.rx_crc_errors++; break;
1033                         }
1034                 } else {
1035                         short pkt_len = rx_status & 0x7ff;
1036                         struct sk_buff *skb;
1037
1038                         skb = dev_alloc_skb(pkt_len+5);
1039
1040                         pr_debug("  Receiving packet size %d status %4.4x.\n",
1041                                   pkt_len, rx_status);
1042                         if (skb != NULL) {
1043                                 skb_reserve(skb, 2);
1044                                 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
1045                                                 ((pkt_len+3)>>2));
1046                                 skb->protocol = eth_type_trans(skb, dev);
1047                                 netif_rx(skb);
1048                                 dev->stats.rx_packets++;
1049                                 dev->stats.rx_bytes += pkt_len;
1050                         } else {
1051                                 pr_debug("%s: couldn't allocate a sk_buff of"
1052                                           " size %d.\n", dev->name, pkt_len);
1053                                 dev->stats.rx_dropped++;
1054                         }
1055                 }
1056                 tc574_wait_for_completion(dev, RxDiscard);
1057         }
1058
1059         return worklimit;
1060 }
1061
1062 /* Provide ioctl() calls to examine the MII xcvr state. */
1063 static int el3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1064 {
1065         struct el3_private *lp = netdev_priv(dev);
1066         unsigned int ioaddr = dev->base_addr;
1067         struct mii_ioctl_data *data = if_mii(rq);
1068         int phy = lp->phys & 0x1f;
1069
1070         pr_debug("%s: In ioct(%-.6s, %#4.4x) %4.4x %4.4x %4.4x %4.4x.\n",
1071                   dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1072                   data->phy_id, data->reg_num, data->val_in, data->val_out);
1073
1074         switch(cmd) {
1075         case SIOCGMIIPHY:               /* Get the address of the PHY in use. */
1076                 data->phy_id = phy;
1077         case SIOCGMIIREG:               /* Read the specified MII register. */
1078                 {
1079                         int saved_window;
1080                         unsigned long flags;
1081
1082                         spin_lock_irqsave(&lp->window_lock, flags);
1083                         saved_window = inw(ioaddr + EL3_CMD) >> 13;
1084                         EL3WINDOW(4);
1085                         data->val_out = mdio_read(ioaddr, data->phy_id & 0x1f,
1086                                                   data->reg_num & 0x1f);
1087                         EL3WINDOW(saved_window);
1088                         spin_unlock_irqrestore(&lp->window_lock, flags);
1089                         return 0;
1090                 }
1091         case SIOCSMIIREG:               /* Write the specified MII register */
1092                 {
1093                         int saved_window;
1094                        unsigned long flags;
1095
1096                         spin_lock_irqsave(&lp->window_lock, flags);
1097                         saved_window = inw(ioaddr + EL3_CMD) >> 13;
1098                         EL3WINDOW(4);
1099                         mdio_write(ioaddr, data->phy_id & 0x1f,
1100                                    data->reg_num & 0x1f, data->val_in);
1101                         EL3WINDOW(saved_window);
1102                         spin_unlock_irqrestore(&lp->window_lock, flags);
1103                         return 0;
1104                 }
1105         default:
1106                 return -EOPNOTSUPP;
1107         }
1108 }
1109
1110 /* The Odie chip has a 64 bin multicast filter, but the bit layout is not
1111    documented.  Until it is we revert to receiving all multicast frames when
1112    any multicast reception is desired.
1113    Note: My other drivers emit a log message whenever promiscuous mode is
1114    entered to help detect password sniffers.  This is less desirable on
1115    typical PC card machines, so we omit the message.
1116    */
1117
1118 static void set_rx_mode(struct net_device *dev)
1119 {
1120         unsigned int ioaddr = dev->base_addr;
1121
1122         if (dev->flags & IFF_PROMISC)
1123                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1124                          ioaddr + EL3_CMD);
1125         else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI))
1126                 outw(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
1127         else
1128                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1129 }
1130
1131 static void set_multicast_list(struct net_device *dev)
1132 {
1133         struct el3_private *lp = netdev_priv(dev);
1134         unsigned long flags;
1135
1136         spin_lock_irqsave(&lp->window_lock, flags);
1137         set_rx_mode(dev);
1138         spin_unlock_irqrestore(&lp->window_lock, flags);
1139 }
1140
1141 static int el3_close(struct net_device *dev)
1142 {
1143         unsigned int ioaddr = dev->base_addr;
1144         struct el3_private *lp = netdev_priv(dev);
1145         struct pcmcia_device *link = lp->p_dev;
1146
1147         dev_dbg(&link->dev, "%s: shutting down ethercard.\n", dev->name);
1148         
1149         if (pcmcia_dev_present(link)) {
1150                 unsigned long flags;
1151
1152                 /* Turn off statistics ASAP.  We update lp->stats below. */
1153                 outw(StatsDisable, ioaddr + EL3_CMD);
1154                 
1155                 /* Disable the receiver and transmitter. */
1156                 outw(RxDisable, ioaddr + EL3_CMD);
1157                 outw(TxDisable, ioaddr + EL3_CMD);
1158                 
1159                 /* Note: Switching to window 0 may disable the IRQ. */
1160                 EL3WINDOW(0);
1161                 spin_lock_irqsave(&lp->window_lock, flags);
1162                 update_stats(dev);
1163                 spin_unlock_irqrestore(&lp->window_lock, flags);
1164
1165                 /* force interrupts off */
1166                 outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1167         }
1168
1169         link->open--;
1170         netif_stop_queue(dev);
1171         del_timer_sync(&lp->media);
1172
1173         return 0;
1174 }
1175
1176 static struct pcmcia_device_id tc574_ids[] = {
1177         PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0574),
1178         PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x0556, "cis/3CCFEM556.cis"),
1179         PCMCIA_DEVICE_NULL,
1180 };
1181 MODULE_DEVICE_TABLE(pcmcia, tc574_ids);
1182
1183 static struct pcmcia_driver tc574_driver = {
1184         .owner          = THIS_MODULE,
1185         .drv            = {
1186                 .name   = "3c574_cs",
1187         },
1188         .probe          = tc574_probe,
1189         .remove         = tc574_detach,
1190         .id_table       = tc574_ids,
1191         .suspend        = tc574_suspend,
1192         .resume         = tc574_resume,
1193 };
1194
1195 static int __init init_tc574(void)
1196 {
1197         return pcmcia_register_driver(&tc574_driver);
1198 }
1199
1200 static void __exit exit_tc574(void)
1201 {
1202         pcmcia_unregister_driver(&tc574_driver);
1203 }
1204
1205 module_init(init_tc574);
1206 module_exit(exit_tc574);