summaryrefslogtreecommitdiff
path: root/src/via_mode.c
blob: 1d3870e35c65fe6627c62bd395ec58f85b643995 (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
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
/*
 * Copyright 2004-2006 Luc Verhaegen.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*
 * via_mode.c
 *
 * Everything to do with setting and changing modes.
 *
 */

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

#include "via_driver.h"
#include "via_vgahw.h"
#include "via_id.h"
#include "via_mode.h"

/*
 * Some VGA register debugging.
 */
void
ViaVgaPrintRegs(ScrnInfoPtr pScrn, const char *function)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    int i;

    ViaDebug(pScrn->scrnIndex, "%s: Printing VGA registers:\n", function);
    ViaDebug(pScrn->scrnIndex, "Printing VGA Sequence registers:\n");
    for (i = 0x00; i < 0x80; i++)
	ViaDebug(pScrn->scrnIndex, "SR%02X: 0x%02X\n", i, hwp->readSeq(hwp, i));

    ViaDebug(pScrn->scrnIndex, "Printing VGA CRTM/C registers:\n");
    for (i = 0x00; i < 0x19; i++)
        ViaDebug(pScrn->scrnIndex, "CR%02X: 0x%02X\n", i, hwp->readCrtc(hwp, i));
    for (i = 0x33; i < 0xA3; i++)
	ViaDebug(pScrn->scrnIndex, "CR%02X: 0x%02X\n", i, hwp->readCrtc(hwp, i));

    ViaDebug(pScrn->scrnIndex, "Printing VGA Graphics registers:\n");
    for (i = 0x00; i < 0x08; i++)
	ViaDebug(pScrn->scrnIndex, "GR%02X: 0x%02X\n", i, hwp->readGr(hwp, i));

    ViaDebug(pScrn->scrnIndex, "Printing VGA Attribute registers:\n");
    for (i = 0x00; i < 0x14; i++)
	ViaDebug(pScrn->scrnIndex, "AR%02X: 0x%02X\n", i, hwp->readAttr(hwp, i));
    
    ViaDebug(pScrn->scrnIndex, "Printing VGA Miscellaneous register:\n");
    ViaDebug(pScrn->scrnIndex, "Misc: 0x%02X\n", hwp->readMiscOut(hwp));

    ViaDebug(pScrn->scrnIndex, "End of VGA Registers.\n");
}

/*
 *
 * Dotclock handling.
 *
 */


/*
 * Standard vga call really.
 * Needs to be called to reset the dotclock (after SR40:2/1 reset)
 */
void
ViaSetUseExternalClock(ScrnInfoPtr pScrn)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    CARD8 data;

    VIAFUNC(pScrn->scrnIndex);

    data = hwp->readMiscOut(hwp);
    hwp->writeMiscOut(hwp, data | 0x0C);
}


/* 
 *
 */
static void
ViaSetPrimaryDotclock(ScrnInfoPtr pScrn, CARD32 clock)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    VIAPtr pVia = VIAPTR(pScrn);

    ViaDebug(pScrn->scrnIndex, "%s to 0x%lX\n", __func__, clock);

    if ((pVia->Chipset == VT3122) || (pVia->Chipset == VT7205)) {
	hwp->writeSeq(hwp, 0x46, clock >> 8);
	hwp->writeSeq(hwp, 0x47, clock & 0xFF);
    } else { /* unichrome pro */
	hwp->writeSeq(hwp, 0x44, clock >> 16);
	hwp->writeSeq(hwp, 0x45, (clock >> 8) & 0xFF);
	hwp->writeSeq(hwp, 0x46, clock & 0xFF);
    }

    ViaSeqMask(hwp, 0x40, 0x02, 0x02);
    ViaSeqMask(hwp, 0x40, 0x00, 0x02);
}


/* 
 *
 */
void
ViaSetSecondaryDotclock(ScrnInfoPtr pScrn, CARD32 clock)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    VIAPtr pVia = VIAPTR(pScrn);

    ViaDebug(pScrn->scrnIndex, "%s to 0x%lX\n", __func__, clock);

    if ((pVia->Chipset == VT3122) || (pVia->Chipset == VT7205)) {
	hwp->writeSeq(hwp, 0x44, clock >> 8);
	hwp->writeSeq(hwp, 0x45, clock & 0xFF);
    } else { /* unichrome pro */
	hwp->writeSeq(hwp, 0x4A, clock >> 16);
	hwp->writeSeq(hwp, 0x4B, (clock >> 8) & 0xFF);
	hwp->writeSeq(hwp, 0x4C, clock & 0xFF);
    }

    ViaSeqMask(hwp, 0x40, 0x04, 0x04);
    ViaSeqMask(hwp, 0x40, 0x00, 0x04);
}

/*
 *
 */
static CARD32
VT3122PLLGenerateBest(int Clock, int Shift, int MinDiv, int MaxDiv,
                             int *BestDiff)
{
    CARD32 PLL = 0;
    CARD8 PLLShift;
    int Div, Mult, Diff;
    float Ratio = Clock / 14318.0;

    switch (Shift) {
    case 4:
        PLLShift = 0x80;
        break;
    case 2:
        PLLShift = 0x40;
        break;
    default:
        PLLShift = 0x00;
        break;
    }

    for (Div = MinDiv; Div <= MaxDiv; Div++) {
        Mult = Ratio * Div * Shift + 0.5;
        
        if (Mult < 129) {
            Diff = Clock - Mult * 14318 / Div / Shift;

            if (Diff < 0)
                Diff *= -1;

            if (Diff < *BestDiff) {
                *BestDiff = Diff;
                PLL = ((PLLShift | Div) << 8) | Mult;
            }
        }
    }

    return PLL;
}

/*
 * This might seem nasty and ugly, but it's the best solution given the crappy
 * limitations the VT3122 PLL has.
 *
 * The below information has been gathered using nothing but a lot of time and
 * perseverance.
 */
