This source file includes following definitions.
- atp_init
- atp_probe1
- get_node_ID
- eeprom_op
- net_open
- hardware_init
- trigger_send
- write_packet
- net_send_packet
- net_interrupt
- atp_timed_checker
- net_rx
- read_block
- net_close
- net_get_stats
- set_multicast_list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 static char *version =
22 "atp.c:v1.01 1/18/95 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
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 #include <linux/kernel.h>
85 #include <linux/sched.h>
86 #include <linux/types.h>
87 #include <linux/fcntl.h>
88 #include <linux/interrupt.h>
89 #include <linux/ptrace.h>
90 #include <linux/ioport.h>
91 #include <linux/in.h>
92 #include <linux/malloc.h>
93 #include <linux/string.h>
94 #include <asm/system.h>
95 #include <asm/bitops.h>
96 #include <asm/io.h>
97 #include <asm/dma.h>
98 #include <linux/errno.h>
99
100 #include <linux/netdevice.h>
101 #include <linux/etherdevice.h>
102 #include <linux/skbuff.h>
103
104 #include "atp.h"
105
106
107 #ifndef NET_DEBUG
108 #define NET_DEBUG 1
109 #endif
110 static unsigned int net_debug = NET_DEBUG;
111
112
113 #define ETHERCARD_TOTAL_SIZE 3
114
115
116
117
118 #define TIMED_CHECKER (HZ/4)
119 #ifdef TIMED_CHECKER
120 #include <linux/timer.h>
121 static void atp_timed_checker(unsigned long ignored);
122 static struct device *atp_timed_dev;
123 static struct timer_list atp_timer = {NULL, NULL, 0, 0, atp_timed_checker};
124 #endif
125
126
127
128 extern int atp_probe(struct device *dev);
129
130 static int atp_probe1(struct device *dev, short ioaddr);
131 static void get_node_ID(struct device *dev);
132 static unsigned short eeprom_op(short ioaddr, unsigned int cmd);
133 static int net_open(struct device *dev);
134 static void hardware_init(struct device *dev);
135 static void write_packet(short ioaddr, int length, unsigned char *packet, int mode);
136 static void trigger_send(short ioaddr, int length);
137 static int net_send_packet(struct sk_buff *skb, struct device *dev);
138 static void net_interrupt(int irq, struct pt_regs *regs);
139 static void net_rx(struct device *dev);
140 static void read_block(short ioaddr, int length, unsigned char *buffer, int data_mode);
141 static int net_close(struct device *dev);
142 static struct enet_statistics *net_get_stats(struct device *dev);
143 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
144
145
146
147
148
149
150
151
152 int
153 atp_init(struct device *dev)
154 {
155 int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
156 int base_addr = dev->base_addr;
157
158 if (base_addr > 0x1ff)
159 return atp_probe1(dev, base_addr);
160 else if (base_addr == 1)
161 return ENXIO;
162
163 for (port = ports; *port; port++) {
164 int ioaddr = *port;
165 outb(0x57, ioaddr + PAR_DATA);
166 if (inb(ioaddr + PAR_DATA) != 0x57)
167 continue;
168 if (atp_probe1(dev, ioaddr) == 0)
169 return 0;
170 }
171
172 return ENODEV;
173 }
174
175 static int atp_probe1(struct device *dev, short ioaddr)
176 {
177 int saved_ctrl_reg, status;
178
179 outb(0xff, ioaddr + PAR_DATA);
180
181
182 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
183
184 outb(0x04, ioaddr + PAR_CONTROL);
185 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
186 eeprom_delay(2048);
187 status = read_nibble(ioaddr, CMR1);
188
189 if ((status & 0x78) != 0x08) {
190
191 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
192 return 1;
193 }
194 status = read_nibble(ioaddr, CMR2_h);
195 if ((status & 0x78) != 0x10) {
196 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
197 return 1;
198 }
199
200 write_reg_byte(ioaddr, CMR2, 0x01);
201 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
202
203
204 if (ioaddr == 0x378)
205 dev->irq = 7;
206 else
207 dev->irq = 5;
208 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF);
209 write_reg(ioaddr, CMR2, CMR2_NULL);
210
211 dev->base_addr = ioaddr;
212
213
214 get_node_ID(dev);
215
216 printk("%s: Pocket adapter found at %#3x, IRQ %d, SAPROM "
217 "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
218 dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
219 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
220
221
222 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
223
224 if (net_debug)
225 printk(version);
226
227
228 ether_setup(dev);
229 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
230 memset(dev->priv, 0, sizeof(struct net_local));
231
232
233 {
234 struct net_local *lp = (struct net_local *)dev->priv;
235 lp->addr_mode = CMR2h_Normal;
236 }
237
238
239 dev->if_port = (dev->mem_start & 0xf) ? dev->mem_start & 0x7 : 4;
240 if (dev->mem_end & 0xf)
241 net_debug = dev->mem_end & 7;
242
243 dev->open = net_open;
244 dev->stop = net_close;
245 dev->hard_start_xmit = net_send_packet;
246 dev->get_stats = net_get_stats;
247 dev->set_multicast_list = &set_multicast_list;
248
249 #ifdef TIMED_CHECKER
250 del_timer(&atp_timer);
251 atp_timer.expires = TIMED_CHECKER;
252 atp_timed_dev = dev;
253 add_timer(&atp_timer);
254 #endif
255 return 0;
256 }
257
258
259 static void get_node_ID(struct device *dev)
260 {
261 short ioaddr = dev->base_addr;
262 int sa_offset = 0;
263 int i;
264
265 write_reg(ioaddr, CMR2, CMR2_EEPROM);
266
267
268
269 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
270 sa_offset = 15;
271
272 for (i = 0; i < 3; i++)
273 ((unsigned short *)dev->dev_addr)[i] =
274 ntohs(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
275
276 write_reg(ioaddr, CMR2, CMR2_NULL);
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290
291 static unsigned short eeprom_op(short ioaddr, unsigned int cmd)
292 {
293 unsigned eedata_out = 0;
294 int num_bits = EE_CMD_SIZE;
295
296 while (--num_bits >= 0) {
297 char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
298 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
299 eeprom_delay(5);
300 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
301 eedata_out <<= 1;
302 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
303 eedata_out++;
304 eeprom_delay(5);
305 }
306 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
307 return eedata_out;
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321 static int net_open(struct device *dev)
322 {
323
324
325
326
327 if (irq2dev_map[dev->irq] != 0
328 || (irq2dev_map[dev->irq] = dev) == 0
329 || request_irq(dev->irq, &net_interrupt, 0, "ATP")) {
330 return -EAGAIN;
331 }
332
333 hardware_init(dev);
334 dev->start = 1;
335 return 0;
336 }
337
338
339
340 static void hardware_init(struct device *dev)
341 {
342 struct net_local *lp = (struct net_local *)dev->priv;
343 int ioaddr = dev->base_addr;
344 int i;
345
346 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
347
348 for (i = 0; i < 6; i++)
349 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
350
351 write_reg_high(ioaddr, CMR2, lp->addr_mode);
352
353 if (net_debug > 2) {
354 printk("%s: Reset: current Rx mode %d.\n", dev->name,
355 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
356 }
357
358 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
359 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
360
361
362 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
363
364
365 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
366 write_reg_high(ioaddr, IMR, ISRh_RxErr);
367
368 lp->tx_unit_busy = 0;
369 lp->pac_cnt_in_tx_buf = 0;
370 lp->saved_tx_size = 0;
371
372 dev->tbusy = 0;
373 dev->interrupt = 0;
374 }
375
376 static void trigger_send(short ioaddr, int length)
377 {
378 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
379 write_reg(ioaddr, TxCNT1, length >> 8);
380 write_reg(ioaddr, CMR1, CMR1_Xmit);
381 }
382
383 static void write_packet(short ioaddr, int length, unsigned char *packet, int data_mode)
384 {
385 length = (length + 1) & ~1;
386 outb(EOC+MAR, ioaddr + PAR_DATA);
387 if ((data_mode & 1) == 0) {
388
389 outb(WrAddr+MAR, ioaddr + PAR_DATA);
390 do {
391 write_byte_mode0(ioaddr, *packet++);
392 } while (--length > 0) ;
393 } else {
394
395 unsigned char outbyte = *packet++;
396
397 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
398 outb(WrAddr+MAR, ioaddr + PAR_DATA);
399
400 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
401 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
402 outbyte >>= 4;
403 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
404 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
405 while (--length > 0)
406 write_byte_mode1(ioaddr, *packet++);
407 }
408
409 outb(0xff, ioaddr + PAR_DATA);
410 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
411 }
412
413 static int
414 net_send_packet(struct sk_buff *skb, struct device *dev)
415 {
416 struct net_local *lp = (struct net_local *)dev->priv;
417 int ioaddr = dev->base_addr;
418
419 if (dev->tbusy) {
420
421
422 int tickssofar = jiffies - dev->trans_start;
423 if (tickssofar < 5)
424 return 1;
425 printk("%s: transmit timed out, %s?\n", dev->name,
426 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
427 : "IRQ conflict");
428 lp->stats.tx_errors++;
429
430 hardware_init(dev);
431 dev->tbusy=0;
432 dev->trans_start = jiffies;
433 }
434
435
436
437
438 if (skb == NULL) {
439 dev_tint(dev);
440 return 0;
441 }
442
443
444
445 if (set_bit(0, (void*)&dev->tbusy) != 0)
446 printk("%s: Transmitter access conflict.\n", dev->name);
447 else {
448 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
449 unsigned char *buf = skb->data;
450 int flags;
451
452
453
454 save_flags(flags);
455 cli();
456 write_reg(ioaddr, IMR, 0);
457 write_reg_high(ioaddr, IMR, 0);
458 restore_flags(flags);
459
460 write_packet(ioaddr, length, buf, dev->if_port);
461
462 lp->pac_cnt_in_tx_buf++;
463 if (lp->tx_unit_busy == 0) {
464 trigger_send(ioaddr, length);
465 lp->saved_tx_size = 0;
466 lp->re_tx = 0;
467 lp->tx_unit_busy = 1;
468 } else
469 lp->saved_tx_size = length;
470
471 dev->trans_start = jiffies;
472
473 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
474 write_reg_high(ioaddr, IMR, ISRh_RxErr);
475 }
476
477 dev_kfree_skb (skb, FREE_WRITE);
478
479 return 0;
480 }
481
482
483
484 static void
485 net_interrupt(int irq, struct pt_regs * regs)
486 {
487 struct device *dev = (struct device *)(irq2dev_map[irq]);
488 struct net_local *lp;
489 int ioaddr, status, boguscount = 20;
490 static int num_tx_since_rx = 0;
491
492 if (dev == NULL) {
493 printk ("ATP_interrupt(): irq %d for unknown device.\n", irq);
494 return;
495 }
496 dev->interrupt = 1;
497
498 ioaddr = dev->base_addr;
499 lp = (struct net_local *)dev->priv;
500
501
502 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
503
504
505 write_reg(ioaddr, CMR2, CMR2_NULL);
506 write_reg(ioaddr, IMR, 0);
507
508 if (net_debug > 5) printk("%s: In interrupt ", dev->name);
509 while (--boguscount > 0) {
510 status = read_nibble(ioaddr, ISR);
511 if (net_debug > 5) printk("loop status %02x..", status);
512
513 if (status & (ISR_RxOK<<3)) {
514 write_reg(ioaddr, ISR, ISR_RxOK);
515 do {
516 int read_status = read_nibble(ioaddr, CMR1);
517 if (net_debug > 6)
518 printk("handling Rx packet %02x..", read_status);
519
520
521 if (read_status & (CMR1_IRQ << 3)) {
522 lp->stats.rx_over_errors++;
523
524 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
525 net_rx(dev);
526
527 write_reg_high(ioaddr, ISR, ISRh_RxErr);
528 write_reg_high(ioaddr, CMR2, lp->addr_mode);
529 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
530 net_rx(dev);
531 dev->last_rx = jiffies;
532 num_tx_since_rx = 0;
533 } else
534 break;
535 } while (--boguscount > 0);
536 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
537 if (net_debug > 6) printk("handling Tx done..");
538
539
540 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
541 if (status & (ISR_TxErr<<3)) {
542 lp->stats.collisions++;
543 if (++lp->re_tx > 15) {
544 lp->stats.tx_aborted_errors++;
545 hardware_init(dev);
546 break;
547 }
548
549 if (net_debug > 6) printk("attempting to ReTx");
550 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
551 } else {
552
553 lp->stats.tx_packets++;
554 lp->pac_cnt_in_tx_buf--;
555 if ( lp->saved_tx_size) {
556 trigger_send(ioaddr, lp->saved_tx_size);
557 lp->saved_tx_size = 0;
558 lp->re_tx = 0;
559 } else
560 lp->tx_unit_busy = 0;
561 dev->tbusy = 0;
562 mark_bh(NET_BH);
563 }
564 num_tx_since_rx++;
565 } else if (num_tx_since_rx > 8
566 && jiffies > dev->last_rx + 100) {
567 if (net_debug > 2)
568 printk("%s: Missed packet? No Rx after %d Tx and %ld jiffies"
569 " status %02x CMR1 %02x.\n", dev->name,
570 num_tx_since_rx, jiffies - dev->last_rx, status,
571 (read_nibble(ioaddr, CMR1) >> 3) & 15);
572 lp->stats.rx_missed_errors++;
573 hardware_init(dev);
574 num_tx_since_rx = 0;
575 break;
576 } else
577 break;
578 }
579
580
581
582 {
583 int i;
584 for (i = 0; i < 6; i++)
585 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
586 #ifdef TIMED_CHECKER
587 del_timer(&atp_timer);
588 atp_timer.expires = TIMED_CHECKER;
589 add_timer(&atp_timer);
590 #endif
591 }
592
593
594 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
595
596 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
597
598 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
599 write_reg_high(ioaddr, IMR, ISRh_RxErr);
600
601 if (net_debug > 5) printk("exiting interrupt.\n");
602
603 dev->interrupt = 0;
604
605 return;
606 }
607
608 #ifdef TIMED_CHECKER
609
610
611 static void atp_timed_checker(unsigned long ignored)
612 {
613 int i;
614 int ioaddr = atp_timed_dev->base_addr;
615
616 if (!atp_timed_dev->interrupt)
617 {
618 for (i = 0; i < 6; i++)
619 #if 0
620 if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
621 {
622 struct net_local *lp = (struct net_local *)atp_timed_dev->priv;
623 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
624 if (i == 2)
625 lp->stats.tx_errors++;
626 else if (i == 3)
627 lp->stats.tx_dropped++;
628 else if (i == 4)
629 lp->stats.collisions++;
630 else
631 lp->stats.rx_errors++;
632 }
633 #else
634 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
635 #endif
636 }
637 del_timer(&atp_timer);
638 atp_timer.expires = TIMED_CHECKER;
639 add_timer(&atp_timer);
640 }
641 #endif
642
643
644 static void net_rx(struct device *dev)
645 {
646 struct net_local *lp = (struct net_local *)dev->priv;
647 int ioaddr = dev->base_addr;
648 #ifdef notdef
649 ushort header[4];
650 #else
651 struct rx_header rx_head;
652 #endif
653
654
655 outb(EOC+MAR, ioaddr + PAR_DATA);
656 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
657 if (net_debug > 5)
658 printk(" rx_count %04x %04x %04x %04x..", rx_head.pad,
659 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
660 if ((rx_head.rx_status & 0x77) != 0x01) {
661 lp->stats.rx_errors++;
662
663
664 if (net_debug > 3) printk("%s: Unknown ATP Rx error %04x.\n",
665 dev->name, rx_head.rx_status);
666 hardware_init(dev);
667 return;
668 } else {
669
670 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
671 struct sk_buff *skb;
672
673 skb = dev_alloc_skb(pkt_len);
674 if (skb == NULL) {
675 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
676 lp->stats.rx_dropped++;
677 goto done;
678 }
679 skb->dev = dev;
680
681 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
682
683 if (net_debug > 6) {
684 unsigned char *data = skb->data;
685 printk(" data %02x%02x%02x %02x%02x%02x %02x%02x%02x"
686 "%02x%02x%02x %02x%02x..",
687 data[0], data[1], data[2], data[3], data[4], data[5],
688 data[6], data[7], data[8], data[9], data[10], data[11],
689 data[12], data[13]);
690 }
691
692 skb->protocol=eth_type_trans(skb,dev);
693 netif_rx(skb);
694 lp->stats.rx_packets++;
695 }
696 done:
697 write_reg(ioaddr, CMR1, CMR1_NextPkt);
698 return;
699 }
700
701 static void read_block(short ioaddr, int length, unsigned char *p, int data_mode)
702 {
703
704 if (data_mode <= 3) {
705 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
706 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
707 ioaddr + PAR_DATA);
708 if (data_mode <= 1) {
709 do *p++ = read_byte_mode0(ioaddr); while (--length > 0);
710 } else
711 do *p++ = read_byte_mode2(ioaddr); while (--length > 0);
712 } else if (data_mode <= 5)
713 do *p++ = read_byte_mode4(ioaddr); while (--length > 0);
714 else
715 do *p++ = read_byte_mode6(ioaddr); while (--length > 0);
716
717 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
718 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
719 }
720
721
722 static int
723 net_close(struct device *dev)
724 {
725 struct net_local *lp = (struct net_local *)dev->priv;
726 int ioaddr = dev->base_addr;
727
728 dev->tbusy = 1;
729 dev->start = 0;
730
731
732 lp->addr_mode = CMR2h_OFF;
733 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
734
735
736 outb(0x00, ioaddr + PAR_CONTROL);
737 free_irq(dev->irq);
738 irq2dev_map[dev->irq] = 0;
739
740
741 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
742
743 return 0;
744 }
745
746
747
748 static struct enet_statistics *
749 net_get_stats(struct device *dev)
750 {
751 struct net_local *lp = (struct net_local *)dev->priv;
752 return &lp->stats;
753 }
754
755
756
757
758
759
760
761 static void
762 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
763 {
764 struct net_local *lp = (struct net_local *)dev->priv;
765 short ioaddr = dev->base_addr;
766 lp->addr_mode = num_addrs ? CMR2h_PROMISC : CMR2h_Normal;
767 write_reg_high(ioaddr, CMR2, lp->addr_mode);
768 }
769
770
771
772
773
774
775
776
777