5d8358295e71a3ddbf94bf90cf16833bf049853b
[powerpc.git] / drivers / usb / misc / sisusbvga / sisusb_init.c
1 /*
2  * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
3  *
4  * Display mode initializing code
5  *
6  * Copyright (C) 2001-2005 by Thomas Winischhofer, Vienna, Austria
7  *
8  * If distributed as part of the Linux kernel, this code is licensed under the
9  * terms of the GPL v2.
10  *
11  * Otherwise, the following license terms apply:
12  *
13  * * Redistribution and use in source and binary forms, with or without
14  * * modification, are permitted provided that the following conditions
15  * * are met:
16  * * 1) Redistributions of source code must retain the above copyright
17  * *    notice, this list of conditions and the following disclaimer.
18  * * 2) Redistributions in binary form must reproduce the above copyright
19  * *    notice, this list of conditions and the following disclaimer in the
20  * *    documentation and/or other materials provided with the distribution.
21  * * 3) The name of the author may not be used to endorse or promote products
22  * *    derived from this software without specific prior written permission.
23  * *
24  * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author:      Thomas Winischhofer <thomas@winischhofer.net>
36  *
37  */
38
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/poll.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
46 #include <linux/kref.h>
47
48 #include "sisusb.h"
49
50 #ifdef INCL_SISUSB_CON
51
52 #include "sisusb_init.h"
53
54 /*********************************************/
55 /*         POINTER INITIALIZATION            */
56 /*********************************************/
57
58 static void
59 SiSUSB_InitPtr(struct SiS_Private *SiS_Pr)
60 {
61         SiS_Pr->SiS_ModeResInfo   = SiSUSB_ModeResInfo;
62         SiS_Pr->SiS_StandTable    = SiSUSB_StandTable;
63
64         SiS_Pr->SiS_SModeIDTable  = SiSUSB_SModeIDTable;
65         SiS_Pr->SiS_EModeIDTable  = SiSUSB_EModeIDTable;
66         SiS_Pr->SiS_RefIndex      = SiSUSB_RefIndex;
67         SiS_Pr->SiS_CRT1Table     = SiSUSB_CRT1Table;
68
69         SiS_Pr->SiS_VCLKData      = SiSUSB_VCLKData;
70 }
71
72 /*********************************************/
73 /*          HELPER: SetReg, GetReg           */
74 /*********************************************/
75
76 static void
77 SiS_SetReg(struct SiS_Private *SiS_Pr, unsigned long port,
78                         unsigned short index, unsigned short data)
79 {
80         sisusb_setidxreg(SiS_Pr->sisusb, port, index, data);
81 }
82
83 static void
84 SiS_SetRegByte(struct SiS_Private *SiS_Pr, unsigned long port,
85                                                 unsigned short data)
86 {
87         sisusb_setreg(SiS_Pr->sisusb, port, data);
88 }
89
90 static unsigned char
91 SiS_GetReg(struct SiS_Private *SiS_Pr, unsigned long port,
92                                                 unsigned short index)
93 {
94         u8 data;
95
96         sisusb_getidxreg(SiS_Pr->sisusb, port, index, &data);
97
98         return data;
99 }
100
101 static unsigned char
102 SiS_GetRegByte(struct SiS_Private *SiS_Pr, unsigned long port)
103 {
104         u8 data;
105
106         sisusb_getreg(SiS_Pr->sisusb, port, &data);
107
108         return data;
109 }
110
111 static void
112 SiS_SetRegANDOR(struct SiS_Private *SiS_Pr, unsigned long port,
113                         unsigned short index, unsigned short DataAND,
114                                                 unsigned short DataOR)
115 {
116         sisusb_setidxregandor(SiS_Pr->sisusb, port, index, DataAND, DataOR);
117 }
118
119 static void
120 SiS_SetRegAND(struct SiS_Private *SiS_Pr, unsigned long port,
121                         unsigned short index, unsigned short DataAND)
122 {
123         sisusb_setidxregand(SiS_Pr->sisusb, port, index, DataAND);
124 }
125
126 static void
127 SiS_SetRegOR(struct SiS_Private *SiS_Pr,unsigned long port,
128                         unsigned short index, unsigned short DataOR)
129 {
130         sisusb_setidxregor(SiS_Pr->sisusb, port, index, DataOR);
131 }
132
133 /*********************************************/
134 /*      HELPER: DisplayOn, DisplayOff        */
135 /*********************************************/
136
137 static void
138 SiS_DisplayOn(struct SiS_Private *SiS_Pr)
139 {
140         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, 0xDF);
141 }
142
143 /*********************************************/
144 /*        HELPER: Init Port Addresses        */
145 /*********************************************/
146
147 static void
148 SiSUSBRegInit(struct SiS_Private *SiS_Pr, unsigned long BaseAddr)
149 {
150         SiS_Pr->SiS_P3c4 = BaseAddr + 0x14;
151         SiS_Pr->SiS_P3d4 = BaseAddr + 0x24;
152         SiS_Pr->SiS_P3c0 = BaseAddr + 0x10;
153         SiS_Pr->SiS_P3ce = BaseAddr + 0x1e;
154         SiS_Pr->SiS_P3c2 = BaseAddr + 0x12;
155         SiS_Pr->SiS_P3ca = BaseAddr + 0x1a;
156         SiS_Pr->SiS_P3c6 = BaseAddr + 0x16;
157         SiS_Pr->SiS_P3c7 = BaseAddr + 0x17;
158         SiS_Pr->SiS_P3c8 = BaseAddr + 0x18;
159         SiS_Pr->SiS_P3c9 = BaseAddr + 0x19;
160         SiS_Pr->SiS_P3cb = BaseAddr + 0x1b;
161         SiS_Pr->SiS_P3cc = BaseAddr + 0x1c;
162         SiS_Pr->SiS_P3cd = BaseAddr + 0x1d;
163         SiS_Pr->SiS_P3da = BaseAddr + 0x2a;
164         SiS_Pr->SiS_Part1Port = BaseAddr + SIS_CRT2_PORT_04;
165 }
166
167 /*********************************************/
168 /*             HELPER: GetSysFlags           */
169 /*********************************************/
170
171 static void
172 SiS_GetSysFlags(struct SiS_Private *SiS_Pr)
173 {
174         SiS_Pr->SiS_MyCR63 = 0x63;
175 }
176
177 /*********************************************/
178 /*         HELPER: Init PCI & Engines        */
179 /*********************************************/
180
181 static void
182 SiSInitPCIetc(struct SiS_Private *SiS_Pr)
183 {
184         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x20, 0xa1);
185         /*  - Enable 2D (0x40)
186          *  - Enable 3D (0x02)
187          *  - Enable 3D vertex command fetch (0x10)
188          *  - Enable 3D command parser (0x08)
189          *  - Enable 3D G/L transformation engine (0x80)
190          */
191         SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1E, 0xDA);
192 }
193
194 /*********************************************/
195 /*        HELPER: SET SEGMENT REGISTERS      */
196 /*********************************************/
197
198 static void
199 SiS_SetSegRegLower(struct SiS_Private *SiS_Pr, unsigned short value)
200 {
201         unsigned short temp;
202
203         value &= 0x00ff;
204         temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb) & 0xf0;
205         temp |= (value >> 4);
206         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb, temp);
207         temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd) & 0xf0;
208         temp |= (value & 0x0f);
209         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd, temp);
210 }
211
212 static void
213 SiS_SetSegRegUpper(struct SiS_Private *SiS_Pr, unsigned short value)
214 {
215         unsigned short temp;
216
217         value &= 0x00ff;
218         temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb) & 0x0f;
219         temp |= (value & 0xf0);
220         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb, temp);
221         temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd) & 0x0f;
222         temp |= (value << 4);
223         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd, temp);
224 }
225
226 static void
227 SiS_SetSegmentReg(struct SiS_Private *SiS_Pr, unsigned short value)
228 {
229         SiS_SetSegRegLower(SiS_Pr, value);
230         SiS_SetSegRegUpper(SiS_Pr, value);
231 }
232
233 static void
234 SiS_ResetSegmentReg(struct SiS_Private *SiS_Pr)
235 {
236         SiS_SetSegmentReg(SiS_Pr, 0);
237 }
238
239 static void
240 SiS_SetSegmentRegOver(struct SiS_Private *SiS_Pr, unsigned short value)
241 {
242         unsigned short temp = value >> 8;
243
244         temp &= 0x07;
245         temp |= (temp << 4);
246         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1d, temp);
247         SiS_SetSegmentReg(SiS_Pr, value);
248 }
249
250 static void
251 SiS_ResetSegmentRegOver(struct SiS_Private *SiS_Pr)
252 {
253         SiS_SetSegmentRegOver(SiS_Pr, 0);
254 }
255
256 static void
257 SiS_ResetSegmentRegisters(struct SiS_Private *SiS_Pr)
258 {
259         SiS_ResetSegmentReg(SiS_Pr);
260         SiS_ResetSegmentRegOver(SiS_Pr);
261 }
262
263 /*********************************************/
264 /*           HELPER: SearchModeID            */
265 /*********************************************/
266
267 static int
268 SiS_SearchModeID(struct SiS_Private *SiS_Pr, unsigned short *ModeNo,
269                                                 unsigned short *ModeIdIndex)
270 {
271         if ((*ModeNo) <= 0x13) {
272
273                 if ((*ModeNo) != 0x03)
274                         return 0;
275
276                 (*ModeIdIndex) = 0;
277
278         } else {
279
280                 for(*ModeIdIndex = 0; ;(*ModeIdIndex)++) {
281
282                         if (SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID == (*ModeNo))
283                                 break;
284
285                         if (SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID == 0xFF)
286                                 return 0;
287                 }
288
289         }
290
291         return 1;
292 }
293
294 /*********************************************/
295 /*            HELPER: ENABLE CRT1            */
296 /*********************************************/
297
298 static void
299 SiS_HandleCRT1(struct SiS_Private *SiS_Pr)
300 {
301         /* Enable CRT1 gating */
302         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, SiS_Pr->SiS_MyCR63, 0xbf);
303 }
304
305 /*********************************************/
306 /*           HELPER: GetColorDepth           */
307 /*********************************************/
308
309 static unsigned short
310 SiS_GetColorDepth(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
311                 unsigned short ModeIdIndex)
312 {
313         static const unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8};
314         unsigned short modeflag;
315         short index;
316
317         if (ModeNo <= 0x13) {
318                 modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
319         } else {
320                 modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
321         }
322
323         index = (modeflag & ModeTypeMask) - ModeEGA;
324         if (index < 0) index = 0;
325         return ColorDepth[index];
326 }
327
328 /*********************************************/
329 /*             HELPER: GetOffset             */
330 /*********************************************/
331
332 static unsigned short
333 SiS_GetOffset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
334                 unsigned short ModeIdIndex, unsigned short rrti)
335 {
336         unsigned short xres, temp, colordepth, infoflag;
337
338         infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
339         xres = SiS_Pr->SiS_RefIndex[rrti].XRes;
340
341         colordepth = SiS_GetColorDepth(SiS_Pr, ModeNo, ModeIdIndex);
342
343         temp = xres / 16;
344
345         if (infoflag & InterlaceMode)
346                 temp <<= 1;
347
348         temp *= colordepth;
349
350         if (xres % 16)
351                 temp += (colordepth >> 1);
352
353         return temp;
354 }
355
356 /*********************************************/
357 /*                   SEQ                     */
358 /*********************************************/
359
360 static void
361 SiS_SetSeqRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
362 {
363         unsigned char SRdata;
364         int i;
365
366         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x00, 0x03);
367
368         SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[0] | 0x20;
369         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, SRdata);
370
371         for(i = 2; i <= 4; i++) {
372                 SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[i-1];
373                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, i, SRdata);
374         }
375 }
376
377 /*********************************************/
378 /*                  MISC                     */
379 /*********************************************/
380
381 static void
382 SiS_SetMiscRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
383 {
384         unsigned char Miscdata = SiS_Pr->SiS_StandTable[StandTableIndex].MISC;
385
386         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c2, Miscdata);
387 }
388
389 /*********************************************/
390 /*                  CRTC                     */
391 /*********************************************/
392
393 static void
394 SiS_SetCRTCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
395 {
396         unsigned char CRTCdata;
397         unsigned short i;
398
399         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, 0x11, 0x7f);
400
401         for(i = 0; i <= 0x18; i++) {
402                 CRTCdata = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[i];
403                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, i, CRTCdata);
404         }
405 }
406
407 /*********************************************/
408 /*                   ATT                     */
409 /*********************************************/
410
411 static void
412 SiS_SetATTRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
413 {
414         unsigned char ARdata;
415         unsigned short i;
416
417         for(i = 0; i <= 0x13; i++) {
418                 ARdata = SiS_Pr->SiS_StandTable[StandTableIndex].ATTR[i];
419                 SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
420                 SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, i);
421                 SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, ARdata);
422         }
423         SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
424         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x14);
425         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x00);
426
427         SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
428         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x20);
429         SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
430 }
431
432 /*********************************************/
433 /*                   GRC                     */
434 /*********************************************/
435
436 static void
437 SiS_SetGRCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
438 {
439         unsigned char GRdata;
440         unsigned short i;
441
442         for(i = 0; i <= 0x08; i++) {
443                 GRdata = SiS_Pr->SiS_StandTable[StandTableIndex].GRC[i];
444                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3ce, i, GRdata);
445         }
446
447         if (SiS_Pr->SiS_ModeType > ModeVGA) {
448                 /* 256 color disable */
449                 SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3ce, 0x05, 0xBF);
450         }
451 }
452
453 /*********************************************/
454 /*          CLEAR EXTENDED REGISTERS         */
455 /*********************************************/
456
457 static void
458 SiS_ClearExt1Regs(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
459 {
460         int i;
461
462         for(i = 0x0A; i <= 0x0E; i++) {
463                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, i, 0x00);
464         }
465
466         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x37, 0xFE);
467 }
468
469 /*********************************************/
470 /*              Get rate index               */
471 /*********************************************/
472
473 static unsigned short
474 SiS_GetRatePtr(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
475                                         unsigned short ModeIdIndex)
476 {
477         unsigned short rrti, i, index, temp;
478
479         if (ModeNo <= 0x13)
480                 return 0xFFFF;
481
482         index = SiS_GetReg(SiS_Pr,SiS_Pr->SiS_P3d4, 0x33) & 0x0F;
483         if (index > 0) index--;
484
485         rrti = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
486         ModeNo = SiS_Pr->SiS_RefIndex[rrti].ModeID;
487
488         i = 0;
489         do {
490                 if (SiS_Pr->SiS_RefIndex[rrti + i].ModeID != ModeNo)
491                         break;
492
493                 temp = SiS_Pr->SiS_RefIndex[rrti + i].Ext_InfoFlag & ModeTypeMask;
494                 if (temp < SiS_Pr->SiS_ModeType)
495                         break;
496
497                 i++;
498                 index--;
499         } while(index != 0xFFFF);
500
501         i--;
502
503         return (rrti + i);
504 }
505
506 /*********************************************/
507 /*                  SYNC                     */
508 /*********************************************/
509
510 static void
511 SiS_SetCRT1Sync(struct SiS_Private *SiS_Pr, unsigned short rrti)
512 {
513         unsigned short sync = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag >> 8;
514         sync &= 0xC0;
515         sync |= 0x2f;
516         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c2, sync);
517 }
518
519 /*********************************************/
520 /*                  CRTC/2                   */
521 /*********************************************/
522
523 static void
524 SiS_SetCRT1CRTC(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
525                         unsigned short ModeIdIndex, unsigned short rrti)
526 {
527         unsigned char  index;
528         unsigned short temp, i, j, modeflag;
529
530         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4,0x11,0x7f);
531
532         modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
533
534         index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRT1CRTC;
535
536         for(i = 0,j = 0; i <= 7; i++, j++) {
537                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
538                                 SiS_Pr->SiS_CRT1Table[index].CR[i]);
539         }
540         for(j = 0x10; i <= 10; i++, j++) {
541                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
542                                 SiS_Pr->SiS_CRT1Table[index].CR[i]);
543         }
544         for(j = 0x15; i <= 12; i++, j++) {
545                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
546                                 SiS_Pr->SiS_CRT1Table[index].CR[i]);
547         }
548         for(j = 0x0A; i <= 15; i++, j++) {
549                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, j,
550                                 SiS_Pr->SiS_CRT1Table[index].CR[i]);
551         }
552
553         temp = SiS_Pr->SiS_CRT1Table[index].CR[16] & 0xE0;
554         SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4, 0x0E, temp);
555
556         temp = ((SiS_Pr->SiS_CRT1Table[index].CR[16]) & 0x01) << 5;
557         if (modeflag & DoubleScanMode)  temp |= 0x80;
558         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3d4, 0x09, 0x5F, temp);
559
560         if (SiS_Pr->SiS_ModeType > ModeVGA)
561                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x14, 0x4F);
562 }
563
564 /*********************************************/
565 /*               OFFSET & PITCH              */
566 /*********************************************/
567 /*  (partly overruled by SetPitch() in XF86) */
568 /*********************************************/
569
570 static void
571 SiS_SetCRT1Offset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
572                         unsigned short ModeIdIndex, unsigned short rrti)
573 {
574         unsigned short du = SiS_GetOffset(SiS_Pr, ModeNo, ModeIdIndex, rrti);
575         unsigned short infoflag =  SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
576         unsigned short temp;
577
578         temp = (du >> 8) & 0x0f;
579         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0E, 0xF0, temp);
580
581         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x13, (du & 0xFF));
582
583         if (infoflag & InterlaceMode) du >>= 1;
584
585         du <<= 5;
586         temp = (du >> 8) & 0xff;
587         if (du & 0xff) temp++;
588         temp++;
589         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x10, temp);
590 }
591
592 /*********************************************/
593 /*                  VCLK                     */
594 /*********************************************/
595
596 static void
597 SiS_SetCRT1VCLK(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
598                                                 unsigned short rrti)
599 {
600         unsigned short index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRTVCLK;
601         unsigned short clka = SiS_Pr->SiS_VCLKData[index].SR2B;
602         unsigned short clkb = SiS_Pr->SiS_VCLKData[index].SR2C;
603
604         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4,0x31,0xCF);
605
606         SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4,0x2B,clka);
607         SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4,0x2C,clkb);
608         SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4,0x2D,0x01);
609 }
610
611 /*********************************************/
612 /*                  FIFO                     */
613 /*********************************************/
614
615 static void
616 SiS_SetCRT1FIFO_310(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
617                                                         unsigned short mi)
618 {
619         unsigned short modeflag = SiS_Pr->SiS_EModeIDTable[mi].Ext_ModeFlag;
620
621         /* disable auto-threshold */
622         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x3D, 0xFE);
623
624         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x08, 0xAE);
625         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x09, 0xF0);
626
627         if (ModeNo <= 0x13)
628                 return;
629
630         if ((!(modeflag & DoubleScanMode)) || (!(modeflag & HalfDCLK))) {
631                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x08, 0x34);
632                 SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x3D, 0x01);
633         }
634 }
635
636 /*********************************************/
637 /*              MODE REGISTERS               */
638 /*********************************************/
639
640 static void
641 SiS_SetVCLKState(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
642                                                         unsigned short rrti)
643 {
644         unsigned short data = 0, VCLK = 0, index = 0;
645
646         if (ModeNo > 0x13) {
647                 index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRTVCLK;
648                 VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;
649         }
650
651         if (VCLK >= 166) data |= 0x0c;
652         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x32, 0xf3, data);
653
654         if (VCLK >= 166)
655                 SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1f, 0xe7);
656
657         /* DAC speed */
658         data = 0x03;
659         if (VCLK >= 260)
660                 data = 0x00;
661         else if (VCLK >= 160)
662                 data = 0x01;
663         else if (VCLK >= 135)
664                 data = 0x02;
665
666         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x07, 0xF8, data);
667 }
668
669 static void
670 SiS_SetCRT1ModeRegs(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
671                         unsigned short ModeIdIndex, unsigned short rrti)
672 {
673         unsigned short data, infoflag = 0, modeflag;
674
675         if (ModeNo <= 0x13)
676                 modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
677         else {
678                 modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
679                 infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
680         }
681
682         /* Disable DPMS */
683         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1F, 0x3F);
684
685         data = 0;
686         if (ModeNo > 0x13) {
687                 if (SiS_Pr->SiS_ModeType > ModeEGA) {
688                         data |= 0x02;
689                         data |= ((SiS_Pr->SiS_ModeType - ModeVGA) << 2);
690                 }
691                 if (infoflag & InterlaceMode) data |= 0x20;
692         }
693         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x06, 0xC0, data);
694
695         data = 0;
696         if (infoflag & InterlaceMode) {
697                 /* data = (Hsync / 8) - ((Htotal / 8) / 2) + 3 */
698                 unsigned short hrs = (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x04) |
699                         ((SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0b) & 0xc0) << 2)) - 3;
700                 unsigned short hto = (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x00) |
701                         ((SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0b) & 0x03) << 8)) + 5;
702                 data = hrs - (hto >> 1) + 3;
703         }
704         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x19, (data & 0xFF));
705         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3d4, 0x1a, 0xFC, (data >> 8));
706
707         if (modeflag & HalfDCLK)
708                 SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, 0x08);
709
710         data = 0;
711         if (modeflag & LineCompareOff)
712                 data = 0x08;
713         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0F, 0xB7, data);
714
715         if ((SiS_Pr->SiS_ModeType == ModeEGA) && (ModeNo > 0x13))
716                 SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0F, 0x40);
717
718         SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x31, 0xfb);
719
720         data = 0x60;
721         if (SiS_Pr->SiS_ModeType != ModeText) {
722                 data ^= 0x60;
723                 if (SiS_Pr->SiS_ModeType != ModeEGA)
724                         data ^= 0xA0;
725         }
726         SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x21, 0x1F, data);
727
728         SiS_SetVCLKState(SiS_Pr, ModeNo, rrti);
729
730         if (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x31) & 0x40)
731                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x52, 0x2c);
732         else
733                 SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x52, 0x6c);
734 }
735
736 /*********************************************/
737 /*                 LOAD DAC                  */
738 /*********************************************/
739
740 static void
741 SiS_WriteDAC(struct SiS_Private *SiS_Pr, unsigned long DACData,
742                 unsigned short shiftflag, unsigned short dl, unsigned short ah,
743                 unsigned short al, unsigned short dh)
744 {
745         unsigned short d1, d2, d3;
746
747         switch (dl) {
748                 case  0:
749                         d1 = dh; d2 = ah; d3 = al;
750                         break;
751                 case  1:
752                         d1 = ah; d2 = al; d3 = dh;
753                         break;
754                 default:
755                         d1 = al; d2 = dh; d3 = ah;
756         }
757         SiS_SetRegByte(SiS_Pr, DACData, (d1 << shiftflag));
758         SiS_SetRegByte(SiS_Pr, DACData, (d2 << shiftflag));
759         SiS_SetRegByte(SiS_Pr, DACData, (d3 << shiftflag));
760 }
761
762 static void
763 SiS_LoadDAC(struct SiS_Private *SiS_Pr, unsigned short ModeNo, unsigned short mi)
764 {
765         unsigned short data, data2, time, i, j, k, m, n, o;
766         unsigned short si, di, bx, sf;
767         unsigned long DACAddr, DACData;
768         const unsigned char *table = NULL;
769
770         if (ModeNo < 0x13)
771                 data = SiS_Pr->SiS_SModeIDTable[mi].St_ModeFlag;
772         else
773                 data = SiS_Pr->SiS_EModeIDTable[mi].Ext_ModeFlag;
774
775         data &= DACInfoFlag;
776
777         j = time = 64;
778         if (data == 0x00)
779                 table = SiS_MDA_DAC;
780         else if (data == 0x08)
781                 table = SiS_CGA_DAC;
782         else if (data == 0x10)
783                 table = SiS_EGA_DAC;
784         else {
785                 j = 16;
786                 time = 256;
787                 table = SiS_VGA_DAC;
788         }
789
790         DACAddr = SiS_Pr->SiS_P3c8;
791         DACData = SiS_Pr->SiS_P3c9;
792         sf = 0;
793         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c6, 0xFF);
794
795         SiS_SetRegByte(SiS_Pr, DACAddr, 0x00);
796
797         for(i = 0; i < j; i++) {
798                 data = table[i];
799                 for(k = 0; k < 3; k++) {
800                         data2 = 0;
801                         if (data & 0x01) data2 += 0x2A;
802                         if (data & 0x02) data2 += 0x15;
803                         SiS_SetRegByte(SiS_Pr, DACData, (data2 << sf));
804                         data >>= 2;
805                 }
806         }
807
808         if (time == 256) {
809                 for(i = 16; i < 32; i++) {
810                         data = table[i] << sf;
811                         for(k = 0; k < 3; k++)
812                                 SiS_SetRegByte(SiS_Pr, DACData, data);
813                 }
814                 si = 32;
815                 for(m = 0; m < 9; m++) {
816                         di = si;
817                         bx = si + 4;
818                         for(n = 0; n < 3; n++) {
819                                 for(o = 0; o < 5; o++) {
820                                         SiS_WriteDAC(SiS_Pr, DACData, sf, n,
821                                                 table[di], table[bx], table[si]);
822                                         si++;
823                                 }
824                                 si -= 2;
825                                 for(o = 0; o < 3; o++) {
826                                         SiS_WriteDAC(SiS_Pr, DACData, sf, n,
827                                                 table[di], table[si], table[bx]);
828                                         si--;
829                                 }
830                         }
831                 si += 5;
832                 }
833         }
834 }
835
836 /*********************************************/
837 /*         SET CRT1 REGISTER GROUP           */
838 /*********************************************/
839
840 static void
841 SiS_SetCRT1Group(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
842                                         unsigned short ModeIdIndex)
843 {
844         unsigned short StandTableIndex, rrti;
845
846         SiS_Pr->SiS_CRT1Mode = ModeNo;
847
848         if (ModeNo <= 0x13)
849                 StandTableIndex = 0;
850         else
851                 StandTableIndex = 1;
852
853         SiS_ResetSegmentRegisters(SiS_Pr);
854         SiS_SetSeqRegs(SiS_Pr, StandTableIndex);
855         SiS_SetMiscRegs(SiS_Pr, StandTableIndex);
856         SiS_SetCRTCRegs(SiS_Pr, StandTableIndex);
857         SiS_SetATTRegs(SiS_Pr, StandTableIndex);
858         SiS_SetGRCRegs(SiS_Pr, StandTableIndex);
859         SiS_ClearExt1Regs(SiS_Pr, ModeNo);
860
861         rrti = SiS_GetRatePtr(SiS_Pr, ModeNo, ModeIdIndex);
862
863         if (rrti != 0xFFFF) {
864                 SiS_SetCRT1Sync(SiS_Pr, rrti);
865                 SiS_SetCRT1CRTC(SiS_Pr, ModeNo, ModeIdIndex, rrti);
866                 SiS_SetCRT1Offset(SiS_Pr, ModeNo, ModeIdIndex, rrti);
867                 SiS_SetCRT1VCLK(SiS_Pr, ModeNo, rrti);
868         }
869
870         SiS_SetCRT1FIFO_310(SiS_Pr, ModeNo, ModeIdIndex);
871
872         SiS_SetCRT1ModeRegs(SiS_Pr, ModeNo, ModeIdIndex, rrti);
873
874         SiS_LoadDAC(SiS_Pr, ModeNo, ModeIdIndex);
875
876         SiS_DisplayOn(SiS_Pr);
877 }
878
879 /*********************************************/
880 /*                 SiSSetMode()              */
881 /*********************************************/
882
883 int
884 SiSUSBSetMode(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
885 {
886         unsigned short ModeIdIndex;
887         unsigned long  BaseAddr = SiS_Pr->IOAddress;
888
889         SiSUSB_InitPtr(SiS_Pr);
890         SiSUSBRegInit(SiS_Pr, BaseAddr);
891         SiS_GetSysFlags(SiS_Pr);
892
893         if (!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex)))
894                 return 0;
895
896         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x05, 0x86);
897
898         SiSInitPCIetc(SiS_Pr);
899
900         ModeNo &= 0x7f;
901
902         SiS_Pr->SiS_ModeType =
903                 SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag & ModeTypeMask;
904
905         SiS_Pr->SiS_SetFlag = LowModeTests;
906
907         /* Set mode on CRT1 */
908         SiS_SetCRT1Group(SiS_Pr, ModeNo, ModeIdIndex);
909
910         SiS_HandleCRT1(SiS_Pr);
911
912         SiS_DisplayOn(SiS_Pr);
913         SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c6, 0xFF);
914
915         /* Store mode number */
916         SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x34, ModeNo);
917
918         return 1;
919 }
920
921 int
922 SiSUSBSetVESAMode(struct SiS_Private *SiS_Pr, unsigned short VModeNo)
923 {
924         unsigned short ModeNo = 0;
925         int i;
926
927         SiSUSB_InitPtr(SiS_Pr);
928
929         if (VModeNo == 0x03) {
930
931                 ModeNo = 0x03;
932
933         } else {
934
935                 i = 0;
936                 do {
937
938                         if (SiS_Pr->SiS_EModeIDTable[i].Ext_VESAID == VModeNo) {
939                                 ModeNo = SiS_Pr->SiS_EModeIDTable[i].Ext_ModeID;
940                                 break;
941                         }
942
943                 } while (SiS_Pr->SiS_EModeIDTable[i++].Ext_ModeID != 0xff);
944
945         }
946
947         if (!ModeNo)
948                 return 0;
949
950         return SiSUSBSetMode(SiS_Pr, ModeNo);
951 }
952
953 #endif /* INCL_SISUSB_CON */
954
955
956
957