Module Name:    xsrc
Committed By:   mrg
Date:           Mon Jul 20 00:55:42 UTC 2015

Modified Files:
        xsrc/external/mit/xf86-video-r128/dist/src: r128.h r128_driver.c
            r128_reg.h
Removed Files:
        xsrc/external/mit/xf86-video-r128/dist/src: r128_dga.c

Log Message:
attempt to merge r128 driver.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 xsrc/external/mit/xf86-video-r128/dist/src/r128.h
cvs rdiff -u -r1.1.1.2 -r0 \
    xsrc/external/mit/xf86-video-r128/dist/src/r128_dga.c
cvs rdiff -u -r1.11 -r1.12 \
    xsrc/external/mit/xf86-video-r128/dist/src/r128_driver.c
cvs rdiff -u -r1.2 -r1.3 \
    xsrc/external/mit/xf86-video-r128/dist/src/r128_reg.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: xsrc/external/mit/xf86-video-r128/dist/src/r128.h
diff -u xsrc/external/mit/xf86-video-r128/dist/src/r128.h:1.5 xsrc/external/mit/xf86-video-r128/dist/src/r128.h:1.6
--- xsrc/external/mit/xf86-video-r128/dist/src/r128.h:1.5	Wed Mar 19 23:56:47 2014
+++ xsrc/external/mit/xf86-video-r128/dist/src/r128.h	Mon Jul 20 00:55:42 2015
@@ -74,6 +74,7 @@
 #endif
 
 #include "fb.h"
+#include "xf86Crtc.h"
 
 #include "compat-api.h"
 #include "atipcirename.h"
@@ -136,6 +137,14 @@
 #define R128_ALIGN(x,bytes) (((x) + ((bytes) - 1)) & ~((bytes) - 1))
 #define R128PTR(pScrn) ((R128InfoPtr)(pScrn)->driverPrivate)
 
