summaryrefslogtreecommitdiff
path: root/driver/xf86-video-voodoo/src/voodoo_hardware.c
blob: 82efaf38b8d6816a860706cc92a37def6735f084 (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
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
/*
 * Portions derived from  linux/drivers/video/sstfb.c 
 *	-- voodoo graphics frame buffer
 *     Copyright (c) 2000-2002 Ghozlane Toumi <gtoumi@laposte.net>
 *
 * Relicensed from GPL to the X license by consent of the author
 *
 * Other code Alan Cox (c) Copyright 2004 Red Hat Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of Red Hat, Alan Cox and Ghozlane Toumi
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Th authors make no 
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL RICHARD HECKER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 * 
 * THIS SOFTWARE IS NOT DESIGNED FOR USE IN SAFETY CRITICAL SYSTEMS OF
 * ANY KIND OR FORM.
 *
 *	Freely reviewably music for this hacking supplied by
 *		http://www.hoerstreich.de
 *		http://www.machinaesupremacy.com
 *		http://www.magnatune.com
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "fb.h"
#include "mibank.h"
#include "micmap.h"
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86Version.h"
#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "xf86cmap.h"
#include "shadowfb.h"
#include "vgaHW.h"
#include "xf86DDC.h"
#include "xf86RAC.h"
#include "xf86Resources.h"
#include "xaa.h"
#include "compiler.h"

#include "voodoo.h"

#include <X11/extensions/xf86dgastr.h>

#include "opaque.h"
#define DPMS_SERVER
#include <X11/extensions/dpms.h>
#include "mipict.h"
#include "dixstruct.h"

static int debug = 0;

/*
 *	Big endian might need to byteswap these ?
 */
 
/*
 *	Write to chuck and bruce
 */
 
static CARD32 mmio32_r(VoodooPtr pVoo, int reg)
{
	volatile CARD32 *ptr = (CARD32 *)(pVoo->MMIO + reg);
	CARD32 val = *ptr;
	return val;
}

static void mmio32_w(VoodooPtr pVoo, int reg, CARD32 val)
{
	volatile CARD32 *ptr = (CARD32 *)(pVoo->MMIO + reg);
	*ptr = val;
}

/*
 *	Write to chuck only
 */
 
static void mmio32_w_chuck(VoodooPtr pVoo, int reg, CARD32 val)
{
	volatile CARD32 *ptr = (CARD32 *)(pVoo->MMIO + (reg |(1<<10)));
	*ptr = val;
}

/*
 *	Size the video RAM. Texture ram sizing is different and seperate
 *	but we don't use the texture ram for 2D anyway.
 */
 
int VoodooMemorySize(VoodooPtr pVoo)
{
	volatile CARD32 *fp = (volatile CARD32 *)pVoo->FBBase;
	fp[0] = 0xA5A5A5A5;
	fp[1<<18] = 0xA5A5A5A5;
	fp[1<<19] = 0xA5A5A5A5;
	
	fp[0] = 0x5A5A5A5A;
	
	if(fp[1<<19] == 0xA5A5A5A5)
		return 4;	/* 4Mb Card */
	if(fp[1<<18] == 0xA5A5A5A5)
		return 2;	/* 2Mb Card */
	return 1;
}

/*
 *	FIXME: If we are also doing 3D or DRI paths do we need
 *	to fix this path to issue a NOP command as per the chip errata? 
 */
 
static void wait_idle(VoodooPtr pVoo)
{
	int i = 0;
	
	while(i < 5)
	{
		if((mmio32_r(pVoo,0) & 0x80) == 0)
			i++;
	}
}
	
/*
 *	PCI control registers
 */

/*
 *	PCI enables. Used for setup, mode switch etc
 */ 

static void pci_enable(VoodooPtr pVoo, int wr, int dac, int fifo)
{
	CARD32 x;
	PCI_READ_LONG(pVoo->PciInfo, &x, 0x40);
	x &= ~7;
	x |= wr;
	x |= fifo<<1;
	x |= dac<<2;
	PCI_WRITE_LONG(pVoo->PciInfo, x, 0x40);
}

/*
 *	VClock control
 */
 
static void vclock_enable(VoodooPtr pVoo, int enable)
{
	if(enable)
		PCI_WRITE_LONG(pVoo->PciInfo, 0, 0xc0);
	else
		PCI_WRITE_LONG(pVoo->PciInfo, 0, 0xe0);
}

/*
 *	DPMS
 */
 
void VoodooBlank(VoodooPtr pVoo)
{
	vclock_enable(pVoo, 0);
	pci_enable(pVoo, 1, 0, 0);
	
	mmio32_w(pVoo, 0x214, mmio32_r(pVoo, 0x214) | 0x100);
	wait_idle(pVoo);
	
	mmio32_w(pVoo, 0x210, mmio32_r(pVoo, 0x210) | 6);
	wait_idle(pVoo);
	
	mmio32_w(pVoo, 0x218, mmio32_r(pVoo, 0x218) & ~(1<<22));
	wait_idle(pVoo);
}

/*
 *	Read and write the DAC registers
 */

static void dac_out(VoodooPtr pVoo, CARD32 reg, CARD32 val)
{
	mmio32_w(pVoo, 0x22C, (reg << 8) | val);
	wait_idle(pVoo);
}

static CARD32 dac_in(VoodooPtr pVoo, CARD32 reg)
{
	CARD32 r;
	mmio32_w(pVoo, 0x22C, (1<<11)|(reg<< 8));
	wait_idle(pVoo);
	r = mmio32_r(pVoo, 0x218) & 0xFF;
	return r;
}

/*
 *	Indexed (or should that be "demented") mode on the TI
 *	and AT&T DAC's
 */
 
static void dac_out_idx(VoodooPtr pVoo, CARD32 reg, CARD32 val)
{
	dac_out(pVoo, 0, reg);
	dac_out(pVoo, 2, val);
}

static CARD32 dac_in_idx(VoodooPtr pVoo, CARD32 reg)
{
	dac_out(pVoo, 0, reg);
	return dac_in(pVoo, 2);
}

/*
 *	We have generic ics5432 code we	perhaps ought to use instead. 
 *	This is based on the sstfb and Glide logic.
 */
 
#define ICS_PLL_CLK0_1_INI	0x55	/* Frequencies for detecting ICS */
#define ICS_PLL_CLK0_7_INI	0x71
#define ICS_PLL_CLK1_B_INI	0x79

static int probe_ics5432(VoodooPtr pVoo)
{
	int i;
	CARD32 m01, m07, m11;
	
	for(i = 0; i < 5; i++)
	{
		dac_out(pVoo, 7, 1);
		m01 = dac_in(pVoo,5);
		dac_in(pVoo,5);
		dac_out(pVoo,7, 7);
		m07 = dac_in(pVoo, 5);
		dac_in(pVoo,5);
		dac_out(pVoo,7, 11);
		m11 = dac_in(pVoo, 5);
		dac_in(pVoo, 5);
		
		if(m01 == ICS_PLL_CLK0_1_INI &&
		   m07 == ICS_PLL_CLK0_7_INI &&
		   m11 == ICS_PLL_CLK1_B_INI)
		   return 1;
	}
	return 0;
}

/*
 *	Activate backdoor on DACs
 */
 
static void dacdoor(VoodooPtr pVoo)
{
	dac_out(pVoo,0, 0);
	dac_in(pVoo,2);
	dac_in(pVoo,2);
	dac_in(pVoo,2);
	dac_in(pVoo,2);
}

/*
 *	Set a PLL on the DAC. Either vClock (0) or gClock (1)
 */

static void voodoo_set_pll(VoodooPtr pVoo, int pllnum)
{
	CARD32 cr0;
	if(pVoo->DAC == DAC_ID_ATT || pVoo->DAC == DAC_ID_TI)
	{
		CARD32 cc;
		dacdoor(pVoo);
		cr0 = dac_in(pVoo, 2);
		
		dacdoor(pVoo);
		dac_out(pVoo, 2, (cr0 & 0xF0) | 0xB);	/* Switch to index mode */
		
		/* Sleep a bit */
		usleep(300);
		
		cc = dac_in_idx(pVoo, 6);
		if(pllnum == 0)
		{
			dac_out_idx(pVoo, 0x48, pVoo->vClock.m);
			dac_out_idx(pVoo, 0x49, (pVoo->vClock.p << 6) | pVoo->vClock.n);
			dac_out_idx(pVoo, 6, (cc & 0x0F) | 0xA0 );
		}
		else
		{	
			dac_out_idx(pVoo, 0x6C, pVoo->gClock.m);
			dac_out_idx(pVoo, 0x6D, (pVoo->gClock.p << 6) | pVoo->vClock.n);
			dac_out_idx(pVoo, 6, (cc & 0x0F) | 0x0B);
		}
		return;
	}
	/* ICS5432 */
	dac_out(pVoo, 7, 14);
	cr0 = dac_in(pVoo, 5);
	if(pllnum == 0)
	{
		dac_out(pVoo, 4, 0);
		dac_out(pVoo, 5, pVoo->vClock.m);
		dac_out(pVoo, 5, pVoo->vClock.p << 5 | pVoo->vClock.n);
		dac_out(pVoo, 4, 14);
		dac_out(pVoo, 5, (cr0 & 0xD8) | (1<<5));
	}
	else
	{
		dac_out(pVoo, 4, 10);
		dac_out(pVoo, 5, pVoo->gClock.m);
		dac_out(pVoo, 5, pVoo->gClock.p << 5 | pVoo->gClock.n);
		dac_out(pVoo, 4, 14);
		dac_out(pVoo, 5, (cr0 & 0xEF));
	}
}

/*
 *	Set the depth in the DAC. This must match the frame
 *	buffer format. Right now we could hard code 16, in fact
 * 	it may be correct to always do so.. ?
 */
 
static void voodoo_set_depth(VoodooPtr pVoo, int depth)
{
	CARD32 cr0;
	
	if(pVoo->DAC == DAC_ID_ATT || pVoo->DAC == DAC_ID_TI)
	{
		dacdoor(pVoo);
		cr0 = dac_in(pVoo, 2);
		
		dacdoor(pVoo);
		
		if(depth == 16)
			dac_out(pVoo, 2, (cr0 & 0x0F) | 0x50);
		else if(depth ==24 || depth == 32)
			dac_out(pVoo, 2, (cr0 & 0x0F) | 0x70);
	}
	else if(pVoo->DAC == DAC_ID_ICS)
	{
		if(depth == 16)
			dac_out(pVoo, 6, 0x50);
		else
			dac_out(pVoo, 6, 0x70);
	}
}
		
static int voodoo_find_dac(VoodooPtr pVoo)
{
	CARD32 vendor_id, device_id;
	
#define DAC_VENDOR_ATT		0x84
#define DAC_DEVICE_ATT20C409	0x09
#define DAC_VENDOR_TI		0x97
#define DAC_DEVICE_TITVP3409	0x09

	dacdoor(pVoo);	
	dac_in(pVoo, 2);
	vendor_id = dac_in(pVoo, 2);
	device_id = dac_in(pVoo, 2);
	
	/* AT&T 20C409 and clones */
	if(vendor_id == DAC_VENDOR_ATT && DAC_DEVICE_ATT20C409)
		return DAC_ID_ATT;
		
	if(vendor_id == DAC_VENDOR_TI && DAC_DEVICE_TITVP3409)
		return DAC_ID_TI;
		
	/* ICS5432 doesn't implement the back door. Glide does some
	   quick tests to see if it is an ICS5432 just in case. */
	   
	if(probe_ics5432(pVoo))
		return DAC_ID_ICS;
	
	/* Shouldn't be any boards that get this far */
	ErrorF("Voodoo card with unknown DAC. Not supported.\n");
	return DAC_UNKNOWN;
}

/*
 *	Compute the PLL clock values. This is directly based on the
 *	technique used by sstfb. 
 */
 
/* compute the m,n,p  , returns the real freq
 * (ics datasheet :  N <-> N1 , P <-> N2)
 *
 * Fout= Fref * (M+2)/( 2^P * (N+2))
 *  we try to get close to the asked freq
 *  with P as high, and M as low as possible
 *  range:
 *	ti/att : 0 <= M <= 255; 0 <= P <= 3; 0<= N <= 63
 *	ics    : 1 <= M <= 127; 0 <= P <= 3; 1<= N <= 31
 *
 *  We will use the lowest limitation, should be precise enough
 */

static int iabs(int a)
{
	if(a >= 0)
		return a;
	return -a;
}

static int sst_calc_pll(const int freq, PLLClock *t)
{
	int m, m2, n, p, best_err, fout;
	int best_n=-1;
	int best_m=-1;

	best_err = freq;
	p=3;
	/* f * 2^P = vco should be less than VCOmax ~ 250 MHz for ics */
	while (((1 << p) * freq > 260000) && (p >= 0))
		p--;
	if (p == -1)
		return 0;
	for (n = 1; n < 32; n++) {
		/* Calc 2 * m so we can round it later */
		m2 = (2 * freq * (1 << p) * (n + 2) ) / 14318 - 4 ;

		m = (m2 % 2) ? m2/2+1 : m2/2 ;
		if (m >= 128)
			break;
		fout = (14318 * (m + 2)) / ((1 << p) * (n + 2));
		if (iabs(fout - freq) < best_err && m > 0) {	
			best_n = n;
			best_m = m;
			best_err = iabs(fout - freq);
			/* We get the lowest m , allowing 0.5% error in freq */
			if (200*best_err < freq)
				break;
		}
	}
	if (best_n == -1)  /* Unlikely, but who knows ? */
		return 0;
	t->p=p;
	t->n=best_n;
	t->m=best_m;
	return (14318 * (t->m + 2)) / ((1 << t->p) * (t->n + 2));
}


/*
 *		Here endeth the DAC code
 */
 

/*
 *		Higher level mode management
 */
 
static void voodoo_set_mode(VoodooPtr pVoo, DisplayModePtr mode)
{
	CARD32 hbporch = mode->CrtcHTotal - mode->CrtcHSyncEnd;
	CARD32 vbporch = mode->CrtcVTotal - mode->CrtcVSyncEnd;
	CARD32 hsyncstart = mode->CrtcHSyncEnd - mode->CrtcHSyncStart;
	CARD32 hsyncend = mode->CrtcHTotal - hsyncstart;
	CARD32 vsyncstart = mode->CrtcVSyncEnd - mode->CrtcVSyncStart;
	CARD32 vsyncend = mode->CrtcVTotal - vsyncstart;
	CARD32 width = mode->CrtcHDisplay;
	CARD32 height = mode->CrtcVDisplay;
	
	/* Even numbers of lines for interlace */
	if((mode->Flags & V_INTERLACE) && (vbporch & 1))
		vbporch += 1;
	/* Doublescan does what you expect */
	if(mode->Flags & V_DBLSCAN)
	{
		/* Not sure if we need to do all of these */
		vbporch <<=  1;
		hbporch <<= 1;
		hsyncend <<= 1;
		hsyncstart <<= 1;
		vsyncend <<= 1;
		vsyncstart <<= 1;
		width <<= 1;
		height <<= 1;
	}

	/* Write back porch values */	
	mmio32_w(pVoo, 0x208, (vbporch << 16) | (hbporch - 2));
	/* Write displayed area */
	mmio32_w(pVoo, 0x20C, (height << 16) | (width - 1));
	mmio32_w(pVoo, 0x220, (hsyncend - 1 ) << 16 | (hsyncstart - 1));
	mmio32_w(pVoo, 0x224, (vsyncend << 16) | vsyncstart);
}

/*
 *	Set up a Voodoo video mode. Note that we do need to have
 *	save/restore functions here too because the user might be 
 *	using sstfb... Eventually..
 */

int VoodooMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
	CARD32 f1, f2, f3;
	VoodooPtr pVoo = VoodooPTR(pScrn);
	int tiles;

	/* FIXME: Compute PLL and check */
	
	sst_calc_pll(mode->SynthClock, &pVoo->vClock);
		
	mmio32_w(pVoo, 0x120, 0);	/* NOP */
	wait_idle(pVoo);
	pci_enable(pVoo, 1, 0, 0);
	mmio32_w(pVoo, 0x214, mmio32_r(pVoo,0x214)|(1<<8));
	mmio32_w(pVoo, 0x210, mmio32_r(pVoo,0x210)|6);
	mmio32_w(pVoo, 0x218, mmio32_r(pVoo,0x218) & ~(1<<22));
	wait_idle(pVoo);

	voodoo_set_mode(pVoo, mode);
	
	f2 = mmio32_r(pVoo,0x218);
	f3 = mmio32_r(pVoo,0x21c);

	pci_enable(pVoo, 1, 1, 0);	
	
	voodoo_set_depth(pVoo, 16);	/* Hardware output is always 16bpp */
	voodoo_set_pll(pVoo, 0);
	
	pci_enable(pVoo, 1, 0, 0);
	
	mmio32_w(pVoo, 0x218, f2);
	mmio32_w(pVoo, 0x21C, f3);
	
	f1 = mmio32_r(pVoo,0x214);
	
	f1 &= 0x8080010F;		/* Mask off video bits */
	f1 |= 0x21E000;			/* Enable blanking, data, vsync, dclock 2x sel */
	
	/* Number of 64 pixel tiles */
	tiles = (mode->CrtcHDisplay + 63) / 64;
	
	if(pVoo->Voodoo2)
		f1|= ((tiles & 0x10) ? 1<<24 : 0) | (tiles & 0x0f) << 4;
	else
		f1|= tiles << 4;
		
	pVoo->Tiles = tiles * 2;
	pVoo->Width = mode->CrtcHDisplay;
	pVoo->Height = mode->CrtcVDisplay;
	if(!pVoo->Accel)
		pVoo->FullHeight = mode->CrtcVDisplay;
		 
	mmio32_w(pVoo, 0x214, f1);
	
	/* Voodoo 2 support */
	if(pVoo->Voodoo2)
	{
		CARD32 f5 = mmio32_r(pVoo, 0x244);
		mmio32_w(pVoo, 0x248, 0);
		
		f5 &= ~0x05BF0000;
		
		f5 &= ~(1<<22);	/* For now */

		if(mode->Flags & V_INTERLACE)
			f5 |= 1<<26;
		/* FIXME: is this H, V or both doublescan ?? */
		if(mode->Flags & V_DBLSCAN)
			f5 |= (1<<21) | (1<<20);
		if(mode->Flags & V_PHSYNC)
			f5 |= (1<<23);
		if(mode->Flags & V_PVSYNC)
			f5 |= (1<<24);
		mmio32_w(pVoo, 0x244, f5);
	}
	wait_idle(pVoo);
	mmio32_w(pVoo, 0x214, mmio32_r(pVoo,0x214) & ~0x100);
	mmio32_w(pVoo, 0x210, 1 |  (mmio32_r(pVoo,0x210) & ~6));
	mmio32_w(pVoo, 0x218, mmio32_r(pVoo,0x218) | (1<<22));
	
	pci_enable(pVoo, 0, 0, 1);

	mmio32_w(pVoo, 0x114, 0x100);
	pVoo->lfbMode = 0x100;	/* Cached LFB mode, front, front, on */

	/*
	 *	Set a clipping rectangle. We really deeply emphatically
	 *	don't want to write off screen otherwise.
	 */
	mmio32_w(pVoo, 0x118, mode->CrtcHDisplay);	
	mmio32_w(pVoo, 0x11C, mode->CrtcVDisplay);
	mmio32_w(pVoo, 0x110, /*mmio32_r(pVoo, 0x110) | */0x201);

	/*
	 *	Set up the 2D engine drawing logic
	 */
	
	if(pVoo->Voodoo2)
	{
		mmio32_w_chuck(pVoo, 0x2C0, 0);	/* Zero source */
		mmio32_w_chuck(pVoo, 0x2C4, 0);	/* Zero destination */
		/* Use screen sized tiles for src/dst */
		mmio32_w_chuck(pVoo, 0x2C8, (pVoo->Tiles << 16) | pVoo->Tiles);
		mmio32_w_chuck(pVoo, 0x2D4, pVoo->Width);
		mmio32_w_chuck(pVoo, 0x2D8, pVoo->FullHeight);
	}
	return 0;
}

