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#include <common.h>
62#include <command.h>
63#include <malloc.h>
64#include "lan91c96.h"
65#include <net.h>
66
67
68
69
70
71
72
73
74#define POWER_DOWN 0
75
76
77
78
79
80
81#define MEMORY_WAIT_TIME 16
82
83#define SMC_DEBUG 0
84
85#if (SMC_DEBUG > 2 )
86#define PRINTK3(args...) printf(args)
87#else
88#define PRINTK3(args...)
89#endif
90
91#if SMC_DEBUG > 1
92#define PRINTK2(args...) printf(args)
93#else
94#define PRINTK2(args...)
95#endif
96
97#ifdef SMC_DEBUG
98#define PRINTK(args...) printf(args)
99#else
100#define PRINTK(args...)
101#endif
102
103
104
105
106
107
108
109
110
111
112#define DRIVER_NAME "LAN91C96"
113#define SMC_ALLOC_MAX_TRY 5
114#define SMC_TX_TIMEOUT 30
115
116#define ETH_ZLEN 60
117
118#ifdef CONFIG_LAN91C96_USE_32_BIT
119#define USE_32_BIT 1
120#else
121#undef USE_32_BIT
122#endif
123
124
125
126
127
128static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
129static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
130
131
132
133
134
135
136static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
137
138
139
140
141
142
143static void smc_set_mac_addr(const unsigned char *addr)
144{
145 int i;
146
147 for (i = 0; i < sizeof (smc_mac_addr); i++) {
148 smc_mac_addr[i] = addr[i];
149 }
150}
151
152
153
154
155void dump_memory_info(struct eth_device *dev)
156{
157 word mem_info;
158 word old_bank;
159
160 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
161
162 SMC_SELECT_BANK(dev, 0);
163 mem_info = SMC_inw(dev, LAN91C96_MIR);
164 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
165
166 SMC_SELECT_BANK(dev, old_bank);
167}
168
169
170
171
172#if SMC_DEBUG > 2
173static void print_packet (byte *, int);
174#endif
175
176static int poll4int (struct eth_device *dev, byte mask, int timeout)
177{
178 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
179 int is_timeout = 0;
180 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
181
182 PRINTK2 ("Polling...\n");
183 SMC_SELECT_BANK(dev, 2);
184 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
185 if (get_timer (0) >= tmo) {
186 is_timeout = 1;
187 break;
188 }
189 }
190
191
192 SMC_SELECT_BANK(dev, old_bank);
193
194 if (is_timeout)
195 return 1;
196 else
197 return 0;
198}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217static void smc_reset(struct eth_device *dev)
218{
219 PRINTK2("%s:smc_reset\n", dev->name);
220
221
222
223 SMC_SELECT_BANK(dev, 0);
224 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
225
226 udelay (10);
227
228
229 SMC_outw(dev, 0, LAN91C96_RCR);
230 SMC_outw(dev, 0, LAN91C96_TCR);
231
232
233 SMC_SELECT_BANK(dev, 1);
234 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
235 LAN91C96_CONTROL);
236
237
238 SMC_outb(dev, 0, LAN91C96_INT_MASK);
239}
240
241
242
243
244
245
246
247
248
249static void smc_enable(struct eth_device *dev)
250{
251 PRINTK2("%s:smc_enable\n", dev->name);
252 SMC_SELECT_BANK(dev, 0);
253
254
255
256 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
257
258
259 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
260
261
262
263
264
265
266 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283static void smc_shutdown(struct eth_device *dev)
284{
285 PRINTK2("%s:smc_shutdown\n", dev->name);
286
287
288 SMC_SELECT_BANK(dev, 2);
289 SMC_outb(dev, 0, LAN91C96_INT_MASK);
290
291
292 SMC_SELECT_BANK(dev, 0);
293 SMC_outb(dev, 0, LAN91C96_RCR);
294 SMC_outb(dev, 0, LAN91C96_TCR);
295}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316static int smc_send_packet(struct eth_device *dev, volatile void *packet,
317 int packet_length)
318{
319 byte packet_no;
320 unsigned long ioaddr;
321 byte *buf;
322 int length;
323 int numPages;
324 int try = 0;
325 int time_out;
326 byte status;
327
328
329 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
330
331 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
332
333
334
335
336
337
338
339
340
341
342
343
344
345 numPages = ((length & 0xfffe) + 6);
346 numPages >>= 8;
347
348 if (numPages > 7) {
349 printf("%s: Far too big packet error. \n", dev->name);
350 return 0;
351 }
352
353
354
355 SMC_SELECT_BANK(dev, 2);
356 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
357
358 again:
359 try++;
360 time_out = MEMORY_WAIT_TIME;
361 do {
362 status = SMC_inb(dev, LAN91C96_INT_STATS);
363 if (status & LAN91C96_IST_ALLOC_INT) {
364
365 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
366 LAN91C96_INT_STATS);
367 break;
368 }
369 } while (--time_out);
370
371 if (!time_out) {
372 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
373 dev->name, try);
374 if (try < SMC_ALLOC_MAX_TRY)
375 goto again;
376 else
377 return 0;
378 }
379
380 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
381 dev->name, try);
382
383
384
385 ioaddr = dev->iobase;
386
387 buf = (byte *) packet;
388
389
390 packet_no = SMC_inb(dev, LAN91C96_ARR);
391 if (packet_no & LAN91C96_ARR_FAILED) {
392
393 printf("%s: Memory allocation failed. \n", dev->name);
394 return 0;
395 }
396
397
398 SMC_outb(dev, packet_no, LAN91C96_PNR);
399
400
401 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
402
403 PRINTK3("%s: Trying to xmit packet of length %x\n",
404 dev->name, length);
405
406#if SMC_DEBUG > 2
407 printf ("Transmitting Packet\n");
408 print_packet (buf, length);
409#endif
410
411
412
413#ifdef USE_32_BIT
414 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
415#else
416 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
417
418 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
419#endif
420
421
422
423
424
425
426
427
428#ifdef USE_32_BIT
429 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
430 if (length & 0x2)
431 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
432 LAN91C96_DATA_HIGH);
433#else
434 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
435#endif
436
437
438 if ((length & 1) == 0) {
439 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
440 } else {
441 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
442 }
443
444
445 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
446
447
448 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
449
450 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
451
452
453 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
454
455
456 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
457 udelay (10);
458
459 PRINTK2("MMU ready\n");
460
461
462 return 0;
463 } else {
464
465 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
466
467 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
468
469
470 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
471
472
473 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
474 udelay (10);
475
476 PRINTK2 ("MMU ready\n");
477 }
478
479 return length;
480}
481
482
483
484
485
486
487
488
489static int smc_open(bd_t *bd, struct eth_device *dev)
490{
491 int i, err;
492
493 PRINTK2("%s:smc_open\n", dev->name);
494
495
496
497 smc_reset(dev);
498 smc_enable(dev);
499
500 SMC_SELECT_BANK(dev, 1);
501
502 err = smc_get_ethaddr(bd, dev);
503 if (err < 0)
504 return -1;
505#ifdef USE_32_BIT
506 for (i = 0; i < 6; i += 2) {
507 word address;
508
509 address = smc_mac_addr[i + 1] << 8;
510 address |= smc_mac_addr[i];
511 SMC_outw(dev, address, LAN91C96_IA0 + i);
512 }
513#else
514 for (i = 0; i < 6; i++)
515 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
516#endif
517 return 0;
518}
519
520
521
522
523
524
525
526
527
528
529
530
531
532static int smc_rcv(struct eth_device *dev)
533{
534 int packet_number;
535 word status;
536 word packet_length;
537 int is_error = 0;
538
539#ifdef USE_32_BIT
540 dword stat_len;
541#endif
542
543
544 SMC_SELECT_BANK(dev, 2);
545 packet_number = SMC_inw(dev, LAN91C96_FIFO);
546
547 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
548 return 0;
549 }
550
551 PRINTK3("%s:smc_rcv\n", dev->name);
552
553 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
554 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
555
556
557#ifdef USE_32_BIT
558 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
559 status = stat_len & 0xffff;
560 packet_length = stat_len >> 16;
561#else
562 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
563 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
564#endif
565
566 packet_length &= 0x07ff;
567
568 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
569
570 if (!(status & FRAME_FILTER)) {
571
572 packet_length -= 4;
573
574
575
576
577
578
579
580#ifdef USE_32_BIT
581 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
582 packet_length >> 2, packet_length & 3);
583
584
585
586
587 SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
588 packet_length >> 2);
589
590 if (packet_length & 3) {
591 int i;
592
593 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
594 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
595
596 for (i = 0; i < (packet_length & 3); i++)
597 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
598 }
599#else
600 PRINTK3 (" Reading %d words and %d byte(s) \n",
601 (packet_length >> 1), packet_length & 1);
602 SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
603 packet_length >> 1);
604
605#endif
606
607#if SMC_DEBUG > 2
608 printf ("Receiving Packet\n");
609 print_packet((byte *)NetRxPackets[0], packet_length);
610#endif
611 } else {
612
613
614 is_error = 1;
615 }
616
617 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
618 udelay (1);
619
620
621 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
622
623 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
624 udelay (1);
625
626 if (!is_error) {
627
628 NetReceive (NetRxPackets[0], packet_length);
629 return packet_length;
630 } else {
631 return 0;
632 }
633
634}
635
636
637
638
639
640
641
642
643
644static int smc_close(struct eth_device *dev)
645{
646 PRINTK2("%s:smc_close\n", dev->name);
647
648
649 smc_shutdown(dev);
650
651 return 0;
652}
653
654#if SMC_DEBUG > 2
655static void print_packet(byte *buf, int length)
656{
657#if 0
658 int i;
659 int remainder;
660 int lines;
661
662 printf ("Packet of length %d \n", length);
663
664 lines = length / 16;
665 remainder = length % 16;
666
667 for (i = 0; i < lines; i++) {
668 int cur;
669
670 for (cur = 0; cur < 8; cur++) {
671 byte a, b;
672
673 a = *(buf++);
674 b = *(buf++);
675 printf ("%02x%02x ", a, b);
676 }
677 printf ("\n");
678 }
679 for (i = 0; i < remainder / 2; i++) {
680 byte a, b;
681
682 a = *(buf++);
683 b = *(buf++);
684 printf ("%02x%02x ", a, b);
685 }
686 printf ("\n");
687#endif
688}
689#endif
690
691static int lan91c96_init(struct eth_device *dev, bd_t *bd)
692{
693 return smc_open(bd, dev);
694}
695
696static void lan91c96_halt(struct eth_device *dev)
697{
698 smc_close(dev);
699}
700
701static int lan91c96_recv(struct eth_device *dev)
702{
703 return smc_rcv(dev);
704}
705
706static int lan91c96_send(struct eth_device *dev, volatile void *packet,
707 int length)
708{
709 return smc_send_packet(dev, packet, length);
710}
711
712
713
714
715
716
717
718static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
719{
720 uchar v_mac[6];
721
722 if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
723
724 if (!get_rom_mac(dev, v_mac)) {
725 printf("\n*** ERROR: ethaddr is NOT set !!\n");
726 return -1;
727 }
728 eth_setenv_enetaddr("ethaddr", v_mac);
729 }
730
731 smc_set_mac_addr(v_mac);
732 PRINTK("Using MAC Address %pM\n", v_mac);
733 return 0;
734}
735
736
737
738
739
740
741static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
742{
743#ifdef HARDCODE_MAC
744 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
745
746 memcpy (v_rom_mac, hw_mac_addr, 6);
747 return (1);
748#else
749 int i;
750 SMC_SELECT_BANK(dev, 1);
751 for (i=0; i<6; i++)
752 {
753 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
754 }
755 return (1);
756#endif
757}
758
759
760struct id_type {
761 u8 id;
762 char *name;
763};
764static struct id_type supported_chips[] = {
765 {0, ""},
766 {9, "LAN91C110"},
767 {8, "LAN91C100FD"},
768 {7, "LAN91C100"},
769 {5, "LAN91C95"},
770 {4, "LAN91C94/LAN91C96"},
771 {3, "LAN91C90/LAN91C92"},
772};
773
774
775
776
777
778static int lan91c96_detect_chip(struct eth_device *dev)
779{
780 u8 chip_id;
781 int r;
782 SMC_SELECT_BANK(dev, 3);
783 chip_id = SMC_inw(dev, 0xA) & LAN91C96_REV_REVID;
784 SMC_SELECT_BANK(dev, 0);
785 for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++)
786 if (chip_id == supported_chips[r].id)
787 return r;
788 return 0;
789}
790
791int lan91c96_initialize(u8 dev_num, int base_addr)
792{
793 struct eth_device *dev;
794 int r = 0;
795
796 dev = malloc(sizeof(*dev));
797 if (!dev) {
798 free(dev);
799 return 0;
800 }
801 memset(dev, 0, sizeof(*dev));
802
803 dev->iobase = base_addr;
804
805
806 r = lan91c96_detect_chip(dev);
807 if (!r) {
808 free(dev);
809 return 0;
810 }
811 get_rom_mac(dev, dev->enetaddr);
812
813 dev->init = lan91c96_init;
814 dev->halt = lan91c96_halt;
815 dev->send = lan91c96_send;
816 dev->recv = lan91c96_recv;
817 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
818
819 eth_register(dev);
820 return 0;
821}
822