static CARD32
VT3122PLLGenerate(ScrnInfoPtr pScrn, int Clock)
{
    CARD32 PLL;
    int Diff = 300000;

    VIAFUNC(pScrn->scrnIndex);

    if (Clock > 72514)
        PLL = VT3122PLLGenerateBest(Clock, 1, 2, 25, &Diff);
    else if (Clock > 71788)
        PLL = VT3122PLLGenerateBest(Clock, 1, 16, 24, &Diff);
    else if (Clock > 71389) {
        PLL = 0x1050; /* Big singularity. */
        
        Diff = Clock - 71590;
        if (Diff < 0)
            Diff *= -1;
    } else if (Clock > 48833) {
        CARD32 tmpPLL;

        PLL = VT3122PLLGenerateBest(Clock, 2, 7, 18, &Diff);
    
        if (Clock > 69024)
            tmpPLL = VT3122PLLGenerateBest(Clock, 1, 15, 23, &Diff);
        else if (Clock > 63500)
            tmpPLL = VT3122PLLGenerateBest(Clock, 1, 15, 21, &Diff);
        else if (Clock > 52008)
            tmpPLL = VT3122PLLGenerateBest(Clock, 1, 17, 19, &Diff);
        else
            tmpPLL = VT3122PLLGenerateBest(Clock, 1, 17, 17, &Diff);

        if (tmpPLL)
            PLL =  tmpPLL;
    } else if (Clock > 35220)
        PLL = VT3122PLLGenerateBest(Clock, 2, 11, 24, &Diff);
    else if (Clock > 34511)
        PLL = VT3122PLLGenerateBest(Clock, 2, 11, 23, &Diff);
    else if (Clock > 33441)
        PLL = VT3122PLLGenerateBest(Clock, 2, 13, 22, &Diff);
    else if (Clock > 31967)
        PLL = VT3122PLLGenerateBest(Clock, 2, 11, 21, &Diff);
    else
        PLL = VT3122PLLGenerateBest(Clock, 4, 8, 19, &Diff);
    
    ViaDebug(pScrn->scrnIndex, "%s: PLL: 0x%04X (%d off from %d)\n",
             __func__, PLL, Diff, Clock);
    return PLL;
}

/*
 * Set the primary CRTC dotclock to act as a slave.
 */
static void
ViaDotclockPrimarySlave(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);

    if ((pVia->Chipset == VT3122) && VT3122_REV_IS_AX(pVia->ChipRev))
        ViaSetPrimaryDotclock(pScrn, 0x471C); /* VT3122Ax use 2x XCLK */
    else
        ViaSetPrimaryDotclock(pScrn, 0x871C);
    ViaSetUseExternalClock(pScrn);
}

/*
 *
 * Output Handling.
 *
 */


/*
 *
 */
static struct ViaOutput *
ViaOutputDestroy(struct ViaOutput *Output)
{
    struct ViaOutput *Next;

    Next = Output->Next;

    if (Output->PrivateDestroy)
        Output->PrivateDestroy(Output);
    
    if (Output->I2CDev) 
        xf86DestroyI2CDevRec(Output->I2CDev, TRUE);
    xfree(Output->Options);
    
    xfree(Output);

    return Next;
}

/*
 *
 */
void
ViaOutputsDestroy(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output)
        Output = ViaOutputDestroy(Output);
}

/*
 *
 */
static Bool
ViaOutputAdd(ScrnInfoPtr pScrn, struct ViaOutput *Output)
{
    struct ViaOutput *Last = NULL;

    /* Is this Output sound? */
    if (!Output->Name || !Output->ModeValid || !Output->Mode ||
        !Output->Power) {
        
        if (!Output->Name)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 
                       "%s: Nameless output device.\n", __func__);
        
        if (!Output->ModeValid)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: %s: ModeValid "
                       "callback missing\n", __func__, Output->Name);
        
        if (!Output->Mode)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: %s: Mode "
                       "callback missing\n", __func__, Output->Name);
        
        if (!Output->Power)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: %s: Power "
                       "callback missing\n", __func__, Output->Name);
        
        return FALSE;
    }
    
    /* add Output to list */
    Last = VIAPTR(pScrn)->Outputs;
    if (Last) {
        while (Last->Next)
            Last = Last->Next;
        
        Last->Next = Output;
        Output->Prev = Last;
    } else {
        VIAPTR(pScrn)->Outputs = Output;
        Output->Prev = NULL;
    }

    return TRUE;
}

#ifdef UNUSED
/*
 *
 */
static void
ViaOutputRemove(ScrnInfoPtr pScrn, struct ViaOutput *Output)
{
    if (!Output)
        return;

    /* remove from list */
    if (Output->Prev)
        Output->Prev->Next = Output->Next;
    else
        VIAPTR(pScrn)->Outputs = Output->Next;
    
    if (Output->Next)
        Output->Next->Prev = Output->Prev;

    ViaOutputDestroy(Output);
}
#endif

/*
 *
 * TV specific code.
 *
 */

static struct {
    CARD8 Address;
    char * Name;
    struct ViaOutput * (*Init) (ScrnInfoPtr pScrn, I2CDevPtr pDev);
} ViaI2CDevices[] = {
    {0x40, "VT162x", ViaVT162xInit},
    {0x42, "VT162x", ViaVT162xInit},
    {0xEA, "CH7xxx", ViaCH7xxxInit},
    {0xEC, "CH7xxx", ViaCH7xxxInit},
    {0x00, NULL, NULL}
};

/*
 *
 */
static void
ViaScanBus(ScrnInfoPtr pScrn, I2CBusPtr pI2CBus)
{
    struct ViaOutput *Output;
    int i;

    VIAFUNC(pScrn->scrnIndex);

    for (i = 0; ViaI2CDevices[i].Init; i++) {
        I2CDevPtr pDev = NULL;

        if (!xf86I2CProbeAddress(pI2CBus, ViaI2CDevices[i].Address))
            continue;

        pDev = xf86CreateI2CDevRec();
        
        pDev->DevName = ViaI2CDevices[i].Name;
        pDev->SlaveAddr = ViaI2CDevices[i].Address;
        pDev->pI2CBus = pI2CBus;
        
        if (!xf86I2CDevInit(pDev)) {
            xf86DestroyI2CDevRec(pDev, TRUE);
            continue;
        }

        Output = ViaI2CDevices[i].Init(pScrn, pDev);
        
        /* did we actually find anything? */
        if (!Output) {
            xf86DestroyI2CDevRec(pDev,TRUE);
        } else if (!ViaOutputAdd(pScrn, Output))
            while (Output) /* could be a whole list */
                Output = ViaOutputDestroy(Output);
    }
}

