summaryrefslogtreecommitdiff
path: root/driver/xf86-video-glint/src/pm3_dac.c
blob: 92633ea3c09816c9313c636ba2e5310c3e993d75 (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
/*
 * Copyright 2000,2001 by Sven Luther <luther@dpt-info.u-strasbg.fr>.
 *
 * 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 name of Sven Luther not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission. Sven Luther makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * SVEN LUTHER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL SVEN LUTHER 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.
 *
 * Authors: Sven Luther, <luther@dpt-info.u-strasbg.fr>
 *          Thomas Witzel, <twitzel@nmr.mgh.harvard.edu>
 *          Alan Hourihane, <alanh@fairlite.demon.co.uk>
 *
 * this work is sponsored by Appian Graphics.
 * 
 */
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/glint/pm3_dac.c,v 1.33tsi Exp $ */

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

#include "xf86.h"
#include "xf86_OSproc.h"

#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "xf86int10.h"

#include "glint_regs.h"
#include "pm3_regs.h"
#include "glint.h"

#define DEBUG 0

#if DEBUG
# define TRACE_ENTER(str)       ErrorF("glint: " str " %d\n",pScrn->scrnIndex)
# define TRACE_EXIT(str)        ErrorF("glint: " str " done\n")
# define TRACE(str)             ErrorF("glint trace: " str "\n")
#else
# define TRACE_ENTER(str)
# define TRACE_EXIT(str)
# define TRACE(str)
#endif

int
Permedia3MemorySizeDetect(ScrnInfoPtr pScrn)
{
    GLINTPtr pGlint = GLINTPTR (pScrn);
    CARD32 size = 0, temp, temp1, temp2, i;

    /* We can map 64MB, as that's the size of the Permedia3 aperture 
     * regardless of memory configuration */
    pGlint->FbMapSize = 64*1024*1024;

#ifndef XSERVER_LIBPCIACCESS    
    /* Mark as VIDMEM_MMIO to avoid write-combining while detecting memory */
    pGlint->FbBase = xf86MapPciMem(pScrn->scrnIndex, VIDMEM_MMIO,
			pGlint->PciTag, pGlint->FbAddress, pGlint->FbMapSize);
#else
    {
      void** result = (void**)&pGlint->FbBase;
      int err = pci_device_map_range(pGlint->PciInfo,
				     pGlint->FbAddress,
				     pGlint->FbMapSize,
				     PCI_DEV_MAP_FLAG_WRITABLE,
				     result);
      
      if (err) 
	return FALSE;
    }

#endif
    if (pGlint->FbBase == NULL) 
	return 0;

    temp = GLINT_READ_REG(PM3MemBypassWriteMask);
    GLINT_SLOW_WRITE_REG(0xffffffff, PM3MemBypassWriteMask);

    /* The Permedia3 splits up memory, and even replicates it. Grrr.
     * So that each 32MB appears at offset 0, and offset 32, unless
     * there's really 64MB attached to the chip.
     * So, 16MB appears at offset 0, nothing between 16-32, then it re-appears
     * at offset 32.
     * This below is to detect the cases of memory combinations
     */

    /* Test first 32MB */
    for(i=0;i<32;i++) {
    	/* write test pattern */
	MMIO_OUT32(pGlint->FbBase, i*1024*1024, i*0x00345678);
	mem_barrier();
	temp1 = MMIO_IN32(pGlint->FbBase, i*1024*1024);
    	/* Let's check for wrapover, write will fail at 16MB boundary */
	if (temp1 == (i*0x00345678)) 
	    size = i;
	else 
	    break;
    }

    /* Ok, we're satisfied we've got 32MB, let's test the second lot */
    if ((size + 1) == i) {
	for(i=0;i<32;i++) {
	    /* Clear first 32MB */
	    MMIO_OUT32(pGlint->FbBase, i*1024*1024, 0);
	    mem_barrier();
	}
        for(i=32;i<64;i++) {
    	    /* write test pattern */
	    MMIO_OUT32(pGlint->FbBase, i*1024*1024, i*0x00345678);
	    mem_barrier();
	    temp1 = MMIO_IN32(pGlint->FbBase, i*1024*1024);
	    temp2 = MMIO_IN32(pGlint->FbBase, (i-32)*1024*1024);
    	    /* Let's check for wrapover */
	    if ( (temp1 == (i*0x00345678)) && (temp2 == 0) )
	        size = i;
	    else 
	        break;
	}
    }

    GLINT_SLOW_WRITE_REG(temp, PM3MemBypassWriteMask);

#ifndef XSERVER_LIBPCIACCESS
    xf86UnMapVidMem(pScrn->scrnIndex, (pointer)pGlint->FbBase, 
							pGlint->FbMapSize);
#else
    pci_device_unmap_range(pGlint->PciInfo, pGlint->FbBase, pGlint->FbMapSize);
#endif

    pGlint->FbBase = NULL;
    pGlint->FbMapSize = 0;

    return ( (size+1) * 1024 );
}

static unsigned long
PM3DAC_CalculateClock
(
 unsigned long ReqClock,		/* In kHz units */
 unsigned long RefClock,		/* In kHz units */
 unsigned char *prescale,		/* ClkPreScale */
 unsigned char *feedback,		/* ClkFeedBackScale */
 unsigned char *postscale		/* ClkPostScale */
 )
{
  unsigned long fMinVCO = 2000000; /* min fVCO is 200MHz (in 100Hz units) */
  unsigned long fMaxVCO = 6220000; /* max fVCO is 622MHz (in 100Hz units) */
  unsigned long fMinINTREF = 10000;/* min fINTREF is 1MHz (in 100Hz units) */
  unsigned long fMaxINTREF = 20000;/* max fINTREF is 2MHz (in 100Hz units) */
  unsigned long	M, N, P; /* M=feedback, N=prescale, P=postscale */
  unsigned long	fINTREF;
  unsigned long	fVCO;
  unsigned long	ActualClock;
  long		Error;
  unsigned long LowestError = 1000000;
  unsigned int	bFoundFreq = FALSE;
  int		cInnerLoopIterations = 0;
  int		LoopCount;
  unsigned long	ClosestClock = 0;

  ReqClock*=10; /* convert into 100Hz units */
  RefClock*=10; /* convert into 100Hz units */

  for(P = 0; P <= 5; ++P)
    {
      unsigned long fVCOLowest, fVCOHighest;

      /* it is pointless going through the main loop if all values of
         N produce an fVCO outside the acceptable range */
      N = 1;
      M = (N * (1UL << P) * ReqClock) / (2 * RefClock);
      fVCOLowest = (2 * RefClock * M) / N;
      N = 255;
      M = (N * (1UL << P) * ReqClock) / (2 * RefClock);
      fVCOHighest = (2 * RefClock * M) / N;

      if(fVCOHighest < fMinVCO || fVCOLowest > fMaxVCO)
	{
	  continue;
	}

      for(N = 1; N <= 255; ++N, ++cInnerLoopIterations)
	{
	  fINTREF = RefClock / N;
	  if(fINTREF < fMinINTREF || fINTREF > fMaxINTREF)
	    {
	      if(fINTREF > fMaxINTREF)
		{
		  /* hopefully we will get into range as the prescale
		     value increases */
		  continue;
		}
	      else
		{
		  /* already below minimum and it will only get worse:
		     move to the next postscale value */
		  break;
		}
	    }

	  M = (N * (1UL << P) * ReqClock) / (2 * RefClock);
	  if(M > 255)
	    {
	      /* M, N & P registers are only 8 bits wide */
	      break;
	    }

	  /* we can expect rounding errors in calculating M, which
	     will always be rounded down. So we will checkout our
	     calculated value of M along with (M+1) */
	  for(LoopCount = (M == 255) ? 1 : 2; --LoopCount >= 0; ++M)
	    {
	      fVCO = (2 * RefClock * M) / N;
	      if(fVCO >= fMinVCO && fVCO <= fMaxVCO)
		{
		  ActualClock = fVCO / (1UL << P);
		  Error = ActualClock - ReqClock;
		  if(Error < 0)
		    Error = -Error;
		  if(Error < LowestError)
		    {
		      bFoundFreq = TRUE;
		      LowestError = Error;
		      ClosestClock = ActualClock;
		      *prescale = N;
		      *feedback = M;
		      *postscale = P;
		      if(Error == 0)
			goto Done;
		    }
		}
	    }
	}
    }

Done:
	
  if(bFoundFreq)
    ActualClock = ClosestClock;
  else
    ActualClock = 0;
	
#if 0
  ErrorF("PM3DAC_CalculateClock: Got prescale=%d, feedback=%d, postscale=%d, WantedClock = %d00 ActualClock = %d00 (Error %d00)\n",
	 *prescale, *feedback, *postscale, ReqClock, ActualClock, LowestError);
#endif

    return(ActualClock);
}

static unsigned long
PM4DAC_CalculateClock
(
 unsigned long req_clock,		/* In kHz units */
 unsigned long ref_clock,		/* In kHz units */
 unsigned char *param_m,		/* ClkPreScale */
 unsigned char *param_n, 		/* ClkFeedBackScale */
 unsigned char *param_p			/* ClkPostScale */
 )
{
#define INITIALFREQERR 10000

  long fMinVCO = 200000;	/* min fVCO is 200MHz (in 10000Hz units) */
  long fMaxVCO = 400000;	/* max fVCO is 400MHz (in 10000Hz units) */
  unsigned long int	M, N, P;
  unsigned long int	fVCO;
  unsigned long int	ActualClock;
  int		Error;
  int		LowestError = INITIALFREQERR;
  short		bFoundFreq = FALSE;
  int		cInnerLoopIterations = 0;
  int		LoopCount;

  /*
   * Actual Equations:
   *		fVCO = (ref_clock * M)/(N+1)
   *		PIXELCLOCK = fVCO/(1<<p)
   *		200 <= fVCO <= 400
   *		24 <= N <= 80
   *		1 <= M <= 15
   *		0 <= P <= 3
   *		1Mhz < ref_clock/(N+1) <= 2Mhz - not used
   * For refclk == 14.318 we have the tighter equations:
   *		32 <= N <= 80
   *		3 <= M <= 12
   * Notes:
   *		The spec says that the PLLs will only do 260Mhz, but I have assumed 300Mhz 'cos
   *		260Mhz is a crap limit.
   */

#define	P4RD_PLL_MIN_P	0
#define	P4RD_PLL_MAX_P	3
#define	P4RD_PLL_MIN_M	1
#define	P4RD_PLL_MAX_M	12
#define	P4RD_PLL_MIN_N	24
#define	P4RD_PLL_MAX_N	80

  for(P = P4RD_PLL_MIN_P; P <= P4RD_PLL_MAX_P; ++P) {
      unsigned long int fVCOLowest, fVCOHighest;

      /* it's pointless going through the main loop if all values of 
       * N produce an fVCO outside the acceptable range */ 

      M = P4RD_PLL_MIN_M;
      N = ((M + 1) * (1 << P) * req_clock) / ref_clock;

      fVCOLowest = (ref_clock * N) / (M + 1);

      M = P4RD_PLL_MAX_M;
      N = ((M + 1) * (1 << P) * req_clock) / ref_clock;

      fVCOHighest = (ref_clock * N) / (M + 1);

      if(fVCOHighest < fMinVCO || fVCOLowest > fMaxVCO)
	  continue;

      for(M = P4RD_PLL_MIN_M; M <= P4RD_PLL_MAX_M; ++M, ++cInnerLoopIterations)
	{
	  N = ((M + 1) * (1 << P) * req_clock) / ref_clock;

	  if(N > P4RD_PLL_MAX_N || N < P4RD_PLL_MIN_N)
	      continue;

	  /*  we can expect rounding errors in calculating M, which will always be rounded down. */
	  /*  So we'll checkout our calculated value of M along with (M+1) */

	  for(LoopCount = (N == P4RD_PLL_MAX_N) ? 1 : 2; --LoopCount >= 0; ++N)
	    {
	      fVCO = (ref_clock * N) / (M + 1);
	      
	      if( (fVCO >= fMinVCO) && (fVCO <= fMaxVCO) )
		{
		  ActualClock = (fVCO / (1 << P));

		  Error = ActualClock - req_clock;

		  if(Error < 0)
		      Error = -Error;

		  /*  It is desirable that we use the lowest value of M if the*/
		  /*  frequencies are the same.*/
		  if(Error < LowestError || (Error == LowestError && M < *param_m))
		    {
		      bFoundFreq = TRUE;
		      LowestError = Error;
		      *param_m = M;
		      *param_n = N;
		      *param_p = P;
		      if(Error == 0)
			goto Done;
		    }
		}
	    }
	}
    }

Done:
  if(bFoundFreq)
    ActualClock = (ref_clock * (*param_n)) / (((*param_m) + 1) * (1 << (*param_p)));
  else
    ActualClock = 0;
  
  return(ActualClock);
}

void
Permedia3PreInit(ScrnInfoPtr pScrn)
{
    GLINTPtr pGlint = GLINTPTR(pScrn);
    CARD32 LocalMemCaps;

    TRACE_ENTER("Permedia3PreInit");

    if (IS_J2000) {
    	unsigned char m,n,p;

	if (pGlint->Chipset == PCI_VENDOR_3DLABS_CHIP_GAMMA)
	    GLINT_SLOW_WRITE_REG(GCSRSecondaryGLINTMapEn, GCSRAperture);

	/* Memory timings for the Appian J2000 board.
	 * This is needed for the second head which is left un-initialized
	 * by the bios, thus freezing the machine. */
	GLINT_SLOW_WRITE_REG(0x02e311B8, PM3LocalMemCaps);
	GLINT_SLOW_WRITE_REG(0x07424905, PM3LocalMemTimings);
	GLINT_SLOW_WRITE_REG(0x0c000003, PM3LocalMemControl);
	GLINT_SLOW_WRITE_REG(0x00000061, PM3LocalMemRefresh);
	GLINT_SLOW_WRITE_REG(0x00000000, PM3LocalMemPowerDown);
	
	/* Let's program the K, M and S Clocks to the same values as the bios
	 * does for first head :
	 *   - KClk and MClk are 105Mhz.
	 *   - S Clock is set to PClk.
	 * Note 1 : pGlint->RefClock is not set yet, so use 14318 instead.
	 * Note 2 : KClk gets internally halved, so we need to double it.
	 */
	(void) PM3DAC_CalculateClock(2*105000, 14318, &m,&n,&p);
        Permedia2vOutIndReg(pScrn, PM3RD_KClkPreScale, 0x00, m);
        Permedia2vOutIndReg(pScrn, PM3RD_KClkFeedbackScale, 0x00, n);
        Permedia2vOutIndReg(pScrn, PM3RD_KClkPostScale, 0x00, p);
        Permedia2vOutIndReg(pScrn, PM3RD_KClkControl, 0x00,
	    PM3RD_KClkControl_STATE_RUN |
	    PM3RD_KClkControl_SOURCE_PLL |
	    PM3RD_KClkControl_ENABLE);
        Permedia2vOutIndReg(pScrn, PM3RD_MClkControl, 0x00,
	    PM3RD_MClkControl_STATE_RUN |
	    PM3RD_MClkControl_SOURCE_KCLK |
	    PM3RD_MClkControl_ENABLE);
        Permedia2vOutIndReg(pScrn, PM3RD_SClkControl, 0x00,
	    PM3RD_SClkControl_STATE_RUN |
	    PM3RD_SClkControl_SOURCE_PCLK |
	    PM3RD_SClkControl_ENABLE);
    }

#if defined(__alpha__)
    /*
     * On Alpha, we have to "int10" secondary VX1 cards early;
     * otherwise, some information taken from registers, like
     * memory size, is incorrect.
     */
    if (!xf86IsPrimaryPci(pGlint->PciInfo)) {
        if ( IS_QVX1 ) {

	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "VX1 secondary enabling VGA before int10\n");

	    /* Enable VGA on the current card. */
	    PCI_WRITE_LONG( pGlint->PciInfo, 0, 0xf8 );
	    PCI_WRITE_LONG( pGlint->PciInfo, 0, 0xf4 );
	    PCI_WRITE_LONG( pGlint->PciInfo, 0, 0xfc );

	    /* The card we are on should be VGA-enabled now, so run int10. */
	    if (xf86LoadSubModule(pScrn, "int10")) {
	        xf86Int10InfoPtr pInt;

	        xf86LoaderReqSymLists(GLINTint10Symbols, NULL);
		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Initializing int10\n");
		pInt = xf86InitInt10(pGlint->pEnt->index);
		xf86FreeInt10(pInt);
	    }

	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "VX1 secondary disabling VGA after int10\n");

	    /* Finally, disable VGA on the current card. */
	    PCI_WRITE_LONG( pGlint->PciInfo, 0x70, 0xf8 );
	    PCI_WRITE_LONG( pGlint->PciInfo, 0x01, 0xf4 );
	    PCI_WRITE_LONG( pGlint->PciInfo, 0x00, 0xfc );
	}
    }
