summaryrefslogtreecommitdiffstats
path: root/board/MAI/AmigaOneG3SE/enet.c
blob: ad2bcdeffea78c2aa7f0de169a4a634b1c4c36db (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
 * (C) Copyright 2002
 * Adam Kowalczyk, ACK Software Controls Inc. akowalczyk@cogeco.ca
 *
 * Some portions taken from 3c59x.c Written 1996-1999 by Donald Becker.
 *
 * Outline of the program based on eepro100.c which is
 *
 * (C) Copyright 2002
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <malloc.h>
#include <net.h>
#include <asm/io.h>
#include <pci.h>

#include "articiaS.h"
#include "memio.h"

/* 3Com Ethernet PCI definitions*/

/* #define PCI_VENDOR_ID_3COM		0x10B7 */
#define PCI_DEVICE_ID_3COM_3C905C	0x9200

/* 3Com Commands, top 5 bits are command and bottom 11 bits are parameters */

#define TotalReset		(0<<11)
#define SelectWindow		(1<<11)
#define StartCoax		(2<<11)
#define RxDisable		(3<<11)
#define RxEnable		(4<<11)
#define RxReset			(5<<11)
#define UpStall			(6<<11)
#define UpUnstall		(6<<11)+1
#define DownStall		(6<<11)+2
#define DownUnstall		(6<<11)+3
#define RxDiscard		(8<<11)
#define TxEnable		(9<<11)
#define TxDisable		(10<<11)
#define TxReset			(11<<11)
#define FakeIntr		(12<<11)
#define AckIntr			(13<<11)
#define SetIntrEnb		(14<<11)
#define SetStatusEnb		(15<<11)
#define SetRxFilter		(16<<11)
#define SetRxThreshold		(17<<11)
#define SetTxThreshold		(18<<11)
#define SetTxStart		(19<<11)
#define StartDMAUp		(20<<11)
#define StartDMADown		(20<<11)+1
#define StatsEnable		(21<<11)
#define StatsDisable		(22<<11)
#define StopCoax		(23<<11)
#define SetFilterBit		(25<<11)

/* The SetRxFilter command accepts the following classes */

#define RxStation		1
#define RxMulticast		2
#define RxBroadcast		4
#define RxProm			8

/* 3Com status word defnitions */

#define IntLatch		0x0001
#define HostError		0x0002
#define TxComplete		0x0004
#define TxAvailable		0x0008
#define RxComplete		0x0010
#define RxEarly			0x0020
#define IntReq			0x0040
#define StatsFull		0x0080
#define DMADone			(1<<8)
#define DownComplete		(1<<9)
#define UpComplete		(1<<10)
#define DMAInProgress		(1<<11)			/* DMA controller is still busy.*/
#define CmdInProgress		(1<<12)          	/* EL3_CMD is still busy.*/

/* Polling Registers */

#define DnPoll			0x2d
#define UpPoll			0x3d

/* Register window 0 offets */

#define Wn0EepromCmd		10	         	/* Window 0: EEPROM command register. */
#define Wn0EepromData		12            		/* Window 0: EEPROM results register. */
#define IntrStatus		0x0E	                /* Valid in all windows. */

/* Register window 0 EEPROM bits */

#define EEPROM_Read		0x80
#define EEPROM_WRITE		0x40
#define EEPROM_ERASE		0xC0
#define EEPROM_EWENB		0x30          		/* Enable erasing/writing for 10 msec. */
#define EEPROM_EWDIS		0x00           		/* Disable EWENB before 10 msec timeout. */

/* EEPROM locations. */

#define PhysAddr01		0
#define PhysAddr23		1
#define PhysAddr45		2
#define ModelID			3
#define EtherLink3ID		7
#define IFXcvrIO		8
#define IRQLine			9
#define NodeAddr01		10
#define NodeAddr23		11
#define NodeAddr45		12
#define DriverTune		13
#define Checksum		15

/* Register window 1 offsets, the window used in normal operation */

#define TX_FIFO			0x10
#define RX_FIFOa		0x10
#define RxErrors		0x14
#define RxStatus		0x18
#define Timer			0x1A
#define TxStatus		0x1B
#define TxFree			0x1C			/* Remaining free bytes in Tx buffer. */

/* Register Window 2 */

#define Wn2_ResetOptions	12

/* Register Window 3: MAC/config bits */

#define Wn3_Config		0			/* Internal Configuration */
#define Wn3_MAC_Ctrl		6
#define Wn3_Options		8

#define BFEXT(value, offset, bitcount)					\
	((((unsigned long)(value)) >> (offset)) & ((1 << (bitcount)) - 1))

#define BFINS(lhs, rhs, offset, bitcount)                                       \
	(((lhs) & ~((((1 << (bitcount)) - 1)) << (offset))) |			\
	(((rhs) & ((1 << (bitcount)) - 1)) << (offset)))

#define RAM_SIZE(v)             BFEXT(v, 0, 3)
#define RAM_WIDTH(v)		BFEXT(v, 3, 1)
#define RAM_SPEED(v) 	   	BFEXT(v, 4, 2)
#define ROM_SIZE(v)             BFEXT(v, 6, 2)
#define RAM_SPLIT(v)		BFEXT(v, 16, 2)
#define XCVR(v)                 BFEXT(v, 20, 4)
#define AUTOSELECT(v)		BFEXT(v, 24, 1)

/* Register Window 4: Xcvr/media bits */

#define Wn4_FIFODiag		4
#define Wn4_NetDiag		6
#define Wn4_PhysicalMgmt	8
#define Wn4_Media		10

#define Media_SQE		0x0008		/* Enable SQE error counting for AUI. */
#define Media_10TP		0x00C0		/* Enable link beat and jabber for 10baseT. */
#define Media_Lnk		0x0080		/* Enable just link beat for 100TX/100FX. */
#define Media_LnkBeat		0x0800

/* Register Window 7: Bus Master control */

#define Wn7_MasterAddr		0
#define Wn7_MasterLen		6
#define Wn7_MasterStatus	12

/* Boomerang bus master control registers. */

#define PktStatus		0x20
#define DownListPtr		0x24
#define FragAddr		0x28
#define FragLen			0x2c
#define TxFreeThreshold 	0x2f
#define UpPktStatus		0x30
#define UpListPtr 		0x38

/* The Rx and Tx descriptor lists. */

#define LAST_FRAG       0x80000000                      /* Last Addr/Len pair in descriptor. */
#define DN_COMPLETE     0x00010000                      /* This packet has been downloaded */

struct rx_desc_3com {
	u32 next;                                       /* Last entry points to 0	   	*/
	u32 status;					/* FSH -> Frame Start Header 		*/
	u32 addr;                                       /* Up to 63 addr/len pairs possible 	*/
	u32 length;                                     /* Set LAST_FRAG to indicate last pair	*/
};

/* Values for the Rx status entry. */

#define RxDComplete		0x00008000
#define RxDError		0x4000
#define IPChksumErr		(1<<25)
#define TCPChksumErr		(1<<26)
#define UDPChksumErr		(1<<27)
#define IPChksumValid		(1<<29)
#define TCPChksumValid		(1<<30)
#define UDPChksumValid		(1<<31)

struct tx_desc_3com {
	u32 next;                                       /* Last entry points to 0		*/
	u32 status;                                     /* bits 0:12 length, others see below	*/
	u32 addr;
	u32 length;
};

/* Values for the Tx status entry. */

#define CRCDisable		0x2000
#define TxDComplete		0x8000
#define AddIPChksum		0x02000000
#define AddTCPChksum		0x04000000
#define AddUDPChksum		0x08000000
#define TxIntrUploaded		0x80000000              /* IRQ when in FIFO, but maybe not sent. */

/* XCVR Types */

#define XCVR_10baseT		0
#define XCVR_AUI		1
#define XCVR_10baseTOnly	2
#define XCVR_10base2		3
#define XCVR_100baseTx		4
#define XCVR_100baseFx		5
#define XCVR_MII		6
#define XCVR_NWAY		8
#define XCVR_ExtMII		9
#define XCVR_Default		10			/* I don't think this is correct -> should have been 0x10 if Auto Negotiate */

struct descriptor {			    		/* A generic descriptor. */
	u32 next;                                       /* Last entry points to 0	   	*/
	u32 status;					/* FSH -> Frame Start Header 		*/
	u32 addr;                                       /* Up to 63 addr/len pairs possible 	*/
	u32 length;                                     /* Set LAST_FRAG to indicate last pair	*/
};

/* Misc. definitions */

#define NUM_RX_DESC 		PKTBUFSRX * 10
#define NUM_TX_DESC 		1            /* Number of TX descriptors   */

#define TOUT_LOOP		1000000

#define ETH_ALEN		6

#define EL3WINDOW(dev, win_num) ETH_OUTW(dev, SelectWindow + (win_num), EL3_CMD)
#define EL3_CMD 0x0e
#define EL3_STATUS 0x0e


#undef ETH_DEBUG

#ifdef ETH_DEBUG
#define PRINTF(fmt,args...)     printf (fmt ,##args)
#else
#define PRINTF(fmt,args...)
#endif


static struct rx_desc_3com *rx_ring;		 	/* RX descriptor ring         		*/
static struct tx_desc_3com *tx_ring;		 	/* TX descriptor ring         		*/
static u8 rx_buffer[NUM_RX_DESC][PKTSIZE_ALIGN];	/* storage for the incoming messages 	*/
static int rx_next = 0;                      		/* RX descriptor ring pointer 		*/
static int tx_next = 0;                      		/* TX descriptor ring pointer 		*/
static int tx_threshold;

static void  init_rx_ring(struct eth_device* dev);
static void  purge_tx_ring(struct eth_device* dev);

static void  read_hw_addr(struct eth_device* dev, bd_t * bis);

static int eth_3com_init(struct eth_device* dev, bd_t *bis);
static int eth_3com_send(struct eth_device* dev, volatile void *packet, int length);
static int eth_3com_recv(struct eth_device* dev);
static void eth_3com_halt(struct eth_device* dev);

#define io_to_phys(a)	pci_io_to_phys((pci_dev_t)dev->priv, a)
#define phys_to_io(a)	pci_phys_to_io((pci_dev_t)dev->priv, a)
#define mem_to_phys(a)	pci_mem_to_phys((pci_dev_t)dev->priv, a)
#define phys_to_mem(a)	pci_phys_to_mem((pci_dev_t)dev->priv, a)

static inline int ETH_INL(struct eth_device* dev, u_long addr)
{
    __asm volatile ("eieio");
	return le32_to_cpu(*(volatile u32 *)io_to_phys(addr + dev->iobase));
}

static inline int ETH_INW(struct eth_device* dev, u_long addr)
{
    __asm volatile ("eieio");
	return le16_to_cpu(*(volatile u16 *)io_to_phys(addr + dev->iobase));
}

static inline int ETH_INB(struct eth_device* dev, u_long addr)
{
    __asm volatile ("eieio");
	return *(volatile u8 *)io_to_phys(addr + dev->iobase);
}

static inline void ETH_OUTB(struct eth_device* dev, int command, u_long addr)
{
	*(volatile u8 *)io_to_phys(addr + dev->iobase) = command;
    __asm volatile ("eieio");
}

static inline void ETH_OUTW(struct eth_device* dev, int command, u_long addr)
{
	*(volatile u16 *)io_to_phys(addr + dev->iobase) = cpu_to_le16(command);
    __asm volatile ("eieio");
}

static inline void ETH_OUTL(struct eth_device* dev, int command, u_long addr)
{
	*(volatile u32 *)io_to_phys(addr + dev->iobase) = cpu_to_le32(command);
    __asm volatile ("eieio");
}

static inline int ETH_STATUS(struct eth_device* dev)
{
    __asm volatile ("eieio");
	return le16_to_cpu(*(volatile u16 *)io_to_phys(EL3_STATUS + dev->iobase));
}

static inline void ETH_CMD(struct eth_device* dev, int command)
{
	*(volatile u16 *)io_to_phys(EL3_CMD + dev->iobase) = cpu_to_le16(command);
    __asm volatile ("eieio");
}

/* Command register is always in the same spot in all the register windows */
/* This function issues a command and waits for it so complete by checking the CmdInProgress bit */

static int issue_and_wait(struct eth_device* dev, int command)
{

	int i, status;

	ETH_CMD(dev, command);
	for (i = 0; i < 2000; i++) {
		status = ETH_STATUS(dev);
		/*printf ("Issue: status 0x%4x.\n", status); */
		if (!(status & CmdInProgress))
			return 1;
	}

	/* OK, that didn't work.  Do it the slow way.  One second */
	for (i = 0; i < 100000; i++) {
		status = ETH_STATUS(dev);
		/*printf ("Issue: status 0x%4x.\n", status); */
			return 1;
		udelay(10);
	}
	PRINTF("Ethernet command: 0x%4x did not complete! Status: 0x%4x\n", command, ETH_STATUS(dev) );
	return 0;
}

/* Determine network media type and set up 3com accordingly           */
/* I think I'm going to start with something known first like 10baseT */

static int auto_negotiate(struct eth_device* dev)
{
    int i;

    EL3WINDOW(dev, 1);

    /* Wait for Auto negotiation to complete */
    for (i = 0; i <= 1000; i++)
    {
	if (ETH_INW(dev, 2) & 0x04)
	    break;
	udelay(100);

	if (i == 1000)
	{
	    PRINTF("Error: Auto negotiation failed\n");
	    return 0;
	}
    }


    return 1;
}

void eth_interrupt(struct eth_device *dev)
{
    u16 status = ETH_STATUS(dev);

    printf("eth0: status = 0x%04x\n", status);

    if (!(status & IntLatch))
	return;

    if (status & (1<<6))
    {
	ETH_CMD(dev, AckIntr | (1<<6));
	printf("Acknowledged Interrupt command\n");
    }

    if (status & DownComplete)
    {
	ETH_CMD(dev, AckIntr | DownComplete);
	printf("Acknowledged DownComplete\n");
    }

    if (status & UpComplete)
    {
	ETH_CMD(dev, AckIntr | UpComplete);
	printf("Acknowledged UpComplete\n");
    }

    ETH_CMD(dev, AckIntr | IntLatch);
    printf("Acknowledged IntLatch\n");
}

int eth_3com_initialize(bd_t *bis)
{
	u32 eth_iobase = 0, status;
	int card_number = 0, ret;
	struct eth_device* dev;
	pci_dev_t devno;
	char *s;

	s = getenv("3com_base");

	/* Find ethernet controller on the PCI bus */

	if ((devno = pci_find_device(PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C905C, 0)) < 0)
	{
		PRINTF("Error: Cannot find the ethernet device on the PCI bus\n");
		goto Done;
	}

	if (s)
	{
	    unsigned long base = atoi(s);
	    pci_write_config_dword(devno, PCI_BASE_ADDRESS_0, base | 0x01);
	}

	ret = pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &eth_iobase);
	eth_iobase &= ~0xf;

	PRINTF("eth: 3Com Found at Address: 0x%x\n", eth_iobase);

	pci_write_config_dword(devno, PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);

	 /* Check if I/O accesses and Bus Mastering are enabled */

	ret = pci_read_config_dword(devno, PCI_COMMAND, &status);

	if (!(status & PCI_COMMAND_IO))
	{
		printf("Error: Cannot enable IO access.\n");
		goto Done;
	}

	if (!(status & PCI_COMMAND_MEMORY))
	{
		printf("Error: Cannot enable MEMORY access.\n");
		goto Done;
	}

	if (!(status & PCI_COMMAND_MASTER))
	{
		printf("Error: Cannot enable Bus Mastering.\n");
		goto Done;
	}

	dev = (struct eth_device*) malloc(sizeof(*dev)); /*struct eth_device)); */

	sprintf(dev->name, "3Com 3c920c#%d", card_number);
	dev->iobase = eth_iobase;
	dev->priv   = (void*) devno;
	dev->init   = eth_3com_init;
	dev->halt   = eth_3com_halt;
	dev->send   = eth_3com_send;
	dev->recv   = eth_3com_recv;

	eth_register(dev);

/* 	{ */
/* 	    char interrupt; */
/* 	    devno = pci_find_device(PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C905C, 0); */
/* 	    pci_read_config_byte(devno, PCI_INTERRUPT_LINE, &interrupt); */

/* 	    printf("Installing eth0 interrupt handler to %d\n", interrupt); */
/* 	    irq_install_handler(interrupt, eth_interrupt, dev); */
/* 	} */

	card_number++;

	/* Set the latency timer for value */
	s = getenv("3com_latency");
	if (s)
	{
	    ret = pci_write_config_byte(devno, PCI_LATENCY_TIMER, (unsigned char)atoi(s));
	}
	else ret = pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x0a);

	read_hw_addr(dev, bis); 				/* get the MAC address from Window 2*/

	/* Reset the ethernet controller */

	PRINTF ("Issuing reset command....\n");
	if (!issue_and_wait(dev, TotalReset))
	{
		printf("Error: Cannot reset ethernet controller.\n");
		goto Done;
	}
	else
		PRINTF ("Ethernet controller reset.\n");

	/* allocate memory for rx and tx rings */

	if(!(rx_ring = memalign(sizeof(struct rx_desc_3com) * NUM_RX_DESC, 16)))
	{
		PRINTF ("Cannot allocate memory for RX_RING.....\n");
		goto Done;
	}

	if (!(tx_ring = memalign(sizeof(struct tx_desc_3com) * NUM_TX_DESC, 16)))
	{
		PRINTF ("Cannot allocate memory for TX_RING.....\n");
		goto Done;
	}