/*
 *
 */
static Bool
ViaOutputsInit(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaOutput *Output;

    VIAFUNC(pScrn->scrnIndex);

    Output = ViaPanelInit(pScrn, NULL);
    if (Output && !ViaOutputAdd(pScrn, Output))
        while (Output) /* could be a whole list */
            Output = ViaOutputDestroy(Output);

    if (pVia->pI2CBus2)
        ViaScanBus(pScrn, pVia->pI2CBus2);

    if (pVia->pI2CBus3)
        ViaScanBus(pScrn, pVia->pI2CBus3);

    if (!pVia->Outputs)
        return FALSE;
        
    Output = pVia->Outputs;

    while (Output) {
        /* check bus position */
        switch (Output->Type) {
        case OUTPUT_TV:
            if (Output->I2CDev->pI2CBus == pVia->pI2CBus2)
                Output->Position = OUTPUT_BUS_DI0;
            else
                xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: Unhandled bus for TV"
                           " encoder.\n", __func__);
            break;
        case OUTPUT_PANEL:
            /* clean me up asap! */
            Output->Position = OUTPUT_BUS_DFP;
            break;
        default:
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: Unhandled output device"
                       " type.\n", __func__);
            break;
        }

        /* Save now */
        if (Output->Save)
            Output->Save(Output);

        /* Dump registers as early as possible */
        if (pVia->PrintTVRegs && Output->PrintRegs)
            Output->PrintRegs(Output, __func__);

        Output = Output->Next;
    }

    return TRUE;
}

/*
 * Save all output devices.
 */
void
ViaOutputsSave(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        if (Output->Save)
            Output->Save(Output);

        Output = Output->Next;
    }
}

/*
 * Restore all output devices.
 */
void
ViaOutputsRestore(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        if (Output->Restore)
            Output->Restore(Output);

        Output = Output->Next;
    }
}

/*
 * Check which output devices are active.
 */
static Bool
ViaOutputsSense(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;
    Bool Active = FALSE;

    while (Output) {
        if (Output->Sense) {
            Output->Active = Output->Sense(Output);
            if (Output->Active)
                Active = TRUE;
        } else {
            Output->Active = TRUE;
            Active = TRUE;
        }

        Output = Output->Next;
    }

    return Active;
}

/*
 * Power according to On all active devices, power off when inactive.
 */
void
ViaOutputsPower(ScrnInfoPtr pScrn, Bool On)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    ViaDebug(pScrn->scrnIndex, "%s: %s.\n", __func__, On ? "On" : "Off");

    while (Output) {
        if (Output->Power) {
            if (Output->Active)
                Output->Power(Output, On);
            else
                Output->Power(Output, FALSE);
        }

        Output = Output->Next;
    }
} 

/*
 *
 */
void
ViaOutputsPrintRegs(ScrnInfoPtr pScrn, const char *function)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        if (Output->PrintRegs)
            Output->PrintRegs(Output, function);

        Output = Output->Next;
    }
}

/*
 * Is the CRTC in control of the dotclock, or is the output device?
 */
static void
ViaOutputsFindDotclockMaster(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaOutput  *Output = pVia->Outputs;
   
    pVia->ClockSlave = FALSE;

    while (Output) {
        if (Output->Active && Output->ClockMaster) {
            if (pVia->ClockSlave)
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "%s: multiple Outputs "
                           "want to be the dotclock master.\n", __func__);
            pVia->ClockSlave = TRUE;
        }
        Output = Output->Next;
    }
}

/*
 *
 */
static ModeStatus
ViaOutputsModeValid(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        if (Output->Active && Output->ModeValid) {
            ModeStatus Status;
            Status = Output->ModeValid(Output, mode);
            
            if (Status != MODE_OK)
                return Status;
        }
        
        Output = Output->Next;
    }

    return MODE_OK;
}


/*
 * This function is all about handling the output bus surrounding the
 * actual setting of the mode of the output device.
 * 
 */
static void
ViaOutputMode(ScrnInfoPtr pScrn, struct ViaOutput *Output, DisplayModePtr Mode)
{
    VIAPtr pVia = VIAPTR(pScrn);
    vgaHWPtr hwp = VGAHWPTR(pScrn);

    VIAFUNC(pScrn->scrnIndex);

    if (!Output->Mode || !Mode)
        return;

    switch (Output->Position) {
    case OUTPUT_BUS_DI0:
        /* Quick 'n dirty workaround for non-primary case where a completely
         * idle bus fails to properly activate the tv encoder. */
        ViaDotclockPrimarySlave(pScrn);
        
        Output->Mode(Output, Mode);
        
        if (pVia->IsSecondary) {
            ViaCrtcMask(hwp, 0x6A, 0x80, 0x80);
            ViaCrtcMask(hwp, 0x6C, 0x80, 0x80);
            
            if ((pVia->Chipset == VT3122) && VT3122_REV_IS_AX(pVia->ChipRev)) {
                ViaCrtcMask(hwp, 0x6B, 0x20, 0x20);
                ViaCrtcMask(hwp, 0x6C, 0x10, 0x10);
            }
            
            /* Disable LCD Scaling */
            if (!pVia->SAMM || pVia->FirstInit)
                hwp->writeCrtc(hwp, 0x79, 0x00);
        } else {
            if ((pVia->Chipset == VT3122) && VT3122_REV_IS_AX(pVia->ChipRev))
                ViaCrtcMask(hwp, 0x6B, 0x80, 0x80);
        }
        ViaCrtcMask(hwp, 0x6A, 0x40, 0x40);
        ViaCrtcMask(hwp, 0x6B, 0x01, 0x01);
        ViaCrtcMask(hwp, 0x6C, 0x01, 0x01);
        ViaSeqMask(hwp, 0x1E, 0xC0, 0xC0); /* Enable DI0/DVP0 */
        break;
    case OUTPUT_BUS_DFP:
        /* If i ever get to clean up the panel crap... */
        Output->Mode(Output, Mode);  
        break;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: unhandled bus: %d\n",
                   __func__, Output->Position);
        break;
    }
}


/*
 * Print a list of active output devices.
 */
