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