+#define R128_BIOS8(v)  ((info->VBIOS[(v)]))
+#define R128_BIOS16(v) ((info->VBIOS[(v)])           | \
+			(info->VBIOS[(v) + 1] << 8))
+#define R128_BIOS32(v) ((info->VBIOS[(v)])           | \
+			(info->VBIOS[(v) + 1] << 8)  | \
+			(info->VBIOS[(v) + 2] << 16) | \
+			(info->VBIOS[(v) + 3] << 24))
+
 typedef struct {        /* All values in XCLKS    */
     int  ML;            /* Memory Read Latency    */
     int  MB;            /* Memory Burst Length    */
@@ -151,106 +160,106 @@ typedef struct {        /* All values in
 
 typedef struct {
 				/* Common registers */
-    CARD32     ovr_clr;
-    CARD32     ovr_wid_left_right;
-    CARD32     ovr_wid_top_bottom;
-    CARD32     ov0_scale_cntl;
-    CARD32     mpp_tb_config;
-    CARD32     mpp_gp_config;
-    CARD32     subpic_cntl;
-    CARD32     viph_control;
-    CARD32     i2c_cntl_1;
-    CARD32     gen_int_cntl;
-    CARD32     cap0_trig_cntl;
-    CARD32     cap1_trig_cntl;
-    CARD32     bus_cntl;
-    CARD32     config_cntl;
+    uint32_t   ovr_clr;
+    uint32_t   ovr_wid_left_right;
+    uint32_t   ovr_wid_top_bottom;
+    uint32_t   ov0_scale_cntl;
+    uint32_t   mpp_tb_config;
+    uint32_t   mpp_gp_config;
+    uint32_t   subpic_cntl;
+    uint32_t   viph_control;
+    uint32_t   i2c_cntl_1;
+    uint32_t   gen_int_cntl;
+    uint32_t   cap0_trig_cntl;
+    uint32_t   cap1_trig_cntl;
+    uint32_t   bus_cntl;
+    uint32_t   config_cntl;
 
 				/* Other registers to save for VT switches */
-    CARD32     dp_datatype;
-    CARD32     gen_reset_cntl;
-    CARD32     clock_cntl_index;
-    CARD32     amcgpio_en_reg;
-    CARD32     amcgpio_mask;
+    uint32_t   dp_datatype;
+    uint32_t   gen_reset_cntl;
+    uint32_t   clock_cntl_index;
+    uint32_t   amcgpio_en_reg;
+    uint32_t   amcgpio_mask;
 
 				/* CRTC registers */
-    CARD32     crtc_gen_cntl;
-    CARD32     crtc_ext_cntl;
-    CARD32     dac_cntl;
-    CARD32     crtc_h_total_disp;
-    CARD32     crtc_h_sync_strt_wid;
-    CARD32     crtc_v_total_disp;
-    CARD32     crtc_v_sync_strt_wid;
-    CARD32     crtc_offset;
-    CARD32     crtc_offset_cntl;
-    CARD32     crtc_pitch;
+    uint32_t   crtc_gen_cntl;
+    uint32_t   crtc_ext_cntl;
+    uint32_t   dac_cntl;
+    uint32_t   crtc_h_total_disp;
+    uint32_t   crtc_h_sync_strt_wid;
+    uint32_t   crtc_v_total_disp;
+    uint32_t   crtc_v_sync_strt_wid;
+    uint32_t   crtc_offset;
+    uint32_t   crtc_offset_cntl;
+    uint32_t   crtc_pitch;
 
 				/* CRTC2 registers */
-    CARD32     crtc2_gen_cntl;
-    CARD32     crtc2_h_total_disp;
-    CARD32     crtc2_h_sync_strt_wid;
-    CARD32     crtc2_v_total_disp;
-    CARD32     crtc2_v_sync_strt_wid;
-    CARD32     crtc2_offset;
-    CARD32     crtc2_offset_cntl;
-    CARD32     crtc2_pitch;
+    uint32_t   crtc2_gen_cntl;
+    uint32_t   crtc2_h_total_disp;
+    uint32_t   crtc2_h_sync_strt_wid;
+    uint32_t   crtc2_v_total_disp;
+    uint32_t   crtc2_v_sync_strt_wid;
+    uint32_t   crtc2_offset;
+    uint32_t   crtc2_offset_cntl;
+    uint32_t   crtc2_pitch;
 
 				/* Flat panel registers */
-    CARD32     fp_crtc_h_total_disp;
-    CARD32     fp_crtc_v_total_disp;
-    CARD32     fp_gen_cntl;
-    CARD32     fp_h_sync_strt_wid;
-    CARD32     fp_horz_stretch;
-    CARD32     fp_panel_cntl;
-    CARD32     fp_v_sync_strt_wid;
-    CARD32     fp_vert_stretch;
-    CARD32     lvds_gen_cntl;
-    CARD32     tmds_crc;
-    CARD32     tmds_transmitter_cntl;
+    uint32_t   fp_crtc_h_total_disp;
+    uint32_t   fp_crtc_v_total_disp;
+    uint32_t   fp_gen_cntl;
+    uint32_t   fp_h_sync_strt_wid;
+    uint32_t   fp_horz_stretch;
+    uint32_t   fp_panel_cntl;
+    uint32_t   fp_v_sync_strt_wid;
+    uint32_t   fp_vert_stretch;
+    uint32_t   lvds_gen_cntl;
+    uint32_t   tmds_crc;
+    uint32_t   tmds_transmitter_cntl;
 
 				/* Computed values for PLL */
-    CARD32     dot_clock_freq;
-    CARD32     pll_output_freq;
+    uint32_t   dot_clock_freq;
+    uint32_t   pll_output_freq;
     int        feedback_div;
     int        post_div;
 
 				/* PLL registers */
-    CARD32     ppll_ref_div;
-    CARD32     ppll_div_3;
-    CARD32     ppll_div_0;
-    CARD32     htotal_cntl;
+    uint32_t   ppll_ref_div;
+    uint32_t   ppll_div_3;
+    uint32_t   ppll_div_0;
+    uint32_t   htotal_cntl;
 
 				/* Computed values for PLL2 */
-    CARD32     dot_clock_freq_2;
-    CARD32     pll_output_freq_2;
+    uint32_t   dot_clock_freq_2;
+    uint32_t   pll_output_freq_2;
     int        feedback_div_2;
     int        post_div_2;
 
 				/* PLL2 registers */
-    CARD32     p2pll_ref_div;
-    CARD32     p2pll_div_0;
-    CARD32     htotal_cntl2;
+    uint32_t   p2pll_ref_div;
+    uint32_t   p2pll_div_0;
+    uint32_t   htotal_cntl2;
 
 				/* DDA register */
-    CARD32     dda_config;
-    CARD32     dda_on_off;
+    uint32_t   dda_config;
+    uint32_t   dda_on_off;
 
 				/* DDA2 register */
-    CARD32     dda2_config;
-    CARD32     dda2_on_off;
+    uint32_t   dda2_config;
+    uint32_t   dda2_on_off;
 
 				/* Pallet */
     Bool       palette_valid;
-    CARD32     palette[256];
-    CARD32     palette2[256];
+    uint32_t   palette[256];
+    uint32_t   palette2[256];
 } R128SaveRec, *R128SavePtr;
 
 typedef struct {
-    CARD16        reference_freq;
-    CARD16        reference_div;
+    uint16_t      reference_freq;
+    uint16_t      reference_div;
     unsigned      min_pll_freq;
     unsigned      max_pll_freq;
-    CARD16        xclk;
+    uint16_t      xclk;
 } R128PLLRec, *R128PLLPtr;
 
 typedef struct {
@@ -262,16 +271,6 @@ typedef struct {
     DisplayModePtr     mode;
 } R128FBLayout;
 
-typedef enum
-{
-    MT_NONE,
-    MT_CRT,
-    MT_LCD,
-    MT_DFP,
-    MT_CTV,
-    MT_STV
-} R128MonitorType;
-
 #ifdef USE_EXA
 struct r128_2d_state {
     Bool in_use;
@@ -303,9 +302,10 @@ struct r128_2d_state {
 typedef struct {
     EntityInfoPtr     pEnt;
     pciVideoPtr       PciInfo;
+#ifndef XSERVER_LIBPCIACCESS
     PCITAG            PciTag;
+#endif
     int               Chipset;
-    Bool              Primary;
 
 #ifndef AVOID_FBDEV
     Bool              FBDev;
@@ -317,28 +317,13 @@ typedef struct {
 
     void              *MMIO;        /* Map of MMIO region                    */
     void              *FB;          /* Map of frame buffer                   */
+    uint8_t           *VBIOS;       /* Video BIOS for mode validation on FPs */
+    int               FPBIOSstart;  /* Start of the flat panel info          */
 
-    CARD32            MemCntl;
-    CARD32            BusCntl;
+    uint32_t          MemCntl;
+    uint32_t          BusCntl;
     unsigned long     FbMapSize;    /* Size of frame buffer, in bytes        */
-    int               Flags;        /* Saved copy of mode flags              */
-
-    CARD8             BIOSDisplay;  /* Device the BIOS is set to display to  */
-
     Bool              HasPanelRegs; /* Current chip can connect to a FP      */
-    CARD8             *VBIOS;       /* Video BIOS for mode validation on FPs */
-    int               FPBIOSstart;  /* Start of the flat panel info          */
-
-				/* Computed values for FPs */
-    int               PanelXRes;
-    int               PanelYRes;
-    int               HOverPlus;
-    int               HSyncWidth;
-    int               HBlank;
-    int               VOverPlus;
-    int               VSyncWidth;
-    int               VBlank;
-    int               PanelPwrDly;
 
     R128PLLRec        pll;
     R128RAMPtr        ram;
@@ -354,7 +339,6 @@ typedef struct {
     XAAInfoRecPtr     accel;
 #endif
     Bool              accelOn;
-
     Bool	      useEXA;
     Bool	      RenderAccel;
 #ifdef USE_EXA
@@ -363,16 +347,6 @@ typedef struct {
     struct r128_2d_state state_2d;
 #endif
 
-    xf86CursorInfoPtr cursor;
-    unsigned long     cursor_start;
-    unsigned long     cursor_end;
-
-    /*
-     * XAAForceTransBlit is used to change the behavior of the XAA
-     * SetupForScreenToScreenCopy function, to make it DGA-friendly.
-     */
-    Bool              XAAForceTransBlit;
-
     int               fifo_slots;   /* Free slots in the FIFO (64 max)       */
     int               pix24bpp;     /* Depth of pixmap for 24bpp framebuffer */
     Bool              dac6bits;     /* Use 6 bit DAC?                        */
@@ -380,7 +354,7 @@ typedef struct {
 				/* Computed values for Rage 128 */
     int               pitch;
     int               datatype;
-    CARD32            dp_gui_master_cntl;
+    uint32_t          dp_gui_master_cntl;
 
 				/* Saved values for ScreenToScreenCopy */
     int               xdir;
@@ -405,30 +379,24 @@ typedef struct {
     int               scanline_direct;
     int               scanline_bpp; /* Only used for ImageWrite */
 
-    DGAModePtr        DGAModes;
-    int               numDGAModes;
-    Bool              DGAactive;
-    int               DGAViewportStatus;
-    DGAFunctionRec    DGAFuncs;
-
     R128FBLayout      CurrentLayout;
 #ifdef R128DRI
     Bool              directRenderingEnabled;
     DRIInfoPtr        pDRIInfo;
     int               drmFD;
-    drm_context_t        drmCtx;
+    drm_context_t     drmCtx;
     int               numVisualConfigs;
     __GLXvisualConfig *pVisualConfigs;
     R128ConfigPrivPtr pVisualConfigsPriv;
 
-    drm_handle_t         fbHandle;
+    drm_handle_t      fbHandle;
 
     drmSize           registerSize;
-    drm_handle_t         registerHandle;
+    drm_handle_t      registerHandle;
 
     Bool              IsPCI;            /* Current card is a PCI card */
     drmSize           pciSize;
-    drm_handle_t         pciMemHandle;
+    drm_handle_t      pciMemHandle;
     drmAddress        PCI;              /* Map */
 
     Bool              allowPageFlip;    /* Enable 3d page flipping */
@@ -436,7 +404,7 @@ typedef struct {
     int               drmMinor;
 
     drmSize           agpSize;
-    drm_handle_t         agpMemHandle;     /* Handle from drmAgpAlloc */
+    drm_handle_t      agpMemHandle;     /* Handle from drmAgpAlloc */
     unsigned long     agpOffset;
     drmAddress        AGP;              /* Map */
     int               agpMode;
@@ -449,20 +417,20 @@ typedef struct {
 
 				/* CCE ring buffer data */
     unsigned long     ringStart;        /* Offset into AGP space */
-    drm_handle_t         ringHandle;       /* Handle from drmAddMap */
+    drm_handle_t      ringHandle;       /* Handle from drmAddMap */
     drmSize           ringMapSize;      /* Size of map */
     int               ringSize;         /* Size of ring (in MB) */
     drmAddress        ring;             /* Map */
     int               ringSizeLog2QW;
 
     unsigned long     ringReadOffset;   /* Offset into AGP space */
-    drm_handle_t         ringReadPtrHandle; /* Handle from drmAddMap */
+    drm_handle_t      ringReadPtrHandle;/* Handle from drmAddMap */
     drmSize           ringReadMapSize;  /* Size of map */
     drmAddress        ringReadPtr;      /* Map */
 
 				/* CCE vertex/indirect buffer data */
     unsigned long     bufStart;        /* Offset into AGP space */
-    drm_handle_t         bufHandle;       /* Handle from drmAddMap */
+    drm_handle_t      bufHandle;       /* Handle from drmAddMap */
     drmSize           bufMapSize;      /* Size of map */
     int               bufSize;         /* Size of buffers (in MB) */
     drmAddress        buf;             /* Map */
@@ -471,7 +439,7 @@ typedef struct {
 
 				/* CCE AGP Texture data */
     unsigned long     agpTexStart;      /* Offset into AGP space */
-    drm_handle_t         agpTexHandle;     /* Handle from drmAddMap */
+    drm_handle_t      agpTexHandle;     /* Handle from drmAddMap */
     drmSize           agpTexMapSize;    /* Size of map */
     int               agpTexSize;       /* Size of AGP tex space (in MB) */
     drmAddress        agpTex;           /* Map */
@@ -501,18 +469,18 @@ typedef struct {
     int               log2TexGran;
 
 				/* Saved scissor values */
-    CARD32            sc_left;
-    CARD32            sc_right;
-    CARD32            sc_top;
-    CARD32            sc_bottom;
+    uint32_t          sc_left;
+    uint32_t          sc_right;
+    uint32_t          sc_top;
+    uint32_t          sc_bottom;
 
-    CARD32            re_top_left;
-    CARD32            re_width_height;
+    uint32_t          re_top_left;
+    uint32_t          re_width_height;
 
-    CARD32            aux_sc_cntl;
+    uint32_t          aux_sc_cntl;
 
     int               irq;
-    CARD32            gen_int_cntl;
+    uint32_t          gen_int_cntl;
 
     Bool              DMAForXv;
 #endif
@@ -525,19 +493,10 @@ typedef struct {
 
     Bool              isDFP;
     Bool              isPro2;
-    I2CBusPtr         pI2CBus;
-    CARD32            DDCReg;
+    Bool              SwitchingMode;
+    Bool              DDC;
 
     Bool              VGAAccess;
-
-    /****** Added for dualhead support *******************/
-    BOOL              HasCRTC2;     /* M3/M4 */
-    BOOL              IsSecondary;  /* second Screen */
-    BOOL	      IsPrimary;    /* primary Screen */
-    BOOL              UseCRT;       /* force use CRT port as primary */
-    BOOL              SwitchingMode;
-    R128MonitorType DisplayType;  /* Monitor connected on*/
-
 } R128InfoRec, *R128InfoPtr;
 
 #define R128WaitForFifo(pScrn, entries)                                      \
@@ -558,12 +517,54 @@ extern void        R128WaitForVerticalSy
 extern Bool        R128AccelInit(ScreenPtr pScreen);
 extern void        R128EngineInit(ScrnInfoPtr pScrn);
 extern Bool        R128CursorInit(ScreenPtr pScreen);
-extern Bool        R128DGAInit(ScreenPtr pScreen);
 
 extern int         R128MinBits(int val);
+extern xf86OutputPtr R128FirstOutput(xf86CrtcPtr crtc);
 
 extern void        R128InitVideo(ScreenPtr pScreen);
 
+extern void        R128InitCommonRegisters(R128SavePtr save, R128InfoPtr info);
+extern void        R128InitDACRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output);
+extern void        R128InitRMXRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output, DisplayModePtr mode);
+extern void        R128InitFPRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output);
+extern void        R128InitLVDSRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output);
+extern Bool        R128InitCrtcBase(xf86CrtcPtr crtc, R128SavePtr save, int x, int y);
+extern Bool        R128InitCrtcRegisters(xf86CrtcPtr crtc, R128SavePtr save, DisplayModePtr mode);
+extern void        R128InitPLLRegisters(xf86CrtcPtr crtc, R128SavePtr save, R128PLLPtr pll, double dot_clock);
+extern Bool        R128InitDDARegisters(xf86CrtcPtr crtc, R128SavePtr save, R128PLLPtr pll, DisplayModePtr mode);
+extern Bool        R128InitCrtc2Base(xf86CrtcPtr crtc, R128SavePtr save, int x, int y);
+extern Bool        R128InitCrtc2Registers(xf86CrtcPtr crtc, R128SavePtr save, DisplayModePtr mode);
+extern void        R128InitPLL2Registers(xf86CrtcPtr crtc, R128SavePtr save, R128PLLPtr pll, double dot_clock);
+extern Bool        R128InitDDA2Registers(xf86CrtcPtr crtc, R128SavePtr save, R128PLLPtr pll, DisplayModePtr mode);
+extern void        R128RestoreCommonRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreDACRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreRMXRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreFPRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreLVDSRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreCrtcRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestorePLLRegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreDDARegisters(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreCrtc2Registers(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestorePLL2Registers(ScrnInfoPtr pScrn, R128SavePtr restore);
+extern void        R128RestoreDDA2Registers(ScrnInfoPtr pScrn, R128SavePtr restore);
+
+extern void        r128_crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg);
+extern void        r128_crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y);
+extern void        r128_crtc_show_cursor(xf86CrtcPtr crtc);
+extern void        r128_crtc_hide_cursor(xf86CrtcPtr crtc);
+extern void        r128_crtc_load_cursor_image(xf86CrtcPtr crtc, unsigned char *src);
+
+extern uint32_t    R128AllocateMemory(ScrnInfoPtr pScrn, void **mem_struct, int size, int align, Bool need_accel);
+extern Bool        R128SetupConnectors(ScrnInfoPtr pScrn);
+extern Bool        R128AllocateControllers(ScrnInfoPtr pScrn);
+extern void        R128GetPanelInfoFromBIOS(xf86OutputPtr output);
+extern void        R128Blank(ScrnInfoPtr pScrn);
+extern void        R128Unblank(ScrnInfoPtr pScrn);
+extern void        R128DPMSSetOn(xf86OutputPtr output);
+extern void        R128DPMSSetOff(xf86OutputPtr output);
+extern ModeStatus     R128DoValidMode(xf86OutputPtr output, DisplayModePtr mode, int flags);
+extern DisplayModePtr R128ProbeOutputModes(xf86OutputPtr output);
+
 #ifdef R128DRI
 extern Bool        R128DRIScreenInit(ScreenPtr pScreen);
 extern void        R128DRICloseScreen(ScreenPtr pScreen);
@@ -629,7 +630,7 @@ extern void	   R128DoPrepareCopy(ScrnInf
 
 #define R128_VERBOSE	0
 
-#define RING_LOCALS	CARD32 *__head; int __count;
+#define RING_LOCALS	uint32_t *__head; int __count;
 
 #define R128CCE_REFRESH(pScrn, info)					\
 do {									\
@@ -670,12 +671,12 @@ do {									\
       xf86DrvMsg( pScrn->scrnIndex, X_INFO,				\
 		  "ADVANCE_RING() used: %d+%d=%d/%d\n",			\
 		  info->indirectBuffer->used - info->indirectStart,	\
-		  __count * (int)sizeof(CARD32),			\
+		  __count * (int)sizeof(uint32_t),			\
 		  info->indirectBuffer->used - info->indirectStart +	\
-		  __count * (int)sizeof(CARD32),			\
+		  __count * (int)sizeof(uint32_t),			\
 		  info->indirectBuffer->total - info->indirectStart );	\
    }									\
-   info->indirectBuffer->used += __count * (int)sizeof(CARD32);		\
+   info->indirectBuffer->used += __count * (int)sizeof(uint32_t);		\
 } while (0)
 
 #define OUT_RING( x ) do {						\

Index: xsrc/external/mit/xf86-video-r128/dist/src/r128_driver.c
diff -u xsrc/external/mit/xf86-video-r128/dist/src/r128_driver.c:1.11 xsrc/external/mit/xf86-video-r128/dist/src/r128_driver.c:1.12
--- xsrc/external/mit/xf86-video-r128/dist/src/r128_driver.c:1.11	Wed Jul  8 18:56:39 2015
+++ xsrc/external/mit/xf86-video-r128/dist/src/r128_driver.c	Mon Jul 20 00:55:42 2015
@@ -84,7 +84,7 @@
 				/* X and server generic header files */
 #include "xf86.h"
 #include "xf86_OSproc.h"
-#include "xf86PciInfo.h"
+#include "xf86RandR12.h"
 #if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
 #include "xf86RAC.h"
 #include "xf86Resources.h"
@@ -130,11 +130,6 @@ static Bool R128CloseScreen(CLOSE_SCREEN
 static Bool R128SaveScreen(ScreenPtr pScreen, int mode);
 static void R128Save(ScrnInfoPtr pScrn);
 static void R128Restore(ScrnInfoPtr pScrn);
-static Bool R128ModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
-static void R128DisplayPowerManagementSet(ScrnInfoPtr pScrn,
-					  int PowerManagementMode, int flags);
-static void R128DisplayPowerManagementSetLCD(ScrnInfoPtr pScrn,
-					  int PowerManagementMode, int flags);
 
 typedef enum {
   OPTION_NOACCEL,
@@ -188,7 +183,6 @@ static const OptionInfoRec R128Options[]
   { OPTION_BUFFER_SIZE,  "BufferSize",       OPTV_INTEGER, {0}, FALSE },
   { OPTION_PAGE_FLIP,    "EnablePageFlip",   OPTV_BOOLEAN, {0}, FALSE },
 #endif
-  { OPTION_DISPLAY,      "Display",          OPTV_STRING,  {0}, FALSE },
   { OPTION_PANEL_WIDTH,  "PanelWidth",       OPTV_INTEGER, {0}, FALSE },
   { OPTION_PANEL_HEIGHT, "PanelHeight",      OPTV_INTEGER, {0}, FALSE },
   { OPTION_PROG_FP_REGS, "ProgramFPRegs",    OPTV_BOOLEAN, {0}, FALSE },
@@ -260,22 +254,13 @@ static Bool R128MapMMIO(ScrnInfoPtr pScr
     } else
 #endif
     {
-        /* If the primary screen has already mapped the MMIO region,
-           use its pointer instead of mapping it a second time. */
-        if (info->IsSecondary) {
-            DevUnion* pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                                                   getR128EntityIndex());
-            R128EntPtr pR128Ent = pPriv->ptr;
-            R128InfoPtr info0 = R128PTR(pR128Ent->pPrimaryScrn);
-            info->MMIO=info0->MMIO;
-            if (info->MMIO) return TRUE;
-        }
 #ifndef XSERVER_LIBPCIACCESS
 	info->MMIO = xf86MapPciMem(pScrn->scrnIndex,
 				   VIDMEM_MMIO | VIDMEM_READSIDEEFFECT,
 				   info->PciTag,
 				   info->MMIOAddr,
 				   R128_MMIOSIZE);
+        if (!info->MMIO) return FALSE;
 #else
 	int err = pci_device_map_range(info->PciInfo,
 				       info->MMIOAddr,
@@ -292,7 +277,6 @@ static Bool R128MapMMIO(ScrnInfoPtr pScr
 #endif
     }
 
-    if (!info->MMIO) return FALSE;
     return TRUE;
 }
 
@@ -428,78 +412,6 @@ void R128WaitForVerticalSync(ScrnInfoPtr
     }
 }
 
-/* Blank screen. */
-static void R128Blank(ScrnInfoPtr pScrn)
-{
-    R128InfoPtr   info      = R128PTR(pScrn);
-    unsigned char *R128MMIO = info->MMIO;
-
-    if(!info->IsSecondary)
-    {
-        switch(info->DisplayType)
-        {
-        case MT_LCD:
-            OUTREGP(R128_LVDS_GEN_CNTL, R128_LVDS_DISPLAY_DIS,
-                 ~R128_LVDS_DISPLAY_DIS);
-	    break;
-        case MT_CRT:
-            OUTREGP(R128_CRTC_EXT_CNTL, R128_CRTC_DISPLAY_DIS, ~R128_CRTC_DISPLAY_DIS);
-	    break;
-        case MT_DFP:
-            OUTREGP(R128_FP_GEN_CNTL, R128_FP_BLANK_DIS, ~R128_FP_BLANK_DIS);
-	    break;
-        case MT_NONE:
-        default:
-           break;
-        }
-    }
-    else
-    {
-        OUTREGP(R128_CRTC2_GEN_CNTL, R128_CRTC2_DISP_DIS, ~R128_CRTC2_DISP_DIS);
-    }
-}
-
-/* Unblank screen. */
-static void R128Unblank(ScrnInfoPtr pScrn)
-{
-    R128InfoPtr   info      = R128PTR(pScrn);
-    unsigned char *R128MMIO = info->MMIO;
-
-    if(!info->IsSecondary)
-    {
-        switch(info->DisplayType)
-        {
-        case MT_LCD:
-            OUTREGP(R128_LVDS_GEN_CNTL, 0,
-                 ~R128_LVDS_DISPLAY_DIS);
-	    break;
-        case MT_CRT:
-            OUTREGP(R128_CRTC_EXT_CNTL, 0, ~R128_CRTC_DISPLAY_DIS);
-	    break;
-        case MT_DFP:
-            OUTREGP(R128_FP_GEN_CNTL, 0, ~R128_FP_BLANK_DIS);
-	    break;
-        case MT_NONE:
-        default:
-            break;
-        }
-    }
-    else
-    {
-        switch(info->DisplayType)
-        {
-        case MT_LCD:
-        case MT_DFP:
-        case MT_CRT:
-            OUTREGP(R128_CRTC2_GEN_CNTL, 0, ~R128_CRTC2_DISP_DIS);
-            break;
-        case MT_NONE:
-        default:
-            break;
-        }
-    }
-}
-
 /* Compute log base 2 of val. */
 int R128MinBits(int val)
 {
@@ -516,20 +428,26 @@ static int R128Div(int n, int d)
     return (n + (d / 2)) / d;
 }
 
-/* Read the Video BIOS block and the FP registers (if applicable). */
+/* Finds the first output using a given crtc. */
+xf86OutputPtr R128FirstOutput(xf86CrtcPtr crtc)
+{
+    ScrnInfoPtr pScrn = crtc->scrn;
+    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
+    xf86OutputPtr output = xf86_config->output[0];
+    int o;
+
+    for (o = 0; o < xf86_config->num_output; o++) {
+        output = xf86_config->output[o];
+        if (output->crtc == crtc) break;
+    }
+
+    return output;
+}
+
+/* Read the Video BIOS block. */
 static Bool R128GetBIOSParameters(ScrnInfoPtr pScrn, xf86Int10InfoPtr pInt10)
 {
     R128InfoPtr info = R128PTR(pScrn);
-    int         i;
-    int         FPHeader = 0;
-
-#define R128_BIOS8(v)  (info->VBIOS[v])
-#define R128_BIOS16(v) (info->VBIOS[v] | \
-			(info->VBIOS[(v) + 1] << 8))
-#define R128_BIOS32(v) (info->VBIOS[v] | \
-			(info->VBIOS[(v) + 1] << 8) | \
-			(info->VBIOS[(v) + 2] << 16) | \
-			(info->VBIOS[(v) + 3] << 24))
 
 #ifdef XSERVER_LIBPCIACCESS
     int size = info->PciInfo->rom_size > R128_VBIOS_SIZE ? info->PciInfo->rom_size : R128_VBIOS_SIZE;
@@ -574,142 +492,56 @@ static Bool R128GetBIOSParameters(ScrnIn
 		   "Video BIOS not found!\n");
     }
 
-        if(info->HasCRTC2)
-        {                    
-             if(info->IsSecondary)
-             {  
-		/* there may be a way to detect this, for now, just assume 
-		   second head is CRT */
-                 info->DisplayType = MT_CRT;
-
-                 if(info->DisplayType > MT_NONE)
-                 {
-                     DevUnion* pPriv;
-                     R128EntPtr pR128Ent;
-                     pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                         getR128EntityIndex());
-                     pR128Ent = pPriv->ptr;
-                     pR128Ent->HasSecondary = TRUE;
-
-                 }
-                 else return FALSE;
-                     
-             }
-             else
-             {
-                 /* really need some sort of detection here */
-		 if (info->HasPanelRegs) {
-		 	info->DisplayType = MT_LCD;
-		 } else if (info->isDFP) {
-			info->DisplayType = MT_DFP;
-                 } else 
-                 {
-                     /*DVI port has no monitor connected, try CRT port.
-                     If something on CRT port, treat it as primary*/
-                     if(xf86IsEntityShared(pScrn->entityList[0]))
-                     {
-                         DevUnion* pPriv;
-                         R128EntPtr pR128Ent;
-                         pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                             getR128EntityIndex());
-                         pR128Ent = pPriv->ptr;
-                         pR128Ent->BypassSecondary = TRUE;
-                     }
+    return TRUE;
+}
 
-                     info->DisplayType = MT_CRT;
-#if 0
-                     {
-                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
-                             "No monitor detected!!!\n");
-                         return FALSE;
-                     }
-#endif
-                 }
-             }
-         }
-         else
-         {
-             /*Regular Radeon ASIC, only one CRTC, but it could be
-               used for DFP with a DVI output, like AIW board*/
-             if(info->isDFP) info->DisplayType = MT_DFP;
-             else info->DisplayType = MT_CRT;
-         }
-
-    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s Display == Type %d\n",
-              (info->IsSecondary ? "Secondary" : "Primary"), 
-               info->DisplayType);
-
-
-    if (info->VBIOS && info->DisplayType == MT_LCD) {
-	info->FPBIOSstart = 0;
-
-	/* FIXME: There should be direct access to the start of the FP info
-	   tables, but until we find out where that offset is stored, we
-	   must search for the ATI signature string: "M3      ". */
-	for (i = 4; i < R128_VBIOS_SIZE-8; i++) {
-	    if (R128_BIOS8(i)   == 'M' &&
-		R128_BIOS8(i+1) == '3' &&
-		R128_BIOS8(i+2) == ' ' &&
-		R128_BIOS8(i+3) == ' ' &&
-		R128_BIOS8(i+4) == ' ' &&
-		R128_BIOS8(i+5) == ' ' &&
-		R128_BIOS8(i+6) == ' ' &&
-		R128_BIOS8(i+7) == ' ') {
-		FPHeader = i-2;
-		break;
-	    }
-	}
+/* Read the FP parameters if an LVDS panel is expected. */
+void R128GetPanelInfoFromBIOS(xf86OutputPtr output)
+{
+    ScrnInfoPtr pScrn = output->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    R128OutputPrivatePtr r128_output = output->driver_private;
+    int FPHeader = 0;
+    int i;
 
-	if (!FPHeader) return TRUE;
+    r128_output->PanelPwrDly = 200;
+    xf86GetOptValInteger(info->Options, OPTION_PANEL_WIDTH,  &(r128_output->PanelXRes));
+    xf86GetOptValInteger(info->Options, OPTION_PANEL_HEIGHT, &(r128_output->PanelYRes));
+
+    if (!info->VBIOS) return;
+    info->FPBIOSstart = 0;
+
+    /* FIXME: There should be direct access to the start of the FP info
+     * tables, but until we find out where that offset is stored, we
+     * must search for the ATI signature string: "M3      ".
+     */
+    for (i = 4; i < R128_VBIOS_SIZE - 8; i++) {
+        if (R128_BIOS8(i)     == 'M' &&
+            R128_BIOS8(i + 1) == '3' &&
+            R128_BIOS8(i + 2) == ' ' &&
+            R128_BIOS8(i + 3) == ' ' &&
+            R128_BIOS8(i + 4) == ' ' &&
+            R128_BIOS8(i + 5) == ' ' &&
+            R128_BIOS8(i + 6) == ' ' &&
+            R128_BIOS8(i + 7) == ' ') {
+            FPHeader = i - 2;
+            break;
+        }
+    }
 
-	/* Assume that only one panel is attached and supported */
-	for (i = FPHeader+20; i < FPHeader+84; i += 2) {
-	    if (R128_BIOS16(i) != 0) {
-		info->FPBIOSstart = R128_BIOS16(i);
-		break;
-	    }
-	}
-	if (!info->FPBIOSstart) return TRUE;
+    if (!FPHeader) return;
 
-	if (!info->PanelXRes)
-	    info->PanelXRes = R128_BIOS16(info->FPBIOSstart+25);
-	if (!info->PanelYRes)
-	    info->PanelYRes = R128_BIOS16(info->FPBIOSstart+27);
-
-	info->PanelPwrDly = R128_BIOS8(info->FPBIOSstart+56);
-
-	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel ID: ");
-	for (i = 1; i <= 24; i++)
-	    ErrorF("%c", R128_BIOS8(info->FPBIOSstart+i));
-	ErrorF("\n");
-	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel Type: ");
-	i = R128_BIOS16(info->FPBIOSstart+29);
-	if (i & 1) ErrorF("Color, ");
-	else       ErrorF("Monochrome, ");
-	if (i & 2) ErrorF("Dual(split), ");
-	else       ErrorF("Single, ");
-	switch ((i >> 2) & 0x3f) {
-	case 0:  ErrorF("STN");        break;
-	case 1:  ErrorF("TFT");        break;
-	case 2:  ErrorF("Active STN"); break;
-	case 3:  ErrorF("EL");         break;
-	case 4:  ErrorF("Plasma");     break;
-	default: ErrorF("UNKNOWN");    break;
-	}
-	ErrorF("\n");
-	if (R128_BIOS8(info->FPBIOSstart+61) & 1) {
-	    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel Interface: LVDS\n");
-	} else {
-	    /* FIXME: Add Non-LVDS flat pael support */
-	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
-		       "Non-LVDS panel interface detected!  "
-		       "This support is untested and may not "
-		       "function properly\n");
-	}
+
+    /* Assume that only one panel is attached and supported */
+    for (i = FPHeader + 20; i < FPHeader + 84; i += 2) {
+        if (R128_BIOS16(i) != 0) {
+            info->FPBIOSstart = R128_BIOS16(i);
+            break;
+        }
     }
 
 #ifdef __NetBSD__
-    if (!info->PanelXRes || !info->PanelYRes) {
+    if (!r128_output->PanelXRes || !r128_output->PanelYRes) {
 	/*
 	 * we may not be on x86 so check wsdisplay for panel dimensions
 	 * XXX this assumes that the r128 is the console, although that should
@@ -721,13 +553,26 @@ static Bool R128GetBIOSParameters(ScrnIn
 	struct wsdisplay_fbinfo fbinfo;
 	
 	if (ioctl(xf86Info.screenFd, WSDISPLAYIO_GINFO, &fbinfo) == 0) {
-	    info->PanelXRes = fbinfo.width;
-	    info->PanelYRes = fbinfo.height;
+	    r128_output->PanelXRes = fbinfo.width;
+	    r128_output->PanelYRes = fbinfo.height;
 	}
     }
 #endif
 
-    if (!info->PanelXRes || !info->PanelYRes) {
+#ifndef AVOID_FBDEV
+    if (!info->FPBIOSstart) return;
+#endif
+
+    if (!r128_output->PanelXRes)
+        r128_output->PanelXRes = R128_BIOS16(info->FPBIOSstart + 25);
+    if (!r128_output->PanelYRes)
+        r128_output->PanelYRes = R128_BIOS16(info->FPBIOSstart + 27);
+    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel size: %dx%d\n",
+               r128_output->PanelXRes, r128_output->PanelYRes);
+
+    r128_output->PanelPwrDly = R128_BIOS8(info->FPBIOSstart + 56);
+
+    if (!r128_output->PanelXRes || !r128_output->PanelYRes) {
         info->HasPanelRegs = FALSE;
         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
 		   "Can't determine panel dimensions, and none specified.\n"
@@ -737,7 +582,39 @@ static Bool R128GetBIOSParameters(ScrnIn
 		   info->PanelXRes, info->PanelYRes);
     }
 
-    return TRUE;
+    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel ID: ");
+    for (i = 1; i <= 24; i++)
+        ErrorF("%c", R128_BIOS8(info->FPBIOSstart + i));
+
+    ErrorF("\n");
+
+    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel Type: ");
+    i = R128_BIOS16(info->FPBIOSstart + 29);
+    if (i & 1) ErrorF("Color, ");
+    else       ErrorF("Monochrome, ");
+    if (i & 2) ErrorF("Dual(split), ");
+    else       ErrorF("Single, ");
+
+    switch ((i >> 2) & 0x3f) {
+    case 0:  ErrorF("STN");        break;
+    case 1:  ErrorF("TFT");        break;
+    case 2:  ErrorF("Active STN"); break;
+    case 3:  ErrorF("EL");         break;
+    case 4:  ErrorF("Plasma");     break;
+    default: ErrorF("UNKNOWN");    break;
+    }
+
+    ErrorF("\n");
+
+    if (R128_BIOS8(info->FPBIOSstart + 61) & 1) {
+        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Panel Interface: LVDS\n");
+    } else {
+        /* FIXME: Add Non-LVDS flat pael support */
+        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
+                   "Non-LVDS panel interface detected!  "
+                   "This support is untested and may not "
+                   "function properly\n");
+    }
 }
 
 /* Read PLL parameters from BIOS block.  Default to typical values if there
@@ -763,15 +640,10 @@ static Bool R128GetPLLParameters(ScrnInf
     pll->min_pll_freq   = 12500;
     pll->max_pll_freq   = 25000;
 
-    /* need to memory map the io to use INPLL since it
-       has not been done yet at this point in the startup */
-    R128MapMMIO(pScrn);
     x_mpll_ref_fb_div = INPLL(pScrn, R128_X_MPLL_REF_FB_DIV);
     xclk_cntl = INPLL(pScrn, R128_XCLK_CNTL) & 0x7;
     pll->reference_div =
 	INPLL(pScrn,R128_PPLL_REF_DIV) & R128_PPLL_REF_DIV_MASK;
-    /* unmap it again */
-    R128UnmapMMIO(pScrn);
 
     Nx = (x_mpll_ref_fb_div & 0x00FF00) >> 8;
     M =  (x_mpll_ref_fb_div & 0x0000FF);
@@ -794,8 +666,8 @@ static Bool R128GetPLLParameters(ScrnInf
 	pll->max_pll_freq   = 25000;
 	pll->xclk           = 10300;
     } else {
-	CARD16 bios_header    = R128_BIOS16(0x48);
-	CARD16 pll_info_block = R128_BIOS16(bios_header + 0x30);
+	uint16_t bios_header    = R128_BIOS16(0x48);
+	uint16_t pll_info_block = R128_BIOS16(bios_header + 0x30);
 	R128TRACE(("Header at 0x%04x; PLL Information at 0x%04x\n",
 		   bios_header, pll_info_block));
 
@@ -908,6 +780,7 @@ static Bool R128PreInitWeight(ScrnInfoPt
 static Bool R128PreInitConfig(ScrnInfoPtr pScrn)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
+    R128EntPtr    pR128Ent  = R128EntPriv(pScrn);
     unsigned char *R128MMIO = info->MMIO;
     EntityInfoPtr pEnt      = info->pEnt;
     GDevPtr       dev       = pEnt->device;
@@ -1002,7 +875,7 @@ static Bool R128PreInitConfig(ScrnInfoPt
     } else {
         info->isDFP = FALSE;
         info->isPro2 = FALSE;
-        info->HasCRTC2 = FALSE;
+        pR128Ent->HasCRTC2 = FALSE;
 	switch (info->Chipset) {
 	/* R128 Pro and Pro2 can have DFP, we will deal with it.
 	   No support for dual-head/xinerama yet.
@@ -1056,7 +929,7 @@ static Bool R128PreInitConfig(ScrnInfoPt
 			info->HasPanelRegs = TRUE; 
 			info->isDFP = TRUE; 
 			/* which chips support dualhead? */
-			info->HasCRTC2 = TRUE;  
+			pR128Ent->HasCRTC2 = TRUE;
 			break;
 	case PCI_CHIP_RAGE128RE:
 	case PCI_CHIP_RAGE128RF:
@@ -1082,7 +955,7 @@ static Bool R128PreInitConfig(ScrnInfoPt
 
 				/* Read registers used to determine options */
     from                      = X_PROBED;
-    R128MapMMIO(pScrn);
+    if (!R128MapMMIO(pScrn)) return FALSE;
     R128MMIO                  = info->MMIO;
 
 #ifndef AVOID_FBDEV
@@ -1095,45 +968,6 @@ static Bool R128PreInitConfig(ScrnInfoPt
     info->MemCntl             = INREG(R128_MEM_CNTL);
     info->BusCntl             = INREG(R128_BUS_CNTL);
 
-    /* On non-flat panel systems, the default is to display to the CRT,
-       and on flat panel systems, the default is to display to the flat
-       panel unless the user explicity chooses otherwise using the "Display"
-       config file setting.  BIOS_5_SCRATCH holds the display device on flat
-       panel systems only. */
-    if (info->HasPanelRegs) {
-        char *Display = xf86GetOptValString(info->Options, OPTION_DISPLAY);
-
-#ifndef AVOID_FBDEV
-	if (info->FBDev)
-	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
-		     "Option \"Display\" ignored "
-		     "(framebuffer device determines display type)\n");
-	else
-#endif
-	 if (info->IsPrimary || info->IsSecondary)
-	    info->BIOSDisplay = R128_DUALHEAD;
-	else if (!Display || !xf86NameCmp(Display, "FP"))
-	    info->BIOSDisplay = R128_BIOS_DISPLAY_FP;
-	else if (!xf86NameCmp(Display, "BIOS"))
-	    info->BIOSDisplay = INREG8(R128_BIOS_5_SCRATCH);
-	else if (!xf86NameCmp(Display, "Mirror"))
-	    info->BIOSDisplay = R128_BIOS_DISPLAY_FP_CRT;
-	else if (!xf86NameCmp(Display, "CRT"))
-	    info->BIOSDisplay = R128_BIOS_DISPLAY_CRT;
-	else {
-	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
-		"Unsupported type \"%s\" specified for Option \"Display\".\n"
-		"\tSupported types are: "
-		"\"BIOS\", \"Mirror\", \"CRT\" and \"FP\"\n", Display);
-	    return FALSE;
-	}
-    } else {
-	info->BIOSDisplay     = R128_BIOS_DISPLAY_CRT;
-    }
-
-    R128MMIO                  = NULL;
-    R128UnmapMMIO(pScrn);
-
 				/* RAM */
     switch (info->MemCntl & 0x3) {
     case 0:                     /* SDR SGRAM 1:1 */
@@ -1172,62 +1006,9 @@ static Bool R128PreInitConfig(ScrnInfoPt
     xf86DrvMsg(pScrn->scrnIndex, from,
 	       "VideoRAM: %d kByte (%s)\n", pScrn->videoRam, info->ram->name);
 
-    if (info->IsPrimary) {
-        pScrn->videoRam /= 2;
-	xf86DrvMsg(pScrn->scrnIndex, X_INFO, 
-		"Using %dk of videoram for primary head\n",
-		pScrn->videoRam);
-    }
-
-    if (info->IsSecondary) {  
-        pScrn->videoRam /= 2;
-        info->LinearAddr += pScrn->videoRam * 1024;
-	xf86DrvMsg(pScrn->scrnIndex, X_INFO, 
-		"Using %dk of videoram for secondary head\n",
-		pScrn->videoRam);
-    }
-
     pScrn->videoRam  &= ~1023;
     info->FbMapSize  = pScrn->videoRam * 1024;
 
-
-				/* Flat panel (part 2) */
-	switch (info->BIOSDisplay) {
-	case R128_DUALHEAD:
-	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
-		       "Dual display\n");
-	    break;
-	case R128_BIOS_DISPLAY_FP:
-	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
-		       "Using flat panel for display\n");
-	    break;
-	case R128_BIOS_DISPLAY_CRT:
-	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
-		       "Using external CRT for display\n");
-	    break;
-	case R128_BIOS_DISPLAY_FP_CRT:
-	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
-		       "Using both flat panel and external CRT "
-		       "for display\n");
-	    break;
-	}
-
-    if (info->HasPanelRegs) {
-				/* Panel width/height overrides */
-	info->PanelXRes = 0;
-	info->PanelYRes = 0;
-	if (xf86GetOptValInteger(info->Options,
-				 OPTION_PANEL_WIDTH, &(info->PanelXRes))) {
-	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
-		       "Flat panel width: %d\n", info->PanelXRes);
-	}
-	if (xf86GetOptValInteger(info->Options,
-				 OPTION_PANEL_HEIGHT, &(info->PanelYRes))) {
-	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
-		       "Flat panel height: %d\n", info->PanelYRes);
-	}
-    }
-
 #ifdef R128DRI
 				/* DMA for Xv */
     info->DMAForXv = xf86ReturnOptValBool(info->Options, OPTION_XV_DMA, FALSE);
@@ -1308,6 +1089,7 @@ static Bool R128PreInitDDC(ScrnInfoPtr p
 #endif
 
     if (!xf86LoadSubModule(pScrn, "ddc")) return FALSE;
+    if (!xf86LoadSubModule(pScrn, "i2c")) return FALSE;
 
 #if defined(__powerpc__) || defined(__alpha__) || defined(__sparc__)
     /* Int10 is broken on PPC and some Alphas */
@@ -1333,492 +1115,6 @@ static Bool R128PreInitGamma(ScrnInfoPtr
     return TRUE;
 }
 
-static void
-R128I2CGetBits(I2CBusPtr b, int *Clock, int *data)
-{
-    ScrnInfoPtr   pScrn       = xf86Screens[b->scrnIndex];
-    R128InfoPtr info = R128PTR(pScrn);
-    unsigned long val;
-    unsigned char *R128MMIO = info->MMIO;
-
-    /* Get the result. */
-    val = INREG(info->DDCReg);
-    *Clock = (val & R128_GPIO_MONID_Y_3) != 0;
-    *data  = (val & R128_GPIO_MONID_Y_0) != 0;
-
-}
-
-static void
-R128I2CPutBits(I2CBusPtr b, int Clock, int data)
-{
-    ScrnInfoPtr   pScrn       = xf86Screens[b->scrnIndex];
-    R128InfoPtr info = R128PTR(pScrn);
-    unsigned long val;
-    unsigned char *R128MMIO = info->MMIO;
-
-    val = INREG(info->DDCReg)
-              & ~(CARD32)(R128_GPIO_MONID_EN_0 | R128_GPIO_MONID_EN_3);
-    val |= (Clock ? 0:R128_GPIO_MONID_EN_3);
-    val |= (data ? 0:R128_GPIO_MONID_EN_0);
-    OUTREG(info->DDCReg, val);
-}
-
-
-static Bool
-R128I2cInit(ScrnInfoPtr pScrn)
-{
-    R128InfoPtr info = R128PTR(pScrn);
-    if ( !xf86LoadSubModule(pScrn, "i2c") ) {
-        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
-            "Failed to load i2c module\n");
-		return FALSE;
-    }
-
-    info->pI2CBus = xf86CreateI2CBusRec();
-    if(!info->pI2CBus) return FALSE;
-
-    info->pI2CBus->BusName    = "DDC";
-    info->pI2CBus->scrnIndex  = pScrn->scrnIndex;
-    info->DDCReg = R128_GPIO_MONID;
-    info->pI2CBus->I2CPutBits = R128I2CPutBits;
-    info->pI2CBus->I2CGetBits = R128I2CGetBits;
-    info->pI2CBus->AcknTimeout = 5;
-
-    if (!xf86I2CBusInit(info->pI2CBus)) {
-        return FALSE;
-    }
-    return TRUE;
-}
-
-/* XXX
- * the following comment is bogus - the code scans the main monitor port,
- * not R128_GPIO_MONIDB
- */
-/* return TRUE is a DFP is indeed connected to a DVI port */
-static Bool R128GetDFPInfo(ScrnInfoPtr pScrn)
-{
-    R128InfoPtr info  = R128PTR(pScrn);
-    int i;
-    xf86MonPtr MonInfo = NULL;
-    xf86MonPtr ddc;
-    unsigned char *R128MMIO = info->MMIO;
-
-    if(!R128I2cInit(pScrn)){
-        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
-                  "I2C initialization failed!\n");
-    }
-
-    OUTREG(info->DDCReg, (INREG(info->DDCReg)
-           | R128_GPIO_MONID_MASK_0 | R128_GPIO_MONID_MASK_3));
-
-    OUTREG(info->DDCReg, INREG(info->DDCReg)
-           & ~(CARD32)(R128_GPIO_MONID_A_0 | R128_GPIO_MONID_A_3));
-
-    MonInfo = xf86DoEDID_DDC2(XF86_SCRN_ARG(pScrn), info->pI2CBus);
-#ifdef __NetBSD__
-    if (MonInfo == NULL) {
-    	struct wsdisplayio_edid_info ei;
-    	char buffer[1024];
-    	int i, j;
-
-	ei.edid_data = buffer;
-	ei.buffer_size = 1024;
-	if (ioctl(xf86Info.screenFd, WSDISPLAYIO_GET_EDID, &ei) != -1) {
-	    xf86Msg(X_INFO, "got %d bytes worth of EDID from wsdisplay\n", ei.data_size);
-	    MonInfo = xf86InterpretEDID(pScrn->scrnIndex, buffer);
-	} else
-	    xf86Msg(X_INFO, "ioctl failed %d\n", errno);
-    }
-#endif
-		
-    if(!MonInfo) {
-        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
-                   "No DFP detected\n");
-        return FALSE;
-    }
-    xf86SetDDCproperties(pScrn, MonInfo);
-    ddc = pScrn->monitor->DDC;
-
-    for(i=0; i<4; i++)
-    {
-        if((ddc->det_mon[i].type == 0) &&
-	  (ddc->det_mon[i].section.d_timings.h_active > 0) &&
-	  (ddc->det_mon[i].section.d_timings.v_active > 0))
-        {
-            info->PanelXRes =
-                ddc->det_mon[i].section.d_timings.h_active;
-            info->PanelYRes =
-                ddc->det_mon[i].section.d_timings.v_active;
-
-            info->HOverPlus =
-                ddc->det_mon[i].section.d_timings.h_sync_off;
-            info->HSyncWidth =
-                ddc->det_mon[i].section.d_timings.h_sync_width;
-            info->HBlank =
-                ddc->det_mon[i].section.d_timings.h_blanking;
-            info->VOverPlus =
-                ddc->det_mon[i].section.d_timings.v_sync_off;
-            info->VSyncWidth =
-                ddc->det_mon[i].section.d_timings.v_sync_width;
-            info->VBlank =
-                ddc->det_mon[i].section.d_timings.v_blanking;
-        }
-    }
-    return TRUE;
-}
-
-
-static void R128SetSyncRangeFromEdid(ScrnInfoPtr pScrn, int flag)
-{
-    int i;
-    xf86MonPtr ddc = pScrn->monitor->DDC;
-    if(flag)  /*HSync*/
-    {
-        for(i=0; i<4; i++)
-        {
-            if(ddc->det_mon[i].type == DS_RANGES)
-            {
-                pScrn->monitor->nHsync = 1;
-                pScrn->monitor->hsync[0].lo =
-                    ddc->det_mon[i].section.ranges.min_h;
-                pScrn->monitor->hsync[0].hi =
-                    ddc->det_mon[i].section.ranges.max_h;
-                return;
-            }
-        }
-        /*if no sync ranges detected in detailed timing table,
-          let's try to derive them from supported VESA modes
-          Are we doing too much here!!!?
-        **/
-        i = 0;
-        if(ddc->timings1.t1 & 0x02) /*800x600@56*/
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 35.2;
-            i++;
-        }
-        if(ddc->timings1.t1 & 0x04) /*640x480@75*/
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 37.5;
-            i++;
-        }
-        if((ddc->timings1.t1 & 0x08) || (ddc->timings1.t1 & 0x01))
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 37.9;
-            i++;
-        }
-        if(ddc->timings1.t2 & 0x40)
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 46.9;
-            i++;
-        }
-        if((ddc->timings1.t2 & 0x80) || (ddc->timings1.t2 & 0x08))
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 48.1;
-            i++;
-        }
-        if(ddc->timings1.t2 & 0x04)
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 56.5;
-            i++;
-        }
-        if(ddc->timings1.t2 & 0x02)
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 60.0;
-            i++;
-        }
-        if(ddc->timings1.t2 & 0x01)
-        {
-            pScrn->monitor->hsync[i].lo =
-                pScrn->monitor->hsync[i].hi = 64.0;
-            i++;
-        }
-        pScrn->monitor->nHsync = i;
-    }
-    else      /*Vrefresh*/
-    {
-        for(i=0; i<4; i++)
-        {
-            if(ddc->det_mon[i].type == DS_RANGES)
-            {
-                pScrn->monitor->nVrefresh = 1;
-                pScrn->monitor->vrefresh[0].lo =
-                    ddc->det_mon[i].section.ranges.min_v;
-                pScrn->monitor->vrefresh[0].hi =
-                    ddc->det_mon[i].section.ranges.max_v;
-                return;
-            }
-        }
-        i = 0;
-        if(ddc->timings1.t1 & 0x02) /*800x600@56*/
-        {
-            pScrn->monitor->vrefresh[i].lo =
-                pScrn->monitor->vrefresh[i].hi = 56;
-            i++;
-        }
-        if((ddc->timings1.t1 & 0x01) || (ddc->timings1.t2 & 0x08))
-        {
-            pScrn->monitor->vrefresh[i].lo =
-                pScrn->monitor->vrefresh[i].hi = 60;
-            i++;
-        }
-        if(ddc->timings1.t2 & 0x04)
-        {
-            pScrn->monitor->vrefresh[i].lo =
-                pScrn->monitor->vrefresh[i].hi = 70;
-            i++;
-        }
-        if((ddc->timings1.t1 & 0x08) || (ddc->timings1.t2 & 0x80))
-        {
-            pScrn->monitor->vrefresh[i].lo =
-                pScrn->monitor->vrefresh[i].hi = 72;
-            i++;
-        }
-        if((ddc->timings1.t1 & 0x04) || (ddc->timings1.t2 & 0x40)
-           || (ddc->timings1.t2 & 0x02) || (ddc->timings1.t2 & 0x01))
-        {
-            pScrn->monitor->vrefresh[i].lo =
-                pScrn->monitor->vrefresh[i].hi = 75;
-            i++;
-        }
-        pScrn->monitor->nVrefresh = i;
-    }
-}
-
-/***********
-   free's xf86ValidateModes routine deosn't work well with DFPs
-   here is our own validation routine. All modes between
-   640<=XRes<=MaxRes and 480<=YRes<=MaxYRes will be permitted.
-   NOTE: RageProII doesn't support rmx, can only work with the
-         standard modes the monitor can support (scale).
-************/
-
-static int R128ValidateFPModes(ScrnInfoPtr pScrn)
-{
-    int i, j, count=0, width, height;
-    R128InfoPtr info = R128PTR(pScrn);
-    DisplayModePtr last = NULL, new = NULL, first = NULL;
-    xf86MonPtr ddc;
-
-    /* Free any allocated modes during configuration. We don't need them*/
-    while (pScrn->modes)
-    {
-	    xf86DeleteMode(&pScrn->modes, pScrn->modes);
-    }
-    while (pScrn->modePool)
-    {
-	    xf86DeleteMode(&pScrn->modePool, pScrn->modePool);
-    }
-
-    pScrn->virtualX = pScrn->display->virtualX;
-    pScrn->virtualY = pScrn->display->virtualY;
-
-    /* If no mode specified in config, we use native resolution*/
-    if(!pScrn->display->modes[0])
-    {
-        pScrn->display->modes[0] = xnfalloc(32);
-        snprintf(pScrn->display->modes[0], 32, "%dx%d",
-               info->PanelXRes, info->PanelYRes);
-        /* don't forget to NULL terminate */
-        pScrn->display->modes[1] = NULL;
-    }
-
-    for(i=0; pScrn->display->modes[i] != NULL; i++)
-    {
-        if (sscanf(pScrn->display->modes[i], "%dx%d", &width, &height) == 2)
-        {
-
-            if(width < 640 || width > info->PanelXRes ||
-               height < 480 || height > info->PanelYRes)
-            {
-                xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
-                    "Mode %s is out of range.\n"
-                    "Valid mode should be between 640x480-%dx%d\n",
-                    pScrn->display->modes[i], info->PanelXRes, info->PanelYRes);
-                continue;
-            }
-
-            new = xnfcalloc(1, sizeof(DisplayModeRec));
-            new->prev = last;
-            new->name = xnfalloc(strlen(pScrn->display->modes[i]) + 1);
-            strcpy(new->name, pScrn->display->modes[i]);
-            new->HDisplay = new->CrtcHDisplay = width;
-            new->VDisplay = new->CrtcVDisplay = height;
-
-            ddc = pScrn->monitor->DDC;
-            for(j=0; j<DET_TIMINGS; j++)
-            {
-                /*We use native mode clock only*/
-                if(ddc->det_mon[j].type == 0){
-                    new->Clock = ddc->det_mon[j].section.d_timings.clock / 1000;
-                    break;
-                }
-            }
-
-            if(new->prev) new->prev->next = new;
-            last = new;
-            if(!first) first = new;
-            pScrn->display->virtualX =
-            pScrn->virtualX = MAX(pScrn->virtualX, width);
-            pScrn->display->virtualY =
-            pScrn->virtualY = MAX(pScrn->virtualY, height);
-            count++;
-        }
-        else
-        {
-            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
-                "Mode name %s is invalid\n", pScrn->display->modes[i]);
-            continue;
-        }
-   }
-
-   if(last)
-   {
-       last->next = first;
-       first->prev = last;
-       pScrn->modes = first;
-
-       /*FIXME: May need to validate line pitch here*/
-       {
-           int dummy = 0;
-           switch(pScrn->depth / 8)
-           {
-              case 1:
-                  dummy = 128 - pScrn->virtualX % 128;
-                  break;
-              case 2:
-                  dummy = 32 - pScrn->virtualX % 32;
-                  break;
-              case 3:
-              case 4:
-                  dummy = 16 - pScrn->virtualX % 16;
-           }
-           pScrn->displayWidth = pScrn->virtualX + dummy;
-       }
-
-   }
-
-   return count;
-}
-
-
-/* This is called by R128PreInit to validate modes and compute parameters
-   for all of the valid modes. */
-static Bool R128PreInitModes(ScrnInfoPtr pScrn)
-{
-    R128InfoPtr   info = R128PTR(pScrn);
-    ClockRangePtr clockRanges;
-    int           modesFound;
-
-    if(info->isDFP) {
-        R128MapMem(pScrn);
-        info->BIOSDisplay = R128_BIOS_DISPLAY_FP;
-        /* validate if DFP really connected. */
-        if(!R128GetDFPInfo(pScrn)) {
-            info->isDFP = FALSE;
-            info->BIOSDisplay = R128_BIOS_DISPLAY_CRT;
-        } else if(!info->isPro2) {
-            /* RageProII doesn't support rmx, we can't use native-mode
-               stretching for other non-native modes. It will rely on
-               whatever VESA modes monitor can support. */
-            modesFound = R128ValidateFPModes(pScrn);
-            if(modesFound < 1) {
-                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
-                     "No valid mode found for this DFP/LCD\n");
-                 R128UnmapMem(pScrn);
-                 return FALSE;
-
-            }
-        }
-        R128UnmapMem(pScrn);
-    }
-
-    if(!info->isDFP || info->isPro2) {
-				/* Get mode information */
-        pScrn->progClock                   = TRUE;
-        clockRanges                        = xnfcalloc(sizeof(*clockRanges), 1);
-        clockRanges->next                  = NULL;
-        clockRanges->minClock              = info->pll.min_pll_freq;
-        clockRanges->maxClock              = info->pll.max_pll_freq * 10;
-        clockRanges->clockIndex            = -1;
-        if (info->HasPanelRegs || info->isDFP) {
-            clockRanges->interlaceAllowed  = FALSE;
-            clockRanges->doubleScanAllowed = FALSE;
-        } else {
-            clockRanges->interlaceAllowed  = TRUE;
-            clockRanges->doubleScanAllowed = TRUE;
-        }
-
-        if(pScrn->monitor->DDC) {
-        /*if we still don't know sync range yet, let's try EDID.
-          Note that, since we can have dual heads, the Xconfigurator
-          may not be able to probe both monitors correctly through
-          vbe probe function (R128ProbeDDC). Here we provide an
-          additional way to auto-detect sync ranges if they haven't
-          been added to XF86Config manually.
-        **/
-            if(pScrn->monitor->nHsync <= 0)
-                R128SetSyncRangeFromEdid(pScrn, 1);
-            if(pScrn->monitor->nVrefresh <= 0)
-                R128SetSyncRangeFromEdid(pScrn, 0);
-        }
-
-        modesFound = xf86ValidateModes(pScrn,
-				   pScrn->monitor->Modes,
-				   pScrn->display->modes,
-				   clockRanges,
-				   NULL,        /* linePitches */
-				   8 * 64,      /* minPitch */
-				   8 * 1024,    /* maxPitch */
-/*
- * ATI docs say pitchInc must be 8 * 64, but this doesn't permit a pitch of
- * 800 bytes, which is known to work on the Rage128 LF on clamshell iBooks
- */
-				   8 * 32,      /* pitchInc */
-				   128,         /* minHeight */
-				   2048,        /* maxHeight */
-				   pScrn->display->virtualX,
-				   pScrn->display->virtualY,
-				   info->FbMapSize,
-				   LOOKUP_BEST_REFRESH);
-#ifndef AVOID_FBDEV
-        if (modesFound < 1 && info->FBDev) {
-		fbdevHWUseBuildinMode(pScrn);
-		pScrn->displayWidth = fbdevHWGetLineLength(pScrn) /
-		    (pScrn->bitsPerPixel / 8);
-		modesFound = 1;
-        }
-#endif
-
-        if (modesFound == -1) return FALSE;
-        xf86PruneDriverModes(pScrn);
-        if (!modesFound || !pScrn->modes) {
-            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes found\n");
-            return FALSE;
-        }
-        xf86SetCrtcForModes(pScrn, 0);
-    }
-				/* Set DPI */
-    pScrn->currentMode = pScrn->modes;
-    xf86PrintModes(pScrn);
-
-    xf86SetDpi(pScrn, 0, 0);
-
-				/* Get ScreenInit function */
-    if (!xf86LoadSubModule(pScrn, "fb")) return FALSE;
-
-    info->CurrentLayout.displayWidth = pScrn->displayWidth;
-    info->CurrentLayout.mode = pScrn->currentMode;
-
-    return TRUE;
-}
-
 /* This is called by R128PreInit to initialize the hardware cursor. */
 static Bool R128PreInitCursor(ScrnInfoPtr pScrn)
 {
@@ -1833,8 +1129,8 @@ static Bool R128PreInitCursor(ScrnInfoPt
 static Bool R128PreInitInt10(ScrnInfoPtr pScrn, xf86Int10InfoPtr *ppInt10)
 {
     R128InfoPtr   info = R128PTR(pScrn);
-#if 1 && !defined(__alpha__)
-    /* int10 is broken on some Alphas */
+#if !defined(__powerpc__) && !defined(__alpha__)
+    /* int10 is broken on some Alphas and powerpc */
     if (xf86LoadSubModule(pScrn, "int10")) {
 	xf86DrvMsg(pScrn->scrnIndex,X_INFO,"initializing int10\n");
 	*ppInt10 = xf86InitInt10(info->pEnt->index);
@@ -1961,17 +1257,59 @@ static Bool R128PreInitDRI(ScrnInfoPtr p
 }
 #endif
 
+static Bool R128PreInitControllers(ScrnInfoPtr pScrn, xf86Int10InfoPtr pInt10)
+{
+    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
+    int found = 0;
+    int i;
+
+    if (!R128GetBIOSParameters(pScrn, pInt10))
+        return FALSE;
+
+    if (!R128GetPLLParameters(pScrn))
+        return FALSE;
+
+    if (!R128AllocateControllers(pScrn))
+        return FALSE;
+
+    if (!R128SetupConnectors(pScrn))
+        return FALSE;
+
+    for (i = 0; i < config->num_output; i++) {
+        xf86OutputPtr output = config->output[i];
+
+        output->status = (*output->funcs->detect) (output);
+        if (output->status == XF86OutputStatusConnected)
+            found++;
+    }
+    return !!found;
+}
+
 static void
 R128ProbeDDC(ScrnInfoPtr pScrn, int indx)
 {
     vbeInfoPtr pVbe;
+
+#if !defined(__powerpc__) && !defined(__alpha__) && !defined(__sparc__)
     if (xf86LoadSubModule(pScrn, "vbe")) {
 	pVbe = VBEInit(NULL,indx);
 	ConfiguredMonitor = vbeDoEDID(pVbe, NULL);
 	vbeFree(pVbe);
     }
+#endif
+}
+
+static Bool R128CRTCResize(ScrnInfoPtr pScrn, int width, int height)
+{
+    pScrn->virtualX = width;
+    pScrn->virtualY = height;
+    return TRUE;
 }
 
+static const xf86CrtcConfigFuncsRec R128CRTCResizeFuncs = {
+    R128CRTCResize
+};
+
 /* R128PreInit is called once at server startup. */
 Bool R128PreInit(ScrnInfoPtr pScrn, int flags)
 {
@@ -1984,55 +1322,19 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
 
     if (!R128GetRec(pScrn)) return FALSE;
 
-    info               = R128PTR(pScrn);
-
-    info->IsSecondary  = FALSE;
-    info->IsPrimary = FALSE;
+    info                = R128PTR(pScrn);
     info->SwitchingMode = FALSE;
+    info->MMIO          = NULL;
 
-    info->pEnt         = xf86GetEntityInfo(pScrn->entityList[0]);
+    info->pEnt          = xf86GetEntityInfo(pScrn->entityList[0]);
     if (info->pEnt->location.type != BUS_PCI) goto fail;
 
-    if(xf86IsEntityShared(pScrn->entityList[0]))
-    {
-        if(xf86IsPrimInitDone(pScrn->entityList[0]))
-        {
-            DevUnion* pPriv;
-            R128EntPtr pR128Ent;
-            info->IsSecondary = TRUE;
-            pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                    getR128EntityIndex());
-            pR128Ent = pPriv->ptr;
-            if(pR128Ent->BypassSecondary) return FALSE;
-            pR128Ent->pSecondaryScrn = pScrn;
-        }
-        else
-        {
-            DevUnion* pPriv;
-            R128EntPtr pR128Ent;
-	    info->IsPrimary = TRUE;
-            xf86SetPrimInitDone(pScrn->entityList[0]);
-            pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                    getR128EntityIndex());
-            pR128Ent = pPriv->ptr;
-            pR128Ent->pPrimaryScrn = pScrn;
-            pR128Ent->IsDRIEnabled = FALSE;
-            pR128Ent->BypassSecondary = FALSE;
-            pR128Ent->HasSecondary = FALSE;
-            pR128Ent->RestorePrimary = FALSE;
-            pR128Ent->IsSecondaryRestored = FALSE;
-        }
-    }
-
     if (flags & PROBE_DETECT) {
 	R128ProbeDDC(pScrn, info->pEnt->index);
 	return TRUE;
     }
 
-    info->PciInfo      = xf86GetPciInfoForEntity(info->pEnt->index);
-    info->PciTag       = pciTag(PCI_DEV_BUS(info->PciInfo),
-				PCI_DEV_DEV(info->PciInfo),
-				PCI_DEV_FUNC(info->PciInfo));
+    info->PciInfo       = xf86GetPciInfoForEntity(info->pEnt->index);
 
     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 	       "PCI bus %d card %d func %d\n",
@@ -2041,12 +1343,19 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
 	       PCI_DEV_FUNC(info->PciInfo));
 
 #ifndef XSERVER_LIBPCIACCESS
+    info->PciTag        = pciTag(PCI_DEV_BUS(info->PciInfo),
+				PCI_DEV_DEV(info->PciInfo),
+				PCI_DEV_FUNC(info->PciInfo));
+
     if (xf86RegisterResources(info->pEnt->index, 0, ResNone)) goto fail;
     if (xf86SetOperatingState(resVga, info->pEnt->index, ResUnusedOpr)) goto fail;
 
-    pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_VIEWPORT | RAC_CURSOR;
+    pScrn->racMemFlags  = RAC_FB | RAC_COLORMAP | RAC_VIEWPORT | RAC_CURSOR;
 #endif
-    pScrn->monitor     = pScrn->confScreen->monitor;
+    pScrn->monitor      = pScrn->confScreen->monitor;
+
+    /* Allocate an xf86CrtcConfig */
+    xf86CrtcConfigInit(pScrn, &R128CRTCResizeFuncs);
 
     if (!R128PreInitVisual(pScrn))    goto fail;
 
@@ -2088,8 +1397,6 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
                "module load skipped\n");
 #endif
 
-
-
     if (!R128PreInitWeight(pScrn))    goto fail;
 
     if(xf86GetOptValInteger(info->Options, OPTION_VIDEO_KEY, &(info->videoKey))) {
@@ -2127,20 +1434,30 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
 
     if (!info->FBDev)
 #endif /* !AVOID_FBDEV */
-	if (!R128PreInitInt10(pScrn, &pInt10)) goto fail;
-
-    if (!R128PreInitConfig(pScrn))             goto fail;
+	if (!R128PreInitInt10(pScrn, &pInt10))  goto fail;
 
-    if (!R128GetBIOSParameters(pScrn, pInt10)) goto fail;
+    if (!R128PreInitConfig(pScrn))              goto fail;
 
-    if (!R128GetPLLParameters(pScrn))          goto fail;
+    xf86CrtcSetSizeRange(pScrn, 320, 200, 4096, 4096);
 
     /* Don't fail on this one */
-    R128PreInitDDC(pScrn, pInt10);
+    info->DDC = R128PreInitDDC(pScrn, pInt10);
 
-    if (!R128PreInitGamma(pScrn))              goto fail;
+    if (!R128PreInitControllers(pScrn, pInt10)) goto fail;
 
-    if (!R128PreInitModes(pScrn))              goto fail;
+    if (!xf86InitialConfiguration(pScrn, TRUE)) {
+        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes.\n");
+        goto fail;
+    }
+    pScrn->displayWidth = (pScrn->virtualX + 63) & ~63;
+
+    /* Set display resolution */
+    xf86SetDpi(pScrn, 0, 0);
+
+    /* Get ScreenInit function */
+    if (!xf86LoadSubModule(pScrn, "fb")) return FALSE;
+
+    if (!R128PreInitGamma(pScrn))              goto fail;
 
     if (!R128PreInitCursor(pScrn))             goto fail;
 
@@ -2148,6 +1465,18 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
     if (!R128PreInitDRI(pScrn))                goto fail;
 #endif
 
+    info->CurrentLayout.displayWidth = pScrn->displayWidth;
+
+    if (!xf86RandR12PreInit(pScrn)) {
+        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "RandR initialization failure\n");
+        goto fail;
+    }
+
+    if (pScrn->modes == NULL) {
+        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
+        goto fail;
+    }
+
 				/* Free the video bios (if applicable) */
     if (info->VBIOS) {
 	free(info->VBIOS);
@@ -2158,6 +1487,9 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
     if (pInt10)
 	xf86FreeInt10(pInt10);
 
+    if (info->MMIO) R128UnmapMMIO(pScrn);
+    info->MMIO = NULL;
+
     xf86DrvMsg(pScrn->scrnIndex, X_NOTICE,
 	"For information on using the multimedia capabilities\n\tof this"
 	" adapter, please see http://gatos.sf.net.\n";);
@@ -2181,6 +1513,10 @@ Bool R128PreInit(ScrnInfoPtr pScrn, int 
     if (info->VGAAccess)
            vgaHWFreeHWRec(pScrn);
 #endif
+
+    if (info->MMIO) R128UnmapMMIO(pScrn);
+    info->MMIO = NULL;
+
     R128FreeRec(pScrn);
     return FALSE;
 }
@@ -2190,56 +1526,66 @@ static void R128LoadPalette(ScrnInfoPtr 
 			    int *indices, LOCO *colors, VisualPtr pVisual)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
-    unsigned char *R128MMIO = info->MMIO;
-    int           i, j;
-    int           idx;
-    unsigned char r, g, b;
-
-    /* If the second monitor is connected, we also 
-       need to deal with the secondary palette*/
-    if (info->IsSecondary) j = 1;
-    else j = 0;
-    
-    PAL_SELECT(j);
-
+    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
+    int i, j;
+    int c, index;
+    uint16_t lut_r[256], lut_g[256], lut_b[256];
+
+    for (c = 0; c < xf86_config->num_crtc; c++) {
+        xf86CrtcPtr crtc = xf86_config->crtc[c];
+        R128CrtcPrivatePtr r128_crtc = crtc->driver_private;
+
+        for (i = 0 ; i < 256; i++) {
+            lut_r[i] = r128_crtc->lut_r[i] << 8;
+            lut_g[i] = r128_crtc->lut_g[i] << 8;
+            lut_b[i] = r128_crtc->lut_b[i] << 8;
+        }
+
+        switch (info->CurrentLayout.depth) {
+        case 15:
+            for (i = 0; i < numColors; i++) {
+                index = indices[i];
+                for (j = 0; j < 8; j++) {
+                    lut_r[index * 8 + j] = colors[index].red << 8;
+                    lut_g[index * 8 + j] = colors[index].green << 8;
+                    lut_b[index * 8 + j] = colors[index].blue << 8;
+                }
+            }
+        case 16:
+            for (i = 0; i < numColors; i++) {
+                index = indices[i];
+
+                /* XXX: The old version of R128LoadPalette did not do this and
+                 * the old version of RADEONLoadPalette has a comment asking why.
+                 */
+                if (i <= 31) {
+                    for (j = 0; j < 8; j++) {
+                        lut_r[index * 8 + j] = colors[index].red << 8;
+                        lut_b[index * 8 + j] = colors[index].blue << 8;
+                    }
+                }
 
-    /* Select palette 0 (main CRTC) if using FP-enabled chip */
-    /*if (info->HasPanelRegs || info->isDFP) PAL_SELECT(0);*/
+                for (j = 0; j < 4; j++) {
+                    lut_g[index * 4 + j] = colors[index].green << 8;
+                }
+            }
+        default:
+            for (i = 0; i < numColors; i++) {
+                index = indices[i];
+                lut_r[index] = colors[index].red << 8;
+                lut_g[index] = colors[index].green << 8;
+                lut_b[index] = colors[index].blue << 8;
+            }
+            break;
+        }
 
-    if (info->CurrentLayout.depth == 15) {
-	/* 15bpp mode.  This sends 32 values. */
-	for (i = 0; i < numColors; i++) {
-	    idx = indices[i];
-	    r   = colors[idx].red;
-	    g   = colors[idx].green;
-	    b   = colors[idx].blue;
-	    OUTPAL(idx * 8, r, g, b);
-	}
-    }
-    else if (info->CurrentLayout.depth == 16) {
-	/* 16bpp mode.  This sends 64 values. */
-				/* There are twice as many green values as
-				   there are values for red and blue.  So,
-				   we take each red and blue pair, and
-				   combine it with each of the two green
-				   values. */
-	for (i = 0; i < numColors; i++) {
-	    idx = indices[i];
-	    r   = colors[idx / 2].red;
-	    g   = colors[idx].green;
-	    b   = colors[idx / 2].blue;
-	    OUTPAL(idx * 4, r, g, b);
-	}
-    }
-    else {
-	/* 8bpp mode.  This sends 256 values. */
-	for (i = 0; i < numColors; i++) {
-	    idx = indices[i];
-	    r   = colors[idx].red;
-	    b   = colors[idx].blue;
-	    g   = colors[idx].green;
-	    OUTPAL(idx, r, g, b);
-	}
+        /* Make the change through RandR */
+#ifdef RANDR_12_INTERFACE
+        if (crtc->randr_crtc)
+            RRCrtcGammaSet(crtc->randr_crtc, lut_r, lut_g, lut_b);
+        else
+#endif
+        crtc->funcs->gamma_set(crtc, lut_r, lut_g, lut_b, 256);
     }
 }
 
@@ -2359,7 +1705,7 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
 
     if (!R128MapMem(pScrn)) return FALSE;
     pScrn->fbOffset    = 0;
-    if(info->IsSecondary) pScrn->fbOffset = pScrn->videoRam * 1024;
+    //if(info->IsSecondary) pScrn->fbOffset = pScrn->videoRam * 1024;
 #ifdef R128DRI
     info->fbX          = 0;
     info->fbY          = 0;
@@ -2373,9 +1719,10 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
 #ifndef AVOID_FBDEV
     if (info->FBDev) {
 	if (!fbdevHWModeInit(pScrn, pScrn->currentMode)) return FALSE;
-    } else 
+    } else {
 #endif
 	if (!R128ModeInit(pScrn, pScrn->currentMode)) return FALSE;
+    }
 
     R128SaveScreen(pScreen, SCREEN_SAVER_ON);
     pScrn->AdjustFrame(ADJUST_FRAME_ARGS(pScrn, pScrn->frameX0, pScrn->frameY0));
@@ -2389,6 +1736,7 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
     miSetPixmapDepths ();
 
     noAccel = xf86ReturnOptValBool(info->Options, OPTION_NOACCEL, FALSE);
+    if (noAccel) info->useEXA = FALSE;
 
 #ifdef R128DRI
 				/* Setup DRI after visuals have been
@@ -2414,33 +1762,7 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
 			info->CurrentLayout.pixel_bytes * 3 + 1023) / 1024);
 	    info->directRenderingEnabled = FALSE;
 	} else {
-            if(info->IsSecondary)
-                info->directRenderingEnabled = FALSE;
-            else 
-            {
-                /* Xinerama has sync problem with DRI, disable it for now */
-                if(xf86IsEntityShared(pScrn->entityList[0]))
-                {
-                    info->directRenderingEnabled = FALSE;
- 	            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
-                        "Direct Rendering Disabled -- "
-                        "Dual-head configuration is not working with DRI "
-                        "at present.\nPlease use only one Device/Screen "
-                        "section in your XFConfig file.\n");
-                }
-                else
-                info->directRenderingEnabled =
-                    R128DRIScreenInit(pScreen);
-                if(xf86IsEntityShared(pScrn->entityList[0]))
-                {
-                    DevUnion* pPriv;
-                    R128EntPtr pR128Ent;
-                    pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                        getR128EntityIndex());
-                    pR128Ent = pPriv->ptr;
-                    pR128Ent->IsDRIEnabled = info->directRenderingEnabled;
-                }
-            }
+            info->directRenderingEnabled = R128DRIScreenInit(pScreen);
 	}
     }
 #endif
