TODO
[tpg/acess2.git] / KernelLand / Modules / Display / VIAVideo / main.c
1 /*
2  * Acess2 VIA Video Driver
3  * - By John Hodge
4  * 
5  * main.c
6  * - Driver core
7  *
8  * NOTE: Based off the UniChrome driver for X, http://unichrome.sourceforge.net/
9  */
10 #define DEBUG   0
11 #include <acess.h>
12 #include <fs_devfs.h>
13 #include <drv_pci.h>
14 #include <modules.h>
15 #include <api_drv_video.h>
16 #include <timers.h>
17 #include "common.h"
18
19 #define VERSION VER2(0,1)
20
21 // === CONSTANTS ===
22 const struct sVGA_Timings
23 {
24          int    HFP, HSync, HDisplay, HBP;
25          int    VFP, VSync, VDisplay, VBP;
26 } csaTimings[] = {
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
30 };
31 const Uint16    caVIAVideo_CardIDs[][2] = {
32         {0x1106, 0x3108},       // minuet (unk chip)
33         {0x1106, 0x3157},       // CX700/VX700 (S3 UniChrome Pro)
34 };
35 const int       ciVIAVideo_NumCardIDs = sizeof(caVIAVideo_CardIDs)/sizeof(caVIAVideo_CardIDs[0]);
36
37 // === PROTOTYPES ===
38  int    VIAVideo_Initialise(char **Arguments);
39 void    VIAVideo_Cleanup(void);
40
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);
43
44 void    VIAVideo_int_SetMode(const struct sVGA_Timings *Timings, int Depth);
45 void    VIAVideo_int_ResetCRTC(int Index, BOOL Reset);
46
47 static void     _SRMask(int Reg, Uint8 Byte, Uint8 Mask);
48 static void     _CRMask(int Reg, Uint8 Byte, Uint8 Mask);
49
50 // === GLOBALS ===
51 MODULE_DEFINE(0, VERSION, VIAVideo, VIAVideo_Initialise, VIAVideo_Cleanup, NULL);
52 tVFS_NodeType   gVIAVideo_NodeType = {
53         .Write = VIAVideo_Write,
54         .IOCtl = VIAVideo_IOCtl
55         };
56 tDevFS_Driver   gVIAVideo_DriverStruct = {
57         NULL, "VIAVideo",
58         {.Type = &gVIAVideo_NodeType}
59         };
60  int    giVIAVideo_DriverID;
61 tVIAVideo_Dev   gVIAVideo_Info;
62
63 // === CODE ===
64 int VIAVideo_Initialise(char **Arguments)
65 {
66          int    count = 0;
67         
68         for( int i = 0; i < ciVIAVideo_NumCardIDs; i ++ )
69         {
70                 count += PCI_CountDevices(caVIAVideo_CardIDs[i][0], caVIAVideo_CardIDs[i][1]);
71         }
72         if(count == 0)  return MODULE_ERR_NOTNEEDED;
73         
74
75         for( int i = 0; i < ciVIAVideo_NumCardIDs; i ++ )
76         {
77                 for( int id = 0; (id = PCI_GetDevice(caVIAVideo_CardIDs[i][0], caVIAVideo_CardIDs[i][1], id)) != -1; id ++ )
78                 {
79                         // TODO: Support MMIO
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));
86                         
87                         // Ignore multiple cards
88                         if( gVIAVideo_Info.FramebufferPhys )    continue ;
89                         
90                         gVIAVideo_Info.FramebufferPhys = PCI_GetBAR(id, 0);
91                         gVIAVideo_Info.MMIOPhys = PCI_GetBAR(id, 1);
92                         
93                         gVIAVideo_Info.Framebuffer = (void*)MM_MapHWPages(
94                                 gVIAVideo_Info.FramebufferPhys, (1024*768*4)/PAGE_SIZE
95                                 );
96                         // TODO: Map MMIO
97
98                         Uint8   tmp;
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);
102
103
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 */
108
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;
114                 }
115         }
116
117         VIAVideo_int_SetMode( &csaTimings[1], 32 );
118         for( int i = 0; i < 768; i ++ )
119         {
120                 Uint32  *scanline = gVIAVideo_Info.Framebuffer + i * 0x1000;
121                 for( int j = 0; j < 200; j ++ )
122                 {
123                         scanline[j] = 0xFFA06000;
124                 }
125                 Time_Delay(500);
126         }
127
128         // Install Device
129         giVIAVideo_DriverID = DevFS_AddDevice( &gVIAVideo_DriverStruct );
130         if(giVIAVideo_DriverID == -1)   return MODULE_ERR_MISC;
131
132         return MODULE_ERR_OK;
133 }
134
135 void VIAVideo_Cleanup(void)
136 {
137         return ;
138 }
139
140 size_t VIAVideo_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
141 {
142         #if 0
143         return DrvUtil_Video_WriteLFB(&gVIAVideo_Info.BufInfo, Offset, Length, Buffer);
144         #endif
145         return 0;
146         #if 0
147         if( Offset >= gVIAVideo_Info.FBSize )
148                 return 0;
149         if( Length > gVIAVideo_Info.FBSize )
150                 Length = gVIAVideo_Info.FBSize;
151         if( Offset + Length > gVIAVideo_Info.FBSize )
152                 Length = gVIAVideo_Info.FBSize - Offset;
153         
154         memcpy( (Uint8*)gVIAVideo_Info.Framebuffer + Offset, Buffer, Length );
155         
156         return Length;
157         #endif
158 }
159
160 int VIAVideo_IOCtl(tVFS_Node *Node, int ID, void *Data)
161 {
162         return 0;
163 }
164
165 // --- Modeset!
166 void VIAVideo_int_SetMode(const struct sVGA_Timings *Timings, int Depth)
167 {
168          int    temp;
169
170         // Disable power
171         _CRMask(0x36, 0x30, 0x30);      // CRT Off (TODO: Others)
172         // VQ Disable (optional?)
173         
174         // Unlock registers
175         _CRMask(0x17, 0x00, 0x80);
176         
177         // Reset CRTC1
178         VIAVideo_int_ResetCRTC(0, TRUE);
179         
180         // -- Set framebuffer --
181         // Set colour depth bits
182         // VGASRMask(Crtc, 0x15, {0x00,0x14,0x0C}[Depth=8,16,24/32], 0x1C)
183         switch(Depth) {
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;
188         default:        return ;
189         }
190         // Set line length
191         {
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);
196         }
197         // - Set frame origin? (->FrameSet(Crtc, X, Y))
198         // DWORD Offset in memory
199         // Stored in CR0D + CR0C + CR34 + CR48&0x03
200
201         // -- Set CRTC Mode --
202         // NOTES:
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)
205
206         // Set mode
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")
245         temp = 0;
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")
260         temp = 0x3FFF;
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")
267         temp = 0;
268         _CRMask(0x09, temp, 0x1F);
269         // 0: Cursor location
270         temp = 0;
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);
283         // 0:  CR32 (???)
284         _CRMask(0x32, 0, 0xFF);
285         // 0:  CR33&0xC8
286         _CRMask(0x33, 0, 0xC8);
287
288         // Set scaling?
289
290         // Some magic? (Not Simultaneous)
291         _CRMask(0x6B, 0x00, 0x08);
292
293         // Disable CRTC reset
294         VIAVideo_int_ResetCRTC(0, FALSE);
295
296         // Some other magic? (VGA - Lock registers?)
297         _CRMask(0x17, 0x80, 0x80);
298                 
299         _CRMask(0x36, 0x00, 0x30);      // CRT On (TODO: Others)
300 }
301
302 void VIAVideo_int_ResetCRTC(int Index, BOOL Reset)
303 {
304         // VGASRMask(Crtc, 0x00, (Reset ? 0x00 : 0x02), 0x02);
305         _SRMask(0x00, (Reset ? 0x00 : 0x02), 0x02);
306 }
307
308 void _SRMask(int Reg, Uint8 Byte, Uint8 Mask)
309 {
310         Uint8   cv = 0;
311         outb(0x3C4, Reg);
312         if(Mask != 0xFF)        cv = inb(0x3C5) & ~Mask;
313         cv |= Byte & Mask;
314         outb(0x3C5, cv);
315 }
316
317 void _CRMask(int Reg, Uint8 Byte, Uint8 Mask)
318 {
319         Uint8   cv = 0;
320         outb(0x3D4, Reg);
321         if(Mask != 0xFF)        cv = inb(0x3D5) & ~Mask;
322         cv |= Byte & Mask;
323         outb(0x3D5, cv);
324 }
325

UCC git Repository :: git.ucc.asn.au