summaryrefslogtreecommitdiff
path: root/src/ffb_driver.c
blob: f6ba2ae685a6f7848ef8d9a9056a68296751411a (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
/*
 * Creator, Creator3D and Elite3D framebuffer driver.
 *
 * Copyright (C) 2000 Jakub Jelinek (jakub@redhat.com)
 *
 * 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, sublicense, 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * JAKUB JELINEK 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.
 */
/* $XFree86$ */

#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86_ansic.h"
#include "xf86Version.h"
#include "mipointer.h"
#include "mibstore.h"
#include "micmap.h"

#define PSZ 8
#include "cfb.h"
#undef PSZ
#include "cfb32.h"
#include "cfb8_32wid.h"

#include "xf86cmap.h"

#include "ffb.h"

static const OptionInfoRec * FFBAvailableOptions(int chipid, int busid);
static void	FFBIdentify(int flags);
static Bool	FFBProbe(DriverPtr drv, int flags);
static Bool	FFBPreInit(ScrnInfoPtr pScrn, int flags);
static Bool	FFBScreenInit(int Index, ScreenPtr pScreen, int argc,
			      char **argv);
static Bool	FFBEnterVT(int scrnIndex, int flags);
static void	FFBLeaveVT(int scrnIndex, int flags);
static Bool	FFBCloseScreen(int scrnIndex, ScreenPtr pScreen);
static Bool	FFBSaveScreen(ScreenPtr pScreen, int mode);

/* Required if the driver supports mode switching */
static Bool	FFBSwitchMode(int scrnIndex, DisplayModePtr mode, int flags);
/* Required if the driver supports moving the viewport */
static void	FFBAdjustFrame(int scrnIndex, int x, int y, int flags);

/* Optional functions */
static void	FFBFreeScreen(int scrnIndex, int flags);
static ModeStatus FFBValidMode(int scrnIndex, DisplayModePtr mode,
			       Bool verbose, int flags);
static void     FFBDPMSMode(ScrnInfoPtr pScrn, int DPMSMode, int flags);
/* ffb_dga.c */
extern void FFB_InitDGA(ScreenPtr pScreen);

void FFBSync(ScrnInfoPtr pScrn);

#define VERSION 4000
#define FFB_NAME "SUNFFB"
#define FFB_DRIVER_NAME "sunffb"
#define FFB_MAJOR_VERSION 1
#define FFB_MINOR_VERSION 0
#define FFB_PATCHLEVEL 0

/* 
 * This contains the functions needed by the server after loading the driver
 * module.  It must be supplied, and gets passed back by the SetupProc
 * function in the dynamic case.  In the static case, a reference to this
 * is compiled in, and this requires that the name of this DriverRec be
 * an upper-case version of the driver name.
 */

DriverRec SUNFFB = {
    VERSION,
    FFB_DRIVER_NAME,
    FFBIdentify,
    FFBProbe,
    FFBAvailableOptions,
    NULL,
    0
};

typedef enum {
    OPTION_SW_CURSOR,
    OPTION_HW_CURSOR,
    OPTION_NOACCEL
} FFBOpts;

static const OptionInfoRec FFBOptions[] = {
    { OPTION_SW_CURSOR,		"SWcursor",	OPTV_BOOLEAN,	{0}, FALSE },
    { OPTION_HW_CURSOR,		"HWcursor",	OPTV_BOOLEAN,	{0}, FALSE },
    { OPTION_NOACCEL,		"NoAccel",	OPTV_BOOLEAN,	{0}, FALSE },
    { -1,			NULL,		OPTV_NONE,	{0}, FALSE }
};

#ifdef XFree86LOADER

static MODULESETUPPROTO(ffbSetup);

static XF86ModuleVersionInfo sunffbVersRec =
{
	"sunffb",
	MODULEVENDORSTRING,
	MODINFOSTRING1,
	MODINFOSTRING2,
	XF86_VERSION_CURRENT,
	FFB_MAJOR_VERSION, FFB_MINOR_VERSION, FFB_PATCHLEVEL,
	ABI_CLASS_VIDEODRV,
	ABI_VIDEODRV_VERSION,
	MOD_CLASS_VIDEODRV,
	{0,0,0,0}
};

XF86ModuleData sunffbModuleData = { &sunffbVersRec, ffbSetup, NULL };

pointer
ffbSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
    static Bool setupDone = FALSE;

    if (!setupDone) {
	setupDone = TRUE;
	xf86AddDriver(&SUNFFB, module, 0);

	/*
	 * Modules that this driver always requires can be loaded here
	 * by calling LoadSubModule().
	 */

	/*
	 * The return value must be non-NULL on success even though there
	 * is no TearDownProc.
	 */
	return (pointer)TRUE;
    } else {
	if (errmaj) *errmaj = LDR_ONCEONLY;
	return NULL;
    }
}

