summaryrefslogtreecommitdiffstats
path: root/drivers/net/saa9730.c
blob: 27f33848aecea6ea724c8d2f4beab389937f97cc (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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
/*
 * Carsten Langgaard, carstenl@mips.com
 * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
 *
 * ########################################################################
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope 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.
 *
 * ########################################################################
 *
 * SAA9730 ethernet driver.
 *
 */

#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/delay.h>
#include <linux/etherdevice.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/pci.h>

#include <asm/addrspace.h>
#include <asm/mips-boards/prom.h>

#include "saa9730.h"

#ifdef LAN_SAA9730_DEBUG
int lan_saa9730_debug = LAN_SAA9730_DEBUG;
#else
int lan_saa9730_debug;
#endif


/* Non-zero only if the current card is a PCI with BIOS-set IRQ. */
static unsigned int pci_irq_line = 0;

#define INL(a)     inl((unsigned long)a)
#define OUTL(x,a)  outl(x,(unsigned long)a)

static void evm_saa9730_enable_lan_int(struct lan_saa9730_private *lp)
{
	OUTL(INL(&lp->evm_saa9730_regs->InterruptBlock1) | EVM_LAN_INT,
	     &lp->evm_saa9730_regs->InterruptBlock1);
	OUTL(INL(&lp->evm_saa9730_regs->InterruptStatus1) | EVM_LAN_INT,
	     &lp->evm_saa9730_regs->InterruptStatus1);
	OUTL(INL(&lp->evm_saa9730_regs->InterruptEnable1) | EVM_LAN_INT |
	     EVM_MASTER_EN, &lp->evm_saa9730_regs->InterruptEnable1);
}
static void evm_saa9730_disable_lan_int(struct lan_saa9730_private *lp)
{
	OUTL(INL(&lp->evm_saa9730_regs->InterruptBlock1) & ~EVM_LAN_INT,
	     &lp->evm_saa9730_regs->InterruptBlock1);
	OUTL(INL(&lp->evm_saa9730_regs->InterruptEnable1) & ~EVM_LAN_INT,
	     &lp->evm_saa9730_regs->InterruptEnable1);
}

static void evm_saa9730_clear_lan_int(struct lan_saa9730_private *lp)
{
	OUTL(EVM_LAN_INT, &lp->evm_saa9730_regs->InterruptStatus1);
}

static void evm_saa9730_block_lan_int(struct lan_saa9730_private *lp)
{
	OUTL(INL(&lp->evm_saa9730_regs->InterruptBlock1) & ~EVM_LAN_INT,
	     &lp->evm_saa9730_regs->InterruptBlock1);
}

static void evm_saa9730_unblock_lan_int(struct lan_saa9730_private *lp)
{
	OUTL(INL(&lp->evm_saa9730_regs->InterruptBlock1) | EVM_LAN_INT,
	     &lp->evm_saa9730_regs->InterruptBlock1);
}

static void show_saa9730_regs(struct lan_saa9730_private *lp)
{
	int i, j;
	printk("TxmBufferA = %x\n", lp->TxmBuffer[0][0]);
	printk("TxmBufferB = %x\n", lp->TxmBuffer[1][0]);
	printk("RcvBufferA = %x\n", lp->RcvBuffer[0][0]);
	printk("RcvBufferB = %x\n", lp->RcvBuffer[1][0]);
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
			printk("TxmBuffer[%d][%d] = %x\n", i, j,
			       le32_to_cpu(*(unsigned int *)
					   lp->TxmBuffer[i][j]));
		}
	}
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
			printk("RcvBuffer[%d][%d] = %x\n", i, j,
			       le32_to_cpu(*(unsigned int *)
					   lp->RcvBuffer[i][j]));
		}
	}
	printk("lp->evm_saa9730_regs->InterruptBlock1 = %x\n",
	       INL(&lp->evm_saa9730_regs->InterruptBlock1));
	printk("lp->evm_saa9730_regs->InterruptStatus1 = %x\n",
	       INL(&lp->evm_saa9730_regs->InterruptStatus1));
	printk("lp->evm_saa9730_regs->InterruptEnable1 = %x\n",
	       INL(&lp->evm_saa9730_regs->InterruptEnable1));
	printk("lp->lan_saa9730_regs->Ok2Use = %x\n",
	       INL(&lp->lan_saa9730_regs->Ok2Use));
	printk("lp->NextTxmBufferIndex = %x\n", lp->NextTxmBufferIndex);
	printk("lp->NextTxmPacketIndex = %x\n", lp->NextTxmPacketIndex);
	printk("lp->PendingTxmBufferIndex = %x\n",
	       lp->PendingTxmBufferIndex);
	printk("lp->PendingTxmPacketIndex = %x\n",
	       lp->PendingTxmPacketIndex);
	printk("lp->lan_saa9730_regs->LanDmaCtl = %x\n",
	       INL(&lp->lan_saa9730_regs->LanDmaCtl));
	printk("lp->lan_saa9730_regs->DmaStatus = %x\n",
	       INL(&lp->lan_saa9730_regs->DmaStatus));
	printk("lp->lan_saa9730_regs->CamCtl = %x\n",
	       INL(&lp->lan_saa9730_regs->CamCtl));
	printk("lp->lan_saa9730_regs->TxCtl = %x\n",
	       INL(&lp->lan_saa9730_regs->TxCtl));
	printk("lp->lan_saa9730_regs->TxStatus = %x\n",
	       INL(&lp->lan_saa9730_regs->TxStatus));
	printk("lp->lan_saa9730_regs->RxCtl = %x\n",
	       INL(&lp->lan_saa9730_regs->RxCtl));
	printk("lp->lan_saa9730_regs->RxStatus = %x\n",
	       INL(&lp->lan_saa9730_regs->RxStatus));
	for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
		OUTL(i, &lp->lan_saa9730_regs->CamAddress);
		printk("lp->lan_saa9730_regs->CamData = %x\n",
		       INL(&lp->lan_saa9730_regs->CamData));
	}
	printk("lp->stats.tx_packets = %lx\n", lp->stats.tx_packets);
	printk("lp->stats.tx_errors = %lx\n", lp->stats.tx_errors);
	printk("lp->stats.tx_aborted_errors = %lx\n",
	       lp->stats.tx_aborted_errors);
	printk("lp->stats.tx_window_errors = %lx\n",
	       lp->stats.tx_window_errors);
	printk("lp->stats.tx_carrier_errors = %lx\n",
	       lp->stats.tx_carrier_errors);
	printk("lp->stats.tx_fifo_errors = %lx\n",
	       lp->stats.tx_fifo_errors);
	printk("lp->stats.tx_heartbeat_errors = %lx\n",
	       lp->stats.tx_heartbeat_errors);
	printk("lp->stats.collisions = %lx\n", lp->stats.collisions);

	printk("lp->stats.rx_packets = %lx\n", lp->stats.rx_packets);
	printk("lp->stats.rx_errors = %lx\n", lp->stats.rx_errors);
	printk("lp->stats.rx_dropped = %lx\n", lp->stats.rx_dropped);
	printk("lp->stats.rx_crc_errors = %lx\n", lp->stats.rx_crc_errors);
	printk("lp->stats.rx_frame_errors = %lx\n",
	       lp->stats.rx_frame_errors);
	printk("lp->stats.rx_fifo_errors = %lx\n",
	       lp->stats.rx_fifo_errors);
	printk("lp->stats.rx_length_errors = %lx\n",
	       lp->stats.rx_length_errors);

	printk("lp->lan_saa9730_regs->DebugPCIMasterAddr = %x\n",
	       INL(&lp->lan_saa9730_regs->DebugPCIMasterAddr));
	printk("lp->lan_saa9730_regs->DebugLanTxStateMachine = %x\n",
	       INL(&lp->lan_saa9730_regs->DebugLanTxStateMachine));
	printk("lp->lan_saa9730_regs->DebugLanRxStateMachine = %x\n",
	       INL(&lp->lan_saa9730_regs->DebugLanRxStateMachine));
	printk("lp->lan_saa9730_regs->DebugLanTxFifoPointers = %x\n",
	       INL(&lp->lan_saa9730_regs->DebugLanTxFifoPointers));
	printk("lp->lan_saa9730_regs->DebugLanRxFifoPointers = %x\n",
	       INL(&lp->lan_saa9730_regs->DebugLanRxFifoPointers));
	printk("lp->lan_saa9730_regs->DebugLanCtlStateMachine = %x\n",
	       INL(&lp->lan_saa9730_regs->DebugLanCtlStateMachine));
}