static void
ViaOutputsActivePrint(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        ViaDebug(pScrn->scrnIndex, "Output %s: %s.\n", Output->Name,
                 Output->Active ? "Active" : "Disabled");

        Output = Output->Next;
    }
}

/*
 *
 */
void
ViaOutputsDetect(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);

    VIAFUNC(pScrn->scrnIndex);

#if 0
    pVia->CrtPresent = FALSE;

    
    /* Crt */
    if (pVia->DDC1)
	pVia->CrtPresent = TRUE;
    /* If any of the unichromes support this, add CRT detection here */
    else
        pVia->CrtPresent = FALSE;
#endif
    pVia->CrtPresent = TRUE;

    ViaOutputsInit(pScrn);
}

/*
 *
 */
Bool
ViaOutputsSelect(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);

    if (pVia->IsSecondary) { /* we just abort for now */
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: Not handling secondary.\n",
                   __func__);
	return FALSE;
    }

    VIAFUNC(pScrn->scrnIndex);
    ViaDebug(pScrn->scrnIndex, "X Configuration: 0x%02x\n", pVia->ActiveDevice);

    pVia->CrtActive = FALSE;

    if (!pVia->ActiveDevice) {
        ViaOutputsSense(pScrn);

	/* CRT can be used with everything when present */
	if (pVia->CrtPresent)
	    pVia->CrtActive = TRUE;
    } else {
	if (pVia->ActiveDevice & VIA_DEVICE_LCD) {
            struct ViaOutput *Output = pVia->Outputs;
            Bool Found = FALSE;
            
            while (Output) {
                if (Output->Type & OUTPUT_PANEL) {
                    Found = TRUE;
                    break;
                }

                Output = Output->Next;
            }

            if (!Found)
		xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Unable to activate"
			   " panel: no panel is present.\n");
	}
        
	if (pVia->ActiveDevice & VIA_DEVICE_TV) {
            /* way too simplistic */
            struct ViaOutput *Output = pVia->Outputs;
            Bool Found = FALSE;
            
            while (Output) {
                if (Output->Type & OUTPUT_TV) {
                    if (Output->Sense) {
                        if (Output->Sense(Output))
                            Output->Active = TRUE;
                        else {
                            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                                       "Unable to activate TV encoder: "
                                       "no cable attached.\n");
                            
                            Output->Active = FALSE;
                        }
                    } else
                        Output->Active = TRUE;
                    Found = TRUE;
                }
                
                Output = Output->Next;
            }
            
            if (!Found)
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Unable to activate"
                           " TV encoder: no TV encoder present.\n");
        }
    
	if ((pVia->ActiveDevice & VIA_DEVICE_CRT))
	    pVia->CrtPresent = TRUE;
        pVia->CrtActive = TRUE;
    }
    
    /* Make sure that a panel is the only Active Output device */
    /* BAH! */
    {
        struct ViaOutput *Output = pVia->Outputs, *Panel = NULL;

        /* find panel */
        while (Output) {
            if (Output->Active && (Output->Type == OUTPUT_PANEL)) {
                Panel = Output;
                break;
            }

            Output = Output->Next;
        }

        /* disable everything else */
        if (Panel) {
            Output = pVia->Outputs;

            while (Output) {
                if ((Output != Panel) && Output->Active) {
                    xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Disabling %s: "
                               "Not compatible with %s.\n", Output->Name,
                               Panel->Name);
                    Output->Active = FALSE;
                }

                Output = Output->Next;
            }  
        }
    }

    ViaDebug(pScrn->scrnIndex, "Output CRT: %s.\n",
             pVia->CrtActive ? "Active" : "Disabled");

    ViaOutputsActivePrint(pScrn);

    ViaOutputsFindDotclockMaster(pScrn);

    return TRUE; /* Primary only always has at least CRT */
}


/*
 * Stolen from xf86Config.c's addDefaultModes
 */
static void
ViaModesAttachHelper(ScrnInfoPtr pScrn, MonPtr monitorp, DisplayModePtr Modes)
{
    DisplayModePtr mode;
    DisplayModePtr last = monitorp->Last;
    int i;

    for (i = 0; Modes[i].name; i++) {
	mode = xnfalloc(sizeof(DisplayModeRec));
	memcpy(mode, &Modes[i], sizeof(DisplayModeRec));
	mode->name = xnfstrdup(Modes[i].name);
	if (last) {
	    mode->prev = last;
	    last->next = mode;
	} else { /* this is the first mode */
	    monitorp->Modes = mode;
	    mode->prev = NULL;
	}
	last = mode;
    }
    monitorp->Last = last;
}

/*
 *
 */
void 
ViaOutputsModesAttach(ScrnInfoPtr pScrn, MonPtr monitorp)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    VIAFUNC(pScrn->scrnIndex);

    while (Output) {
        if (Output->Active && Output->Modes)
            ViaModesAttachHelper(pScrn, monitorp, Output->Modes);

        Output = Output->Next;
    }
}

/*
 * Checks for limitations imposed by the available VGA timing registers.
 *
 */
static ModeStatus
ViaModePrimaryVGAValid(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    VIAFUNC(pScrn->scrnIndex);

    if (mode->CrtcHTotal > 4100) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHTotal out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if (mode->CrtcHDisplay > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHDisplay out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if (mode->CrtcHBlankStart > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHBlankStart out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if ((mode->CrtcHBlankEnd - mode->CrtcHBlankStart) > 1025) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHBlankEnd out of range.\n");
	return MODE_HBLANK_WIDE;
    }

    if (mode->CrtcHSyncStart > 4095) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHSyncStart out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if ((mode->CrtcHSyncEnd - mode->CrtcHSyncStart) > 256) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHSyncEnd out of range.\n");
	return MODE_HSYNC_WIDE;
    }

    if (mode->CrtcVTotal > 2049) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVTotal out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if (mode->CrtcVDisplay > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVDisplay out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if (mode->CrtcVSyncStart > 2047) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVSyncStart out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if ((mode->CrtcVSyncEnd - mode->CrtcVSyncStart) > 16) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVSyncEnd out of range.\n");
	return MODE_VSYNC_WIDE;
    }

    if (mode->CrtcVBlankStart > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVBlankStart out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if ((mode->CrtcVBlankEnd - mode->CrtcVBlankStart) > 129) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVBlankEnd out of range.\n");
	return MODE_VBLANK_WIDE;
    }

    return MODE_OK;
}