@@ -2775,8 +2097,23 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
 #endif
     }
 
+    pScrn->vtSema = TRUE;
+    /* xf86CrtcRotate accesses pScrn->pScreen */
+    pScrn->pScreen = pScreen;
+
+    if (info->FBDev) {
+	if (!fbdevHWModeInit(pScrn, pScrn->currentMode)) return FALSE;
+    } else {
+	if (!xf86SetDesiredModes(pScrn)) return FALSE;
+    }
+
+    R128SaveScreen(pScreen, SCREEN_SAVER_ON);
+    //pScrn->AdjustFrame(ADJUST_FRAME_ARGS(pScrn, pScrn->frameX0, pScrn->frameY0));
+
 				/* DGA setup */
-    R128DGAInit(pScreen);
+#ifdef XFreeXDGA
+    xf86DiDGAInit(pScreen, info->LinearAddr + pScrn->fbOffset);
+#endif
 
 				/* Backing store setup */
     xf86SetBackingStore(pScreen);
@@ -2792,9 +2129,6 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
 	if (R128CursorInit(pScreen)) {
 	    int width, height;
 
-	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
-		       "Using hardware cursor (scanline %ld)\n",
-		       info->cursor_start / pScrn->displayWidth);
 	    if (xf86QueryLargestOffscreenArea(pScreen, &width, &height,
 					      0, 0, 0)) {
 		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
@@ -2807,29 +2141,15 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
 	    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Using software cursor\n");
 	}
     } else {
-	info->cursor_start = 0;
 	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Using software cursor\n");
     }
 