/*
 *	Probe and initialize a Voodoo 1 or Voodoo 2 that we found.
 *	We don't set up any video mode at this point.
 */
 
int VoodooHardwareInit(VoodooPtr pVoo)
{
	vclock_enable(pVoo, 0);
	pci_enable(pVoo, 1, 0, 0);
	
	mmio32_w(pVoo, 0x214, mmio32_r(pVoo, 0x214) | 0x100);
	wait_idle(pVoo);

 	mmio32_w(pVoo, 0x210, mmio32_r(pVoo, 0x210) | 7); 
	wait_idle(pVoo);
	
	mmio32_w(pVoo, 0x218, mmio32_r(pVoo, 0x218) & ~(1<<22));
	wait_idle(pVoo);
	
	/* At this point we are basically shut down */
	
	pci_enable(pVoo, 1, 1, 0);
	
	pVoo->DAC = voodoo_find_dac(pVoo);
	
	/* Graphics clock depends on the board */
	pVoo->MaxClock = 50000;
	if(pVoo->Voodoo2)
		pVoo->MaxClock = 75000;
	
	sst_calc_pll(pVoo->MaxClock, &pVoo->gClock);
	voodoo_set_pll(pVoo, 1);

	pci_enable(pVoo, 1, 0, 1);	
	
	mmio32_w(pVoo, 0x210, 0);		/* 1 for VGA pass through */
	wait_idle(pVoo);
	mmio32_w(pVoo, 0x214, 0x1A8 | (1<<21) );
	wait_idle(pVoo);
	mmio32_w(pVoo, 0x218, 0x186000E0);	/* RAM setup with fast ras, oe, fifo, refresh on and a 16mS refresh */
	wait_idle(pVoo);
	mmio32_w(pVoo, 0x21C, 0x40);		/* Need to review with DRI */
	wait_idle(pVoo);
	mmio32_w(pVoo, 0x200, 2);
	wait_idle(pVoo);
	
	if(pVoo->Voodoo2)
	{
		mmio32_w(pVoo, 0x248, 0);
		wait_idle(pVoo);
	}

	pci_enable(pVoo, 0, 0, 1);
	vclock_enable(pVoo, 1);
	
	return 0;
}	