#endif /* XFree86LOADER */

static Bool
FFBGetRec(ScrnInfoPtr pScrn)
{
    /*
     * Allocate an FFBRec, and hook it into pScrn->driverPrivate.
     * pScrn->driverPrivate is initialised to NULL, so we can check if
     * the allocation has already been done.
     */
    if (pScrn->driverPrivate != NULL)
	return TRUE;

    pScrn->driverPrivate = xnfcalloc(sizeof(FFBRec), 1);
    return TRUE;
}

static void
FFBFreeRec(ScrnInfoPtr pScrn)
{
    FFBPtr pFfb;

    if (pScrn->driverPrivate == NULL)
	return;

    pFfb = GET_FFB_FROM_SCRN(pScrn);

    xfree(pScrn->driverPrivate);
    pScrn->driverPrivate = NULL;

    return;
}

static const OptionInfoRec *
FFBAvailableOptions(int chipid, int busid)
{
    return FFBOptions;
}

/* Mandatory */
static void
FFBIdentify(int flags)
{
    xf86Msg(X_INFO, "%s: driver for Creator, Creator 3D and Elite 3D\n", FFB_NAME);
}


/* Mandatory */
static Bool
FFBProbe(DriverPtr drv, int flags)
{
    int i;
    GDevPtr *devSections;
    int *usedChips;
    int numDevSections;
    int numUsed;
    Bool foundScreen = FALSE;
    EntityInfoPtr pEnt;

    /*
     * The aim here is to find all cards that this driver can handle,
     * and for the ones not already claimed by another driver, claim the
     * slot, and allocate a ScrnInfoRec.
     *
     * This should be a minimal probe, and it should under no circumstances
     * change the state of the hardware.  Because a device is found, don't
     * assume that it will be used.  Don't do any initialisations other than
     * the required ScrnInfoRec initialisations.  Don't allocate any new
     * data structures.
     */

    /*
     * Next we check, if there has been a chipset override in the config file.
     * For this we must find out if there is an active device section which
     * is relevant, i.e., which has no driver specified or has THIS driver
     * specified.
     */

    if ((numDevSections = xf86MatchDevice(FFB_DRIVER_NAME,
					  &devSections)) <= 0) {
	/*
	 * There's no matching device section in the config file, so quit
	 * now.
	 */
	return FALSE;
    }

    /*
     * We need to probe the hardware first.  We then need to see how this
     * fits in with what is given in the config file, and allow the config
     * file info to override any contradictions.
     */

    numUsed = xf86MatchSbusInstances(FFB_NAME, SBUS_DEVICE_FFB,
		   devSections, numDevSections,
		   drv, &usedChips);
				    
    xfree(devSections);
    if (numUsed <= 0)
	return FALSE;

    if (flags & PROBE_DETECT)
	foundScreen = TRUE;
    else for (i = 0; i < numUsed; i++) {
	pEnt = xf86GetEntityInfo(usedChips[i]);

	/*
	 * Check that nothing else has claimed the slots.
	 */
	if(pEnt->active) {
	    ScrnInfoPtr pScrn;
	    
	    /* Allocate a ScrnInfoRec and claim the slot */
	    pScrn = xf86AllocateScreen(drv, 0);

	    /* Fill in what we can of the ScrnInfoRec */
	    pScrn->driverVersion = VERSION;
	    pScrn->driverName	 = FFB_DRIVER_NAME;
	    pScrn->name		 = FFB_NAME;
	    pScrn->Probe	 = FFBProbe;
	    pScrn->PreInit	 = FFBPreInit;
	    pScrn->ScreenInit	 = FFBScreenInit;
  	    pScrn->SwitchMode	 = FFBSwitchMode;
  	    pScrn->AdjustFrame	 = FFBAdjustFrame;
	    pScrn->EnterVT	 = FFBEnterVT;
	    pScrn->LeaveVT	 = FFBLeaveVT;
	    pScrn->FreeScreen	 = FFBFreeScreen;
	    pScrn->ValidMode	 = FFBValidMode;
	    xf86AddEntityToScreen(pScrn, pEnt->index);
	    foundScreen = TRUE;
	}
	xfree(pEnt);
    }
    xfree(usedChips);
    return foundScreen;
}