Done:
	return status;
}


static int eth_3com_init(struct eth_device* dev, bd_t *bis)
{
	int i, status = 0;
	int tx_cur, loop;
	u16 status_enable, intr_enable;
	struct descriptor *ias_cmd;

	/* Determine what type of network the machine is connected to	*/
	/* presently drops the connect to 10Mbps			*/

	if (!auto_negotiate(dev))
	{
		printf("Error: Cannot determine network media.\n");
		goto Done;
	}

	issue_and_wait(dev, TxReset);
	issue_and_wait(dev, RxReset|0x04);

	/* Switch to register set 7 for normal use. */
	EL3WINDOW(dev, 7);

	/* Initialize Rx and Tx rings */

	init_rx_ring(dev);
	purge_tx_ring(dev);

	ETH_CMD(dev, SetRxFilter | RxStation | RxBroadcast | RxProm);

	issue_and_wait(dev,SetTxStart|0x07ff);

	/* Below sets which indication bits to be seen. */

	status_enable = SetStatusEnb | HostError | DownComplete | UpComplete | (1<<6);
	ETH_CMD(dev, status_enable);

	/* Below sets no bits are to cause an interrupt since this is just polling */

	intr_enable   = SetIntrEnb;
/*	intr_enable = SetIntrEnb | (1<<9) | (1<<10) | (1<<6); */
	ETH_CMD(dev, intr_enable);
	ETH_OUTB(dev, 127, UpPoll);

	/* Ack all pending events, and set active indicator mask */

	ETH_CMD(dev, AckIntr | IntLatch | TxAvailable | RxEarly | IntReq);
	ETH_CMD(dev, intr_enable);

	/* Tell the adapter where the RX ring is located */

	issue_and_wait(dev,UpStall);				/* Stall and set the UplistPtr 		*/
	ETH_OUTL(dev, (u32)&rx_ring[rx_next], UpListPtr);
	ETH_CMD(dev, RxEnable); 				/* Enable the receiver. 		*/
	issue_and_wait(dev,UpUnstall);

	/* Send the Individual Address Setup frame */

	tx_cur       = tx_next;
	tx_next      = ((tx_next+1) % NUM_TX_DESC);

	ias_cmd             = (struct descriptor *)&tx_ring[tx_cur];
	ias_cmd->status     = cpu_to_le32(1<<31);		/* set DnIndicate bit. 			*/
	ias_cmd->next       = 0;
	ias_cmd->addr       = cpu_to_le32((u32)&bis->bi_enetaddr[0]);
	ias_cmd->length     = cpu_to_le32(6 | LAST_FRAG);

	/* Tell the adapter where the TX ring is located */

	ETH_CMD(dev, TxEnable); 				/* Enable transmitter. 			*/
	issue_and_wait(dev, DownStall);				/* Stall and set the DownListPtr. 	*/
	ETH_OUTL(dev, (u32)&tx_ring[tx_cur], DownListPtr);
	issue_and_wait(dev, DownUnstall);
	for (i=0; !(ETH_STATUS(dev) & DownComplete); i++)
	{
		if (i >= TOUT_LOOP)
		{
			PRINTF("TX Ring status (Init):  0x%4x\n", le32_to_cpu(tx_ring[tx_cur].status));
			PRINTF("ETH_STATUS: 0x%x\n", ETH_STATUS(dev));
			goto Done;
		}
	}
	if (ETH_STATUS(dev) & DownComplete)			/* If DownLoad Complete ACK the bit 	*/
	{
		ETH_CMD(dev, AckIntr | DownComplete);		/* acknowledge the indication bit	*/
		issue_and_wait(dev, DownStall);			/* stall and clear DownListPtr 		*/
		ETH_OUTL(dev, 0, DownListPtr);
		issue_and_wait(dev, DownUnstall);
	}
	status = 1;

Done:
	return status;
}