/*
 *	Copiers for Voodoo1
 *
 *	Voodoo1 has no CPU to screen blit, and also lacks SGRAM fill
 */
 
void VoodooCopy16(VoodooPtr pVoo, CARD32 x1, CARD32 y1, CARD32 w, CARD32 h, CARD32 spitch, unsigned char *src)
{
	/* DWord copy pointer */
	CARD32 *out = (CARD32 *)(pVoo->FBBase + y1 * pVoo->Pitch + x1 *2);
	CARD32 *in = (CARD32 *)src;
	/* DWords to skip */
	CARD32 skipo = (pVoo->Pitch - w*2)>>2;
	CARD32 skipi = (pVoo->ShadowPitch - w*2)>>2;
	int ct;
	
	/* LFB will do all our work for us */

	mmio32_w(pVoo, 0x10C, 0);
	mmio32_w(pVoo, 0x110, (1<<9)|1);
	mmio32_w(pVoo, 0x114, (1<<8));
	while(h > 0)
	{
		for(ct = 0; ct < w; ct+=2)
			*out++=*in++;
		in += skipi;
		out += skipo;
		h--;
	}
}

/*
 *	Copiers for Voodoo 1 and Voodoo 2 24bit
 *
 *	Voodoo1 has no CPU to screen blit, and also lacks SGRAM fill
 *	Voodoo2 has no read side 24bit, nor 24bit accelerator
 */
 
