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