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