static void lan_saa9730_buffer_init(struct lan_saa9730_private *lp)
{
	int i, j;

	/* Init RX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
			*(unsigned int *) lp->RcvBuffer[i][j] =
			    cpu_to_le32(RXSF_READY <<
					RX_STAT_CTL_OWNER_SHF);
		}
	}

	/* Init TX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
			*(unsigned int *) lp->TxmBuffer[i][j] =
			    cpu_to_le32(TXSF_EMPTY <<
					TX_STAT_CTL_OWNER_SHF);
		}
	}
}

static int lan_saa9730_allocate_buffers(struct lan_saa9730_private *lp)
{
	unsigned int mem_size;
	void *Pa;
	unsigned int i, j, RcvBufferSize, TxmBufferSize;
	unsigned int buffer_start;

	/* 
	 * Allocate all RX and TX packets in one chunk. 
	 * The Rx and Tx packets must be PACKET_SIZE aligned.
	 */
	mem_size = ((LAN_SAA9730_RCV_Q_SIZE + LAN_SAA9730_TXM_Q_SIZE) *
		    LAN_SAA9730_PACKET_SIZE * LAN_SAA9730_BUFFERS) +
	    LAN_SAA9730_PACKET_SIZE;
	buffer_start =
	    (unsigned int) kmalloc(mem_size, GFP_DMA | GFP_KERNEL);

	/* 
	 * Set DMA buffer to kseg1 (uncached).
	 * Make sure to flush before using it uncached.
	 */
	Pa = (void *) KSEG1ADDR((buffer_start + LAN_SAA9730_PACKET_SIZE) &
				~(LAN_SAA9730_PACKET_SIZE - 1));
	dma_cache_wback_inv((unsigned long) Pa, mem_size);

	/* Initialize buffer space */
	RcvBufferSize = LAN_SAA9730_PACKET_SIZE;
	TxmBufferSize = LAN_SAA9730_PACKET_SIZE;
	lp->DmaRcvPackets = LAN_SAA9730_RCV_Q_SIZE;
	lp->DmaTxmPackets = LAN_SAA9730_TXM_Q_SIZE;

	/* Init RX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_RCV_Q_SIZE; j++) {
			*(unsigned int *) Pa =
			    cpu_to_le32(RXSF_READY <<
					RX_STAT_CTL_OWNER_SHF);
			lp->RcvBuffer[i][j] = (unsigned int) Pa;
			Pa += RcvBufferSize;
		}
	}

	/* Init TX buffers */
	for (i = 0; i < LAN_SAA9730_BUFFERS; i++) {
		for (j = 0; j < LAN_SAA9730_TXM_Q_SIZE; j++) {
			*(unsigned int *) Pa =
			    cpu_to_le32(TXSF_EMPTY <<
					TX_STAT_CTL_OWNER_SHF);
			lp->TxmBuffer[i][j] = (unsigned int) Pa;
			Pa += TxmBufferSize;
		}
	}

	/* 
	 * Set rx buffer A and rx buffer B to point to the first two buffer 
	 * spaces.
	 */
	OUTL(PHYSADDR(lp->RcvBuffer[0][0]),
	     &lp->lan_saa9730_regs->RxBuffA);
	OUTL(PHYSADDR(lp->RcvBuffer[1][0]),
	     &lp->lan_saa9730_regs->RxBuffB);

	/* Initialize Buffer Index */
	lp->NextRcvPacketIndex = 0;
	lp->NextRcvToUseIsA = 1;

	/* Set current buffer index & next availble packet index */
	lp->NextTxmPacketIndex = 0;
	lp->NextTxmBufferIndex = 0;
	lp->PendingTxmPacketIndex = 0;
	lp->PendingTxmBufferIndex = 0;

	/* 
	 * Set txm_buf_a and txm_buf_b to point to the first two buffer
	 * space 
	 */
	OUTL(PHYSADDR(lp->TxmBuffer[0][0]),
	     &lp->lan_saa9730_regs->TxBuffA);
	OUTL(PHYSADDR(lp->TxmBuffer[1][0]),
	     &lp->lan_saa9730_regs->TxBuffB);

	/* Set packet number */
	OUTL((lp->DmaRcvPackets << PK_COUNT_RX_A_SHF) |
	     (lp->DmaRcvPackets << PK_COUNT_RX_B_SHF) |
	     (lp->DmaTxmPackets << PK_COUNT_TX_A_SHF) |
	     (lp->DmaTxmPackets << PK_COUNT_TX_B_SHF),
	     &lp->lan_saa9730_regs->PacketCount);

	return 0;
}