void VoodooCopy24(VoodooPtr pVoo, CARD32 x1, CARD32 y1, CARD32 w, CARD32 h, CARD32 spitch, unsigned char *src)
{
	/* DWord copy pointer */
	CARD32 *out = (CARD32 *)(pVoo->FBBase + y1 * pVoo->Pitch + x1 * 4);
	CARD32 *in = (CARD32 *)src;
	/* DWords to skip */
	CARD32 skipo = (pVoo->Pitch - w*4)>>2;
	CARD32 skipi = (pVoo->ShadowPitch - w*4)>>2;
	int ct;
	
	/* LFB will do all our work for us */
	/* FIXME: Should we allow dither options ? */
	
	mmio32_w(pVoo, 0x10C, 0);
	mmio32_w(pVoo, 0x110, (1<<9)|1);
	mmio32_w(pVoo, 0x114, (1<<8)|4);
	while(h > 0)
	{
		for(ct = 0; ct < w; ct++)
			*out++=*in++;
		in += skipi;
		out += skipo;
		h--;
	}
}

/*
 *	Use hardware clear. For Voodoo2 we want to use the SGRAM fill
 *	really. fbzMode bits 9/10 write to depth buffer, bit 14/15 to select
 *	the right buffer, bit 9 off for no dither, depth in zacolor, bits in
 *	colour1.
 *
 *	load cliplr/lyhy and then fire.
 */
 
void VoodooClear(VoodooPtr pVoo)
{
	memset(pVoo->FBBase,0, 0x400000);
#if 0
	/*
	 *	We can't do this as the 3D engine setup is not
	 *	done by this driver..
	 */
	mmio32_w(pVoo, 0x130, 0);		/* No Alpha ! */
	
	mmio32_w(pVoo, 0x118, pVoo->Width);
	mmio32_w(pVoo, 0x11C, pVoo->Height << 16);
	/* On entry we know Clip is set correctly so we wil clear the lot */
	mmio32_w(pVoo, 0x148, 0xC0C0C0);	/* RGB888 black */
	mmio32_w(pVoo, 0x130, 0xFFFF);	/* I think ?? */
	/* We want to write to screen and depth, front buffer, and no dither */
	mmio32_w(pVoo, 0x110, (mmio32_r(pVoo, 0x110) | 0x601) & 0xFFE00EE1);
	/* Fire */
	mmio32_w(pVoo, 0x124, 1);
	wait_idle(pVoo);
	/* In case X decides to read the LFB before clear finishes */
#endif	
}