int eth_3com_send(struct eth_device* dev, volatile void *packet, int length)
{
	int i, status = 0;
	int tx_cur;

	if (length <= 0)
	{
		PRINTF("eth: bad packet size: %d\n", length);
		goto Done;
	}

	tx_cur  = tx_next;
	tx_next = (tx_next+1) % NUM_TX_DESC;

	tx_ring[tx_cur].status  = cpu_to_le32(1<<31);		/* set DnIndicate bit 			*/
	tx_ring[tx_cur].next    = 0;
	tx_ring[tx_cur].addr    = cpu_to_le32(((u32) packet));
	tx_ring[tx_cur].length  = cpu_to_le32(length | LAST_FRAG);

	/* Send the packet */

	issue_and_wait(dev, DownStall);				/* stall and set the DownListPtr 	*/
	ETH_OUTL(dev, (u32) &tx_ring[tx_cur], DownListPtr);
	issue_and_wait(dev, DownUnstall);

	for (i=0; !(ETH_STATUS(dev) & DownComplete); i++)
	{
		if (i >= TOUT_LOOP)
		{
			PRINTF("TX Ring status (send): 0x%4x\n", le32_to_cpu(tx_ring[tx_cur].status));
			goto Done;
		}
	}
	if (ETH_STATUS(dev) & DownComplete)			/* If DownLoad Complete ACK the bit 	*/
	{
		ETH_CMD(dev, AckIntr | DownComplete);		/* acknowledge the indication bit	*/
		issue_and_wait(dev, DownStall);			/* stall and clear DownListPtr 		*/
		ETH_OUTL(dev, 0, DownListPtr);
		issue_and_wait(dev, DownUnstall);
	}
	status=1;
 Done:
	return status;
}

