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