#endif /* __alpha__ */

    /* If we have SDRAM instead of SGRAM, we have to do some things
       differently in the FillRectSolid code. */       
    LocalMemCaps = GLINT_READ_REG(PM3LocalMemCaps);
    pGlint->PM3_UsingSGRAM = !(LocalMemCaps & PM3LocalMemCaps_NoWriteMask);
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Using %s memory\n", 
	       pGlint->PM3_UsingSGRAM ? "SGRAM" : "SDRAM");

    TRACE_EXIT("Permedia3PreInit");
}

Bool
Permedia3Init(ScrnInfoPtr pScrn, DisplayModePtr mode, GLINTRegPtr pReg)
{
    GLINTPtr pGlint = GLINTPTR(pScrn);
    CARD32 temp1, temp2, temp3, temp4;

    if ((pGlint->numMultiDevices == 2) || (IS_J2000)) {
	STOREREG(GCSRAperture, GCSRSecondaryGLINTMapEn);
    }

    if (pGlint->MultiAperture) {
	STOREREG(GMultGLINTAperture, pGlint->realWidth);
	STOREREG(GMultGLINT1, PCI_REGION_BASE(pGlint->MultiPciInfo[0], 2, REGION_MEM) & 0xFF800000);
	STOREREG(GMultGLINT2, PCI_REGION_BASE(pGlint->MultiPciInfo[1], 2, REGION_MEM) & 0xFF800000);
    }

    STOREREG(PM3MemBypassWriteMask, 	0xffffffff);
    STOREREG(Aperture0,		 	0x00000000);
    STOREREG(Aperture1,		 	0x00000000);

    if (pGlint->Chipset == PCI_VENDOR_3DLABS_CHIP_GAMMA)
    	STOREREG(DFIFODis,			0x00000001);

    STOREREG(FIFODis,			0x00000007);

    temp1 = mode->CrtcHSyncStart - mode->CrtcHDisplay;
    temp2 = mode->CrtcVSyncStart - mode->CrtcVDisplay;
    temp3 = mode->CrtcHSyncEnd - mode->CrtcHSyncStart;
    temp4 = mode->CrtcVSyncEnd - mode->CrtcVSyncStart;

    STOREREG(PMHTotal,	Shiftbpp(pScrn, mode->CrtcHTotal - 1));
    STOREREG(PMHsEnd,	Shiftbpp(pScrn, temp1 + temp3));
    STOREREG(PMHsStart,	Shiftbpp(pScrn, temp1));
    STOREREG(PMHbEnd,	Shiftbpp(pScrn, mode->CrtcHTotal - mode->CrtcHDisplay));
    STOREREG(PMHgEnd,	Shiftbpp(pScrn, mode->CrtcHTotal - mode->CrtcHDisplay));
    STOREREG(PMScreenStride, Shiftbpp(pScrn, pScrn->displayWidth));

    STOREREG(PMVTotal,	mode->CrtcVTotal - 1);
    STOREREG(PMVsEnd,	temp2 + temp4 - 1);
    STOREREG(PMVsStart,	temp2 - 1);
    STOREREG(PMVbEnd,	mode->CrtcVTotal - mode->CrtcVDisplay);

    switch (pScrn->bitsPerPixel)
    {
	case 8:
	    STOREREG(PM3ByAperture1Mode, PM3ByApertureMode_PIXELSIZE_8BIT);
	    STOREREG(PM3ByAperture2Mode, PM3ByApertureMode_PIXELSIZE_8BIT);
	    STOREREG(PMVideoControl,	 1 | (1 << 3) | (1 << 5) | (0 << 19));
	    break;
	case 16:
#if X_BYTE_ORDER != X_BIG_ENDIAN
	    STOREREG(PM3ByAperture1Mode, PM3ByApertureMode_PIXELSIZE_16BIT);
	    STOREREG(PM3ByAperture2Mode, PM3ByApertureMode_PIXELSIZE_16BIT);
#else
	    STOREREG(PM3ByAperture1Mode, PM3ByApertureMode_PIXELSIZE_16BIT |
					 PM3ByApertureMode_BYTESWAP_BADC);
	    STOREREG(PM3ByAperture2Mode, PM3ByApertureMode_PIXELSIZE_16BIT |
					 PM3ByApertureMode_BYTESWAP_BADC);
#endif
	    STOREREG(PMVideoControl,	 1 | (1 << 3) | (1 << 5) | (1 << 19));
	    break;
	case 32:
#if X_BYTE_ORDER != X_BIG_ENDIAN
	    STOREREG(PM3ByAperture1Mode, PM3ByApertureMode_PIXELSIZE_32BIT);
	    STOREREG(PM3ByAperture2Mode, PM3ByApertureMode_PIXELSIZE_32BIT);
#else
	    STOREREG(PM3ByAperture1Mode, PM3ByApertureMode_PIXELSIZE_32BIT |
					 PM3ByApertureMode_BYTESWAP_DCBA);
	    STOREREG(PM3ByAperture2Mode, PM3ByApertureMode_PIXELSIZE_32BIT |
					 PM3ByApertureMode_BYTESWAP_DCBA);
#endif
	    STOREREG(PMVideoControl,	 1 | (1 << 3) | (1 << 5) | (2 << 19));
	    break;
    }

    STOREREG(VClkCtl, GLINT_READ_REG(VClkCtl) & 0xFFFFFFFC);
    STOREREG(PMScreenBase, 0x00000000);
    STOREREG(ChipConfig, GLINT_READ_REG(ChipConfig) & 0xFFFFFFFD);

    {
	/* Get the programmable clock values */
    	unsigned char m,n,p;
	
	/* Let's program the dot clock */
	switch (pGlint->Chipset) {
	case PCI_VENDOR_3DLABS_CHIP_PERMEDIA4:
	case PCI_VENDOR_3DLABS_CHIP_R4:
	  (void) PM4DAC_CalculateClock(mode->Clock, pGlint->RefClock, &m,&n,&p);
	  break;
	case PCI_VENDOR_3DLABS_CHIP_PERMEDIA3:
	  (void) PM3DAC_CalculateClock(mode->Clock, pGlint->RefClock, &m,&n,&p);
	  break;
	case PCI_VENDOR_3DLABS_CHIP_GAMMA:
	  switch (pGlint->MultiChip) {
	    case PCI_CHIP_PERMEDIA3:
		(void) PM3DAC_CalculateClock(mode->Clock,
					    pGlint->RefClock, &m,&n,&p);
		break;
	    case PCI_CHIP_R4:
		(void) PM4DAC_CalculateClock(mode->Clock,
					    pGlint->RefClock, &m,&n,&p);
		break;
	  }
	  break;
	}
	STOREDAC(PM3RD_DClk0PreScale, m);
	STOREDAC(PM3RD_DClk0FeedbackScale, n);
	STOREDAC(PM3RD_DClk0PostScale, p);
    }

    temp1 = 0;
    temp2 = 0;
    temp3 = 0;

    if (pGlint->UseFlatPanel) {
    	temp2 |= PM3RD_DACControl_BLANK_PEDESTAL_ENABLE;
    	temp3 |= PM3RD_MiscControl_VSB_OUTPUT_ENABLE;
    	STOREREG(VSConfiguration, 0x06); 
    	STOREREG(VSBBase, 1<<14);
    }

    if (mode->Flags & V_PHSYNC) temp1 |= PM3RD_SyncControl_HSYNC_ACTIVE_HIGH;
    if (mode->Flags & V_PVSYNC) temp1 |= PM3RD_SyncControl_VSYNC_ACTIVE_HIGH;

    STOREREG(PM2VDACRDIndexControl, 0x00);
    STOREDAC(PM2VDACRDSyncControl, temp1);
    STOREDAC(PM2VDACRDDACControl, temp2);

    if (pScrn->rgbBits == 8)
	temp3 |= 0x01; /* 8bit DAC */

    switch (pScrn->bitsPerPixel)
    {
    case 8:
	STOREDAC(PM2VDACRDPixelSize, 0x00);
	STOREDAC(PM2VDACRDColorFormat, 0x2E);
    	break;
    case 16:
    	temp3 |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE;
	STOREDAC(PM2VDACRDPixelSize, 0x01);
	if (pScrn->depth == 15) {
	    STOREDAC(PM2VDACRDColorFormat, 0x61);
	} else {
	    STOREDAC(PM2VDACRDColorFormat, 0x70);
	}
    	break;
    case 24:
    	temp3 |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE;
	STOREDAC(PM2VDACRDPixelSize, 0x04);
	STOREDAC(PM2VDACRDColorFormat, 0x20);
    	break;
    case 32:
    	temp3 |= PM3RD_MiscControl_DIRECTCOLOR_ENABLE;
	if (pScrn->overlayFlags & OVERLAY_8_32_PLANAR) {
	    temp3 |= 0x18;
	    STOREDAC(PM2VDACRDOverlayKey, pScrn->colorKey);
	}
	STOREDAC(PM2VDACRDPixelSize, 0x02);
	STOREDAC(PM2VDACRDColorFormat, 0x20);
    	break;
    }
    STOREDAC(PM2VDACRDMiscControl, temp3);

    STOREREG(PM3FifoControl, 0x905); /* Lower the default fifo threshold */

    return(TRUE);
}