static int lan_saa9730_cam_load(struct lan_saa9730_private *lp)
{
	unsigned int i;
	unsigned char *NetworkAddress;

	NetworkAddress = (unsigned char *) &lp->PhysicalAddress[0][0];

	for (i = 0; i < LAN_SAA9730_CAM_DWORDS; i++) {
		/* First set address to where data is written */
		OUTL(i, &lp->lan_saa9730_regs->CamAddress);
		OUTL((NetworkAddress[0] << 24) | (NetworkAddress[1] << 16)
		     | (NetworkAddress[2] << 8) | NetworkAddress[3],
		     &lp->lan_saa9730_regs->CamData);
		NetworkAddress += 4;
	}
	return 0;
}

static int lan_saa9730_cam_init(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;
	unsigned int i;

	/* Copy MAC-address into all entries. */
	for (i = 0; i < LAN_SAA9730_CAM_ENTRIES; i++) {
		memcpy((unsigned char *) lp->PhysicalAddress[i],
		       (unsigned char *) dev->dev_addr, 6);
	}

	return 0;
}

static int lan_saa9730_mii_init(struct lan_saa9730_private *lp)
{
	int i, l;

	/* Check link status, spin here till station is not busy. */
	i = 0;
	while (INL(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
		i++;
		if (i > 100) {
			printk("Error: lan_saa9730_mii_init: timeout\n");
			return -1;
		}
		mdelay(1);	/* wait 1 ms. */
	}

	/* Now set the control and address register. */
	OUTL(MD_CA_BUSY | PHY_STATUS | PHY_ADDRESS << MD_CA_PHY_SHF,
	     &lp->lan_saa9730_regs->StationMgmtCtl);

	/* check link status, spin here till station is not busy */
	i = 0;
	while (INL(&lp->lan_saa9730_regs->StationMgmtCtl) & MD_CA_BUSY) {
		i++;
		if (i > 100) {
			printk("Error: lan_saa9730_mii_init: timeout\n");
			return -1;
		}
		mdelay(1);	/* wait 1 ms. */
	}

	/* Wait for 1 ms. */
	mdelay(1);

	/* Check the link status. */
	if (INL(&lp->lan_saa9730_regs->StationMgmtData) &
	    PHY_STATUS_LINK_UP) {
		/* Link is up. */
		return 0;
	} else {
		/* Link is down, reset the PHY first. */

		/* set PHY address = 'CONTROL' */
		OUTL(PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR | PHY_CONTROL,
		     &lp->lan_saa9730_regs->StationMgmtCtl);

		/* Wait for 1 ms. */
		mdelay(1);

		/* set 'CONTROL' = force reset and renegotiate */
		OUTL(PHY_CONTROL_RESET | PHY_CONTROL_AUTO_NEG |
		     PHY_CONTROL_RESTART_AUTO_NEG,
		     &lp->lan_saa9730_regs->StationMgmtData);

		/* Wait for 50 ms. */
		mdelay(50);

		/* set 'BUSY' to start operation */
		OUTL(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF | MD_CA_WR |
		     PHY_CONTROL, &lp->lan_saa9730_regs->StationMgmtCtl);

		/* await completion */
		i = 0;
		while (INL(&lp->lan_saa9730_regs->StationMgmtCtl) &
		       MD_CA_BUSY) {
			i++;
			if (i > 100) {
				printk
				    ("Error: lan_saa9730_mii_init: timeout\n");
				return -1;
			}
			mdelay(1);	/* wait 1 ms. */
		}

		/* Wait for 1 ms. */
		mdelay(1);

		for (l = 0; l < 2; l++) {
			/* set PHY address = 'STATUS' */
			OUTL(MD_CA_BUSY | PHY_ADDRESS << MD_CA_PHY_SHF |
			     PHY_STATUS,
			     &lp->lan_saa9730_regs->StationMgmtCtl);

			/* await completion */
			i = 0;
			while (INL(&lp->lan_saa9730_regs->StationMgmtCtl) &
			       MD_CA_BUSY) {
				i++;
				if (i > 100) {
					printk
					    ("Error: lan_saa9730_mii_init: timeout\n");
					return -1;
				}
				mdelay(1);	/* wait 1 ms. */
			}

			/* wait for 3 sec. */
			mdelay(3000);

			/* check the link status */
			if (INL(&lp->lan_saa9730_regs->StationMgmtData) &
			    PHY_STATUS_LINK_UP) {
				/* link is up */
				break;
			}
		}
	}

	return 0;
}

static int lan_saa9730_control_init(struct lan_saa9730_private *lp)
{
	/* Initialize DMA control register. */
	OUTL((LANMB_ANY << DMA_CTL_MAX_XFER_SHF) |
	     (LANEND_LITTLE << DMA_CTL_ENDIAN_SHF) |
	     (LAN_SAA9730_RCV_Q_INT_THRESHOLD << DMA_CTL_RX_INT_COUNT_SHF)
	     | DMA_CTL_RX_INT_TO_EN | DMA_CTL_RX_INT_EN |
	     DMA_CTL_MAC_RX_INT_EN | DMA_CTL_MAC_TX_INT_EN,
	     &lp->lan_saa9730_regs->LanDmaCtl);

	/* Initial MAC control register. */
	OUTL((MACCM_MII << MAC_CONTROL_CONN_SHF) | MAC_CONTROL_FULL_DUP,
	     &lp->lan_saa9730_regs->MacCtl);

	/* Initialize CAM control register. */
	OUTL(CAM_CONTROL_COMP_EN | CAM_CONTROL_BROAD_ACC,
	     &lp->lan_saa9730_regs->CamCtl);

	/* 
	 * Initialize CAM enable register, only turn on first entry, should
	 * contain own addr. 
	 */
	OUTL(0x0001, &lp->lan_saa9730_regs->CamEnable);

	/* Initialize Tx control register */
	OUTL(TX_CTL_EN_COMP, &lp->lan_saa9730_regs->TxCtl);

	/* Initialize Rcv control register */
	OUTL(RX_CTL_STRIP_CRC, &lp->lan_saa9730_regs->RxCtl);

	/* Reset DMA engine */
	OUTL(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);

	return 0;
}

static int lan_saa9730_stop(struct lan_saa9730_private *lp)
{
	int i;

	/* Stop DMA first */
	OUTL(INL(&lp->lan_saa9730_regs->LanDmaCtl) &
	     ~(DMA_CTL_EN_TX_DMA | DMA_CTL_EN_RX_DMA),
	     &lp->lan_saa9730_regs->LanDmaCtl);

	/* Set the SW Reset bits in DMA and MAC control registers */
	OUTL(DMA_TEST_SW_RESET, &lp->lan_saa9730_regs->DmaTest);
	OUTL(INL(&lp->lan_saa9730_regs->MacCtl) | MAC_CONTROL_RESET,
	     &lp->lan_saa9730_regs->MacCtl);

	/* 
	 * Wait for MAC reset to have finished. The reset bit is auto cleared
	 * when the reset is done.
	 */
	i = 0;
	while (INL(&lp->lan_saa9730_regs->MacCtl) & MAC_CONTROL_RESET) {
		i++;
		if (i > 100) {
			printk
			    ("Error: lan_sa9730_stop: MAC reset timeout\n");
			return -1;
		}
		mdelay(1);	/* wait 1 ms. */
	}

	return 0;
}

static int lan_saa9730_dma_init(struct lan_saa9730_private *lp)
{
	/* Stop lan controller. */
	lan_saa9730_stop(lp);

	OUTL(LAN_SAA9730_DEFAULT_TIME_OUT_CNT,
	     &lp->lan_saa9730_regs->Timeout);

	return 0;
}

static int lan_saa9730_start(struct lan_saa9730_private *lp)
{
	lan_saa9730_buffer_init(lp);

	/* Initialize Rx Buffer Index */
	lp->NextRcvPacketIndex = 0;
	lp->NextRcvToUseIsA = 1;

	/* Set current buffer index & next availble packet index */
	lp->NextTxmPacketIndex = 0;
	lp->NextTxmBufferIndex = 0;
	lp->PendingTxmPacketIndex = 0;
	lp->PendingTxmBufferIndex = 0;

	OUTL(INL(&lp->lan_saa9730_regs->LanDmaCtl) | DMA_CTL_EN_TX_DMA |
	     DMA_CTL_EN_RX_DMA, &lp->lan_saa9730_regs->LanDmaCtl);

	/* For Tx, turn on MAC then DMA */
	OUTL(INL(&lp->lan_saa9730_regs->TxCtl) | TX_CTL_TX_EN,
	     &lp->lan_saa9730_regs->TxCtl);

	/* For Rx, turn on DMA then MAC */
	OUTL(INL(&lp->lan_saa9730_regs->RxCtl) | RX_CTL_RX_EN,
	     &lp->lan_saa9730_regs->RxCtl);

	/* Set Ok2Use to let hardware owns the buffers */
	OUTL(OK2USE_RX_A | OK2USE_RX_B | OK2USE_TX_A | OK2USE_TX_B,
	     &lp->lan_saa9730_regs->Ok2Use);

	return 0;
}

static int lan_saa9730_restart(struct lan_saa9730_private *lp)
{
	lan_saa9730_stop(lp);
	lan_saa9730_start(lp);

	return 0;
}

static int lan_saa9730_tx(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;
	unsigned int *pPacket;
	unsigned int tx_status;

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_tx interrupt\n");

	/* Clear interrupt. */
	OUTL(DMA_STATUS_MAC_TX_INT, &lp->lan_saa9730_regs->DmaStatus);

	while (1) {
		pPacket =
		    (unsigned int *) lp->TxmBuffer[lp->
						   PendingTxmBufferIndex]
		    [lp->PendingTxmPacketIndex];

		/* Get status of first packet transmitted. */
		tx_status = le32_to_cpu(*pPacket);

		/* Check ownership. */
		if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
		    (TXSF_HWDONE << TX_STAT_CTL_OWNER_SHF)) break;

		/* Check for error. */
		if (tx_status & TX_STAT_CTL_ERROR_MSK) {
			if (lan_saa9730_debug > 1)
				printk("lan_saa9730_tx: tx error = %x\n",
				       tx_status);

			lp->stats.tx_errors++;
			if (tx_status &
			    (TX_STATUS_EX_COLL << TX_STAT_CTL_STATUS_SHF))
				    lp->stats.tx_aborted_errors++;
			if (tx_status &
			    (TX_STATUS_LATE_COLL <<
			     TX_STAT_CTL_STATUS_SHF)) lp->stats.
	     tx_window_errors++;
			if (tx_status &
			    (TX_STATUS_L_CARR << TX_STAT_CTL_STATUS_SHF))
				    lp->stats.tx_carrier_errors++;
			if (tx_status &
			    (TX_STATUS_UNDER << TX_STAT_CTL_STATUS_SHF))
				    lp->stats.tx_fifo_errors++;
			if (tx_status &
			    (TX_STATUS_SQ_ERR << TX_STAT_CTL_STATUS_SHF))
				    lp->stats.tx_heartbeat_errors++;

			lp->stats.collisions +=
			    tx_status & TX_STATUS_TX_COLL_MSK;
		}

		/* Free buffer. */
		*pPacket =
		    cpu_to_le32(TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF);

		/* Update pending index pointer. */
		lp->PendingTxmPacketIndex++;
		if (lp->PendingTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
			lp->PendingTxmPacketIndex = 0;
			lp->PendingTxmBufferIndex ^= 1;
		}
	}

	/* Make sure A and B are available to hardware. */
	OUTL(OK2USE_TX_A | OK2USE_TX_B, &lp->lan_saa9730_regs->Ok2Use);

	if (netif_queue_stopped(dev)) {
		/* The tx buffer is no longer full. */
		netif_wake_queue(dev);
	}

	return 0;
}