void PrintPacket (uchar *packet, int length)
{
int loop;
uchar *ptr;

	printf ("Printing packet of length %x.\n\n", length);
	ptr = packet;
	for (loop = 1; loop <= length; loop++)
	{
		printf ("%2x ", *ptr++);
		if ((loop % 40)== 0)
			printf ("\n");
	}
}

int eth_3com_recv(struct eth_device* dev)
{
	u16 stat = 0;
	u32 status;
	int rx_prev, length = 0;

	while (!(ETH_STATUS(dev) & UpComplete))			/* wait on receipt of packet 	*/
		;

	status = le32_to_cpu(rx_ring[rx_next].status);		/* packet status		*/

	while (status & (1<<15))
	{
		/* A packet has been received */

		if (status & (1<<15))
		{
			/* A valid frame received  */

			length = le32_to_cpu(rx_ring[rx_next].status) & 0x1fff;		/* length is in bits 0 - 12 	*/

			/* Pass the packet up to the protocol layers */

			NetReceive((uchar *)le32_to_cpu(rx_ring[rx_next].addr), length);
			rx_ring[rx_next].status = 0;					/* clear the status word 	*/
			ETH_CMD(dev, AckIntr | UpComplete);
			issue_and_wait(dev, UpUnstall);
		}
		else
		if (stat & HostError)
		{
			/* There was an error */

			printf("Rx error status:  0x%4x\n", stat);
			init_rx_ring(dev);
			goto Done;
		}

		rx_prev = rx_next;
		rx_next = (rx_next + 1) % NUM_RX_DESC;
		stat = ETH_STATUS(dev);					/* register status 	*/
		status = le32_to_cpu(rx_ring[rx_next].status);		/* packet status 	*/
	}

Done:
	return length;
}