/* Mandatory */
static Bool
FFBPreInit(ScrnInfoPtr pScrn, int flags)
{
    FFBPtr pFfb;
    sbusDevicePtr psdp;
    MessageType from;
    int i;

    if (flags & PROBE_DETECT) return FALSE;

    /*
     * Note: This function is only called once at server startup, and
     * not at the start of each server generation.  This means that
     * only things that are persistent across server generations can
     * be initialised here.  xf86Screens[] is (pScrn is a pointer to one
     * of these).  Privates allocated using xf86AllocateScrnInfoPrivateIndex()  
     * are too, and should be used for data that must persist across
     * server generations.
     *
     * Per-generation data should be allocated with
     * AllocateScreenPrivateIndex() from the ScreenInit() function.
     */

    /* Allocate the FFBRec driverPrivate */
    if (!FFBGetRec(pScrn))
	return FALSE;

    pFfb = GET_FFB_FROM_SCRN(pScrn);
    
    /* Set pScrn->monitor */
    pScrn->monitor = pScrn->confScreen->monitor;

    /* This driver doesn't expect more than one entity per screen */
    if (pScrn->numEntities > 1)
	return FALSE;
    /* This is the general case */
    for (i = 0; i < pScrn->numEntities; i++) {
	EntityInfoPtr pEnt = xf86GetEntityInfo(pScrn->entityList[i]);

	/* FFB is purely UPA (but we handle it as SBUS) */
	if (pEnt->location.type == BUS_SBUS) {
	    psdp = xf86GetSbusInfoForEntity(pEnt->index);
	    pFfb->psdp = psdp;
	} else
	    return FALSE;
    }

    /*********************
    deal with depth
    *********************/
    
    if (!xf86SetDepthBpp(pScrn, 24, 0, 32, Support32bppFb)) {
	return FALSE;
    } else {
	/* Check that the returned depth is one we support */
	switch (pScrn->depth) {
	case 24:
	    /* OK */
	    break;
	default:
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		       "Given depth (%d) is not supported by this driver\n",
		       pScrn->depth);
	    return FALSE;
	}
    }

    /* Collect all of the relevant option flags (fill in pScrn->options) */
    xf86CollectOptions(pScrn, NULL);
    /* Process the options */
    if (!(pFfb->Options = xalloc(sizeof(FFBOptions))))
	return FALSE;
    memcpy(pFfb->Options, FFBOptions, sizeof(FFBOptions));
    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pFfb->Options);
    
    /*
     * This must happen after pScrn->display has been set because
     * xf86SetWeight references it.
     */
    if (pScrn->depth > 8) {
	rgb weight = {8, 8, 8};
	rgb mask = {0xff, 0xff00, 0xff0000};

	if (!xf86SetWeight(pScrn, weight, mask)) {
	    return FALSE;
	}
    }

    if (!xf86SetDefaultVisual(pScrn, -1))
	return FALSE;

    /*
     * The new cmap code requires this to be initialised.
     */

    {
	Gamma zeros = {0.0, 0.0, 0.0};

	if (!xf86SetGamma(pScrn, zeros)) {
	    return FALSE;
	}
    }

    /* Set the bits per RGB for 8bpp mode */
    from = X_DEFAULT;

    /* determine whether we use hardware or software cursor */
    
    pFfb->HWCursor = TRUE;
    if (xf86GetOptValBool(pFfb->Options, OPTION_HW_CURSOR, &pFfb->HWCursor))
	from = X_CONFIG;
    if (xf86ReturnOptValBool(pFfb->Options, OPTION_SW_CURSOR, FALSE)) {
	from = X_CONFIG;
	pFfb->HWCursor = FALSE;
    }
    
    xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
		pFfb->HWCursor ? "HW" : "SW");

    if (xf86ReturnOptValBool(pFfb->Options, OPTION_NOACCEL, FALSE)) {
	pFfb->NoAccel = TRUE;
	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Acceleration disabled\n");
    }
        
    if (xf86LoadSubModule(pScrn, "xf8_32wid") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }

    if (xf86LoadSubModule(pScrn, "cfb32") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }

    if (xf86LoadSubModule(pScrn, "cfb") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }

    if (pFfb->HWCursor && xf86LoadSubModule(pScrn, "ramdac") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }

    if (xf86LoadSubModule(pScrn, "dbe") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }

#if 0
/*#ifdef XF86DRI*/
/*
 * Loading this automatically isn't compatible
 * to the behavior of other drivers
 */
    if (xf86LoadSubModule(pScrn, "drm") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }

    if (xf86LoadSubModule(pScrn, "dri") == NULL) {
	FFBFreeRec(pScrn);
	return FALSE;
    }
#endif

    /*********************
    set up clock and mode stuff
    *********************/
    
    pScrn->progClock = TRUE;

    if(pScrn->display->virtualX || pScrn->display->virtualY) {
	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
		   "FFB does not support a virtual desktop\n");
	pScrn->display->virtualX = 0;
	pScrn->display->virtualY = 0;
    }

    xf86SbusUseBuiltinMode(pScrn, pFfb->psdp);
    pScrn->currentMode = pScrn->modes;
    pScrn->displayWidth = pScrn->virtualX;

    /* Set display resolution */
    xf86SetDpi(pScrn, 0, 0);

    return TRUE;
}

/* Determine the FFB/AFB board type.  We need this information even
 * if acceleration is disabled because the ramdac support layer needs
 * to know what kind of FFB/AFB this is.
 */
static void
FFBProbeBoardType(FFBPtr pFfb)
{
	ffb_fbcPtr ffb = pFfb->regs;
	volatile unsigned int *afb_fem;
	unsigned int val;

	afb_fem = ((volatile unsigned int *) ((char *)ffb + 0x1540));
	val = *afb_fem;
	val &= 0x7f;

	xf86Msg(X_INFO, "%s: ", pFfb->psdp->device);
	if (val == 0x3f || val == 0x07 || val == 0x01) {
		/* When firmware has not been loaded onto AFB we
		 * just assume it is an M6 board.
		 */
		if (val == 0x3f || val != 0x07) {
			pFfb->ffb_type = afb_m6;
			ErrorF("AFB: Detected Elite3D/M6.\n");
		} else {
			pFfb->ffb_type = afb_m3;
			ErrorF("AFB: Detected Elite3D/M3.\n");
		}

		/* These attributes are invariant on AFB. */
		pFfb->has_double_res = 0;
		pFfb->has_z_buffer = 1;
		pFfb->has_double_buffer = 1;
	} else {
		unsigned char sbits;

		/* Read the board strapping bits twice, because sometimes
		 * the strapping pins can get misrouted to the bus interface
		 * on the first attempt.  The second attempt will get the
		 * correct value.
		 */
		sbits = *((volatile unsigned char *)pFfb->strapping_bits);
		sbits = *((volatile unsigned char *)pFfb->strapping_bits);
		switch (sbits & 0x78) {
		case (0x0 << 5) | (0x0 << 3):
			pFfb->ffb_type = ffb1_prototype;
			ErrorF("Detected FFB1 pre-FCS prototype, ");
			break;
		case (0x0 << 5) | (0x1 << 3):
			pFfb->ffb_type = ffb1_standard;
			ErrorF("Detected FFB1, ");
			break;
		case (0x0 << 5) | (0x3 << 3):
			pFfb->ffb_type = ffb1_speedsort;
			ErrorF("Detected FFB1-SpeedSort, ");
			break;
		case (0x1 << 5) | (0x0 << 3):
			pFfb->ffb_type = ffb2_prototype;
			ErrorF("Detected FFB2/vertical pre-FCS prototype, ");
			break;
		case (0x1 << 5) | (0x1 << 3):
			pFfb->ffb_type = ffb2_vertical;
			ErrorF("Detected FFB2/vertical, ");
			break;
		case (0x1 << 5) | (0x2 << 3):
			pFfb->ffb_type = ffb2_vertical_plus;
			ErrorF("Detected FFB2+/vertical, ");
			break;
		case (0x2 << 5) | (0x0 << 3):
			pFfb->ffb_type = ffb2_horizontal;
			ErrorF("Detected FFB2/horizontal, ");
			break;
		case (0x2 << 5) | (0x2 << 3):
			pFfb->ffb_type = ffb2_horizontal;
			ErrorF("Detected FFB2+/horizontal, ");
			break;
		default:
			pFfb->ffb_type = ffb2_vertical;
			ErrorF("Unknown boardID[%08x], assuming FFB2, ", sbits);
			break;
		};

		if (sbits & (1 << 2)) {
			ErrorF("DoubleRES, ");
			pFfb->has_double_res = 1;
		} else {
			pFfb->has_double_res = 0;
		}
		if (sbits & (1 << 1)) {
			ErrorF("Z-buffer, ");
			pFfb->has_z_buffer = 1;
		} else {
			pFfb->has_z_buffer = 0;
		}
		if (sbits & (1 << 0)) {
			ErrorF("Double-buffered.\n");

			/* This state really means to the driver that the double
			 * buffers are available for hw accelerate Dbe.  When the
			 * FFB is in high-resolution mode, the buffers are combined
			 * into one single large framebuffer.  So in high-resolution
			 * hw accelerated double-buffering is not available.
			 */
			if ((ffb->fbcfg0 & FFB_FBCFG0_RES_MASK) != FFB_FBCFG0_RES_HIGH)
				pFfb->has_double_buffer = 1;
			else
				pFfb->has_double_buffer = 1;
		} else {
			ErrorF("Single-buffered.\n");
			pFfb->has_double_buffer = 0;
		}
	}
}