static int lan_saa9730_rx(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;
	int len = 0;
	struct sk_buff *skb = 0;
	unsigned int rx_status;
	int BufferIndex;
	int PacketIndex;
	unsigned int *pPacket;
	unsigned char *pData;

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_rx interrupt\n");

	/* Clear receive interrupts. */
	OUTL(DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
	     DMA_STATUS_RX_TO_INT, &lp->lan_saa9730_regs->DmaStatus);

	/* Address next packet */
	if (lp->NextRcvToUseIsA)
		BufferIndex = 0;
	else
		BufferIndex = 1;
	PacketIndex = lp->NextRcvPacketIndex;
	pPacket = (unsigned int *) lp->RcvBuffer[BufferIndex][PacketIndex];
	rx_status = le32_to_cpu(*pPacket);

	/* Process each packet. */
	while ((rx_status & RX_STAT_CTL_OWNER_MSK) ==
	       (RXSF_HWDONE << RX_STAT_CTL_OWNER_SHF)) {
		/* Check the rx status. */
		if (rx_status & (RX_STATUS_GOOD << RX_STAT_CTL_STATUS_SHF)) {
			/* Received packet is good. */
			len = (rx_status & RX_STAT_CTL_LENGTH_MSK) >>
			    RX_STAT_CTL_LENGTH_SHF;

			pData = (unsigned char *) pPacket;
			pData += 4;
			skb = dev_alloc_skb(len + 2);
			if (skb == 0) {
				printk
				    ("%s: Memory squeeze, deferring packet.\n",
				     dev->name);
				lp->stats.rx_dropped++;
			} else {
				lp->stats.rx_bytes += len;
				lp->stats.rx_packets++;
				skb->dev = dev;
				skb_reserve(skb, 2);	/* 16 byte align */
				skb_put(skb, len);	/* make room */
				eth_copy_and_sum(skb,
						 (unsigned char *) pData,
						 len, 0);
				skb->protocol = eth_type_trans(skb, dev);
				netif_rx(skb);
			}
		} else {
			/* We got an error packet. */
			if (lan_saa9730_debug > 2)
				printk
				    ("lan_saa9730_rx: We got an error packet = %x\n",
				     rx_status);

			lp->stats.rx_errors++;
			if (rx_status &
			    (RX_STATUS_CRC_ERR << RX_STAT_CTL_STATUS_SHF))
				    lp->stats.rx_crc_errors++;
			if (rx_status &
			    (RX_STATUS_ALIGN_ERR <<
			     RX_STAT_CTL_STATUS_SHF)) lp->stats.
	     rx_frame_errors++;
			if (rx_status &
			    (RX_STATUS_OVERFLOW << RX_STAT_CTL_STATUS_SHF))
				    lp->stats.rx_fifo_errors++;
			if (rx_status &
			    (RX_STATUS_LONG_ERR << RX_STAT_CTL_STATUS_SHF))
				    lp->stats.rx_length_errors++;
		}

		/* Indicate we have processed the buffer. */
		*pPacket =
		    cpu_to_le32(RXSF_READY << RX_STAT_CTL_OWNER_SHF);

		/* Go to next packet in sequence. */
		lp->NextRcvPacketIndex++;
		if (lp->NextRcvPacketIndex >= LAN_SAA9730_RCV_Q_SIZE) {
			lp->NextRcvPacketIndex = 0;
			if (BufferIndex) {
				lp->NextRcvToUseIsA = 1;
			} else {
				lp->NextRcvToUseIsA = 0;
			}
		}
		OUTL(OK2USE_RX_A | OK2USE_RX_B,
		     &lp->lan_saa9730_regs->Ok2Use);

		/* Address next packet */
		if (lp->NextRcvToUseIsA)
			BufferIndex = 0;
		else
			BufferIndex = 1;
		PacketIndex = lp->NextRcvPacketIndex;
		pPacket =
		    (unsigned int *) lp->
		    RcvBuffer[BufferIndex][PacketIndex];
		rx_status = le32_to_cpu(*pPacket);
	}

	/* Make sure A and B are available to hardware. */
	OUTL(OK2USE_RX_A | OK2USE_RX_B, &lp->lan_saa9730_regs->Ok2Use);

	return 0;
}

