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