/*
 *
 */
static ModeStatus
ViaModeSecondaryVGAValid(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    VIAFUNC(pScrn->scrnIndex);

    if (mode->CrtcHTotal > 4096) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHTotal out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if (mode->CrtcHDisplay > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHDisplay out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if (mode->CrtcHBlankStart > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHBlankStart out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if (mode->CrtcHBlankEnd > 4096) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHBlankEnd out of range.\n");
	return MODE_HBLANK_WIDE;
    }

    if (mode->CrtcHSyncStart > 2047) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHSyncStart out of range.\n");
	return MODE_BAD_HVALUE;
    }

    if ((mode->CrtcHSyncEnd - mode->CrtcHSyncStart) > 512) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcHSyncEnd out of range.\n");
	return MODE_HSYNC_WIDE;
    }

    if (mode->CrtcVTotal > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVTotal out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if (mode->CrtcVDisplay > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVDisplay out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if (mode->CrtcVBlankStart > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVBlankStart out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if (mode->CrtcVBlankEnd > 2048) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVBlankEnd out of range.\n");
	return MODE_VBLANK_WIDE;
    }

    if (mode->CrtcVSyncStart > 2047) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVSyncStart out of range.\n");
	return MODE_BAD_VVALUE;
    }

    if ((mode->CrtcVSyncEnd - mode->CrtcVSyncStart) > 32) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CrtcVSyncEnd out of range.\n");
	return MODE_VSYNC_WIDE;
    }

    return MODE_OK;
}

/*
 *
 */
ModeStatus 
ViaValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    VIAPtr pVia = VIAPTR(pScrn);
    ModeStatus ret;
    CARD32 temp;

    ViaDebug(scrnIndex, "%s: Validating %s (%d)\n", __func__, mode->name,
             mode->Clock);

    if (mode->Flags & V_INTERLACE)
	return MODE_NO_INTERLACE;
    
    if (pVia->IsSecondary)
	ret = ViaModeSecondaryVGAValid(pScrn, mode);
    else
	ret = ViaModePrimaryVGAValid(pScrn, mode);

    if (ret != MODE_OK)
	return ret;

    ret = ViaOutputsModeValid(pScrn, mode);
    if (ret != MODE_OK) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Mode \"%s\" not supported by"
                   " output devices.\n", mode->name);
        return ret;
    }

    temp = mode->CrtcHDisplay * mode->CrtcVDisplay *  mode->VRefresh *
	(pScrn->bitsPerPixel >> 3);
    if (pVia->Bandwidth < temp) {
	xf86DrvMsg(scrnIndex, X_INFO, "Required bandwidth is not available. (%u > %u)\n",
		   (unsigned) temp, (unsigned) pVia->Bandwidth);
	return MODE_CLOCK_HIGH; /* since there is no MODE_BANDWIDTH */
    }

    return MODE_OK;
}

/*
 *
 */
