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