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