static unsigned long CreatorUnaccelGeneration = 0;
static int CreatorUnaccelWindowPrivateIndex;

#define CreatorUnaccelGetWid(w) \
	((w)->devPrivates[CreatorUnaccelWindowPrivateIndex].val)
#define CreatorUnaccelSetWid(w,wid) \
	(((w)->devPrivates[CreatorUnaccelWindowPrivateIndex].val) = (wid))

static unsigned int
CreatorWidGet(WindowPtr pWin)
{
	return CreatorUnaccelGetWid(pWin);
}

static Bool
CreatorWidAlloc(WindowPtr pWin)
{
	ScreenPtr pScreen = pWin->drawable.pScreen;
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);
	unsigned int wid;
	int i, visual, visclass;

	visual = wVisual(pWin);
	visclass = 0;
	for (i = 0; i < pScreen->numVisuals; i++) {
		if (pScreen->visuals[i].vid == visual) {
			visclass = pScreen->visuals[i].class;
			break;
		}
	}

	wid = FFBWidAlloc(pFfb, visclass, wColormap(pWin), TRUE);
	if (wid == (unsigned int) -1)
		return FALSE;

	CreatorUnaccelSetWid(pWin, wid);

	return TRUE;
}

static void
CreatorWidFree(WindowPtr pWin)
{
	ScrnInfoPtr pScrn = xf86Screens[pWin->drawable.pScreen->myNum];
	FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);
	unsigned int wid = CreatorUnaccelGetWid(pWin);

	FFBWidFree(pFfb, wid);
}

static cfb8_32WidOps CreatorUnaccelWidOps = {
	CreatorWidGet,
	CreatorWidAlloc,
	CreatorWidFree,
	NULL,
	NULL
};

static Bool
CreatorUnaccelWidInit(ScreenPtr pScreen)
{
	if (serverGeneration != CreatorUnaccelGeneration) {
		CreatorUnaccelWindowPrivateIndex =
			AllocateWindowPrivateIndex();
		if (CreatorUnaccelWindowPrivateIndex == -1)
			return FALSE;
	}

	return TRUE;
}

/* Mandatory */

/* This gets called at the start of each server generation */