static void lan_saa9730_interrupt(const int irq, void *dev_id,
				  struct pt_regs *regs)
{
	struct net_device *dev = (struct net_device *) dev_id;
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_interrupt\n");

	/* Disable the EVM LAN interrupt. */
	evm_saa9730_block_lan_int(lp);

	/* Clear the EVM LAN interrupt. */
	evm_saa9730_clear_lan_int(lp);

	/* Service pending transmit interrupts. */
	if (INL(&lp->lan_saa9730_regs->DmaStatus) & DMA_STATUS_MAC_TX_INT)
		lan_saa9730_tx(dev);

	/* Service pending receive interrupts. */
	if (INL(&lp->lan_saa9730_regs->DmaStatus) &
	    (DMA_STATUS_MAC_RX_INT | DMA_STATUS_RX_INT |
	     DMA_STATUS_RX_TO_INT)) lan_saa9730_rx(dev);

	/* Enable the EVM LAN interrupt. */
	evm_saa9730_unblock_lan_int(lp);

	return;
}

static int lan_saa9730_open_fail(struct net_device *dev)
{
	return -ENODEV;
}

static int lan_saa9730_open(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;

	/* Associate IRQ with lan_saa9730_interrupt */
	if (request_irq(dev->irq, &lan_saa9730_interrupt, 0, "SAA9730 Eth",
			dev)) {
		printk("lan_saa9730_open: Can't get irq %d\n", dev->irq);
		return -EAGAIN;
	}

	/* Enable the Lan interrupt in the event manager. */
	evm_saa9730_enable_lan_int(lp);

	/* Start the LAN controller */
	if (lan_saa9730_start(lp))
		return -1;

	netif_start_queue(dev);

	return 0;
}