static void
ViaModePrimaryVGA(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    CARD16 temp;

    VIAFUNC(pScrn->scrnIndex);
    ViaDebug(pScrn->scrnIndex, "Setting up %s\n", mode->name);

    ViaCrtcMask(hwp, 0x11, 0x00, 0x80); /* modify starting address */
    ViaCrtcMask(hwp, 0x03, 0x80, 0x80); /* enable vertical retrace access */
    hwp->writeSeq(hwp, 0x10, 0x01); /* unlock extended registers */
    ViaCrtcMask(hwp, 0x47, 0x00, 0x01); /* unlock CRT registers */

    /* Set Misc Register */
    temp = 0x23;
    if (mode->Flags & V_NHSYNC)
	temp |= 0x40;
    if (mode->Flags & V_NVSYNC)
	temp |= 0x80;
    temp |= 0x0C; /* Undefined/external clock */
    hwp->writeMiscOut(hwp, temp);

    /* Sequence registers */
    hwp->writeSeq(hwp, 0x00, 0x00);

    /* if (mode->Flags & V_CLKDIV2)
	hwp->writeSeq(hwp, 0x01, 0x09);
    else */
    hwp->writeSeq(hwp, 0x01, 0x01);

    hwp->writeSeq(hwp, 0x02, 0x0F);
    hwp->writeSeq(hwp, 0x03, 0x00);
    hwp->writeSeq(hwp, 0x04, 0x0E);
    
    ViaSeqMask(hwp, 0x15, 0x02, 0x02);

    /* bpp */
    switch (pScrn->bitsPerPixel) {
    case 8:
	ViaSeqMask(hwp, 0x15, 0x20, 0xFC);
        break;
    case 16:
	ViaSeqMask(hwp, 0x15, 0xB4, 0xFC);
        break;
    case 24:
    case 32:
	ViaSeqMask(hwp, 0x15, 0xAC, 0xFC);
        break;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unhandled bitdepth: %d\n",
                   pScrn->bitsPerPixel);
        break;
    }

    ViaSeqMask(hwp, 0x16, 0x08, 0xBF);
    ViaSeqMask(hwp, 0x17, 0x1F, 0xFF);
    ViaSeqMask(hwp, 0x18, 0x4E, 0xFF);
    ViaSeqMask(hwp, 0x1A, 0x08, 0xFD);

    /* graphics registers */
    hwp->writeGr(hwp, 0x00, 0x00);
    hwp->writeGr(hwp, 0x01, 0x00);
    hwp->writeGr(hwp, 0x02, 0x00);
    hwp->writeGr(hwp, 0x03, 0x00);
    hwp->writeGr(hwp, 0x04, 0x00);
    hwp->writeGr(hwp, 0x05, 0x40);
    hwp->writeGr(hwp, 0x06, 0x05);
    hwp->writeGr(hwp, 0x07, 0x0F);
    hwp->writeGr(hwp, 0x08, 0xFF);

    ViaGrMask(hwp, 0x20, 0, 0xFF);
    ViaGrMask(hwp, 0x21, 0, 0xFF);
    ViaGrMask(hwp, 0x22, 0, 0xFF);
    
    /* attribute registers */
    hwp->writeAttr(hwp, 0x00, 0x00);
    hwp->writeAttr(hwp, 0x01, 0x01);
    hwp->writeAttr(hwp, 0x02, 0x02);
    hwp->writeAttr(hwp, 0x03, 0x03);
    hwp->writeAttr(hwp, 0x04, 0x04);
    hwp->writeAttr(hwp, 0x05, 0x05);
    hwp->writeAttr(hwp, 0x06, 0x06);
    hwp->writeAttr(hwp, 0x07, 0x07);
    hwp->writeAttr(hwp, 0x08, 0x08);
    hwp->writeAttr(hwp, 0x09, 0x09);
    hwp->writeAttr(hwp, 0x0A, 0x0A);
    hwp->writeAttr(hwp, 0x0B, 0x0B);
    hwp->writeAttr(hwp, 0x0C, 0x0C);
    hwp->writeAttr(hwp, 0x0D, 0x0D);
    hwp->writeAttr(hwp, 0x0E, 0x0E);
    hwp->writeAttr(hwp, 0x0F, 0x0F);
    hwp->writeAttr(hwp, 0x10, 0x41);
    hwp->writeAttr(hwp, 0x11, 0xFF);
    hwp->writeAttr(hwp, 0x12, 0x0F);
    hwp->writeAttr(hwp, 0x13, 0x00);
    hwp->writeAttr(hwp, 0x14, 0x00);

    /* Crtc registers */
    /* horizontal total : 4100 */
    ViaDebug(pScrn->scrnIndex, "CrtcHTotal: 0x%03X\n", mode->CrtcHTotal);
    temp = (mode->CrtcHTotal >> 3) - 5;
    hwp->writeCrtc(hwp, 0x00, temp & 0xFF);
    ViaCrtcMask(hwp, 0x36, temp >> 5, 0x08);
    
    /* horizontal address : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcHDisplay: 0x%03X\n", mode->CrtcHDisplay);
    temp = (mode->CrtcHDisplay >> 3) - 1;
    hwp->writeCrtc(hwp, 0x01, temp & 0xFF);

    /* horizontal blanking start : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcHBlankStart: 0x%03X\n", mode->CrtcHBlankStart);
    temp = (mode->CrtcHBlankStart >> 3) - 1;
    hwp->writeCrtc(hwp, 0x02, temp & 0xFF);

    /* horizontal blanking end : start + 1025 */
    ViaDebug(pScrn->scrnIndex, "CrtcHBlankEnd: 0x%03X\n", mode->CrtcHBlankEnd);
    temp = (mode->CrtcHBlankEnd >> 3) - 1;
    ViaCrtcMask(hwp, 0x03, temp, 0x1F);
    ViaCrtcMask(hwp, 0x05, temp << 2, 0x80);
    ViaCrtcMask(hwp, 0x33, temp >> 1, 0x20);

    /* CrtcHSkew ??? */

    /* horizontal sync start : 4095 */
    ViaDebug(pScrn->scrnIndex, "CrtcHSyncStart: 0x%03X\n", mode->CrtcHSyncStart);
    temp = mode->CrtcHSyncStart >> 3;
    hwp->writeCrtc(hwp, 0x04, temp & 0xFF);
    ViaCrtcMask(hwp, 0x33, temp >> 4, 0x10);

    /* horizontal sync end : start + 256 */
    ViaDebug(pScrn->scrnIndex, "CrtcHSyncEnd: 0x%03X\n",  mode->CrtcHSyncEnd);
    temp = mode->CrtcHSyncEnd >> 3;
    ViaCrtcMask(hwp, 0x05, temp, 0x1F);

    /* vertical total : 2049 */
    ViaDebug(pScrn->scrnIndex, "CrtcVTotal: 0x%03X\n", mode->CrtcVTotal);
    temp = mode->CrtcVTotal - 2;
    hwp->writeCrtc(hwp, 0x06, temp & 0xFF);
    ViaCrtcMask(hwp, 0x07, temp >> 8, 0x01);
    ViaCrtcMask(hwp, 0x07, temp >> 4, 0x20);
    ViaCrtcMask(hwp, 0x35, temp >> 10, 0x01);

    /* vertical address : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcVDisplay: 0x%03X\n", mode->CrtcVDisplay);
    temp = mode->CrtcVDisplay - 1;
    hwp->writeCrtc(hwp, 0x12, temp & 0xFF);
    ViaCrtcMask(hwp, 0x07, temp >> 7, 0x02);
    ViaCrtcMask(hwp, 0x07, temp >> 3, 0x40);
    ViaCrtcMask(hwp, 0x35, temp >> 8, 0x04);

    /* Primary starting address -> 0x00, adjustframe does the rest */
    hwp->writeCrtc(hwp, 0x0C, 0x00);
    hwp->writeCrtc(hwp, 0x0D, 0x00);
    hwp->writeCrtc(hwp, 0x34, 0x00);
    ViaCrtcMask(hwp, 0x48, 0x00, 0x03);

    /* vertical sync start : 2047 */
    ViaDebug(pScrn->scrnIndex, "CrtcVSyncStart: 0x%03X\n", mode->CrtcVSyncStart);
    temp = mode->CrtcVSyncStart;
    hwp->writeCrtc(hwp, 0x10, temp & 0xFF);
    ViaCrtcMask(hwp, 0x07, temp >> 6, 0x04);
    ViaCrtcMask(hwp, 0x07, temp >> 2, 0x80);
    ViaCrtcMask(hwp, 0x35, temp >> 9, 0x02);

    /* vertical sync end : start + 16 -- other bits someplace? */
    ViaDebug(pScrn->scrnIndex, "CrtcVSyncEnd: 0x%03X\n", mode->CrtcVSyncEnd);
    ViaCrtcMask(hwp, 0x11, mode->CrtcVSyncEnd, 0x0F);

    /* line compare: We are not doing splitscreen so 0x3FFF */
    hwp->writeCrtc(hwp, 0x18, 0xFF);
    ViaCrtcMask(hwp, 0x07, 0x10, 0x10);
    ViaCrtcMask(hwp, 0x09, 0x40, 0x40);
    ViaCrtcMask(hwp, 0x33, 0x07, 0x06);
    ViaCrtcMask(hwp, 0x35, 0x10, 0x10);

    /* zero Maximum scan line */
    ViaCrtcMask(hwp, 0x09, 0x00, 0x1F);
    hwp->writeCrtc(hwp, 0x14, 0x00);

    /* vertical blanking start : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcVBlankStart: 0x%03X\n", mode->CrtcVBlankStart);
    temp = mode->CrtcVBlankStart - 1;
    hwp->writeCrtc(hwp, 0x15, temp & 0xFF);
    ViaCrtcMask(hwp, 0x07, temp >> 5, 0x08);
    ViaCrtcMask(hwp, 0x09, temp >> 4, 0x20);
    ViaCrtcMask(hwp, 0x35, temp >> 7, 0x08);

    /* vertical blanking end : start + 129 */
    ViaDebug(pScrn->scrnIndex, "CrtcVBlankEnd: 0x%03X\n", mode->CrtcVBlankEnd);
    temp = mode->CrtcVBlankEnd - 1;
    hwp->writeCrtc(hwp, 0x16, temp & 0x7F);

    /* some leftovers */
    hwp->writeCrtc(hwp, 0x08, 0x00);
    ViaCrtcMask(hwp, 0x32, 0, 0xFF); /* ? */
    ViaCrtcMask(hwp, 0x33, 0, 0xC8);
    
    /* offset */
    temp = (pScrn->displayWidth * (pScrn->bitsPerPixel >> 3)) >> 3;
    /* Make sure that this is 32byte aligned */
    if (temp & 0x03) {
	temp += 0x03;
	temp &= ~0x03;
    }
    ViaDebug(pScrn->scrnIndex, "Offset: 0x%03X\n", temp);
    hwp->writeCrtc(hwp, 0x13, temp & 0xFF);
    ViaCrtcMask(hwp, 0x35, temp >> 3, 0xE0);

    /* fetch count */
    temp = (mode->CrtcHDisplay * (pScrn->bitsPerPixel >> 3)) >> 3;
    /* Make sure that this is 32byte aligned */
    if (temp & 0x03) {
	temp += 0x03;
	temp &= ~0x03;
    }
    ViaDebug(pScrn->scrnIndex, "Fetch Count: 0x%03X\n", temp);
    hwp->writeSeq(hwp, 0x1C, (temp >> 1) & 0xFF);
    ViaSeqMask(hwp, 0x1D, temp >> 9, 0x03);

    /* some leftovers */
    ViaCrtcMask(hwp, 0x32, 0, 0xFF);
    ViaCrtcMask(hwp, 0x33, 0, 0xC8);
}