void
Permedia3Save(ScrnInfoPtr pScrn, GLINTRegPtr pReg)
{
    GLINTPtr pGlint = GLINTPTR(pScrn);
    int i;

    /* We can't rely on the vgahw layer copying the font information
     * back properly, due to problems with MMIO access to VGA space
     * so we memcpy the information using the slow routines */
    xf86SlowBcopy((CARD8*)pGlint->FbBase, (CARD8*)pGlint->VGAdata, 65536);

    if ((pGlint->numMultiDevices == 2) || (IS_J2000)) {
	SAVEREG(GCSRAperture);
    }

    if (pGlint->MultiAperture) {
	SAVEREG(GMultGLINTAperture);
	SAVEREG(GMultGLINT1);
	SAVEREG(GMultGLINT2);
    }

    /* Permedia 3 memory Timings */
    SAVEREG(PM3MemBypassWriteMask);
    SAVEREG(PM3ByAperture1Mode);
    SAVEREG(PM3ByAperture2Mode);
    SAVEREG(ChipConfig);
    SAVEREG(Aperture0);
    SAVEREG(Aperture1);
    SAVEREG(PM3FifoControl);

    if (pGlint->Chipset == PCI_VENDOR_3DLABS_CHIP_GAMMA)
   	SAVEREG(DFIFODis);
    SAVEREG(FIFODis);
    SAVEREG(PMHTotal);
    SAVEREG(PMHbEnd);
    SAVEREG(PMHgEnd);
    SAVEREG(PMScreenStride);
    SAVEREG(PMHsStart);
    SAVEREG(PMHsEnd);
    SAVEREG(PMVTotal);
    SAVEREG(PMVbEnd);
    SAVEREG(PMVsStart);
    SAVEREG(PMVsEnd);
    SAVEREG(PMScreenBase);
    SAVEREG(PMVideoControl);
    SAVEREG(VClkCtl);
    if (pGlint->UseFlatPanel) {
    	SAVEREG(VSConfiguration);
    	SAVEREG(VSBBase);
    }

    for (i=0;i<768;i++) {
    	Permedia2ReadAddress(pScrn, i);
	pReg->cmap[i] = Permedia2ReadData(pScrn);
    }

    SAVEREG(PM2VDACRDIndexControl);
    P2VIN(PM2VDACRDOverlayKey);
    P2VIN(PM2VDACRDSyncControl);
    P2VIN(PM2VDACRDMiscControl);
    P2VIN(PM2VDACRDDACControl);
    P2VIN(PM2VDACRDPixelSize);
    P2VIN(PM2VDACRDColorFormat);
    P2VIN(PM2VDACRDDClk0PreScale);
    P2VIN(PM2VDACRDDClk0FeedbackScale);
    P2VIN(PM2VDACRDDClk0PostScale);
}