/*
 *	Voodoo banking. The voodoo isn't exactly banked in the conventional
 *	sense but claiming to be banked allows us to use the back buffer
 *	as the pixcache, providing we are careful with our acceleration
 *	ops
 *
 *	There are two banks. Bank 0 is the front buffer, bank 1 is the
 *	back buffer. We don't use the aux buffer. Additionally the
 *	back bufer in 1024x768 with 2Mbyte cards is only a partial buffer.
 *	(No SLI yet)
 *
 *	Not yet used (TODO: figure out the offsets for the backbuffer layout)
 *	For now we work the screen as one deep display instead
 */
 
void VoodooReadBank(ScreenPtr pScreen, int bank)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	VoodooPtr pVoo = VoodooPTR(pScrn);
	if(bank)
	{
		mmio32_w(pVoo, 0x2C0, pVoo->Height);
		pVoo->lfbMode |= (1<<6);
	}
	else
	{
		mmio32_w(pVoo, 0x2C0, 0);
		pVoo->lfbMode &= ~(1<<6);
	}
	mmio32_w(pVoo, 0x114, pVoo->lfbMode);
}

void VoodooWriteBank(ScreenPtr pScreen, int bank)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	VoodooPtr pVoo = VoodooPTR(pScrn);
	if(bank)
	{
		mmio32_w(pVoo, 0x2C4, pVoo->Height);
		pVoo->lfbMode |= (1<<4);
	}
	else
	{
		mmio32_w(pVoo, 0x2C4, 0);
		pVoo->lfbMode &= ~(1<<4);
	}
	mmio32_w(pVoo, 0x114, pVoo->lfbMode);
}

void VoodooReadWriteBank(ScreenPtr pScreen, int bank)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	VoodooPtr pVoo = VoodooPTR(pScrn);
	if(bank)
	{
		mmio32_w(pVoo, 0x2C0, pVoo->Height);
		mmio32_w(pVoo, 0x2C4, pVoo->Height);
		pVoo->lfbMode |= (1<<4) | (1<<6);
	}
	else
	{
		mmio32_w(pVoo, 0x2C0, 0);
		mmio32_w(pVoo, 0x2C4, 0);
		pVoo->lfbMode &= ~((1<<4) | (1<<6));
	}
	mmio32_w(pVoo, 0x114, pVoo->lfbMode);
}

/*
 *	We normally want to load all four rop variants at once so
 *	the table is the 16bits for the lot equal.
 */

static CARD16 ropxlate[16] = {
	0x0000, 	/* GXclear */
	0x8888,		/* GXand */
	0x4444,		/* GXandReverse */
	0xCCCC,		/* GXcopy */
	0x2222,		/* GXandInverted */
	0xAAAA,		/* GXnop */
	0x6666,		/* GXxor */
	0xEEEE,		/* GXor */
	0x1111,		/* GXnor */
	0x9999,		/* GXequiv */
	0x5555,		/* GXinvert */
	0xDDDD,		/* GXorReverse */
	0x3333,		/* GXcopyInverted */
	0xBBBB,		/* GXorInverted */
	0x7777,		/* GXnand */
	0xFFFF		/* GXset */
};

/*
 *	Transparent mask rops
 */

static CARD16 tropxlate[16] = {
	0xAA00, 	/* GXclear */
	0xAA88,		/* GXand */
	0xAA44,		/* GXandReverse */
	0xAACC,		/* GXcopy */
	0xAA22,		/* GXandInverted */
	0xAAAA,		/* GXnop */
	0xAA66,		/* GXxor */
	0xAAEE,		/* GXor */
	0xAA11,		/* GXnor */
	0xAA99,		/* GXequiv */
	0xAA55,		/* GXinvert */
	0xAADD,		/* GXorReverse */
	0xAA33,		/* GXcopyInverted */
	0xAABB,		/* GXorInverted */
	0xAA77,		/* GXnand */
	0xAAFF		/* GXset */
};

	
void VoodooSync(ScrnInfoPtr pScrn)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	/* FIXME: Check if cliprect dirty if so rewrite */
	wait_idle(pVoo);
	mmio32_w(pVoo, 0x10C, 0);	/* Maybe flag this */
}

static void Voodoo2Setup2D(VoodooPtr pVoo)
{
	wait_idle(pVoo);
}

static void Voodoo2SetupForScreenToScreenCopy(ScrnInfoPtr pScrn,
			int xdir, int ydir, int rop,
			unsigned int planemask,
			int trans_color)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	Voodoo2Setup2D(pVoo);
	pVoo->BlitDirX = xdir;
	pVoo->BlitDirY = ydir;
	
	if(trans_color == -1)
	{
		mmio32_w_chuck(pVoo, 0x2EC, ropxlate[rop]);	/* Set the rop */
		mmio32_w_chuck(pVoo, 0x2F8, 0 | (1<<14) | (1<<15) | (1<<16));	/* 16bpp no color compare */
	}
	else
	{
		mmio32_w_chuck(pVoo, 0x2EC, tropxlate[rop]);	 /* Transparent src rop */
		mmio32_w_chuck(pVoo, 0x2CC, (trans_color << 16) | trans_color);	/* Match transparent colour */
		mmio32_w_chuck(pVoo, 0x2F8, 0 | (1<<10) | (1<<14) | (1<<15) | (1<<16));	/* 16bpp color compare */
	}
}

static void Voodoo2SubsequentScreenToScreenCopy(ScrnInfoPtr pScrn,
			int x1, int y1,
			int x2, int y2,
			int width, int height)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	wait_idle(pVoo);
	/* Adjust co-ordinates for backward blits */
	height --;	/* Adjust for fenceposting in the hardware */
	width --;
	if(pVoo->BlitDirY < 0)
	{
		y1 += height;
		y2 += height;
		height = -height;
	}
	if(pVoo->BlitDirY < 0)
	{
		x1 += width;
		x2 += width;
		width = -width;
	}
	mmio32_w_chuck(pVoo, 0x2E0, (y1 << 16) | x1);  /* Src x/y */
	mmio32_w_chuck(pVoo, 0x2E4, (y2 << 16) | x2);  /* Dst x/y */
	/* Set size and fire */
	height &= 0xFFF;
	width  &= 0xFFF;
	mmio32_w_chuck(pVoo, 0x2E8, (height << 16) | width | (1<<31));
}

static void Voodoo2SetupForSolidFill(ScrnInfoPtr pScrn, int color,
			int rop, unsigned int planemask)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	Voodoo2Setup2D(pVoo);
	mmio32_w_chuck(pVoo, 0x2EC, ropxlate[rop]); 	/* rop */
	mmio32_w_chuck(pVoo, 0x2F0, color);		/* fg color */
	mmio32_w_chuck(pVoo, 0x2F8, 2 | (1<<14) | (1<<15) | (0/*1*/<<16)); /* Solid fill 16bpp front */
}