/*
 *
 */
void
ViaModePrimary(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaOutput *Output = pVia->Outputs;

    VIAFUNC(pScrn->scrnIndex);

    ViaCrtcMask(hwp, 0x17, 0x00, 0x80);

    /* Clean Second Path Status */
    hwp->writeCrtc(hwp, 0x6A, 0x00);
    hwp->writeCrtc(hwp, 0x6B, 0x00);
    hwp->writeCrtc(hwp, 0x6C, 0x00);
    hwp->writeCrtc(hwp, 0x93, 0x00);

    ViaModePrimaryVGA(pScrn, mode);

    /* Don't do this before the Sequencer is set: locks up VT7205 and VT3108 */
    if (pVia->FirstInit)
	memset(pVia->FBBase, 0x00, pVia->videoRambytes);

    /* Enable MMIO & PCI burst (1 wait state) */
    ViaSeqMask(hwp, 0x1A, 0x06, 0x06);

    ViaSetPrimaryFIFO(pScrn, mode);

    if (!pVia->CrtActive)
	ViaCrtcMask(hwp, 0x36, 0x30, 0x30);

    /* Work around panel blob. */
    pVia->PanelClock = 0x00;

    while (Output) {
        if (Output->Active)
            ViaOutputMode(pScrn, Output, mode);
        else {
            /* add bus handling code here */
            if (Output->Power)
                Output->Power(Output, FALSE);
        }

        Output = Output->Next;
    }

    if (pVia->ClockSlave)
        ViaDotclockPrimarySlave(pScrn);
    else {
        if (pVia->PanelClock)
            ViaSetPrimaryDotclock(pScrn, pVia->PanelClock);
        else
            ViaSetPrimaryDotclock(pScrn, VT3122PLLGenerate(pScrn, mode->Clock));

	ViaSetUseExternalClock(pScrn);
	ViaCrtcMask(hwp, 0x6B, 0x00, 0x01);
    }

    ViaCrtcMask(hwp, 0x17, 0x80, 0x80);
}

/*
 *
 */