static Bool
FFBScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
{
    ScrnInfoPtr pScrn;
    FFBPtr pFfb;
    int ret;
    unsigned int afb_fem;
    VisualPtr visual;

    /* Add in our Dbe hook. */
    if (!FFBDbePreInit(pScreen))
	return FALSE;

    /* 
     * First get the ScrnInfoRec
     */
    pScrn = xf86Screens[pScreen->myNum];

    pFfb = GET_FFB_FROM_SCRN(pScrn);

    /* Map the FFB framebuffer, for each view. */

    /* 24-bit RGB Dumb view */
    pFfb->fb = pFfb->dfb24 =
	xf86MapSbusMem (pFfb->psdp, FFB_DFB24_VOFF, 0x1000000);

    if (! pFfb->dfb24)
	return FALSE;

    /* 8-bit R Dumb view */
    pFfb->dfb8r =
	xf86MapSbusMem (pFfb->psdp, FFB_DFB8R_VOFF, 0x400000);

    if (! pFfb->dfb8r)
	return FALSE;

    /* 8-bit X Dumb view */
    pFfb->dfb8x =
	xf86MapSbusMem (pFfb->psdp, FFB_DFB8X_VOFF, 0x400000);

    if (! pFfb->dfb8x)
	return FALSE;

    /* 32-bit RGB Smart view */
    pFfb->sfb32 =
	xf86MapSbusMem (pFfb->psdp, FFB_SFB32_VOFF, 0x1000000);

    if (!pFfb->sfb32)
	return FALSE;

    /* 8-bit R Smart view */
    pFfb->sfb8r =
	xf86MapSbusMem(pFfb->psdp, FFB_SFB8R_VOFF, 0x400000);

    if (!pFfb->sfb8r)
	return FALSE;

    /* 8-bit X Smart view */
    pFfb->sfb8x =
	xf86MapSbusMem(pFfb->psdp, FFB_SFB8X_VOFF, 0x400000);

    if (!pFfb->sfb8x)
	return FALSE;

    /* Map the rendering pipeline */
    pFfb->regs =
	xf86MapSbusMem (pFfb->psdp, FFB_FBC_REGS_VOFF, 16384);

    if (! pFfb->regs)
	return FALSE;

    /* Map the ramdac */
    pFfb->dac =
	xf86MapSbusMem (pFfb->psdp, FFB_DAC_VOFF, 8192);

    if (! pFfb->dac)
	return FALSE;

    /* Map the board strapping bits */
    pFfb->strapping_bits = (volatile unsigned int *)
	    xf86MapSbusMem(pFfb->psdp, FFB_EXP_VOFF, 8192);

    if (! pFfb->strapping_bits)
	return FALSE;

    /* Probe for the type of FFB/AFB we have. */
    FFBProbeBoardType(pFfb);

    /* Now that we have the board type, we can init the ramdac layer. */
    if (FFBDacInit(pFfb) == FALSE)
	return FALSE;

    /* OK, a fun gross hack to detect if this is
     * AFB and if so whether the correct firmware
     * has been loaded.  The machine will flatline
     * if you try to use certain acceleration features
     * without the full firmware loaded.
     *
     * The bootup Elite3D/AFB firmware is minimal, and
     * will leave the FloatEnableMask register at a
     * value of 0x01.  Creator{,3D} lacks the FEM register
     * and will return a "nonsense" value on attempts to
     * read this location.  After experimentation, an
     * appropriate definition for "nonsense" seems to
     * be anything with all low 7 bits not 0x3f, 0x07,
     * of 0x01.
     *
     * If the FEM register is non-zero and is some value
     * other than 0x1 (usually 0x3f or 0x7 depending upon
     * whether the card has 3 or 6 floats) we can assume
     * the correct firmware has been loaded. -DaveM
     */
    afb_fem = *(unsigned int *)((char *)pFfb->regs + 0x1540);
    if ((afb_fem & 0x7f) != 0x3f &&
	(afb_fem & 0x7f) != 0x07 &&
	(afb_fem & 0x7f) != 0x01)
	xf86Msg(X_INFO, "%s: Detected Creator/Creator3D\n", pFfb->psdp->device);
    else {
	xf86Msg(X_INFO, "%s: Detected Elite3D M3/M6, checking firmware...\n", pFfb->psdp->device);
	if (afb_fem == 0x1) {
	    xf86Msg(X_INFO, "%s: ... AFB firmware not loaded\n", pFfb->psdp->device);
	    if (!pFfb->NoAccel) {
		xf86Msg(X_WARNING, "%s: Forcing no acceleration on Elite3D M3/M6\n", pFfb->psdp->device);
		pFfb->NoAccel = TRUE;
	    }
	} else
	    xf86Msg(X_INFO, "%s: ... AFB firmware is loaded\n", pFfb->psdp->device);
    }

    /* Darken the screen for aesthetic reasons and set the viewport */
    FFBSaveScreen(pScreen, SCREEN_SAVER_ON);

    if (pFfb->NoAccel == TRUE) {
	    if (!CreatorUnaccelWidInit(pScreen))
		    return FALSE;
    }

    /*
     * The next step is to setup the screen's visuals, and initialise the
     * framebuffer code.  In cases where the framebuffer's default
     * choices for things like visual layouts and bits per RGB are OK,
     * this may be as simple as calling the framebuffer's ScreenInit()
     * function.  If not, the visuals will need to be setup before calling
     * a fb ScreenInit() function and fixed up after.
     */

    /*
     * Reset visual list.
     */
    miClearVisualTypes();

    /* Setup the visuals we support. */
    if (!miSetVisualTypes(24, TrueColorMask | DirectColorMask,
			  pScrn->rgbBits, TrueColor))
	    return FALSE;
    if (!miSetVisualTypes(8, PseudoColorMask | GrayScaleMask | StaticGrayMask,
			  pScrn->rgbBits, PseudoColor))
	    return FALSE;

#ifdef XF86DRI
    if (pFfb->ffb_type != afb_m3 && pFfb->ffb_type != afb_m6 &&
	pFfb->NoAccel == FALSE) {
	    pFfb->dri_enabled = FFBDRIScreenInit(pScreen);
	    if (pFfb->dri_enabled == TRUE)
		    xf86Msg(X_INFO, "%s: DRM initialized\n",
			    pFfb->psdp->device);
	    else
		    xf86Msg(X_INFO, "%s: DRM setup failed\n",
			    pFfb->psdp->device);
    } else {
	    pFfb->dri_enabled = FALSE;
    }
#endif

    /*
     * Call the framebuffer layer's ScreenInit function, and fill in other
     * pScreen fields.
     */
    if (pFfb->NoAccel == TRUE) {
	    ret = cfb8_32WidScreenInit(pScreen, pFfb->dfb24, pFfb->dfb8r, pFfb->dfb8x,
				       pScrn->virtualX, pScrn->virtualY,
				       pScrn->xDpi, pScrn->yDpi,
				       2048, 2048, 2048, 8,
				       &CreatorUnaccelWidOps);
    } else {
	    /* Use smart framebuffer aperture for cfb8/cfb32. */
	    ret = cfb8_32WidScreenInit(pScreen, pFfb->sfb32, pFfb->sfb8r, pFfb->dfb8x,
				       pScrn->virtualX, pScrn->virtualY,
				       pScrn->xDpi, pScrn->yDpi,
				       2048, 2048, 2048, 8,
				       &CreatorUnaccelWidOps);
    }

    if (!ret)
	return FALSE;

    miInitializeBackingStore(pScreen);
    xf86SetBackingStore(pScreen);
    xf86SetSilkenMouse(pScreen);

    xf86SetBlackWhitePixels(pScreen);

    if (pScrn->bitsPerPixel > 8) {
        /* Fixup RGB ordering */
        visual = pScreen->visuals + pScreen->numVisuals;
        while (--visual >= pScreen->visuals) {
	    if ((visual->class | DynamicClass) == DirectColor) {
		visual->offsetRed = pScrn->offset.red;
		visual->offsetGreen = pScrn->offset.green;
		visual->offsetBlue = pScrn->offset.blue;
		visual->redMask = pScrn->mask.red;
		visual->greenMask = pScrn->mask.green;
		visual->blueMask = pScrn->mask.blue;
	    }
	}
    }

    if (!pFfb->NoAccel) {
	if (!FFBAccelInit(pScreen, pFfb))
	    return FALSE;
	xf86Msg(X_INFO, "%s: Using acceleration\n", pFfb->psdp->device);
    }

    /* Initialise cursor functions */
    miDCInitialize (pScreen, xf86GetPointerScreenFuncs());

    /* Initialize HW cursor layer. 
     * Must follow software cursor initialization.
     */
    if (pFfb->HWCursor) { 
	if(!FFBHWCursorInit(pScreen)) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 
		       "Hardware cursor initialization failed\n");
	    return(FALSE);
	}
	xf86SbusHideOsHwCursor(pFfb->psdp);
    }

    /* Initialise default colourmap. */
    if (!miCreateDefColormap(pScreen))
	return FALSE;

    /* Initialize colormap layer.
     * Must follow initialization of the default colormap.
     */
    if (!xf86HandleColormaps(pScreen, 256, 8,
			     FFBDacLoadPalette, NULL,
#if 0
			     CMAP_PALETTED_TRUECOLOR |
#endif
			     CMAP_LOAD_EVEN_IF_OFFSCREEN |
			     CMAP_RELOAD_ON_MODE_SWITCH))
	return FALSE;

    /* Setup DGA support. */
    if (!pFfb->NoAccel)
	    FFB_InitDGA(pScreen);