static void Voodoo2SubsequentSolidFillRect(ScrnInfoPtr pScrn, int x, int y, 
			int w, int h)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	wait_idle(pVoo);
	mmio32_w_chuck(pVoo, 0x2E4, (y<<16) | x);	/* Dst x,y */
	/* Set size and fire */
	mmio32_w_chuck(pVoo, 0x2E8, ((h-1) << 16) | (w-1) | (1<<31));
}


/*
 *	Colour expand fills are standard hardware goodies
 */
 
static void Voodoo2SetupForScanlineCPUToScreenColorExpandFill(ScrnInfoPtr pScrn,
			int fg, int bg,
			int rop,
			unsigned int planemask)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	Voodoo2Setup2D(pVoo);
	mmio32_w_chuck(pVoo, 0x2EC, ropxlate[rop]);	/* Pattern op */
	mmio32_w_chuck(pVoo, 0x2F0, fg | (bg << 16));	/* colors */
	if(bg != -1)	/* Set transparent if needed */
		mmio32_w_chuck(pVoo, 0x2F8, 1 | (1<<14) | (1<<15) | (1<<16));
	else
		mmio32_w_chuck(pVoo, 0x2F8, 1 | (1<<14) | (1<<15) | (1<<16) | (1<<17));
}

static void Voodoo2SubsequentScanlineCPUToScreenColorExpandFill(ScrnInfoPtr pScrn,
			int x, int y, int w, int h,
			int skipleft)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	wait_idle(pVoo);
	mmio32_w_chuck(pVoo, 0x2E4,  x | (y<<16));			/* destination */
	mmio32_w_chuck(pVoo, 0x2E8, (w-1) | ((h-1)<<16) | (1<<31)); 	/* fire */
	pVoo->texW = w;
}

static void Voodoo2SubsequentColorExpandScanline(ScrnInfoPtr pScrn, int bufno)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	CARD32 *data = (CARD32 *)pVoo->LineBuffer;
	int w = pVoo->texW;
	int i;
	
	wait_idle(pVoo);
	for(i = 0; i < w; i += 32)	/* Each dword is 32 pixels mask */
		mmio32_w(pVoo, 0x2FC, *data++);
}

static void Voodoo2SetupForMono8x8PatternFill(ScrnInfoPtr pScrn, int patx, int paty,
	int fg, int bg, int rop, unsigned int planemask)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	Voodoo2Setup2D(pVoo);
	mmio32_w_chuck(pVoo, 0x2EC, ropxlate[rop]);
	mmio32_w_chuck(pVoo, 0x2F0, fg | (bg << 16));
	if(bg != -1)
		mmio32_w_chuck(pVoo, 0x2F8, 1 | (1<<14) | (1<<15) | (1<<16));
	else
		mmio32_w_chuck(pVoo, 0x2F8, 1 | (1<<14) | (1<<15) | (1<<16) | (1<<17));
}

/*
 *	We don't have pattern fill hardware but for any operation that
 *	references dst it is going to be faster to use the hardware
 *	and simply upload the pattern a lot as we avoid reading
 *	video memory. GXcopy ought to be the same either way.
 */
 
static __inline__ CARD32 spread(CARD32 v)
{
	return v * 16843009;
}

static void Voodoo2SubsequentMono8x8PatternFillRect(ScrnInfoPtr pScrn, int patx, int paty,
	int x, int y, int w, int h)
{
	int ln = 0;
	CARD32 l[8];
	VoodooPtr pVoo = VoodooPTR(pScrn);

	wait_idle(pVoo);
	
	if( w <3)
		return;
	
	mmio32_w_chuck(pVoo, 0x2E4,  x | (y<<16));
	mmio32_w_chuck(pVoo, 0x2E8, (w - 1) | ((h - 1)<<16) | (1<<31));
	
	/* Turn the pattern into 32x8 for the expansion engine */
	l[0] = spread((patx >> 24) & 0xFF);
	l[1] = spread((patx >> 16) & 0xFF);
	l[2] = spread((patx >> 8)  & 0xFF);
	l[3] = spread(patx & 0xFF);
	
	l[4] = spread((paty >> 24) & 0xFF);
	l[5] = spread((paty >> 16) & 0xFF);
	l[6] = spread((paty >> 8)  & 0xFF);
	l[7] = spread(paty & 0xFF);
	
	while(h > 0)
	{
		int i;
		for(i = 0; i < w; i += 32) /* DWORD pad */
			mmio32_w_chuck(pVoo, 0x2FC, l[ln]);
		wait_idle(pVoo);
		ln = (ln + 1) & 7;
		h--;
	}
}

/*
 *	The XAA layer uses video memory as the basis for colour pattern
 *	fill, so we can't usefully perform it. 
 */
 
static void Voodoo2SetupForSolidLine(ScrnInfoPtr pScrn, int color, int rop,
	unsigned int planemask)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	Voodoo2Setup2D(pVoo);
	mmio32_w_chuck(pVoo, 0x2CC, color);
	mmio32_w_chuck(pVoo, 0x2EC, ropxlate[rop]);
	mmio32_w_chuck(pVoo, 0x2F8, 2 | (1<<14) | (1<<15) | (1<<16)); 	/* Solid fill 16 bpp front */
}	

static void Voodoo2SubsequentSolidHorVertLine(ScrnInfoPtr pScrn, int x, int y, int len, int dir)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	wait_idle(pVoo);
	mmio32_w_chuck(pVoo, 0x2E4, (y<<16) | x);	/* Dst x,y */
	if(dir == DEGREES_0)
		mmio32_w_chuck(pVoo, 0x2E8,  (len - 1) | (1<<31));
	else
		mmio32_w_chuck(pVoo, 0x2E8, ((len - 1) << 16) | (1<<31));
}
	
static void Voodoo2SetupForScanlineImageWrite(ScrnInfoPtr pScrn, int rop,
			unsigned int planemask, int trans_color,
			int bpp, int depth)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	Voodoo2Setup2D(pVoo);
	if(trans_color != -1)
	{
		mmio32_w_chuck(pVoo, 0x2CC, (trans_color << 16) | trans_color);
		mmio32_w_chuck(pVoo, 0x2EC, tropxlate[rop]);
		mmio32_w_chuck(pVoo, 0x2F8, 1 | (2<<3) | (1<<10) | (1<<14) | (1<<15) | (1<<16));
	}
	else
	{
		mmio32_w_chuck(pVoo, 0x2EC, ropxlate[rop]);
		mmio32_w_chuck(pVoo, 0x2F8, 1 | (2<<3) | (1<<14) | (1<<15) | (1<<16));
	}
	if(debug)
		ErrorF("Setup for image write rop %d col %d bpp %d depth %d\n",
			rop, trans_color, bpp, depth);
}

