1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91#include <linux/config.h>
92#include <linux/module.h>
93
94
95
96
97
98
99
100#define ALLOW_DMA 1
101
102
103
104
105
106#define DEBUGGING 1
107
108
109
110
111
112
113
114
115
116
117#include <linux/errno.h>
118#include <linux/netdevice.h>
119#include <linux/etherdevice.h>
120#include <linux/kernel.h>
121#include <linux/types.h>
122#include <linux/fcntl.h>
123#include <linux/interrupt.h>
124#include <linux/ioport.h>
125#include <linux/in.h>
126#include <linux/skbuff.h>
127#include <linux/slab.h>
128#include <linux/spinlock.h>
129#include <linux/string.h>
130#include <linux/init.h>
131
132#include <asm/system.h>
133#include <asm/bitops.h>
134#include <asm/io.h>
135#if ALLOW_DMA
136#include <asm/dma.h>
137#endif
138
139#include "cs89x0.h"
140
141static char version[] __initdata =
142"cs89x0.c: v2.4.3-pre1 Russell Nelson <nelson@crynwr.com>, Andrew Morton <andrewm@uow.edu.au>\n";
143
144
145
146
147
148
149
150
151
152
153
154
155#ifdef CONFIG_ARCH_CLPS7500
156static unsigned int netcard_portlist[] __initdata =
157 { 0x80090303, 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
158static unsigned int cs8900_irq_map[] = {12,0,0,0};
159#elif defined(CONFIG_SH_HICOSH4)
160static unsigned int netcard_portlist[] __initdata =
161 { 0x0300, 0};
162static unsigned int cs8900_irq_map[] = {1,0,0,0};
163#else
164static unsigned int netcard_portlist[] __initdata =
165 { 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
166static unsigned int cs8900_irq_map[] = {10,11,12,5};
167#endif
168
169#if DEBUGGING
170static unsigned int net_debug = DEBUGGING;
171#else
172#define net_debug 0
173#endif
174
175
176#define NETCARD_IO_EXTENT 16
177
178
179#define FORCE_RJ45 0x0001
180#define FORCE_AUI 0x0002
181#define FORCE_BNC 0x0004
182
183#define FORCE_AUTO 0x0010
184#define FORCE_HALF 0x0020
185#define FORCE_FULL 0x0030
186
187
188struct net_local {
189 struct net_device_stats stats;
190 int chip_type;
191 char chip_revision;
192 int send_cmd;
193 int auto_neg_cnf;
194 int adapter_cnf;
195 int isa_config;
196 int irq_map;
197 int rx_mode;
198 int curr_rx_cfg;
199 int linectl;
200 int send_underrun;
201 int force;
202 spinlock_t lock;
203#if ALLOW_DMA
204 int use_dma;
205 int dma;
206 int dmasize;
207 unsigned char *dma_buff;
208 unsigned char *end_dma_buff;
209 unsigned char *rx_dma_ptr;
210#endif
211};
212
213
214
215extern int cs89x0_probe(struct net_device *dev);
216
217static int cs89x0_probe1(struct net_device *dev, int ioaddr);
218static int net_open(struct net_device *dev);
219static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
220static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
221static void set_multicast_list(struct net_device *dev);
222static void net_timeout(struct net_device *dev);
223static void net_rx(struct net_device *dev);
224static int net_close(struct net_device *dev);
225static struct net_device_stats *net_get_stats(struct net_device *dev);
226static void reset_chip(struct net_device *dev);
227static int get_eeprom_data(struct net_device *dev, int off, int len, int *buffer);
228static int get_eeprom_cksum(int off, int len, int *buffer);
229static int set_mac_address(struct net_device *dev, void *addr);
230static void count_rx_errors(int status, struct net_local *lp);
231#if ALLOW_DMA
232static void get_dma_channel(struct net_device *dev);
233static void release_dma_buff(struct net_local *lp);
234#endif
235
236
237#define tx_done(dev) 1
238
239
240
241
242#if !defined(MODULE) && (ALLOW_DMA != 0)
243static int g_cs89x0_dma;
244
245static int __init dma_fn(char *str)
246{
247 g_cs89x0_dma = simple_strtol(str,NULL,0);
248 return 1;
249}
250
251__setup("cs89x0_dma=", dma_fn);
252#endif
253
254#ifndef MODULE
255static int g_cs89x0_media__force;
256
257static int __init media_fn(char *str)
258{
259 if (!strcmp(str, "rj45")) g_cs89x0_media__force = FORCE_RJ45;
260 else if (!strcmp(str, "aui")) g_cs89x0_media__force = FORCE_AUI;
261 else if (!strcmp(str, "bnc")) g_cs89x0_media__force = FORCE_BNC;
262 return 1;
263}
264
265__setup("cs89x0_media=", media_fn);
266#endif
267
268
269
270
271
272
273
274
275
276
277int __init cs89x0_probe(struct net_device *dev)
278{
279 int i;
280 int base_addr = dev ? dev->base_addr : 0;
281
282 SET_MODULE_OWNER(dev);
283
284 if (net_debug)
285 printk("cs89x0:cs89x0_probe(0x%x)\n", base_addr);
286
287 if (base_addr > 0x1ff)
288 return cs89x0_probe1(dev, base_addr);
289 else if (base_addr != 0)
290 return -ENXIO;
291
292 for (i = 0; netcard_portlist[i]; i++) {
293 if (cs89x0_probe1(dev, netcard_portlist[i]) == 0)
294 return 0;
295 }
296 printk(KERN_WARNING "cs89x0: no cs8900 or cs8920 detected. Be sure to disable PnP with SETUP\n");
297 return -ENODEV;
298}
299
300static int
301readreg(struct net_device *dev, int portno)
302{
303 outw(portno, dev->base_addr + ADD_PORT);
304 return inw(dev->base_addr + DATA_PORT);
305}
306
307static void
308writereg(struct net_device *dev, int portno, int value)
309{
310 outw(portno, dev->base_addr + ADD_PORT);
311 outw(value, dev->base_addr + DATA_PORT);
312}
313
314static int
315readword(struct net_device *dev, int portno)
316{
317 return inw(dev->base_addr + portno);
318}
319
320static void
321writeword(struct net_device *dev, int portno, int value)
322{
323 outw(value, dev->base_addr + portno);
324}
325
326static int __init
327wait_eeprom_ready(struct net_device *dev)
328{
329 int timeout = jiffies;
330
331
332
333 while(readreg(dev, PP_SelfST) & SI_BUSY)
334 if (jiffies - timeout >= 40)
335 return -1;
336 return 0;
337}
338
339static int __init
340get_eeprom_data(struct net_device *dev, int off, int len, int *buffer)
341{
342 int i;
343
344 if (net_debug > 3) printk("EEPROM data from %x for %x:\n",off,len);
345 for (i = 0; i < len; i++) {
346 if (wait_eeprom_ready(dev) < 0) return -1;
347
348 writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
349 if (wait_eeprom_ready(dev) < 0) return -1;
350 buffer[i] = readreg(dev, PP_EEData);
351 if (net_debug > 3) printk("%04x ", buffer[i]);
352 }
353 if (net_debug > 3) printk("\n");
354 return 0;
355}
356
357static int __init
358get_eeprom_cksum(int off, int len, int *buffer)
359{
360 int i, cksum;
361
362 cksum = 0;
363 for (i = 0; i < len; i++)
364 cksum += buffer[i];
365 cksum &= 0xffff;
366 if (cksum == 0)
367 return 0;
368 return -1;
369}
370
371
372
373
374
375
376
377static int __init
378cs89x0_probe1(struct net_device *dev, int ioaddr)
379{
380 struct net_local *lp;
381 static unsigned version_printed;
382 int i;
383 unsigned rev_type = 0;
384 int eeprom_buff[CHKSUM_LEN];
385 int retval;
386
387
388 if (dev->priv == NULL) {
389 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
390 if (dev->priv == 0) {
391 retval = -ENOMEM;
392 goto out;
393 }
394 lp = (struct net_local *)dev->priv;
395 memset(lp, 0, sizeof(*lp));
396 spin_lock_init(&lp->lock);
397#if !defined(MODULE) && (ALLOW_DMA != 0)
398 if (g_cs89x0_dma) {
399 lp->use_dma = 1;
400 lp->dma = g_cs89x0_dma;
401 lp->dmasize = 16;
402 }
403#endif
404#ifndef MODULE
405 lp->force = g_cs89x0_media__force;
406#endif
407 }
408 lp = (struct net_local *)dev->priv;
409
410
411 if (!request_region(ioaddr & ~3, NETCARD_IO_EXTENT, dev->name)) {
412 printk(KERN_ERR "%s: request_region(0x%x, 0x%x) failed\n",
413 dev->name, ioaddr, NETCARD_IO_EXTENT);
414 retval = -EBUSY;
415 goto out1;
416 }
417
418#ifdef CONFIG_SH_HICOSH4
419
420 outw(0x0114, ioaddr + ADD_PORT);
421 outw(0x0040, ioaddr + DATA_PORT);
422#endif
423
424
425
426
427
428 if (ioaddr & 1) {
429 if (net_debug > 1)
430 printk(KERN_INFO "%s: odd ioaddr 0x%x\n", dev->name, ioaddr);
431 if ((ioaddr & 2) != 2)
432 if ((inw((ioaddr & ~3)+ ADD_PORT) & ADD_MASK) != ADD_SIG) {
433 printk(KERN_ERR "%s: bad signature 0x%x\n",
434 dev->name, inw((ioaddr & ~3)+ ADD_PORT));
435 retval = -ENODEV;
436 goto out2;
437 }
438 ioaddr &= ~3;
439 outw(PP_ChipID, ioaddr + ADD_PORT);
440 }
441printk("PP_addr=0x%x\n", inw(ioaddr + ADD_PORT));
442
443 if (inw(ioaddr + DATA_PORT) != CHIP_EISA_ID_SIG) {
444 printk(KERN_ERR "%s: incorrect signature 0x%x\n",
445 dev->name, inw(ioaddr + DATA_PORT));
446 retval = -ENODEV;
447 goto out2;
448 }
449
450
451 dev->base_addr = ioaddr;
452
453
454 rev_type = readreg(dev, PRODUCT_ID_ADD);
455 lp->chip_type = rev_type &~ REVISON_BITS;
456 lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
457
458
459
460 lp->send_cmd = TX_AFTER_381;
461 if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
462 lp->send_cmd = TX_NOW;
463 if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
464 lp->send_cmd = TX_NOW;
465
466 if (net_debug && version_printed++ == 0)
467 printk(version);
468
469 printk(KERN_INFO "%s: cs89%c0%s rev %c found at %#3lx ",
470 dev->name,
471 lp->chip_type==CS8900?'0':'2',
472 lp->chip_type==CS8920M?"M":"",
473 lp->chip_revision,
474 dev->base_addr);
475
476 reset_chip(dev);
477
478
479
480
481
482
483
484
485#ifdef CONFIG_SH_HICOSH4
486 if (1) {
487
488
489
490 __u16 *confd;
491 short cnt;
492 if (((* (volatile __u32 *) 0xa0013ff0) & 0x00ffffff)
493 == 0x006c3000) {
494 confd = (__u16*) 0xa0013fc0;
495 } else {
496 confd = (__u16*) 0xa001ffc0;
497 }
498 cnt = (*confd++ & 0x00ff) >> 1;
499 while (--cnt > 0) {
500 __u16 j = *confd++;
501
502 switch (j & 0x0fff) {
503 case PP_IA:
504 for (i = 0; i < ETH_ALEN/2; i++) {
505 dev->dev_addr[i*2] = confd[i] & 0xFF;
506 dev->dev_addr[i*2+1] = confd[i] >> 8;
507 }
508 break;
509 }
510 j = (j >> 12) + 1;
511 confd += j;
512 cnt -= j;
513 }
514 } else
515#endif
516
517 if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) ==
518 (EEPROM_OK|EEPROM_PRESENT)) {
519
520 for (i=0; i < ETH_ALEN/2; i++) {
521 unsigned int Addr;
522 Addr = readreg(dev, PP_IA+i*2);
523 dev->dev_addr[i*2] = Addr & 0xFF;
524 dev->dev_addr[i*2+1] = Addr >> 8;
525 }
526
527
528
529
530
531
532
533
534
535
536
537 lp->adapter_cnf = 0;
538 i = readreg(dev, PP_LineCTL);
539
540 if ((i & (HCB1 | HCB1_ENBL)) == (HCB1 | HCB1_ENBL))
541 lp->adapter_cnf |= A_CNF_DC_DC_POLARITY;
542
543 if ((i & LOW_RX_SQUELCH) == LOW_RX_SQUELCH)
544 lp->adapter_cnf |= A_CNF_EXTND_10B_2 | A_CNF_LOW_RX_SQUELCH;
545
546 if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == 0)
547 lp->adapter_cnf |= A_CNF_10B_T | A_CNF_MEDIA_10B_T;
548
549 if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUI_ONLY)
550 lp->adapter_cnf |= A_CNF_AUI | A_CNF_MEDIA_AUI;
551
552 if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUTO_AUI_10BASET)
553 lp->adapter_cnf |= A_CNF_AUI | A_CNF_10B_T |
554 A_CNF_MEDIA_AUI | A_CNF_MEDIA_10B_T | A_CNF_MEDIA_AUTO;
555
556 if (net_debug > 1)
557 printk(KERN_INFO "%s: PP_LineCTL=0x%x, adapter_cnf=0x%x\n",
558 dev->name, i, lp->adapter_cnf);
559
560
561 if (lp->chip_type == CS8900)
562 lp->isa_config = readreg(dev, PP_CS8900_ISAINT) & INT_NO_MASK;
563
564 printk( "[Cirrus EEPROM] ");
565 }
566
567 printk("\n");
568
569
570#ifdef CONFIG_SH_HICOSH4
571 if (1) {
572 printk(KERN_NOTICE "cs89x0: No EEPROM on HiCO.SH4\n");
573 } else
574#endif
575 if ((readreg(dev, PP_SelfST) & EEPROM_PRESENT) == 0)
576 printk(KERN_WARNING "cs89x0: No EEPROM, relying on command line....\n");
577 else if (get_eeprom_data(dev, START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
578 printk(KERN_WARNING "\ncs89x0: EEPROM read failed, relying on command line.\n");
579 } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
580
581
582 if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) !=
583 (EEPROM_OK|EEPROM_PRESENT))
584 printk(KERN_WARNING "cs89x0: Extended EEPROM checksum bad and no Cirrus EEPROM, relying on command line\n");
585
586 } else {
587
588
589
590
591 if (!lp->auto_neg_cnf) lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
592
593 if (!lp->adapter_cnf) lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
594
595 lp->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
596 dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET/2] << 8;
597
598
599
600 for (i = 0; i < ETH_ALEN/2; i++) {
601 dev->dev_addr[i*2] = eeprom_buff[i];
602 dev->dev_addr[i*2+1] = eeprom_buff[i] >> 8;
603 }
604 if (net_debug > 1)
605 printk(KERN_DEBUG "%s: new adapter_cnf: 0x%x\n",
606 dev->name, lp->adapter_cnf);
607 }
608
609
610 {
611 int count = 0;
612 if (lp->force & FORCE_RJ45) {lp->adapter_cnf |= A_CNF_10B_T; count++; }
613 if (lp->force & FORCE_AUI) {lp->adapter_cnf |= A_CNF_AUI; count++; }
614 if (lp->force & FORCE_BNC) {lp->adapter_cnf |= A_CNF_10B_2; count++; }
615 if (count > 1) {lp->adapter_cnf |= A_CNF_MEDIA_AUTO; }
616 else if (lp->force & FORCE_RJ45){lp->adapter_cnf |= A_CNF_MEDIA_10B_T; }
617 else if (lp->force & FORCE_AUI) {lp->adapter_cnf |= A_CNF_MEDIA_AUI; }
618 else if (lp->force & FORCE_BNC) {lp->adapter_cnf |= A_CNF_MEDIA_10B_2; }
619 }
620
621 if (net_debug > 1)
622 printk(KERN_DEBUG "%s: after force 0x%x, adapter_cnf=0x%x\n",
623 dev->name, lp->force, lp->adapter_cnf);
624
625
626
627
628
629
630
631
632 printk(KERN_INFO "cs89x0 media %s%s%s",
633 (lp->adapter_cnf & A_CNF_10B_T)?"RJ-45,":"",
634 (lp->adapter_cnf & A_CNF_AUI)?"AUI,":"",
635 (lp->adapter_cnf & A_CNF_10B_2)?"BNC,":"");
636
637 lp->irq_map = 0xffff;
638
639
640 if (lp->chip_type != CS8900 &&
641
642 (i = readreg(dev, PP_CS8920_ISAINT) & 0xff,
643 (i != 0 && i < CS8920_NO_INTS))) {
644 if (!dev->irq)
645 dev->irq = i;
646 } else {
647 i = lp->isa_config & INT_NO_MASK;
648 if (lp->chip_type == CS8900) {
649
650 if (i >= sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]))
651 printk("\ncs89x0: invalid ISA interrupt number %d\n", i);
652 else
653 i = cs8900_irq_map[i];
654
655 lp->irq_map = CS8900_IRQ_MAP;
656 } else {
657 int irq_map_buff[IRQ_MAP_LEN/2];
658
659 if (get_eeprom_data(dev, IRQ_MAP_EEPROM_DATA,
660 IRQ_MAP_LEN/2,
661 irq_map_buff) >= 0) {
662 if ((irq_map_buff[0] & 0xff) == PNP_IRQ_FRMT)
663 lp->irq_map = (irq_map_buff[0]>>8) | (irq_map_buff[1] << 8);
664 }
665 }
666 if (!dev->irq)
667 dev->irq = i;
668 }
669
670 printk(" IRQ %d", dev->irq);
671
672#if ALLOW_DMA
673 if (lp->use_dma) {
674 get_dma_channel(dev);
675 printk(", DMA %d", dev->dma);
676 }
677 else
678#endif
679 {
680 printk(", programmed I/O");
681 }
682
683
684 printk(", MAC");
685 for (i = 0; i < ETH_ALEN; i++)
686 {
687 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
688 }
689
690 dev->open = net_open;
691 dev->stop = net_close;
692 dev->tx_timeout = net_timeout;
693 dev->watchdog_timeo = HZ;
694 dev->hard_start_xmit = net_send_packet;
695 dev->get_stats = net_get_stats;
696 dev->set_multicast_list = set_multicast_list;
697 dev->set_mac_address = set_mac_address;
698
699
700 ether_setup(dev);
701
702 printk("\n");
703 if (net_debug)
704 printk("cs89x0_probe1() successful\n");
705 return 0;
706out2:
707 release_region(ioaddr & ~3, NETCARD_IO_EXTENT);
708out1:
709 kfree(dev->priv);
710 dev->priv = 0;
711out:
712 return retval;
713}
714
715
716
717
718
719
720#if ALLOW_DMA
721
722#define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
723
724static void
725get_dma_channel(struct net_device *dev)
726{
727 struct net_local *lp = (struct net_local *)dev->priv;
728
729 if (lp->dma) {
730 dev->dma = lp->dma;
731 lp->isa_config |= ISA_RxDMA;
732 } else {
733 if ((lp->isa_config & ANY_ISA_DMA) == 0)
734 return;
735 dev->dma = lp->isa_config & DMA_NO_MASK;
736 if (lp->chip_type == CS8900)
737 dev->dma += 5;
738 if (dev->dma < 5 || dev->dma > 7) {
739 lp->isa_config &= ~ANY_ISA_DMA;
740 return;
741 }
742 }
743 return;
744}
745
746static void
747write_dma(struct net_device *dev, int chip_type, int dma)
748{
749 struct net_local *lp = (struct net_local *)dev->priv;
750 if ((lp->isa_config & ANY_ISA_DMA) == 0)
751 return;
752 if (chip_type == CS8900) {
753 writereg(dev, PP_CS8900_ISADMA, dma-5);
754 } else {
755 writereg(dev, PP_CS8920_ISADMA, dma);
756 }
757}
758
759static void
760set_dma_cfg(struct net_device *dev)
761{
762 struct net_local *lp = (struct net_local *)dev->priv;
763
764 if (lp->use_dma) {
765 if ((lp->isa_config & ANY_ISA_DMA) == 0) {
766 if (net_debug > 3)
767 printk("set_dma_cfg(): no DMA\n");
768 return;
769 }
770 if (lp->isa_config & ISA_RxDMA) {
771 lp->curr_rx_cfg |= RX_DMA_ONLY;
772 if (net_debug > 3)
773 printk("set_dma_cfg(): RX_DMA_ONLY\n");
774 } else {
775 lp->curr_rx_cfg |= AUTO_RX_DMA;
776 if (net_debug > 3)
777 printk("set_dma_cfg(): AUTO_RX_DMA\n");
778 }
779 }
780}
781
782static int
783dma_bufcfg(struct net_device *dev)
784{
785 struct net_local *lp = (struct net_local *)dev->priv;
786 if (lp->use_dma)
787 return (lp->isa_config & ANY_ISA_DMA)? RX_DMA_ENBL : 0;
788 else
789 return 0;
790}
791
792static int
793dma_busctl(struct net_device *dev)
794{
795 int retval = 0;
796 struct net_local *lp = (struct net_local *)dev->priv;
797 if (lp->use_dma) {
798 if (lp->isa_config & ANY_ISA_DMA)
799 retval |= RESET_RX_DMA;
800 if (lp->isa_config & DMA_BURST)
801 retval |= DMA_BURST_MODE;
802 if (lp->dmasize == 64)
803 retval |= RX_DMA_SIZE_64K;
804 retval |= MEMORY_ON;
805 }
806 return retval;
807}
808
809static void
810dma_rx(struct net_device *dev)
811{
812 struct net_local *lp = (struct net_local *)dev->priv;
813 struct sk_buff *skb;
814 int status, length;
815 unsigned char *bp = lp->rx_dma_ptr;
816
817 status = bp[0] + (bp[1]<<8);
818 length = bp[2] + (bp[3]<<8);
819 bp += 4;
820 if (net_debug > 5) {
821 printk( "%s: receiving DMA packet at %lx, status %x, length %x\n",
822 dev->name, (unsigned long)bp, status, length);
823 }
824 if ((status & RX_OK) == 0) {
825 count_rx_errors(status, lp);
826 goto skip_this_frame;
827 }
828
829
830 skb = dev_alloc_skb(length + 2);
831 if (skb == NULL) {
832 if (net_debug)
833 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
834 lp->stats.rx_dropped++;
835
836
837skip_this_frame:
838 bp += (length + 3) & ~3;
839 if (bp >= lp->end_dma_buff) bp -= lp->dmasize*1024;
840 lp->rx_dma_ptr = bp;
841 return;
842 }
843 skb_reserve(skb, 2);
844 skb->dev = dev;
845
846 if (bp + length > lp->end_dma_buff) {
847 int semi_cnt = lp->end_dma_buff - bp;
848 memcpy(skb_put(skb,semi_cnt), bp, semi_cnt);
849 memcpy(skb_put(skb,length - semi_cnt), lp->dma_buff,
850 length - semi_cnt);
851 } else {
852 memcpy(skb_put(skb,length), bp, length);
853 }
854 bp += (length + 3) & ~3;
855 if (bp >= lp->end_dma_buff) bp -= lp->dmasize*1024;
856 lp->rx_dma_ptr = bp;
857
858 if (net_debug > 3) {
859 printk( "%s: received %d byte DMA packet of type %x\n",
860 dev->name, length,
861 (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
862 }
863 skb->protocol=eth_type_trans(skb,dev);
864 netif_rx(skb);
865 dev->last_rx = jiffies;
866 lp->stats.rx_packets++;
867 lp->stats.rx_bytes += length;
868}
869
870#endif
871
872void __init reset_chip(struct net_device *dev)
873{
874 struct net_local *lp = (struct net_local *)dev->priv;
875 int ioaddr = dev->base_addr;
876 int reset_start_time;
877
878 writereg(dev, PP_SelfCTL, readreg(dev, PP_SelfCTL) | POWER_ON_RESET);
879
880
881 current->state = TASK_INTERRUPTIBLE;
882 schedule_timeout(30*HZ/1000);
883
884 if (lp->chip_type != CS8900) {
885
886 outw(PP_CS8920_ISAINT, ioaddr + ADD_PORT);
887 outb(dev->irq, ioaddr + DATA_PORT);
888 outb(0, ioaddr + DATA_PORT + 1);
889
890 outw(PP_CS8920_ISAMemB, ioaddr + ADD_PORT);
891 outb((dev->mem_start >> 16) & 0xff, ioaddr + DATA_PORT);
892 outb((dev->mem_start >> 8) & 0xff, ioaddr + DATA_PORT + 1);
893 }
894
895 reset_start_time = jiffies;
896 while( (readreg(dev, PP_SelfST) & INIT_DONE) == 0 && jiffies - reset_start_time < 2)
897 ;
898}
899
900
901static void
902control_dc_dc(struct net_device *dev, int on_not_off)
903{
904 struct net_local *lp = (struct net_local *)dev->priv;
905 unsigned int selfcontrol;
906 int timenow = jiffies;
907
908
909
910
911 selfcontrol = HCB1_ENBL;
912 if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
913 selfcontrol |= HCB1;
914 else
915 selfcontrol &= ~HCB1;
916 writereg(dev, PP_SelfCTL, selfcontrol);
917
918
919 while (jiffies - timenow < HZ)
920 ;
921}
922
923#define DETECTED_NONE 0
924#define DETECTED_RJ45H 1
925#define DETECTED_RJ45F 2
926#define DETECTED_AUI 3
927#define DETECTED_BNC 4
928
929static int
930detect_tp(struct net_device *dev)
931{
932 struct net_local *lp = (struct net_local *)dev->priv;
933 int timenow = jiffies;
934 int fdx;
935
936 if (net_debug > 1) printk("%s: Attempting TP\n", dev->name);
937
938
939
940
941
942
943 writereg(dev, PP_LineCTL, lp->linectl &~ AUI_ONLY);
944 control_dc_dc(dev, 0);
945
946
947 for (timenow = jiffies; jiffies - timenow < 15; )
948 ;
949 if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
950 return DETECTED_NONE;
951
952 if (lp->chip_type == CS8900) {
953 switch (lp->force & 0xf0) {
954#if 0
955 case FORCE_AUTO:
956 printk("%s: cs8900 doesn't autonegotiate\n",dev->name);
957 return DETECTED_NONE;
958#endif
959
960 case FORCE_AUTO:
961 lp->force &= ~FORCE_AUTO;
962 lp->force |= FORCE_HALF;
963 break;
964 case FORCE_HALF:
965 break;
966 case FORCE_FULL:
967 writereg(dev, PP_TestCTL, readreg(dev, PP_TestCTL) | FDX_8900);
968 break;
969 }
970 fdx = readreg(dev, PP_TestCTL) & FDX_8900;
971 } else {
972 switch (lp->force & 0xf0) {
973 case FORCE_AUTO:
974 lp->auto_neg_cnf = AUTO_NEG_ENABLE;
975 break;
976 case FORCE_HALF:
977 lp->auto_neg_cnf = 0;
978 break;
979 case FORCE_FULL:
980 lp->auto_neg_cnf = RE_NEG_NOW | ALLOW_FDX;
981 break;
982 }
983
984 writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
985
986 if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
987 printk(KERN_INFO "%s: negotiating duplex...\n",dev->name);
988 while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
989 if (jiffies - timenow > 4000) {
990 printk(KERN_ERR "**** Full / half duplex auto-negotiation timed out ****\n");
991 break;
992 }
993 }
994 }
995 fdx = readreg(dev, PP_AutoNegST) & FDX_ACTIVE;
996 }
997 if (fdx)
998 return DETECTED_RJ45F;
999 else
1000 return DETECTED_RJ45H;
1001}
1002
1003
1004static int
1005send_test_pkt(struct net_device *dev)
1006{
1007 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
1008 0, 46,
1009 0, 0,
1010 0xf3, 0 };
1011 long timenow = jiffies;
1012
1013 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
1014
1015 memcpy(test_packet, dev->dev_addr, ETH_ALEN);
1016 memcpy(test_packet+ETH_ALEN, dev->dev_addr, ETH_ALEN);
1017
1018 writeword(dev, TX_CMD_PORT, TX_AFTER_ALL);
1019 writeword(dev, TX_LEN_PORT, ETH_ZLEN);
1020
1021
1022 while (jiffies - timenow < 5)
1023 if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
1024 break;
1025 if (jiffies - timenow >= 5)
1026 return 0;
1027
1028
1029 outsw(dev->base_addr + TX_FRAME_PORT,test_packet,(ETH_ZLEN+1) >>1);
1030
1031 if (net_debug > 1) printk("Sending test packet ");
1032
1033 for (timenow = jiffies; jiffies - timenow < 3; )
1034 ;
1035 if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
1036 if (net_debug > 1) printk("succeeded\n");
1037 return 1;
1038 }
1039 if (net_debug > 1) printk("failed\n");
1040 return 0;
1041}
1042
1043
1044static int
1045detect_aui(struct net_device *dev)
1046{
1047 struct net_local *lp = (struct net_local *)dev->priv;
1048
1049 if (net_debug > 1) printk("%s: Attempting AUI\n", dev->name);
1050 control_dc_dc(dev, 0);
1051
1052 writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
1053
1054 if (send_test_pkt(dev))
1055 return DETECTED_AUI;
1056 else
1057 return DETECTED_NONE;
1058}
1059
1060static int
1061detect_bnc(struct net_device *dev)
1062{
1063 struct net_local *lp = (struct net_local *)dev->priv;
1064
1065 if (net_debug > 1) printk("%s: Attempting BNC\n", dev->name);
1066 control_dc_dc(dev, 1);
1067
1068 writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
1069
1070 if (send_test_pkt(dev))
1071 return DETECTED_BNC;
1072 else
1073 return DETECTED_NONE;
1074}
1075
1076
1077static void
1078write_irq(struct net_device *dev, int chip_type, int irq)
1079{
1080 int i;
1081
1082 if (chip_type == CS8900) {
1083
1084 for (i = 0; i != sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]); i++)
1085 if (cs8900_irq_map[i] == irq)
1086 break;
1087
1088 if (i == sizeof(cs8900_irq_map)/sizeof(cs8900_irq_map[0]))
1089 i = 3;
1090 writereg(dev, PP_CS8900_ISAINT, i);
1091 } else {
1092 writereg(dev, PP_CS8920_ISAINT, irq);
1093 }
1094}
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106static int
1107net_open(struct net_device *dev)
1108{
1109 struct net_local *lp = (struct net_local *)dev->priv;
1110 int result = 0;
1111 int i;
1112 int ret;
1113
1114#ifndef CONFIG_SH_HICOSH4
1115 if (dev->irq < 2) {
1116
1117
1118#if 0
1119 writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ );
1120#endif
1121
1122 writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
1123
1124 for (i = 2; i < CS8920_NO_INTS; i++) {
1125 if ((1 << i) & lp->irq_map) {
1126 if (request_irq(i, net_interrupt, 0, dev->name, dev) == 0) {
1127 dev->irq = i;
1128 write_irq(dev, lp->chip_type, i);
1129
1130 break;
1131 }
1132 }
1133 }
1134
1135 if (i >= CS8920_NO_INTS) {
1136 writereg(dev, PP_BusCTL, 0);
1137 printk(KERN_ERR "cs89x0: can't get an interrupt\n");
1138 ret = -EAGAIN;
1139 goto bad_out;
1140 }
1141 }
1142 else
1143#endif
1144 {
1145 if (((1 << dev->irq) & lp->irq_map) == 0) {
1146 printk(KERN_ERR "%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
1147 dev->name, dev->irq, lp->irq_map);
1148 ret = -EAGAIN;
1149 goto bad_out;
1150 }
1151
1152 writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ );
1153
1154#if 0
1155 writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
1156#endif
1157 write_irq(dev, lp->chip_type, dev->irq);
1158 ret = request_irq(dev->irq, &net_interrupt, 0, dev->name, dev);
1159 if (ret) {
1160 if (net_debug)
1161 printk(KERN_DEBUG "cs89x0: request_irq(%d) failed\n", dev->irq);
1162 goto bad_out;
1163 }
1164 }
1165
1166#if ALLOW_DMA
1167 if (lp->use_dma) {
1168 if (lp->isa_config & ANY_ISA_DMA) {
1169 unsigned long flags;
1170 lp->dma_buff = (unsigned char *)__get_dma_pages(GFP_KERNEL,
1171 get_order(lp->dmasize * 1024));
1172
1173 if (!lp->dma_buff) {
1174 printk(KERN_ERR "%s: cannot get %dK memory for DMA\n", dev->name, lp->dmasize);
1175 goto release_irq;
1176 }
1177 if (net_debug > 1) {
1178 printk( "%s: dma %lx %lx\n",
1179 dev->name,
1180 (unsigned long)lp->dma_buff,
1181 (unsigned long)isa_virt_to_bus(lp->dma_buff));
1182 }
1183 if ((unsigned long) lp->dma_buff >= MAX_DMA_ADDRESS ||
1184 !dma_page_eq(lp->dma_buff, lp->dma_buff+lp->dmasize*1024-1)) {
1185 printk(KERN_ERR "%s: not usable as DMA buffer\n", dev->name);
1186 goto release_irq;
1187 }
1188 memset(lp->dma_buff, 0, lp->dmasize * 1024);
1189 if (request_dma(dev->dma, dev->name)) {
1190 printk(KERN_ERR "%s: cannot get dma channel %d\n", dev->name, dev->dma);
1191 goto release_irq;
1192 }
1193 write_dma(dev, lp->chip_type, dev->dma);
1194 lp->rx_dma_ptr = lp->dma_buff;
1195 lp->end_dma_buff = lp->dma_buff + lp->dmasize*1024;
1196 spin_lock_irqsave(&lp->lock, flags);
1197 disable_dma(dev->dma);
1198 clear_dma_ff(dev->dma);
1199 set_dma_mode(dev->dma, 0x14);
1200 set_dma_addr(dev->dma, isa_virt_to_bus(lp->dma_buff));
1201 set_dma_count(dev->dma, lp->dmasize*1024);
1202 enable_dma(dev->dma);
1203 spin_unlock_irqrestore(&lp->lock, flags);
1204 }
1205 }
1206#endif
1207
1208
1209 for (i=0; i < ETH_ALEN/2; i++)
1210 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1211
1212
1213 writereg(dev, PP_BusCTL, MEMORY_ON);
1214
1215
1216 if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) && (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
1217 lp->linectl = LOW_RX_SQUELCH;
1218 else
1219 lp->linectl = 0;
1220
1221
1222 switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
1223 case A_CNF_MEDIA_10B_T: result = lp->adapter_cnf & A_CNF_10B_T; break;
1224 case A_CNF_MEDIA_AUI: result = lp->adapter_cnf & A_CNF_AUI; break;
1225 case A_CNF_MEDIA_10B_2: result = lp->adapter_cnf & A_CNF_10B_2; break;
1226 default: result = lp->adapter_cnf & (A_CNF_10B_T | A_CNF_AUI | A_CNF_10B_2);
1227 }
1228 if (!result) {
1229 printk(KERN_ERR "%s: EEPROM is configured for unavailable media\n", dev->name);
1230 release_irq:
1231#if ALLOW_DMA
1232 release_dma_buff(lp);
1233#endif
1234 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
1235 free_irq(dev->irq, dev);
1236 ret = -EAGAIN;
1237 goto bad_out;
1238 }
1239
1240
1241 switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
1242 case A_CNF_MEDIA_10B_T:
1243 result = detect_tp(dev);
1244 if (result==DETECTED_NONE) {
1245 printk(KERN_WARNING "%s: 10Base-T (RJ-45) has no cable\n", dev->name);
1246 if (lp->auto_neg_cnf & IMM_BIT)
1247 result = DETECTED_RJ45H;
1248 }
1249 break;
1250 case A_CNF_MEDIA_AUI:
1251 result = detect_aui(dev);
1252 if (result==DETECTED_NONE) {
1253 printk(KERN_WARNING "%s: 10Base-5 (AUI) has no cable\n", dev->name);
1254 if (lp->auto_neg_cnf & IMM_BIT)
1255 result = DETECTED_AUI;
1256 }
1257 break;
1258 case A_CNF_MEDIA_10B_2:
1259 result = detect_bnc(dev);
1260 if (result==DETECTED_NONE) {
1261 printk(KERN_WARNING "%s: 10Base-2 (BNC) has no cable\n", dev->name);
1262 if (lp->auto_neg_cnf & IMM_BIT)
1263 result = DETECTED_BNC;
1264 }
1265 break;
1266 case A_CNF_MEDIA_AUTO:
1267 writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
1268 if (lp->adapter_cnf & A_CNF_10B_T)
1269 if ((result = detect_tp(dev)) != DETECTED_NONE)
1270 break;
1271 if (lp->adapter_cnf & A_CNF_AUI)
1272 if ((result = detect_aui(dev)) != DETECTED_NONE)
1273 break;
1274 if (lp->adapter_cnf & A_CNF_10B_2)
1275 if ((result = detect_bnc(dev)) != DETECTED_NONE)
1276 break;
1277 printk(KERN_ERR "%s: no media detected\n", dev->name);
1278 goto release_irq;
1279 }
1280 switch(result) {
1281 case DETECTED_NONE:
1282 printk(KERN_ERR "%s: no network cable attached to configured media\n", dev->name);
1283 goto release_irq;
1284 case DETECTED_RJ45H:
1285 printk(KERN_INFO "%s: using half-duplex 10Base-T (RJ-45)\n", dev->name);
1286 break;
1287 case DETECTED_RJ45F:
1288 printk(KERN_INFO "%s: using full-duplex 10Base-T (RJ-45)\n", dev->name);
1289 break;
1290 case DETECTED_AUI:
1291 printk(KERN_INFO "%s: using 10Base-5 (AUI)\n", dev->name);
1292 break;
1293 case DETECTED_BNC:
1294 printk(KERN_INFO "%s: using 10Base-2 (BNC)\n", dev->name);
1295 break;
1296 }
1297
1298
1299 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
1300
1301
1302 lp->rx_mode = 0;
1303 writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
1304
1305 lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
1306
1307 if (lp->isa_config & STREAM_TRANSFER)
1308 lp->curr_rx_cfg |= RX_STREAM_ENBL;
1309#if ALLOW_DMA
1310 set_dma_cfg(dev);
1311#endif
1312 writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
1313
1314 writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
1315 TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
1316
1317 writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
1318#if ALLOW_DMA
1319 dma_bufcfg(dev) |
1320#endif
1321 TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
1322
1323
1324 writereg(dev, PP_BusCTL, ENABLE_IRQ
1325 | (dev->mem_start?MEMORY_ON : 0)
1326#if ALLOW_DMA
1327 | dma_busctl(dev)
1328#endif
1329 );
1330 netif_start_queue(dev);
1331 if (net_debug > 1)
1332 printk("cs89x0: net_open() succeeded\n");
1333 return 0;
1334bad_out:
1335 return ret;
1336}
1337
1338static void net_timeout(struct net_device *dev)
1339{
1340
1341
1342 if (net_debug > 0) printk("%s: transmit timed out, %s?\n", dev->name,
1343 tx_done(dev) ? "IRQ conflict ?" : "network cable problem");
1344
1345 netif_wake_queue(dev);
1346}
1347
1348static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
1349{
1350 struct net_local *lp = (struct net_local *)dev->priv;
1351
1352 if (net_debug > 3) {
1353 printk("%s: sent %d byte packet of type %x\n",
1354 dev->name, skb->len,
1355 (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1356 }
1357
1358
1359
1360
1361
1362 spin_lock_irq(&lp->lock);
1363 netif_stop_queue(dev);
1364
1365
1366 writeword(dev, TX_CMD_PORT, lp->send_cmd);
1367 writeword(dev, TX_LEN_PORT, skb->len);
1368
1369
1370 if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
1371
1372
1373
1374
1375
1376 spin_unlock_irq(&lp->lock);
1377 if (net_debug) printk("cs89x0: Tx buffer not free!\n");
1378 return 1;
1379 }
1380
1381 outsw(dev->base_addr + TX_FRAME_PORT,skb->data,(skb->len+1) >>1);
1382 spin_unlock_irq(&lp->lock);
1383 dev->trans_start = jiffies;
1384 dev_kfree_skb (skb);
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397 return 0;
1398}
1399
1400
1401
1402
1403static irqreturn_t net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1404{
1405 struct net_device *dev = dev_id;
1406 struct net_local *lp;
1407 int ioaddr, status;
1408 int handled = 0;
1409
1410 ioaddr = dev->base_addr;
1411 lp = (struct net_local *)dev->priv;
1412
1413
1414
1415
1416
1417
1418
1419
1420 while ((status = readword(dev, ISQ_PORT))) {
1421 if (net_debug > 4)printk("%s: event=%04x\n", dev->name, status);
1422 handled = 1;
1423 switch(status & ISQ_EVENT_MASK) {
1424 case ISQ_RECEIVER_EVENT:
1425
1426 net_rx(dev);
1427 break;
1428 case ISQ_TRANSMITTER_EVENT:
1429 lp->stats.tx_packets++;
1430 netif_wake_queue(dev);
1431 if ((status & ( TX_OK |
1432 TX_LOST_CRS |
1433 TX_SQE_ERROR |
1434 TX_LATE_COL |
1435 TX_16_COL)) != TX_OK) {
1436 if ((status & TX_OK) == 0) lp->stats.tx_errors++;
1437 if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++;
1438 if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++;
1439 if (status & TX_LATE_COL) lp->stats.tx_window_errors++;
1440 if (status & TX_16_COL) lp->stats.tx_aborted_errors++;
1441 }
1442 break;
1443 case ISQ_BUFFER_EVENT:
1444 if (status & READY_FOR_TX) {
1445
1446
1447
1448
1449
1450 netif_wake_queue(dev);
1451 }
1452 if (status & TX_UNDERRUN) {
1453 if (net_debug > 0) printk("%s: transmit underrun\n", dev->name);
1454 lp->send_underrun++;
1455 if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
1456 else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
1457
1458
1459
1460
1461
1462 netif_wake_queue(dev);
1463 }
1464#if ALLOW_DMA
1465 if (lp->use_dma && (status & RX_DMA)) {
1466 int count = readreg(dev, PP_DmaFrameCnt);
1467 while(count) {
1468 if (net_debug > 5)
1469 printk("%s: receiving %d DMA frames\n", dev->name, count);
1470 if (net_debug > 2 && count >1)
1471 printk("%s: receiving %d DMA frames\n", dev->name, count);
1472 dma_rx(dev);
1473 if (--count == 0)
1474 count = readreg(dev, PP_DmaFrameCnt);
1475 if (net_debug > 2 && count > 0)
1476 printk("%s: continuing with %d DMA frames\n", dev->name, count);
1477 }
1478 }
1479#endif
1480 break;
1481 case ISQ_RX_MISS_EVENT:
1482 lp->stats.rx_missed_errors += (status >>6);
1483 break;
1484 case ISQ_TX_COL_EVENT:
1485 lp->stats.collisions += (status >>6);
1486 break;
1487 }
1488 }
1489 return IRQ_RETVAL(handled);
1490}
1491
1492static void
1493count_rx_errors(int status, struct net_local *lp)
1494{
1495 lp->stats.rx_errors++;
1496 if (status & RX_RUNT) lp->stats.rx_length_errors++;
1497 if (status & RX_EXTRA_DATA) lp->stats.rx_length_errors++;
1498 if (status & RX_CRC_ERROR) if (!(status & (RX_EXTRA_DATA|RX_RUNT)))
1499
1500 lp->stats.rx_crc_errors++;
1501 if (status & RX_DRIBBLE) lp->stats.rx_frame_errors++;
1502 return;
1503}
1504
1505
1506static void
1507net_rx(struct net_device *dev)
1508{
1509 struct net_local *lp = (struct net_local *)dev->priv;
1510 struct sk_buff *skb;
1511 int status, length;
1512
1513 int ioaddr = dev->base_addr;
1514 status = inw(ioaddr + RX_FRAME_PORT);
1515 length = inw(ioaddr + RX_FRAME_PORT);
1516
1517 if ((status & RX_OK) == 0) {
1518 count_rx_errors(status, lp);
1519 return;
1520 }
1521
1522
1523 skb = dev_alloc_skb(length + 2);
1524 if (skb == NULL) {
1525#if 0
1526 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
1527#endif
1528 lp->stats.rx_dropped++;
1529 return;
1530 }
1531 skb_reserve(skb, 2);
1532 skb->dev = dev;
1533
1534 insw(ioaddr + RX_FRAME_PORT, skb_put(skb, length), length >> 1);
1535 if (length & 1)
1536 skb->data[length-1] = inw(ioaddr + RX_FRAME_PORT);
1537
1538 if (net_debug > 3) {
1539 printk( "%s: received %d byte packet of type %x\n",
1540 dev->name, length,
1541 (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
1542 }
1543
1544 skb->protocol=eth_type_trans(skb,dev);
1545 netif_rx(skb);
1546 dev->last_rx = jiffies;
1547 lp->stats.rx_packets++;
1548 lp->stats.rx_bytes += length;
1549}
1550
1551#if ALLOW_DMA
1552static void release_dma_buff(struct net_local *lp)
1553{
1554 if (lp->dma_buff) {
1555 free_pages((unsigned long)(lp->dma_buff), get_order(lp->dmasize * 1024));
1556 lp->dma_buff = 0;
1557 }
1558}
1559#endif
1560
1561
1562static int
1563net_close(struct net_device *dev)
1564{
1565 struct net_local *lp = (struct net_local *)dev->priv;
1566
1567 netif_stop_queue(dev);
1568
1569 writereg(dev, PP_RxCFG, 0);
1570 writereg(dev, PP_TxCFG, 0);
1571 writereg(dev, PP_BufCFG, 0);
1572 writereg(dev, PP_BusCTL, 0);
1573
1574 free_irq(dev->irq, dev);
1575
1576#if ALLOW_DMA
1577 if (lp->use_dma && lp->dma) {
1578 free_dma(dev->dma);
1579 release_dma_buff(lp);
1580 }
1581#endif
1582
1583
1584 return 0;
1585}
1586
1587
1588
1589static struct net_device_stats *
1590net_get_stats(struct net_device *dev)
1591{
1592 struct net_local *lp = (struct net_local *)dev->priv;
1593 unsigned long flags;
1594
1595 spin_lock_irqsave(&lp->lock, flags);
1596
1597 lp->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
1598 lp->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
1599 spin_unlock_irqrestore(&lp->lock, flags);
1600
1601 return &lp->stats;
1602}
1603
1604static void set_multicast_list(struct net_device *dev)
1605{
1606 struct net_local *lp = (struct net_local *)dev->priv;
1607 unsigned long flags;
1608
1609 spin_lock_irqsave(&lp->lock, flags);
1610 if(dev->flags&IFF_PROMISC)
1611 {
1612 lp->rx_mode = RX_ALL_ACCEPT;
1613 }
1614 else if((dev->flags&IFF_ALLMULTI)||dev->mc_list)
1615 {
1616
1617
1618 lp->rx_mode = RX_MULTCAST_ACCEPT;
1619 }
1620 else
1621 lp->rx_mode = 0;
1622
1623 writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
1624
1625
1626 writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
1627 (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
1628 spin_unlock_irqrestore(&lp->lock, flags);
1629}
1630
1631
1632static int set_mac_address(struct net_device *dev, void *p)
1633{
1634 int i;
1635 struct sockaddr *addr = p;
1636
1637
1638 if (netif_running(dev))
1639 return -EBUSY;
1640
1641 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1642
1643 if (net_debug) {
1644 printk("%s: Setting MAC address to ", dev->name);
1645 for (i = 0; i < dev->addr_len; i++)
1646 printk(" %2.2x", dev->dev_addr[i]);
1647 printk(".\n");
1648 }
1649
1650 for (i=0; i < ETH_ALEN/2; i++)
1651 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
1652
1653 return 0;
1654}
1655
1656#ifdef MODULE
1657
1658static struct net_device dev_cs89x0;
1659
1660
1661
1662
1663
1664
1665static int io;
1666static int irq;
1667static int debug;
1668static char media[8];
1669static int duplex=-1;
1670
1671static int use_dma;
1672static int dma;
1673static int dmasize=16;
1674
1675MODULE_PARM(io, "i");
1676MODULE_PARM(irq, "i");
1677MODULE_PARM(debug, "i");
1678MODULE_PARM(media, "c8");
1679MODULE_PARM(duplex, "i");
1680MODULE_PARM(dma , "i");
1681MODULE_PARM(dmasize , "i");
1682MODULE_PARM(use_dma , "i");
1683MODULE_PARM_DESC(io, "cs89x0 I/O base address");
1684MODULE_PARM_DESC(irq, "cs89x0 IRQ number");
1685#if DEBUGGING
1686MODULE_PARM_DESC(debug, "cs89x0 debug level (0-6)");
1687#else
1688MODULE_PARM_DESC(debug, "(ignored)");
1689#endif
1690MODULE_PARM_DESC(media, "Set cs89x0 adapter(s) media type(s) (rj45,bnc,aui)");
1691
1692MODULE_PARM_DESC(duplex, "(ignored)");
1693#if ALLOW_DMA
1694MODULE_PARM_DESC(dma , "cs89x0 ISA DMA channel; ignored if use_dma=0");
1695MODULE_PARM_DESC(dmasize , "cs89x0 DMA size in kB (16,64); ignored if use_dma=0");
1696MODULE_PARM_DESC(use_dma , "cs89x0 using DMA (0-1)");
1697#else
1698MODULE_PARM_DESC(dma , "(ignored)");
1699MODULE_PARM_DESC(dmasize , "(ignored)");
1700MODULE_PARM_DESC(use_dma , "(ignored)");
1701#endif
1702
1703MODULE_AUTHOR("Mike Cruse, Russwll Nelson <nelson@crynwr.com>, Andrew Morton <andrewm@uow.edu.au>");
1704MODULE_LICENSE("GPL");
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733int
1734init_module(void)
1735{
1736 struct net_local *lp;
1737 int ret = 0;
1738
1739#if DEBUGGING
1740 net_debug = debug;
1741#else
1742 debug = 0;
1743#endif
1744
1745 dev_cs89x0.irq = irq;
1746 dev_cs89x0.base_addr = io;
1747
1748 dev_cs89x0.init = cs89x0_probe;
1749 dev_cs89x0.priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
1750 if (dev_cs89x0.priv == 0) {
1751 printk(KERN_ERR "cs89x0.c: Out of memory.\n");
1752 return -ENOMEM;
1753 }
1754 memset(dev_cs89x0.priv, 0, sizeof(struct net_local));
1755 lp = (struct net_local *)dev_cs89x0.priv;
1756
1757#if ALLOW_DMA
1758 if (use_dma) {
1759 lp->use_dma = use_dma;
1760 lp->dma = dma;
1761 lp->dmasize = dmasize;
1762 }
1763#endif
1764
1765 spin_lock_init(&lp->lock);
1766
1767
1768 if (!strcmp(media, "rj45"))
1769 lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1770 else if (!strcmp(media, "aui"))
1771 lp->adapter_cnf = A_CNF_MEDIA_AUI | A_CNF_AUI;
1772 else if (!strcmp(media, "bnc"))
1773 lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
1774 else
1775 lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1776
1777 if (duplex==-1)
1778 lp->auto_neg_cnf = AUTO_NEG_ENABLE;
1779
1780 if (io == 0) {
1781 printk(KERN_ERR "cs89x0.c: Module autoprobing not allowed.\n");
1782 printk(KERN_ERR "cs89x0.c: Append io=0xNNN\n");
1783 ret = -EPERM;
1784 goto out;
1785 }
1786
1787#if ALLOW_DMA
1788 if (use_dma && dmasize != 16 && dmasize != 64) {
1789 printk(KERN_ERR "cs89x0.c: dma size must be either 16K or 64K, not %dK\n", dmasize);
1790 ret = -EPERM;
1791 goto out;
1792 }
1793#endif
1794
1795 if (register_netdev(&dev_cs89x0) != 0) {
1796 printk(KERN_ERR "cs89x0.c: No card found at 0x%x\n", io);
1797 ret = -ENXIO;
1798 goto out;
1799 }
1800out:
1801 if (ret)
1802 kfree(dev_cs89x0.priv);
1803 return ret;
1804}
1805
1806void
1807cleanup_module(void)
1808{
1809 if (dev_cs89x0.priv != NULL) {
1810
1811 unregister_netdev(&dev_cs89x0);
1812 outw(PP_ChipID, dev_cs89x0.base_addr + ADD_PORT);
1813 kfree(dev_cs89x0.priv);
1814 dev_cs89x0.priv = NULL;
1815
1816 release_region(dev_cs89x0.base_addr, NETCARD_IO_EXTENT);
1817 }
1818}
1819#endif
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830