void
Permedia3Restore(ScrnInfoPtr pScrn, GLINTRegPtr pReg)
{
    GLINTPtr pGlint = GLINTPTR(pScrn);
    CARD32 temp;
    int i;

    /* We can't rely on the vgahw layer copying the font information
     * back properly, due to problems with MMIO access to VGA space
     * so we memcpy the information using the slow routines */
    if (pGlint->STATE)
	xf86SlowBcopy((CARD8*)pGlint->VGAdata, (CARD8*)pGlint->FbBase, 65536);

    if ((pGlint->numMultiDevices == 2) || (IS_J2000)) {
	RESTOREREG(GCSRAperture);
    }

    if (pGlint->MultiAperture) {
	RESTOREREG(GMultGLINTAperture);
	RESTOREREG(GMultGLINT1);
	RESTOREREG(GMultGLINT2);
    }

    /* Permedia 3 memory Timings */
    RESTOREREG(PM3MemBypassWriteMask);
    RESTOREREG(PM3ByAperture1Mode);
    RESTOREREG(PM3ByAperture2Mode);
    RESTOREREG(ChipConfig);
    RESTOREREG(Aperture0);
    RESTOREREG(Aperture1);
    RESTOREREG(PM3FifoControl);
    if (pGlint->Chipset == PCI_VENDOR_3DLABS_CHIP_GAMMA)
    	RESTOREREG(DFIFODis);
    RESTOREREG(FIFODis);
    RESTOREREG(PMVideoControl);
    RESTOREREG(PMHbEnd);
    RESTOREREG(PMHgEnd);
    RESTOREREG(PMScreenBase);
    RESTOREREG(VClkCtl);
    RESTOREREG(PMScreenStride);
    RESTOREREG(PMHTotal);
    RESTOREREG(PMHsStart);
    RESTOREREG(PMHsEnd);
    RESTOREREG(PMVTotal);
    RESTOREREG(PMVbEnd);
    RESTOREREG(PMVsStart);
    RESTOREREG(PMVsEnd);

    if (pGlint->UseFlatPanel) {
    	RESTOREREG(VSConfiguration);
    	RESTOREREG(VSBBase);
    }

    RESTOREREG(PM2VDACRDIndexControl);
    P2VOUT(PM2VDACRDOverlayKey);
    P2VOUT(PM2VDACRDSyncControl);
    P2VOUT(PM2VDACRDMiscControl);
    P2VOUT(PM2VDACRDDACControl);
    P2VOUT(PM2VDACRDPixelSize);
    P2VOUT(PM2VDACRDColorFormat);

    for (i=0;i<768;i++) {
    	Permedia2WriteAddress(pScrn, i);
	Permedia2WriteData(pScrn, pReg->cmap[i]);
    }

    temp = Permedia2vInIndReg(pScrn, PM2VDACIndexClockControl) & 0xFC;
    P2VOUT(PM2VDACRDDClk0PreScale);
    P2VOUT(PM2VDACRDDClk0FeedbackScale);
    P2VOUT(PM2VDACRDDClk0PostScale);
    Permedia2vOutIndReg(pScrn, PM2VDACIndexClockControl, 0x00, temp|0x03);
}