void eth_3com_halt(struct eth_device* dev)
{
	if (!(dev->iobase))
	{
		goto Done;
	}

	issue_and_wait(dev, DownStall);		/* shut down transmit and receive */
	issue_and_wait(dev, UpStall);
	issue_and_wait(dev, RxDisable);
	issue_and_wait(dev, TxDisable);

/*	free(tx_ring);				/###* release memory allocated to the DPD and UPD rings */
/*	free(rx_ring); */

Done:
	return;
}

static void init_rx_ring(struct eth_device* dev)
{
	int i;

	PRINTF("Initializing rx_ring. rx_buffer = %p\n", rx_buffer);
	issue_and_wait(dev, UpStall);

	for (i = 0; i < NUM_RX_DESC; i++)
	{
		rx_ring[i].next    = cpu_to_le32(((u32) &rx_ring[(i+1) % NUM_RX_DESC]));
		rx_ring[i].status  = 0;
		rx_ring[i].addr    = cpu_to_le32(((u32) &rx_buffer[i][0]));
		rx_ring[i].length  = cpu_to_le32(PKTSIZE_ALIGN | LAST_FRAG);
	}
	rx_next = 0;
}

static void purge_tx_ring(struct eth_device* dev)
{
	int i;

	PRINTF("Purging tx_ring.\n");

	tx_next      = 0;

	for (i = 0; i < NUM_TX_DESC; i++)
	{
		tx_ring[i].next    = 0;
		tx_ring[i].status  = 0;
		tx_ring[i].addr    = 0;
		tx_ring[i].length  = 0;
	}
}

