[PATCH] cs89x0: use u16 for device register data
[pandora-kernel.git] / drivers / net / cs89x0.c
1 /* cs89x0.c: A Crystal Semiconductor (Now Cirrus Logic) CS89[02]0
2  *  driver for linux.
3  */
4
5 /*
6         Written 1996 by Russell Nelson, with reference to skeleton.c
7         written 1993-1994 by Donald Becker.
8
9         This software may be used and distributed according to the terms
10         of the GNU General Public License, incorporated herein by reference.
11
12         The author may be reached at nelson@crynwr.com, Crynwr
13         Software, 521 Pleasant Valley Rd., Potsdam, NY 13676
14
15   Changelog:
16
17   Mike Cruse        : mcruse@cti-ltd.com
18                     : Changes for Linux 2.0 compatibility. 
19                     : Added dev_id parameter in net_interrupt(),
20                     : request_irq() and free_irq(). Just NULL for now.
21
22   Mike Cruse        : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
23                     : in net_open() and net_close() so kerneld would know
24                     : that the module is in use and wouldn't eject the 
25                     : driver prematurely.
26
27   Mike Cruse        : Rewrote init_module() and cleanup_module using 8390.c
28                     : as an example. Disabled autoprobing in init_module(),
29                     : not a good thing to do to other devices while Linux
30                     : is running from all accounts.
31
32   Russ Nelson       : Jul 13 1998.  Added RxOnly DMA support.
33
34   Melody Lee        : Aug 10 1999.  Changes for Linux 2.2.5 compatibility. 
35                     : email: ethernet@crystal.cirrus.com
36
37   Alan Cox          : Removed 1.2 support, added 2.1 extra counters.
38
39   Andrew Morton     : andrewm@uow.edu.au
40                     : Kernel 2.3.48
41                     : Handle kmalloc() failures
42                     : Other resource allocation fixes
43                     : Add SMP locks
44                     : Integrate Russ Nelson's ALLOW_DMA functionality back in.
45                     : If ALLOW_DMA is true, make DMA runtime selectable
46                     : Folded in changes from Cirrus (Melody Lee
47                     : <klee@crystal.cirrus.com>)
48                     : Don't call netif_wake_queue() in net_send_packet()
49                     : Fixed an out-of-mem bug in dma_rx()
50                     : Updated Documentation/networking/cs89x0.txt
51
52   Andrew Morton     : andrewm@uow.edu.au / Kernel 2.3.99-pre1
53                     : Use skb_reserve to longword align IP header (two places)
54                     : Remove a delay loop from dma_rx()
55                     : Replace '100' with HZ
56                     : Clean up a couple of skb API abuses
57                     : Added 'cs89x0_dma=N' kernel boot option
58                     : Correctly initialise lp->lock in non-module compile
59
60   Andrew Morton     : andrewm@uow.edu.au / Kernel 2.3.99-pre4-1
61                     : MOD_INC/DEC race fix (see
62                     : http://www.uwsg.indiana.edu/hypermail/linux/kernel/0003.3/1532.html)
63
64   Andrew Morton     : andrewm@uow.edu.au / Kernel 2.4.0-test7-pre2
65                     : Enhanced EEPROM support to cover more devices,
66                     :   abstracted IRQ mapping to support CONFIG_ARCH_CLPS7500 arch
67                     :   (Jason Gunthorpe <jgg@ualberta.ca>)
68
69   Andrew Morton     : Kernel 2.4.0-test11-pre4
70                     : Use dev->name in request_*() (Andrey Panin)
71                     : Fix an error-path memleak in init_module()
72                     : Preserve return value from request_irq()
73                     : Fix type of `media' module parm (Keith Owens)
74                     : Use SET_MODULE_OWNER()
75                     : Tidied up strange request_irq() abuse in net_open().
76
77   Andrew Morton     : Kernel 2.4.3-pre1
78                     : Request correct number of pages for DMA (Hugh Dickens)
79                     : Select PP_ChipID _after_ unregister_netdev in cleanup_module()
80                     :  because unregister_netdev() calls get_stats.
81                     : Make `version[]' __initdata
82                     : Uninlined the read/write reg/word functions.
83
84   Oskar Schirmer    : oskar@scara.com
85                     : HiCO.SH4 (superh) support added (irq#1, cs89x0_media=)
86
87   Deepak Saxena     : dsaxena@plexity.net
88                     : Intel IXDP2x01 (XScale ixp2x00 NPU) platform support
89
90   Dmitry Pervushin  : dpervushin@ru.mvista.com
91                     : PNX010X platform support
92
93 */
94
95 /* Always include 'config.h' first in case the user wants to turn on
96    or override something. */
97 #include <linux/config.h>
98 #include <linux/module.h>
99
100 /*
101  * Set this to zero to disable DMA code
102  *
103  * Note that even if DMA is turned off we still support the 'dma' and  'use_dma'
104  * module options so we don't break any startup scripts.
105  */
106 #ifndef CONFIG_ISA_DMA_API
107 #define ALLOW_DMA       0
108 #else
109 #define ALLOW_DMA       1
110 #endif
111
112 /*
113  * Set this to zero to remove all the debug statements via
114  * dead code elimination
115  */
116 #define DEBUGGING       1
117
118 /*
119   Sources:
120
121         Crynwr packet driver epktisa.
122
123         Crystal Semiconductor data sheets.
124
125 */
126
127 #include <linux/errno.h>
128 #include <linux/netdevice.h>
129 #include <linux/etherdevice.h>
130 #include <linux/kernel.h>
131 #include <linux/types.h>
132 #include <linux/fcntl.h>
133 #include <linux/interrupt.h>
134 #include <linux/ioport.h>
135 #include <linux/in.h>
136 #include <linux/skbuff.h>
137 #include <linux/slab.h>
138 #include <linux/spinlock.h>
139 #include <linux/string.h>
140 #include <linux/init.h>
141 #include <linux/bitops.h>
142 #include <linux/delay.h>
143
144 #include <asm/system.h>
145 #include <asm/io.h>
146 #include <asm/irq.h>
147 #if ALLOW_DMA
148 #include <asm/dma.h>
149 #endif
150
151 #include "cs89x0.h"
152
153 static char version[] __initdata =
154 "cs89x0.c: v2.4.3-pre1 Russell Nelson <nelson@crynwr.com>, Andrew Morton <andrewm@uow.edu.au>\n";
155
156 #define DRV_NAME "cs89x0"
157
158 /* First, a few definitions that the brave might change.
159    A zero-terminated list of I/O addresses to be probed. Some special flags..
160       Addr & 1 = Read back the address port, look for signature and reset
161                  the page window before probing 
162       Addr & 3 = Reset the page window and probe 
163    The CLPS eval board has the Cirrus chip at 0x80090300, in ARM IO space,
164    but it is possible that a Cirrus board could be plugged into the ISA
165    slots. */
166 /* The cs8900 has 4 IRQ pins, software selectable. cs8900_irq_map maps 
167    them to system IRQ numbers. This mapping is card specific and is set to
168    the configuration of the Cirrus Eval board for this chip. */
169 #ifdef CONFIG_ARCH_CLPS7500
170 static unsigned int netcard_portlist[] __initdata =
171    { 0x80090303, 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
172 static unsigned int cs8900_irq_map[] = {12,0,0,0};
173 #elif defined(CONFIG_SH_HICOSH4)
174 static unsigned int netcard_portlist[] __initdata =
175    { 0x0300, 0};
176 static unsigned int cs8900_irq_map[] = {1,0,0,0};
177 #elif defined(CONFIG_ARCH_IXDP2X01)
178 #include <asm/irq.h>
179 static unsigned int netcard_portlist[] __initdata = {IXDP2X01_CS8900_VIRT_BASE, 0};
180 static unsigned int cs8900_irq_map[] = {IRQ_IXDP2X01_CS8900, 0, 0, 0};
181 #elif defined(CONFIG_ARCH_PNX010X)
182 #include <asm/irq.h>
183 #include <asm/arch/gpio.h>
184 #define CIRRUS_DEFAULT_BASE     IO_ADDRESS(EXT_STATIC2_s0_BASE + 0x200000)      /* = Physical address 0x48200000 */
185 #define CIRRUS_DEFAULT_IRQ      VH_INTC_INT_NUM_CASCADED_INTERRUPT_1 /* Event inputs bank 1 - ID 35/bit 3 */
186 static unsigned int netcard_portlist[] __initdata = {CIRRUS_DEFAULT_BASE, 0};
187 static unsigned int cs8900_irq_map[] = {CIRRUS_DEFAULT_IRQ, 0, 0, 0};
188 #else
189 static unsigned int netcard_portlist[] __initdata =
190    { 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
191 static unsigned int cs8900_irq_map[] = {10,11,12,5};
192 #endif
193
194 #if DEBUGGING
195 static unsigned int net_debug = DEBUGGING;
196 #else
197 #define net_debug 0     /* gcc will remove all the debug code for us */
198 #endif
199
200 /* The number of low I/O ports used by the ethercard. */
201 #define NETCARD_IO_EXTENT       16
202
203 /* we allow the user to override various values normally set in the EEPROM */
204 #define FORCE_RJ45      0x0001    /* pick one of these three */
205 #define FORCE_AUI       0x0002
206 #define FORCE_BNC       0x0004
207
208 #define FORCE_AUTO      0x0010    /* pick one of these three */
209 #define FORCE_HALF      0x0020
210 #define FORCE_FULL      0x0030
211
212 /* Information that need to be kept for each board. */
213 struct net_local {
214         struct net_device_stats stats;
215         int chip_type;          /* one of: CS8900, CS8920, CS8920M */
216         char chip_revision;     /* revision letter of the chip ('A'...) */
217         int send_cmd;           /* the proper send command: TX_NOW, TX_AFTER_381, or TX_AFTER_ALL */
218         int auto_neg_cnf;       /* auto-negotiation word from EEPROM */
219         int adapter_cnf;        /* adapter configuration from EEPROM */
220         int isa_config;         /* ISA configuration from EEPROM */
221         int irq_map;            /* IRQ map from EEPROM */
222         int rx_mode;            /* what mode are we in? 0, RX_MULTCAST_ACCEPT, or RX_ALL_ACCEPT */
223         int curr_rx_cfg;        /* a copy of PP_RxCFG */
224         int linectl;            /* either 0 or LOW_RX_SQUELCH, depending on configuration. */
225         int send_underrun;      /* keep track of how many underruns in a row we get */
226         int force;              /* force various values; see FORCE* above. */
227         spinlock_t lock;
228 #if ALLOW_DMA
229         int use_dma;            /* Flag: we're using dma */
230         int dma;                /* DMA channel */
231         int dmasize;            /* 16 or 64 */
232         unsigned char *dma_buff;        /* points to the beginning of the buffer */
233         unsigned char *end_dma_buff;    /* points to the end of the buffer */
234         unsigned char *rx_dma_ptr;      /* points to the next packet  */
235 #endif
236 };
237
238 /* Index to functions, as function prototypes. */
239
240 static int cs89x0_probe1(struct net_device *dev, int ioaddr, int modular);
241 static int net_open(struct net_device *dev);
242 static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
243 static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
244 static void set_multicast_list(struct net_device *dev);
245 static void net_timeout(struct net_device *dev);
246 static void net_rx(struct net_device *dev);
247 static int net_close(struct net_device *dev);
248 static struct net_device_stats *net_get_stats(struct net_device *dev);
249 static void reset_chip(struct net_device *dev);
250 static int get_eeprom_data(struct net_device *dev, int off, int len, int *buffer);
251 static int get_eeprom_cksum(int off, int len, int *buffer);
252 static int set_mac_address(struct net_device *dev, void *addr);
253 static void count_rx_errors(int status, struct net_local *lp);
254 #ifdef CONFIG_NET_POLL_CONTROLLER
255 static void net_poll_controller(struct net_device *dev);
256 #endif
257 #if ALLOW_DMA
258 static void get_dma_channel(struct net_device *dev);
259 static void release_dma_buff(struct net_local *lp);
260 #endif
261
262 /* Example routines you must write ;->. */
263 #define tx_done(dev) 1
264
265 /*
266  * Permit 'cs89x0_dma=N' in the kernel boot environment
267  */
268 #if !defined(MODULE) && (ALLOW_DMA != 0)
269 static int g_cs89x0_dma;
270
271 static int __init dma_fn(char *str)
272 {
273         g_cs89x0_dma = simple_strtol(str,NULL,0);
274         return 1;
275 }
276
277 __setup("cs89x0_dma=", dma_fn);
278 #endif  /* !defined(MODULE) && (ALLOW_DMA != 0) */
279
280 #ifndef MODULE
281 static int g_cs89x0_media__force;
282
283 static int __init media_fn(char *str)
284 {
285         if (!strcmp(str, "rj45")) g_cs89x0_media__force = FORCE_RJ45;
286         else if (!strcmp(str, "aui")) g_cs89x0_media__force = FORCE_AUI;
287         else if (!strcmp(str, "bnc")) g_cs89x0_media__force = FORCE_BNC;
288         return 1;
289 }
290
291 __setup("cs89x0_media=", media_fn);
292
293 \f
294 /* Check for a network adaptor of this type, and return '0' iff one exists.
295    If dev->base_addr == 0, probe all likely locations.
296    If dev->base_addr == 1, always return failure.
297    If dev->base_addr == 2, allocate space for the device and return success
298    (detachable devices only).
299    Return 0 on success.
300    */
301
302 struct net_device * __init cs89x0_probe(int unit)
303 {
304         struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
305         unsigned *port;
306         int err = 0;
307         int irq;
308         int io;
309
310         if (!dev)
311                 return ERR_PTR(-ENODEV);
312
313         sprintf(dev->name, "eth%d", unit);
314         netdev_boot_setup_check(dev);
315         io = dev->base_addr;
316         irq = dev->irq;
317
318         if (net_debug)
319                 printk("cs89x0:cs89x0_probe(0x%x)\n", io);
320
321         if (io > 0x1ff) {       /* Check a single specified location. */
322                 err = cs89x0_probe1(dev, io, 0);
323         } else if (io != 0) {   /* Don't probe at all. */
324                 err = -ENXIO;
325         } else {
326                 for (port = netcard_portlist; *port; port++) {
327                         if (cs89x0_probe1(dev, *port, 0) == 0)
328                                 break;
329                         dev->irq = irq;
330                 }
331                 if (!*port)
332                         err = -ENODEV;
333         }
334         if (err)
335                 goto out;
336         return dev;
337 out:
338         free_netdev(dev);
339         printk(KERN_WARNING "cs89x0: no cs8900 or cs8920 detected.  Be sure to disable PnP with SETUP\n");
340         return ERR_PTR(err);
341 }
342 #endif
343
344 #if defined(CONFIG_ARCH_IXDP2X01)
345 static u16
346 readword(unsigned long base_addr, int portno)
347 {
348         return __raw_readl(base_addr + (portno << 1));
349 }
350
351 static void
352 writeword(unsigned long base_addr, int portno, u16 value)
353 {
354         __raw_writel(value, base_addr + (portno << 1));
355 }
356 #elif defined(CONFIG_ARCH_PNX010X)
357 static u16
358 readword(unsigned long base_addr, int portno)
359 {
360         return inw(base_addr + (portno << 1));
361 }
362
363 static void
364 writeword(unsigned long base_addr, int portno, u16 value)
365 {
366         outw(value, base_addr + (portno << 1));
367 }
368 #else
369 static u16
370 readword(unsigned long base_addr, int portno)
371 {
372         return inw(base_addr + portno);
373 }
374
375 static void
376 writeword(unsigned long base_addr, int portno, u16 value)
377 {
378         outw(value, base_addr + portno);
379 }
380 #endif
381
382 static void
383 readwords(unsigned long base_addr, int portno, void *buf, int length)
384 {
385         u8 *buf8 = (u8 *)buf;
386
387         do {
388                 u16 tmp16;
389
390                 tmp16 = readword(base_addr, portno);
391                 *buf8++ = (u8)tmp16;
392                 *buf8++ = (u8)(tmp16 >> 8);
393         } while (--length);
394 }
395
396 static void
397 writewords(unsigned long base_addr, int portno, void *buf, int length)
398 {
399         u8 *buf8 = (u8 *)buf;
400
401         do {
402                 u16 tmp16;
403
404                 tmp16 = *buf8++;
405                 tmp16 |= (*buf8++) << 8;
406                 writeword(base_addr, portno, tmp16);
407         } while (--length);
408 }
409
410 static u16
411 readreg(struct net_device *dev, u16 regno)
412 {
413         writeword(dev->base_addr, ADD_PORT, regno);
414         return readword(dev->base_addr, DATA_PORT);
415 }
416
417 static void
418 writereg(struct net_device *dev, u16 regno, u16 value)
419 {
420         writeword(dev->base_addr, ADD_PORT, regno);
421         writeword(dev->base_addr, DATA_PORT, value);
422 }
423
424 static int __init
425 wait_eeprom_ready(struct net_device *dev)
426 {
427         int timeout = jiffies;
428         /* check to see if the EEPROM is ready, a timeout is used -
429            just in case EEPROM is ready when SI_BUSY in the
430            PP_SelfST is clear */
431         while(readreg(dev, PP_SelfST) & SI_BUSY)
432                 if (jiffies - timeout >= 40)
433                         return -1;
434         return 0;
435 }
436
437 static int __init
438 get_eeprom_data(struct net_device *dev, int off, int len, int *buffer)
439 {
440         int i;
441
442         if (net_debug > 3) printk("EEPROM data from %x for %x:\n",off,len);
443         for (i = 0; i < len; i++) {
444                 if (wait_eeprom_ready(dev) < 0) return -1;
445                 /* Now send the EEPROM read command and EEPROM location to read */
446                 writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
447                 if (wait_eeprom_ready(dev) < 0) return -1;
448                 buffer[i] = readreg(dev, PP_EEData);
449                 if (net_debug > 3) printk("%04x ", buffer[i]);
450         }
451         if (net_debug > 3) printk("\n");
452         return 0;
453 }
454
455 static int  __init
456 get_eeprom_cksum(int off, int len, int *buffer)
457 {
458         int i, cksum;
459
460         cksum = 0;
461         for (i = 0; i < len; i++)
462                 cksum += buffer[i];
463         cksum &= 0xffff;
464         if (cksum == 0)
465                 return 0;
466         return -1;
467 }
468
469 #ifdef CONFIG_NET_POLL_CONTROLLER
470 /*
471  * Polling receive - used by netconsole and other diagnostic tools
472  * to allow network i/o with interrupts disabled.
473  */
474 static void net_poll_controller(struct net_device *dev)
475 {
476         disable_irq(dev->irq);
477         net_interrupt(dev->irq, dev, NULL);
478         enable_irq(dev->irq);
479 }
480 #endif
481
482 /* This is the real probe routine.  Linux has a history of friendly device
483    probes on the ISA bus.  A good device probes avoids doing writes, and
484    verifies that the correct device exists and functions.
485    Return 0 on success.
486  */
487
488 static int __init
489 cs89x0_probe1(struct net_device *dev, int ioaddr, int modular)
490 {
491         struct net_local *lp = netdev_priv(dev);
492         static unsigned version_printed;
493         int i;
494         int tmp;
495         unsigned rev_type = 0;
496         int eeprom_buff[CHKSUM_LEN];
497         int retval;
498
499         SET_MODULE_OWNER(dev);
500         /* Initialize the device structure. */
501         if (!modular) {
502                 memset(lp, 0, sizeof(*lp));
503                 spin_lock_init(&lp->lock);
504 #ifndef MODULE
505 #if ALLOW_DMA
506                 if (g_cs89x0_dma) {
507                         lp->use_dma = 1;
508                         lp->dma = g_cs89x0_dma;
509                         lp->dmasize = 16;       /* Could make this an option... */
510                 }
511 #endif
512                 lp->force = g_cs89x0_media__force;
513 #endif
514         }
515
516 #ifdef CONFIG_ARCH_PNX010X
517         initialize_ebi();
518
519         /* Map GPIO registers for the pins connected to the CS8900a. */
520         if (map_cirrus_gpio() < 0)
521                 return -ENODEV;
522
523         reset_cirrus();
524
525         /* Map event-router registers. */
526         if (map_event_router() < 0)
527                 return -ENODEV;
528
529         enable_cirrus_irq();
530
531         unmap_cirrus_gpio();
532         unmap_event_router();
533
534         dev->base_addr = ioaddr;
535
536         for (i = 0 ; i < 3 ; i++)
537                 readreg(dev, 0);
538 #endif
539
540         /* Grab the region so we can find another board if autoIRQ fails. */
541         /* WTF is going on here? */
542         if (!request_region(ioaddr & ~3, NETCARD_IO_EXTENT, DRV_NAME)) {
543                 printk(KERN_ERR "%s: request_region(0x%x, 0x%x) failed\n",
544                                 DRV_NAME, ioaddr, NETCARD_IO_EXTENT);
545                 retval = -EBUSY;
546                 goto out1;
547         }
548
549 #ifdef CONFIG_SH_HICOSH4
550         /* truely reset the chip */
551         writeword(ioaddr, ADD_PORT, 0x0114);
552         writeword(ioaddr, DATA_PORT, 0x0040);
553 #endif
554
555         /* if they give us an odd I/O address, then do ONE write to
556            the address port, to get it back to address zero, where we
557            expect to find the EISA signature word. An IO with a base of 0x3
558            will skip the test for the ADD_PORT. */
559         if (ioaddr & 1) {
560                 if (net_debug > 1)
561                         printk(KERN_INFO "%s: odd ioaddr 0x%x\n", dev->name, ioaddr);
562                 if ((ioaddr & 2) != 2)
563                         if ((readword(ioaddr & ~3, ADD_PORT) & ADD_MASK) != ADD_SIG) {
564                                 printk(KERN_ERR "%s: bad signature 0x%x\n",
565                                         dev->name, readword(ioaddr & ~3, ADD_PORT));
566                                 retval = -ENODEV;
567                                 goto out2;
568                         }
569         }
570         printk(KERN_DEBUG "PP_addr at %x[%x]: 0x%x\n",
571                         ioaddr, ADD_PORT, readword(ioaddr, ADD_PORT));
572
573         ioaddr &= ~3;
574         writeword(ioaddr, ADD_PORT, PP_ChipID);
575
576         tmp = readword(ioaddr, DATA_PORT);
577         if (tmp != CHIP_EISA_ID_SIG) {
578                 printk(KERN_DEBUG "%s: incorrect signature at %x[%x]: 0x%x!="
579                         CHIP_EISA_ID_SIG_STR "\n",
580                         dev->name, ioaddr, DATA_PORT, tmp);
581                 retval = -ENODEV;
582                 goto out2;
583         }
584
585         /* Fill in the 'dev' fields. */
586         dev->base_addr = ioaddr;
587
588         /* get the chip type */
589         rev_type = readreg(dev, PRODUCT_ID_ADD);
590         lp->chip_type = rev_type &~ REVISON_BITS;
591         lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
592
593         /* Check the chip type and revision in order to set the correct send command
594         CS8920 revision C and CS8900 revision F can use the faster send. */
595         lp->send_cmd = TX_AFTER_381;
596         if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
597                 lp->send_cmd = TX_NOW;
598         if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
599                 lp->send_cmd = TX_NOW;
600
601         if (net_debug  &&  version_printed++ == 0)
602                 printk(version);
603
604         printk(KERN_INFO "%s: cs89%c0%s rev %c found at %#3lx ",
605                dev->name,
606                lp->chip_type==CS8900?'0':'2',
607                lp->chip_type==CS8920M?"M":"",
608                lp->chip_revision,
609                dev->base_addr);
610
611         reset_chip(dev);
612    
613         /* Here we read the current configuration of the chip. If there
614            is no Extended EEPROM then the idea is to not disturb the chip
615            configuration, it should have been correctly setup by automatic
616            EEPROM read on reset. So, if the chip says it read the EEPROM
617            the driver will always do *something* instead of complain that
618            adapter_cnf is 0. */
619
620 #ifdef CONFIG_SH_HICOSH4
621         if (1) {
622                 /* For the HiCO.SH4 board, things are different: we don't
623                    have EEPROM, but there is some data in flash, so we go
624                    get it there directly (MAC). */
625                 __u16 *confd;
626                 short cnt;
627                 if (((* (volatile __u32 *) 0xa0013ff0) & 0x00ffffff)
628                         == 0x006c3000) {
629                         confd = (__u16*) 0xa0013fc0;
630                 } else {
631                         confd = (__u16*) 0xa001ffc0;
632                 }
633                 cnt = (*confd++ & 0x00ff) >> 1;
634                 while (--cnt > 0) {
635                         __u16 j = *confd++;
636                         
637                         switch (j & 0x0fff) {
638                         case PP_IA:
639                                 for (i = 0; i < ETH_ALEN/2; i++) {
640                                         dev->dev_addr[i*2] = confd[i] & 0xFF;
641                                         dev->dev_addr[i*2+1] = confd[i] >> 8;
642                                 }
643                                 break;
644                         }
645                         j = (j >> 12) + 1;
646                         confd += j;
647                         cnt -= j;
648                 }
649         } else
650 #endif
651
652         if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) == 
653               (EEPROM_OK|EEPROM_PRESENT)) {
654                 /* Load the MAC. */
655                 for (i=0; i < ETH_ALEN/2; i++) {
656                         unsigned int Addr;
657                         Addr = readreg(dev, PP_IA+i*2);
658                         dev->dev_addr[i*2] = Addr & 0xFF;
659                         dev->dev_addr[i*2+1] = Addr >> 8;
660                 }
661    
662                 /* Load the Adapter Configuration. 
663                    Note:  Barring any more specific information from some 
664                    other source (ie EEPROM+Schematics), we would not know 
665                    how to operate a 10Base2 interface on the AUI port. 
666                    However, since we  do read the status of HCB1 and use 
667                    settings that always result in calls to control_dc_dc(dev,0) 
668                    a BNC interface should work if the enable pin 
669                    (dc/dc converter) is on HCB1. It will be called AUI 
670                    however. */
671            
672                 lp->adapter_cnf = 0;
673                 i = readreg(dev, PP_LineCTL);
674                 /* Preserve the setting of the HCB1 pin. */
675                 if ((i & (HCB1 | HCB1_ENBL)) ==  (HCB1 | HCB1_ENBL))
676                         lp->adapter_cnf |= A_CNF_DC_DC_POLARITY;
677                 /* Save the sqelch bit */
678                 if ((i & LOW_RX_SQUELCH) == LOW_RX_SQUELCH)
679                         lp->adapter_cnf |= A_CNF_EXTND_10B_2 | A_CNF_LOW_RX_SQUELCH;
680                 /* Check if the card is in 10Base-t only mode */
681                 if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == 0)
682                         lp->adapter_cnf |=  A_CNF_10B_T | A_CNF_MEDIA_10B_T;
683                 /* Check if the card is in AUI only mode */
684                 if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUI_ONLY)
685                         lp->adapter_cnf |=  A_CNF_AUI | A_CNF_MEDIA_AUI;
686                 /* Check if the card is in Auto mode. */
687                 if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUTO_AUI_10BASET)
688                         lp->adapter_cnf |=  A_CNF_AUI | A_CNF_10B_T | 
689                         A_CNF_MEDIA_AUI | A_CNF_MEDIA_10B_T | A_CNF_MEDIA_AUTO;
690                 
691                 if (net_debug > 1)
692                         printk(KERN_INFO "%s: PP_LineCTL=0x%x, adapter_cnf=0x%x\n",
693                                         dev->name, i, lp->adapter_cnf);
694
695                 /* IRQ. Other chips already probe, see below. */
696                 if (lp->chip_type == CS8900) 
697                         lp->isa_config = readreg(dev, PP_CS8900_ISAINT) & INT_NO_MASK;
698            
699                 printk( "[Cirrus EEPROM] ");
700         }
701
702         printk("\n");
703    
704         /* First check to see if an EEPROM is attached. */
705 #ifdef CONFIG_SH_HICOSH4 /* no EEPROM on HiCO, don't hazzle with it here */
706         if (1) {
707                 printk(KERN_NOTICE "cs89x0: No EEPROM on HiCO.SH4\n");
708         } else
709 #endif
710         if ((readreg(dev, PP_SelfST) & EEPROM_PRESENT) == 0)
711                 printk(KERN_WARNING "cs89x0: No EEPROM, relying on command line....\n");
712         else if (get_eeprom_data(dev, START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
713                 printk(KERN_WARNING "\ncs89x0: EEPROM read failed, relying on command line.\n");
714         } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
715                 /* Check if the chip was able to read its own configuration starting
716                    at 0 in the EEPROM*/
717                 if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) !=
718                     (EEPROM_OK|EEPROM_PRESENT)) 
719                         printk(KERN_WARNING "cs89x0: Extended EEPROM checksum bad and no Cirrus EEPROM, relying on command line\n");
720                    
721         } else {
722                 /* This reads an extended EEPROM that is not documented
723                    in the CS8900 datasheet. */
724                 
725                 /* get transmission control word  but keep the autonegotiation bits */
726                 if (!lp->auto_neg_cnf) lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
727                 /* Store adapter configuration */
728                 if (!lp->adapter_cnf) lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
729                 /* Store ISA configuration */
730                 lp->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
731                 dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET/2] << 8;
732
733                 /* eeprom_buff has 32-bit ints, so we can't just memcpy it */
734                 /* store the initial memory base address */
735                 for (i = 0; i < ETH_ALEN/2; i++) {
736                         dev->dev_addr[i*2] = eeprom_buff[i];
737                         dev->dev_addr[i*2+1] = eeprom_buff[i] >> 8;
738                 }
739                 if (net_debug > 1)
740                         printk(KERN_DEBUG "%s: new adapter_cnf: 0x%x\n",
741                                 dev->name, lp->adapter_cnf);
742         }
743
744         /* allow them to force multiple transceivers.  If they force multiple, autosense */
745         {
746                 int count = 0;
747                 if (lp->force & FORCE_RJ45)     {lp->adapter_cnf |= A_CNF_10B_T; count++; }
748                 if (lp->force & FORCE_AUI)      {lp->adapter_cnf |= A_CNF_AUI; count++; }
749                 if (lp->force & FORCE_BNC)      {lp->adapter_cnf |= A_CNF_10B_2; count++; }
750                 if (count > 1)                  {lp->adapter_cnf |= A_CNF_MEDIA_AUTO; }
751                 else if (lp->force & FORCE_RJ45){lp->adapter_cnf |= A_CNF_MEDIA_10B_T; }
752                 else if (lp->force & FORCE_AUI) {lp->adapter_cnf |= A_CNF_MEDIA_AUI; }
753                 else if (lp->force & FORCE_BNC) {lp->adapter_cnf |= A_CNF_MEDIA_10B_2; }
754         }
755
756         if (net_debug > 1)
757                 printk(KERN_DEBUG "%s: after force 0x%x, adapter_cnf=0x%x\n",
758                         dev->name, lp->force, lp->adapter_cnf);
759
760         /* FIXME: We don't let you set dc-dc polarity or low RX squelch from the command line: add it here */
761
762         /* FIXME: We don't let you set the IMM bit from the command line: add it to lp->auto_neg_cnf here */
763
764         /* FIXME: we don't set the Ethernet address on the command line.  Use
765            ifconfig IFACE hw ether AABBCCDDEEFF */
766
767         printk(KERN_INFO "cs89x0 media %s%s%s",
768                (lp->adapter_cnf & A_CNF_10B_T)?"RJ-45,":"",
769                (lp->adapter_cnf & A_CNF_AUI)?"AUI,":"",
770                (lp->adapter_cnf & A_CNF_10B_2)?"BNC,":"");
771
772         lp->irq_map = 0xffff;
773
774         /* If this is a CS8900 then no pnp soft */
775         if (lp->chip_type != CS8900 &&
776             /* Check if the ISA IRQ has been set  */
777                 (i = readreg(dev, PP_CS8920_ISAINT) & 0xff,
778                  (i != 0 && i < CS8920_NO_INTS))) {
779                 if (!dev->irq)
780                         dev->irq = i;
781         } else {
782                 i = lp->isa_config & INT_NO_MASK;
783                 if (lp->chip_type == CS8900) {
784 #if defined(CONFIG_ARCH_IXDP2X01) || defined(CONFIG_ARCH_PNX010X)
785                         i = cs8900_irq_map[0];
786 #else
787                         /* Translate the IRQ using the IRQ mapping table. */
788                         if (i >= sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]))
789                                 printk("\ncs89x0: invalid ISA interrupt number %d\n", i);
790                         else
791                                 i = cs8900_irq_map[i];
792                         
793                         lp->irq_map = CS8900_IRQ_MAP; /* fixed IRQ map for CS8900 */
794                 } else {
795                         int irq_map_buff[IRQ_MAP_LEN/2];
796
797                         if (get_eeprom_data(dev, IRQ_MAP_EEPROM_DATA,
798                                             IRQ_MAP_LEN/2,
799                                             irq_map_buff) >= 0) {
800                                 if ((irq_map_buff[0] & 0xff) == PNP_IRQ_FRMT)
801                                         lp->irq_map = (irq_map_buff[0]>>8) | (irq_map_buff[1] << 8);
802                         }
803 #endif
804                 }
805                 if (!dev->irq)
806                         dev->irq = i;
807         }
808
809         printk(" IRQ %d", dev->irq);
810
811 #if ALLOW_DMA
812         if (lp->use_dma) {
813                 get_dma_channel(dev);
814                 printk(", DMA %d", dev->dma);
815         }
816         else
817 #endif
818         {
819                 printk(", programmed I/O");
820         }
821
822         /* print the ethernet address. */
823         printk(", MAC");
824         for (i = 0; i < ETH_ALEN; i++)
825         {
826                 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
827         }
828
829         dev->open               = net_open;
830         dev->stop               = net_close;
831         dev->tx_timeout         = net_timeout;
832         dev->watchdog_timeo     = HZ;
833         dev->hard_start_xmit    = net_send_packet;
834         dev->get_stats          = net_get_stats;
835         dev->set_multicast_list = set_multicast_list;
836         dev->set_mac_address    = set_mac_address;
837 #ifdef CONFIG_NET_POLL_CONTROLLER
838         dev->poll_controller    = net_poll_controller;
839 #endif
840
841         printk("\n");
842         if (net_debug)
843                 printk("cs89x0_probe1() successful\n");
844
845         retval = register_netdev(dev);
846         if (retval)
847                 goto out3;
848         return 0;
849 out3:
850         writeword(dev->base_addr, ADD_PORT, PP_ChipID);
851 out2:
852         release_region(ioaddr & ~3, NETCARD_IO_EXTENT);
853 out1:
854         return retval;
855 }
856
857 \f
858 /*********************************
859  * This page contains DMA routines
860 **********************************/
861
862 #if ALLOW_DMA
863
864 #define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
865
866 static void
867 get_dma_channel(struct net_device *dev)
868 {
869         struct net_local *lp = netdev_priv(dev);
870
871         if (lp->dma) {
872                 dev->dma = lp->dma;
873                 lp->isa_config |= ISA_RxDMA;
874         } else {
875                 if ((lp->isa_config & ANY_ISA_DMA) == 0)
876                         return;
877                 dev->dma = lp->isa_config & DMA_NO_MASK;
878                 if (lp->chip_type == CS8900)
879                         dev->dma += 5;
880                 if (dev->dma < 5 || dev->dma > 7) {
881                         lp->isa_config &= ~ANY_ISA_DMA;
882                         return;
883                 }
884         }
885         return;
886 }
887
888 static void
889 write_dma(struct net_device *dev, int chip_type, int dma)
890 {
891         struct net_local *lp = netdev_priv(dev);
892         if ((lp->isa_config & ANY_ISA_DMA) == 0)
893                 return;
894         if (chip_type == CS8900) {
895                 writereg(dev, PP_CS8900_ISADMA, dma-5);
896         } else {
897                 writereg(dev, PP_CS8920_ISADMA, dma);
898         }
899 }
900
901 static void
902 set_dma_cfg(struct net_device *dev)
903 {
904         struct net_local *lp = netdev_priv(dev);
905
906         if (lp->use_dma) {
907                 if ((lp->isa_config & ANY_ISA_DMA) == 0) {
908                         if (net_debug > 3)
909                                 printk("set_dma_cfg(): no DMA\n");
910                         return;
911                 }
912                 if (lp->isa_config & ISA_RxDMA) {
913                         lp->curr_rx_cfg |= RX_DMA_ONLY;
914                         if (net_debug > 3)
915                                 printk("set_dma_cfg(): RX_DMA_ONLY\n");
916                 } else {
917                         lp->curr_rx_cfg |= AUTO_RX_DMA; /* not that we support it... */
918                         if (net_debug > 3)
919                                 printk("set_dma_cfg(): AUTO_RX_DMA\n");
920                 }
921         }
922 }
923
924 static int
925 dma_bufcfg(struct net_device *dev)
926 {
927         struct net_local *lp = netdev_priv(dev);
928         if (lp->use_dma)
929                 return (lp->isa_config & ANY_ISA_DMA)? RX_DMA_ENBL : 0;
930         else
931                 return 0;
932 }
933
934 static int
935 dma_busctl(struct net_device *dev)
936 {
937         int retval = 0;
938         struct net_local *lp = netdev_priv(dev);
939         if (lp->use_dma) {
940                 if (lp->isa_config & ANY_ISA_DMA)
941                         retval |= RESET_RX_DMA; /* Reset the DMA pointer */
942                 if (lp->isa_config & DMA_BURST)
943                         retval |= DMA_BURST_MODE; /* Does ISA config specify DMA burst ? */
944                 if (lp->dmasize == 64)
945                         retval |= RX_DMA_SIZE_64K; /* did they ask for 64K? */
946                 retval |= MEMORY_ON;    /* we need memory enabled to use DMA. */
947         }
948         return retval;
949 }
950
951 static void
952 dma_rx(struct net_device *dev)
953 {
954         struct net_local *lp = netdev_priv(dev);
955         struct sk_buff *skb;
956         int status, length;
957         unsigned char *bp = lp->rx_dma_ptr;
958
959         status = bp[0] + (bp[1]<<8);
960         length = bp[2] + (bp[3]<<8);
961         bp += 4;
962         if (net_debug > 5) {
963                 printk( "%s: receiving DMA packet at %lx, status %x, length %x\n",
964                         dev->name, (unsigned long)bp, status, length);
965         }
966         if ((status & RX_OK) == 0) {
967                 count_rx_errors(status, lp);
968                 goto skip_this_frame;
969         }
970
971         /* Malloc up new buffer. */
972         skb = dev_alloc_skb(length + 2);
973         if (skb == NULL) {
974                 if (net_debug)  /* I don't think we want to do this to a stressed system */
975                         printk("%s: Memory squeeze, dropping packet.\n", dev->name);
976                 lp->stats.rx_dropped++;
977
978                 /* AKPM: advance bp to the next frame */
979 skip_this_frame:
980                 bp += (length + 3) & ~3;
981                 if (bp >= lp->end_dma_buff) bp -= lp->dmasize*1024;
982                 lp->rx_dma_ptr = bp;
983                 return;
984         }
985         skb_reserve(skb, 2);    /* longword align L3 header */
986         skb->dev = dev;
987
988         if (bp + length > lp->end_dma_buff) {
989                 int semi_cnt = lp->end_dma_buff - bp;
990                 memcpy(skb_put(skb,semi_cnt), bp, semi_cnt);
991                 memcpy(skb_put(skb,length - semi_cnt), lp->dma_buff,
992                        length - semi_cnt);
993         } else {
994                 memcpy(skb_put(skb,length), bp, length);
995         }
996         bp += (length + 3) & ~3;
997         if (bp >= lp->end_dma_buff) bp -= lp->dmasize*1024;
998         lp->rx_dma_ptr = bp;
999
1000         if (net_debug > 3) {
1001                 printk( "%s: received %d byte DMA packet of type %x\n",
1002                         dev->name, length,
1003                         (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1004         }
1005         skb->protocol=eth_type_trans(skb,dev);
1006         netif_rx(skb);
1007         dev->last_rx = jiffies;
1008         lp->stats.rx_packets++;
1009         lp->stats.rx_bytes += length;
1010 }
1011
1012 #endif  /* ALLOW_DMA */
1013
1014 void  __init reset_chip(struct net_device *dev)
1015 {
1016 #ifndef CONFIG_ARCH_IXDP2X01
1017         struct net_local *lp = netdev_priv(dev);
1018         int ioaddr = dev->base_addr;
1019 #endif
1020         int reset_start_time;
1021
1022         writereg(dev, PP_SelfCTL, readreg(dev, PP_SelfCTL) | POWER_ON_RESET);
1023
1024         /* wait 30 ms */
1025         msleep(30);
1026
1027 #ifndef CONFIG_ARCH_IXDP2X01
1028         if (lp->chip_type != CS8900) {
1029                 /* Hardware problem requires PNP registers to be reconfigured after a reset */
1030                 writeword(ioaddr, ADD_PORT, PP_CS8920_ISAINT);
1031                 outb(dev->irq, ioaddr + DATA_PORT);
1032                 outb(0,      ioaddr + DATA_PORT + 1);
1033
1034                 writeword(ioaddr, ADD_PORT, PP_CS8920_ISAMemB);
1035                 outb((dev->mem_start >> 16) & 0xff, ioaddr + DATA_PORT);
1036                 outb((dev->mem_start >> 8) & 0xff,   ioaddr + DATA_PORT + 1);
1037         }
1038 #endif  /* IXDP2x01 */
1039
1040         /* Wait until the chip is reset */
1041         reset_start_time = jiffies;
1042         while( (readreg(dev, PP_SelfST) & INIT_DONE) == 0 && jiffies - reset_start_time < 2)
1043                 ;
1044 }
1045
1046 \f
1047 static void
1048 control_dc_dc(struct net_device *dev, int on_not_off)
1049 {
1050         struct net_local *lp = netdev_priv(dev);
1051         unsigned int selfcontrol;
1052         int timenow = jiffies;
1053         /* control the DC to DC convertor in the SelfControl register.  
1054            Note: This is hooked up to a general purpose pin, might not
1055            always be a DC to DC convertor. */
1056
1057         selfcontrol = HCB1_ENBL; /* Enable the HCB1 bit as an output */
1058         if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
1059                 selfcontrol |= HCB1;
1060         else
1061                 selfcontrol &= ~HCB1;
1062         writereg(dev, PP_SelfCTL, selfcontrol);
1063
1064         /* Wait for the DC/DC converter to power up - 500ms */
1065         while (jiffies - timenow < HZ)
1066                 ;
1067 }
1068
1069 #define DETECTED_NONE  0
1070 #define DETECTED_RJ45H 1
1071 #define DETECTED_RJ45F 2
1072 #define DETECTED_AUI   3
1073 #define DETECTED_BNC   4
1074
1075 static int
1076 detect_tp(struct net_device *dev)
1077 {
1078         struct net_local *lp = netdev_priv(dev);
1079         int timenow = jiffies;
1080         int fdx;
1081
1082         if (net_debug > 1) printk("%s: Attempting TP\n", dev->name);
1083
1084         /* If connected to another full duplex capable 10-Base-T card the link pulses
1085            seem to be lost when the auto detect bit in the LineCTL is set.
1086            To overcome this the auto detect bit will be cleared whilst testing the
1087            10-Base-T interface.  This would not be necessary for the sparrow chip but
1088            is simpler to do it anyway. */
1089         writereg(dev, PP_LineCTL, lp->linectl &~ AUI_ONLY);
1090         control_dc_dc(dev, 0);
1091
1092         /* Delay for the hardware to work out if the TP cable is present - 150ms */
1093         for (timenow = jiffies; jiffies - timenow < 15; )
1094                 ;
1095         if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
1096                 return DETECTED_NONE;
1097
1098         if (lp->chip_type == CS8900) {
1099                 switch (lp->force & 0xf0) {
1100 #if 0
1101                 case FORCE_AUTO:
1102                         printk("%s: cs8900 doesn't autonegotiate\n",dev->name);
1103                         return DETECTED_NONE;
1104 #endif
1105                 /* CS8900 doesn't support AUTO, change to HALF*/
1106                 case FORCE_AUTO:
1107                         lp->force &= ~FORCE_AUTO;
1108                         lp->force |= FORCE_HALF;
1109                         break;
1110                 case FORCE_HALF:
1111                         break;
1112                 case FORCE_FULL:
1113                         writereg(dev, PP_TestCTL, readreg(dev, PP_TestCTL) | FDX_8900);
1114                         break;
1115                 }
1116                 fdx = readreg(dev, PP_TestCTL) & FDX_8900;
1117         } else {
1118                 switch (lp->force & 0xf0) {
1119                 case FORCE_AUTO:
1120                         lp->auto_neg_cnf = AUTO_NEG_ENABLE;
1121                         break;
1122                 case FORCE_HALF:
1123                         lp->auto_neg_cnf = 0;
1124                         break;
1125                 case FORCE_FULL:
1126                         lp->auto_neg_cnf = RE_NEG_NOW | ALLOW_FDX;
1127                         break;
1128                 }
1129
1130                 writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
1131
1132                 if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
1133                         printk(KERN_INFO "%s: negotiating duplex...\n",dev->name);
1134                         while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
1135                                 if (jiffies - timenow > 4000) {
1136                                         printk(KERN_ERR "**** Full / half duplex auto-negotiation timed out ****\n");
1137                                         break;
1138                                 }
1139                         }
1140                 }
1141                 fdx = readreg(dev, PP_AutoNegST) & FDX_ACTIVE;
1142         }
1143         if (fdx)
1144                 return DETECTED_RJ45F;
1145         else
1146                 return DETECTED_RJ45H;
1147 }
1148
1149 /* send a test packet - return true if carrier bits are ok */
1150 static int
1151 send_test_pkt(struct net_device *dev)
1152 {
1153         char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
1154                                  0, 46, /* A 46 in network order */
1155                                  0, 0, /* DSAP=0 & SSAP=0 fields */
1156                                  0xf3, 0 /* Control (Test Req + P bit set) */ };
1157         long timenow = jiffies;
1158
1159         writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
1160
1161         memcpy(test_packet,          dev->dev_addr, ETH_ALEN);
1162         memcpy(test_packet+ETH_ALEN, dev->dev_addr, ETH_ALEN);
1163
1164         writeword(dev->base_addr, TX_CMD_PORT, TX_AFTER_ALL);
1165         writeword(dev->base_addr, TX_LEN_PORT, ETH_ZLEN);
1166
1167         /* Test to see if the chip has allocated memory for the packet */
1168         while (jiffies - timenow < 5)
1169                 if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
1170                         break;
1171         if (jiffies - timenow >= 5)
1172                 return 0;       /* this shouldn't happen */
1173
1174         /* Write the contents of the packet */
1175         writewords(dev->base_addr, TX_FRAME_PORT,test_packet,(ETH_ZLEN+1) >>1);
1176
1177         if (net_debug > 1) printk("Sending test packet ");
1178         /* wait a couple of jiffies for packet to be received */
1179         for (timenow = jiffies; jiffies - timenow < 3; )
1180                 ;
1181         if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
1182                 if (net_debug > 1) printk("succeeded\n");
1183                 return 1;
1184         }
1185         if (net_debug > 1) printk("failed\n");
1186         return 0;
1187 }
1188
1189
1190 static int
1191 detect_aui(struct net_device *dev)
1192 {
1193         struct net_local *lp = netdev_priv(dev);
1194
1195         if (net_debug > 1) printk("%s: Attempting AUI\n", dev->name);
1196         control_dc_dc(dev, 0);
1197
1198         writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
1199
1200         if (send_test_pkt(dev))
1201                 return DETECTED_AUI;
1202         else
1203                 return DETECTED_NONE;
1204 }
1205
1206 static int
1207 detect_bnc(struct net_device *dev)
1208 {
1209         struct net_local *lp = netdev_priv(dev);
1210
1211         if (net_debug > 1) printk("%s: Attempting BNC\n", dev->name);
1212         control_dc_dc(dev, 1);
1213
1214         writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
1215
1216         if (send_test_pkt(dev))
1217                 return DETECTED_BNC;
1218         else
1219                 return DETECTED_NONE;
1220 }
1221
1222 \f
1223 static void
1224 write_irq(struct net_device *dev, int chip_type, int irq)
1225 {
1226         int i;
1227
1228         if (chip_type == CS8900) {
1229                 /* Search the mapping table for the corresponding IRQ pin. */
1230                 for (i = 0; i != sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]); i++)
1231                         if (cs8900_irq_map[i] == irq)
1232                                 break;
1233                 /* Not found */
1234                 if (i == sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]))
1235                         i = 3;
1236                 writereg(dev, PP_CS8900_ISAINT, i);
1237         } else {
1238                 writereg(dev, PP_CS8920_ISAINT, irq);
1239         }
1240 }
1241
1242 /* Open/initialize the board.  This is called (in the current kernel)
1243    sometime after booting when the 'ifconfig' program is run.
1244
1245    This routine should set everything up anew at each open, even
1246    registers that "should" only need to be set once at boot, so that
1247    there is non-reboot way to recover if something goes wrong.
1248    */
1249
1250 /* AKPM: do we need to do any locking here? */
1251
1252 static int
1253 net_open(struct net_device *dev)
1254 {
1255         struct net_local *lp = netdev_priv(dev);
1256         int result = 0;
1257         int i;
1258         int ret;
1259
1260 #if !defined(CONFIG_SH_HICOSH4) && !defined(CONFIG_ARCH_PNX010X) /* uses irq#1, so this won't work */
1261         if (dev->irq < 2) {
1262                 /* Allow interrupts to be generated by the chip */
1263 /* Cirrus' release had this: */
1264 #if 0
1265                 writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ );
1266 #endif
1267 /* And 2.3.47 had this: */
1268                 writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
1269
1270                 for (i = 2; i < CS8920_NO_INTS; i++) {
1271                         if ((1 << i) & lp->irq_map) {
1272                                 if (request_irq(i, net_interrupt, 0, dev->name, dev) == 0) {
1273                                         dev->irq = i;
1274                                         write_irq(dev, lp->chip_type, i);
1275                                         /* writereg(dev, PP_BufCFG, GENERATE_SW_INTERRUPT); */
1276                                         break;
1277                                 }
1278                         }
1279                 }
1280
1281                 if (i >= CS8920_NO_INTS) {
1282                         writereg(dev, PP_BusCTL, 0);    /* disable interrupts. */
1283                         printk(KERN_ERR "cs89x0: can't get an interrupt\n");
1284                         ret = -EAGAIN;
1285                         goto bad_out;
1286                 }
1287         }
1288         else
1289 #endif
1290         {
1291 #if !defined(CONFIG_ARCH_IXDP2X01) && !defined(CONFIG_ARCH_PNX010X)
1292                 if (((1 << dev->irq) & lp->irq_map) == 0) {
1293                         printk(KERN_ERR "%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
1294                                dev->name, dev->irq, lp->irq_map);
1295                         ret = -EAGAIN;
1296                         goto bad_out;
1297                 }
1298 #endif
1299 /* FIXME: Cirrus' release had this: */
1300                 writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ );
1301 /* And 2.3.47 had this: */
1302 #if 0
1303                 writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
1304 #endif
1305                 write_irq(dev, lp->chip_type, dev->irq);
1306                 ret = request_irq(dev->irq, &net_interrupt, 0, dev->name, dev);
1307                 if (ret) {
1308                         if (net_debug)
1309                                 printk(KERN_DEBUG "cs89x0: request_irq(%d) failed\n", dev->irq);
1310                         goto bad_out;
1311                 }
1312         }
1313
1314 #if ALLOW_DMA
1315         if (lp->use_dma) {
1316                 if (lp->isa_config & ANY_ISA_DMA) {
1317                         unsigned long flags;
1318                         lp->dma_buff = (unsigned char *)__get_dma_pages(GFP_KERNEL,
1319                                                         get_order(lp->dmasize * 1024));
1320
1321                         if (!lp->dma_buff) {
1322                                 printk(KERN_ERR "%s: cannot get %dK memory for DMA\n", dev->name, lp->dmasize);
1323                                 goto release_irq;
1324                         }
1325                         if (net_debug > 1) {
1326                                 printk( "%s: dma %lx %lx\n",
1327                                         dev->name,
1328                                         (unsigned long)lp->dma_buff,
1329                                         (unsigned long)isa_virt_to_bus(lp->dma_buff));
1330                         }
1331                         if ((unsigned long) lp->dma_buff >= MAX_DMA_ADDRESS ||
1332                             !dma_page_eq(lp->dma_buff, lp->dma_buff+lp->dmasize*1024-1)) {
1333                                 printk(KERN_ERR "%s: not usable as DMA buffer\n", dev->name);
1334                                 goto release_irq;
1335                         }
1336                         memset(lp->dma_buff, 0, lp->dmasize * 1024);    /* Why? */
1337                         if (request_dma(dev->dma, dev->name)) {
1338                                 printk(KERN_ERR "%s: cannot get dma channel %d\n", dev->name, dev->dma);
1339                                 goto release_irq;
1340                         }
1341                         write_dma(dev, lp->chip_type, dev->dma);
1342                         lp->rx_dma_ptr = lp->dma_buff;
1343                         lp->end_dma_buff = lp->dma_buff + lp->dmasize*1024;
1344                         spin_lock_irqsave(&lp->lock, flags);
1345                         disable_dma(dev->dma);
1346                         clear_dma_ff(dev->dma);
1347                         set_dma_mode(dev->dma, 0x14); /* auto_init as well */
1348                         set_dma_addr(dev->dma, isa_virt_to_bus(lp->dma_buff));
1349                         set_dma_count(dev->dma, lp->dmasize*1024);
1350                         enable_dma(dev->dma);
1351                         spin_unlock_irqrestore(&lp->lock, flags);
1352                 }
1353         }
1354 #endif  /* ALLOW_DMA */
1355
1356         /* set the Ethernet address */
1357         for (i=0; i < ETH_ALEN/2; i++)
1358                 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1359
1360         /* while we're testing the interface, leave interrupts disabled */
1361         writereg(dev, PP_BusCTL, MEMORY_ON);
1362
1363         /* Set the LineCTL quintuplet based on adapter configuration read from EEPROM */
1364         if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) && (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
1365                 lp->linectl = LOW_RX_SQUELCH;
1366         else
1367                 lp->linectl = 0;
1368
1369         /* check to make sure that they have the "right" hardware available */
1370         switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
1371         case A_CNF_MEDIA_10B_T: result = lp->adapter_cnf & A_CNF_10B_T; break;
1372         case A_CNF_MEDIA_AUI:   result = lp->adapter_cnf & A_CNF_AUI; break;
1373         case A_CNF_MEDIA_10B_2: result = lp->adapter_cnf & A_CNF_10B_2; break;
1374         default: result = lp->adapter_cnf & (A_CNF_10B_T | A_CNF_AUI | A_CNF_10B_2);
1375         }
1376 #ifdef CONFIG_ARCH_PNX010X
1377         result = A_CNF_10B_T;
1378 #endif
1379         if (!result) {
1380                 printk(KERN_ERR "%s: EEPROM is configured for unavailable media\n", dev->name);
1381         release_irq:
1382 #if ALLOW_DMA
1383                 release_dma_buff(lp);
1384 #endif
1385                 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
1386                 free_irq(dev->irq, dev);
1387                 ret = -EAGAIN;
1388                 goto bad_out;
1389         }
1390
1391         /* set the hardware to the configured choice */
1392         switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
1393         case A_CNF_MEDIA_10B_T:
1394                 result = detect_tp(dev);
1395                 if (result==DETECTED_NONE) {
1396                         printk(KERN_WARNING "%s: 10Base-T (RJ-45) has no cable\n", dev->name);
1397                         if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
1398                                 result = DETECTED_RJ45H; /* Yes! I don't care if I see a link pulse */
1399                 }
1400                 break;
1401         case A_CNF_MEDIA_AUI:
1402                 result = detect_aui(dev);
1403                 if (result==DETECTED_NONE) {
1404                         printk(KERN_WARNING "%s: 10Base-5 (AUI) has no cable\n", dev->name);
1405                         if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
1406                                 result = DETECTED_AUI; /* Yes! I don't care if I see a carrrier */
1407                 }
1408                 break;
1409         case A_CNF_MEDIA_10B_2:
1410                 result = detect_bnc(dev);
1411                 if (result==DETECTED_NONE) {
1412                         printk(KERN_WARNING "%s: 10Base-2 (BNC) has no cable\n", dev->name);
1413                         if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
1414                                 result = DETECTED_BNC; /* Yes! I don't care if I can xmit a packet */
1415                 }
1416                 break;
1417         case A_CNF_MEDIA_AUTO:
1418                 writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
1419                 if (lp->adapter_cnf & A_CNF_10B_T)
1420                         if ((result = detect_tp(dev)) != DETECTED_NONE)
1421                                 break;
1422                 if (lp->adapter_cnf & A_CNF_AUI)
1423                         if ((result = detect_aui(dev)) != DETECTED_NONE)
1424                                 break;
1425                 if (lp->adapter_cnf & A_CNF_10B_2)
1426                         if ((result = detect_bnc(dev)) != DETECTED_NONE)
1427                                 break;
1428                 printk(KERN_ERR "%s: no media detected\n", dev->name);
1429                 goto release_irq;
1430         }
1431         switch(result) {
1432         case DETECTED_NONE:
1433                 printk(KERN_ERR "%s: no network cable attached to configured media\n", dev->name);
1434                 goto release_irq;
1435         case DETECTED_RJ45H:
1436                 printk(KERN_INFO "%s: using half-duplex 10Base-T (RJ-45)\n", dev->name);
1437                 break;
1438         case DETECTED_RJ45F:
1439                 printk(KERN_INFO "%s: using full-duplex 10Base-T (RJ-45)\n", dev->name);
1440                 break;
1441         case DETECTED_AUI:
1442                 printk(KERN_INFO "%s: using 10Base-5 (AUI)\n", dev->name);
1443                 break;
1444         case DETECTED_BNC:
1445                 printk(KERN_INFO "%s: using 10Base-2 (BNC)\n", dev->name);
1446                 break;
1447         }
1448
1449         /* Turn on both receive and transmit operations */
1450         writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
1451
1452         /* Receive only error free packets addressed to this card */
1453         lp->rx_mode = 0;
1454         writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
1455
1456         lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
1457
1458         if (lp->isa_config & STREAM_TRANSFER)
1459                 lp->curr_rx_cfg |= RX_STREAM_ENBL;
1460 #if ALLOW_DMA
1461         set_dma_cfg(dev);
1462 #endif
1463         writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
1464
1465         writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
1466                 TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
1467
1468         writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
1469 #if ALLOW_DMA
1470                 dma_bufcfg(dev) |
1471 #endif
1472                 TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
1473
1474         /* now that we've got our act together, enable everything */
1475         writereg(dev, PP_BusCTL, ENABLE_IRQ
1476                  | (dev->mem_start?MEMORY_ON : 0) /* turn memory on */
1477 #if ALLOW_DMA
1478                  | dma_busctl(dev)
1479 #endif
1480                  );
1481         netif_start_queue(dev);
1482         if (net_debug > 1)
1483                 printk("cs89x0: net_open() succeeded\n");
1484         return 0;
1485 bad_out:
1486         return ret;
1487 }
1488
1489 static void net_timeout(struct net_device *dev)
1490 {
1491         /* If we get here, some higher level has decided we are broken.
1492            There should really be a "kick me" function call instead. */
1493         if (net_debug > 0) printk("%s: transmit timed out, %s?\n", dev->name,
1494                    tx_done(dev) ? "IRQ conflict ?" : "network cable problem");
1495         /* Try to restart the adaptor. */
1496         netif_wake_queue(dev);
1497 }
1498
1499 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
1500 {
1501         struct net_local *lp = netdev_priv(dev);
1502
1503         if (net_debug > 3) {
1504                 printk("%s: sent %d byte packet of type %x\n",
1505                         dev->name, skb->len,
1506                         (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1507         }
1508
1509         /* keep the upload from being interrupted, since we
1510                   ask the chip to start transmitting before the
1511                   whole packet has been completely uploaded. */
1512
1513         spin_lock_irq(&lp->lock);
1514         netif_stop_queue(dev);
1515
1516         /* initiate a transmit sequence */
1517         writeword(dev->base_addr, TX_CMD_PORT, lp->send_cmd);
1518         writeword(dev->base_addr, TX_LEN_PORT, skb->len);
1519
1520         /* Test to see if the chip has allocated memory for the packet */
1521         if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
1522                 /*
1523                  * Gasp!  It hasn't.  But that shouldn't happen since
1524                  * we're waiting for TxOk, so return 1 and requeue this packet.
1525                  */
1526                 
1527                 spin_unlock_irq(&lp->lock);
1528                 if (net_debug) printk("cs89x0: Tx buffer not free!\n");
1529                 return 1;
1530         }
1531         /* Write the contents of the packet */
1532         writewords(dev->base_addr, TX_FRAME_PORT,skb->data,(skb->len+1) >>1);
1533         spin_unlock_irq(&lp->lock);
1534         lp->stats.tx_bytes += skb->len;
1535         dev->trans_start = jiffies;
1536         dev_kfree_skb (skb);
1537
1538         /*
1539          * We DO NOT call netif_wake_queue() here.
1540          * We also DO NOT call netif_start_queue().
1541          *
1542          * Either of these would cause another bottom half run through
1543          * net_send_packet() before this packet has fully gone out.  That causes
1544          * us to hit the "Gasp!" above and the send is rescheduled.  it runs like
1545          * a dog.  We just return and wait for the Tx completion interrupt handler
1546          * to restart the netdevice layer
1547          */
1548
1549         return 0;
1550 }
1551 \f
1552 /* The typical workload of the driver:
1553    Handle the network interface interrupts. */
1554    
1555 static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1556 {
1557         struct net_device *dev = dev_id;
1558         struct net_local *lp;
1559         int ioaddr, status;
1560         int handled = 0;
1561
1562         ioaddr = dev->base_addr;
1563         lp = netdev_priv(dev);
1564
1565         /* we MUST read all the events out of the ISQ, otherwise we'll never
1566            get interrupted again.  As a consequence, we can't have any limit
1567            on the number of times we loop in the interrupt handler.  The
1568            hardware guarantees that eventually we'll run out of events.  Of
1569            course, if you're on a slow machine, and packets are arriving
1570            faster than you can read them off, you're screwed.  Hasta la
1571            vista, baby!  */
1572         while ((status = readword(dev->base_addr, ISQ_PORT))) {
1573                 if (net_debug > 4)printk("%s: event=%04x\n", dev->name, status);
1574                 handled = 1;
1575                 switch(status & ISQ_EVENT_MASK) {
1576                 case ISQ_RECEIVER_EVENT:
1577                         /* Got a packet(s). */
1578                         net_rx(dev);
1579                         break;
1580                 case ISQ_TRANSMITTER_EVENT:
1581                         lp->stats.tx_packets++;
1582                         netif_wake_queue(dev);  /* Inform upper layers. */
1583                         if ((status & ( TX_OK |
1584                                         TX_LOST_CRS |
1585                                         TX_SQE_ERROR |
1586                                         TX_LATE_COL |
1587                                         TX_16_COL)) != TX_OK) {
1588                                 if ((status & TX_OK) == 0) lp->stats.tx_errors++;
1589                                 if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++;
1590                                 if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++;
1591                                 if (status & TX_LATE_COL) lp->stats.tx_window_errors++;
1592                                 if (status & TX_16_COL) lp->stats.tx_aborted_errors++;
1593                         }
1594                         break;
1595                 case ISQ_BUFFER_EVENT:
1596                         if (status & READY_FOR_TX) {
1597                                 /* we tried to transmit a packet earlier,
1598                                    but inexplicably ran out of buffers.
1599                                    That shouldn't happen since we only ever
1600                                    load one packet.  Shrug.  Do the right
1601                                    thing anyway. */
1602                                 netif_wake_queue(dev);  /* Inform upper layers. */
1603                         }
1604                         if (status & TX_UNDERRUN) {
1605                                 if (net_debug > 0) printk("%s: transmit underrun\n", dev->name);
1606                                 lp->send_underrun++;
1607                                 if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
1608                                 else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
1609                                 /* transmit cycle is done, although
1610                                    frame wasn't transmitted - this
1611                                    avoids having to wait for the upper
1612                                    layers to timeout on us, in the
1613                                    event of a tx underrun */
1614                                 netif_wake_queue(dev);  /* Inform upper layers. */
1615                         }
1616 #if ALLOW_DMA
1617                         if (lp->use_dma && (status & RX_DMA)) {
1618                                 int count = readreg(dev, PP_DmaFrameCnt);
1619                                 while(count) {
1620                                         if (net_debug > 5)
1621                                                 printk("%s: receiving %d DMA frames\n", dev->name, count);
1622                                         if (net_debug > 2 && count >1)
1623                                                 printk("%s: receiving %d DMA frames\n", dev->name, count);
1624                                         dma_rx(dev);
1625                                         if (--count == 0)
1626                                                 count = readreg(dev, PP_DmaFrameCnt);
1627                                         if (net_debug > 2 && count > 0)
1628                                                 printk("%s: continuing with %d DMA frames\n", dev->name, count);
1629                                 }
1630                         }
1631 #endif
1632                         break;
1633                 case ISQ_RX_MISS_EVENT:
1634                         lp->stats.rx_missed_errors += (status >>6);
1635                         break;
1636                 case ISQ_TX_COL_EVENT:
1637                         lp->stats.collisions += (status >>6);
1638                         break;
1639                 }
1640         }
1641         return IRQ_RETVAL(handled);
1642 }
1643
1644 static void
1645 count_rx_errors(int status, struct net_local *lp)
1646 {
1647         lp->stats.rx_errors++;
1648         if (status & RX_RUNT) lp->stats.rx_length_errors++;
1649         if (status & RX_EXTRA_DATA) lp->stats.rx_length_errors++;
1650         if (status & RX_CRC_ERROR) if (!(status & (RX_EXTRA_DATA|RX_RUNT)))
1651                 /* per str 172 */
1652                 lp->stats.rx_crc_errors++;
1653         if (status & RX_DRIBBLE) lp->stats.rx_frame_errors++;
1654         return;
1655 }
1656
1657 /* We have a good packet(s), get it/them out of the buffers. */
1658 static void
1659 net_rx(struct net_device *dev)
1660 {
1661         struct net_local *lp = netdev_priv(dev);
1662         struct sk_buff *skb;
1663         int status, length;
1664
1665         int ioaddr = dev->base_addr;
1666         status = readword(ioaddr, RX_FRAME_PORT);
1667         length = readword(ioaddr, RX_FRAME_PORT);
1668
1669         if ((status & RX_OK) == 0) {
1670                 count_rx_errors(status, lp);
1671                 return;
1672         }
1673
1674         /* Malloc up new buffer. */
1675         skb = dev_alloc_skb(length + 2);
1676         if (skb == NULL) {
1677 #if 0           /* Again, this seems a cruel thing to do */
1678                 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
1679 #endif
1680                 lp->stats.rx_dropped++;
1681                 return;
1682         }
1683         skb_reserve(skb, 2);    /* longword align L3 header */
1684         skb->dev = dev;
1685
1686         readwords(ioaddr, RX_FRAME_PORT, skb_put(skb, length), length >> 1);
1687         if (length & 1)
1688                 skb->data[length-1] = readword(ioaddr, RX_FRAME_PORT);
1689
1690         if (net_debug > 3) {
1691                 printk( "%s: received %d byte packet of type %x\n",
1692                         dev->name, length,
1693                         (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1694         }
1695
1696         skb->protocol=eth_type_trans(skb,dev);
1697         netif_rx(skb);
1698         dev->last_rx = jiffies;
1699         lp->stats.rx_packets++;
1700         lp->stats.rx_bytes += length;
1701 }
1702
1703 #if ALLOW_DMA
1704 static void release_dma_buff(struct net_local *lp)
1705 {
1706         if (lp->dma_buff) {
1707                 free_pages((unsigned long)(lp->dma_buff), get_order(lp->dmasize * 1024));
1708                 lp->dma_buff = NULL;
1709         }
1710 }
1711 #endif
1712
1713 /* The inverse routine to net_open(). */
1714 static int
1715 net_close(struct net_device *dev)
1716 {
1717 #if ALLOW_DMA
1718         struct net_local *lp = netdev_priv(dev);
1719 #endif
1720
1721         netif_stop_queue(dev);
1722         
1723         writereg(dev, PP_RxCFG, 0);
1724         writereg(dev, PP_TxCFG, 0);
1725         writereg(dev, PP_BufCFG, 0);
1726         writereg(dev, PP_BusCTL, 0);
1727
1728         free_irq(dev->irq, dev);
1729
1730 #if ALLOW_DMA
1731         if (lp->use_dma && lp->dma) {
1732                 free_dma(dev->dma);
1733                 release_dma_buff(lp);
1734         }
1735 #endif
1736
1737         /* Update the statistics here. */
1738         return 0;
1739 }
1740
1741 /* Get the current statistics.  This may be called with the card open or
1742    closed. */
1743 static struct net_device_stats *
1744 net_get_stats(struct net_device *dev)
1745 {
1746         struct net_local *lp = netdev_priv(dev);
1747         unsigned long flags;
1748
1749         spin_lock_irqsave(&lp->lock, flags);
1750         /* Update the statistics from the device registers. */
1751         lp->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
1752         lp->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
1753         spin_unlock_irqrestore(&lp->lock, flags);
1754
1755         return &lp->stats;
1756 }
1757
1758 static void set_multicast_list(struct net_device *dev)
1759 {
1760         struct net_local *lp = netdev_priv(dev);
1761         unsigned long flags;
1762
1763         spin_lock_irqsave(&lp->lock, flags);
1764         if(dev->flags&IFF_PROMISC)
1765         {
1766                 lp->rx_mode = RX_ALL_ACCEPT;
1767         }
1768         else if((dev->flags&IFF_ALLMULTI)||dev->mc_list)
1769         {
1770                 /* The multicast-accept list is initialized to accept-all, and we
1771                    rely on higher-level filtering for now. */
1772                 lp->rx_mode = RX_MULTCAST_ACCEPT;
1773         } 
1774         else
1775                 lp->rx_mode = 0;
1776
1777         writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
1778
1779         /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
1780         writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
1781              (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
1782         spin_unlock_irqrestore(&lp->lock, flags);
1783 }
1784
1785
1786 static int set_mac_address(struct net_device *dev, void *p)
1787 {
1788         int i;
1789         struct sockaddr *addr = p;
1790
1791
1792         if (netif_running(dev))
1793                 return -EBUSY;
1794
1795         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1796
1797         if (net_debug) {
1798                 printk("%s: Setting MAC address to ", dev->name);
1799                 for (i = 0; i < dev->addr_len; i++)
1800                         printk(" %2.2x", dev->dev_addr[i]);
1801                 printk(".\n");
1802         }
1803         /* set the Ethernet address */
1804         for (i=0; i < ETH_ALEN/2; i++)
1805                 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1806
1807         return 0;
1808 }
1809
1810 #ifdef MODULE
1811
1812 static struct net_device *dev_cs89x0;
1813
1814 /*
1815  * Support the 'debug' module parm even if we're compiled for non-debug to 
1816  * avoid breaking someone's startup scripts 
1817  */
1818
1819 static int io;
1820 static int irq;
1821 static int debug;
1822 static char media[8];
1823 static int duplex=-1;
1824
1825 static int use_dma;                     /* These generate unused var warnings if ALLOW_DMA = 0 */
1826 static int dma;
1827 static int dmasize=16;                  /* or 64 */
1828
1829 module_param(io, int, 0);
1830 module_param(irq, int, 0);
1831 module_param(debug, int, 0);
1832 module_param_string(media, media, sizeof(media), 0);
1833 module_param(duplex, int, 0);
1834 module_param(dma , int, 0);
1835 module_param(dmasize , int, 0);
1836 module_param(use_dma , int, 0);
1837 MODULE_PARM_DESC(io, "cs89x0 I/O base address");
1838 MODULE_PARM_DESC(irq, "cs89x0 IRQ number");
1839 #if DEBUGGING
1840 MODULE_PARM_DESC(debug, "cs89x0 debug level (0-6)");
1841 #else
1842 MODULE_PARM_DESC(debug, "(ignored)");
1843 #endif
1844 MODULE_PARM_DESC(media, "Set cs89x0 adapter(s) media type(s) (rj45,bnc,aui)");
1845 /* No other value than -1 for duplex seems to be currently interpreted */
1846 MODULE_PARM_DESC(duplex, "(ignored)");
1847 #if ALLOW_DMA
1848 MODULE_PARM_DESC(dma , "cs89x0 ISA DMA channel; ignored if use_dma=0");
1849 MODULE_PARM_DESC(dmasize , "cs89x0 DMA size in kB (16,64); ignored if use_dma=0");
1850 MODULE_PARM_DESC(use_dma , "cs89x0 using DMA (0-1)");
1851 #else
1852 MODULE_PARM_DESC(dma , "(ignored)");
1853 MODULE_PARM_DESC(dmasize , "(ignored)");
1854 MODULE_PARM_DESC(use_dma , "(ignored)");
1855 #endif
1856
1857 MODULE_AUTHOR("Mike Cruse, Russwll Nelson <nelson@crynwr.com>, Andrew Morton <andrewm@uow.edu.au>");
1858 MODULE_LICENSE("GPL");
1859
1860
1861 /*
1862 * media=t             - specify media type
1863    or media=2
1864    or media=aui
1865    or medai=auto
1866 * duplex=0            - specify forced half/full/autonegotiate duplex
1867 * debug=#             - debug level
1868
1869
1870 * Default Chip Configuration:
1871   * DMA Burst = enabled
1872   * IOCHRDY Enabled = enabled
1873     * UseSA = enabled
1874     * CS8900 defaults to half-duplex if not specified on command-line
1875     * CS8920 defaults to autoneg if not specified on command-line
1876     * Use reset defaults for other config parameters
1877
1878 * Assumptions:
1879   * media type specified is supported (circuitry is present)
1880   * if memory address is > 1MB, then required mem decode hw is present
1881   * if 10B-2, then agent other than driver will enable DC/DC converter
1882     (hw or software util)
1883
1884
1885 */
1886
1887 int
1888 init_module(void)
1889 {
1890         struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
1891         struct net_local *lp;
1892         int ret = 0;
1893
1894 #if DEBUGGING
1895         net_debug = debug;
1896 #else
1897         debug = 0;
1898 #endif
1899         if (!dev)
1900                 return -ENOMEM;
1901
1902         dev->irq = irq;
1903         dev->base_addr = io;
1904         lp = netdev_priv(dev);
1905
1906 #if ALLOW_DMA
1907         if (use_dma) {
1908                 lp->use_dma = use_dma;
1909                 lp->dma = dma;
1910                 lp->dmasize = dmasize;
1911         }
1912 #endif
1913
1914         spin_lock_init(&lp->lock);
1915
1916         /* boy, they'd better get these right */
1917         if (!strcmp(media, "rj45"))
1918                 lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1919         else if (!strcmp(media, "aui"))
1920                 lp->adapter_cnf = A_CNF_MEDIA_AUI   | A_CNF_AUI;
1921         else if (!strcmp(media, "bnc"))
1922                 lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
1923         else
1924                 lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1925
1926         if (duplex==-1)
1927                 lp->auto_neg_cnf = AUTO_NEG_ENABLE;
1928
1929         if (io == 0) {
1930                 printk(KERN_ERR "cs89x0.c: Module autoprobing not allowed.\n");
1931                 printk(KERN_ERR "cs89x0.c: Append io=0xNNN\n");
1932                 ret = -EPERM;
1933                 goto out;
1934         } else if (io <= 0x1ff) {
1935                 ret = -ENXIO;
1936                 goto out;
1937         }
1938
1939 #if ALLOW_DMA
1940         if (use_dma && dmasize != 16 && dmasize != 64) {
1941                 printk(KERN_ERR "cs89x0.c: dma size must be either 16K or 64K, not %dK\n", dmasize);
1942                 ret = -EPERM;
1943                 goto out;
1944         }
1945 #endif
1946         ret = cs89x0_probe1(dev, io, 1);
1947         if (ret)
1948                 goto out;
1949
1950         dev_cs89x0 = dev;
1951         return 0;
1952 out:
1953         free_netdev(dev);
1954         return ret;
1955 }
1956
1957 void
1958 cleanup_module(void)
1959 {
1960         unregister_netdev(dev_cs89x0);
1961         writeword(dev_cs89x0->base_addr, ADD_PORT, PP_ChipID);
1962         release_region(dev_cs89x0->base_addr, NETCARD_IO_EXTENT);
1963         free_netdev(dev_cs89x0);
1964 }
1965 #endif /* MODULE */
1966 \f
1967 /*
1968  * Local variables:
1969  *  version-control: t
1970  *  kept-new-versions: 5
1971  *  c-indent-level: 8
1972  *  tab-width: 8
1973  * End:
1974  *
1975  */