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