static int lan_saa9730_write(struct lan_saa9730_private *lp,
			     struct sk_buff *skb, int skblen)
{
	unsigned char *pbData = skb->data;
	unsigned int len = skblen;
	unsigned char *pbPacketData;
	unsigned int tx_status;
	int BufferIndex;
	int PacketIndex;

	if (lan_saa9730_debug > 5)
		printk("lan_saa9730_write: skb=%08x\n",
		       (unsigned int) skb);

	BufferIndex = lp->NextTxmBufferIndex;
	PacketIndex = lp->NextTxmPacketIndex;

	tx_status =
	    le32_to_cpu(*(unsigned int *) lp->
			TxmBuffer[BufferIndex][PacketIndex]);
	if ((tx_status & TX_STAT_CTL_OWNER_MSK) !=
	    (TXSF_EMPTY << TX_STAT_CTL_OWNER_SHF)) {
		if (lan_saa9730_debug > 4)
			printk
			    ("lan_saa9730_write: Tx buffer not available: tx_status = %x\n",
			     tx_status);
		return -1;
	}

	lp->NextTxmPacketIndex++;
	if (lp->NextTxmPacketIndex >= LAN_SAA9730_TXM_Q_SIZE) {
		lp->NextTxmPacketIndex = 0;
		lp->NextTxmBufferIndex ^= 1;
	}

