1/*======================================================================
2
3    A PCMCIA ethernet driver for SMC91c92-based cards.
4
5    This driver supports Megahertz PCMCIA ethernet cards; and
6    Megahertz, Motorola, Ositech, and Psion Dacom ethernet/modem
7    multifunction cards.
8
9    Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
10
11    smc91c92_cs.c 1.122 2002/10/25 06:26:39
12
13    This driver contains code written by Donald Becker
14    (becker@scyld.com), Rowan Hughes (x-csrdh@jcu.edu.au),
15    David Hinds (dahinds@users.sourceforge.net), and Erik Stahlman
16    (erik@vt.edu).  Donald wrote the SMC 91c92 code using parts of
17    Erik's SMC 91c94 driver.  Rowan wrote a similar driver, and I've
18    incorporated some parts of his driver here.  I (Dave) wrote most
19    of the PCMCIA glue code, and the Ositech support code.  Kelly
20    Stephens (kstephen@holli.com) added support for the Motorola
21    Mariner, with help from Allen Brost.
22
23    This software may be used and distributed according to the terms of
24    the GNU General Public License, incorporated herein by reference.
25
26======================================================================*/
27
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/string.h>
33#include <linux/timer.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
36#include <linux/crc32.h>
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
39#include <linux/skbuff.h>
40#include <linux/if_arp.h>
41#include <linux/ioport.h>
42#include <linux/ethtool.h>
43#include <linux/mii.h>
44#include <linux/jiffies.h>
45
46#include <pcmcia/cs_types.h>
47#include <pcmcia/cs.h>
48#include <pcmcia/cistpl.h>
49#include <pcmcia/cisreg.h>
50#include <pcmcia/ciscode.h>
51#include <pcmcia/ds.h>
52#include <pcmcia/ss.h>
53
54#include <asm/io.h>
55#include <asm/system.h>
56#include <asm/uaccess.h>
57
58/* Ositech Seven of Diamonds firmware */
59#include "ositech.h"
60
61/*====================================================================*/
62
63static const char *if_names[] = { "auto", "10baseT", "10base2"};
64
65/* Module parameters */
66
67MODULE_DESCRIPTION("SMC 91c92 series PCMCIA ethernet driver");
68MODULE_LICENSE("GPL");
69
70#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
71
72/*
73  Transceiver/media type.
74   0 = auto
75   1 = 10baseT (and autoselect if #define AUTOSELECT),
76   2 = AUI/10base2,
77*/
78INT_MODULE_PARM(if_port, 0);
79
80#ifdef PCMCIA_DEBUG
81INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
82static const char *version =
83"smc91c92_cs.c 1.123 2006/11/09 Donald Becker, becker@scyld.com.\n";
84#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
85#else
86#define DEBUG(n, args...)
87#endif
88
89#define DRV_NAME	"smc91c92_cs"
90#define DRV_VERSION	"1.123"
91
92/*====================================================================*/
93
94/* Operational parameter that usually are not changed. */
95
96/* Time in jiffies before concluding Tx hung */
97#define TX_TIMEOUT		((400*HZ)/1000)
98
99/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
100#define INTR_WORK		4
101
102/* Times to check the check the chip before concluding that it doesn't
103   currently have room for another Tx packet. */
104#define MEMORY_WAIT_TIME       	8
105
106struct smc_private {
107	struct pcmcia_device	*p_dev;
108    spinlock_t			lock;
109    u_short			manfid;
110    u_short			cardid;
111    struct net_device_stats	stats;
112    dev_node_t			node;
113    struct sk_buff		*saved_skb;
114    int				packets_waiting;
115    void			__iomem *base;
116    u_short			cfg;
117    struct timer_list		media;
118    int				watchdog, tx_err;
119    u_short			media_status;
120    u_short			fast_poll;
121    u_short			link_status;
122    struct mii_if_info		mii_if;
123    int				duplex;
124    int				rx_ovrn;
125};
126
127struct smc_cfg_mem {
128    tuple_t tuple;
129    cisparse_t parse;
130    u_char buf[255];
131};
132
133/* Special definitions for Megahertz multifunction cards */
134#define MEGAHERTZ_ISR		0x0380
135
136/* Special function registers for Motorola Mariner */
137#define MOT_LAN			0x0000
138#define MOT_UART		0x0020
139#define MOT_EEPROM		0x20
140
141#define MOT_NORMAL \
142(COR_LEVEL_REQ | COR_FUNC_ENA | COR_ADDR_DECODE | COR_IREQ_ENA)
143
144/* Special function registers for Ositech cards */
145#define OSITECH_AUI_CTL		0x0c
146#define OSITECH_PWRDOWN		0x0d
147#define OSITECH_RESET		0x0e
148#define OSITECH_ISR		0x0f
149#define OSITECH_AUI_PWR		0x0c
150#define OSITECH_RESET_ISR	0x0e
151
152#define OSI_AUI_PWR		0x40
153#define OSI_LAN_PWRDOWN		0x02
154#define OSI_MODEM_PWRDOWN	0x01
155#define OSI_LAN_RESET		0x02
156#define OSI_MODEM_RESET		0x01
157
158/* Symbolic constants for the SMC91c9* series chips, from Erik Stahlman. */
159#define	BANK_SELECT		14		/* Window select register. */
160#define SMC_SELECT_BANK(x)  { outw(x, ioaddr + BANK_SELECT); }
161
162/* Bank 0 registers. */
163#define	TCR 		0	/* transmit control register */
164#define	 TCR_CLEAR	0	/* do NOTHING */
165#define  TCR_ENABLE	0x0001	/* if this is 1, we can transmit */
166#define	 TCR_PAD_EN	0x0080	/* pads short packets to 64 bytes */
167#define  TCR_MONCSN	0x0400  /* Monitor Carrier. */
168#define  TCR_FDUPLX	0x0800  /* Full duplex mode. */
169#define	 TCR_NORMAL TCR_ENABLE | TCR_PAD_EN
170
171#define EPH		2	/* Ethernet Protocol Handler report. */
172#define  EPH_TX_SUC	0x0001
173#define  EPH_SNGLCOL	0x0002
174#define  EPH_MULCOL	0x0004
175#define  EPH_LTX_MULT	0x0008
176#define  EPH_16COL	0x0010
177#define  EPH_SQET	0x0020
178#define  EPH_LTX_BRD	0x0040
179#define  EPH_TX_DEFR	0x0080
180#define  EPH_LAT_COL	0x0200
181#define  EPH_LOST_CAR	0x0400
182#define  EPH_EXC_DEF	0x0800
183#define  EPH_CTR_ROL	0x1000
184#define  EPH_RX_OVRN	0x2000
185#define  EPH_LINK_OK	0x4000
186#define  EPH_TX_UNRN	0x8000
187#define MEMINFO		8	/* Memory Information Register */
188#define MEMCFG		10	/* Memory Configuration Register */
189
190/* Bank 1 registers. */
191#define CONFIG			0
192#define  CFG_MII_SELECT		0x8000	/* 91C100 only */
193#define  CFG_NO_WAIT		0x1000
194#define  CFG_FULL_STEP		0x0400
195#define  CFG_SET_SQLCH		0x0200
196#define  CFG_AUI_SELECT	 	0x0100
197#define  CFG_16BIT		0x0080
198#define  CFG_DIS_LINK		0x0040
199#define  CFG_STATIC		0x0030
200#define  CFG_IRQ_SEL_1		0x0004
201#define  CFG_IRQ_SEL_0		0x0002
202#define BASE_ADDR		2
203#define	ADDR0			4
204#define	GENERAL			10
205#define	CONTROL			12
206#define  CTL_STORE		0x0001
207#define  CTL_RELOAD		0x0002
208#define  CTL_EE_SELECT		0x0004
209#define  CTL_TE_ENABLE		0x0020
210#define  CTL_CR_ENABLE		0x0040
211#define  CTL_LE_ENABLE		0x0080
212#define  CTL_AUTO_RELEASE	0x0800
213#define	 CTL_POWERDOWN		0x2000
214
215/* Bank 2 registers. */
216#define MMU_CMD		0
217#define	 MC_ALLOC	0x20  	/* or with number of 256 byte packets */
218#define	 MC_RESET	0x40
219#define  MC_RELEASE  	0x80  	/* remove and release the current rx packet */
220#define  MC_FREEPKT  	0xA0  	/* Release packet in PNR register */
221#define  MC_ENQUEUE	0xC0 	/* Enqueue the packet for transmit */
222#define	PNR_ARR		2
223#define FIFO_PORTS	4
224#define  FP_RXEMPTY	0x8000
225#define	POINTER		6
226#define  PTR_AUTO_INC	0x0040
227#define  PTR_READ	0x2000
228#define	 PTR_AUTOINC 	0x4000
229#define	 PTR_RCV	0x8000
230#define	DATA_1		8
231#define	INTERRUPT	12
232#define  IM_RCV_INT		0x1
233#define	 IM_TX_INT		0x2
234#define	 IM_TX_EMPTY_INT	0x4
235#define	 IM_ALLOC_INT		0x8
236#define	 IM_RX_OVRN_INT		0x10
237#define	 IM_EPH_INT		0x20
238
239#define	RCR		4
240enum RxCfg { RxAllMulti = 0x0004, RxPromisc = 0x0002,
241	     RxEnable = 0x0100, RxStripCRC = 0x0200};
242#define  RCR_SOFTRESET	0x8000 	/* resets the chip */
243#define	 RCR_STRIP_CRC	0x200	/* strips CRC */
244#define  RCR_ENABLE	0x100	/* IFF this is set, we can receive packets */
245#define  RCR_ALMUL	0x4 	/* receive all multicast packets */
246#define	 RCR_PROMISC	0x2	/* enable promiscuous mode */
247
248/* the normal settings for the RCR register : */
249#define	 RCR_NORMAL	(RCR_STRIP_CRC | RCR_ENABLE)
250#define  RCR_CLEAR	0x0		/* set it to a base state */
251#define	COUNTER		6
252
253/* BANK 3 -- not the same values as in smc9194! */
254#define	MULTICAST0	0
255#define	MULTICAST2	2
256#define	MULTICAST4	4
257#define	MULTICAST6	6
258#define MGMT    	8
259#define REVISION	0x0a
260
261/* Transmit status bits. */
262#define TS_SUCCESS 0x0001
263#define TS_16COL   0x0010
264#define TS_LATCOL  0x0200
265#define TS_LOSTCAR 0x0400
266
267/* Receive status bits. */
268#define RS_ALGNERR	0x8000
269#define RS_BADCRC	0x2000
270#define RS_ODDFRAME	0x1000
271#define RS_TOOLONG	0x0800
272#define RS_TOOSHORT	0x0400
273#define RS_MULTICAST	0x0001
274#define RS_ERRORS	(RS_ALGNERR | RS_BADCRC | RS_TOOLONG | RS_TOOSHORT)
275
276#define set_bits(v, p) outw(inw(p)|(v), (p))
277#define mask_bits(v, p) outw(inw(p)&(v), (p))
278
279/*====================================================================*/
280
281static void smc91c92_detach(struct pcmcia_device *p_dev);
282static int smc91c92_config(struct pcmcia_device *link);
283static void smc91c92_release(struct pcmcia_device *link);
284
285static int smc_open(struct net_device *dev);
286static int smc_close(struct net_device *dev);
287static int smc_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
288static void smc_tx_timeout(struct net_device *dev);
289static int smc_start_xmit(struct sk_buff *skb, struct net_device *dev);
290static irqreturn_t smc_interrupt(int irq, void *dev_id);
291static void smc_rx(struct net_device *dev);
292static struct net_device_stats *smc_get_stats(struct net_device *dev);
293static void set_rx_mode(struct net_device *dev);
294static int s9k_config(struct net_device *dev, struct ifmap *map);
295static void smc_set_xcvr(struct net_device *dev, int if_port);
296static void smc_reset(struct net_device *dev);
297static void media_check(u_long arg);
298static void mdio_sync(kio_addr_t addr);
299static int mdio_read(struct net_device *dev, int phy_id, int loc);
300static void mdio_write(struct net_device *dev, int phy_id, int loc, int value);
301static int smc_link_ok(struct net_device *dev);
302static const struct ethtool_ops ethtool_ops;
303
304/*======================================================================
305
306  smc91c92_attach() creates an "instance" of the driver, allocating
307  local data structures for one device.  The device is registered
308  with Card Services.
309
310======================================================================*/
311
312static int smc91c92_probe(struct pcmcia_device *link)
313{
314    struct smc_private *smc;
315    struct net_device *dev;
316
317    DEBUG(0, "smc91c92_attach()\n");
318
319    /* Create new ethernet device */
320    dev = alloc_etherdev(sizeof(struct smc_private));
321    if (!dev)
322	return -ENOMEM;
323    smc = netdev_priv(dev);
324    smc->p_dev = link;
325    link->priv = dev;
326
327    spin_lock_init(&smc->lock);
328    link->io.NumPorts1 = 16;
329    link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
330    link->io.IOAddrLines = 4;
331    link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
332    link->irq.IRQInfo1 = IRQ_LEVEL_ID;
333    link->irq.Handler = &smc_interrupt;
334    link->irq.Instance = dev;
335    link->conf.Attributes = CONF_ENABLE_IRQ;
336    link->conf.IntType = INT_MEMORY_AND_IO;
337
338    /* The SMC91c92-specific entries in the device structure. */
339    SET_MODULE_OWNER(dev);
340    dev->hard_start_xmit = &smc_start_xmit;
341    dev->get_stats = &smc_get_stats;
342    dev->set_config = &s9k_config;
343    dev->set_multicast_list = &set_rx_mode;
344    dev->open = &smc_open;
345    dev->stop = &smc_close;
346    dev->do_ioctl = &smc_ioctl;
347    SET_ETHTOOL_OPS(dev, &ethtool_ops);
348#ifdef HAVE_TX_TIMEOUT
349    dev->tx_timeout = smc_tx_timeout;
350    dev->watchdog_timeo = TX_TIMEOUT;
351#endif
352
353    smc->mii_if.dev = dev;
354    smc->mii_if.mdio_read = mdio_read;
355    smc->mii_if.mdio_write = mdio_write;
356    smc->mii_if.phy_id_mask = 0x1f;
357    smc->mii_if.reg_num_mask = 0x1f;
358
359    return smc91c92_config(link);
360} /* smc91c92_attach */
361
362/*======================================================================
363
364    This deletes a driver "instance".  The device is de-registered
365    with Card Services.  If it has been released, all local data
366    structures are freed.  Otherwise, the structures will be freed
367    when the device is released.
368
369======================================================================*/
370
371static void smc91c92_detach(struct pcmcia_device *link)
372{
373    struct net_device *dev = link->priv;
374
375    DEBUG(0, "smc91c92_detach(0x%p)\n", link);
376
377    if (link->dev_node)
378	unregister_netdev(dev);
379
380    smc91c92_release(link);
381
382    free_netdev(dev);
383} /* smc91c92_detach */
384
385/*====================================================================*/
386
387static int cvt_ascii_address(struct net_device *dev, char *s)
388{
389    int i, j, da, c;
390
391    if (strlen(s) != 12)
392	return -1;
393    for (i = 0; i < 6; i++) {
394	da = 0;
395	for (j = 0; j < 2; j++) {
396	    c = *s++;
397	    da <<= 4;
398	    da += ((c >= '0') && (c <= '9')) ?
399		(c - '0') : ((c & 0x0f) + 9);
400	}
401	dev->dev_addr[i] = da;
402    }
403    return 0;
404}
405
406/*====================================================================*/
407
408static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple,
409		cisparse_t *parse)
410{
411	int i;
412
413	if ((i = pcmcia_get_first_tuple(handle, tuple)) != CS_SUCCESS ||
414			(i = pcmcia_get_tuple_data(handle, tuple)) != CS_SUCCESS)
415		return i;
416	return pcmcia_parse_tuple(handle, tuple, parse);
417}
418
419static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple,
420		cisparse_t *parse)
421{
422	int i;
423
424	if ((i = pcmcia_get_next_tuple(handle, tuple)) != CS_SUCCESS ||
425			(i = pcmcia_get_tuple_data(handle, tuple)) != CS_SUCCESS)
426		return i;
427	return pcmcia_parse_tuple(handle, tuple, parse);
428}
429
430/*======================================================================
431
432    Configuration stuff for Megahertz cards
433
434    mhz_3288_power() is used to power up a 3288's ethernet chip.
435    mhz_mfc_config() handles socket setup for multifunction (1144
436    and 3288) cards.  mhz_setup() gets a card's hardware ethernet
437    address.
438
439======================================================================*/
440
441static int mhz_3288_power(struct pcmcia_device *link)
442{
443    struct net_device *dev = link->priv;
444    struct smc_private *smc = netdev_priv(dev);
445    u_char tmp;
446
447    /* Read the ISR twice... */
448    readb(smc->base+MEGAHERTZ_ISR);
449    udelay(5);
450    readb(smc->base+MEGAHERTZ_ISR);
451
452    /* Pause 200ms... */
453    mdelay(200);
454
455    /* Now read and write the COR... */
456    tmp = readb(smc->base + link->conf.ConfigBase + CISREG_COR);
457    udelay(5);
458    writeb(tmp, smc->base + link->conf.ConfigBase + CISREG_COR);
459
460    return 0;
461}
462
463static int mhz_mfc_config(struct pcmcia_device *link)
464{
465    struct net_device *dev = link->priv;
466    struct smc_private *smc = netdev_priv(dev);
467    struct smc_cfg_mem *cfg_mem;
468    tuple_t *tuple;
469    cisparse_t *parse;
470    cistpl_cftable_entry_t *cf;
471    u_char *buf;
472    win_req_t req;
473    memreq_t mem;
474    int i, k;
475
476    cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
477    if (!cfg_mem)
478        return CS_OUT_OF_RESOURCE;
479
480    tuple = &cfg_mem->tuple;
481    parse = &cfg_mem->parse;
482    cf = &parse->cftable_entry;
483    buf = cfg_mem->buf;
484
485    link->conf.Attributes |= CONF_ENABLE_SPKR;
486    link->conf.Status = CCSR_AUDIO_ENA;
487    link->irq.Attributes =
488	IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED|IRQ_HANDLE_PRESENT;
489    link->io.IOAddrLines = 16;
490    link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
491    link->io.NumPorts2 = 8;
492
493    tuple->Attributes = tuple->TupleOffset = 0;
494    tuple->TupleData = (cisdata_t *)buf;
495    tuple->TupleDataMax = 255;
496    tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
497
498    i = first_tuple(link, tuple, parse);
499    /* The Megahertz combo cards have modem-like CIS entries, so
500       we have to explicitly try a bunch of port combinations. */
501    while (i == CS_SUCCESS) {
502	link->conf.ConfigIndex = cf->index;
503	link->io.BasePort2 = cf->io.win[0].base;
504	for (k = 0; k < 0x400; k += 0x10) {
505	    if (k & 0x80) continue;
506	    link->io.BasePort1 = k ^ 0x300;
507	    i = pcmcia_request_io(link, &link->io);
508	    if (i == CS_SUCCESS) break;
509	}
510	if (i == CS_SUCCESS) break;
511	i = next_tuple(link, tuple, parse);
512    }
513    if (i != CS_SUCCESS)
514	goto free_cfg_mem;
515    dev->base_addr = link->io.BasePort1;
516
517    /* Allocate a memory window, for accessing the ISR */
518    req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
519    req.Base = req.Size = 0;
520    req.AccessSpeed = 0;
521    i = pcmcia_request_window(&link, &req, &link->win);
522    if (i != CS_SUCCESS)
523	goto free_cfg_mem;
524    smc->base = ioremap(req.Base, req.Size);
525    mem.CardOffset = mem.Page = 0;
526    if (smc->manfid == MANFID_MOTOROLA)
527	mem.CardOffset = link->conf.ConfigBase;
528    i = pcmcia_map_mem_page(link->win, &mem);
529
530    if ((i == CS_SUCCESS)
531	&& (smc->manfid == MANFID_MEGAHERTZ)
532	&& (smc->cardid == PRODID_MEGAHERTZ_EM3288))
533	mhz_3288_power(link);
534
535free_cfg_mem:
536    kfree(cfg_mem);
537    return i;
538}
539
540static int mhz_setup(struct pcmcia_device *link)
541{
542    struct net_device *dev = link->priv;
543    struct smc_cfg_mem *cfg_mem;
544    tuple_t *tuple;
545    cisparse_t *parse;
546    u_char *buf, *station_addr;
547    int rc;
548
549    cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
550    if (!cfg_mem)
551	return -1;
552
553    tuple = &cfg_mem->tuple;
554    parse = &cfg_mem->parse;
555    buf = cfg_mem->buf;
556
557    tuple->Attributes = tuple->TupleOffset = 0;
558    tuple->TupleData = (cisdata_t *)buf;
559    tuple->TupleDataMax = 255;
560
561    /* Read the station address from the CIS.  It is stored as the last
562       (fourth) string in the Version 1 Version/ID tuple. */
563    if (link->prod_id[3]) {
564	station_addr = link->prod_id[3];
565	if (cvt_ascii_address(dev, station_addr) == 0) {
566		rc = 0;
567		goto free_cfg_mem;
568	}
569    }
570
571    /* Another possibility: for the EM3288, in a special tuple */
572    tuple->DesiredTuple = 0x81;
573    if (pcmcia_get_first_tuple(link, tuple) != CS_SUCCESS) {
574	rc = -1;
575	goto free_cfg_mem;
576    }
577    if (pcmcia_get_tuple_data(link, tuple) != CS_SUCCESS) {
578	rc = -1;
579	goto free_cfg_mem;
580    }
581    buf[12] = '\0';
582    if (cvt_ascii_address(dev, buf) == 0) {
583	rc = 0;
584	goto free_cfg_mem;
585   }
586    rc = -1;
587free_cfg_mem:
588   kfree(cfg_mem);
589   return rc;
590}
591
592/*======================================================================
593
594    Configuration stuff for the Motorola Mariner
595
596    mot_config() writes directly to the Mariner configuration
597    registers because the CIS is just bogus.
598
599======================================================================*/
600
601static void mot_config(struct pcmcia_device *link)
602{
603    struct net_device *dev = link->priv;
604    struct smc_private *smc = netdev_priv(dev);
605    kio_addr_t ioaddr = dev->base_addr;
606    kio_addr_t iouart = link->io.BasePort2;
607
608    /* Set UART base address and force map with COR bit 1 */
609    writeb(iouart & 0xff,        smc->base + MOT_UART + CISREG_IOBASE_0);
610    writeb((iouart >> 8) & 0xff, smc->base + MOT_UART + CISREG_IOBASE_1);
611    writeb(MOT_NORMAL,           smc->base + MOT_UART + CISREG_COR);
612
613    /* Set SMC base address and force map with COR bit 1 */
614    writeb(ioaddr & 0xff,        smc->base + MOT_LAN + CISREG_IOBASE_0);
615    writeb((ioaddr >> 8) & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_1);
616    writeb(MOT_NORMAL,           smc->base + MOT_LAN + CISREG_COR);
617
618    /* Wait for things to settle down */
619    mdelay(100);
620}
621
622static int mot_setup(struct pcmcia_device *link)
623{
624    struct net_device *dev = link->priv;
625    kio_addr_t ioaddr = dev->base_addr;
626    int i, wait, loop;
627    u_int addr;
628
629    /* Read Ethernet address from Serial EEPROM */
630
631    for (i = 0; i < 3; i++) {
632	SMC_SELECT_BANK(2);
633	outw(MOT_EEPROM + i, ioaddr + POINTER);
634	SMC_SELECT_BANK(1);
635	outw((CTL_RELOAD | CTL_EE_SELECT), ioaddr + CONTROL);
636
637	for (loop = wait = 0; loop < 200; loop++) {
638	    udelay(10);
639	    wait = ((CTL_RELOAD | CTL_STORE) & inw(ioaddr + CONTROL));
640	    if (wait == 0) break;
641	}
642
643	if (wait)
644	    return -1;
645
646	addr = inw(ioaddr + GENERAL);
647	dev->dev_addr[2*i]   = addr & 0xff;
648	dev->dev_addr[2*i+1] = (addr >> 8) & 0xff;
649    }
650
651    return 0;
652}
653
654/*====================================================================*/
655
656static int smc_config(struct pcmcia_device *link)
657{
658    struct net_device *dev = link->priv;
659    struct smc_cfg_mem *cfg_mem;
660    tuple_t *tuple;
661    cisparse_t *parse;
662    cistpl_cftable_entry_t *cf;
663    u_char *buf;
664    int i;
665
666    cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
667    if (!cfg_mem)
668	return CS_OUT_OF_RESOURCE;
669
670    tuple = &cfg_mem->tuple;
671    parse = &cfg_mem->parse;
672    cf = &parse->cftable_entry;
673    buf = cfg_mem->buf;
674
675    tuple->Attributes = tuple->TupleOffset = 0;
676    tuple->TupleData = (cisdata_t *)buf;
677    tuple->TupleDataMax = 255;
678    tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
679
680    link->io.NumPorts1 = 16;
681    i = first_tuple(link, tuple, parse);
682    while (i != CS_NO_MORE_ITEMS) {
683	if (i == CS_SUCCESS) {
684	    link->conf.ConfigIndex = cf->index;
685	    link->io.BasePort1 = cf->io.win[0].base;
686	    link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
687	    i = pcmcia_request_io(link, &link->io);
688	    if (i == CS_SUCCESS) break;
689	}
690	i = next_tuple(link, tuple, parse);
691    }
692    if (i == CS_SUCCESS)
693	dev->base_addr = link->io.BasePort1;
694
695    kfree(cfg_mem);
696    return i;
697}
698
699static int smc_setup(struct pcmcia_device *link)
700{
701    struct net_device *dev = link->priv;
702    struct smc_cfg_mem *cfg_mem;
703    tuple_t *tuple;
704    cisparse_t *parse;
705    cistpl_lan_node_id_t *node_id;
706    u_char *buf, *station_addr;
707    int i, rc;
708
709    cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
710    if (!cfg_mem)
711	return CS_OUT_OF_RESOURCE;
712
713    tuple = &cfg_mem->tuple;
714    parse = &cfg_mem->parse;
715    buf = cfg_mem->buf;
716
717    tuple->Attributes = tuple->TupleOffset = 0;
718    tuple->TupleData = (cisdata_t *)buf;
719    tuple->TupleDataMax = 255;
720
721    /* Check for a LAN function extension tuple */
722    tuple->DesiredTuple = CISTPL_FUNCE;
723    i = first_tuple(link, tuple, parse);
724    while (i == CS_SUCCESS) {
725	if (parse->funce.type == CISTPL_FUNCE_LAN_NODE_ID)
726	    break;
727	i = next_tuple(link, tuple, parse);
728    }
729    if (i == CS_SUCCESS) {
730	node_id = (cistpl_lan_node_id_t *)parse->funce.data;
731	if (node_id->nb == 6) {
732	    for (i = 0; i < 6; i++)
733		dev->dev_addr[i] = node_id->id[i];
734	    rc = 0;
735	    goto free_cfg_mem;
736	}
737    }
738    /* Try the third string in the Version 1 Version/ID tuple. */
739    if (link->prod_id[2]) {
740	station_addr = link->prod_id[2];
741	if (cvt_ascii_address(dev, station_addr) == 0) {
742		rc = 0;
743		goto free_cfg_mem;
744	}
745    }
746
747    rc = -1;
748free_cfg_mem:
749    kfree(cfg_mem);
750    return rc;
751}
752
753/*====================================================================*/
754
755static int osi_config(struct pcmcia_device *link)
756{
757    struct net_device *dev = link->priv;
758    static const kio_addr_t com[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
759    int i, j;
760
761    link->conf.Attributes |= CONF_ENABLE_SPKR;
762    link->conf.Status = CCSR_AUDIO_ENA;
763    link->irq.Attributes =
764	IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED|IRQ_HANDLE_PRESENT;
765    link->io.NumPorts1 = 64;
766    link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
767    link->io.NumPorts2 = 8;
768    link->io.IOAddrLines = 16;
769
770    /* Enable Hard Decode, LAN, Modem */
771    link->conf.ConfigIndex = 0x23;
772
773    for (i = j = 0; j < 4; j++) {
774	link->io.BasePort2 = com[j];
775	i = pcmcia_request_io(link, &link->io);
776	if (i == CS_SUCCESS) break;
777    }
778    if (i != CS_SUCCESS) {
779	/* Fallback: turn off hard decode */
780	link->conf.ConfigIndex = 0x03;
781	link->io.NumPorts2 = 0;
782	i = pcmcia_request_io(link, &link->io);
783    }
784    dev->base_addr = link->io.BasePort1 + 0x10;
785    return i;
786}
787
788static int osi_setup(struct pcmcia_device *link, u_short manfid, u_short cardid)
789{
790    struct net_device *dev = link->priv;
791    struct smc_cfg_mem *cfg_mem;
792    tuple_t *tuple;
793    u_char *buf;
794    int i, rc;
795
796    cfg_mem = kmalloc(sizeof(struct smc_cfg_mem), GFP_KERNEL);
797    if (!cfg_mem)
798        return -1;
799
800    tuple = &cfg_mem->tuple;
801    buf = cfg_mem->buf;
802
803    tuple->Attributes = TUPLE_RETURN_COMMON;
804    tuple->TupleData = (cisdata_t *)buf;
805    tuple->TupleDataMax = 255;
806    tuple->TupleOffset = 0;
807
808    /* Read the station address from tuple 0x90, subtuple 0x04 */
809    tuple->DesiredTuple = 0x90;
810    i = pcmcia_get_first_tuple(link, tuple);
811    while (i == CS_SUCCESS) {
812	i = pcmcia_get_tuple_data(link, tuple);
813	if ((i != CS_SUCCESS) || (buf[0] == 0x04))
814	    break;
815	i = pcmcia_get_next_tuple(link, tuple);
816    }
817    if (i != CS_SUCCESS) {
818	rc = -1;
819	goto free_cfg_mem;
820    }
821    for (i = 0; i < 6; i++)
822	dev->dev_addr[i] = buf[i+2];
823
824    if (((manfid == MANFID_OSITECH) &&
825	 (cardid == PRODID_OSITECH_SEVEN)) ||
826	((manfid == MANFID_PSION) &&
827	 (cardid == PRODID_PSION_NET100))) {
828	/* Download the Seven of Diamonds firmware */
829	for (i = 0; i < sizeof(__Xilinx7OD); i++) {
830	    outb(__Xilinx7OD[i], link->io.BasePort1+2);
831	    udelay(50);
832	}
833    } else if (manfid == MANFID_OSITECH) {
834	/* Make sure both functions are powered up */
835	set_bits(0x300, link->io.BasePort1 + OSITECH_AUI_PWR);
836	/* Now, turn on the interrupt for both card functions */
837	set_bits(0x300, link->io.BasePort1 + OSITECH_RESET_ISR);
838	DEBUG(2, "AUI/PWR: %4.4x RESET/ISR: %4.4x\n",
839	      inw(link->io.BasePort1 + OSITECH_AUI_PWR),
840	      inw(link->io.BasePort1 + OSITECH_RESET_ISR));
841    }
842    rc = 0;
843free_cfg_mem:
844   kfree(cfg_mem);
845   return rc;
846}
847
848static int smc91c92_suspend(struct pcmcia_device *link)
849{
850	struct net_device *dev = link->priv;
851
852	if (link->open)
853		netif_device_detach(dev);
854
855	return 0;
856}
857
858static int smc91c92_resume(struct pcmcia_device *link)
859{
860	struct net_device *dev = link->priv;
861	struct smc_private *smc = netdev_priv(dev);
862	int i;
863
864	if ((smc->manfid == MANFID_MEGAHERTZ) &&
865	    (smc->cardid == PRODID_MEGAHERTZ_EM3288))
866		mhz_3288_power(link);
867	if (smc->manfid == MANFID_MOTOROLA)
868		mot_config(link);
869	if ((smc->manfid == MANFID_OSITECH) &&
870	    (smc->cardid != PRODID_OSITECH_SEVEN)) {
871		/* Power up the card and enable interrupts */
872		set_bits(0x0300, dev->base_addr-0x10+OSITECH_AUI_PWR);
873		set_bits(0x0300, dev->base_addr-0x10+OSITECH_RESET_ISR);
874	}
875	if (((smc->manfid == MANFID_OSITECH) &&
876	     (smc->cardid == PRODID_OSITECH_SEVEN)) ||
877	    ((smc->manfid == MANFID_PSION) &&
878	     (smc->cardid == PRODID_PSION_NET100))) {
879		/* Download the Seven of Diamonds firmware */
880		for (i = 0; i < sizeof(__Xilinx7OD); i++) {
881			outb(__Xilinx7OD[i], link->io.BasePort1+2);
882			udelay(50);
883		}
884	}
885	if (link->open) {
886		smc_reset(dev);
887		netif_device_attach(dev);
888	}
889
890	return 0;
891}
892
893
894/*======================================================================
895
896    This verifies that the chip is some SMC91cXX variant, and returns
897    the revision code if successful.  Otherwise, it returns -ENODEV.
898
899======================================================================*/
900
901static int check_sig(struct pcmcia_device *link)
902{
903    struct net_device *dev = link->priv;
904    kio_addr_t ioaddr = dev->base_addr;
905    int width;
906    u_short s;
907
908    SMC_SELECT_BANK(1);
909    if (inw(ioaddr + BANK_SELECT) >> 8 != 0x33) {
910	/* Try powering up the chip */
911	outw(0, ioaddr + CONTROL);
912	mdelay(55);
913    }
914
915    /* Try setting bus width */
916    width = (link->io.Attributes1 == IO_DATA_PATH_WIDTH_AUTO);
917    s = inb(ioaddr + CONFIG);
918    if (width)
919	s |= CFG_16BIT;
920    else
921	s &= ~CFG_16BIT;
922    outb(s, ioaddr + CONFIG);
923
924    /* Check Base Address Register to make sure bus width is OK */
925    s = inw(ioaddr + BASE_ADDR);
926    if ((inw(ioaddr + BANK_SELECT) >> 8 == 0x33) &&
927	((s >> 8) != (s & 0xff))) {
928	SMC_SELECT_BANK(3);
929	s = inw(ioaddr + REVISION);
930	return (s & 0xff);
931    }
932
933    if (width) {
934	    modconf_t mod = {
935		    .Attributes = CONF_IO_CHANGE_WIDTH,
936	    };
937	    printk(KERN_INFO "smc91c92_cs: using 8-bit IO window.\n");
938
939	    smc91c92_suspend(link);
940	    pcmcia_modify_configuration(link, &mod);
941	    smc91c92_resume(link);
942	    return check_sig(link);
943    }
944    return -ENODEV;
945}
946
947/*======================================================================
948
949    smc91c92_config() is scheduled to run after a CARD_INSERTION event
950    is received, to configure the PCMCIA socket, and to make the
951    ethernet device available to the system.
952
953======================================================================*/
954
955#define CS_EXIT_TEST(ret, svc, label) \
956if (ret != CS_SUCCESS) { cs_error(link, svc, ret); goto label; }
957
958static int smc91c92_config(struct pcmcia_device *link)
959{
960    struct net_device *dev = link->priv;
961    struct smc_private *smc = netdev_priv(dev);
962    char *name;
963    int i, j, rev;
964    kio_addr_t ioaddr;
965    u_long mir;
966
967    DEBUG(0, "smc91c92_config(0x%p)\n", link);
968
969    smc->manfid = link->manf_id;
970    smc->cardid = link->card_id;
971
972    if ((smc->manfid == MANFID_OSITECH) &&
973	(smc->cardid != PRODID_OSITECH_SEVEN)) {
974	i = osi_config(link);
975    } else if ((smc->manfid == MANFID_MOTOROLA) ||
976	       ((smc->manfid == MANFID_MEGAHERTZ) &&
977		((smc->cardid == PRODID_MEGAHERTZ_VARIOUS) ||
978		 (smc->cardid == PRODID_MEGAHERTZ_EM3288)))) {
979	i = mhz_mfc_config(link);
980    } else {
981	i = smc_config(link);
982    }
983    CS_EXIT_TEST(i, RequestIO, config_failed);
984
985    i = pcmcia_request_irq(link, &link->irq);
986    CS_EXIT_TEST(i, RequestIRQ, config_failed);
987    i = pcmcia_request_configuration(link, &link->conf);
988    CS_EXIT_TEST(i, RequestConfiguration, config_failed);
989
990    if (smc->manfid == MANFID_MOTOROLA)
991	mot_config(link);
992
993    dev->irq = link->irq.AssignedIRQ;
994
995    if ((if_port >= 0) && (if_port <= 2))
996	dev->if_port = if_port;
997    else
998	printk(KERN_NOTICE "smc91c92_cs: invalid if_port requested\n");
999
1000    switch (smc->manfid) {
1001    case MANFID_OSITECH:
1002    case MANFID_PSION:
1003	i = osi_setup(link, smc->manfid, smc->cardid); break;
1004    case MANFID_SMC:
1005    case MANFID_NEW_MEDIA:
1006	i = smc_setup(link); break;
1007    case 0x128: /* For broken Megahertz cards */
1008    case MANFID_MEGAHERTZ:
1009	i = mhz_setup(link); break;
1010    case MANFID_MOTOROLA:
1011    default: /* get the hw address from EEPROM */
1012	i = mot_setup(link); break;
1013    }
1014
1015    if (i != 0) {
1016	printk(KERN_NOTICE "smc91c92_cs: Unable to find hardware address.\n");
1017	goto config_undo;
1018    }
1019
1020    smc->duplex = 0;
1021    smc->rx_ovrn = 0;
1022
1023    rev = check_sig(link);
1024    name = "???";
1025    if (rev > 0)
1026	switch (rev >> 4) {
1027	case 3: name = "92"; break;
1028	case 4: name = ((rev & 15) >= 6) ? "96" : "94"; break;
1029	case 5: name = "95"; break;
1030	case 7: name = "100"; break;
1031	case 8: name = "100-FD"; break;
1032	case 9: name = "110"; break;
1033	}
1034
1035    ioaddr = dev->base_addr;
1036    if (rev > 0) {
1037	u_long mcr;
1038	SMC_SELECT_BANK(0);
1039	mir = inw(ioaddr + MEMINFO) & 0xff;
1040	if (mir == 0xff) mir++;
1041	/* Get scale factor for memory size */
1042	mcr = ((rev >> 4) > 3) ? inw(ioaddr + MEMCFG) : 0x0200;
1043	mir *= 128 * (1<<((mcr >> 9) & 7));
1044	SMC_SELECT_BANK(1);
1045	smc->cfg = inw(ioaddr + CONFIG) & ~CFG_AUI_SELECT;
1046	smc->cfg |= CFG_NO_WAIT | CFG_16BIT | CFG_STATIC;
1047	if (smc->manfid == MANFID_OSITECH)
1048	    smc->cfg |= CFG_IRQ_SEL_1 | CFG_IRQ_SEL_0;
1049	if ((rev >> 4) >= 7)
1050	    smc->cfg |= CFG_MII_SELECT;
1051    } else
1052	mir = 0;
1053
1054    if (smc->cfg & CFG_MII_SELECT) {
1055	SMC_SELECT_BANK(3);
1056
1057	for (i = 0; i < 32; i++) {
1058	    j = mdio_read(dev, i, 1);
1059	    if ((j != 0) && (j != 0xffff)) break;
1060	}
1061	smc->mii_if.phy_id = (i < 32) ? i : -1;
1062
1063	SMC_SELECT_BANK(0);
1064    }
1065
1066    link->dev_node = &smc->node;
1067    SET_NETDEV_DEV(dev, &handle_to_dev(link));
1068
1069    if (register_netdev(dev) != 0) {
1070	printk(KERN_ERR "smc91c92_cs: register_netdev() failed\n");
1071	link->dev_node = NULL;
1072	goto config_undo;
1073    }
1074
1075    strcpy(smc->node.dev_name, dev->name);
1076
1077    printk(KERN_INFO "%s: smc91c%s rev %d: io %#3lx, irq %d, "
1078	   "hw_addr ", dev->name, name, (rev & 0x0f), dev->base_addr,
1079	   dev->irq);
1080    for (i = 0; i < 6; i++)
1081	printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
1082
1083    if (rev > 0) {
1084	if (mir & 0x3ff)
1085	    printk(KERN_INFO "  %lu byte", mir);
1086	else
1087	    printk(KERN_INFO "  %lu kb", mir>>10);
1088	printk(" buffer, %s xcvr\n", (smc->cfg & CFG_MII_SELECT) ?
1089	       "MII" : if_names[dev->if_port]);
1090    }
1091
1092    if (smc->cfg & CFG_MII_SELECT) {
1093	if (smc->mii_if.phy_id != -1) {
1094	    DEBUG(0, "  MII transceiver at index %d, status %x.\n",
1095		  smc->mii_if.phy_id, j);
1096	} else {
1097    	    printk(KERN_NOTICE "  No MII transceivers found!\n");
1098	}
1099    }
1100    return 0;
1101
1102config_undo:
1103    unregister_netdev(dev);
1104config_failed:			/* CS_EXIT_TEST() calls jump to here... */
1105    smc91c92_release(link);
1106    return -ENODEV;
1107} /* smc91c92_config */
1108
1109/*======================================================================
1110
1111    After a card is removed, smc91c92_release() will unregister the net
1112    device, and release the PCMCIA configuration.  If the device is
1113    still open, this will be postponed until it is closed.
1114
1115======================================================================*/
1116
1117static void smc91c92_release(struct pcmcia_device *link)
1118{
1119	DEBUG(0, "smc91c92_release(0x%p)\n", link);
1120	if (link->win) {
1121		struct net_device *dev = link->priv;
1122		struct smc_private *smc = netdev_priv(dev);
1123		iounmap(smc->base);
1124	}
1125	pcmcia_disable_device(link);
1126}
1127
1128/*======================================================================
1129
1130    MII interface support for SMC91cXX based cards
1131======================================================================*/
1132
1133#define MDIO_SHIFT_CLK		0x04
1134#define MDIO_DATA_OUT		0x01
1135#define MDIO_DIR_WRITE		0x08
1136#define MDIO_DATA_WRITE0	(MDIO_DIR_WRITE)
1137#define MDIO_DATA_WRITE1	(MDIO_DIR_WRITE | MDIO_DATA_OUT)
1138#define MDIO_DATA_READ		0x02
1139
1140static void mdio_sync(kio_addr_t addr)
1141{
1142    int bits;
1143    for (bits = 0; bits < 32; bits++) {
1144	outb(MDIO_DATA_WRITE1, addr);
1145	outb(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
1146    }
1147}
1148
1149static int mdio_read(struct net_device *dev, int phy_id, int loc)
1150{
1151    kio_addr_t addr = dev->base_addr + MGMT;
1152    u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
1153    int i, retval = 0;
1154
1155    mdio_sync(addr);
1156    for (i = 13; i >= 0; i--) {
1157	int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
1158	outb(dat, addr);
1159	outb(dat | MDIO_SHIFT_CLK, addr);
1160    }
1161    for (i = 19; i > 0; i--) {
1162	outb(0, addr);
1163	retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
1164	outb(MDIO_SHIFT_CLK, addr);
1165    }
1166    return (retval>>1) & 0xffff;
1167}
1168
1169static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
1170{
1171    kio_addr_t addr = dev->base_addr + MGMT;
1172    u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
1173    int i;
1174
1175    mdio_sync(addr);
1176    for (i = 31; i >= 0; i--) {
1177	int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
1178	outb(dat, addr);
1179	outb(dat | MDIO_SHIFT_CLK, addr);
1180    }
1181    for (i = 1; i >= 0; i--) {
1182	outb(0, addr);
1183	outb(MDIO_SHIFT_CLK, addr);
1184    }
1185}
1186
1187/*======================================================================
1188
1189    The driver core code, most of which should be common with a
1190    non-PCMCIA implementation.
1191
1192======================================================================*/
1193
1194#ifdef PCMCIA_DEBUG
1195static void smc_dump(struct net_device *dev)
1196{
1197    kio_addr_t ioaddr = dev->base_addr;
1198    u_short i, w, save;
1199    save = inw(ioaddr + BANK_SELECT);
1200    for (w = 0; w < 4; w++) {
1201	SMC_SELECT_BANK(w);
1202	printk(KERN_DEBUG "bank %d: ", w);
1203	for (i = 0; i < 14; i += 2)
1204	    printk(" %04x", inw(ioaddr + i));
1205	printk("\n");
1206    }
1207    outw(save, ioaddr + BANK_SELECT);
1208}
1209#endif
1210
1211static int smc_open(struct net_device *dev)
1212{
1213    struct smc_private *smc = netdev_priv(dev);
1214    struct pcmcia_device *link = smc->p_dev;
1215
1216#ifdef PCMCIA_DEBUG
1217    DEBUG(0, "%s: smc_open(%p), ID/Window %4.4x.\n",
1218	  dev->name, dev, inw(dev->base_addr + BANK_SELECT));
1219    if (pc_debug > 1) smc_dump(dev);
1220#endif
1221
1222    /* Check that the PCMCIA card is still here. */
1223    if (!pcmcia_dev_present(link))
1224	return -ENODEV;
1225    /* Physical device present signature. */
1226    if (check_sig(link) < 0) {
1227	printk("smc91c92_cs: Yikes!  Bad chip signature!\n");
1228	return -ENODEV;
1229    }
1230    link->open++;
1231
1232    netif_start_queue(dev);
1233    smc->saved_skb = NULL;
1234    smc->packets_waiting = 0;
1235
1236    smc_reset(dev);
1237    init_timer(&smc->media);
1238    smc->media.function = &media_check;
1239    smc->media.data = (u_long) dev;
1240    smc->media.expires = jiffies + HZ;
1241    add_timer(&smc->media);
1242
1243    return 0;
1244} /* smc_open */
1245
1246/*====================================================================*/
1247
1248static int smc_close(struct net_device *dev)
1249{
1250    struct smc_private *smc = netdev_priv(dev);
1251    struct pcmcia_device *link = smc->p_dev;
1252    kio_addr_t ioaddr = dev->base_addr;
1253
1254    DEBUG(0, "%s: smc_close(), status %4.4x.\n",
1255	  dev->name, inw(ioaddr + BANK_SELECT));
1256
1257    netif_stop_queue(dev);
1258
1259    /* Shut off all interrupts, and turn off the Tx and Rx sections.
1260       Don't bother to check for chip present. */
1261    SMC_SELECT_BANK(2);	/* Nominally paranoia, but do no assume... */
1262    outw(0, ioaddr + INTERRUPT);
1263    SMC_SELECT_BANK(0);
1264    mask_bits(0xff00, ioaddr + RCR);
1265    mask_bits(0xff00, ioaddr + TCR);
1266
1267    /* Put the chip into power-down mode. */
1268    SMC_SELECT_BANK(1);
1269    outw(CTL_POWERDOWN, ioaddr + CONTROL );
1270
1271    link->open--;
1272    del_timer_sync(&smc->media);
1273
1274    return 0;
1275} /* smc_close */
1276
1277/*======================================================================
1278
1279   Transfer a packet to the hardware and trigger the packet send.
1280   This may be called at either from either the Tx queue code
1281   or the interrupt handler.
1282
1283======================================================================*/
1284
1285static void smc_hardware_send_packet(struct net_device * dev)
1286{
1287    struct smc_private *smc = netdev_priv(dev);
1288    struct sk_buff *skb = smc->saved_skb;
1289    kio_addr_t ioaddr = dev->base_addr;
1290    u_char packet_no;
1291
1292    if (!skb) {
1293	printk(KERN_ERR "%s: In XMIT with no packet to send.\n", dev->name);
1294	return;
1295    }
1296
1297    /* There should be a packet slot waiting. */
1298    packet_no = inw(ioaddr + PNR_ARR) >> 8;
1299    if (packet_no & 0x80) {
1300	/* If not, there is a hardware problem!  Likely an ejected card. */
1301	printk(KERN_WARNING "%s: 91c92 hardware Tx buffer allocation"
1302	       " failed, status %#2.2x.\n", dev->name, packet_no);
1303	dev_kfree_skb_irq(skb);
1304	smc->saved_skb = NULL;
1305	netif_start_queue(dev);
1306	return;
1307    }
1308
1309    smc->stats.tx_bytes += skb->len;
1310    /* The card should use the just-allocated buffer. */
1311    outw(packet_no, ioaddr + PNR_ARR);
1312    /* point to the beginning of the packet */
1313    outw(PTR_AUTOINC , ioaddr + POINTER);
1314
1315    /* Send the packet length (+6 for status, length and ctl byte)
1316       and the status word (set to zeros). */
1317    {
1318	u_char *buf = skb->data;
1319	u_int length = skb->len; /* The chip will pad to ethernet min. */
1320
1321	DEBUG(2, "%s: Trying to xmit packet of length %d.\n",
1322	      dev->name, length);
1323
1324	/* send the packet length: +6 for status word, length, and ctl */
1325	outw(0, ioaddr + DATA_1);
1326	outw(length + 6, ioaddr + DATA_1);
1327	outsw(ioaddr + DATA_1, buf, length >> 1);
1328
1329	/* The odd last byte, if there is one, goes in the control word. */
1330	outw((length & 1) ? 0x2000 | buf[length-1] : 0, ioaddr + DATA_1);
1331    }
1332
1333    /* Enable the Tx interrupts, both Tx (TxErr) and TxEmpty. */
1334    outw(((IM_TX_INT|IM_TX_EMPTY_INT)<<8) |
1335	 (inw(ioaddr + INTERRUPT) & 0xff00),
1336	 ioaddr + INTERRUPT);
1337
1338    /* The chip does the rest of the work. */
1339    outw(MC_ENQUEUE , ioaddr + MMU_CMD);
1340
1341    smc->saved_skb = NULL;
1342    dev_kfree_skb_irq(skb);
1343    dev->trans_start = jiffies;
1344    netif_start_queue(dev);
1345    return;
1346}
1347
1348/*====================================================================*/
1349
1350static void smc_tx_timeout(struct net_device *dev)
1351{
1352    struct smc_private *smc = netdev_priv(dev);
1353    kio_addr_t ioaddr = dev->base_addr;
1354
1355    printk(KERN_NOTICE "%s: SMC91c92 transmit timed out, "
1356	   "Tx_status %2.2x status %4.4x.\n",
1357	   dev->name, inw(ioaddr)&0xff, inw(ioaddr + 2));
1358    smc->stats.tx_errors++;
1359    smc_reset(dev);
1360    dev->trans_start = jiffies;
1361    smc->saved_skb = NULL;
1362    netif_wake_queue(dev);
1363}
1364
1365static int smc_start_xmit(struct sk_buff *skb, struct net_device *dev)
1366{
1367    struct smc_private *smc = netdev_priv(dev);
1368    kio_addr_t ioaddr = dev->base_addr;
1369    u_short num_pages;
1370    short time_out, ir;
1371
1372    netif_stop_queue(dev);
1373
1374    DEBUG(2, "%s: smc_start_xmit(length = %d) called,"
1375	  " status %4.4x.\n", dev->name, skb->len, inw(ioaddr + 2));
1376
1377    if (smc->saved_skb) {
1378	/* THIS SHOULD NEVER HAPPEN. */
1379	smc->stats.tx_aborted_errors++;
1380	printk(KERN_DEBUG "%s: Internal error -- sent packet while busy.\n",
1381	       dev->name);
1382	return 1;
1383    }
1384    smc->saved_skb = skb;
1385
1386    num_pages = skb->len >> 8;
1387
1388    if (num_pages > 7) {
1389	printk(KERN_ERR "%s: Far too big packet error.\n", dev->name);
1390	dev_kfree_skb (skb);
1391	smc->saved_skb = NULL;
1392	smc->stats.tx_dropped++;
1393	return 0;		/* Do not re-queue this packet. */
1394    }
1395    /* A packet is now waiting. */
1396    smc->packets_waiting++;
1397
1398    SMC_SELECT_BANK(2);	/* Paranoia, we should always be in window 2 */
1399
1400    /* need MC_RESET to keep the memory consistent. errata? */
1401    if (smc->rx_ovrn) {
1402	outw(MC_RESET, ioaddr + MMU_CMD);
1403	smc->rx_ovrn = 0;
1404    }
1405
1406    /* Allocate the memory; send the packet now if we win. */
1407    outw(MC_ALLOC | num_pages, ioaddr + MMU_CMD);
1408    for (time_out = MEMORY_WAIT_TIME; time_out >= 0; time_out--) {
1409	ir = inw(ioaddr+INTERRUPT);
1410	if (ir & IM_ALLOC_INT) {
1411	    /* Acknowledge the interrupt, send the packet. */
1412	    outw((ir&0xff00) | IM_ALLOC_INT, ioaddr + INTERRUPT);
1413	    smc_hardware_send_packet(dev);	/* Send the packet now.. */
1414	    return 0;
1415	}
1416    }
1417
1418    /* Otherwise defer until the Tx-space-allocated interrupt. */
1419    DEBUG(2, "%s: memory allocation deferred.\n", dev->name);
1420    outw((IM_ALLOC_INT << 8) | (ir & 0xff00), ioaddr + INTERRUPT);
1421
1422    return 0;
1423}
1424
1425/*======================================================================
1426
1427    Handle a Tx anomolous event.  Entered while in Window 2.
1428
1429======================================================================*/
1430
1431static void smc_tx_err(struct net_device * dev)
1432{
1433    struct smc_private *smc = netdev_priv(dev);
1434    kio_addr_t ioaddr = dev->base_addr;
1435    int saved_packet = inw(ioaddr + PNR_ARR) & 0xff;
1436    int packet_no = inw(ioaddr + FIFO_PORTS) & 0x7f;
1437    int tx_status;
1438
1439    /* select this as the packet to read from */
1440    outw(packet_no, ioaddr + PNR_ARR);
1441
1442    /* read the first word from this packet */
1443    outw(PTR_AUTOINC | PTR_READ | 0, ioaddr + POINTER);
1444
1445    tx_status = inw(ioaddr + DATA_1);
1446
1447    smc->stats.tx_errors++;
1448    if (tx_status & TS_LOSTCAR) smc->stats.tx_carrier_errors++;
1449    if (tx_status & TS_LATCOL)  smc->stats.tx_window_errors++;
1450    if (tx_status & TS_16COL) {
1451	smc->stats.tx_aborted_errors++;
1452	smc->tx_err++;
1453    }
1454
1455    if (tx_status & TS_SUCCESS) {
1456	printk(KERN_NOTICE "%s: Successful packet caused error "
1457	       "interrupt?\n", dev->name);
1458    }
1459    /* re-enable transmit */
1460    SMC_SELECT_BANK(0);
1461    outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1462    SMC_SELECT_BANK(2);
1463
1464    outw(MC_FREEPKT, ioaddr + MMU_CMD); 	/* Free the packet memory. */
1465
1466    /* one less packet waiting for me */
1467    smc->packets_waiting--;
1468
1469    outw(saved_packet, ioaddr + PNR_ARR);
1470    return;
1471}
1472
1473/*====================================================================*/
1474
1475static void smc_eph_irq(struct net_device *dev)
1476{
1477    struct smc_private *smc = netdev_priv(dev);
1478    kio_addr_t ioaddr = dev->base_addr;
1479    u_short card_stats, ephs;
1480
1481    SMC_SELECT_BANK(0);
1482    ephs = inw(ioaddr + EPH);
1483    DEBUG(2, "%s: Ethernet protocol handler interrupt, status"
1484	  " %4.4x.\n", dev->name, ephs);
1485    /* Could be a counter roll-over warning: update stats. */
1486    card_stats = inw(ioaddr + COUNTER);
1487    /* single collisions */
1488    smc->stats.collisions += card_stats & 0xF;
1489    card_stats >>= 4;
1490    /* multiple collisions */
1491    smc->stats.collisions += card_stats & 0xF;
1492    /* If we had a transmit error we must re-enable the transmitter. */
1493    outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1494
1495    /* Clear a link error interrupt. */
1496    SMC_SELECT_BANK(1);
1497    outw(CTL_AUTO_RELEASE | 0x0000, ioaddr + CONTROL);
1498    outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1499	 ioaddr + CONTROL);
1500    SMC_SELECT_BANK(2);
1501}
1502
1503/*====================================================================*/
1504
1505static irqreturn_t smc_interrupt(int irq, void *dev_id)
1506{
1507    struct net_device *dev = dev_id;
1508    struct smc_private *smc = netdev_priv(dev);
1509    kio_addr_t ioaddr;
1510    u_short saved_bank, saved_pointer, mask, status;
1511    unsigned int handled = 1;
1512    char bogus_cnt = INTR_WORK;		/* Work we are willing to do. */
1513
1514    if (!netif_device_present(dev))
1515	return IRQ_NONE;
1516
1517    ioaddr = dev->base_addr;
1518
1519    DEBUG(3, "%s: SMC91c92 interrupt %d at %#x.\n", dev->name,
1520	  irq, ioaddr);
1521
1522    smc->watchdog = 0;
1523    saved_bank = inw(ioaddr + BANK_SELECT);
1524    if ((saved_bank & 0xff00) != 0x3300) {
1525	/* The device does not exist -- the card could be off-line, or
1526	   maybe it has been ejected. */
1527	DEBUG(1, "%s: SMC91c92 interrupt %d for non-existent"
1528	      "/ejected device.\n", dev->name, irq);
1529	handled = 0;
1530	goto irq_done;
1531    }
1532
1533    SMC_SELECT_BANK(2);
1534    saved_pointer = inw(ioaddr + POINTER);
1535    mask = inw(ioaddr + INTERRUPT) >> 8;
1536    /* clear all interrupts */
1537    outw(0, ioaddr + INTERRUPT);
1538
1539    do { /* read the status flag, and mask it */
1540	status = inw(ioaddr + INTERRUPT) & 0xff;
1541	DEBUG(3, "%s: Status is %#2.2x (mask %#2.2x).\n", dev->name,
1542	      status, mask);
1543	if ((status & mask) == 0) {
1544	    if (bogus_cnt == INTR_WORK)
1545		handled = 0;
1546	    break;
1547	}
1548	if (status & IM_RCV_INT) {
1549	    /* Got a packet(s). */
1550	    smc_rx(dev);
1551	}
1552	if (status & IM_TX_INT) {
1553	    smc_tx_err(dev);
1554	    outw(IM_TX_INT, ioaddr + INTERRUPT);
1555	}
1556	status &= mask;
1557	if (status & IM_TX_EMPTY_INT) {
1558	    outw(IM_TX_EMPTY_INT, ioaddr + INTERRUPT);
1559	    mask &= ~IM_TX_EMPTY_INT;
1560	    smc->stats.tx_packets += smc->packets_waiting;
1561	    smc->packets_waiting = 0;
1562	}
1563	if (status & IM_ALLOC_INT) {
1564	    /* Clear this interrupt so it doesn't happen again */
1565	    mask &= ~IM_ALLOC_INT;
1566
1567	    smc_hardware_send_packet(dev);
1568
1569	    /* enable xmit interrupts based on this */
1570	    mask |= (IM_TX_EMPTY_INT | IM_TX_INT);
1571
1572	    /* and let the card send more packets to me */
1573	    netif_wake_queue(dev);
1574	}
1575	if (status & IM_RX_OVRN_INT) {
1576	    smc->stats.rx_errors++;
1577	    smc->stats.rx_fifo_errors++;
1578	    if (smc->duplex)
1579		smc->rx_ovrn = 1; /* need MC_RESET outside smc_interrupt */
1580	    outw(IM_RX_OVRN_INT, ioaddr + INTERRUPT);
1581	}
1582	if (status & IM_EPH_INT)
1583	    smc_eph_irq(dev);
1584    } while (--bogus_cnt);
1585
1586    DEBUG(3, "  Restoring saved registers mask %2.2x bank %4.4x"
1587	  " pointer %4.4x.\n", mask, saved_bank, saved_pointer);
1588
1589    /* restore state register */
1590    outw((mask<<8), ioaddr + INTERRUPT);
1591    outw(saved_pointer, ioaddr + POINTER);
1592    SMC_SELECT_BANK(saved_bank);
1593
1594    DEBUG(3, "%s: Exiting interrupt IRQ%d.\n", dev->name, irq);
1595
1596irq_done:
1597
1598    if ((smc->manfid == MANFID_OSITECH) &&
1599	(smc->cardid != PRODID_OSITECH_SEVEN)) {
1600	/* Retrigger interrupt if needed */
1601	mask_bits(0x00ff, ioaddr-0x10+OSITECH_RESET_ISR);
1602	set_bits(0x0300, ioaddr-0x10+OSITECH_RESET_ISR);
1603    }
1604    if (smc->manfid == MANFID_MOTOROLA) {
1605	u_char cor;
1606	cor = readb(smc->base + MOT_UART + CISREG_COR);
1607	writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_UART + CISREG_COR);
1608	writeb(cor, smc->base + MOT_UART + CISREG_COR);
1609	cor = readb(smc->base + MOT_LAN + CISREG_COR);
1610	writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_LAN + CISREG_COR);
1611	writeb(cor, smc->base + MOT_LAN + CISREG_COR);
1612    }
1613#ifdef DOES_NOT_WORK
1614    if (smc->base != NULL) { /* Megahertz MFC's */
1615	readb(smc->base+MEGAHERTZ_ISR);
1616	readb(smc->base+MEGAHERTZ_ISR);
1617    }
1618#endif
1619    return IRQ_RETVAL(handled);
1620}
1621
1622/*====================================================================*/
1623
1624static void smc_rx(struct net_device *dev)
1625{
1626    struct smc_private *smc = netdev_priv(dev);
1627    kio_addr_t ioaddr = dev->base_addr;
1628    int rx_status;
1629    int packet_length;	/* Caution: not frame length, rather words
1630			   to transfer from the chip. */
1631
1632    /* Assertion: we are in Window 2. */
1633
1634    if (inw(ioaddr + FIFO_PORTS) & FP_RXEMPTY) {
1635	printk(KERN_ERR "%s: smc_rx() with nothing on Rx FIFO.\n",
1636	       dev->name);
1637	return;
1638    }
1639
1640    /*  Reset the read pointer, and read the status and packet length. */
1641    outw(PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER);
1642    rx_status = inw(ioaddr + DATA_1);
1643    packet_length = inw(ioaddr + DATA_1) & 0x07ff;
1644
1645    DEBUG(2, "%s: Receive status %4.4x length %d.\n",
1646	  dev->name, rx_status, packet_length);
1647
1648    if (!(rx_status & RS_ERRORS)) {
1649	/* do stuff to make a new packet */
1650	struct sk_buff *skb;
1651
1652	/* Note: packet_length adds 5 or 6 extra bytes here! */
1653	skb = dev_alloc_skb(packet_length+2);
1654
1655	if (skb == NULL) {
1656	    DEBUG(1, "%s: Low memory, packet dropped.\n", dev->name);
1657	    smc->stats.rx_dropped++;
1658	    outw(MC_RELEASE, ioaddr + MMU_CMD);
1659	    return;
1660	}
1661
1662	packet_length -= (rx_status & RS_ODDFRAME ? 5 : 6);
1663	skb_reserve(skb, 2);
1664	insw(ioaddr+DATA_1, skb_put(skb, packet_length),
1665	     (packet_length+1)>>1);
1666	skb->protocol = eth_type_trans(skb, dev);
1667
1668	netif_rx(skb);
1669	dev->last_rx = jiffies;
1670	smc->stats.rx_packets++;
1671	smc->stats.rx_bytes += packet_length;
1672	if (rx_status & RS_MULTICAST)
1673	    smc->stats.multicast++;
1674    } else {
1675	/* error ... */
1676	smc->stats.rx_errors++;
1677
1678	if (rx_status & RS_ALGNERR)  smc->stats.rx_frame_errors++;
1679	if (rx_status & (RS_TOOSHORT | RS_TOOLONG))
1680	    smc->stats.rx_length_errors++;
1681	if (rx_status & RS_BADCRC)	smc->stats.rx_crc_errors++;
1682    }
1683    /* Let the MMU free the memory of this packet. */
1684    outw(MC_RELEASE, ioaddr + MMU_CMD);
1685
1686    return;
1687}
1688
1689/*====================================================================*/
1690
1691static struct net_device_stats *smc_get_stats(struct net_device *dev)
1692{
1693    struct smc_private *smc = netdev_priv(dev);
1694    /* Nothing to update - the 91c92 is a pretty primative chip. */
1695    return &smc->stats;
1696}
1697
1698/*======================================================================
1699
1700    Calculate values for the hardware multicast filter hash table.
1701
1702======================================================================*/
1703
1704static void fill_multicast_tbl(int count, struct dev_mc_list *addrs,
1705			       u_char *multicast_table)
1706{
1707    struct dev_mc_list	*mc_addr;
1708
1709    for (mc_addr = addrs;  mc_addr && count-- > 0;  mc_addr = mc_addr->next) {
1710	u_int position = ether_crc(6, mc_addr->dmi_addr);
1711#ifndef final_version		    /* Verify multicast address. */
1712	if ((mc_addr->dmi_addr[0] & 1) == 0)
1713	    continue;
1714#endif
1715	multicast_table[position >> 29] |= 1 << ((position >> 26) & 7);
1716    }
1717}
1718
1719/*======================================================================
1720
1721    Set the receive mode.
1722
1723    This routine is used by both the protocol level to notify us of
1724    promiscuous/multicast mode changes, and by the open/reset code to
1725    initialize the Rx registers.  We always set the multicast list and
1726    leave the receiver running.
1727
1728======================================================================*/
1729
1730static void set_rx_mode(struct net_device *dev)
1731{
1732    kio_addr_t ioaddr = dev->base_addr;
1733    struct smc_private *smc = netdev_priv(dev);
1734    u_int multicast_table[ 2 ] = { 0, };
1735    unsigned long flags;
1736    u_short rx_cfg_setting;
1737
1738    if (dev->flags & IFF_PROMISC) {
1739	rx_cfg_setting = RxStripCRC | RxEnable | RxPromisc | RxAllMulti;
1740    } else if (dev->flags & IFF_ALLMULTI)
1741	rx_cfg_setting = RxStripCRC | RxEnable | RxAllMulti;
1742    else {
1743	if (dev->mc_count)  {
1744	    fill_multicast_tbl(dev->mc_count, dev->mc_list,
1745			       (u_char *)multicast_table);
1746	}
1747	rx_cfg_setting = RxStripCRC | RxEnable;
1748    }
1749
1750    /* Load MC table and Rx setting into the chip without interrupts. */
1751    spin_lock_irqsave(&smc->lock, flags);
1752    SMC_SELECT_BANK(3);
1753    outl(multicast_table[0], ioaddr + MULTICAST0);
1754    outl(multicast_table[1], ioaddr + MULTICAST4);
1755    SMC_SELECT_BANK(0);
1756    outw(rx_cfg_setting, ioaddr + RCR);
1757    SMC_SELECT_BANK(2);
1758    spin_unlock_irqrestore(&smc->lock, flags);
1759
1760    return;
1761}
1762
1763/*======================================================================
1764
1765    Senses when a card's config changes. Here, it's coax or TP.
1766
1767======================================================================*/
1768
1769static int s9k_config(struct net_device *dev, struct ifmap *map)
1770{
1771    struct smc_private *smc = netdev_priv(dev);
1772    if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1773	if (smc->cfg & CFG_MII_SELECT)
1774	    return -EOPNOTSUPP;
1775	else if (map->port > 2)
1776	    return -EINVAL;
1777	dev->if_port = map->port;
1778	printk(KERN_INFO "%s: switched to %s port\n",
1779	       dev->name, if_names[dev->if_port]);
1780	smc_reset(dev);
1781    }
1782    return 0;
1783}
1784
1785/*======================================================================
1786
1787    Reset the chip, reloading every register that might be corrupted.
1788
1789======================================================================*/
1790
1791/*
1792  Set transceiver type, perhaps to something other than what the user
1793  specified in dev->if_port.
1794*/
1795static void smc_set_xcvr(struct net_device *dev, int if_port)
1796{
1797    struct smc_private *smc = netdev_priv(dev);
1798    kio_addr_t ioaddr = dev->base_addr;
1799    u_short saved_bank;
1800
1801    saved_bank = inw(ioaddr + BANK_SELECT);
1802    SMC_SELECT_BANK(1);
1803    if (if_port == 2) {
1804	outw(smc->cfg | CFG_AUI_SELECT, ioaddr + CONFIG);
1805	if ((smc->manfid == MANFID_OSITECH) &&
1806	    (smc->cardid != PRODID_OSITECH_SEVEN))
1807	    set_bits(OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1808	smc->media_status = ((dev->if_port == 0) ? 0x0001 : 0x0002);
1809    } else {
1810	outw(smc->cfg, ioaddr + CONFIG);
1811	if ((smc->manfid == MANFID_OSITECH) &&
1812	    (smc->cardid != PRODID_OSITECH_SEVEN))
1813	    mask_bits(~OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1814	smc->media_status = ((dev->if_port == 0) ? 0x0012 : 0x4001);
1815    }
1816    SMC_SELECT_BANK(saved_bank);
1817}
1818
1819static void smc_reset(struct net_device *dev)
1820{
1821    kio_addr_t ioaddr = dev->base_addr;
1822    struct smc_private *smc = netdev_priv(dev);
1823    int i;
1824
1825    DEBUG(0, "%s: smc91c92 reset called.\n", dev->name);
1826
1827    /* The first interaction must be a write to bring the chip out
1828       of sleep mode. */
1829    SMC_SELECT_BANK(0);
1830    /* Reset the chip. */
1831    outw(RCR_SOFTRESET, ioaddr + RCR);
1832    udelay(10);
1833
1834    /* Clear the transmit and receive configuration registers. */
1835    outw(RCR_CLEAR, ioaddr + RCR);
1836    outw(TCR_CLEAR, ioaddr + TCR);
1837
1838    /* Set the Window 1 control, configuration and station addr registers.
1839       No point in writing the I/O base register ;-> */
1840    SMC_SELECT_BANK(1);
1841    /* Automatically release successfully transmitted packets,
1842       Accept link errors, counter and Tx error interrupts. */
1843    outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1844	 ioaddr + CONTROL);
1845    smc_set_xcvr(dev, dev->if_port);
1846    if ((smc->manfid == MANFID_OSITECH) &&
1847	(smc->cardid != PRODID_OSITECH_SEVEN))
1848	outw((dev->if_port == 2 ? OSI_AUI_PWR : 0) |
1849	     (inw(ioaddr-0x10+OSITECH_AUI_PWR) & 0xff00),
1850	     ioaddr - 0x10 + OSITECH_AUI_PWR);
1851
1852    /* Fill in the physical address.  The databook is wrong about the order! */
1853    for (i = 0; i < 6; i += 2)
1854	outw((dev->dev_addr[i+1]<<8)+dev->dev_addr[i],
1855	     ioaddr + ADDR0 + i);
1856
1857    /* Reset the MMU */
1858    SMC_SELECT_BANK(2);
1859    outw(MC_RESET, ioaddr + MMU_CMD);
1860    outw(0, ioaddr + INTERRUPT);
1861
1862    /* Re-enable the chip. */
1863    SMC_SELECT_BANK(0);
1864    outw(((smc->cfg & CFG_MII_SELECT) ? 0 : TCR_MONCSN) |
1865	 TCR_ENABLE | TCR_PAD_EN | smc->duplex, ioaddr + TCR);
1866    set_rx_mode(dev);
1867
1868    if (smc->cfg & CFG_MII_SELECT) {
1869	SMC_SELECT_BANK(3);
1870
1871	/* Reset MII */
1872	mdio_write(dev, smc->mii_if.phy_id, 0, 0x8000);
1873
1874	/* Advertise 100F, 100H, 10F, 10H */
1875	mdio_write(dev, smc->mii_if.phy_id, 4, 0x01e1);
1876
1877	/* Restart MII autonegotiation */
1878	mdio_write(dev, smc->mii_if.phy_id, 0, 0x0000);
1879	mdio_write(dev, smc->mii_if.phy_id, 0, 0x1200);
1880    }
1881
1882    /* Enable interrupts. */
1883    SMC_SELECT_BANK(2);
1884    outw((IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT) << 8,
1885	 ioaddr + INTERRUPT);
1886}
1887
1888/*======================================================================
1889
1890    Media selection timer routine
1891
1892======================================================================*/
1893
1894static void media_check(u_long arg)
1895{
1896    struct net_device *dev = (struct net_device *) arg;
1897    struct smc_private *smc = netdev_priv(dev);
1898    kio_addr_t ioaddr = dev->base_addr;
1899    u_short i, media, saved_bank;
1900    u_short link;
1901
1902    saved_bank = inw(ioaddr + BANK_SELECT);
1903
1904    if (!netif_device_present(dev))
1905	goto reschedule;
1906
1907    SMC_SELECT_BANK(2);
1908
1909    /* need MC_RESET to keep the memory consistent. errata? */
1910    if (smc->rx_ovrn) {
1911	outw(MC_RESET, ioaddr + MMU_CMD);
1912	smc->rx_ovrn = 0;
1913    }
1914    i = inw(ioaddr + INTERRUPT);
1915    SMC_SELECT_BANK(0);
1916    media = inw(ioaddr + EPH) & EPH_LINK_OK;
1917    SMC_SELECT_BANK(1);
1918    media |= (inw(ioaddr + CONFIG) & CFG_AUI_SELECT) ? 2 : 1;
1919
1920    /* Check for pending interrupt with watchdog flag set: with
1921       this, we can limp along even if the interrupt is blocked */
1922    if (smc->watchdog++ && ((i>>8) & i)) {
1923	if (!smc->fast_poll)
1924	    printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1925	smc_interrupt(dev->irq, dev);
1926	smc->fast_poll = HZ;
1927    }
1928    if (smc->fast_poll) {
1929	smc->fast_poll--;
1930	smc->media.expires = jiffies + HZ/100;
1931	add_timer(&smc->media);
1932	SMC_SELECT_BANK(saved_bank);
1933	return;
1934    }
1935
1936    if (smc->cfg & CFG_MII_SELECT) {
1937	if (smc->mii_if.phy_id < 0)
1938	    goto reschedule;
1939
1940	SMC_SELECT_BANK(3);
1941	link = mdio_read(dev, smc->mii_if.phy_id, 1);
1942	if (!link || (link == 0xffff)) {
1943  	    printk(KERN_INFO "%s: MII is missing!\n", dev->name);
1944	    smc->mii_if.phy_id = -1;
1945	    goto reschedule;
1946	}
1947
1948	link &= 0x0004;
1949	if (link != smc->link_status) {
1950	    u_short p = mdio_read(dev, smc->mii_if.phy_id, 5);
1951	    printk(KERN_INFO "%s: %s link beat\n", dev->name,
1952		(link) ? "found" : "lost");
1953	    smc->duplex = (((p & 0x0100) || ((p & 0x1c0) == 0x40))
1954			   ? TCR_FDUPLX : 0);
1955	    if (link) {
1956	        printk(KERN_INFO "%s: autonegotiation complete: "
1957		       "%sbaseT-%cD selected\n", dev->name,
1958		       ((p & 0x0180) ? "100" : "10"),
1959		       (smc->duplex ? 'F' : 'H'));
1960	    }
1961	    SMC_SELECT_BANK(0);
1962	    outw(inw(ioaddr + TCR) | smc->duplex, ioaddr + TCR);
1963	    smc->link_status = link;
1964	}
1965	goto reschedule;
1966    }
1967
1968    /* Ignore collisions unless we've had no rx's recently */
1969    if (time_after(jiffies, dev->last_rx + HZ)) {
1970	if (smc->tx_err || (smc->media_status & EPH_16COL))
1971	    media |= EPH_16COL;
1972    }
1973    smc->tx_err = 0;
1974
1975    if (media != smc->media_status) {
1976	if ((media & smc->media_status & 1) &&
1977	    ((smc->media_status ^ media) & EPH_LINK_OK))
1978	    printk(KERN_INFO "%s: %s link beat\n", dev->name,
1979		   (smc->media_status & EPH_LINK_OK ? "lost" : "found"));
1980	else if ((media & smc->media_status & 2) &&
1981		 ((smc->media_status ^ media) & EPH_16COL))
1982	    printk(KERN_INFO "%s: coax cable %s\n", dev->name,
1983		   (media & EPH_16COL ? "problem" : "ok"));
1984	if (dev->if_port == 0) {
1985	    if (media & 1) {
1986		if (media & EPH_LINK_OK)
1987		    printk(KERN_INFO "%s: flipped to 10baseT\n",
1988			   dev->name);
1989		else
1990		    smc_set_xcvr(dev, 2);
1991	    } else {
1992		if (media & EPH_16COL)
1993		    smc_set_xcvr(dev, 1);
1994		else
1995		    printk(KERN_INFO "%s: flipped to 10base2\n",
1996			   dev->name);
1997	    }
1998	}
1999	smc->media_status = media;
2000    }
2001
2002reschedule:
2003    smc->media.expires = jiffies + HZ;
2004    add_timer(&smc->media);
2005    SMC_SELECT_BANK(saved_bank);
2006}
2007
2008static int smc_link_ok(struct net_device *dev)
2009{
2010    kio_addr_t ioaddr = dev->base_addr;
2011    struct smc_private *smc = netdev_priv(dev);
2012
2013    if (smc->cfg & CFG_MII_SELECT) {
2014	return mii_link_ok(&smc->mii_if);
2015    } else {
2016        SMC_SELECT_BANK(0);
2017	return inw(ioaddr + EPH) & EPH_LINK_OK;
2018    }
2019}
2020
2021static int smc_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2022{
2023    u16 tmp;
2024    kio_addr_t ioaddr = dev->base_addr;
2025
2026    ecmd->supported = (SUPPORTED_TP | SUPPORTED_AUI |
2027	SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full);
2028
2029    SMC_SELECT_BANK(1);
2030    tmp = inw(ioaddr + CONFIG);
2031    ecmd->port = (tmp & CFG_AUI_SELECT) ? PORT_AUI : PORT_TP;
2032    ecmd->transceiver = XCVR_INTERNAL;
2033    ecmd->speed = SPEED_10;
2034    ecmd->phy_address = ioaddr + MGMT;
2035
2036    SMC_SELECT_BANK(0);
2037    tmp = inw(ioaddr + TCR);
2038    ecmd->duplex = (tmp & TCR_FDUPLX) ? DUPLEX_FULL : DUPLEX_HALF;
2039
2040    return 0;
2041}
2042
2043static int smc_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2044{
2045    u16 tmp;
2046    kio_addr_t ioaddr = dev->base_addr;
2047
2048    if (ecmd->speed != SPEED_10)
2049    	return -EINVAL;
2050    if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
2051    	return -EINVAL;
2052    if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI)
2053	return -EINVAL;
2054    if (ecmd->transceiver != XCVR_INTERNAL)
2055    	return -EINVAL;
2056
2057    if (ecmd->port == PORT_AUI)
2058	smc_set_xcvr(dev, 1);
2059    else
2060	smc_set_xcvr(dev, 0);
2061
2062    SMC_SELECT_BANK(0);
2063    tmp = inw(ioaddr + TCR);
2064    if (ecmd->duplex == DUPLEX_FULL)
2065	tmp |= TCR_FDUPLX;
2066    else
2067	tmp &= ~TCR_FDUPLX;
2068    outw(tmp, ioaddr + TCR);
2069
2070    return 0;
2071}
2072
2073static int check_if_running(struct net_device *dev)
2074{
2075	if (!netif_running(dev))
2076		return -EINVAL;
2077	return 0;
2078}
2079
2080static void smc_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2081{
2082	strcpy(info->driver, DRV_NAME);
2083	strcpy(info->version, DRV_VERSION);
2084}
2085
2086static int smc_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2087{
2088	struct smc_private *smc = netdev_priv(dev);
2089	kio_addr_t ioaddr = dev->base_addr;
2090	u16 saved_bank = inw(ioaddr + BANK_SELECT);
2091	int ret;
2092
2093	SMC_SELECT_BANK(3);
2094	spin_lock_irq(&smc->lock);
2095	if (smc->cfg & CFG_MII_SELECT)
2096		ret = mii_ethtool_gset(&smc->mii_if, ecmd);
2097	else
2098		ret = smc_netdev_get_ecmd(dev, ecmd);
2099	spin_unlock_irq(&smc->lock);
2100	SMC_SELECT_BANK(saved_bank);
2101	return ret;
2102}
2103
2104static int smc_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2105{
2106	struct smc_private *smc = netdev_priv(dev);
2107	kio_addr_t ioaddr = dev->base_addr;
2108	u16 saved_bank = inw(ioaddr + BANK_SELECT);
2109	int ret;
2110
2111	SMC_SELECT_BANK(3);
2112	spin_lock_irq(&smc->lock);
2113	if (smc->cfg & CFG_MII_SELECT)
2114		ret = mii_ethtool_sset(&smc->mii_if, ecmd);
2115	else
2116		ret = smc_netdev_set_ecmd(dev, ecmd);
2117	spin_unlock_irq(&smc->lock);
2118	SMC_SELECT_BANK(saved_bank);
2119	return ret;
2120}
2121
2122static u32 smc_get_link(struct net_device *dev)
2123{
2124	struct smc_private *smc = netdev_priv(dev);
2125	kio_addr_t ioaddr = dev->base_addr;
2126	u16 saved_bank = inw(ioaddr + BANK_SELECT);
2127	u32 ret;
2128
2129	SMC_SELECT_BANK(3);
2130	spin_lock_irq(&smc->lock);
2131	ret = smc_link_ok(dev);
2132	spin_unlock_irq(&smc->lock);
2133	SMC_SELECT_BANK(saved_bank);
2134	return ret;
2135}
2136
2137#ifdef PCMCIA_DEBUG
2138static u32 smc_get_msglevel(struct net_device *dev)
2139{
2140	return pc_debug;
2141}
2142
2143static void smc_set_msglevel(struct net_device *dev, u32 val)
2144{
2145	pc_debug = val;
2146}
2147#endif
2148
2149static int smc_nway_reset(struct net_device *dev)
2150{
2151	struct smc_private *smc = netdev_priv(dev);
2152	if (smc->cfg & CFG_MII_SELECT) {
2153		kio_addr_t ioaddr = dev->base_addr;
2154		u16 saved_bank = inw(ioaddr + BANK_SELECT);
2155		int res;
2156
2157		SMC_SELECT_BANK(3);
2158		res = mii_nway_restart(&smc->mii_if);
2159		SMC_SELECT_BANK(saved_bank);
2160
2161		return res;
2162	} else
2163		return -EOPNOTSUPP;
2164}
2165
2166static const struct ethtool_ops ethtool_ops = {
2167	.begin = check_if_running,
2168	.get_drvinfo = smc_get_drvinfo,
2169	.get_settings = smc_get_settings,
2170	.set_settings = smc_set_settings,
2171	.get_link = smc_get_link,
2172#ifdef PCMCIA_DEBUG
2173	.get_msglevel = smc_get_msglevel,
2174	.set_msglevel = smc_set_msglevel,
2175#endif
2176	.nway_reset = smc_nway_reset,
2177};
2178
2179static int smc_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
2180{
2181	struct smc_private *smc = netdev_priv(dev);
2182	struct mii_ioctl_data *mii = if_mii(rq);
2183	int rc = 0;
2184	u16 saved_bank;
2185	kio_addr_t ioaddr = dev->base_addr;
2186
2187	if (!netif_running(dev))
2188		return -EINVAL;
2189
2190	spin_lock_irq(&smc->lock);
2191	saved_bank = inw(ioaddr + BANK_SELECT);
2192	SMC_SELECT_BANK(3);
2193	rc = generic_mii_ioctl(&smc->mii_if, mii, cmd, NULL);
2194	SMC_SELECT_BANK(saved_bank);
2195	spin_unlock_irq(&smc->lock);
2196	return rc;
2197}
2198
2199static struct pcmcia_device_id smc91c92_ids[] = {
2200	PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0109, 0x0501),
2201	PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0140, 0x000a),
2202	PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3288", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x04cd2988, 0x46a52d63),
2203	PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3336", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x0143b773, 0x46a52d63),
2204	PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "EM1144T", "PCMCIA MODEM", 0xf510db04, 0x856d66c8, 0xbd6c43ef),
2205	PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "XJEM1144/CCEM1144", "PCMCIA MODEM", 0xf510db04, 0x52d21e1e, 0xbd6c43ef),
2206	PCMCIA_PFC_DEVICE_PROD_ID12(0, "Gateway 2000", "XJEM3336", 0xdd9989be, 0x662c394c),
2207	PCMCIA_PFC_DEVICE_PROD_ID12(0, "MEGAHERTZ", "XJEM1144/CCEM1144", 0xf510db04, 0x52d21e1e),
2208	PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Diamonds Modem+Ethernet", 0xc2f80cd, 0x656947b9),
2209	PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Hearts Modem+Ethernet", 0xc2f80cd, 0xdc9ba5ed),
2210	PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x016c, 0x0020),
2211	PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0023),
2212	PCMCIA_DEVICE_PROD_ID123("BASICS by New Media Corporation", "Ethernet", "SMC91C94", 0x23c78a9d, 0x00b2e941, 0xcef397fb),
2213	PCMCIA_DEVICE_PROD_ID12("ARGOSY", "Fast Ethernet PCCard", 0x78f308dc, 0xdcea68bc),
2214	PCMCIA_DEVICE_PROD_ID12("dit Co., Ltd.", "PC Card-10/100BTX", 0xe59365c8, 0x6a2161d1),
2215	PCMCIA_DEVICE_PROD_ID12("DYNALINK", "L100C", 0x6a26d1cf, 0xc16ce9c5),
2216	PCMCIA_DEVICE_PROD_ID12("Farallon", "Farallon Enet", 0x58d93fc4, 0x244734e9),
2217	PCMCIA_DEVICE_PROD_ID12("Megahertz", "CC10BT/2", 0x33234748, 0x3c95b953),
2218	PCMCIA_DEVICE_PROD_ID12("MELCO/SMC", "LPC-TX", 0xa2cd8e6d, 0x42da662a),
2219	PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Four of Diamonds Ethernet", 0xc2f80cd, 0xb3466314),
2220	PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Seven of Diamonds Ethernet", 0xc2f80cd, 0x194b650a),
2221	PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Fast Ethernet PCCard", 0x281f1c5d, 0xdcea68bc),
2222	PCMCIA_DEVICE_PROD_ID12("Psion", "10Mb Ethernet", 0x4ef00b21, 0x844be9e9),
2223	PCMCIA_DEVICE_PROD_ID12("SMC", "EtherEZ Ethernet 8020", 0xc4f8b18b, 0x4a0eeb2d),
2224	/* These conflict with other cards! */
2225	/* PCMCIA_DEVICE_MANF_CARD(0x0186, 0x0100), */
2226	/* PCMCIA_DEVICE_MANF_CARD(0x8a01, 0xc1ab), */
2227	PCMCIA_DEVICE_NULL,
2228};
2229MODULE_DEVICE_TABLE(pcmcia, smc91c92_ids);
2230
2231static struct pcmcia_driver smc91c92_cs_driver = {
2232	.owner		= THIS_MODULE,
2233	.drv		= {
2234		.name	= "smc91c92_cs",
2235	},
2236	.probe		= smc91c92_probe,
2237	.remove		= smc91c92_detach,
2238	.id_table       = smc91c92_ids,
2239	.suspend	= smc91c92_suspend,
2240	.resume		= smc91c92_resume,
2241};
2242
2243static int __init init_smc91c92_cs(void)
2244{
2245	return pcmcia_register_driver(&smc91c92_cs_driver);
2246}
2247
2248static void __exit exit_smc91c92_cs(void)
2249{
2250	pcmcia_unregister_driver(&smc91c92_cs_driver);
2251}
2252
2253module_init(init_smc91c92_cs);
2254module_exit(exit_smc91c92_cs);
2255