static void read_hw_addr(struct eth_device* dev, bd_t *bis)
{
	u8 hw_addr[ETH_ALEN];
	unsigned int eeprom[0x40];
	unsigned int checksum = 0;
	int i, j, timer;

	/* Read the station address from the EEPROM. */

	EL3WINDOW(dev, 0);
	for (i = 0; i < 0x40; i++)
	{
		ETH_OUTW(dev, EEPROM_Read + i, Wn0EepromCmd);
		/* Pause for at least 162 us. for the read to take place. */
		for (timer = 10; timer >= 0; timer--)
		{
			udelay(162);
			if ((ETH_INW(dev, Wn0EepromCmd) & 0x8000) == 0)
				break;
		}
		eeprom[i] = ETH_INW(dev, Wn0EepromData);
	}

	/* Checksum calculation.  I'm not sure about this part and there seems to be a bug on the 3com side of things */

	for (i = 0; i < 0x21; i++)
		checksum  ^= eeprom[i];
	checksum = (checksum ^ (checksum >> 8)) & 0xff;

	if (checksum != 0xbb)
		printf(" *** INVALID EEPROM CHECKSUM %4.4x *** \n", checksum);

	for (i = 0, j = 0; i < 3; i++)
	{
		hw_addr[j++] = (u8)((eeprom[i+10] >> 8) & 0xff);
		hw_addr[j++] = (u8)(eeprom[i+10] & 0xff);
	}

	/*  MAC Address is in window 2, write value from EEPROM to window 2 */

	EL3WINDOW(dev, 2);
	for (i = 0; i < 6; i++)
		ETH_OUTB(dev, hw_addr[i], i);

	for (j = 0; j < ETH_ALEN; j+=2)
	{
		hw_addr[j]   = (u8)(ETH_INW(dev, j) & 0xff);
		hw_addr[j+1] = (u8)((ETH_INW(dev, j) >> 8) & 0xff);
	}

	for (i=0;i<ETH_ALEN;i++)
	{
		if (hw_addr[i] != bis->bi_enetaddr[i])
		{
/* 			printf("Warning: HW address don't match:\n"); */
/* 			printf("Address in 3Com Window 2 is         " */
/* 			       "%02X:%02X:%02X:%02X:%02X:%02X\n", */
/* 			       hw_addr[0], hw_addr[1], hw_addr[2], */
/* 			       hw_addr[3], hw_addr[4], hw_addr[5]); */
/* 			printf("Address used by U-Boot is " */
/* 			       "%02X:%02X:%02X:%02X:%02X:%02X\n", */
/* 			       bis->bi_enetaddr[0], bis->bi_enetaddr[1],  */
/* 			       bis->bi_enetaddr[2], bis->bi_enetaddr[3],  */
/* 			       bis->bi_enetaddr[4], bis->bi_enetaddr[5]); */
/* 			goto Done; */
		    char buffer[256];
		    if (bis->bi_enetaddr[0] == 0 && bis->bi_enetaddr[1] == 0 &&
			bis->bi_enetaddr[2] == 0 && bis->bi_enetaddr[3] == 0 &&
			bis->bi_enetaddr[4] == 0 && bis->bi_enetaddr[5] == 0)
		    {

			sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X",
				hw_addr[0], hw_addr[1], hw_addr[2],
				hw_addr[3], hw_addr[4], hw_addr[5]);
			setenv("ethaddr", buffer);
		    }
		}
	}

	for(i=0; i<ETH_ALEN; i++) dev->enetaddr[i] = hw_addr[i];

Done:
	return;
}
OpenPOWER on IntegriCloud