1 /* [xirc2ps_cs.c wk 03.11.99] (1.40 1999/11/18 00:06:03)
2  * Xircom CreditCard Ethernet Adapter IIps driver
3  * Xircom Realport 10/100 (RE-100) driver
4  *
5  * This driver supports various Xircom CreditCard Ethernet adapters
6  * including the CE2, CE IIps, RE-10, CEM28, CEM33, CE33, CEM56,
7  * CE3-100, CE3B, RE-100, REM10BT, and REM56G-100.
8  *
9  * 2000-09-24 <psheer@icon.co.za> The Xircom CE3B-100 may not
10  * autodetect the media properly. In this case use the
11  * if_port=1 (for 10BaseT) or if_port=4 (for 100BaseT) options
12  * to force the media type.
13  *
14  * Written originally by Werner Koch based on David Hinds' skeleton of the
15  * PCMCIA driver.
16  *
17  * Copyright (c) 1997,1998 Werner Koch (dd9jn)
18  *
19  * This driver is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * It is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
32  *
33  *
34  * ALTERNATIVELY, this driver may be distributed under the terms of
35  * the following license, in which case the provisions of this license
36  * are required INSTEAD OF the GNU General Public License.  (This clause
37  * is necessary due to a potential bad interaction between the GPL and
38  * the restrictions contained in a BSD-style copyright.)
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, and the entire permission notice in its entirety,
45  *    including the disclaimer of warranties.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote
50  *    products derived from this software without specific prior
51  *    written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
61  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
63  * OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/init.h>
69 #include <linux/sched.h>
70 #include <linux/ptrace.h>
71 #include <linux/slab.h>
72 #include <linux/string.h>
73 #include <linux/timer.h>
74 #include <linux/interrupt.h>
75 #include <linux/in.h>
76 #include <linux/delay.h>
77 #include <linux/ethtool.h>
78 #include <asm/io.h>
79 #include <asm/system.h>
80 #include <asm/bitops.h>
81 #include <asm/uaccess.h>
82 
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/if_arp.h>
87 #include <linux/ioport.h>
88 
89 #include <pcmcia/version.h>
90 #include <pcmcia/cs_types.h>
91 #include <pcmcia/cs.h>
92 #include <pcmcia/cistpl.h>
93 #include <pcmcia/cisreg.h>
94 #include <pcmcia/ciscode.h>
95 
96 #ifndef MANFID_COMPAQ
97   #define MANFID_COMPAQ 	   0x0138
98   #define MANFID_COMPAQ2	   0x0183  /* is this correct? */
99 #endif
100 
101 #include <pcmcia/ds.h>
102 
103 /* Time in jiffies before concluding Tx hung */
104 #define TX_TIMEOUT	((400*HZ)/1000)
105 
106 /****************
107  * Some constants used to access the hardware
108  */
109 
110 /* Register offsets and value constans */
111 #define XIRCREG_CR  0	/* Command register (wr) */
112 enum xirc_cr {
113     TransmitPacket = 0x01,
114     SoftReset = 0x02,
115     EnableIntr = 0x04,
116     ForceIntr  = 0x08,
117     ClearTxFIFO = 0x10,
118     ClearRxOvrun = 0x20,
119     RestartTx	 = 0x40
120 };
121 #define XIRCREG_ESR 0	/* Ethernet status register (rd) */
122 enum xirc_esr {
123     FullPktRcvd = 0x01, /* full packet in receive buffer */
124     PktRejected = 0x04, /* a packet has been rejected */
125     TxPktPend = 0x08,	/* TX Packet Pending */
126     IncorPolarity = 0x10,
127     MediaSelect = 0x20	/* set if TP, clear if AUI */
128 };
129 #define XIRCREG_PR  1	/* Page Register select */
130 #define XIRCREG_EDP 4	/* Ethernet Data Port Register */
131 #define XIRCREG_ISR 6	/* Ethernet Interrupt Status Register */
132 enum xirc_isr {
133     TxBufOvr = 0x01,	/* TX Buffer Overflow */
134     PktTxed  = 0x02,	/* Packet Transmitted */
135     MACIntr  = 0x04,	/* MAC Interrupt occurred */
136     TxResGrant = 0x08,	/* Tx Reservation Granted */
137     RxFullPkt = 0x20,	/* Rx Full Packet */
138     RxPktRej  = 0x40,	/* Rx Packet Rejected */
139     ForcedIntr= 0x80	/* Forced Interrupt */
140 };
141 #define XIRCREG1_IMR0 12 /* Ethernet Interrupt Mask Register (on page 1)*/
142 #define XIRCREG1_IMR1 13
143 #define XIRCREG0_TSO  8  /* Transmit Space Open Register (on page 0)*/
144 #define XIRCREG0_TRS  10 /* Transmit reservation Size Register (page 0)*/
145 #define XIRCREG0_DO   12 /* Data Offset Register (page 0) (wr) */
146 #define XIRCREG0_RSR  12 /* Receive Status Register (page 0) (rd) */
147 enum xirc_rsr {
148     PhyPkt = 0x01,	/* set:physical packet, clear: multicast packet */
149     BrdcstPkt = 0x02,	/* set if it is a broadcast packet */
150     PktTooLong = 0x04,	/* set if packet length > 1518 */
151     AlignErr = 0x10,	/* incorrect CRC and last octet not complete */
152     CRCErr = 0x20,	/* incorrect CRC and last octet is complete */
153     PktRxOk = 0x80	/* received ok */
154 };
155 #define XIRCREG0_PTR 13 /* packets transmitted register (rd) */
156 #define XIRCREG0_RBC 14 /* receive byte count regsister (rd) */
157 #define XIRCREG1_ECR 14 /* ethernet configurationn register */
158 enum xirc_ecr {
159     FullDuplex = 0x04,	/* enable full duplex mode */
160     LongTPMode = 0x08,	/* adjust for longer lengths of TP cable */
161     DisablePolCor = 0x10,/* disable auto polarity correction */
162     DisableLinkPulse = 0x20, /* disable link pulse generation */
163     DisableAutoTx = 0x40, /* disable auto-transmit */
164 };
165 #define XIRCREG2_RBS 8	/* receive buffer start register */
166 #define XIRCREG2_LED 10 /* LED Configuration register */
167 /* values for the leds:    Bits 2-0 for led 1
168  *  0 disabled		   Bits 5-3 for led 2
169  *  1 collision
170  *  2 noncollision
171  *  3 link_detected
172  *  4 incor_polarity
173  *  5 jabber
174  *  6 auto_assertion
175  *  7 rx_tx_activity
176  */
177 #define XIRCREG2_MSR 12 /* Mohawk specific register */
178 
179 #define XIRCREG4_GPR0 8 /* General Purpose Register 0 */
180 #define XIRCREG4_GPR1 9 /* General Purpose Register 1 */
181 #define XIRCREG2_GPR2 13 /* General Purpose Register 2 (page2!)*/
182 #define XIRCREG4_BOV 10 /* Bonding Version Register */
183 #define XIRCREG4_LMA 12 /* Local Memory Address Register */
184 #define XIRCREG4_LMD 14 /* Local Memory Data Port */
185 /* MAC register can only by accessed with 8 bit operations */
186 #define XIRCREG40_CMD0 8    /* Command Register (wr) */
187 enum xirc_cmd { 	    /* Commands */
188     Transmit = 0x01,
189     EnableRecv = 0x04,
190     DisableRecv = 0x08,
191     Abort = 0x10,
192     Online = 0x20,
193     IntrAck = 0x40,
194     Offline = 0x80
195 };
196 #define XIRCREG5_RHSA0	10  /* Rx Host Start Address */
197 #define XIRCREG40_RXST0 9   /* Receive Status Register */
198 #define XIRCREG40_TXST0 11  /* Transmit Status Register 0 */
199 #define XIRCREG40_TXST1 12  /* Transmit Status Register 10 */
200 #define XIRCREG40_RMASK0 13  /* Receive Mask Register */
201 #define XIRCREG40_TMASK0 14  /* Transmit Mask Register 0 */
202 #define XIRCREG40_TMASK1 15  /* Transmit Mask Register 0 */
203 #define XIRCREG42_SWC0	8   /* Software Configuration 0 */
204 #define XIRCREG42_SWC1	9   /* Software Configuration 1 */
205 #define XIRCREG42_BOC	10  /* Back-Off Configuration */
206 #define XIRCREG44_TDR0	8   /* Time Domain Reflectometry 0 */
207 #define XIRCREG44_TDR1	9   /* Time Domain Reflectometry 1 */
208 #define XIRCREG44_RXBC_LO 10 /* Rx Byte Count 0 (rd) */
209 #define XIRCREG44_RXBC_HI 11 /* Rx Byte Count 1 (rd) */
210 #define XIRCREG45_REV	 15 /* Revision Register (rd) */
211 #define XIRCREG50_IA	8   /* Individual Address (8-13) */
212 
213 static char *if_names[] = { "Auto", "10BaseT", "10Base2", "AUI", "100BaseT" };
214 
215 /****************
216  * All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
217  * you do not define PCMCIA_DEBUG at all, all the debug code will be
218  * left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
219  * be present but disabled -- but it can then be enabled for specific
220  * modules at load time with a 'pc_debug=#' option to insmod.
221  */
222 #ifdef PCMCIA_DEBUG
223 static int pc_debug = PCMCIA_DEBUG;
224 MODULE_PARM(pc_debug, "i");
225 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KDBG_XIRC args)
226 #else
227 #define DEBUG(n, args...)
228 #endif
229 static char *version =
230 "xirc2ps_cs.c 1.31 1998/12/09 19:32:55 (dd9jn+kvh)";
231 	    /* !--- CVS revision */
232 #define KDBG_XIRC KERN_DEBUG   "xirc2ps_cs: "
233 #define KERR_XIRC KERN_ERR     "xirc2ps_cs: "
234 #define KWRN_XIRC KERN_WARNING "xirc2ps_cs: "
235 #define KNOT_XIRC KERN_NOTICE  "xirc2ps_cs: "
236 #define KINF_XIRC KERN_INFO    "xirc2ps_cs: "
237 
238 /* card types */
239 #define XIR_UNKNOWN  0	/* unknown: not supported */
240 #define XIR_CE	     1	/* (prodid 1) different hardware: not supported */
241 #define XIR_CE2      2	/* (prodid 2) */
242 #define XIR_CE3      3	/* (prodid 3) */
243 #define XIR_CEM      4	/* (prodid 1) different hardware: not supported */
244 #define XIR_CEM2     5	/* (prodid 2) */
245 #define XIR_CEM3     6	/* (prodid 3) */
246 #define XIR_CEM33    7	/* (prodid 4) */
247 #define XIR_CEM56M   8	/* (prodid 5) */
248 #define XIR_CEM56    9	/* (prodid 6) */
249 #define XIR_CM28    10	/* (prodid 3) modem only: not supported here */
250 #define XIR_CM33    11	/* (prodid 4) modem only: not supported here */
251 #define XIR_CM56    12	/* (prodid 5) modem only: not supported here */
252 #define XIR_CG	    13	/* (prodid 1) GSM modem only: not supported */
253 #define XIR_CBE     14	/* (prodid 1) cardbus ethernet: not supported */
254 /*====================================================================*/
255 
256 /* Module parameters */
257 
258 MODULE_DESCRIPTION("Xircom PCMCIA ethernet driver");
259 MODULE_LICENSE("Dual MPL/GPL");
260 
261 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
262 
263 static int irq_list[4] = { -1 };
264 MODULE_PARM(irq_list, "1-4i");
265 INT_MODULE_PARM(irq_mask,	0xdeb8);
266 INT_MODULE_PARM(if_port,	0);
267 INT_MODULE_PARM(full_duplex,	0);
268 INT_MODULE_PARM(do_sound, 	1);
269 INT_MODULE_PARM(lockup_hack,	0);  /* anti lockup hack */
270 
271 /*====================================================================*/
272 
273 /* We do not process more than these number of bytes during one
274  * interrupt. (Of course we receive complete packets, so this is not
275  * an exact value).
276  * Something between 2000..22000; first value gives best interrupt latency,
277  * the second enables the usage of the complete on-chip buffer. We use the
278  * high value as the initial value.
279  */
280 static unsigned maxrx_bytes = 22000;
281 
282 /* MII management prototypes */
283 static void mii_idle(ioaddr_t ioaddr);
284 static void mii_putbit(ioaddr_t ioaddr, unsigned data);
285 static int  mii_getbit(ioaddr_t ioaddr);
286 static void mii_wbits(ioaddr_t ioaddr, unsigned data, int len);
287 static unsigned mii_rd(ioaddr_t ioaddr,	u_char phyaddr, u_char phyreg);
288 static void mii_wr(ioaddr_t ioaddr, u_char phyaddr, u_char phyreg,
289 		   unsigned data, int len);
290 
291 /*
292  * The event() function is this driver's Card Services event handler.
293  * It will be called by Card Services when an appropriate card status
294  * event is received.  The config() and release() entry points are
295  * used to configure or release a socket, in response to card insertion
296  * and ejection events.  They are invoked from the event handler.
297  */
298 
299 static int has_ce2_string(dev_link_t * link);
300 static void xirc2ps_config(dev_link_t * link);
301 static void xirc2ps_release(u_long arg);
302 static int xirc2ps_event(event_t event, int priority,
303 			 event_callback_args_t * args);
304 
305 /****************
306  * The attach() and detach() entry points are used to create and destroy
307  * "instances" of the driver, where each instance represents everything
308  * needed to manage one actual PCMCIA card.
309  */
310 
311 static dev_link_t *xirc2ps_attach(void);
312 static void xirc2ps_detach(dev_link_t *);
313 
314 /****************
315  * You'll also need to prototype all the functions that will actually
316  * be used to talk to your device.  See 'pcmem_cs' for a good example
317  * of a fully self-sufficient driver; the other drivers rely more or
318  * less on other parts of the kernel.
319  */
320 
321 static void xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs);
322 
323 /*
324  * The dev_info variable is the "key" that is used to match up this
325  * device driver with appropriate cards, through the card configuration
326  * database.
327  */
328 
329 static dev_info_t dev_info = "xirc2ps_cs";
330 
331 /****************
332  * A linked list of "instances" of the device.  Each actual
333  * PCMCIA card corresponds to one device instance, and is described
334  * by one dev_link_t structure (defined in ds.h).
335  *
336  * You may not want to use a linked list for this -- for example, the
337  * memory card driver uses an array of dev_link_t pointers, where minor
338  * device numbers are used to derive the corresponding array index.
339  */
340 
341 static dev_link_t *dev_list;
342 
343 /****************
344  * A dev_link_t structure has fields for most things that are needed
345  * to keep track of a socket, but there will usually be some device
346  * specific information that also needs to be kept track of.  The
347  * 'priv' pointer in a dev_link_t structure can be used to point to
348  * a device-specific private data structure, like this.
349  *
350  * A driver needs to provide a dev_node_t structure for each device
351  * on a card.  In some cases, there is only one device per card (for
352  * example, ethernet cards, modems).  In other cases, there may be
353  * many actual or logical devices (SCSI adapters, memory cards with
354  * multiple partitions).  The dev_node_t structures need to be kept
355  * in a linked list starting at the 'dev' field of a dev_link_t
356  * structure.  We allocate them in the card's private data structure,
357  * because they generally can't be allocated dynamically.
358  */
359 
360 typedef struct local_info_t {
361     dev_link_t link;
362     struct net_device dev;
363     dev_node_t node;
364     struct net_device_stats stats;
365     int card_type;
366     int probe_port;
367     int silicon; /* silicon revision. 0=old CE2, 1=Scipper, 4=Mohawk */
368     int mohawk;  /* a CE3 type card */
369     int dingo;	 /* a CEM56 type card */
370     int new_mii; /* has full 10baseT/100baseT MII */
371     int modem;	 /* is a multi function card (i.e with a modem) */
372     caddr_t dingo_ccr; /* only used for CEM56 cards */
373     unsigned last_ptr_value; /* last packets transmitted value */
374     const char *manf_str;
375 } local_info_t;
376 
377 /****************
378  * Some more prototypes
379  */
380 static int do_start_xmit(struct sk_buff *skb, struct net_device *dev);
381 static void do_tx_timeout(struct net_device *dev);
382 static struct net_device_stats *do_get_stats(struct net_device *dev);
383 static void set_addresses(struct net_device *dev);
384 static void set_multicast_list(struct net_device *dev);
385 static int set_card_type(dev_link_t *link, const void *s);
386 static int do_config(struct net_device *dev, struct ifmap *map);
387 static int do_open(struct net_device *dev);
388 static int do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
389 static struct ethtool_ops netdev_ethtool_ops;
390 static void hardreset(struct net_device *dev);
391 static void do_reset(struct net_device *dev, int full);
392 static int init_mii(struct net_device *dev);
393 static void do_powerdown(struct net_device *dev);
394 static int do_stop(struct net_device *dev);
395 
396 /*=============== Helper functions =========================*/
397 static void
flush_stale_links(void)398 flush_stale_links(void)
399 {
400     dev_link_t *link, *next;
401     for (link = dev_list; link; link = next) {
402 	next = link->next;
403 	if (link->state & DEV_STALE_LINK)
404 	    xirc2ps_detach(link);
405     }
406 }
407 
408 static void
cs_error(client_handle_t handle,int func,int ret)409 cs_error(client_handle_t handle, int func, int ret)
410 {
411     error_info_t err = { func, ret };
412     CardServices(ReportError, handle, &err);
413 }
414 
415 static int
get_tuple_data(int fn,client_handle_t handle,tuple_t * tuple)416 get_tuple_data(int fn, client_handle_t handle, tuple_t *tuple)
417 {
418     int err;
419 
420     if ((err=CardServices(fn, handle, tuple)))
421 	return err;
422     return CardServices(GetTupleData, handle, tuple);
423 }
424 
425 static int
get_tuple(int fn,client_handle_t handle,tuple_t * tuple,cisparse_t * parse)426 get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
427 {
428     int err;
429 
430     if ((err=get_tuple_data(fn, handle, tuple)))
431 	return err;
432     return CardServices(ParseTuple, handle, tuple, parse);
433 }
434 
435 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
436 #define next_tuple(a, b, c)  get_tuple(GetNextTuple, a, b, c)
437 
438 #define SelectPage(pgnr)   outb((pgnr), ioaddr + XIRCREG_PR)
439 #define GetByte(reg)	   ((unsigned)inb(ioaddr + (reg)))
440 #define GetWord(reg)	   ((unsigned)inw(ioaddr + (reg)))
441 #define PutByte(reg,value) outb((value), ioaddr+(reg))
442 #define PutWord(reg,value) outw((value), ioaddr+(reg))
443 
444 static void
busy_loop(u_long len)445 busy_loop(u_long len)
446 {
447     if (in_interrupt()) {
448 	u_long timeout = jiffies + len;
449 	u_long flags;
450 	save_flags(flags);
451 	sti();
452 	while (timeout >= jiffies)
453 	    ;
454 	restore_flags(flags);
455     } else {
456 	__set_current_state(TASK_UNINTERRUPTIBLE);
457 	schedule_timeout(len);
458     }
459 }
460 
461 /*====== Functions used for debugging =================================*/
462 #if defined(PCMCIA_DEBUG) && 0 /* reading regs may change system status */
463 static void
PrintRegisters(struct net_device * dev)464 PrintRegisters(struct net_device *dev)
465 {
466     ioaddr_t ioaddr = dev->base_addr;
467 
468     if (pc_debug > 1) {
469 	int i, page;
470 
471 	printk(KDBG_XIRC "Register  common: ");
472 	for (i = 0; i < 8; i++)
473 	    printk(" %2.2x", GetByte(i));
474 	printk("\n");
475 	for (page = 0; page <= 8; page++) {
476 	    printk(KDBG_XIRC "Register page %2x: ", page);
477 	    SelectPage(page);
478 	    for (i = 8; i < 16; i++)
479 		printk(" %2.2x", GetByte(i));
480 	    printk("\n");
481 	}
482 	for (page=0x40 ; page <= 0x5f; page++) {
483 	    if (page == 0x43 || (page >= 0x46 && page <= 0x4f)
484 		|| (page >= 0x51 && page <=0x5e))
485 		continue;
486 	    printk(KDBG_XIRC "Register page %2x: ", page);
487 	    SelectPage(page);
488 	    for (i = 8; i < 16; i++)
489 		printk(" %2.2x", GetByte(i));
490 	    printk("\n");
491 	}
492     }
493 }
494 #endif /* PCMCIA_DEBUG */
495 
496 /*============== MII Management functions ===============*/
497 
498 /****************
499  * Turn around for read
500  */
501 static void
mii_idle(ioaddr_t ioaddr)502 mii_idle(ioaddr_t ioaddr)
503 {
504     PutByte(XIRCREG2_GPR2, 0x04|0); /* drive MDCK low */
505     udelay(1);
506     PutByte(XIRCREG2_GPR2, 0x04|1); /* and drive MDCK high */
507     udelay(1);
508 }
509 
510 /****************
511  * Write a bit to MDI/O
512  */
513 static void
mii_putbit(ioaddr_t ioaddr,unsigned data)514 mii_putbit(ioaddr_t ioaddr, unsigned data)
515 {
516   #if 1
517     if (data) {
518 	PutByte(XIRCREG2_GPR2, 0x0c|2|0); /* set MDIO */
519 	udelay(1);
520 	PutByte(XIRCREG2_GPR2, 0x0c|2|1); /* and drive MDCK high */
521 	udelay(1);
522     } else {
523 	PutByte(XIRCREG2_GPR2, 0x0c|0|0); /* clear MDIO */
524 	udelay(1);
525 	PutByte(XIRCREG2_GPR2, 0x0c|0|1); /* and drive MDCK high */
526 	udelay(1);
527     }
528   #else
529     if (data) {
530 	PutWord(XIRCREG2_GPR2-1, 0x0e0e);
531 	udelay(1);
532 	PutWord(XIRCREG2_GPR2-1, 0x0f0f);
533 	udelay(1);
534     } else {
535 	PutWord(XIRCREG2_GPR2-1, 0x0c0c);
536 	udelay(1);
537 	PutWord(XIRCREG2_GPR2-1, 0x0d0d);
538 	udelay(1);
539     }
540   #endif
541 }
542 
543 /****************
544  * Get a bit from MDI/O
545  */
546 static int
mii_getbit(ioaddr_t ioaddr)547 mii_getbit(ioaddr_t ioaddr)
548 {
549     unsigned d;
550 
551     PutByte(XIRCREG2_GPR2, 4|0); /* drive MDCK low */
552     udelay(1);
553     d = GetByte(XIRCREG2_GPR2); /* read MDIO */
554     PutByte(XIRCREG2_GPR2, 4|1); /* drive MDCK high again */
555     udelay(1);
556     return d & 0x20; /* read MDIO */
557 }
558 
559 static void
mii_wbits(ioaddr_t ioaddr,unsigned data,int len)560 mii_wbits(ioaddr_t ioaddr, unsigned data, int len)
561 {
562     unsigned m = 1 << (len-1);
563     for (; m; m >>= 1)
564 	mii_putbit(ioaddr, data & m);
565 }
566 
567 static unsigned
mii_rd(ioaddr_t ioaddr,u_char phyaddr,u_char phyreg)568 mii_rd(ioaddr_t ioaddr,	u_char phyaddr, u_char phyreg)
569 {
570     int i;
571     unsigned data=0, m;
572 
573     SelectPage(2);
574     for (i=0; i < 32; i++)		/* 32 bit preamble */
575 	mii_putbit(ioaddr, 1);
576     mii_wbits(ioaddr, 0x06, 4); 	/* Start and opcode for read */
577     mii_wbits(ioaddr, phyaddr, 5);	/* PHY address to be accessed */
578     mii_wbits(ioaddr, phyreg, 5);	/* PHY register to read */
579     mii_idle(ioaddr);			/* turn around */
580     mii_getbit(ioaddr);
581 
582     for (m = 1<<15; m; m >>= 1)
583 	if (mii_getbit(ioaddr))
584 	    data |= m;
585     mii_idle(ioaddr);
586     return data;
587 }
588 
589 static void
mii_wr(ioaddr_t ioaddr,u_char phyaddr,u_char phyreg,unsigned data,int len)590 mii_wr(ioaddr_t ioaddr, u_char phyaddr, u_char phyreg, unsigned data, int len)
591 {
592     int i;
593 
594     SelectPage(2);
595     for (i=0; i < 32; i++)		/* 32 bit preamble */
596 	mii_putbit(ioaddr, 1);
597     mii_wbits(ioaddr, 0x05, 4); 	/* Start and opcode for write */
598     mii_wbits(ioaddr, phyaddr, 5);	/* PHY address to be accessed */
599     mii_wbits(ioaddr, phyreg, 5);	/* PHY Register to write */
600     mii_putbit(ioaddr, 1);		/* turn around */
601     mii_putbit(ioaddr, 0);
602     mii_wbits(ioaddr, data, len);	/* And write the data */
603     mii_idle(ioaddr);
604 }
605 
606 /*============= Main bulk of functions	=========================*/
607 
608 /****************
609  * xirc2ps_attach() creates an "instance" of the driver, allocating
610  * local data structures for one device.  The device is registered
611  * with Card Services.
612  *
613  * The dev_link structure is initialized, but we don't actually
614  * configure the card at this point -- we wait until we receive a
615  * card insertion event.
616  */
617 
618 static dev_link_t *
xirc2ps_attach(void)619 xirc2ps_attach(void)
620 {
621     client_reg_t client_reg;
622     dev_link_t *link;
623     struct net_device *dev;
624     local_info_t *local;
625     int err;
626 
627     DEBUG(0, "attach()\n");
628     flush_stale_links();
629 
630     /* Allocate the device structure */
631     local = kmalloc(sizeof(*local), GFP_KERNEL);
632     if (!local) return NULL;
633     memset(local, 0, sizeof(*local));
634     link = &local->link; dev = &local->dev;
635     link->priv = dev->priv = local;
636 
637     link->release.function = &xirc2ps_release;
638     link->release.data = (u_long) link;
639 
640     /* General socket configuration */
641     link->conf.Attributes = CONF_ENABLE_IRQ;
642     link->conf.Vcc = 50;
643     link->conf.IntType = INT_MEMORY_AND_IO;
644     link->conf.ConfigIndex = 1;
645     link->conf.Present = PRESENT_OPTION;
646     link->irq.Handler = xirc2ps_interrupt;
647     link->irq.Instance = dev;
648 
649     /* Fill in card specific entries */
650     dev->hard_start_xmit = &do_start_xmit;
651     dev->set_config = &do_config;
652     dev->get_stats = &do_get_stats;
653     dev->do_ioctl = &do_ioctl;
654     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
655     dev->set_multicast_list = &set_multicast_list;
656     ether_setup(dev);
657     dev->open = &do_open;
658     dev->stop = &do_stop;
659 #ifdef HAVE_TX_TIMEOUT
660     dev->tx_timeout = do_tx_timeout;
661     dev->watchdog_timeo = TX_TIMEOUT;
662 #endif
663 
664     /* Register with Card Services */
665     link->next = dev_list;
666     dev_list = link;
667     client_reg.dev_info = &dev_info;
668     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
669     client_reg.EventMask =
670 	CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
671 	CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
672 	CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
673     client_reg.event_handler = &xirc2ps_event;
674     client_reg.Version = 0x0210;
675     client_reg.event_callback_args.client_data = link;
676     if ((err = CardServices(RegisterClient, &link->handle, &client_reg))) {
677 	cs_error(link->handle, RegisterClient, err);
678 	xirc2ps_detach(link);
679 	return NULL;
680     }
681 
682     return link;
683 } /* xirc2ps_attach */
684 
685 /****************
686  *  This deletes a driver "instance".  The device is de-registered
687  *  with Card Services.  If it has been released, all local data
688  *  structures are freed.  Otherwise, the structures will be freed
689  *  when the device is released.
690  */
691 
692 static void
xirc2ps_detach(dev_link_t * link)693 xirc2ps_detach(dev_link_t * link)
694 {
695     local_info_t *local = link->priv;
696     dev_link_t **linkp;
697 
698     DEBUG(0, "detach(0x%p)\n", link);
699 
700     /* Locate device structure */
701     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
702 	if (*linkp == link)
703 	    break;
704     if (!*linkp) {
705 	DEBUG(0, "detach(0x%p): dev_link lost\n", link);
706 	return;
707     }
708 
709     /*
710      * If the device is currently configured and active, we won't
711      * actually delete it yet.	Instead, it is marked so that when
712      * the release() function is called, that will trigger a proper
713      * detach().
714      */
715     del_timer(&link->release);
716     if (link->state & DEV_CONFIG) {
717 	DEBUG(0, "detach postponed, '%s' still locked\n",
718 	      link->dev->dev_name);
719 	link->state |= DEV_STALE_LINK;
720 	return;
721     }
722 
723     /* Break the link with Card Services */
724     if (link->handle)
725 	CardServices(DeregisterClient, link->handle);
726 
727     /* Unlink device structure, free it */
728     *linkp = link->next;
729     if (link->dev)
730 	unregister_netdev(&local->dev);
731     kfree(local);
732 
733 } /* xirc2ps_detach */
734 
735 /****************
736  * Detect the type of the card. s is the buffer with the data of tuple 0x20
737  * Returns: 0 := not supported
738  *		       mediaid=11 and prodid=47
739  * Media-Id bits:
740  *  Ethernet	    0x01
741  *  Tokenring	    0x02
742  *  Arcnet	    0x04
743  *  Wireless	    0x08
744  *  Modem	    0x10
745  *  GSM only	    0x20
746  * Prod-Id bits:
747  *  Pocket	    0x10
748  *  External	    0x20
749  *  Creditcard	    0x40
750  *  Cardbus	    0x80
751  *
752  */
753 static int
set_card_type(dev_link_t * link,const void * s)754 set_card_type(dev_link_t *link, const void *s)
755 {
756     local_info_t *local = link->priv;
757   #ifdef PCMCIA_DEBUG
758     unsigned cisrev = ((const unsigned char *)s)[2];
759   #endif
760     unsigned mediaid= ((const unsigned char *)s)[3];
761     unsigned prodid = ((const unsigned char *)s)[4];
762 
763     DEBUG(0, "cisrev=%02x mediaid=%02x prodid=%02x\n",
764 	  cisrev, mediaid, prodid);
765 
766     local->mohawk = 0;
767     local->dingo = 0;
768     local->modem = 0;
769     local->card_type = XIR_UNKNOWN;
770     if (!(prodid & 0x40)) {
771 	printk(KNOT_XIRC "Ooops: Not a creditcard\n");
772 	return 0;
773     }
774     if (!(mediaid & 0x01)) {
775 	printk(KNOT_XIRC "Not an Ethernet card\n");
776 	return 0;
777     }
778     if (mediaid & 0x10) {
779 	local->modem = 1;
780 	switch(prodid & 15) {
781 	  case 1: local->card_type = XIR_CEM   ; break;
782 	  case 2: local->card_type = XIR_CEM2  ; break;
783 	  case 3: local->card_type = XIR_CEM3  ; break;
784 	  case 4: local->card_type = XIR_CEM33 ; break;
785 	  case 5: local->card_type = XIR_CEM56M;
786 		  local->mohawk = 1;
787 		  break;
788 	  case 6:
789 	  case 7: /* 7 is the RealPort 10/56 */
790 		  local->card_type = XIR_CEM56 ;
791 		  local->mohawk = 1;
792 		  local->dingo = 1;
793 		  break;
794 	}
795     } else {
796 	switch(prodid & 15) {
797 	  case 1: local->card_type = has_ce2_string(link)? XIR_CE2 : XIR_CE ;
798 		  break;
799 	  case 2: local->card_type = XIR_CE2; break;
800 	  case 3: local->card_type = XIR_CE3;
801 		  local->mohawk = 1;
802 		  break;
803 	}
804     }
805     if (local->card_type == XIR_CE || local->card_type == XIR_CEM) {
806 	printk(KNOT_XIRC "Sorry, this is an old CE card\n");
807 	return 0;
808     }
809     if (local->card_type == XIR_UNKNOWN)
810 	printk(KNOT_XIRC "unknown card (mediaid=%02x prodid=%02x)\n",
811 	       mediaid, prodid);
812 
813     return 1;
814 }
815 
816 /****************
817  * There are some CE2 cards out which claim to be a CE card.
818  * This function looks for a "CE2" in the 3rd version field.
819  * Returns: true if this is a CE2
820  */
821 static int
has_ce2_string(dev_link_t * link)822 has_ce2_string(dev_link_t * link)
823 {
824     client_handle_t handle = link->handle;
825     tuple_t tuple;
826     cisparse_t parse;
827     u_char buf[256];
828 
829     tuple.Attributes = 0;
830     tuple.TupleData = buf;
831     tuple.TupleDataMax = 254;
832     tuple.TupleOffset = 0;
833     tuple.DesiredTuple = CISTPL_VERS_1;
834     if (!first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 2) {
835 	if (strstr(parse.version_1.str + parse.version_1.ofs[2], "CE2"))
836 	    return 1;
837     }
838     return 0;
839 }
840 
841 /****************
842  * xirc2ps_config() is scheduled to run after a CARD_INSERTION event
843  * is received, to configure the PCMCIA socket, and to make the
844  * ethernet device available to the system.
845  */
846 static void
xirc2ps_config(dev_link_t * link)847 xirc2ps_config(dev_link_t * link)
848 {
849     client_handle_t handle = link->handle;
850     local_info_t *local = link->priv;
851     struct net_device *dev = &local->dev;
852     tuple_t tuple;
853     cisparse_t parse;
854     ioaddr_t ioaddr;
855     int err, i;
856     u_char buf[64];
857     cistpl_lan_node_id_t *node_id = (cistpl_lan_node_id_t*)parse.funce.data;
858     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
859 
860     local->dingo_ccr = 0;
861 
862     DEBUG(0, "config(0x%p)\n", link);
863 
864     /*
865      * This reads the card's CONFIG tuple to find its configuration
866      * registers.
867      */
868     tuple.Attributes = 0;
869     tuple.TupleData = buf;
870     tuple.TupleDataMax = 64;
871     tuple.TupleOffset = 0;
872 
873     /* Is this a valid	card */
874     tuple.DesiredTuple = CISTPL_MANFID;
875     if ((err=first_tuple(handle, &tuple, &parse))) {
876 	printk(KNOT_XIRC "manfid not found in CIS\n");
877 	goto failure;
878     }
879 
880     switch(parse.manfid.manf) {
881       case MANFID_XIRCOM:
882 	local->manf_str = "Xircom";
883 	break;
884       case MANFID_ACCTON:
885 	local->manf_str = "Accton";
886 	break;
887       case MANFID_COMPAQ:
888       case MANFID_COMPAQ2:
889 	local->manf_str = "Compaq";
890 	break;
891       case MANFID_INTEL:
892 	local->manf_str = "Intel";
893 	break;
894       case MANFID_TOSHIBA:
895 	local->manf_str = "Toshiba";
896 	break;
897       default:
898 	printk(KNOT_XIRC "Unknown Card Manufacturer ID: 0x%04x\n",
899 	       (unsigned)parse.manfid.manf);
900 	goto failure;
901     }
902     DEBUG(0, "found %s card\n", local->manf_str);
903 
904     if (!set_card_type(link, buf)) {
905 	printk(KNOT_XIRC "this card is not supported\n");
906 	goto failure;
907     }
908 
909     /* get configuration stuff */
910     tuple.DesiredTuple = CISTPL_CONFIG;
911     if ((err=first_tuple(handle, &tuple, &parse)))
912 	goto cis_error;
913     link->conf.ConfigBase = parse.config.base;
914     link->conf.Present =    parse.config.rmask[0];
915 
916     /* get the ethernet address from the CIS */
917     tuple.DesiredTuple = CISTPL_FUNCE;
918     for (err = first_tuple(handle, &tuple, &parse); !err;
919 			     err = next_tuple(handle, &tuple, &parse)) {
920 	/* Once I saw two CISTPL_FUNCE_LAN_NODE_ID entries:
921 	 * the first one with a length of zero the second correct -
922 	 * so I skip all entries with length 0 */
923 	if (parse.funce.type == CISTPL_FUNCE_LAN_NODE_ID
924 	    && ((cistpl_lan_node_id_t *)parse.funce.data)->nb)
925 	    break;
926     }
927     if (err) { /* not found: try to get the node-id from tuple 0x89 */
928 	tuple.DesiredTuple = 0x89;  /* data layout looks like tuple 0x22 */
929 	if (!(err = get_tuple_data(GetFirstTuple, handle, &tuple))) {
930 	    if (tuple.TupleDataLen == 8 && *buf == CISTPL_FUNCE_LAN_NODE_ID)
931 		memcpy(&parse, buf, 8);
932 	    else
933 		err = -1;
934 	}
935     }
936     if (err) { /* another try	(James Lehmer's CE2 version 4.1)*/
937 	tuple.DesiredTuple = CISTPL_FUNCE;
938 	for (err = first_tuple(handle, &tuple, &parse); !err;
939 				 err = next_tuple(handle, &tuple, &parse)) {
940 	    if (parse.funce.type == 0x02 && parse.funce.data[0] == 1
941 		&& parse.funce.data[1] == 6 && tuple.TupleDataLen == 13) {
942 		buf[1] = 4;
943 		memcpy(&parse, buf+1, 8);
944 		break;
945 	    }
946 	}
947     }
948     if (err) {
949 	printk(KNOT_XIRC "node-id not found in CIS\n");
950 	goto failure;
951     }
952     node_id = (cistpl_lan_node_id_t *)parse.funce.data;
953     if (node_id->nb != 6) {
954 	printk(KNOT_XIRC "malformed node-id in CIS\n");
955 	goto failure;
956     }
957     for (i=0; i < 6; i++)
958 	dev->dev_addr[i] = node_id->id[i];
959 
960     /* Configure card */
961     link->state |= DEV_CONFIG;
962 
963     link->io.IOAddrLines =10;
964     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
965     link->irq.Attributes = IRQ_HANDLE_PRESENT;
966     link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
967     if (irq_list[0] == -1)
968 	link->irq.IRQInfo2 = irq_mask;
969     else {
970 	for (i = 0; i < 4; i++)
971 	    link->irq.IRQInfo2 |= 1 << irq_list[i];
972     }
973     if (local->modem) {
974 	int pass;
975 
976 	if (do_sound) {
977 	    link->conf.Attributes |= CONF_ENABLE_SPKR;
978 	    link->conf.Status |= CCSR_AUDIO_ENA;
979 	}
980 	link->irq.Attributes |= IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED ;
981 	link->io.NumPorts2 = 8;
982 	link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
983 	if (local->dingo) {
984 	    /* Take the Modem IO port from the CIS and scan for a free
985 	     * Ethernet port */
986 	    link->io.NumPorts1 = 16; /* no Mako stuff anymore */
987 	    tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
988 	    for (err = first_tuple(handle, &tuple, &parse); !err;
989 				 err = next_tuple(handle, &tuple, &parse)) {
990 		if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8) {
991 		    for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
992 			link->conf.ConfigIndex = cf->index ;
993 			link->io.BasePort2 = cf->io.win[0].base;
994 			link->io.BasePort1 = ioaddr;
995 			if (!(err=CardServices(RequestIO, link->handle,
996 								&link->io)))
997 			    goto port_found;
998 		    }
999 		}
1000 	    }
1001 	} else {
1002 	    link->io.NumPorts1 = 18;
1003 	    /* We do 2 passes here: The first one uses the regular mapping and
1004 	     * the second tries again, thereby considering that the 32 ports are
1005 	     * mirrored every 32 bytes. Actually we use a mirrored port for
1006 	     * the Mako if (on the first pass) the COR bit 5 is set.
1007 	     */
1008 	    for (pass=0; pass < 2; pass++) {
1009 		tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
1010 		for (err = first_tuple(handle, &tuple, &parse); !err;
1011 				     err = next_tuple(handle, &tuple, &parse)){
1012 		    if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8){
1013 			link->conf.ConfigIndex = cf->index ;
1014 			link->io.BasePort2 = cf->io.win[0].base;
1015 			link->io.BasePort1 = link->io.BasePort2
1016 				    + (pass ? (cf->index & 0x20 ? -24:8)
1017 					    : (cf->index & 0x20 ?   8:-24));
1018 			if (!(err=CardServices(RequestIO, link->handle,
1019 								&link->io)))
1020 			    goto port_found;
1021 		    }
1022 		}
1023 	    }
1024 	    /* if special option:
1025 	     * try to configure as Ethernet only.
1026 	     * .... */
1027 	}
1028 	printk(KNOT_XIRC "no ports available\n");
1029     } else {
1030 	link->irq.Attributes |= IRQ_TYPE_EXCLUSIVE;
1031 	link->io.NumPorts1 = 16;
1032 	for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
1033 	    link->io.BasePort1 = ioaddr;
1034 	    if (!(err=CardServices(RequestIO, link->handle, &link->io)))
1035 		goto port_found;
1036 	}
1037 	link->io.BasePort1 = 0; /* let CS decide */
1038 	if ((err=CardServices(RequestIO, link->handle, &link->io))) {
1039 	    cs_error(link->handle, RequestIO, err);
1040 	    goto config_error;
1041 	}
1042     }
1043   port_found:
1044     if (err)
1045 	 goto config_error;
1046 
1047     /****************
1048      * Now allocate an interrupt line.	Note that this does not
1049      * actually assign a handler to the interrupt.
1050      */
1051     if ((err=CardServices(RequestIRQ, link->handle, &link->irq))) {
1052 	cs_error(link->handle, RequestIRQ, err);
1053 	goto config_error;
1054     }
1055 
1056     /****************
1057      * This actually configures the PCMCIA socket -- setting up
1058      * the I/O windows and the interrupt mapping.
1059      */
1060     if ((err=CardServices(RequestConfiguration,
1061 			  link->handle, &link->conf))) {
1062 	cs_error(link->handle, RequestConfiguration, err);
1063 	goto config_error;
1064     }
1065 
1066     if (local->dingo) {
1067 	conf_reg_t reg;
1068 	win_req_t req;
1069 	memreq_t mem;
1070 
1071 	/* Reset the modem's BAR to the correct value
1072 	 * This is necessary because in the RequestConfiguration call,
1073 	 * the base address of the ethernet port (BasePort1) is written
1074 	 * to the BAR registers of the modem.
1075 	 */
1076 	reg.Action = CS_WRITE;
1077 	reg.Offset = CISREG_IOBASE_0;
1078 	reg.Value = link->io.BasePort2 & 0xff;
1079 	if ((err = CardServices(AccessConfigurationRegister, link->handle,
1080 				&reg))) {
1081 	    cs_error(link->handle, AccessConfigurationRegister, err);
1082 	    goto config_error;
1083 	}
1084 	reg.Action = CS_WRITE;
1085 	reg.Offset = CISREG_IOBASE_1;
1086 	reg.Value = (link->io.BasePort2 >> 8) & 0xff;
1087 	if ((err = CardServices(AccessConfigurationRegister, link->handle,
1088 				&reg))) {
1089 	    cs_error(link->handle, AccessConfigurationRegister, err);
1090 	    goto config_error;
1091 	}
1092 
1093 	/* There is no config entry for the Ethernet part which
1094 	 * is at 0x0800. So we allocate a window into the attribute
1095 	 * memory and write direct to the CIS registers
1096 	 */
1097 	req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
1098 	req.Base = req.Size = 0;
1099 	req.AccessSpeed = 0;
1100 	link->win = (window_handle_t)link->handle;
1101 	if ((err = CardServices(RequestWindow, &link->win, &req))) {
1102 	    cs_error(link->handle, RequestWindow, err);
1103 	    goto config_error;
1104 	}
1105 	local->dingo_ccr = ioremap(req.Base,0x1000) + 0x0800;
1106 	mem.CardOffset = 0x0;
1107 	mem.Page = 0;
1108 	if ((err = CardServices(MapMemPage, link->win, &mem))) {
1109 	    cs_error(link->handle, MapMemPage, err);
1110 	    goto config_error;
1111 	}
1112 
1113 	/* Setup the CCRs; there are no infos in the CIS about the Ethernet
1114 	 * part.
1115 	 */
1116 	writeb(0x47, local->dingo_ccr + CISREG_COR);
1117 	ioaddr = link->io.BasePort1;
1118 	writeb(ioaddr & 0xff	  , local->dingo_ccr + CISREG_IOBASE_0);
1119 	writeb((ioaddr >> 8)&0xff , local->dingo_ccr + CISREG_IOBASE_1);
1120 
1121       #if 0
1122 	{
1123 	    u_char tmp;
1124 	    printk(KERN_INFO "ECOR:");
1125 	    for (i=0; i < 7; i++) {
1126 		tmp = readb(local->dingo_ccr + i*2);
1127 		printk(" %02x", tmp);
1128 	    }
1129 	    printk("\n");
1130 	    printk(KERN_INFO "DCOR:");
1131 	    for (i=0; i < 4; i++) {
1132 		tmp = readb(local->dingo_ccr + 0x20 + i*2);
1133 		printk(" %02x", tmp);
1134 	    }
1135 	    printk("\n");
1136 	    printk(KERN_INFO "SCOR:");
1137 	    for (i=0; i < 10; i++) {
1138 		tmp = readb(local->dingo_ccr + 0x40 + i*2);
1139 		printk(" %02x", tmp);
1140 	    }
1141 	    printk("\n");
1142 	}
1143       #endif
1144 
1145 	writeb(0x01, local->dingo_ccr + 0x20);
1146 	writeb(0x0c, local->dingo_ccr + 0x22);
1147 	writeb(0x00, local->dingo_ccr + 0x24);
1148 	writeb(0x00, local->dingo_ccr + 0x26);
1149 	writeb(0x00, local->dingo_ccr + 0x28);
1150     }
1151 
1152     /* The if_port symbol can be set when the module is loaded */
1153     local->probe_port=0;
1154     if (!if_port) {
1155 	local->probe_port = dev->if_port = 1;
1156     } else if ((if_port >= 1 && if_port <= 2) ||
1157 	       (local->mohawk && if_port==4))
1158 	dev->if_port = if_port;
1159     else
1160 	printk(KNOT_XIRC "invalid if_port requested\n");
1161 
1162     /* we can now register the device with the net subsystem */
1163     dev->irq = link->irq.AssignedIRQ;
1164     dev->base_addr = link->io.BasePort1;
1165     if ((err=register_netdev(dev))) {
1166 	printk(KNOT_XIRC "register_netdev() failed\n");
1167 	goto config_error;
1168     }
1169 
1170     strcpy(local->node.dev_name, dev->name);
1171     link->dev = &local->node;
1172     link->state &= ~DEV_CONFIG_PENDING;
1173 
1174     if (local->dingo)
1175 	do_reset(dev, 1); /* a kludge to make the cem56 work */
1176 
1177     /* give some infos about the hardware */
1178     printk(KERN_INFO "%s: %s: port %#3lx, irq %d, hwaddr",
1179 	 dev->name, local->manf_str,(u_long)dev->base_addr, (int)dev->irq);
1180     for (i = 0; i < 6; i++)
1181 	printk("%c%02X", i?':':' ', dev->dev_addr[i]);
1182     printk("\n");
1183 
1184     return;
1185 
1186   config_error:
1187     link->state &= ~DEV_CONFIG_PENDING;
1188     xirc2ps_release((u_long)link);
1189     return;
1190 
1191   cis_error:
1192     printk(KNOT_XIRC "unable to parse CIS\n");
1193   failure:
1194     link->state &= ~DEV_CONFIG_PENDING;
1195 } /* xirc2ps_config */
1196 
1197 /****************
1198  * After a card is removed, xirc2ps_release() will unregister the net
1199  * device, and release the PCMCIA configuration.  If the device is
1200  * still open, this will be postponed until it is closed.
1201  */
1202 static void
xirc2ps_release(u_long arg)1203 xirc2ps_release(u_long arg)
1204 {
1205     dev_link_t *link = (dev_link_t *) arg;
1206     local_info_t *local = link->priv;
1207     struct net_device *dev = &local->dev;
1208 
1209     DEBUG(0, "release(0x%p)\n", link);
1210 
1211     /*
1212      * If the device is currently in use, we won't release until it
1213      * is actually closed.
1214      */
1215     if (link->open) {
1216 	DEBUG(0, "release postponed, '%s' "
1217 	      "still open\n", link->dev->dev_name);
1218 	link->state |= DEV_STALE_CONFIG;
1219 	return;
1220     }
1221 
1222     if (link->win) {
1223 	local_info_t *local = dev->priv;
1224 	if (local->dingo)
1225 	    iounmap(local->dingo_ccr - 0x0800);
1226 	CardServices(ReleaseWindow, link->win);
1227     }
1228     CardServices(ReleaseConfiguration, link->handle);
1229     CardServices(ReleaseIO, link->handle, &link->io);
1230     CardServices(ReleaseIRQ, link->handle, &link->irq);
1231     link->state &= ~DEV_CONFIG;
1232 
1233 } /* xirc2ps_release */
1234 
1235 /*====================================================================*/
1236 
1237 /****************
1238  * The card status event handler.  Mostly, this schedules other
1239  * stuff to run after an event is received.  A CARD_REMOVAL event
1240  * also sets some flags to discourage the net drivers from trying
1241  * to talk to the card any more.
1242  *
1243  * When a CARD_REMOVAL event is received, we immediately set a flag
1244  * to block future accesses to this device.  All the functions that
1245  * actually access the device should check this flag to make sure
1246  * the card is still present.
1247  */
1248 
1249 static int
xirc2ps_event(event_t event,int priority,event_callback_args_t * args)1250 xirc2ps_event(event_t event, int priority,
1251 	      event_callback_args_t * args)
1252 {
1253     dev_link_t *link = args->client_data;
1254     local_info_t *lp = link->priv;
1255     struct net_device *dev = &lp->dev;
1256 
1257     DEBUG(0, "event(%d)\n", (int)event);
1258 
1259     switch (event) {
1260     case CS_EVENT_REGISTRATION_COMPLETE:
1261 	DEBUG(0, "registration complete\n");
1262 	break;
1263     case CS_EVENT_CARD_REMOVAL:
1264 	link->state &= ~DEV_PRESENT;
1265 	if (link->state & DEV_CONFIG) {
1266 	    netif_device_detach(dev);
1267 	    mod_timer(&link->release, jiffies + HZ/20);
1268 	}
1269 	break;
1270     case CS_EVENT_CARD_INSERTION:
1271 	link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1272 	xirc2ps_config(link);
1273 	break;
1274     case CS_EVENT_PM_SUSPEND:
1275 	link->state |= DEV_SUSPEND;
1276 	/* Fall through... */
1277     case CS_EVENT_RESET_PHYSICAL:
1278 	if (link->state & DEV_CONFIG) {
1279 	    if (link->open) {
1280 		netif_device_detach(dev);
1281 		do_powerdown(dev);
1282 	    }
1283 	    CardServices(ReleaseConfiguration, link->handle);
1284 	}
1285 	break;
1286     case CS_EVENT_PM_RESUME:
1287 	link->state &= ~DEV_SUSPEND;
1288 	/* Fall through... */
1289     case CS_EVENT_CARD_RESET:
1290 	if (link->state & DEV_CONFIG) {
1291 	    CardServices(RequestConfiguration, link->handle, &link->conf);
1292 	    if (link->open) {
1293 		do_reset(dev,1);
1294 		netif_device_attach(dev);
1295 	    }
1296 	}
1297 	break;
1298     }
1299     return 0;
1300 } /* xirc2ps_event */
1301 
1302 /*====================================================================*/
1303 
1304 /****************
1305  * This is the Interrupt service route.
1306  */
1307 static void
xirc2ps_interrupt(int irq,void * dev_id,struct pt_regs * regs)1308 xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1309 {
1310     struct net_device *dev = (struct net_device *)dev_id;
1311     local_info_t *lp = dev->priv;
1312     ioaddr_t ioaddr;
1313     u_char saved_page;
1314     unsigned bytes_rcvd;
1315     unsigned int_status, eth_status, rx_status, tx_status;
1316     unsigned rsr, pktlen;
1317     ulong start_ticks = jiffies; /* fixme: jiffies rollover every 497 days
1318 				  * is this something to worry about?
1319 				  * -- on a laptop?
1320 				  */
1321 
1322     if (!netif_device_present(dev))
1323 	return;
1324 
1325     ioaddr = dev->base_addr;
1326     if (lp->mohawk) { /* must disable the interrupt */
1327 	PutByte(XIRCREG_CR, 0);
1328     }
1329 
1330     DEBUG(6, "%s: interrupt %d at %#x.\n", dev->name, irq, ioaddr);
1331 
1332     saved_page = GetByte(XIRCREG_PR);
1333     /* Read the ISR to see whats the cause for the interrupt.
1334      * This also clears the interrupt flags on CE2 cards
1335      */
1336     int_status = GetByte(XIRCREG_ISR);
1337     bytes_rcvd = 0;
1338   loop_entry:
1339     if (int_status == 0xff) { /* card may be ejected */
1340 	DEBUG(3, "%s: interrupt %d for dead card\n", dev->name, irq);
1341 	goto leave;
1342     }
1343     eth_status = GetByte(XIRCREG_ESR);
1344 
1345     SelectPage(0x40);
1346     rx_status  = GetByte(XIRCREG40_RXST0);
1347     PutByte(XIRCREG40_RXST0, (~rx_status & 0xff));
1348     tx_status = GetByte(XIRCREG40_TXST0);
1349     tx_status |= GetByte(XIRCREG40_TXST1) << 8;
1350     PutByte(XIRCREG40_TXST0, 0);
1351     PutByte(XIRCREG40_TXST1, 0);
1352 
1353     DEBUG(3, "%s: ISR=%#2.2x ESR=%#2.2x RSR=%#2.2x TSR=%#4.4x\n",
1354 	  dev->name, int_status, eth_status, rx_status, tx_status);
1355 
1356     /***** receive section ******/
1357     SelectPage(0);
1358     while (eth_status & FullPktRcvd) {
1359 	rsr = GetByte(XIRCREG0_RSR);
1360 	if (bytes_rcvd > maxrx_bytes && (rsr & PktRxOk)) {
1361 	    /* too many bytes received during this int, drop the rest of the
1362 	     * packets */
1363 	    lp->stats.rx_dropped++;
1364 	    DEBUG(2, "%s: RX drop, too much done\n", dev->name);
1365 	} else if (rsr & PktRxOk) {
1366 	    struct sk_buff *skb;
1367 
1368 	    pktlen = GetWord(XIRCREG0_RBC);
1369 	    bytes_rcvd += pktlen;
1370 
1371 	    DEBUG(5, "rsr=%#02x packet_length=%u\n", rsr, pktlen);
1372 
1373 	    skb = dev_alloc_skb(pktlen+3); /* 1 extra so we can use insw */
1374 	    if (!skb) {
1375 		printk(KNOT_XIRC "low memory, packet dropped (size=%u)\n",
1376 		       pktlen);
1377 		lp->stats.rx_dropped++;
1378 	    } else { /* okay get the packet */
1379 		skb_reserve(skb, 2);
1380 		if (lp->silicon == 0 ) { /* work around a hardware bug */
1381 		    unsigned rhsa; /* receive start address */
1382 
1383 		    SelectPage(5);
1384 		    rhsa = GetWord(XIRCREG5_RHSA0);
1385 		    SelectPage(0);
1386 		    rhsa += 3; /* skip control infos */
1387 		    if (rhsa >= 0x8000)
1388 			rhsa = 0;
1389 		    if (rhsa + pktlen > 0x8000) {
1390 			unsigned i;
1391 			u_char *buf = skb_put(skb, pktlen);
1392 			for (i=0; i < pktlen ; i++, rhsa++) {
1393 			    buf[i] = GetByte(XIRCREG_EDP);
1394 			    if (rhsa == 0x8000) {
1395 				rhsa = 0;
1396 				i--;
1397 			    }
1398 			}
1399 		    } else {
1400 			insw(ioaddr+XIRCREG_EDP,
1401 				skb_put(skb, pktlen), (pktlen+1)>>1);
1402 		    }
1403 		}
1404 	      #if 0
1405 		else if (lp->mohawk) {
1406 		    /* To use this 32 bit access we should use
1407 		     * a manual optimized loop
1408 		     * Also the words are swapped, we can get more
1409 		     * performance by using 32 bit access and swapping
1410 		     * the words in a register. Will need this for cardbus
1411 		     *
1412 		     * Note: don't forget to change the ALLOC_SKB to .. +3
1413 		     */
1414 		    unsigned i;
1415 		    u_long *p = skb_put(skb, pktlen);
1416 		    register u_long a;
1417 		    ioaddr_t edpreg = ioaddr+XIRCREG_EDP-2;
1418 		    for (i=0; i < len ; i += 4, p++) {
1419 			a = inl(edpreg);
1420 			__asm__("rorl $16,%0\n\t"
1421 				:"=q" (a)
1422 				: "0" (a));
1423 			*p = a;
1424 		    }
1425 		}
1426 	      #endif
1427 		else {
1428 		    insw(ioaddr+XIRCREG_EDP, skb_put(skb, pktlen),
1429 			    (pktlen+1)>>1);
1430 		}
1431 		skb->protocol = eth_type_trans(skb, dev);
1432 		skb->dev = dev;
1433 		netif_rx(skb);
1434 		dev->last_rx = jiffies;
1435 		lp->stats.rx_packets++;
1436 		lp->stats.rx_bytes += pktlen;
1437 		if (!(rsr & PhyPkt))
1438 		    lp->stats.multicast++;
1439 	    }
1440 	} else { /* bad packet */
1441 	    DEBUG(5, "rsr=%#02x\n", rsr);
1442 	}
1443 	if (rsr & PktTooLong) {
1444 	    lp->stats.rx_frame_errors++;
1445 	    DEBUG(3, "%s: Packet too long\n", dev->name);
1446 	}
1447 	if (rsr & CRCErr) {
1448 	    lp->stats.rx_crc_errors++;
1449 	    DEBUG(3, "%s: CRC error\n", dev->name);
1450 	}
1451 	if (rsr & AlignErr) {
1452 	    lp->stats.rx_fifo_errors++; /* okay ? */
1453 	    DEBUG(3, "%s: Alignment error\n", dev->name);
1454 	}
1455 
1456 	/* clear the received/dropped/error packet */
1457 	PutWord(XIRCREG0_DO, 0x8000); /* issue cmd: skip_rx_packet */
1458 
1459 	/* get the new ethernet status */
1460 	eth_status = GetByte(XIRCREG_ESR);
1461     }
1462     if (rx_status & 0x10) { /* Receive overrun */
1463 	lp->stats.rx_over_errors++;
1464 	PutByte(XIRCREG_CR, ClearRxOvrun);
1465 	DEBUG(3, "receive overrun cleared\n");
1466     }
1467 
1468     /***** transmit section ******/
1469     if (int_status & PktTxed) {
1470 	unsigned n, nn;
1471 
1472 	n = lp->last_ptr_value;
1473 	nn = GetByte(XIRCREG0_PTR);
1474 	lp->last_ptr_value = nn;
1475 	if (nn < n) /* rollover */
1476 	    lp->stats.tx_packets += 256 - n;
1477 	else if (n == nn) { /* happens sometimes - don't know why */
1478 	    DEBUG(0, "PTR not changed?\n");
1479 	} else
1480 	    lp->stats.tx_packets += lp->last_ptr_value - n;
1481 	netif_wake_queue(dev);
1482     }
1483     if (tx_status & 0x0002) {	/* Execessive collissions */
1484 	DEBUG(0, "tx restarted due to execssive collissions\n");
1485 	PutByte(XIRCREG_CR, RestartTx);  /* restart transmitter process */
1486     }
1487     if (tx_status & 0x0040)
1488 	lp->stats.tx_aborted_errors++;
1489 
1490     /* recalculate our work chunk so that we limit the duration of this
1491      * ISR to about 1/10 of a second.
1492      * Calculate only if we received a reasonable amount of bytes.
1493      */
1494     if (bytes_rcvd > 1000) {
1495 	u_long duration = jiffies - start_ticks;
1496 
1497 	if (duration >= HZ/10) { /* if more than about 1/10 second */
1498 	    maxrx_bytes = (bytes_rcvd * (HZ/10)) / duration;
1499 	    if (maxrx_bytes < 2000)
1500 		maxrx_bytes = 2000;
1501 	    else if (maxrx_bytes > 22000)
1502 		maxrx_bytes = 22000;
1503 	    DEBUG(1, "set maxrx=%u (rcvd=%u ticks=%lu)\n",
1504 		  maxrx_bytes, bytes_rcvd, duration);
1505 	} else if (!duration && maxrx_bytes < 22000) {
1506 	    /* now much faster */
1507 	    maxrx_bytes += 2000;
1508 	    if (maxrx_bytes > 22000)
1509 		maxrx_bytes = 22000;
1510 	    DEBUG(1, "set maxrx=%u\n", maxrx_bytes);
1511 	}
1512     }
1513 
1514   leave:
1515     if (lockup_hack) {
1516 	if (int_status != 0xff && (int_status = GetByte(XIRCREG_ISR)) != 0)
1517 	    goto loop_entry;
1518     }
1519     SelectPage(saved_page);
1520     PutByte(XIRCREG_CR, EnableIntr);  /* re-enable interrupts */
1521     /* Instead of dropping packets during a receive, we could
1522      * force an interrupt with this command:
1523      *	  PutByte(XIRCREG_CR, EnableIntr|ForceIntr);
1524      */
1525 } /* xirc2ps_interrupt */
1526 
1527 /*====================================================================*/
1528 
1529 static void
do_tx_timeout(struct net_device * dev)1530 do_tx_timeout(struct net_device *dev)
1531 {
1532     local_info_t *lp = dev->priv;
1533     printk(KERN_NOTICE "%s: transmit timed out\n", dev->name);
1534     lp->stats.tx_errors++;
1535     /* reset the card */
1536     do_reset(dev,1);
1537     dev->trans_start = jiffies;
1538     netif_wake_queue(dev);
1539 }
1540 
1541 static int
do_start_xmit(struct sk_buff * skb,struct net_device * dev)1542 do_start_xmit(struct sk_buff *skb, struct net_device *dev)
1543 {
1544     local_info_t *lp = dev->priv;
1545     ioaddr_t ioaddr = dev->base_addr;
1546     int okay;
1547     unsigned freespace;
1548     unsigned pktlen = skb? skb->len : 0;
1549 
1550     DEBUG(1, "do_start_xmit(skb=%p, dev=%p) len=%u\n",
1551 	  skb, dev, pktlen);
1552 
1553 
1554     /* adjust the packet length to min. required
1555      * and hope that the buffer is large enough
1556      * to provide some random data.
1557      * fixme: For Mohawk we can change this by sending
1558      * a larger packetlen than we actually have; the chip will
1559      * pad this in his buffer with random bytes
1560      */
1561     if (pktlen < ETH_ZLEN)
1562     {
1563         skb = skb_padto(skb, ETH_ZLEN);
1564         if(skb == NULL)
1565         	return 0;
1566 	pktlen = ETH_ZLEN;
1567     }
1568 
1569     netif_stop_queue(dev);
1570     SelectPage(0);
1571     PutWord(XIRCREG0_TRS, (u_short)pktlen+2);
1572     freespace = GetWord(XIRCREG0_TSO);
1573     okay = freespace & 0x8000;
1574     freespace &= 0x7fff;
1575     /* TRS doesn't work - (indeed it is eliminated with sil-rev 1) */
1576     okay = pktlen +2 < freespace;
1577     DEBUG(2 + (okay ? 2 : 0), "%s: avail. tx space=%u%s\n",
1578 	  dev->name, freespace, okay ? " (okay)":" (not enough)");
1579     if (!okay) { /* not enough space */
1580 	return 1;  /* upper layer may decide to requeue this packet */
1581     }
1582     /* send the packet */
1583     PutWord(XIRCREG_EDP, (u_short)pktlen);
1584     outsw(ioaddr+XIRCREG_EDP, skb->data, pktlen>>1);
1585     if (pktlen & 1)
1586 	PutByte(XIRCREG_EDP, skb->data[pktlen-1]);
1587 
1588     if (lp->mohawk)
1589 	PutByte(XIRCREG_CR, TransmitPacket|EnableIntr);
1590 
1591     dev_kfree_skb (skb);
1592     dev->trans_start = jiffies;
1593     lp->stats.tx_bytes += pktlen;
1594     netif_start_queue(dev);
1595     return 0;
1596 }
1597 
1598 static struct net_device_stats *
do_get_stats(struct net_device * dev)1599 do_get_stats(struct net_device *dev)
1600 {
1601     local_info_t *lp = dev->priv;
1602 
1603     /*	lp->stats.rx_missed_errors = GetByte(?) */
1604     return &lp->stats;
1605 }
1606 
1607 /****************
1608  * Set all addresses: This first one is the individual address,
1609  * the next 9 addresses are taken from the multicast list and
1610  * the rest is filled with the individual address.
1611  */
1612 static void
set_addresses(struct net_device * dev)1613 set_addresses(struct net_device *dev)
1614 {
1615     ioaddr_t ioaddr = dev->base_addr;
1616     local_info_t *lp = dev->priv;
1617     struct dev_mc_list *dmi = dev->mc_list;
1618     char *addr;
1619     int i,j,k,n;
1620 
1621     SelectPage(k=0x50);
1622     for (i=0,j=8,n=0; ; i++, j++) {
1623 	if (i > 5) {
1624 	    if (++n > 9)
1625 		break;
1626 	    i = 0;
1627 	}
1628 	if (j > 15) {
1629 	    j = 8;
1630 	    k++;
1631 	    SelectPage(k);
1632 	}
1633 
1634 	if (n && n <= dev->mc_count && dmi) {
1635 	    addr = dmi->dmi_addr;
1636 	    dmi = dmi->next;
1637 	} else
1638 	    addr = dev->dev_addr;
1639 
1640 	if (lp->mohawk)
1641 	    PutByte(j, addr[5-i]);
1642 	else
1643 	    PutByte(j, addr[i]);
1644     }
1645     SelectPage(0);
1646 }
1647 
1648 /****************
1649  * Set or clear the multicast filter for this adaptor.
1650  * We can filter up to 9 addresses, if more are requested we set
1651  * multicast promiscuous mode.
1652  */
1653 
1654 static void
set_multicast_list(struct net_device * dev)1655 set_multicast_list(struct net_device *dev)
1656 {
1657     ioaddr_t ioaddr = dev->base_addr;
1658 
1659     SelectPage(0x42);
1660     if (dev->flags & IFF_PROMISC) { /* snoop */
1661 	PutByte(XIRCREG42_SWC1, 0x06); /* set MPE and PME */
1662     } else if (dev->mc_count > 9 || (dev->flags & IFF_ALLMULTI)) {
1663 	PutByte(XIRCREG42_SWC1, 0x06); /* set MPE */
1664     } else if (dev->mc_count) {
1665 	/* the chip can filter 9 addresses perfectly */
1666 	PutByte(XIRCREG42_SWC1, 0x00);
1667 	SelectPage(0x40);
1668 	PutByte(XIRCREG40_CMD0, Offline);
1669 	set_addresses(dev);
1670 	SelectPage(0x40);
1671 	PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1672     } else { /* standard usage */
1673 	PutByte(XIRCREG42_SWC1, 0x00);
1674     }
1675     SelectPage(0);
1676 }
1677 
1678 static int
do_config(struct net_device * dev,struct ifmap * map)1679 do_config(struct net_device *dev, struct ifmap *map)
1680 {
1681     local_info_t *local = dev->priv;
1682 
1683     DEBUG(0, "do_config(%p)\n", dev);
1684     if (map->port != 255 && map->port != dev->if_port) {
1685 	if (map->port > 4)
1686 	    return -EINVAL;
1687 	if (!map->port) {
1688 	    local->probe_port = 1;
1689 	    dev->if_port = 1;
1690 	} else {
1691 	    local->probe_port = 0;
1692 	    dev->if_port = map->port;
1693 	}
1694 	printk(KERN_INFO "%s: switching to %s port\n",
1695 	       dev->name, if_names[dev->if_port]);
1696 	do_reset(dev,1);  /* not the fine way :-) */
1697     }
1698     return 0;
1699 }
1700 
1701 /****************
1702  * Open the driver
1703  */
1704 static int
do_open(struct net_device * dev)1705 do_open(struct net_device *dev)
1706 {
1707     local_info_t *lp = dev->priv;
1708     dev_link_t *link = &lp->link;
1709 
1710     DEBUG(0, "do_open(%p)\n", dev);
1711 
1712     /* Check that the PCMCIA card is still here. */
1713     /* Physical device present signature. */
1714     if (!DEV_OK(link))
1715 	return -ENODEV;
1716 
1717     /* okay */
1718     link->open++;
1719     MOD_INC_USE_COUNT;
1720 
1721     netif_start_queue(dev);
1722     do_reset(dev,1);
1723 
1724     return 0;
1725 }
1726 
netdev_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1727 static void netdev_get_drvinfo(struct net_device *dev,
1728 			       struct ethtool_drvinfo *info)
1729 {
1730 	strcpy(info->driver, "xirc2ps_cs");
1731 }
1732 
1733 static struct ethtool_ops netdev_ethtool_ops = {
1734 	.get_drvinfo		= netdev_get_drvinfo,
1735 };
1736 
1737 static int
do_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)1738 do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1739 {
1740     local_info_t *local = dev->priv;
1741     ioaddr_t ioaddr = dev->base_addr;
1742     u16 *data = (u16 *)&rq->ifr_data;
1743 
1744     DEBUG(1, "%s: ioctl(%-.6s, %#04x) %04x %04x %04x %04x\n",
1745 	  dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1746 	  data[0], data[1], data[2], data[3]);
1747 
1748     if (!local->mohawk)
1749 	return -EOPNOTSUPP;
1750 
1751     switch(cmd) {
1752       case SIOCGMIIPHY:		/* Get the address of the PHY in use. */
1753       case SIOCDEVPRIVATE:
1754 	data[0] = 0;		/* we have only this address */
1755 	/* fall trough */
1756       case SIOCGMIIREG:		/* Read the specified MII register. */
1757       case SIOCDEVPRIVATE+1:
1758 	data[3] = mii_rd(ioaddr, data[0] & 0x1f, data[1] & 0x1f);
1759 	break;
1760       case SIOCSMIIREG:		/* Write the specified MII register */
1761       case SIOCDEVPRIVATE+2:
1762 	if (!capable(CAP_NET_ADMIN))
1763 	    return -EPERM;
1764 	mii_wr(ioaddr, data[0] & 0x1f, data[1] & 0x1f, data[2], 16);
1765 	break;
1766       default:
1767 	return -EOPNOTSUPP;
1768     }
1769     return 0;
1770 }
1771 
1772 static void
hardreset(struct net_device * dev)1773 hardreset(struct net_device *dev)
1774 {
1775     local_info_t *local = dev->priv;
1776     ioaddr_t ioaddr = dev->base_addr;
1777 
1778     SelectPage(4);
1779     udelay(1);
1780     PutByte(XIRCREG4_GPR1, 0);	     /* clear bit 0: power down */
1781     busy_loop(HZ/25);		     /* wait 40 msec */
1782     if (local->mohawk)
1783 	PutByte(XIRCREG4_GPR1, 1);	 /* set bit 0: power up */
1784     else
1785 	PutByte(XIRCREG4_GPR1, 1 | 4);	 /* set bit 0: power up, bit 2: AIC */
1786     busy_loop(HZ/50);		     /* wait 20 msec */
1787 }
1788 
1789 static void
do_reset(struct net_device * dev,int full)1790 do_reset(struct net_device *dev, int full)
1791 {
1792     local_info_t *local = dev->priv;
1793     ioaddr_t ioaddr = dev->base_addr;
1794     unsigned value;
1795 
1796     DEBUG(0, "%s: do_reset(%p,%d)\n", dev? dev->name:"eth?", dev, full);
1797 
1798     hardreset(dev);
1799     PutByte(XIRCREG_CR, SoftReset); /* set */
1800     busy_loop(HZ/50);		     /* wait 20 msec */
1801     PutByte(XIRCREG_CR, 0);	     /* clear */
1802     busy_loop(HZ/25);		     /* wait 40 msec */
1803     if (local->mohawk) {
1804 	SelectPage(4);
1805 	/* set pin GP1 and GP2 to output  (0x0c)
1806 	 * set GP1 to low to power up the ML6692 (0x00)
1807 	 * set GP2 to high to power up the 10Mhz chip  (0x02)
1808 	 */
1809 	PutByte(XIRCREG4_GPR0, 0x0e);
1810     }
1811 
1812     /* give the circuits some time to power up */
1813     busy_loop(HZ/2);		/* about 500ms */
1814 
1815     local->last_ptr_value = 0;
1816     local->silicon = local->mohawk ? (GetByte(XIRCREG4_BOV) & 0x70) >> 4
1817 				   : (GetByte(XIRCREG4_BOV) & 0x30) >> 4;
1818 
1819     if (local->probe_port) {
1820 	if (!local->mohawk) {
1821 	    SelectPage(4);
1822 	    PutByte(XIRCREG4_GPR0, 4);
1823 	    local->probe_port = 0;
1824 	}
1825     } else if (dev->if_port == 2) { /* enable 10Base2 */
1826 	SelectPage(0x42);
1827 	PutByte(XIRCREG42_SWC1, 0xC0);
1828     } else { /* enable 10BaseT */
1829 	SelectPage(0x42);
1830 	PutByte(XIRCREG42_SWC1, 0x80);
1831     }
1832     busy_loop(HZ/25);		     /* wait 40 msec to let it complete */
1833 
1834   #ifdef PCMCIA_DEBUG
1835     if (pc_debug) {
1836 	SelectPage(0);
1837 	value = GetByte(XIRCREG_ESR);	 /* read the ESR */
1838 	printk(KERN_DEBUG "%s: ESR is: %#02x\n", dev->name, value);
1839     }
1840   #endif
1841 
1842     /* setup the ECR */
1843     SelectPage(1);
1844     PutByte(XIRCREG1_IMR0, 0xff); /* allow all ints */
1845     PutByte(XIRCREG1_IMR1, 1	); /* and Set TxUnderrunDetect */
1846     value = GetByte(XIRCREG1_ECR);
1847   #if 0
1848     if (local->mohawk)
1849 	value |= DisableLinkPulse;
1850     PutByte(XIRCREG1_ECR, value);
1851   #endif
1852     DEBUG(0, "%s: ECR is: %#02x\n", dev->name, value);
1853 
1854     SelectPage(0x42);
1855     PutByte(XIRCREG42_SWC0, 0x20); /* disable source insertion */
1856 
1857     if (local->silicon != 1) {
1858 	/* set the local memory dividing line.
1859 	 * The comments in the sample code say that this is only
1860 	 * settable with the scipper version 2 which is revision 0.
1861 	 * Always for CE3 cards
1862 	 */
1863 	SelectPage(2);
1864 	PutWord(XIRCREG2_RBS, 0x2000);
1865     }
1866 
1867     if (full)
1868 	set_addresses(dev);
1869 
1870     /* Hardware workaround:
1871      * The receive byte pointer after reset is off by 1 so we need
1872      * to move the offset pointer back to 0.
1873      */
1874     SelectPage(0);
1875     PutWord(XIRCREG0_DO, 0x2000); /* change offset command, off=0 */
1876 
1877     /* setup MAC IMRs and clear status registers */
1878     SelectPage(0x40);		     /* Bit 7 ... bit 0 */
1879     PutByte(XIRCREG40_RMASK0, 0xff); /* ROK, RAB, rsv, RO, CRC, AE, PTL, MP */
1880     PutByte(XIRCREG40_TMASK0, 0xff); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1881     PutByte(XIRCREG40_TMASK1, 0xb0); /* rsv, rsv, PTD, EXT, rsv,rsv,rsv, rsv*/
1882     PutByte(XIRCREG40_RXST0,  0x00); /* ROK, RAB, REN, RO, CRC, AE, PTL, MP */
1883     PutByte(XIRCREG40_TXST0,  0x00); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1884     PutByte(XIRCREG40_TXST1,  0x00); /* TEN, rsv, PTD, EXT, retry_counter:4  */
1885 
1886     if (full && local->mohawk && init_mii(dev)) {
1887 	if (dev->if_port == 4 || local->dingo || local->new_mii) {
1888 	    printk(KERN_INFO "%s: MII selected\n", dev->name);
1889 	    SelectPage(2);
1890 	    PutByte(XIRCREG2_MSR, GetByte(XIRCREG2_MSR) | 0x08);
1891 	    busy_loop(HZ/50);
1892 	} else {
1893 	    printk(KERN_INFO "%s: MII detected; using 10mbs\n",
1894 		   dev->name);
1895 	    SelectPage(0x42);
1896 	    if (dev->if_port == 2) /* enable 10Base2 */
1897 		PutByte(XIRCREG42_SWC1, 0xC0);
1898 	    else  /* enable 10BaseT */
1899 		PutByte(XIRCREG42_SWC1, 0x80);
1900 	    busy_loop(HZ/25);	/* wait 40 msec to let it complete */
1901 	}
1902 	if (full_duplex)
1903 	    PutByte(XIRCREG1_ECR, GetByte(XIRCREG1_ECR | FullDuplex));
1904     } else {  /* No MII */
1905 	SelectPage(0);
1906 	value = GetByte(XIRCREG_ESR);	 /* read the ESR */
1907 	dev->if_port = (value & MediaSelect) ? 1 : 2;
1908     }
1909 
1910     /* configure the LEDs */
1911     SelectPage(2);
1912     if (dev->if_port == 1 || dev->if_port == 4) /* TP: Link and Activity */
1913 	PutByte(XIRCREG2_LED, 0x3b);
1914     else			      /* Coax: Not-Collision and Activity */
1915 	PutByte(XIRCREG2_LED, 0x3a);
1916 
1917     if (local->dingo)
1918 	PutByte(0x0b, 0x04); /* 100 Mbit LED */
1919 
1920     /* enable receiver and put the mac online */
1921     if (full) {
1922 	SelectPage(0x40);
1923 	PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1924     }
1925 
1926     /* setup Ethernet IMR and enable interrupts */
1927     SelectPage(1);
1928     PutByte(XIRCREG1_IMR0, 0xff);
1929     udelay(1);
1930     SelectPage(0);
1931     PutByte(XIRCREG_CR, EnableIntr);
1932     if (local->modem && !local->dingo) { /* do some magic */
1933 	if (!(GetByte(0x10) & 0x01))
1934 	    PutByte(0x10, 0x11); /* unmask master-int bit */
1935     }
1936 
1937     if (full)
1938 	printk(KERN_INFO "%s: media %s, silicon revision %d\n",
1939 	       dev->name, if_names[dev->if_port], local->silicon);
1940     /* We should switch back to page 0 to avoid a bug in revision 0
1941      * where regs with offset below 8 can't be read after an access
1942      * to the MAC registers */
1943     SelectPage(0);
1944 }
1945 
1946 /****************
1947  * Initialize the Media-Independent-Interface
1948  * Returns: True if we have a good MII
1949  */
1950 static int
init_mii(struct net_device * dev)1951 init_mii(struct net_device *dev)
1952 {
1953     local_info_t *local = dev->priv;
1954     ioaddr_t ioaddr = dev->base_addr;
1955     unsigned control, status, linkpartner;
1956     int i;
1957 
1958     if (if_port == 4 || if_port == 1) { /* force 100BaseT or 10BaseT */
1959 	dev->if_port = if_port;
1960 	local->probe_port = 0;
1961 	return 1;
1962     }
1963 
1964     status = mii_rd(ioaddr,  0, 1);
1965     if ((status & 0xff00) != 0x7800)
1966 	return 0; /* No MII */
1967 
1968     local->new_mii = (mii_rd(ioaddr, 0, 2) != 0xffff);
1969 
1970     if (local->probe_port)
1971 	control = 0x1000; /* auto neg */
1972     else if (dev->if_port == 4)
1973 	control = 0x2000; /* no auto neg, 100mbs mode */
1974     else
1975 	control = 0x0000; /* no auto neg, 10mbs mode */
1976     mii_wr(ioaddr,  0, 0, control, 16);
1977     udelay(100);
1978     control = mii_rd(ioaddr, 0, 0);
1979 
1980     if (control & 0x0400) {
1981 	printk(KERN_NOTICE "%s can't take PHY out of isolation mode\n",
1982 	       dev->name);
1983 	local->probe_port = 0;
1984 	return 0;
1985     }
1986 
1987     if (local->probe_port) {
1988 	/* according to the DP83840A specs the auto negotiation process
1989 	 * may take up to 3.5 sec, so we use this also for our ML6692
1990 	 * Fixme: Better to use a timer here!
1991 	 */
1992 	for (i=0; i < 35; i++) {
1993 	    busy_loop(HZ/10);	 /* wait 100 msec */
1994 	    status = mii_rd(ioaddr,  0, 1);
1995 	    if ((status & 0x0020) && (status & 0x0004))
1996 		break;
1997 	}
1998 
1999 	if (!(status & 0x0020)) {
2000 	    printk(KERN_INFO "%s: autonegotiation failed;"
2001 		   " using 10mbs\n", dev->name);
2002 	    if (!local->new_mii) {
2003 		control = 0x0000;
2004 		mii_wr(ioaddr,  0, 0, control, 16);
2005 		udelay(100);
2006 		SelectPage(0);
2007 		dev->if_port = (GetByte(XIRCREG_ESR) & MediaSelect) ? 1 : 2;
2008 	    }
2009 	} else {
2010 	    linkpartner = mii_rd(ioaddr, 0, 5);
2011 	    printk(KERN_INFO "%s: MII link partner: %04x\n",
2012 		   dev->name, linkpartner);
2013 	    if (linkpartner & 0x0080) {
2014 		dev->if_port = 4;
2015 	    } else
2016 		dev->if_port = 1;
2017 	}
2018     }
2019 
2020     return 1;
2021 }
2022 
2023 static void
do_powerdown(struct net_device * dev)2024 do_powerdown(struct net_device *dev)
2025 {
2026 
2027     ioaddr_t ioaddr = dev->base_addr;
2028 
2029     DEBUG(0, "do_powerdown(%p)\n", dev);
2030 
2031     SelectPage(4);
2032     PutByte(XIRCREG4_GPR1, 0);	     /* clear bit 0: power down */
2033     SelectPage(0);
2034 }
2035 
2036 static int
do_stop(struct net_device * dev)2037 do_stop(struct net_device *dev)
2038 {
2039     ioaddr_t ioaddr = dev->base_addr;
2040     local_info_t *lp = dev->priv;
2041     dev_link_t *link = &lp->link;
2042 
2043     DEBUG(0, "do_stop(%p)\n", dev);
2044 
2045     if (!link)
2046 	return -ENODEV;
2047 
2048     netif_stop_queue(dev);
2049 
2050     SelectPage(0);
2051     PutByte(XIRCREG_CR, 0);  /* disable interrupts */
2052     SelectPage(0x01);
2053     PutByte(XIRCREG1_IMR0, 0x00); /* forbid all ints */
2054     SelectPage(4);
2055     PutByte(XIRCREG4_GPR1, 0);	/* clear bit 0: power down */
2056     SelectPage(0);
2057 
2058     link->open--;
2059     if (link->state & DEV_STALE_CONFIG)
2060 	mod_timer(&link->release, jiffies + HZ/20);
2061 
2062     MOD_DEC_USE_COUNT;
2063 
2064     return 0;
2065 }
2066 
2067 static int __init
init_xirc2ps_cs(void)2068 init_xirc2ps_cs(void)
2069 {
2070     servinfo_t serv;
2071 
2072     printk(KERN_INFO "%s\n", version);
2073     if (lockup_hack)
2074 	printk(KINF_XIRC "lockup hack is enabled\n");
2075     CardServices(GetCardServicesInfo, &serv);
2076     if (serv.Revision != CS_RELEASE_CODE) {
2077 	printk(KNOT_XIRC "Card Services release does not match!\n");
2078 	return -1;
2079     }
2080     DEBUG(0, "pc_debug=%d\n", pc_debug);
2081     register_pccard_driver(&dev_info, &xirc2ps_attach, &xirc2ps_detach);
2082     return 0;
2083 }
2084 
2085 static void __exit
exit_xirc2ps_cs(void)2086 exit_xirc2ps_cs(void)
2087 {
2088     DEBUG(0, "unloading\n");
2089     unregister_pccard_driver(&dev_info);
2090     while (dev_list) {
2091 	if (dev_list->state & DEV_CONFIG)
2092 	    xirc2ps_release((u_long)dev_list);
2093 	if (dev_list)	/* xirc2ps_release() might already have detached... */
2094 	    xirc2ps_detach(dev_list);
2095     }
2096 }
2097 
2098 module_init(init_xirc2ps_cs);
2099 module_exit(exit_xirc2ps_cs);
2100 
2101 #ifndef MODULE
setup_xirc2ps_cs(char * str)2102 static int __init setup_xirc2ps_cs(char *str)
2103 {
2104 	/* irq, irq_mask, if_port, full_duplex, do_sound, lockup_hack
2105 	 * [,irq2 [,irq3 [,irq4]]]
2106 	 */
2107 	int ints[10] = { -1 };
2108 
2109 	str = get_options(str, 9, ints);
2110 
2111 #define MAYBE_SET(X,Y) if (ints[0] >= Y && ints[Y] != -1) { X = ints[Y]; }
2112 	MAYBE_SET(irq_list[0], 1);
2113 	MAYBE_SET(irq_mask, 2);
2114 	MAYBE_SET(if_port, 3);
2115 	MAYBE_SET(full_duplex, 4);
2116 	MAYBE_SET(do_sound, 5);
2117 	MAYBE_SET(lockup_hack, 6);
2118 	MAYBE_SET(irq_list[1], 7);
2119 	MAYBE_SET(irq_list[2], 8);
2120 	MAYBE_SET(irq_list[3], 9);
2121 #undef  MAYBE_SET
2122 
2123 	return 0;
2124 }
2125 
2126 __setup("xirc2ps_cs=", setup_xirc2ps_cs);
2127 #endif
2128