#ifdef XF86DRI
    if (pFfb->dri_enabled) {
	    /* Now that mi, cfb, drm and others have done their thing, 
	     * complete the DRI setup.
	     */
	    pFfb->dri_enabled = FFBDRIFinishScreenInit(pScreen);
	    if (pFfb->dri_enabled)
		    xf86Msg(X_INFO, "%s: DRM finish setup complete\n",
			    pFfb->psdp->device);
	    else
		    xf86Msg(X_INFO, "%s: DRM finish setup failed\n",
			    pFfb->psdp->device);
    }
#endif

    pFfb->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = FFBCloseScreen;
    pScreen->SaveScreen = FFBSaveScreen;

    (void) xf86DPMSInit(pScreen, FFBDPMSMode, 0);

    /* Report any unused options (only for the first generation) */
    if (serverGeneration == 1) {
	xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
    }

    /* unblank the screen */
    FFBSaveScreen(pScreen, SCREEN_SAVER_OFF);

    /* Done */
    return TRUE;
}


/* Usually mandatory */
static Bool
FFBSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
{
    return TRUE;
}


/*
 * This function is used to initialize the Start Address - the first
 * displayed location in the video memory.
 */
/* Usually mandatory */
static void 
FFBAdjustFrame(int scrnIndex, int x, int y, int flags)
{
    /* we don't support virtual desktops */
    return;
}