static void Voodoo2SubsequentImageWriteRect(ScrnInfoPtr pScrn, 
			int x, int y,
			int w, int h,
			int skipleft)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	wait_idle(pVoo);
	mmio32_w_chuck(pVoo, 0x2E4, x | (y<<16));
	mmio32_w_chuck(pVoo, 0x2E8, (w - 1) | ((h - 1)<<16) | (1<<31));
	if(debug)
		ErrorF("Image Write (%d,%d) [%d,%d]\n", x,y,w,h);
	pVoo->texW = w;
}

static void Voodoo2SubsequentImageWriteScanline(ScrnInfoPtr pScrn, int bufno)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	CARD32 *data = (CARD32 *)pVoo->LineBuffer;
	int w = pVoo->texW;
	int i;
	
	wait_idle(pVoo);
	for(i = 0; i < w; i += 2)
		mmio32_w(pVoo, 0x2FC, *data++);
}

static void Voodoo2SetClippingRectangle(ScrnInfoPtr pScrn,
			int left, int top, int right, int bottom)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	if(debug)
		ErrorF("Clip to (%d,%d)-(%d,%d)\n", left,top,right,bottom);
	mmio32_w_chuck(pVoo, 0x2D4, (left << 16) | right);
	mmio32_w_chuck(pVoo, 0x2D8, (top << 16 ) | bottom);
}

static void Voodoo2DisableClipping(ScrnInfoPtr pScrn)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	/* FIXME: pVoo->FullHeight for the cache ! */
	if(debug)
		ErrorF("Clip to (0,0)-(%d,%d)\n", (int)pVoo->Width, (int)pVoo->Height);
	mmio32_w_chuck(pVoo, 0x2D4, pVoo->Width);
	mmio32_w_chuck(pVoo, 0x2D8, pVoo->FullHeight);
}

/*
 *	TODO: Implement 2D line acceleration using the 3D engines
 */
 
#ifdef RENDER

/*
 *	Render acceleration. All Voodoo chips support cpu driver alpha
 *	composite to the frame buffer. This is presumably meant for software
 *	fallbacks on rendering 3D but happens to be very useful to avoid
 *	some render operations reading from the frame buffer as much
 *
 *	Possibly we could the 3D engine for this once we get it working.
 *	We can't however use the 2D engine much as it lacks Alpha
 */
 

Bool VoodooSetupForCPUToScreenAlphaTexture(ScrnInfoPtr pScrn, int op, CARD16 red, 
	CARD16 green, CARD16 blue, CARD16 alpha, int alphaType, CARD8 *alphaPtr,
	int alphaPitch,	int width, int height, int flags)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);

	pVoo->alphaType = alphaType;
	pVoo->alphaPitch = alphaPitch;
	pVoo->alphaPtr = alphaPtr;
	pVoo->alphaW = width;
	pVoo->alphaH = height;
	pVoo->alphaC = (red & 0xFF00)  << 8 | (green & 0xFF00) | blue >> 8;
	
	if(op != PictOpOver && op != PictOpSrc)
		return FALSE;

	if(debug)
		ErrorF("Supported CPU To Screen Alpha Texture (%d) -> %d,%d\n", op, width, height);		
	wait_idle(pVoo);
	if(op == PictOpSrc)
		pVoo->alpha = 0;
	else	/* dst = src * srcalpha + (1-a) * dst */
		pVoo->alpha = (1<<4) | (1<<8) | (5<<12);

	return TRUE;	
}	

void VoodooSubsequentCPUToScreenAlphaTexture(ScrnInfoPtr pScrn, int dstx, int dsty, int srcx, int srcy, int width, int height)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	/* 32bit LFB write mode */
	CARD32 *fb = (CARD32 *)(pVoo->FBBase + 4096 * dsty + 4 * dstx);
	CARD8 *db = pVoo->alphaPtr + pVoo->alphaW * srcy + srcx;
	int x, y;
	CARD32 *fdb;
	CARD8 *cdb;
	CARD32 colour = pVoo->alphaC;
	int dw, dh;
	int w, h;

	mmio32_w(pVoo, 0x10C, pVoo->alpha);
	mmio32_w(pVoo, 0x110, 1 | (1<<9));
	mmio32_w(pVoo, 0x114, (1<<8) | 5);	/* ARGB888 */

	dh = srcy;
	w = pVoo->alphaW;
	h = pVoo->alphaH;
	
	for(y = 0; y < height; y++)
	{
		cdb = db;
		fdb = fb;

		dw = srcx;
		for(x = 0; x < width; x++)
		{
			*fdb++ = (*cdb++<< 24) | colour;
			if(++dw == w)
			{
				dw = 0;
				cdb -= pVoo->alphaW;
			}
		}
		db += pVoo->alphaW;
		fb += 1024;
		if(++dh == h)
		{
			db = pVoo->alphaPtr + srcx;
			dh = 0;
		}
	}	
	mmio32_w(pVoo, 0x114, pVoo->lfbMode);
	mmio32_w(pVoo, 0x10C, 0);
}

Bool VoodooSetupForCPUToScreenTexture(ScrnInfoPtr pScrn, int op, int texType,
	CARD8 *texPtr, int texPitch, int width, int height, int flags)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	
	if(op != PictOpOver && op != PictOpSrc)
		return FALSE;	/* For now */

	if(debug)
		ErrorF("Supported CPU TO Screen Texture (%d) -> %d,%d\n", op, width, height);		
	pVoo->texType = texType;
	pVoo->texPitch = texPitch;
	pVoo->texPtr = texPtr;
	pVoo->texW = width;
	pVoo->texH = height;

	wait_idle(pVoo);
	if(op == PictOpSrc || texType == PICT_x8r8g8b8)
		pVoo->alpha = 0;
	else
		pVoo->alpha = (1<<4) | (1<<8) | (5<<12);
		
	return TRUE;
}