static void
ViaModeSecondaryVGA(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    CARD16 temp;

    VIAFUNC(pScrn->scrnIndex);

    /* bpp */
    switch (pScrn->bitsPerPixel) {
    case 8:
        ViaCrtcMask(hwp, 0x67, 0x00, 0xC0);
        break;
    case 16:
        ViaCrtcMask(hwp, 0x67, 0x40, 0xC0);
        break;
    case 24:
    case 32:
        ViaCrtcMask(hwp, 0x67, 0x80, 0xC0);
        break;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unhandled bitdepth: %d\n",
                   pScrn->bitsPerPixel);
        break;
    }

    /* Crtc registers */
    /* horizontal total : 4096 */
    ViaDebug(pScrn->scrnIndex, "CrtcHTotal: 0x%03X\n", mode->CrtcHTotal);
    temp = mode->CrtcHTotal - 1;
    hwp->writeCrtc(hwp, 0x50, temp & 0xFF);
    ViaCrtcMask(hwp, 0x55, temp >> 8, 0x0F);

    /* horizontal address : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcHDisplay: 0x%03X\n", mode->CrtcHDisplay);
    temp = mode->CrtcHDisplay - 1;
    hwp->writeCrtc(hwp, 0x51, temp & 0xFF);
    ViaCrtcMask(hwp, 0x55, temp >> 4, 0x70);

    /* horizontal blanking start : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcHBlankStart: 0x%03X\n", mode->CrtcHBlankStart);
    if (mode->CrtcHBlankStart != mode->CrtcHDisplay) /* FIX ME */
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Caught X working around an old VGA "
		   "limitation (HBlankStart).\n");
    temp = mode->CrtcHDisplay - 1;
    hwp->writeCrtc(hwp, 0x52, temp & 0xFF);
    ViaCrtcMask(hwp, 0x54, temp >> 8, 0x07);

    /* horizontal blanking end : 4096 */
    ViaDebug(pScrn->scrnIndex, "CrtcHBlankEnd: 0x%03X\n", mode->CrtcHBlankEnd);
    if (mode->CrtcHBlankEnd != mode->CrtcHTotal) /* FIX ME */
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Caught X working around an old VGA "
		   "limitation (HBlankEnd).\n");
    temp = mode->CrtcHTotal - 1;
    hwp->writeCrtc(hwp, 0x53, temp & 0xFF);
    ViaCrtcMask(hwp, 0x54, temp >> 5, 0x38);
    ViaCrtcMask(hwp, 0x5D, temp >> 5, 0x40);

    /* horizontal sync start : 2047 */
    ViaDebug(pScrn->scrnIndex, "CrtcHSyncStart: 0x%03X\n", mode->CrtcHSyncStart);
    temp = mode->CrtcHSyncStart;
    hwp->writeCrtc(hwp, 0x56, temp & 0xFF);
    ViaCrtcMask(hwp, 0x54, temp >> 2, 0xC0);
    ViaCrtcMask(hwp, 0x5C, temp >> 3, 0x80);

    /* horizontal sync end : sync start + 512 */
    ViaDebug(pScrn->scrnIndex, "CrtcHSyncEnd: 0x%03X\n", mode->CrtcHSyncEnd);
    temp = mode->CrtcHSyncEnd;
    hwp->writeCrtc(hwp, 0x57, temp & 0xFF);
    ViaCrtcMask(hwp, 0x5C, temp >> 2, 0x40);
    
    /* vertical total : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcVTotal: 0x%03X\n", mode->CrtcVTotal);
    temp = mode->CrtcVTotal - 1;
    hwp->writeCrtc(hwp, 0x58, temp & 0xFF);
    ViaCrtcMask(hwp, 0x5D, temp >> 8, 0x07);

    /* vertical address : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcVDisplay: 0x%03X\n", mode->CrtcVDisplay);
    temp = mode->CrtcVDisplay - 1;
    hwp->writeCrtc(hwp, 0x59, temp & 0xFF);
    ViaCrtcMask(hwp, 0x5D, temp >> 5, 0x38);

    /* vertical blanking start : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcVBlankStart: 0x%03X\n", mode->CrtcVBlankStart);
    if (mode->CrtcVBlankStart != mode->CrtcVDisplay) /* FIX ME */
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Caught X working around an old VGA "
		   "limitation (VBlankStart).\n");
    temp = mode->CrtcVDisplay - 1;
    hwp->writeCrtc(hwp, 0x5A, temp & 0xFF);
    ViaCrtcMask(hwp, 0x5C, temp >> 8, 0x07);

    /* vertical blanking end : 2048 */
    ViaDebug(pScrn->scrnIndex, "CrtcVBlankEnd: 0x%03X\n", mode->CrtcVBlankEnd);
    if (mode->CrtcVBlankEnd != mode->CrtcVTotal) /* FIX ME */
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Caught X working around an old VGA "
		   "limitation (VBlankEnd).\n");
    temp = mode->CrtcVTotal - 1;
    hwp->writeCrtc(hwp, 0x5B, temp & 0xFF);
    ViaCrtcMask(hwp, 0x5C, temp >> 5, 0x38);

    /* vertical sync start : 2047 */
    ViaDebug(pScrn->scrnIndex, "CrtcVSyncStart: 0x%03X\n", mode->CrtcVSyncStart);
    temp = mode->CrtcVSyncStart;
    hwp->writeCrtc(hwp, 0x5E, temp & 0xFF);
    ViaCrtcMask(hwp, 0x5F, temp >> 3, 0xE0);

    /* vertical sync end : start + 32 */
    ViaDebug(pScrn->scrnIndex, "CrtcVSyncEnd: 0x%03X\n", mode->CrtcVSyncEnd);
    temp = mode->CrtcVSyncEnd;
    ViaCrtcMask(hwp, 0x5F, temp, 0x1F);

    /* offset */
    temp = (pScrn->displayWidth * (pScrn->bitsPerPixel >> 3)) >> 3;
    if (temp & 0x03) { /* Make sure that this is 32byte aligned */
	temp += 0x03;
	temp &= ~0x03;
    }
    ViaDebug(pScrn->scrnIndex, "Offset: 0x%03X\n", temp);
    hwp->writeCrtc(hwp, 0x66, temp & 0xFF);
    ViaCrtcMask(hwp, 0x67, temp >> 8, 0x03);

    /* fetch count */
    temp = (mode->CrtcHDisplay * (pScrn->bitsPerPixel >> 3)) >> 3;
    /* Make sure that this is 32byte aligned */
    if (temp & 0x03) {
	temp += 0x03;
	temp &= ~0x03;
    }
    ViaDebug(pScrn->scrnIndex, "Fetch Count: 0x%03X\n", temp);
    hwp->writeCrtc(hwp, 0x65, (temp >> 1) & 0xFF);
    ViaCrtcMask(hwp, 0x67, temp >> 7, 0x0C);
}

/*
 *
 */
void
ViaModeSecondary(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaOutput *Output = pVia->Outputs;

    VIAFUNC(pScrn->scrnIndex);

    /* Turn off Screen */
    ViaCrtcMask(hwp, 0x17, 0x00, 0x80);

    ViaModeSecondaryVGA(pScrn, mode);
    ViaSetSecondaryFIFO(pScrn, mode);

    /* fix me */
    pVia->PanelClock = 0x00;
    while (Output) {
        if (Output->Active && Output->Mode)
            Output->Mode(Output, mode);
        else if (Output->Power)
            Output->Power(Output, FALSE);
        
        Output = Output->Next;
    }

    /* VT3122A2 apparently doesn't like this */
    if ((pVia->Chipset != VT3122) || (pVia->ChipRev != 0x02))
	ViaCrtcMask(hwp, 0x6C, 0x00, 0x1E);

    if (pVia->PanelClock)
        ViaSetSecondaryDotclock(pScrn, pVia->PanelClock);
    else
        ViaSetSecondaryDotclock(pScrn, VT3122PLLGenerate(pScrn, mode->Clock));
    ViaSetUseExternalClock(pScrn);

    ViaCrtcMask(hwp, 0x17, 0x80, 0x80);
}