-				/* Colormap setup */
-    if (!miCreateDefColormap(pScreen)) return FALSE;
-    if (!xf86HandleColormaps(pScreen, 256, info->dac6bits ? 6 : 8,
-			     (
-#ifndef AVOID_FBDEV
-			     info->FBDev ? fbdevHWLoadPaletteWeak() :
-#endif
-			     R128LoadPalette), NULL,
-			     CMAP_PALETTED_TRUECOLOR
-			     | CMAP_RELOAD_ON_MODE_SWITCH
-#if 0 /* This option messes up text mode! (e...@suse.de) */
-			     | CMAP_LOAD_EVEN_IF_OFFSCREEN
-#endif
-			     )) return FALSE;
-
     /* DPMS setup - FIXME: also for mirror mode in non-fbdev case? - Michel */
 #ifndef AVOID_FBDEV
     if (info->FBDev)
 	xf86DPMSInit(pScreen, fbdevHWDPMSSetWeak(), 0);
+    else
+        xf86DPMSInit(pScreen, xf86DPMSSet, 0);
 
     else 
 #endif
@@ -2873,11 +2193,28 @@ Bool R128ScreenInit(SCREEN_INIT_ARGS_DEC
     info->BlockHandler = pScreen->BlockHandler;
     pScreen->BlockHandler = R128BlockHandler;
 
+    if (!xf86CrtcScreenInit(pScreen)) return FALSE;
+
+				/* Colormap setup */
+    if (!miCreateDefColormap(pScreen)) return FALSE;
+    if (!xf86HandleColormaps(pScreen, 256, info->dac6bits ? 6 : 8,
+			     (
+#ifndef AVOID_FBDEV
+			     info->FBDev ? fbdevHWLoadPaletteWeak() :
+#endif
+			     R128LoadPalette), NULL,
+			     CMAP_PALETTED_TRUECOLOR
+			     | CMAP_RELOAD_ON_MODE_SWITCH
+#if 0 /* This option messes up text mode! (e...@suse.de) */
+			     | CMAP_LOAD_EVEN_IF_OFFSCREEN
+#endif
+			     )) return FALSE;
+
     return TRUE;
 }
 
 /* Write common registers (initialized to 0). */
