2 * Acess2 VIA Video Driver
8 * NOTE: Based off the UniChrome driver for X, http://unichrome.sourceforge.net/
15 #include <api_drv_video.h>
19 #define VERSION VER2(0,1)
22 const struct sVGA_Timings
24 int HFP, HSync, HDisplay, HBP;
25 int VFP, VSync, VDisplay, VBP;
27 {40, 128, 800, 88, 1, 4, 600, 23}, // SVGA @ 60Hz
28 {24, 136, 1024, 160, 3, 6, 768, 29}, // XGA @ 60Hz
29 {38, 112, 1280, 248, 1, 3, 1024, 38} // 1280x1024 @ 60Hz
31 const Uint16 caVIAVideo_CardIDs[][2] = {
32 {0x1106, 0x3108}, // minuet (unk chip)
33 {0x1106, 0x3157}, // CX700/VX700 (S3 UniChrome Pro)
35 const int ciVIAVideo_NumCardIDs = sizeof(caVIAVideo_CardIDs)/sizeof(caVIAVideo_CardIDs[0]);
38 int VIAVideo_Initialise(char **Arguments);
39 void VIAVideo_Cleanup(void);
41 size_t VIAVideo_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
42 int VIAVideo_IOCtl(tVFS_Node *Node, int ID, void *Data);
44 void VIAVideo_int_SetMode(const struct sVGA_Timings *Timings, int Depth);
45 void VIAVideo_int_ResetCRTC(int Index, BOOL Reset);
47 static void _SRMask(int Reg, Uint8 Byte, Uint8 Mask);
48 static void _CRMask(int Reg, Uint8 Byte, Uint8 Mask);
51 MODULE_DEFINE(0, VERSION, VIAVideo, VIAVideo_Initialise, VIAVideo_Cleanup, NULL);
52 tVFS_NodeType gVIAVideo_NodeType = {
53 .Write = VIAVideo_Write,
54 .IOCtl = VIAVideo_IOCtl
56 tDevFS_Driver gVIAVideo_DriverStruct = {
58 {.Type = &gVIAVideo_NodeType}
60 int giVIAVideo_DriverID;
61 tVIAVideo_Dev gVIAVideo_Info;
64 int VIAVideo_Initialise(char **Arguments)
68 for( int i = 0; i < ciVIAVideo_NumCardIDs; i ++ )
70 count += PCI_CountDevices(caVIAVideo_CardIDs[i][0], caVIAVideo_CardIDs[i][1]);
72 if(count == 0) return MODULE_ERR_NOTNEEDED;
75 for( int i = 0; i < ciVIAVideo_NumCardIDs; i ++ )
77 for( int id = 0; (id = PCI_GetDevice(caVIAVideo_CardIDs[i][0], caVIAVideo_CardIDs[i][1], id)) != -1; id ++ )
80 Log_Log("VIAVideo", "BAR0 = 0x%x", PCI_GetBAR(id, 0));
81 Log_Log("VIAVideo", "BAR1 = 0x%x", PCI_GetBAR(id, 1));
82 Log_Log("VIAVideo", "BAR2 = 0x%x", PCI_GetBAR(id, 2));
83 Log_Log("VIAVideo", "BAR3 = 0x%x", PCI_GetBAR(id, 3));
84 Log_Log("VIAVideo", "BAR4 = 0x%x", PCI_GetBAR(id, 4));
85 Log_Log("VIAVideo", "BAR5 = 0x%x", PCI_GetBAR(id, 5));
87 // Ignore multiple cards
88 if( gVIAVideo_Info.FramebufferPhys ) continue ;
90 gVIAVideo_Info.FramebufferPhys = PCI_GetBAR(id, 0);
91 gVIAVideo_Info.MMIOPhys = PCI_GetBAR(id, 1);
93 gVIAVideo_Info.Framebuffer = (void*)MM_MapHWPages(
94 gVIAVideo_Info.FramebufferPhys, (1024*768*4)/PAGE_SIZE
99 tmp = PCI_ConfigRead(id, 0xA1, 1);
100 Uint32 vidram = (1 << ((tmp & 0x70) >> 4)) * 4096;
101 Log_Debug("VIAVideo", "0x%x bytes of video ram?", vidram);
104 // Enable Framebuffer
105 _SRMask(0x02, 0x0F, 0xFF); /* Enable writing to all VGA memory planes */
106 _SRMask(0x04, 0x0E, 0xFF); /* Enable Extended VGA memory */
107 _SRMask(0x1A, 0x08, 0x08); /* Enable Extended Memory access */
109 gVIAVideo_Info.BufInfo.Framebuffer = gVIAVideo_Info.Framebuffer;
110 gVIAVideo_Info.BufInfo.Pitch = 1024*4;
111 gVIAVideo_Info.BufInfo.Width = 1024;
112 gVIAVideo_Info.BufInfo.Height = 768;
113 gVIAVideo_Info.BufInfo.Depth = 32;
117 VIAVideo_int_SetMode( &csaTimings[1], 32 );
118 for( int i = 0; i < 768; i ++ )
120 Uint32 *scanline = gVIAVideo_Info.Framebuffer + i * 0x1000;
121 for( int j = 0; j < 200; j ++ )
123 scanline[j] = 0xFFA06000;
129 giVIAVideo_DriverID = DevFS_AddDevice( &gVIAVideo_DriverStruct );
130 if(giVIAVideo_DriverID == -1) return MODULE_ERR_MISC;
132 return MODULE_ERR_OK;
135 void VIAVideo_Cleanup(void)
140 size_t VIAVideo_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
143 return DrvUtil_Video_WriteLFB(&gVIAVideo_Info.BufInfo, Offset, Length, Buffer);
147 if( Offset >= gVIAVideo_Info.FBSize )
149 if( Length > gVIAVideo_Info.FBSize )
150 Length = gVIAVideo_Info.FBSize;
151 if( Offset + Length > gVIAVideo_Info.FBSize )
152 Length = gVIAVideo_Info.FBSize - Offset;
154 memcpy( (Uint8*)gVIAVideo_Info.Framebuffer + Offset, Buffer, Length );
160 int VIAVideo_IOCtl(tVFS_Node *Node, int ID, void *Data)
166 void VIAVideo_int_SetMode(const struct sVGA_Timings *Timings, int Depth)
171 _CRMask(0x36, 0x30, 0x30); // CRT Off (TODO: Others)
172 // VQ Disable (optional?)
175 _CRMask(0x17, 0x00, 0x80);
178 VIAVideo_int_ResetCRTC(0, TRUE);
180 // -- Set framebuffer --
181 // Set colour depth bits
182 // VGASRMask(Crtc, 0x15, {0x00,0x14,0x0C}[Depth=8,16,24/32], 0x1C)
184 case 8: _SRMask(0x15, 0x00, 0x1C); break;
185 case 16: _SRMask(0x15, 0x14, 0x1C); break;
186 case 24: _SRMask(0x15, 0x0C, 0x1C); break;
187 case 32: _SRMask(0x15, 0x0C, 0x1C); break;
192 int pitch = Timings->HDisplay * (Depth/8) / 8;
193 // (Pitch/8) -> CR13 + CR35&0xE0
194 _CRMask(0x13, pitch, 0xFF);
195 _CRMask(0x35, (pitch>>8)<<5, 0xE0);
197 // - Set frame origin? (->FrameSet(Crtc, X, Y))
198 // DWORD Offset in memory
199 // Stored in CR0D + CR0C + CR34 + CR48&0x03
201 // -- Set CRTC Mode --
203 // - VGA Represents the signal cycle as Display, Blank with Sync anywhere in that
204 // - Hence, I program the blanking as HBP, Sync, HFP (with the sync in the blanking area)
207 // VGAMiscMask(Crtc, (HSyncEnabled ? 0x40 : 0x00), 0x40);
208 // VGAMiscMask(Crtc, (VSyncEnabled ? 0x80 : 0x00), 0x80);
209 // HTotal: CR00 + CR36&0x08 = /8-5
210 temp = (Timings->HFP + Timings->HSync + Timings->HDisplay + Timings->HBP) / 8 - 5;
211 _CRMask(0x00, temp, 0xFF);
212 _CRMask(0x36, (temp>>8)<<3, 0x08);
213 // HDisplay: CR01 = /8-1
214 temp = Timings->HDisplay / 8 - 1;
215 _CRMask(0x01, temp, 0xFF);
216 // HBlank Start: CR02 = /8-1
217 temp = (Timings->HDisplay) / 8 - 1;
218 _CRMask(0x02, temp, 0xFF);
219 // HBlank End: CR03&0x1F + CR05&0x80 + CR33&0x20 = /8-1
220 temp = (Timings->HBP + Timings->HSync + Timings->HFP) / 8 - 1;
221 if( temp >> 7 ) Log_Error("VIA", "HBlank End doesn't fit (%i not 7 bits)", temp);
222 _CRMask(0x03, temp, 0x1F);
223 _CRMask(0x05, (temp>>5)<<7, 0x80);
224 _CRMask(0x33, (temp>>6)<<5, 0x20);
225 // HSync Start: CR04 + CR33&0x10 = /8
226 temp = (Timings->HDisplay + Timings->HBP) / 8;
227 _CRMask(0x04, temp, 0xFF);
228 _CRMask(0x33, (temp>>8)<<4, 0x10);
229 // HSync End: CR05&0x1F = /8
230 temp = (Timings->HSync) / 8;
231 _CRMask(0x05, temp, 0x1F);
232 // VTotal: CR06 + CR07&0x01 + CR07&0x20 + CR35&0x01 = -2
233 temp = (Timings->VFP + Timings->VBP + Timings->VSync + Timings->VDisplay) - 2;
234 _CRMask(0x06, temp, 0xFF);
235 _CRMask(0x07, (temp>>8)<<0, 0x01);
236 _CRMask(0x07, (temp>>9)<<5, 0x20);
237 _CRMask(0x35, (temp>>10)<<0, 0x01);
238 // VDisplay: CR12 + CR07&0x02 + CR07&0x40 + CR35&0x04 = -1
239 temp = (Timings->VDisplay) - 1;
240 _CRMask(0x12, temp, 0xFF);
241 _CRMask(0x07, (temp>>8)<<1, 0x02);
242 _CRMask(0x07, (temp>>9)<<6, 0x40);
243 _CRMask(0x07, (temp>>10)<<2, 0x04);
244 // 0: CR0C + CR0D + CD34 + CR48&0x03 ("Primary starting address")
246 _CRMask(0x0C, temp, 0xFF);
247 _CRMask(0x0D, (temp>>8), 0xFF);
248 _CRMask(0x34, (temp>>16), 0xFF);
249 _CRMask(0x48, (temp>>24), 0x03);
250 // VSyncStart: CR10 + CR07&0x04 + CR07&0x80 + CR35&0x02
251 temp = (Timings->VDisplay + Timings->HBP);
252 _CRMask(0x10, temp, 0xFF);
253 _CRMask(0x07, (temp>>8)<<2, 0x04);
254 _CRMask(0x07, (temp>>9)<<7, 0x80);
255 _CRMask(0x35, (temp>>10)<<1, 0x02);
256 // VSyncEnd: CR11&0x0F
257 temp = (Timings->VSync);
258 _CRMask(0x11, temp, 0x0F);
259 // 0x3FFF: CR18 + CR07&0x10 + CR09&0x40 + CR33&0x06 + CR35&0x10 ("line compare")
261 _CRMask(0x18, temp, 0xFF);
262 _CRMask(0x07, (temp>>8)<<4, 0x10);
263 _CRMask(0x09, (temp>>9)<<6, 0x40);
264 _CRMask(0x33, (temp>>10)<<1, 0x06);
265 _CRMask(0x35, (temp>>12)<<4, 0x10);
266 // 0: CR09&0x1F ("Maximum scanline")
268 _CRMask(0x09, temp, 0x1F);
269 // 0: Cursor location
271 _CRMask(0x14, temp, 0xFF);
272 // VBlankStart: CR15 + CR07&0x08 + CR09&0x20 + CR35&0x08 = -1
273 temp = (Timings->VDisplay) - 1;
274 _CRMask(0x15, temp, 0xFF);
275 _CRMask(0x07, (temp>>8)<<3, 0x08);
276 _CRMask(0x09, (temp>>9)<<5, 0x20);
277 _CRMask(0x35, (temp>>10)<<3, 0x08);
278 // VBlankEnd: CR16 = -1
279 temp = (Timings->VBP + Timings->VSync + Timings->VFP) - 1;
280 _CRMask(0x16, temp, 0xFF);
281 // 0: CR08 (Preset Row Scan Register)
282 _CRMask(0x08, 0, 0xFF);
284 _CRMask(0x32, 0, 0xFF);
286 _CRMask(0x33, 0, 0xC8);
290 // Some magic? (Not Simultaneous)
291 _CRMask(0x6B, 0x00, 0x08);
293 // Disable CRTC reset
294 VIAVideo_int_ResetCRTC(0, FALSE);
296 // Some other magic? (VGA - Lock registers?)
297 _CRMask(0x17, 0x80, 0x80);
299 _CRMask(0x36, 0x00, 0x30); // CRT On (TODO: Others)
302 void VIAVideo_int_ResetCRTC(int Index, BOOL Reset)
304 // VGASRMask(Crtc, 0x00, (Reset ? 0x00 : 0x02), 0x02);
305 _SRMask(0x00, (Reset ? 0x00 : 0x02), 0x02);
308 void _SRMask(int Reg, Uint8 Byte, Uint8 Mask)
312 if(Mask != 0xFF) cv = inb(0x3C5) & ~Mask;
317 void _CRMask(int Reg, Uint8 Byte, Uint8 Mask)
321 if(Mask != 0xFF) cv = inb(0x3D5) & ~Mask;