void Permedia3LoadPalette(
    ScrnInfoPtr pScrn, 
    int numColors, 
    int *indices,
    LOCO *colors,
    VisualPtr pVisual
){
#if 0
    GLINTPtr pGlint = GLINTPTR(pScrn);
#endif
    int i, index, shift = 0, j, repeat = 1;

    if (pScrn->depth == 15) {
	repeat = 8;
	shift = 3;
    }

    for(i = 0; i < numColors; i++) {
	index = indices[i];
	for (j = 0; j < repeat; j++) {
	    Permedia2WriteAddress(pScrn, (index << shift)+j);
	    Permedia2WriteData(pScrn, colors[index].red);
	    Permedia2WriteData(pScrn, colors[index].green);
	    Permedia2WriteData(pScrn, colors[index].blue);
	}
#if 0
        GLINT_SLOW_WRITE_REG(index, PM3LUTIndex);
	GLINT_SLOW_WRITE_REG((colors[index].red & 0xFF) |
			     ((colors[index].green & 0xFF) << 8) |
			     ((colors[index].blue & 0xFF) << 16),
			     PM3LUTData);
#endif
    }
}

/* special one for 565 mode */
void Permedia3LoadPalette16(
    ScrnInfoPtr pScrn, 
    int numColors, 
    int *indices,
    LOCO *colors,
    VisualPtr pVisual
){
#if 0
    GLINTPtr pGlint = GLINTPTR(pScrn);
#endif
    int i, index, j;

    for(i = 0; i < numColors; i++) {
	index = indices[i];
	for (j = 0; j < 4; j++) {
	    Permedia2WriteAddress(pScrn, (index << 2)+j);
	    Permedia2WriteData(pScrn, colors[index >> 1].red);
	    Permedia2WriteData(pScrn, colors[index].green);
	    Permedia2WriteData(pScrn, colors[index >> 1].blue);
	}
#if 0
        GLINT_SLOW_WRITE_REG(index, PM3LUTIndex);
	GLINT_SLOW_WRITE_REG((colors[index].red & 0xFF) |
			     ((colors[index].green & 0xFF) << 8) |
			     ((colors[index].blue & 0xFF) << 16),
			     PM3LUTData);
#endif

	if(index <= 31) {
	    for (j = 0; j < 4; j++) {
	    	Permedia2WriteAddress(pScrn, (index << 3)+j);
	    	Permedia2WriteData(pScrn, colors[index].red);
	    	Permedia2WriteData(pScrn, colors[(index << 1) + 1].green);
	    	Permedia2WriteData(pScrn, colors[index].blue);
	    }
	}
    }
}