/*
 * This is called when VT switching back to the X server.  Its job is
 * to reinitialise the video mode.
 */

/* Mandatory */
static Bool
FFBEnterVT(int scrnIndex, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);

    pFfb->vtSema = FALSE;
    if (!pFfb->NoAccel)
	CreatorVtChange (pScrn->pScreen, TRUE);
    if (pFfb->HWCursor)
	xf86SbusHideOsHwCursor (pFfb->psdp);

    FFBDacEnterVT(pFfb);

    return TRUE;
}


/*
 * This is called when VT switching away from the X server.
 */

/* Mandatory */
static void
FFBLeaveVT(int scrnIndex, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);

    FFBDacLeaveVT(pFfb);

    if (!pFfb->NoAccel)
	CreatorVtChange (pScrn->pScreen, FALSE);

    if (pFfb->HWCursor)
	xf86SbusHideOsHwCursor (pFfb->psdp);

    pFfb->vtSema = TRUE;
    return;
}


/*
 * This is called at the end of each server generation.  It restores the
 * original (text) mode.  It should really also unmap the video memory too.
 */

/* Mandatory */
static Bool
FFBCloseScreen(int scrnIndex, ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);

#ifdef XF86DRI
    if (pFfb->dri_enabled)
	    FFBDRICloseScreen(pScreen);
#endif

    /* Restore kernel ramdac state before we unmap registers. */
    FFBDacFini(pFfb);

    pScrn->vtSema = FALSE;

    xf86UnmapSbusMem(pFfb->psdp, pFfb->dfb24, 0x1000000);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->dfb8r, 0x400000);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->dfb8x, 0x400000);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->sfb32, 0x1000000);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->sfb8r, 0x400000);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->sfb8x, 0x400000);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->regs, 16384);
    xf86UnmapSbusMem(pFfb->psdp, pFfb->dac, 8192);
    xf86UnmapSbusMem(pFfb->psdp, (void *)pFfb->strapping_bits, 8192);

    if (pFfb->HWCursor)
	xf86SbusHideOsHwCursor (pFfb->psdp);

    pScreen->CloseScreen = pFfb->CloseScreen;
    return (*pScreen->CloseScreen)(scrnIndex, pScreen);
}


/* Free up any per-generation data structures */

/* Optional */
static void
FFBFreeScreen(int scrnIndex, int flags)
{
    FFBFreeRec(xf86Screens[scrnIndex]);
}


/* Checks if a mode is suitable for the selected chipset. */

/* Optional */
static ModeStatus
FFBValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
{
    if (mode->Flags & V_INTERLACE)
	return(MODE_BAD);

    return(MODE_OK);
}

/* Do screen blanking */

/* Mandatory */
static Bool
FFBSaveScreen(ScreenPtr pScreen, int mode)
    /* This function blanks the screen when mode=SCREEN_SAVER_ON and
       unblanks it when mode=SCREEN_SAVER_OFF.  It is used internally in the
       FFBScreenInit code `for aesthetic reasons,' and it is used for
       blanking if you set "xset s on s blank."  The work (such as it is) is
       done in "ffb_dac.c" `for aesthetic reasons.'
    */
{
    return FFBDacSaveScreen(GET_FFB_FROM_SCREEN(pScreen), mode);
}

/*
 * This is the implementation of the Sync() function.
 */
void
FFBSync(ScrnInfoPtr pScrn)
{
    return;
}

/*
  Hook for DPMS Mode.
*/

static void
FFBDPMSMode(ScrnInfoPtr pScrn, int DPMSMode, int flags)
{
  FFBDacDPMSMode(GET_FFB_FROM_SCRN(pScrn), DPMSMode, flags);
}