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