-static void R128RestoreCommonRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+void R128RestoreCommonRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -2901,7 +2238,7 @@ static void R128RestoreCommonRegisters(S
 }
 
 /* Write CRTC registers. */
-static void R128RestoreCrtcRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+void R128RestoreCrtcRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -2911,9 +2248,6 @@ static void R128RestoreCrtcRegisters(Scr
     OUTREGP(R128_CRTC_EXT_CNTL, restore->crtc_ext_cntl,
 	    R128_CRTC_VSYNC_DIS | R128_CRTC_HSYNC_DIS | R128_CRTC_DISPLAY_DIS);
 
-    OUTREGP(R128_DAC_CNTL, restore->dac_cntl,
-	    R128_DAC_RANGE_CNTL | R128_DAC_BLANKING);
-
     OUTREG(R128_CRTC_H_TOTAL_DISP,    restore->crtc_h_total_disp);
     OUTREG(R128_CRTC_H_SYNC_STRT_WID, restore->crtc_h_sync_strt_wid);
     OUTREG(R128_CRTC_V_TOTAL_DISP,    restore->crtc_v_total_disp);
@@ -2924,8 +2258,7 @@ static void R128RestoreCrtcRegisters(Scr
 }
 
 /* Write CRTC2 registers. */
-static void R128RestoreCrtc2Registers(ScrnInfoPtr pScrn,
-				       R128SavePtr restore)
+void R128RestoreCrtc2Registers(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr info        = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -2942,26 +2275,52 @@ static void R128RestoreCrtc2Registers(Sc
     OUTREG(R128_CRTC2_PITCH,           restore->crtc2_pitch);
 }
 
-/* Write flat panel registers */
-static void R128RestoreFPRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+/* Write DAC registers */
+void R128RestoreDACRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+{
+    R128InfoPtr   info      = R128PTR(pScrn);
+    unsigned char *R128MMIO = info->MMIO;
+
+    OUTREGP(R128_DAC_CNTL, restore->dac_cntl,
+	    R128_DAC_RANGE_CNTL | R128_DAC_BLANKING);
+}
+
+/* Write RMX registers */
+void R128RestoreRMXRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
-    CARD32        tmp;
 
-    if (info->BIOSDisplay != R128_DUALHEAD)
-        OUTREG(R128_CRTC2_GEN_CNTL,       restore->crtc2_gen_cntl);
     OUTREG(R128_FP_HORZ_STRETCH,      restore->fp_horz_stretch);
     OUTREG(R128_FP_VERT_STRETCH,      restore->fp_vert_stretch);
     OUTREG(R128_FP_CRTC_H_TOTAL_DISP, restore->fp_crtc_h_total_disp);
     OUTREG(R128_FP_CRTC_V_TOTAL_DISP, restore->fp_crtc_v_total_disp);
     OUTREG(R128_FP_H_SYNC_STRT_WID,   restore->fp_h_sync_strt_wid);
     OUTREG(R128_FP_V_SYNC_STRT_WID,   restore->fp_v_sync_strt_wid);
-    OUTREG(R128_TMDS_CRC,             restore->tmds_crc);
-    OUTREG(R128_FP_PANEL_CNTL,        restore->fp_panel_cntl);
-    OUTREG(R128_FP_GEN_CNTL, restore->fp_gen_cntl & ~(CARD32)R128_FP_BLANK_DIS);
+}
+
+/* Write flat panel registers */
+void R128RestoreFPRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+{
+    R128InfoPtr   info      = R128PTR(pScrn);
+    unsigned char *R128MMIO = info->MMIO;
+
+    OUTREG(R128_TMDS_CRC,              restore->tmds_crc);
+    OUTREG(R128_TMDS_TRANSMITTER_CNTL, restore->tmds_transmitter_cntl);
+    OUTREG(R128_FP_PANEL_CNTL,         restore->fp_panel_cntl);
+    OUTREG(R128_FP_GEN_CNTL, restore->fp_gen_cntl & ~(uint32_t)R128_FP_BLANK_DIS);
+}
+
+/* Write LVDS registers */
+void R128RestoreLVDSRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+{
+    R128InfoPtr   info      = R128PTR(pScrn);
+    R128EntPtr    pR128Ent  = R128EntPriv(pScrn);
+    unsigned char *R128MMIO = info->MMIO;
+    uint32_t      tmp;
 
-    if(info->isDFP) return;
+    xf86OutputPtr output = R128FirstOutput(pR128Ent->pCrtc[0]);
+    R128OutputPrivatePtr r128_output = output->driver_private;
 
     tmp = INREG(R128_LVDS_GEN_CNTL);
     if ((tmp & (R128_LVDS_ON | R128_LVDS_BLON)) ==
@@ -2970,12 +2329,12 @@ static void R128RestoreFPRegisters(ScrnI
     } else {
 	if (restore->lvds_gen_cntl & (R128_LVDS_ON | R128_LVDS_BLON)) {
 	    OUTREG(R128_LVDS_GEN_CNTL,
-		   restore->lvds_gen_cntl & (CARD32)~R128_LVDS_BLON);
-	    usleep(R128PTR(pScrn)->PanelPwrDly * 1000);
+		   restore->lvds_gen_cntl & (uint32_t)~R128_LVDS_BLON);
+	    usleep(r128_output->PanelPwrDly * 1000);
 	    OUTREG(R128_LVDS_GEN_CNTL, restore->lvds_gen_cntl);
 	} else {
 	    OUTREG(R128_LVDS_GEN_CNTL, restore->lvds_gen_cntl | R128_LVDS_BLON);
-	    usleep(R128PTR(pScrn)->PanelPwrDly * 1000);
+	    usleep(r128_output->PanelPwrDly * 1000);
 	    OUTREG(R128_LVDS_GEN_CNTL, restore->lvds_gen_cntl);
 	}
     }
@@ -3016,7 +2375,7 @@ static void R128PLL2WriteUpdate(ScrnInfo
 }
 
 /* Write PLL registers. */
-static void R128RestorePLLRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+void R128RestorePLLRegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -3088,7 +2447,7 @@ static void R128RestorePLLRegisters(Scrn
 }
 
 /* Write PLL2 registers. */
-static void R128RestorePLL2Registers(ScrnInfoPtr pScrn, R128SavePtr restore)
+void R128RestorePLL2Registers(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr info        = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -3157,7 +2516,7 @@ static void R128RestorePLL2Registers(Scr
 }
 
 /* Write DDA registers. */
-static void R128RestoreDDARegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
+void R128RestoreDDARegisters(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -3167,7 +2526,7 @@ static void R128RestoreDDARegisters(Scrn
 }
 
 /* Write DDA registers. */
-static void R128RestoreDDA2Registers(ScrnInfoPtr pScrn, R128SavePtr restore)
+void R128RestoreDDA2Registers(ScrnInfoPtr pScrn, R128SavePtr restore)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
@@ -3176,126 +2535,6 @@ static void R128RestoreDDA2Registers(Scr
     OUTREG(R128_DDA2_ON_OFF, restore->dda2_on_off);
 }
 
-/* Write palette data. */
-static void R128RestorePalette(ScrnInfoPtr pScrn, R128SavePtr restore)
-{
-    R128InfoPtr   info      = R128PTR(pScrn);
-    unsigned char *R128MMIO = info->MMIO;
-    int           i;
-
-    if (!restore->palette_valid) return;
-
-    PAL_SELECT(1);
-    OUTPAL_START(0);
-    for (i = 0; i < 256; i++) {
-	R128WaitForFifo(pScrn, 32); /* delay */
-	OUTPAL_NEXT_CARD32(restore->palette2[i]);
-    }
-
-    PAL_SELECT(0);
-    OUTPAL_START(0);
-    for (i = 0; i < 256; i++) {
-	R128WaitForFifo(pScrn, 32); /* delay */
-	OUTPAL_NEXT_CARD32(restore->palette[i]);
-    }
-
-}
-
-/* Write out state to define a new video mode.  */
-static void R128RestoreMode(ScrnInfoPtr pScrn, R128SavePtr restore)
-{
-    R128InfoPtr info = R128PTR(pScrn);
-    DevUnion* pPriv;
-    R128EntPtr pR128Ent;
-    static R128SaveRec restore0;
-
-    R128TRACE(("R128RestoreMode(%p)\n", restore));
-    if(!info->HasCRTC2)
-    {
-    	R128RestoreCommonRegisters(pScrn, restore);
-        R128RestoreDDARegisters(pScrn, restore);
-    	R128RestoreCrtcRegisters(pScrn, restore);
-        if((info->DisplayType == MT_DFP) || 
-           (info->DisplayType == MT_LCD))
-        {
-	    R128RestoreFPRegisters(pScrn, restore);
-        }
-        R128RestorePLLRegisters(pScrn, restore);
-        return;
-    }       
-    
-    pPriv = xf86GetEntityPrivate(pScrn->entityList[0], 
-                   getR128EntityIndex());
-    pR128Ent = pPriv->ptr;
-   
-
-    /*****
-      When changing mode with Dual-head card (VE/M6), care must
-      be taken for the special order in setting registers. CRTC2 has
-      to be set before changing CRTC_EXT register.
-      In the dual-head setup, X server calls this routine twice with
-      primary and secondary pScrn pointers respectively. The calls
-      can come with different order. Regardless the order of X server issuing 
-      the calls, we have to ensure we set registers in the right order!!! 
-      Otherwise we may get a blank screen.
-    *****/
-
-    if(info->IsSecondary)
-    {
-	if (!pR128Ent->RestorePrimary  && !info->SwitchingMode)
-	    R128RestoreCommonRegisters(pScrn, restore);
-        R128RestoreDDA2Registers(pScrn, restore);
-        R128RestoreCrtc2Registers(pScrn, restore);        
-        R128RestorePLL2Registers(pScrn, restore);
-        
-	if(info->SwitchingMode) return;
-
-        pR128Ent->IsSecondaryRestored = TRUE;
-
-        if(pR128Ent->RestorePrimary)
-        {
-            R128InfoPtr info0 = R128PTR(pR128Ent->pPrimaryScrn); 
-            pR128Ent->RestorePrimary = FALSE;
-
-            R128RestoreCrtcRegisters(pScrn, &restore0);
-            if((info0->DisplayType == MT_DFP) || 
-               (info0->DisplayType == MT_LCD))
-            {
-                R128RestoreFPRegisters(pScrn, &restore0);
-            }
-            
-            R128RestorePLLRegisters(pScrn, &restore0);   
-            pR128Ent->IsSecondaryRestored = FALSE;
-
-        }
-    }
-    else
-    {
-	if (!pR128Ent->IsSecondaryRestored)
-            R128RestoreCommonRegisters(pScrn, restore);
-        R128RestoreDDARegisters(pScrn, restore);
-        if(!pR128Ent->HasSecondary || pR128Ent->IsSecondaryRestored
-            || info->SwitchingMode)
-        {
-	    pR128Ent->IsSecondaryRestored = FALSE;
-            R128RestoreCrtcRegisters(pScrn, restore);
-            if((info->DisplayType == MT_DFP) || 
-               (info->DisplayType == MT_LCD))
-            {
-               R128RestoreFPRegisters(pScrn, restore);
-            }
-            R128RestorePLLRegisters(pScrn, restore);   
-        }
-        else
-        {
-            memcpy(&restore0, restore, sizeof(restore0));
-            pR128Ent->RestorePrimary = TRUE;
-        }
-    }
-
-    R128RestorePalette(pScrn, restore);
-}
-
 /* Read common registers. */
 static void R128SaveCommonRegisters(ScrnInfoPtr pScrn, R128SavePtr save)
 {
@@ -3342,8 +2581,6 @@ static void R128SaveFPRegisters(ScrnInfo
     R128InfoPtr   info      = R128PTR(pScrn);
     unsigned char *R128MMIO = info->MMIO;
 
-    if (info->BIOSDisplay != R128_DUALHEAD)
-        save->crtc2_gen_cntl       = INREG(R128_CRTC2_GEN_CNTL);
     save->fp_crtc_h_total_disp = INREG(R128_FP_CRTC_H_TOTAL_DISP);
     save->fp_crtc_v_total_disp = INREG(R128_FP_CRTC_V_TOTAL_DISP);
     save->fp_gen_cntl          = INREG(R128_FP_GEN_CNTL);
@@ -3448,28 +2685,23 @@ static void R128SavePalette(ScrnInfoPtr 
 static void R128SaveMode(ScrnInfoPtr pScrn, R128SavePtr save)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
+    R128EntPtr    pR128Ent  = R128EntPriv(pScrn);
 
     R128TRACE(("R128SaveMode(%p)\n", save));
 
-    if(info->IsSecondary)
-    {
+    R128SaveCommonRegisters(pScrn, save);
+    R128SaveCrtcRegisters(pScrn, save);
+    R128SavePLLRegisters(pScrn, save);
+    R128SaveDDARegisters(pScrn, save);
+    if (pR128Ent->HasCRTC2) {
         R128SaveCrtc2Registers(pScrn, save);
         R128SavePLL2Registers(pScrn, save);
         R128SaveDDA2Registers(pScrn, save);
     }
-    else
-    {
-        R128SaveCommonRegisters(pScrn, save);
-        R128SaveCrtcRegisters(pScrn, save);
-        if((info->DisplayType == MT_DFP) || 
-           (info->DisplayType == MT_LCD))
-        {
- 	    R128SaveFPRegisters(pScrn, save);
-        }
-        R128SavePLLRegisters(pScrn, save);
-        R128SaveDDARegisters(pScrn, save);
-        R128SavePalette(pScrn, save);
+    if (info->HasPanelRegs) {
+        R128SaveFPRegisters(pScrn, save);
     }
+    R128SavePalette(pScrn, save);
 
     R128TRACE(("R128SaveMode returns %p\n", save));
 }
@@ -3489,41 +2721,40 @@ static void R128Save(ScrnInfoPtr pScrn)
     }
 #endif
 
-    if (!info->IsSecondary) {
 #ifdef WITH_VGAHW
-        if (info->VGAAccess) {
-            vgaHWPtr hwp = VGAHWPTR(pScrn);
+    if (info->VGAAccess) {
+        vgaHWPtr hwp = VGAHWPTR(pScrn);
 
-            vgaHWUnlock(hwp);
+        vgaHWUnlock(hwp);
 # if defined(__powerpc__)
-            /* temporary hack to prevent crashing on PowerMacs when trying to
-             * read VGA fonts and colormap, will find a better solution
-             * in the future. TODO: Check if there's actually some VGA stuff
-             * setup in the card at all !!
-             */
-            vgaHWSave(pScrn, &hwp->SavedReg, VGA_SR_MODE); /* Save mode only */
+        /* temporary hack to prevent crashing on PowerMacs when trying to
+         * read VGA fonts and colormap, will find a better solution
+         * in the future. TODO: Check if there's actually some VGA stuff
+         * setup in the card at all !!
+         */
+        vgaHWSave(pScrn, &hwp->SavedReg, VGA_SR_MODE); /* Save mode only */
 # else
-            /* Save mode * & fonts & cmap */
-            vgaHWSave(pScrn, &hwp->SavedReg, VGA_SR_MODE | VGA_SR_FONTS);
+        /* Save mode * & fonts & cmap */
+        vgaHWSave(pScrn, &hwp->SavedReg, VGA_SR_MODE | VGA_SR_FONTS);
 # endif
-            vgaHWLock(hwp);
-        }
-#endif
-        save->dp_datatype      = INREG(R128_DP_DATATYPE);
-        save->gen_reset_cntl   = INREG(R128_GEN_RESET_CNTL);
-        save->clock_cntl_index = INREG(R128_CLOCK_CNTL_INDEX);
-        save->amcgpio_en_reg   = INREG(R128_AMCGPIO_EN_REG);
-        save->amcgpio_mask     = INREG(R128_AMCGPIO_MASK);
+        vgaHWLock(hwp);
     }
+#endif
 
-    R128SaveMode(pScrn, save);
+    save->dp_datatype      = INREG(R128_DP_DATATYPE);
+    save->gen_reset_cntl   = INREG(R128_GEN_RESET_CNTL);
+    save->clock_cntl_index = INREG(R128_CLOCK_CNTL_INDEX);
+    save->amcgpio_en_reg   = INREG(R128_AMCGPIO_EN_REG);
+    save->amcgpio_mask     = INREG(R128_AMCGPIO_MASK);
 
+    R128SaveMode(pScrn, save);
 }
 
 /* Restore the original (text) mode. */
 static void R128Restore(ScrnInfoPtr pScrn)
 {
     R128InfoPtr   info      = R128PTR(pScrn);
+    R128EntPtr    pR128Ent  = R128EntPriv(pScrn);
     unsigned char *R128MMIO = info->MMIO;
     R128SavePtr   restore   = &info->SavedReg;
 
@@ -3536,15 +2767,25 @@ static void R128Restore(ScrnInfoPtr pScr
 #endif
     R128Blank(pScrn);
 
-    if (!info->IsSecondary) {
-        OUTREG(R128_AMCGPIO_MASK,     restore->amcgpio_mask);
-        OUTREG(R128_AMCGPIO_EN_REG,   restore->amcgpio_en_reg);
-        OUTREG(R128_CLOCK_CNTL_INDEX, restore->clock_cntl_index);
-        OUTREG(R128_GEN_RESET_CNTL,   restore->gen_reset_cntl);
-        OUTREG(R128_DP_DATATYPE,      restore->dp_datatype);
-    }
+    OUTREG(R128_AMCGPIO_MASK,     restore->amcgpio_mask);
+    OUTREG(R128_AMCGPIO_EN_REG,   restore->amcgpio_en_reg);
+    OUTREG(R128_CLOCK_CNTL_INDEX, restore->clock_cntl_index);
+    OUTREG(R128_GEN_RESET_CNTL,   restore->gen_reset_cntl);
+    OUTREG(R128_DP_DATATYPE,      restore->dp_datatype);
 
-    R128RestoreMode(pScrn, restore);
+    R128RestoreCommonRegisters(pScrn, restore);
+    if (pR128Ent->HasCRTC2) {
+        R128RestoreDDA2Registers(pScrn, restore);
+        R128RestoreCrtc2Registers(pScrn, restore);
+        R128RestorePLL2Registers(pScrn, restore);
+    }
+    R128RestoreDDARegisters(pScrn, restore);
+    R128RestoreCrtcRegisters(pScrn, restore);
+    R128RestorePLLRegisters(pScrn, restore);
+    R128RestoreDACRegisters(pScrn, restore);
+    R128RestoreRMXRegisters(pScrn, restore);
+    R128RestoreFPRegisters(pScrn, restore);
+    R128RestoreLVDSRegisters(pScrn, restore);
 
     if (!info->IsSecondary) {
         OUTREG(R128_AMCGPIO_MASK,     restore->amcgpio_mask);
@@ -3557,34 +2798,16 @@ static void R128Restore(ScrnInfoPtr pScr
 #ifdef WITH_VGAHW
     if (info->VGAAccess) {
         vgaHWPtr hwp = VGAHWPTR(pScrn);
-        if (!info->IsSecondary) {
-            vgaHWUnlock(hwp);
+        vgaHWUnlock(hwp);
 # if defined(__powerpc__)
-            /* Temporary hack to prevent crashing on PowerMacs when trying to
-             * write VGA fonts, will find a better solution in the future
-             */
-            vgaHWRestore(pScrn, &hwp->SavedReg, VGA_SR_MODE );
+        /* Temporary hack to prevent crashing on PowerMacs when trying to
+         * write VGA fonts, will find a better solution in the future
+         */
+        vgaHWRestore(pScrn, &hwp->SavedReg, VGA_SR_MODE );
 # else
-            vgaHWRestore(pScrn, &hwp->SavedReg, VGA_SR_MODE | VGA_SR_FONTS );
+        vgaHWRestore(pScrn, &hwp->SavedReg, VGA_SR_MODE | VGA_SR_FONTS );
 # endif
-            vgaHWLock(hwp);
-        } else {
-            R128EntPtr  pR128Ent = R128EntPriv(pScrn);
-            ScrnInfoPtr   pScrn0 = pR128Ent->pPrimaryScrn;
-            R128InfoPtr info0 = R128PTR(pScrn0);
-            vgaHWPtr      hwp0;
-
-            if (info0->VGAAccess) {
-                hwp0 = VGAHWPTR(pScrn0);
-                vgaHWUnlock(hwp0);
-#if defined(__powerpc__)
-                vgaHWRestore(pScrn0, &hwp0->SavedReg, VGA_SR_MODE);
-#else
-                vgaHWRestore(pScrn0, &hwp0->SavedReg, VGA_SR_MODE | VGA_SR_FONTS );
-#endif
-                vgaHWLock(hwp0);
-            }
-        }
+        vgaHWLock(hwp);
     }
 #endif
 
@@ -3593,7 +2816,7 @@ static void R128Restore(ScrnInfoPtr pScr
 }
 
 /* Define common registers for requested video mode. */
-static void R128InitCommonRegisters(R128SavePtr save, R128InfoPtr info)
+void R128InitCommonRegisters(R128SavePtr save, R128InfoPtr info)
 {
     save->ovr_clr            = 0;
     save->ovr_wid_left_right = 0;
@@ -3619,10 +2842,70 @@ static void R128InitCommonRegisters(R128
 	save->bus_cntl |= R128_BUS_RD_DISCARD_EN | R128_BUS_RD_ABORT_EN;
 }
 
+Bool R128InitCrtcBase(xf86CrtcPtr crtc, R128SavePtr save, int x, int y)
+{
+    ScrnInfoPtr pScrn = crtc->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    int offset = y * info->CurrentLayout.displayWidth + x;
+    int Base = pScrn->fbOffset;
+
+    switch (info->CurrentLayout.pixel_code) {
+    case 15:
+    case 16: offset *= 2; break;
+    case 24: offset *= 3; break;
+    case 32: offset *= 4; break;
+    }
+    Base += offset;
+
+    if (crtc->rotatedData != NULL)
+        Base = pScrn->fbOffset + (char *)crtc->rotatedData - (char *)info->FB;
+
+    Base &= ~7;                 /* 3 lower bits are always 0 */
+    if (info->CurrentLayout.pixel_code == 24)
+	Base += 8 * (Base % 3); /* Must be multiple of 8 and 3 */
+
+    save->crtc_offset = Base;
+    save->crtc_offset_cntl = 0;
+
+    return TRUE;
+}
+
+Bool R128InitCrtc2Base(xf86CrtcPtr crtc, R128SavePtr save, int x, int y)
+{
+    ScrnInfoPtr pScrn = crtc->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    int offset = y * info->CurrentLayout.displayWidth + x;
+    int Base = pScrn->fbOffset;
+
+    switch (info->CurrentLayout.pixel_code) {
+    case 15:
+    case 16: offset *= 2; break;
+    case 24: offset *= 3; break;
+    case 32: offset *= 4; break;
+    }
+    Base += offset;
+
+    if (crtc->rotatedData != NULL)
+        Base = pScrn->fbOffset + (char *)crtc->rotatedData - (char *)info->FB;
+
+    Base &= ~7;                 /* 3 lower bits are always 0 */
+    if (info->CurrentLayout.pixel_code == 24)
+	Base += 8 * (Base % 3); /* Must be multiple of 8 and 3 */
+
+    save->crtc2_offset = Base;
+    save->crtc2_offset_cntl = 0;
+
+    return TRUE;
+}
+
 /* Define CRTC registers for requested video mode. */
-static Bool R128InitCrtcRegisters(ScrnInfoPtr pScrn, R128SavePtr save,
-				  DisplayModePtr mode, R128InfoPtr info)
+Bool R128InitCrtcRegisters(xf86CrtcPtr crtc, R128SavePtr save, DisplayModePtr mode)
 {
+    ScrnInfoPtr pScrn = crtc->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    xf86OutputPtr output = R128FirstOutput(crtc);
+    R128OutputPrivatePtr r128_output = output->driver_private;
+
     int    format;
     int    hsync_start;
     int    hsync_wid;
@@ -3646,8 +2929,7 @@ static Bool R128InitCrtcRegisters(ScrnIn
 	return FALSE;
     }
 
-    if ((info->DisplayType == MT_DFP) || 
-        (info->DisplayType == MT_LCD))
+    if (r128_output->MonType == MT_LCD || r128_output->MonType == MT_DFP)
 	hsync_fudge = hsync_fudge_fp[format-1];
     else               
         hsync_fudge = hsync_fudge_default[format-1];
@@ -3665,37 +2947,10 @@ static Bool R128InitCrtcRegisters(ScrnIn
 			     ? R128_CRTC_CSYNC_EN
 			     : 0));
 
-    if((info->DisplayType == MT_DFP) || 
-       (info->DisplayType == MT_LCD))
-    {
-        save->crtc_ext_cntl = R128_VGA_ATI_LINEAR | 
-        			  R128_XCRT_CNT_EN;
-        save->crtc_gen_cntl &= ~(R128_CRTC_DBL_SCAN_EN | 
-                                  R128_CRTC_INTERLACE_EN);
-    }
-    else
-        save->crtc_ext_cntl = R128_VGA_ATI_LINEAR | 
-			      R128_XCRT_CNT_EN |
-			      R128_CRTC_CRT_ON;
-
-    save->dac_cntl      = (R128_DAC_MASK_ALL
-			   | R128_DAC_VGA_ADR_EN
-			   | (info->dac6bits ? 0 : R128_DAC_8BIT_EN));
+    if (r128_output->MonType == MT_LCD || r128_output->MonType == MT_DFP)
+        save->crtc_gen_cntl &= ~(R128_CRTC_DBL_SCAN_EN | R128_CRTC_INTERLACE_EN);
 
-
-    if(info->isDFP && !info->isPro2)
-    {
-        if(info->PanelXRes < mode->CrtcHDisplay)
-            mode->HDisplay = mode->CrtcHDisplay = info->PanelXRes;
-        if(info->PanelYRes < mode->CrtcVDisplay)
-            mode->VDisplay = mode->CrtcVDisplay = info->PanelYRes;
-        mode->CrtcHTotal = mode->CrtcHDisplay + info->HBlank;
-        mode->CrtcHSyncStart = mode->CrtcHDisplay + info->HOverPlus;
-        mode->CrtcHSyncEnd = mode->CrtcHSyncStart + info->HSyncWidth;
-        mode->CrtcVTotal = mode->CrtcVDisplay + info->VBlank;
-        mode->CrtcVSyncStart = mode->CrtcVDisplay + info->VOverPlus;
-        mode->CrtcVSyncEnd = mode->CrtcVSyncStart + info->VSyncWidth;
-    }
+    save->crtc_ext_cntl |= R128_VGA_ATI_LINEAR | R128_XCRT_CNT_EN;
 
     save->crtc_h_total_disp = ((((mode->CrtcHTotal / 8) - 1) & 0xffff)
 			      | (((mode->CrtcHDisplay / 8) - 1) << 16));
@@ -3734,8 +2989,6 @@ static Bool R128InitCrtcRegisters(ScrnIn
 				 | ((mode->Flags & V_NVSYNC)
 				    ? R128_CRTC_V_SYNC_POL
 				    : 0));
-    save->crtc_offset      = 0;
-    save->crtc_offset_cntl = 0;
     save->crtc_pitch       = info->CurrentLayout.displayWidth / 8;
 
     R128TRACE(("Pitch = %d bytes (virtualX = %d, displayWidth = %d)\n",
@@ -3755,30 +3008,30 @@ static Bool R128InitCrtcRegisters(ScrnIn
 }
 
 /* Define CRTC2 registers for requested video mode. */
-static Bool R128InitCrtc2Registers(ScrnInfoPtr pScrn, R128SavePtr save,
-				  DisplayModePtr mode, R128InfoPtr info)
+Bool R128InitCrtc2Registers(xf86CrtcPtr crtc, R128SavePtr save, DisplayModePtr mode)
 {
+    ScrnInfoPtr pScrn = crtc->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+
     int    format;
     int    hsync_start;
     int    hsync_wid;
     int    hsync_fudge;
     int    vsync_wid;
-    int    bytpp;
     int    hsync_fudge_default[] = { 0x00, 0x12, 0x09, 0x09, 0x06, 0x05 };
 
     switch (info->CurrentLayout.pixel_code) {
-    case 4:  format = 1; bytpp = 0; break;
-    case 8:  format = 2; bytpp = 1; break;
-    case 15: format = 3; bytpp = 2; break;      /*  555 */
-    case 16: format = 4; bytpp = 2; break;      /*  565 */
-    case 24: format = 5; bytpp = 3; break;      /*  RGB */
-    case 32: format = 6; bytpp = 4; break;      /* xRGB */
+    case 4:  format = 1; break;
+    case 8:  format = 2; break;
+    case 15: format = 3; break;      /*  555 */
+    case 16: format = 4; break;      /*  565 */
+    case 24: format = 5; break;      /*  RGB */
+    case 32: format = 6; break;      /* xRGB */
     default:
 	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
 		   "Unsupported pixel depth (%d)\n", info->CurrentLayout.bitsPerPixel);
 	return FALSE;
     }
-    R128TRACE(("Format = %d (%d bytes per pixel)\n", format, bytpp));
 
     hsync_fudge = hsync_fudge_default[format-1];
 
@@ -3828,10 +3081,6 @@ static Bool R128InitCrtc2Registers(ScrnI
 				 | ((mode->Flags & V_NVSYNC)
 				    ? R128_CRTC2_V_SYNC_POL
 				    : 0));
-
-    save->crtc2_offset      = 0;
-    save->crtc2_offset_cntl = 0;
-
     save->crtc2_pitch       = info->CurrentLayout.displayWidth / 8;
 	
     R128TRACE(("Pitch = %d bytes (virtualX = %d, displayWidth = %d)\n",
@@ -3840,36 +3089,50 @@ static Bool R128InitCrtc2Registers(ScrnI
     return TRUE;
 }
 
-/* Define CRTC registers for requested video mode. */
-static void R128InitFPRegisters(R128SavePtr orig, R128SavePtr save,
-				DisplayModePtr mode, R128InfoPtr info)
+/* Define DAC registers for the requested video mode. */
+void R128InitDACRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output)
+{
+    ScrnInfoPtr pScrn = output->scrn;
+    R128InfoPtr info = R128PTR(pScrn);
+    xf86CrtcPtr crtc = output->crtc;
+    R128CrtcPrivatePtr r128_crtc = crtc->driver_private;
+
+    save->dac_cntl = (R128_DAC_MASK_ALL | R128_DAC_VGA_ADR_EN |
+                      (!r128_crtc->crtc_id ? 0 : R128_DAC_CRT_SEL_CRTC2) |
+                      (info->dac6bits      ? 0 : R128_DAC_8BIT_EN));
+}
+
+/* Define RMX registers for the requested video mode. */
+void R128InitRMXRegisters(R128SavePtr orig, R128SavePtr save,
+                          xf86OutputPtr output, DisplayModePtr mode)
 {
+    R128OutputPrivatePtr r128_output = output->driver_private;
+
     int   xres = mode->CrtcHDisplay;
     int   yres = mode->CrtcVDisplay;
     float Hratio, Vratio;
 
-    if (info->BIOSDisplay == R128_BIOS_DISPLAY_CRT) {
-        save->crtc_ext_cntl  |= R128_CRTC_CRT_ON;
-        save->crtc2_gen_cntl  = 0;
-        save->fp_gen_cntl     = orig->fp_gen_cntl;
-        save->fp_gen_cntl    &= ~(R128_FP_FPON |
-            R128_FP_CRTC_USE_SHADOW_VEND |
-            R128_FP_CRTC_HORZ_DIV2_EN |
-            R128_FP_CRTC_HOR_CRT_DIV2_DIS |
-            R128_FP_USE_SHADOW_EN);
-        save->fp_gen_cntl    |= (R128_FP_SEL_CRTC2 |
-                                 R128_FP_CRTC_DONT_SHADOW_VPAR);
-        save->fp_panel_cntl   = orig->fp_panel_cntl & (CARD32)~R128_FP_DIGON;
-        save->lvds_gen_cntl   = orig->lvds_gen_cntl &
-				    (CARD32)~(R128_LVDS_ON | R128_LVDS_BLON);
+    save->fp_crtc_h_total_disp = save->crtc_h_total_disp;
+    save->fp_crtc_v_total_disp = save->crtc_v_total_disp;
+    save->fp_h_sync_strt_wid   = save->crtc_h_sync_strt_wid;
+    save->fp_v_sync_strt_wid   = save->crtc_v_sync_strt_wid;
+
+    if (r128_output->MonType != MT_DFP && r128_output->MonType != MT_LCD)
         return;
-    }
 
-    if (xres > info->PanelXRes) xres = info->PanelXRes;
-    if (yres > info->PanelYRes) yres = info->PanelYRes;
+    if (r128_output->PanelXRes == 0 || r128_output->PanelYRes == 0) {
+        xres = r128_output->PanelXRes;
+        yres = r128_output->PanelYRes;
+
+        Hratio = 1.0;
+        Vratio = 1.0;
+    } else {
+        if (xres > r128_output->PanelXRes) xres = r128_output->PanelXRes;
+        if (yres > r128_output->PanelYRes) yres = r128_output->PanelYRes;
 
-    Hratio = (float)xres/(float)info->PanelXRes;
-    Vratio = (float)yres/(float)info->PanelYRes;
+        Hratio = (float)xres/(float)r128_output->PanelXRes;
+        Vratio = (float)yres/(float)r128_output->PanelYRes;
+    }
 
     save->fp_horz_stretch =
 	(((((int)(Hratio * R128_HORZ_STRETCH_RATIO_MAX + 0.5))
@@ -3879,7 +3142,7 @@ static void R128InitFPRegisters(R128Save
                                  R128_HORZ_STRETCH_RESERVED)));
     save->fp_horz_stretch &= ~R128_HORZ_AUTO_RATIO_FIX_EN;
     save->fp_horz_stretch &= ~R128_AUTO_HORZ_RATIO;
-    if (xres == info->PanelXRes)
+    if (xres == r128_output->PanelXRes)
          save->fp_horz_stretch &= ~(R128_HORZ_STRETCH_BLEND | R128_HORZ_STRETCH_ENABLE);
     else
          save->fp_horz_stretch |=  (R128_HORZ_STRETCH_BLEND | R128_HORZ_STRETCH_ENABLE);
@@ -3890,73 +3153,65 @@ static void R128InitFPRegisters(R128Save
 	 (orig->fp_vert_stretch & (R128_VERT_PANEL_SIZE |
 				   R128_VERT_STRETCH_RESERVED)));
     save->fp_vert_stretch &= ~R128_VERT_AUTO_RATIO_EN;
-    if (yres == info->PanelYRes)
+    if (yres == r128_output->PanelYRes)
         save->fp_vert_stretch &= ~(R128_VERT_STRETCH_ENABLE | R128_VERT_STRETCH_BLEND);
     else
         save->fp_vert_stretch |=  (R128_VERT_STRETCH_ENABLE | R128_VERT_STRETCH_BLEND);
+}
 
-    save->fp_gen_cntl = (orig->fp_gen_cntl &
-			 (CARD32)~(R128_FP_SEL_CRTC2 |
-				   R128_FP_CRTC_USE_SHADOW_VEND |
-				   R128_FP_CRTC_HORZ_DIV2_EN |
-				   R128_FP_CRTC_HOR_CRT_DIV2_DIS |
-				   R128_FP_USE_SHADOW_EN));
-
-    save->fp_panel_cntl        = orig->fp_panel_cntl;
-    save->lvds_gen_cntl        = orig->lvds_gen_cntl;
-    save->tmds_crc             = orig->tmds_crc;
-
-    /* Disable CRT output by disabling CRT output and setting the CRT
-       DAC to use CRTC2, which we set to 0's.  In the future, we will
-       want to use the dual CRTC capabilities of the R128 to allow both
-       the flat panel and external CRT to either simultaneously display
-       the same image or display two different images. */
-
-
-    if(!info->isDFP){
-        if (info->BIOSDisplay == R128_BIOS_DISPLAY_FP_CRT) {
-		save->crtc_ext_cntl  |= R128_CRTC_CRT_ON;
-	} else if (info->BIOSDisplay == R128_DUALHEAD) {
-		save->crtc_ext_cntl  |= R128_CRTC_CRT_ON;
-		save->dac_cntl       |= R128_DAC_CRT_SEL_CRTC2;
-		save->dac_cntl       |= R128_DAC_PALETTE2_SNOOP_EN;
-        } else {
-		save->crtc_ext_cntl  &= ~R128_CRTC_CRT_ON;
-		save->dac_cntl       |= R128_DAC_CRT_SEL_CRTC2;
-		save->crtc2_gen_cntl  = 0;
-        }
-    }
+/* Define flat panel registers for the requested video mode. */
+void R128InitFPRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output)
+{
+    xf86CrtcPtr crtc = output->crtc;
+    R128CrtcPrivatePtr r128_crtc = crtc->driver_private;
 
     /* WARNING: Be careful about turning on the flat panel */
-    if(info->isDFP){
-        save->fp_gen_cntl = orig->fp_gen_cntl;
+    save->fp_gen_cntl            = orig->fp_gen_cntl;
+    save->fp_panel_cntl          = orig->fp_panel_cntl;
+    save->tmds_transmitter_cntl  = orig->tmds_transmitter_cntl;
+    save->tmds_crc               = orig->tmds_crc;
 
-        save->fp_gen_cntl &= ~(R128_FP_CRTC_USE_SHADOW_VEND |
-                               R128_FP_CRTC_USE_SHADOW_ROWCUR |
-                               R128_FP_CRTC_HORZ_DIV2_EN |
-                               R128_FP_CRTC_HOR_CRT_DIV2_DIS |
-                               R128_FP_CRT_SYNC_SEL |
-                               R128_FP_USE_SHADOW_EN);
-
-        save->fp_panel_cntl  |= (R128_FP_DIGON | R128_FP_BLON);
-        save->fp_gen_cntl    |= (R128_FP_FPON | R128_FP_TDMS_EN |
-             R128_FP_CRTC_DONT_SHADOW_VPAR | R128_FP_CRTC_DONT_SHADOW_HEND);
-        save->tmds_transmitter_cntl = (orig->tmds_transmitter_cntl
-            & ~(CARD32)R128_TMDS_PLLRST) | R128_TMDS_PLLEN;
-    }
+    if (r128_crtc->crtc_id)
+        save->fp_gen_cntl       |=   R128_FP_SEL_CRTC2;
     else
-        save->lvds_gen_cntl  |= (R128_LVDS_ON | R128_LVDS_BLON);
+        save->fp_gen_cntl       &=  ~R128_FP_SEL_CRTC2;
 
-    save->fp_crtc_h_total_disp = save->crtc_h_total_disp;
-    save->fp_crtc_v_total_disp = save->crtc_v_total_disp;
-    save->fp_h_sync_strt_wid   = save->crtc_h_sync_strt_wid;
-    save->fp_v_sync_strt_wid   = save->crtc_v_sync_strt_wid;
+    save->fp_gen_cntl           &= ~(R128_FP_CRTC_USE_SHADOW_VEND |
+                                     R128_FP_CRTC_USE_SHADOW_ROWCUR |
+                                     R128_FP_CRTC_HORZ_DIV2_EN |
+                                     R128_FP_CRTC_HOR_CRT_DIV2_DIS |
+                                     R128_FP_CRT_SYNC_SEL |
+                                     R128_FP_USE_SHADOW_EN);
+
+    save->fp_gen_cntl           |=  (R128_FP_CRTC_DONT_SHADOW_VPAR |
+                                     R128_FP_CRTC_DONT_SHADOW_HEND);
+
+    save->fp_panel_cntl         |=  (R128_FP_DIGON | R128_FP_BLON);
+    save->tmds_transmitter_cntl &=  ~R128_TMDS_PLLRST;
+    save->tmds_transmitter_cntl |=   R128_TMDS_PLLEN;
+}
+
+/* Define LVDS registers for the requested video mode. */
+void R128InitLVDSRegisters(R128SavePtr orig, R128SavePtr save, xf86OutputPtr output)
+{
+    xf86CrtcPtr crtc = output->crtc;
+    R128CrtcPrivatePtr r128_crtc = crtc->driver_private;
+
+    save->lvds_gen_cntl      =  orig->lvds_gen_cntl;
+
+    if (r128_crtc->crtc_id)
+        save->lvds_gen_cntl |=  R128_LVDS_SEL_CRTC2;
+    else
+        save->lvds_gen_cntl &= ~R128_LVDS_SEL_CRTC2;
 }
 
 /* Define PLL registers for requested video mode. */
-static void R128InitPLLRegisters(ScrnInfoPtr pScrn, R128SavePtr save,
+void R128InitPLLRegisters(xf86CrtcPtr crtc, R128SavePtr save,
 				R128PLLPtr pll, double dot_clock)
 {
+#if R128_DEBUG
+    ScrnInfoPtr pScrn  = crtc->scrn;
+#endif
     unsigned long freq = dot_clock * 100;
     struct {
 	int divider;
@@ -4006,9 +3261,12 @@ static void R128InitPLLRegisters(ScrnInf
 }
 
 /* Define PLL2 registers for requested video mode. */
-static void R128InitPLL2Registers(ScrnInfoPtr pScrn, R128SavePtr save,
+void R128InitPLL2Registers(xf86CrtcPtr crtc, R128SavePtr save,
 				   R128PLLPtr pll, double dot_clock)
 {
+#if R128_DEBUG
+    ScrnInfoPtr pScrn  = crtc->scrn;
+#endif
     unsigned long freq = dot_clock * 100;
     struct {
 	int divider;
@@ -4058,10 +3316,14 @@ static void R128InitPLL2Registers(ScrnIn
 }
 
 /* Define DDA registers for requested video mode. */
-static Bool R128InitDDARegisters(ScrnInfoPtr pScrn, R128SavePtr save,
-				 R128PLLPtr pll, R128InfoPtr info,
-                                 DisplayModePtr mode)
+Bool R128InitDDARegisters(xf86CrtcPtr crtc, R128SavePtr save,
+				 R128PLLPtr pll, DisplayModePtr mode)
 {
+    ScrnInfoPtr pScrn = crtc->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    xf86OutputPtr output = R128FirstOutput(crtc);
+    R128OutputPrivatePtr r128_output = output->driver_private;
+
     int         DisplayFifoWidth = 128;
     int         DisplayFifoDepth = 32;
     int         XclkFreq;
@@ -4077,10 +3339,10 @@ static Bool R128InitDDARegisters(ScrnInf
     VclkFreq = R128Div(pll->reference_freq * save->feedback_div,
 		       pll->reference_div * save->post_div);
 
-    if(info->isDFP && !info->isPro2){
-        if(info->PanelXRes != mode->CrtcHDisplay)
-            VclkFreq = (VclkFreq * mode->CrtcHDisplay)/info->PanelXRes;
-	}
+    if (info->isDFP && !info->isPro2 && r128_output->PanelXRes > 0) {
+        if (r128_output->PanelXRes != mode->CrtcHDisplay)
+            VclkFreq = (VclkFreq * mode->CrtcHDisplay) / r128_output->PanelXRes;
+    }
 
     XclksPerTransfer = R128Div(XclkFreq * DisplayFifoWidth,
 			       VclkFreq * (info->CurrentLayout.pixel_bytes * 8));
@@ -4127,10 +3389,14 @@ static Bool R128InitDDARegisters(ScrnInf
 }
 
 /* Define DDA2 registers for requested video mode. */
-static Bool R128InitDDA2Registers(ScrnInfoPtr pScrn, R128SavePtr save,
-				 R128PLLPtr pll, R128InfoPtr info,
-                                 DisplayModePtr mode)
+Bool R128InitDDA2Registers(xf86CrtcPtr crtc, R128SavePtr save,
+				 R128PLLPtr pll, DisplayModePtr mode)
 {
+    ScrnInfoPtr pScrn = crtc->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    xf86OutputPtr output = R128FirstOutput(crtc);
+    R128OutputPrivatePtr r128_output = output->driver_private;
+
     int         DisplayFifoWidth = 128;
     int         DisplayFifoDepth = 32;
     int         XclkFreq;
@@ -4146,10 +3412,10 @@ static Bool R128InitDDA2Registers(ScrnIn
     VclkFreq = R128Div(pll->reference_freq * save->feedback_div_2,
 		       pll->reference_div * save->post_div_2);
 
-    if(info->isDFP && !info->isPro2){
-        if(info->PanelXRes != mode->CrtcHDisplay)
-            VclkFreq = (VclkFreq * mode->CrtcHDisplay)/info->PanelXRes;
-	}
+    if (info->isDFP && !info->isPro2 && r128_output->PanelXRes > 0) {
+        if (r128_output->PanelXRes != mode->CrtcHDisplay)
+            VclkFreq = (VclkFreq * mode->CrtcHDisplay) / r128_output->PanelXRes;
+    }
 
     XclksPerTransfer = R128Div(XclkFreq * DisplayFifoWidth,
 			       VclkFreq * (info->CurrentLayout.pixel_bytes * 8));
@@ -4207,122 +3473,6 @@ static void R128InitPalette(R128SavePtr 
 }
 #endif
 
-/* Define registers for a requested video mode. */
-static Bool R128Init(ScrnInfoPtr pScrn, DisplayModePtr mode, R128SavePtr save)
-{
-    R128InfoPtr info      = R128PTR(pScrn);
-    double      dot_clock = mode->Clock/1000.0;
-
-#if R128_DEBUG
-    ErrorF("%-12.12s %7.2f  %4d %4d %4d %4d  %4d %4d %4d %4d (%d,%d)",
-	   mode->name,
-	   dot_clock,
-
-	   mode->HDisplay,
-	   mode->HSyncStart,
-	   mode->HSyncEnd,
-	   mode->HTotal,
-
-	   mode->VDisplay,
-	   mode->VSyncStart,
-	   mode->VSyncEnd,
-	   mode->VTotal,
-	   pScrn->depth,
-	   pScrn->bitsPerPixel);
-    if (mode->Flags & V_DBLSCAN)   ErrorF(" D");
-    if (mode->Flags & V_CSYNC)     ErrorF(" C");
-    if (mode->Flags & V_INTERLACE) ErrorF(" I");
-    if (mode->Flags & V_PHSYNC)    ErrorF(" +H");
-    if (mode->Flags & V_NHSYNC)    ErrorF(" -H");
-    if (mode->Flags & V_PVSYNC)    ErrorF(" +V");
-    if (mode->Flags & V_NVSYNC)    ErrorF(" -V");
-    ErrorF("\n");
-    ErrorF("%-12.12s %7.2f  %4d %4d %4d %4d  %4d %4d %4d %4d (%d,%d)",
-	   mode->name,
-	   dot_clock,
-
-	   mode->CrtcHDisplay,
-	   mode->CrtcHSyncStart,
-	   mode->CrtcHSyncEnd,
-	   mode->CrtcHTotal,
-
-	   mode->CrtcVDisplay,
-	   mode->CrtcVSyncStart,
-	   mode->CrtcVSyncEnd,
-	   mode->CrtcVTotal,
-	   pScrn->depth,
-	   pScrn->bitsPerPixel);
-    if (mode->Flags & V_DBLSCAN)   ErrorF(" D");
-    if (mode->Flags & V_CSYNC)     ErrorF(" C");
-    if (mode->Flags & V_INTERLACE) ErrorF(" I");
-    if (mode->Flags & V_PHSYNC)    ErrorF(" +H");
-    if (mode->Flags & V_NHSYNC)    ErrorF(" -H");
-    if (mode->Flags & V_PVSYNC)    ErrorF(" +V");
-    if (mode->Flags & V_NVSYNC)    ErrorF(" -V");
-    ErrorF("\n");
-#endif
-
-    info->Flags = mode->Flags;
-
-    if(info->IsSecondary)
-    {
-        if (!R128InitCrtc2Registers(pScrn, save, 
-             pScrn->currentMode,info)) 
-            return FALSE;
-        R128InitPLL2Registers(pScrn, save, &info->pll, dot_clock);
-        if (!R128InitDDA2Registers(pScrn, save, &info->pll, info, mode))
-	    return FALSE;
-    }
-    else
-    {
-        R128InitCommonRegisters(save, info);
-        if(!R128InitCrtcRegisters(pScrn, save, mode, info)) 
-            return FALSE;
-        if(dot_clock) 
-        {
-            R128InitPLLRegisters(pScrn, save, &info->pll, dot_clock);
-            if (!R128InitDDARegisters(pScrn, save, &info->pll, info, mode))
-	        return FALSE;
-        }
-        else
-        {
-            save->ppll_ref_div         = info->SavedReg.ppll_ref_div;
-            save->ppll_div_3           = info->SavedReg.ppll_div_3;
-            save->htotal_cntl          = info->SavedReg.htotal_cntl;
-            save->dda_config           = info->SavedReg.dda_config;
-            save->dda_on_off           = info->SavedReg.dda_on_off;
-        }
-        /* not used for now */
-        /*if (!info->PaletteSavedOnVT) RADEONInitPalette(save);*/
-    }
-
-    if (((info->DisplayType == MT_DFP) || 
-        (info->DisplayType == MT_LCD)))
-    {
-        R128InitFPRegisters(&info->SavedReg, save, mode, info);
-    }
-
-    R128TRACE(("R128Init returns %p\n", save));
-    return TRUE;
-}
-
-/* Initialize a new mode. */
-static Bool R128ModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
-{
-    R128InfoPtr info      = R128PTR(pScrn);
-
-    if (!R128Init(pScrn, mode, &info->ModeReg)) return FALSE;
-				/* FIXME?  DRILock/DRIUnlock here? */
-    pScrn->vtSema = TRUE;
-    R128Blank(pScrn);
-    R128RestoreMode(pScrn, &info->ModeReg);
-    R128Unblank(pScrn);
-
-    info->CurrentLayout.mode = mode;
-
-    return TRUE;
-}
-
 static Bool R128SaveScreen(ScreenPtr pScreen, int mode)
 {
     ScrnInfoPtr   pScrn = xf86ScreenToScrn(pScreen);
@@ -4353,74 +3503,64 @@ Bool R128SwitchMode(SWITCH_MODE_ARGS_DEC
     Bool ret;
 
     info->SwitchingMode = TRUE;
-    ret = R128ModeInit(pScrn, mode);
+    ret = xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
     info->SwitchingMode = FALSE;
     return ret;
 }
 
-/* Used to disallow modes that are not supported by the hardware. */
-ModeStatus R128ValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode,
-                                   Bool verbose, int flags)
+ModeStatus R128DoValidMode(xf86OutputPtr output, DisplayModePtr mode, int flags)
 {
-    SCRN_INFO_PTR(arg);
-    R128InfoPtr   info  = R128PTR(pScrn);
-
-    if (info->BIOSDisplay == R128_BIOS_DISPLAY_CRT)
-	return MODE_OK;
+    ScrnInfoPtr pScrn = output->scrn;
+    R128InfoPtr info  = R128PTR(pScrn);
+    R128OutputPrivatePtr r128_output = output->driver_private;
+    int i, j;
 
-    if(info->isDFP) {
-        if(info->PanelXRes < mode->CrtcHDisplay ||
-           info->PanelYRes < mode->CrtcVDisplay)
-            return MODE_NOMODE;
-        else
-            return MODE_OK;
-    }
+    if (r128_output->MonType == MT_CRT)
+        return MODE_OK;
 
-    if (info->DisplayType == MT_LCD) {
+    if (r128_output->MonType == MT_DFP || r128_output->MonType == MT_LCD) {
 	if (mode->Flags & V_INTERLACE) return MODE_NO_INTERLACE;
 	if (mode->Flags & V_DBLSCAN)   return MODE_NO_DBLESCAN;
     }
 
-    if (info->DisplayType == MT_LCD &&
-	info->VBIOS) {
-	int i;
-	for (i = info->FPBIOSstart+64; R128_BIOS16(i) != 0; i += 2) {
-	    int j = R128_BIOS16(i);
+    if (r128_output->MonType == MT_LCD && info->VBIOS) {
+	for (i = info->FPBIOSstart + 64; R128_BIOS16(i) != 0; i += 2) {
+	    j = R128_BIOS16(i);
 
 	    if (mode->CrtcHDisplay == R128_BIOS16(j) &&
-		mode->CrtcVDisplay == R128_BIOS16(j+2)) {
+		mode->CrtcVDisplay == R128_BIOS16(j + 2)) {
 		if ((flags & MODECHECK_FINAL) == MODECHECK_FINAL) {
 		    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 			       "Modifying mode according to VBIOS: %ix%i [pclk %.1f MHz] for FP to: ",
-			       mode->CrtcHDisplay,mode->CrtcVDisplay,
-			       (float)mode->Clock/1000);
+			       mode->CrtcHDisplay, mode->CrtcVDisplay,
+			       (float)mode->Clock / 1000);
 
 		    /* Assume we are using expanded mode */
-		    if (R128_BIOS16(j+5)) j  = R128_BIOS16(j+5);
-		    else                  j += 9;
+		    if (R128_BIOS16(j + 5)) j  = R128_BIOS16(j + 5);
+		    else                    j += 9;
 
-		    mode->Clock = (CARD32)R128_BIOS16(j) * 10;
+		    mode->Clock = (uint32_t)R128_BIOS16(j) * 10;
 
 		    mode->HDisplay   = mode->CrtcHDisplay   =
-			((R128_BIOS16(j+10) & 0x01ff)+1)*8;
+			((R128_BIOS16(j + 10) & 0x01ff) + 1) * 8;
 		    mode->HSyncStart = mode->CrtcHSyncStart =
-			((R128_BIOS16(j+12) & 0x01ff)+1)*8;
+			((R128_BIOS16(j + 12) & 0x01ff) + 1) * 8;
 		    mode->HSyncEnd   = mode->CrtcHSyncEnd   =
-			mode->CrtcHSyncStart + (R128_BIOS8(j+14) & 0x1f);
+			mode->CrtcHSyncStart + (R128_BIOS8(j + 14) & 0x1f);
 		    mode->HTotal     = mode->CrtcHTotal     =
-			((R128_BIOS16(j+8)  & 0x01ff)+1)*8;
+			((R128_BIOS16(j + 8)  & 0x01ff) + 1) * 8;
 
 		    mode->VDisplay   = mode->CrtcVDisplay   =
-			(R128_BIOS16(j+17) & 0x07ff)+1;
+			(R128_BIOS16(j + 17) & 0x07ff) + 1;
 		    mode->VSyncStart = mode->CrtcVSyncStart =
-			(R128_BIOS16(j+19) & 0x07ff)+1;
+			(R128_BIOS16(j + 19) & 0x07ff) + 1;
 		    mode->VSyncEnd   = mode->CrtcVSyncEnd   =
-			mode->CrtcVSyncStart + ((R128_BIOS16(j+19) >> 11) & 0x1f);
+			mode->CrtcVSyncStart + ((R128_BIOS16(j + 19) >> 11) & 0x1f);
 		    mode->VTotal     = mode->CrtcVTotal     =
-			(R128_BIOS16(j+15) & 0x07ff)+1;
+			(R128_BIOS16(j + 15) & 0x07ff) + 1;
 		    xf86ErrorF("%ix%i [pclk %.1f MHz]\n",
 			       mode->CrtcHDisplay,mode->CrtcVDisplay,
-			       (float)mode->Clock/1000);
+			       (float)mode->Clock/ 1000);
 		}
 		return MODE_OK;
 	    }
@@ -4436,6 +3576,17 @@ ModeStatus R128ValidMode(SCRN_ARG_TYPE a
     return MODE_OK;
 }
 
+/* Used to disallow modes that are not supported by the hardware. */
+ModeStatus R128ValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode,
+                                   Bool verbose, int flags)
+{
+    SCRN_INFO_PTR(arg);
+    R128EntPtr  pR128Ent = R128EntPriv(pScrn);
+    xf86OutputPtr output = R128FirstOutput(pR128Ent->pCrtc[0]);
+
+    return R128DoValidMode(output, mode, flags);
+}
+
 /* Adjust viewport into virtual desktop such that (0,0) in viewport space
    is (x,y) in virtual space. */
 void R128AdjustFrame(ADJUST_FRAME_ARGS_DECL)
@@ -4462,14 +3613,7 @@ void R128AdjustFrame(ADJUST_FRAME_ARGS_D
     if (info->CurrentLayout.pixel_code == 24)
 	Base += 8 * (Base % 3); /* Must be multiple of 8 and 3 */
 
-    if(info->IsSecondary)    
-    {
-        Base += pScrn->fbOffset; 
-        OUTREG(R128_CRTC2_OFFSET, Base);
-    }
-    else
     OUTREG(R128_CRTC_OFFSET, Base);
-
 }
 
 /* Called when VT switching back to the X server.  Reinitialize the video
@@ -4480,12 +3624,20 @@ Bool R128EnterVT(VT_FUNC_ARGS_DECL)
     R128InfoPtr info  = R128PTR(pScrn);
 
     R128TRACE(("R128EnterVT\n"));
+
+    pScrn->vtSema = TRUE;
 #ifndef AVOID_FBDEV
     if (info->FBDev) {
         if (!fbdevHWEnterVT(VT_FUNC_ARGS)) return FALSE;
     } else
 #endif
-        if (!R128ModeInit(pScrn, pScrn->currentMode)) return FALSE;
+    if (!R128ModeInit(pScrn, pScrn->currentMode)) return FALSE;
+    else {
+        if (!xf86SetDesiredModes(pScrn)) return FALSE;
+    }
+
+    //if (!R128ModeInit(pScrn, pScrn->currentMode)) return FALSE;
+
     if (info->accelOn)
 	R128EngineInit(pScrn);
 
@@ -4502,7 +3654,7 @@ Bool R128EnterVT(VT_FUNC_ARGS_DECL)
 #endif
 
     info->PaletteSavedOnVT = FALSE;
-    pScrn->AdjustFrame(ADJUST_FRAME_ARGS(pScrn, pScrn->frameX0, pScrn->frameY0));
+    //pScrn->AdjustFrame(ADJUST_FRAME_ARGS(pScrn, pScrn->frameX0, pScrn->frameY0));
 
     return TRUE;
 }
@@ -4576,12 +3728,6 @@ static Bool R128CloseScreen(CLOSE_SCREEN
     if (info->scratch_save)      free(info->scratch_save);
     info->scratch_save           = NULL;
 
-    if (info->cursor)            xf86DestroyCursorInfoRec(info->cursor);
-    info->cursor                 = NULL;
-
-    if (info->DGAModes)          free(info->DGAModes);
-    info->DGAModes               = NULL;
-
     if (info->adaptor) {
         free(info->adaptor->pPortPrivates[0].ptr);
 	xf86XVFreeVideoAdaptorRec(info->adaptor);

Index: xsrc/external/mit/xf86-video-r128/dist/src/r128_reg.h
diff -u xsrc/external/mit/xf86-video-r128/dist/src/r128_reg.h:1.2 xsrc/external/mit/xf86-video-r128/dist/src/r128_reg.h:1.3
--- xsrc/external/mit/xf86-video-r128/dist/src/r128_reg.h:1.2	Thu Dec 23 04:32:50 2010
+++ xsrc/external/mit/xf86-video-r128/dist/src/r128_reg.h	Mon Jul 20 00:55:42 2015
@@ -57,12 +57,12 @@
 #define OUTREG16(addr, val) MMIO_OUT16(R128MMIO, addr, val)
 #define OUTREG(addr, val)   MMIO_OUT32(R128MMIO, addr, val)
 
-#define ADDRREG(addr)       ((volatile CARD32 *)(pointer)(R128MMIO + (addr)))
+#define ADDRREG(addr)       ((volatile uint32_t *)(pointer)(R128MMIO + (addr)))
 
 
 #define OUTREGP(addr, val, mask)   \
     do {                           \
-	CARD32 tmp = INREG(addr);  \
+	uint32_t tmp = INREG(addr);  \
 	tmp &= (mask);             \
 	tmp |= ((val) & ~(mask));  \
 	OUTREG(addr, tmp);         \
@@ -78,7 +78,7 @@
 
 #define OUTPLLP(pScrn, addr, val, mask)                                   \
     do {                                                                  \
-	CARD32 tmp = INPLL(pScrn, addr);                                  \
+	uint32_t tmp = INPLL(pScrn, addr);                                  \
 	tmp &= (mask);                                                    \
 	tmp |= ((val) & ~(mask));                                         \
 	OUTPLL(addr, tmp);                                                \
@@ -94,7 +94,7 @@
 	OUTREG(R128_PALETTE_DATA, ((r) << 16) | ((g) << 8) | (b));        \
     } while (0)
 
-#define OUTPAL_NEXT_CARD32(v)                                             \
+#define OUTPAL_NEXT_uint32_t(v)                                             \
     do {                                                                  \
 	OUTREG(R128_PALETTE_DATA, (v & 0x00ffffff));                      \
     } while (0)
@@ -114,12 +114,12 @@
 
 #define PAL_SELECT(idx)                                                   \
     do {                                                                  \
-	CARD32 tmp = INREG(R128_DAC_CNTL);                                \
+	uint32_t tmp = INREG(R128_DAC_CNTL);                                \
 	if (idx) {                                                        \
 	    OUTREG(R128_DAC_CNTL, tmp | R128_DAC_PALETTE_ACC_CTL);        \
 	} else {                                                          \
 	    OUTREG(R128_DAC_CNTL, tmp &                                   \
-		   (CARD32)~R128_DAC_PALETTE_ACC_CTL);                    \
+		   (uint32_t)~R128_DAC_PALETTE_ACC_CTL);                    \
 	}                                                                 \
     } while (0)
 
@@ -599,7 +599,7 @@
 #define R128_FP_GEN_CNTL                  0x0284
 #       define R128_FP_FPON                  (1 << 0)
 #       define R128_FP_BLANK_DIS             (1 << 1)
-#       define R128_FP_TDMS_EN               (1 <<  2)
+#       define R128_FP_TMDS_EN               (1 <<  2)
 #       define R128_FP_DETECT_SENSE          (1 <<  8)
 #       define R128_FP_SEL_CRTC2             (1 << 13)
 #       define R128_FP_CRTC_DONT_SHADOW_VPAR (1 << 16)

Reply via email to