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#include <common.h>
76#include <command.h>
77#include <env.h>
78#include <log.h>
79#include <net.h>
80#include <malloc.h>
81#include <linux/compiler.h>
82
83
84void uboot_push_packet_len(int len);
85void uboot_push_tx_done(int key, int val);
86
87
88#include "ne2000_base.h"
89
90#if defined(CONFIG_DRIVER_AX88796L)
91
92#include "ax88796.h"
93#else
94
95#include "ne2000.h"
96#endif
97
98static dp83902a_priv_data_t nic;
99
100
101
102
103
104static bool
105dp83902a_init(unsigned char *enetaddr)
106{
107 dp83902a_priv_data_t *dp = &nic;
108 u8* base;
109#if defined(NE2000_BASIC_INIT)
110 int i;
111#endif
112
113 DEBUG_FUNCTION();
114
115 base = dp->base;
116 if (!base)
117 return false;
118
119 DEBUG_LINE();
120
121#if defined(NE2000_BASIC_INIT)
122
123
124 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1);
125
126 for (i = 0; i < 6; i++)
127 DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
128 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0);
129
130 printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
131 "eeprom",
132 dp->esa[0],
133 dp->esa[1],
134 dp->esa[2],
135 dp->esa[3],
136 dp->esa[4],
137 dp->esa[5] );
138
139 memcpy(enetaddr, dp->esa, 6);
140#endif
141 return true;
142}
143
144static void
145dp83902a_stop(void)
146{
147 dp83902a_priv_data_t *dp = &nic;
148 u8 *base = dp->base;
149
150 DEBUG_FUNCTION();
151
152 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP);
153 DP_OUT(base, DP_ISR, 0xFF);
154 DP_OUT(base, DP_IMR, 0x00);
155
156 dp->running = false;
157}
158
159
160
161
162
163
164
165static void
166dp83902a_start(u8 * enaddr)
167{
168 dp83902a_priv_data_t *dp = &nic;
169 u8 *base = dp->base;
170 int i;
171
172 debug("The MAC is %pM\n", enaddr);
173
174 DEBUG_FUNCTION();
175
176 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP);
177 DP_OUT(base, DP_DCR, DP_DCR_INIT);
178 DP_OUT(base, DP_RBCH, 0);
179 DP_OUT(base, DP_RBCL, 0);
180 DP_OUT(base, DP_RCR, DP_RCR_MON);
181 DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
182 DP_OUT(base, DP_TPSR, dp->tx_buf1);
183 dp->tx1 = dp->tx2 = 0;
184 dp->tx_next = dp->tx_buf1;
185 dp->tx_started = false;
186 dp->running = true;
187 DP_OUT(base, DP_PSTART, dp->rx_buf_start);
188 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
189 DP_OUT(base, DP_PSTOP, dp->rx_buf_end);
190 dp->rx_next = dp->rx_buf_start - 1;
191 dp->running = true;
192 DP_OUT(base, DP_ISR, 0xFF);
193 DP_OUT(base, DP_IMR, DP_IMR_All);
194 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP);
195 DP_OUT(base, DP_P1_CURP, dp->rx_buf_start);
196 dp->running = true;
197 for (i = 0; i < ETHER_ADDR_LEN; i++) {
198
199
200
201 DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
202 }
203
204 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
205 DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
206 DP_OUT(base, DP_RCR, DP_RCR_AB);
207 dp->running = true;
208}
209
210
211
212
213
214
215
216static void
217dp83902a_start_xmit(int start_page, int len)
218{
219 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
220 u8 *base = dp->base;
221
222 DEBUG_FUNCTION();
223
224#if DEBUG & 1
225 printf("Tx pkt %d len %d\n", start_page, len);
226 if (dp->tx_started)
227 printf("TX already started?!?\n");
228#endif
229
230 DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
231 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
232 DP_OUT(base, DP_TBCL, len & 0xFF);
233 DP_OUT(base, DP_TBCH, len >> 8);
234 DP_OUT(base, DP_TPSR, start_page);
235 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
236
237 dp->tx_started = true;
238}
239
240
241
242
243
244static void
245dp83902a_send(u8 *data, int total_len, u32 key)
246{
247 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
248 u8 *base = dp->base;
249 int len, start_page, pkt_len, i, isr;
250#if DEBUG & 4
251 int dx;
252#endif
253
254 DEBUG_FUNCTION();
255
256 len = pkt_len = total_len;
257 if (pkt_len < IEEE_8023_MIN_FRAME)
258 pkt_len = IEEE_8023_MIN_FRAME;
259
260 start_page = dp->tx_next;
261 if (dp->tx_next == dp->tx_buf1) {
262 dp->tx1 = start_page;
263 dp->tx1_len = pkt_len;
264 dp->tx1_key = key;
265 dp->tx_next = dp->tx_buf2;
266 } else {
267 dp->tx2 = start_page;
268 dp->tx2_len = pkt_len;
269 dp->tx2_key = key;
270 dp->tx_next = dp->tx_buf1;
271 }
272
273#if DEBUG & 5
274 printf("TX prep page %d len %d\n", start_page, pkt_len);
275#endif
276
277 DP_OUT(base, DP_ISR, DP_ISR_RDC);
278 {
279
280
281
282
283
284
285 __maybe_unused u16 tmp;
286 int len = 1;
287
288 DP_OUT(base, DP_RSAL, 0x100 - len);
289 DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
290 DP_OUT(base, DP_RBCL, len);
291 DP_OUT(base, DP_RBCH, 0);
292 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
293 DP_IN_DATA(dp->data, tmp);
294 }
295
296#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
297
298
299
300
301 CYGACC_CALL_IF_DELAY_US(1);
302#endif
303
304
305 DP_OUT(base, DP_RSAL, 0);
306 DP_OUT(base, DP_RSAH, start_page);
307 DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
308 DP_OUT(base, DP_RBCH, pkt_len >> 8);
309 DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
310
311
312#if DEBUG & 4
313 printf(" sg buf %08lx len %08x\n ", (u32)data, len);
314 dx = 0;
315#endif
316 while (len > 0) {
317#if DEBUG & 4
318 printf(" %02x", *data);
319 if (0 == (++dx % 16)) printf("\n ");
320#endif
321
322 DP_OUT_DATA(dp->data, *data++);
323 len--;
324 }
325#if DEBUG & 4
326 printf("\n");
327#endif
328 if (total_len < pkt_len) {
329#if DEBUG & 4
330 printf(" + %d bytes of padding\n", pkt_len - total_len);
331#endif
332
333 for (i = total_len; i < pkt_len;) {
334 i++;
335 DP_OUT_DATA(dp->data, 0);
336 }
337 }
338
339#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
340
341
342
343
344
345 CYGACC_CALL_IF_DELAY_US(1);
346#endif
347
348
349 do {
350 DP_IN(base, DP_ISR, isr);
351 } while ((isr & DP_ISR_RDC) == 0);
352
353
354 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
355
356
357 if (!dp->tx_started) {
358 if (start_page == dp->tx1) {
359 dp->tx_int = 1;
360 } else {
361 dp->tx_int = 2;
362 }
363 dp83902a_start_xmit(start_page, pkt_len);
364 }
365}
366
367
368
369
370
371
372
373
374static void
375dp83902a_RxEvent(void)
376{
377 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
378 u8 *base = dp->base;
379 __maybe_unused u8 rsr;
380 u8 rcv_hdr[4];
381 int i, len, pkt, cur;
382
383 DEBUG_FUNCTION();
384
385 DP_IN(base, DP_RSR, rsr);
386 while (true) {
387
388 DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
389 DP_IN(base, DP_P1_CURP, cur);
390 DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
391 DP_IN(base, DP_BNDRY, pkt);
392
393 pkt += 1;
394 if (pkt == dp->rx_buf_end)
395 pkt = dp->rx_buf_start;
396
397 if (pkt == cur) {
398 break;
399 }
400 DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
401 DP_OUT(base, DP_RBCH, 0);
402 DP_OUT(base, DP_RSAL, 0);
403 DP_OUT(base, DP_RSAH, pkt);
404 if (dp->rx_next == pkt) {
405 if (cur == dp->rx_buf_start)
406 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
407 else
408 DP_OUT(base, DP_BNDRY, cur - 1);
409 return;
410 }
411 dp->rx_next = pkt;
412 DP_OUT(base, DP_ISR, DP_ISR_RDC);
413 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
414#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
415 CYGACC_CALL_IF_DELAY_US(10);
416#endif
417
418
419 for (i = 0; i < sizeof(rcv_hdr);) {
420 DP_IN_DATA(dp->data, rcv_hdr[i++]);
421 }
422
423#if DEBUG & 5
424 printf("rx hdr %02x %02x %02x %02x\n",
425 rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
426#endif
427 len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
428
429
430 uboot_push_packet_len(len);
431
432 if (rcv_hdr[1] == dp->rx_buf_start)
433 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
434 else
435 DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1);
436 }
437}
438
439
440
441
442
443
444
445
446static void
447dp83902a_recv(u8 *data, int len)
448{
449 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
450 u8 *base = dp->base;
451 int i, mlen;
452 u8 saved_char = 0;
453 bool saved;
454#if DEBUG & 4
455 int dx;
456#endif
457
458 DEBUG_FUNCTION();
459
460#if DEBUG & 5
461 printf("Rx packet %d length %d\n", dp->rx_next, len);
462#endif
463
464
465 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
466 DP_OUT(base, DP_RBCL, len & 0xFF);
467 DP_OUT(base, DP_RBCH, len >> 8);
468 DP_OUT(base, DP_RSAL, 4);
469 DP_OUT(base, DP_RSAH, dp->rx_next);
470 DP_OUT(base, DP_ISR, DP_ISR_RDC);
471 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
472#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
473 CYGACC_CALL_IF_DELAY_US(10);
474#endif
475
476 saved = false;
477 for (i = 0; i < 1; i++) {
478 if (data) {
479 mlen = len;
480#if DEBUG & 4
481 printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
482 dx = 0;
483#endif
484 while (0 < mlen) {
485
486 if (saved) {
487 *data++ = saved_char;
488 mlen--;
489 saved = false;
490 continue;
491 }
492
493 {
494 u8 tmp;
495 DP_IN_DATA(dp->data, tmp);
496#if DEBUG & 4
497 printf(" %02x", tmp);
498 if (0 == (++dx % 16)) printf("\n ");
499#endif
500 *data++ = tmp;
501 mlen--;
502 }
503 }
504#if DEBUG & 4
505 printf("\n");
506#endif
507 }
508 }
509}
510
511static void
512dp83902a_TxEvent(void)
513{
514 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
515 u8 *base = dp->base;
516 __maybe_unused u8 tsr;
517 u32 key;
518
519 DEBUG_FUNCTION();
520
521 DP_IN(base, DP_TSR, tsr);
522 if (dp->tx_int == 1) {
523 key = dp->tx1_key;
524 dp->tx1 = 0;
525 } else {
526 key = dp->tx2_key;
527 dp->tx2 = 0;
528 }
529
530 dp->tx_started = false;
531 if (dp->tx1) {
532 dp83902a_start_xmit(dp->tx1, dp->tx1_len);
533 dp->tx_int = 1;
534 } else if (dp->tx2) {
535 dp83902a_start_xmit(dp->tx2, dp->tx2_len);
536 dp->tx_int = 2;
537 } else {
538 dp->tx_int = 0;
539 }
540
541 uboot_push_tx_done(key, 0);
542}
543
544
545
546
547
548static void
549dp83902a_ClearCounters(void)
550{
551 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
552 u8 *base = dp->base;
553 __maybe_unused u8 cnt1, cnt2, cnt3;
554
555 DP_IN(base, DP_FER, cnt1);
556 DP_IN(base, DP_CER, cnt2);
557 DP_IN(base, DP_MISSED, cnt3);
558 DP_OUT(base, DP_ISR, DP_ISR_CNT);
559}
560
561
562
563
564
565static void
566dp83902a_Overflow(void)
567{
568 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
569 u8 *base = dp->base;
570 u8 isr;
571
572
573 DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
574 CYGACC_CALL_IF_DELAY_US(1600);
575
576
577 DP_OUT(base, DP_RBCL, 0);
578 DP_OUT(base, DP_RBCH, 0);
579
580
581 DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
582 DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
583
584
585
586
587
588
589 dp83902a_RxEvent();
590 DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
591
592
593 DP_OUT(base, DP_ISR, DP_ISR_OFLW);
594 DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
595
596
597
598
599
600 DP_IN(base, DP_ISR, isr);
601 if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
602 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
603 }
604}
605
606static void
607dp83902a_poll(void)
608{
609 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
610 u8 *base = dp->base;
611 u8 isr;
612
613 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
614 DP_IN(base, DP_ISR, isr);
615 while (0 != isr) {
616
617
618
619
620
621 if (isr & DP_ISR_CNT) {
622 dp83902a_ClearCounters();
623 }
624
625
626
627
628
629 if (isr & DP_ISR_OFLW) {
630 dp83902a_Overflow();
631 } else {
632
633
634
635
636
637 DP_OUT(base, DP_ISR, isr);
638 if (!dp->running) break;
639
640
641
642
643 if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
644 dp83902a_TxEvent();
645 }
646 if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
647 dp83902a_RxEvent();
648 }
649 }
650 DP_IN(base, DP_ISR, isr);
651 }
652}
653
654
655
656static u8 *pbuf = NULL;
657
658static int pkey = -1;
659static int initialized = 0;
660
661void uboot_push_packet_len(int len) {
662 PRINTK("pushed len = %d\n", len);
663 if (len >= 2000) {
664 printf("NE2000: packet too big\n");
665 return;
666 }
667 dp83902a_recv(&pbuf[0], len);
668
669
670 net_process_received_packet(&pbuf[0], len);
671}
672
673void uboot_push_tx_done(int key, int val) {
674 PRINTK("pushed key = %d\n", key);
675 pkey = key;
676}
677
678
679
680
681
682
683
684
685static int ne2k_setup_driver(struct eth_device *dev)
686{
687 PRINTK("### ne2k_setup_driver\n");
688
689 if (!pbuf) {
690 pbuf = malloc(2000);
691 if (!pbuf) {
692 printf("Cannot allocate rx buffer\n");
693 return -1;
694 }
695 }
696
697 nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
698
699 nic.data = nic.base + DP_DATA;
700 nic.tx_buf1 = START_PG;
701 nic.tx_buf2 = START_PG2;
702 nic.rx_buf_start = RX_START;
703 nic.rx_buf_end = RX_END;
704
705
706
707
708
709
710 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) {
711
712 if (!get_prom(dev->enetaddr, nic.base))
713 dp83902a_init(dev->enetaddr);
714
715
716
717
718 eth_env_set_enetaddr("ethaddr", dev->enetaddr);
719 }
720 return 0;
721}
722
723static int ne2k_init(struct eth_device *dev, struct bd_info *bd)
724{
725 dp83902a_start(dev->enetaddr);
726 initialized = 1;
727 return 0;
728}
729
730static void ne2k_halt(struct eth_device *dev)
731{
732 debug("### ne2k_halt\n");
733 if(initialized)
734 dp83902a_stop();
735 initialized = 0;
736}
737
738static int ne2k_recv(struct eth_device *dev)
739{
740 dp83902a_poll();
741 return 1;
742}
743
744static int ne2k_send(struct eth_device *dev, void *packet, int length)
745{
746 int tmo;
747
748 debug("### ne2k_send\n");
749
750 pkey = -1;
751
752 dp83902a_send((u8 *) packet, length, 666);
753 tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
754 while(1) {
755 dp83902a_poll();
756 if (pkey != -1) {
757 PRINTK("Packet sucesfully sent\n");
758 return 0;
759 }
760 if (get_timer (0) >= tmo) {
761 printf("transmission error (timoeut)\n");
762 return 0;
763 }
764
765 }
766 return 0;
767}
768
769
770
771
772
773int ne2k_register(void)
774{
775 struct eth_device *dev;
776
777 dev = calloc(sizeof(*dev), 1);
778 if (dev == NULL)
779 return -1;
780
781 if (ne2k_setup_driver(dev))
782 return -1;
783
784 dev->init = ne2k_init;
785 dev->halt = ne2k_halt;
786 dev->send = ne2k_send;
787 dev->recv = ne2k_recv;
788
789 strcpy(dev->name, "NE2000");
790
791 return eth_register(dev);
792}
793