void VoodooSubsequentCPUToScreenTexture(ScrnInfoPtr pScrn, int dstx, int dsty, int srcx, int srcy, int width, int height)
{
	VoodooPtr pVoo = VoodooPTR(pScrn);
	/* 32bit LFB write mode */
	CARD32 *fb = (CARD32 *)(pVoo->FBBase + 4096 * dsty + 4 * dstx);
	CARD32 *db = ((CARD32 *)(pVoo->texPtr)) + pVoo->texW * srcy + srcx;
	int x, y;
	CARD32 *cdb, *fdb;
	int dw, dh;
	int w, h;

	mmio32_w(pVoo, 0x10C, pVoo->alpha);
	mmio32_w(pVoo, 0x110, 1 | (1<<9));
	
	if(pVoo->texType == PICT_a8r8g8b8)
		mmio32_w(pVoo, 0x114, (1<<8) | 5);	/* ARGB888 */
	else if(pVoo->texType == PICT_x8r8g8b8)
		mmio32_w(pVoo, 0x114, (1<<8) | 4);	/* xRGB888 */
	else ErrorF("BOGOFORMAT\n");
	
	dh = srcy;
	w = pVoo->texW;
	h = pVoo->texH;
	
	if(debug)
		ErrorF("CPUToScreenTexture (%d,%d)->(%d,%d)[%d,%d]\n",
			srcx,srcy,dstx,dsty,width,height);
	/*
	 *	Tiled software render letting hardware do the read merge
	 *	that we don't want the CPU to do.
	 */
	 
	for(y = 0; y < height; y++)
	{
		cdb = db;
		fdb = fb;
		dw = srcx;
		for(x = 0; x < width; x++)
		{
			*fdb++ = *cdb++;
			
			if(++dw == w)
			{
				dw = 0;
				cdb -= pVoo->texW;
			}
		}
		db += pVoo->texW;
		fb += 1024;
		dh ++;
		if(dh == h)
		{
			db = ((CARD32 *)pVoo->texPtr) + srcx;
			dh = 0;
		}
	}	
	mmio32_w(pVoo, 0x114, pVoo->lfbMode);
	mmio32_w(pVoo, 0x10C, 0);
}

CARD32 VoodooAlphaTextureFormats[2] = {PICT_a8, 0};
CARD32 VoodooTextureFormats[3] = {PICT_a8r8g8b8, PICT_x8r8g8b8, 0};

#endif

void Voodoo2XAAInit(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	VoodooPtr pVoo = VoodooPTR(pScrn);
	XAAInfoRecPtr pAccel = XAACreateInfoRec();
	BoxRec cacheArea;
	
	pAccel->Flags = OFFSCREEN_PIXMAPS|LINEAR_FRAMEBUFFER;
	pAccel->Sync = VoodooSync;

	pAccel->ScreenToScreenCopyFlags =  NO_PLANEMASK;
	pAccel->SetupForScreenToScreenCopy = Voodoo2SetupForScreenToScreenCopy;
	pAccel->SubsequentScreenToScreenCopy = Voodoo2SubsequentScreenToScreenCopy;

	pAccel->SolidFillFlags = NO_PLANEMASK;
	pAccel->SetupForSolidFill = Voodoo2SetupForSolidFill;
	pAccel->SubsequentSolidFillRect = Voodoo2SubsequentSolidFillRect;

	pAccel->ScanlineCPUToScreenColorExpandFillFlags = 
		BIT_ORDER_IN_BYTE_MSBFIRST | NO_PLANEMASK | 
			SCANLINE_PAD_DWORD | CPU_TRANSFER_BASE_FIXED;
	pAccel->SetupForScanlineCPUToScreenColorExpandFill = 
				Voodoo2SetupForScanlineCPUToScreenColorExpandFill;
	pAccel->SubsequentScanlineCPUToScreenColorExpandFill =
				Voodoo2SubsequentScanlineCPUToScreenColorExpandFill;
	pAccel->SubsequentColorExpandScanline = 
				Voodoo2SubsequentColorExpandScanline;

	pAccel->NumScanlineColorExpandBuffers = 1;
	pVoo->LinePtr = pVoo->LineBuffer;
	pAccel->ScanlineColorExpandBuffers = &pVoo->LinePtr;

	pAccel->SetupForSolidLine = Voodoo2SetupForSolidLine;
	pAccel->SubsequentSolidHorVertLine = Voodoo2SubsequentSolidHorVertLine;
	pAccel->SolidLineFlags = NO_PLANEMASK;

	pAccel->Mono8x8PatternFillFlags = HARDWARE_PATTERN_PROGRAMMED_BITS;
	pAccel->SetupForMono8x8PatternFill = Voodoo2SetupForMono8x8PatternFill;
	pAccel->SubsequentMono8x8PatternFillRect = Voodoo2SubsequentMono8x8PatternFillRect;

	pAccel->ScanlineImageWriteFlags = NO_PLANEMASK;
	pAccel->SetupForScanlineImageWrite = Voodoo2SetupForScanlineImageWrite;
	pAccel->SubsequentImageWriteRect = Voodoo2SubsequentImageWriteRect;
	pAccel->SubsequentImageWriteScanline = Voodoo2SubsequentImageWriteScanline;

	pAccel->ClippingFlags =
		HARDWARE_CLIP_SCREEN_TO_SCREEN_COLOR_EXPAND |
		HARDWARE_CLIP_SCREEN_TO_SCREEN_COPY |
		HARDWARE_CLIP_MONO_8x8_FILL |
		HARDWARE_CLIP_SOLID_FILL;
		
	pAccel->SetClippingRectangle = Voodoo2SetClippingRectangle;
	pAccel->DisableClipping = Voodoo2DisableClipping;
	
#ifdef RENDER
	pAccel->CPUToScreenAlphaTextureFlags = 0;
	pAccel->SetupForCPUToScreenAlphaTexture = VoodooSetupForCPUToScreenAlphaTexture;
	pAccel->SubsequentCPUToScreenAlphaTexture = VoodooSubsequentCPUToScreenAlphaTexture;
	
	pAccel->CPUToScreenTextureFlags = 0;
	pAccel->SetupForCPUToScreenTexture = VoodooSetupForCPUToScreenTexture;
	pAccel->SubsequentCPUToScreenTexture = VoodooSubsequentCPUToScreenTexture;
	
	pAccel->CPUToScreenTextureFormats = VoodooTextureFormats;
	pAccel->CPUToScreenAlphaTextureFormats = VoodooAlphaTextureFormats;
#endif

	cacheArea.x1 = 0;
	cacheArea.x2 = pScrn->displayWidth;
	cacheArea.y1 = pVoo->Height;
	cacheArea.y2 = (pScrn->videoRam * 1024) / (pVoo->Tiles * 64);
	if(cacheArea.y2 > 2047)
		cacheArea.y2 = 2047;
		
	if(cacheArea.y2 > cacheArea.y1)
	{
		xf86DrvMsg(pScrn->scrnIndex, X_DEFAULT, "Using %d lines of pixmap cache.\n", cacheArea.y2-cacheArea.y1);
		pAccel->Flags |= PIXMAP_CACHE;
		pVoo->FullHeight = cacheArea.y2;
		xf86InitFBManager(pScreen, &cacheArea);
	}
	if( XAAInit(pScreen, pAccel) == FALSE)
		ErrorF("Unable to set up acceleration.\n");
		
	Voodoo2DisableClipping(pScrn);
}