	pbPacketData =
	    (unsigned char *) lp->TxmBuffer[BufferIndex][PacketIndex];
	pbPacketData += 4;

	/* copy the bits */
	memcpy(pbPacketData, pbData, len);

	/* Set transmit status for hardware */
	*(unsigned int *) lp->TxmBuffer[BufferIndex][PacketIndex] =
	    cpu_to_le32((TXSF_READY << TX_STAT_CTL_OWNER_SHF) |
			(TX_STAT_CTL_INT_AFTER_TX << TX_STAT_CTL_FRAME_SHF)
			| (len << TX_STAT_CTL_LENGTH_SHF));

	/* Set hardware tx buffer. */
	OUTL(OK2USE_TX_A | OK2USE_TX_B, &lp->lan_saa9730_regs->Ok2Use);

	return 0;
}

static void lan_saa9730_tx_timeout(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;

	/* Transmitter timeout, serious problems */
	lp->stats.tx_errors++;
	printk("%s: transmit timed out, reset\n", dev->name);
	/*show_saa9730_regs(lp); */
	lan_saa9730_restart(lp);

	dev->trans_start = jiffies;
	netif_start_queue(dev);
}

static int lan_saa9730_start_xmit(struct sk_buff *skb,
				  struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;
	unsigned long flags;
	int skblen;
	int len;

	if (lan_saa9730_debug > 4)
		printk("Send packet: skb=%08x\n", (unsigned int) skb);

	skblen = skb->len;
	save_and_cli(flags);
	len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;

	if (lan_saa9730_write(lp, skb, skblen)) {
		restore_flags(flags);
		printk
		    ("Error when writing packet to controller: skb=%08x\n",
		     (unsigned int) skb);
		netif_stop_queue(dev);
		return -1;
	}

	lp->stats.tx_bytes += len;
	lp->stats.tx_packets++;

	dev->trans_start = jiffies;
	netif_start_queue(dev);
	dev_kfree_skb(skb);

	restore_flags(flags);

	return 0;
}

static int lan_saa9730_close(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;

	if (lan_saa9730_debug > 1)
		printk("lan_saa9730_close:\n");

	netif_stop_queue(dev);

	/* Disable the Lan interrupt in the event manager. */
	evm_saa9730_disable_lan_int(lp);

	/* Stop the controller */
	if (lan_saa9730_stop(lp))
		return -1;

	free_irq(dev->irq, (void *) dev);

	return 0;
}

static struct net_device_stats *lan_saa9730_get_stats(struct net_device
						      *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;

	return &lp->stats;
}

static void lan_saa9730_set_multicast(struct net_device *dev)
{
	struct lan_saa9730_private *lp =
	    (struct lan_saa9730_private *) dev->priv;

	/* Stop the controller */
	lan_saa9730_stop(lp);

	if (dev->flags & IFF_PROMISC) {
		/* accept all packets */
		OUTL(CAM_CONTROL_COMP_EN | CAM_CONTROL_STATION_ACC |
		     CAM_CONTROL_GROUP_ACC | CAM_CONTROL_BROAD_ACC,
		     &lp->lan_saa9730_regs->CamCtl);
	} else {
		if (dev->flags & IFF_ALLMULTI) {
			/* accept all multicast packets */
			OUTL(CAM_CONTROL_COMP_EN | CAM_CONTROL_GROUP_ACC |
			     CAM_CONTROL_BROAD_ACC,
			     &lp->lan_saa9730_regs->CamCtl);
		} else {
			/* 
			 * Will handle the multicast stuff later. -carstenl
			 */
		}
	}

	lan_saa9730_restart(lp);
}

static int lan_saa9730_init(struct net_device *dev, int ioaddr, int irq)
{
	struct lan_saa9730_private *lp;
	unsigned char ethernet_addr[6];

	dev = init_etherdev(dev, 0);

	dev->open = lan_saa9730_open_fail;
	if (get_ethernet_addr(ethernet_addr))
		return -1;

	memcpy(dev->dev_addr, ethernet_addr, 6);
	dev->base_addr = ioaddr;
	dev->irq = irq;

	/* 
	 * Make certain the data structures used by the controller are aligned 
	 * and DMAble. 
	 */
	lp = (struct lan_saa9730_private *) (((unsigned long)
					      kmalloc(sizeof(*lp) + 7,
						      GFP_DMA | GFP_KERNEL)
					      + 7) & ~7);

	dev->priv = lp;
	memset(lp, 0, sizeof(*lp));

	/* Set SAA9730 LAN base address. */
	lp->lan_saa9730_regs = (t_lan_saa9730_regmap *) (ioaddr +
							 SAA9730_LAN_REGS_ADDR);

	/* Set SAA9730 EVM base address. */
	lp->evm_saa9730_regs = (t_evm_saa9730_regmap *) (ioaddr +
							 SAA9730_EVM_REGS_ADDR);

	/* Allocate LAN RX/TX frame buffer space. */
	if (lan_saa9730_allocate_buffers(lp))
		return -1;

	/* Stop LAN controller. */
	if (lan_saa9730_stop(lp))
		return -1;

	/* Initialize CAM registers. */
	if (lan_saa9730_cam_init(dev))
		return -1;

	/* Initialize MII registers. */
	if (lan_saa9730_mii_init(lp))
		return -1;

	/* Initialize control registers. */
	if (lan_saa9730_control_init(lp))
		return -1;

	/* Load CAM registers. */
	if (lan_saa9730_cam_load(lp))
		return -1;

	/* Initialize DMA context registers. */
	if (lan_saa9730_dma_init(lp))
		return -1;

	dev->open = lan_saa9730_open;
	dev->hard_start_xmit = lan_saa9730_start_xmit;
	dev->stop = lan_saa9730_close;
	dev->get_stats = lan_saa9730_get_stats;
	dev->set_multicast_list = lan_saa9730_set_multicast;
	dev->tx_timeout = lan_saa9730_tx_timeout;
	dev->watchdog_timeo = (HZ >> 1);
	dev->dma = 0;

	return 0;
}


static int __init saa9730_probe(void)
{
	struct net_device *dev = NULL;

	if (pci_present()) {
		struct pci_dev *pdev = NULL;
		if (lan_saa9730_debug > 1)
			printk
			    ("saa9730.c: PCI bios is present, checking for devices...\n");

		while ((pdev = pci_find_device(PCI_VENDOR_ID_PHILIPS,
					       PCI_DEVICE_ID_PHILIPS_SAA9730,
					       pdev))) {
			unsigned int pci_ioaddr;

			pci_irq_line = pdev->irq;
			/* LAN base address in located at BAR 1. */

			pci_ioaddr = pci_resource_start(pdev, 1);
			pci_set_master(pdev);

			printk("Found SAA9730 (PCI) at %#x, irq %d.\n",
			       pci_ioaddr, pci_irq_line);
			if (!lan_saa9730_init
			    (dev, pci_ioaddr, pci_irq_line)) return 0;
			else
				printk("Lan init failed.\n");
		}
	}

	return -ENODEV;
}

module_init(saa9730_probe);