]> git.sur5r.net Git - u-boot/blob - drivers/ct69000.c
* Patch by Detlev Zundel, 31 Mar 2005:
[u-boot] / drivers / ct69000.c
1 /*
2  * (C) Copyright 2002
3  * Denis Peter, MPL AG Switzerland
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * ported from ctfb.c (linux kernel) for the U-Boot
26  *
27  */
28
29
30 #include <common.h>
31
32 #ifdef CONFIG_VIDEO
33
34 #include <pci.h>
35 #include <video_fb.h>
36 #include "videomodes.h"
37
38 #ifdef CONFIG_VIDEO_CT69000
39
40 /* debug */
41 #undef VGA_DEBUG
42 #undef VGA_DUMP_REG
43 #ifdef VGA_DEBUG
44 #define PRINTF(fmt,args...)     printf (fmt ,##args)
45 #else
46 #define PRINTF(fmt,args...)
47 #endif
48
49 /* Macros */
50 #ifndef min
51 #define min( a, b ) ( ( a ) < ( b ) ) ? ( a ) : ( b )
52 #endif
53 #ifndef max
54 #define max( a, b ) ( ( a ) > ( b ) ) ? ( a ) : ( b )
55 #endif
56 #ifdef minmax
57 #error "term minmax already used."
58 #endif
59 #define minmax( a, x, b ) max( ( a ), min( ( x ), ( b ) ) )
60 #define N_ELTS( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) )
61
62 /* CT Register Offsets */
63 #define CT_AR_O                 0x3c0   /* Index and Data write port of the attribute Registers */
64 #define CT_GR_O                 0x3ce   /* Index port of the Graphic Controller Registers */
65 #define CT_SR_O                 0x3c4   /* Index port of the Sequencer Controller */
66 #define CT_CR_O                 0x3d4   /* Index port of the CRT Controller */
67 #define CT_XR_O                 0x3d6   /* Extended Register index */
68 #define CT_MSR_W_O              0x3c2   /* Misc. Output Register (write only) */
69 #define CT_LUT_MASK_O           0x3c6   /* Color Palette Mask */
70 #define CT_LUT_START_O          0x3c8   /* Color Palette Write Mode Index */
71 #define CT_LUT_RGB_O            0x3c9   /* Color Palette Data Port */
72 #define CT_STATUS_REG0_O        0x3c2   /* Status Register 0 (read only) */
73 #define CT_STATUS_REG1_O        0x3da   /* Input Status Register 1 (read only) */
74
75 #define CT_FP_O                 0x3d0   /* Index port of the Flat panel Registers */
76 #define CT_MR_O                 0x3d2   /* Index Port of the Multimedia Extension */
77
78 /* defines for the memory mapped registers */
79 #define BR00_o          0x400000        /* Source and Destination Span Register */
80 #define BR01_o          0x400004        /* Pattern/Source Expansion Background Color & Transparency Key Register */
81 #define BR02_o          0x400008        /* Pattern/Source Expansion Foreground Color Register */
82 #define BR03_o          0x40000C        /* Monochrome Source Control Register */
83 #define BR04_o          0x400010        /* BitBLT Control Register */
84 #define BR05_o          0x400014        /* Pattern Address Registe */
85 #define BR06_o          0x400018        /* Source Address Register */
86 #define BR07_o          0x40001C        /* Destination Address Register */
87 #define BR08_o          0x400020        /* Destination Width & Height Register */
88 #define BR09_o          0x400024        /* Source Expansion Background Color & Transparency Key Register */
89 #define BR0A_o          0x400028        /* Source Expansion Foreground Color Register */
90
91 #define CURSOR_SIZE     0x1000  /* in KByte for HW Cursor */
92 #define PATTERN_ADR     (pGD->dprBase + CURSOR_SIZE)    /* pattern Memory after Cursor Memory */
93 #define PATTERN_SIZE    8*8*4   /* 4 Bytes per Pixel 8 x 8 Pixel */
94 #define ACCELMEMORY     (CURSOR_SIZE + PATTERN_SIZE)    /* reserved Memory for BITBlt and hw cursor */
95
96 /* Some Mode definitions */
97 #define FB_SYNC_HOR_HIGH_ACT    1       /* horizontal sync high active  */
98 #define FB_SYNC_VERT_HIGH_ACT   2       /* vertical sync high active    */
99 #define FB_SYNC_EXT             4       /* external sync                */
100 #define FB_SYNC_COMP_HIGH_ACT   8       /* composite sync high active   */
101 #define FB_SYNC_BROADCAST       16      /* broadcast video timings      */
102                                         /* vtotal = 144d/288n/576i => PAL  */
103                                         /* vtotal = 121d/242n/484i => NTSC */
104 #define FB_SYNC_ON_GREEN        32      /* sync on green */
105
106 #define FB_VMODE_NONINTERLACED  0       /* non interlaced */
107 #define FB_VMODE_INTERLACED     1       /* interlaced   */
108 #define FB_VMODE_DOUBLE         2       /* double scan */
109 #define FB_VMODE_MASK           255
110
111 #define FB_VMODE_YWRAP          256     /* ywrap instead of panning     */
112 #define FB_VMODE_SMOOTH_XPAN    512     /* smooth xpan possible (internally used) */
113 #define FB_VMODE_CONUPDATE      512     /* don't update x/yoffset       */
114
115 #define text                    0
116 #define fntwidth                8
117
118 /* table for VGA Initialization  */
119 typedef struct {
120         const unsigned char reg;
121         const unsigned char val;
122 } CT_CFG_TABLE;
123
124 /* this table provides some basic initialisations such as Memory Clock etc */
125 static CT_CFG_TABLE xreg[] = {
126         {0x09, 0x01},           /* CRT Controller Extensions Enable */
127         {0x0A, 0x02},           /* Frame Buffer Mapping */
128         {0x0B, 0x01},           /* PCI Write Burst support */
129         {0x20, 0x00},           /* BitBLT Configuration */
130         {0x40, 0x03},           /* Memory Access Control */
131         {0x60, 0x00},           /* Video Pin Control */
132         {0x61, 0x00},           /* DPMS Synch control */
133         {0x62, 0x00},           /* GPIO Pin Control */
134         {0x63, 0xBD},           /* GPIO Pin Data */
135         {0x67, 0x00},           /* Pin Tri-State */
136         {0x80, 0x80},           /* Pixel Pipeline Config 0 register */
137         {0xA0, 0x00},           /* Cursor 1 Control Reg */
138         {0xA1, 0x00},           /* Cursor 1 Vertical Extension Reg */
139         {0xA2, 0x00},           /* Cursor 1 Base Address Low */
140         {0xA3, 0x00},           /* Cursor 1 Base Address High */
141         {0xA4, 0x00},           /* Cursor 1 X-Position Low */
142         {0xA5, 0x00},           /* Cursor 1 X-Position High */
143         {0xA6, 0x00},           /* Cursor 1 Y-Position Low */
144         {0xA7, 0x00},           /* Cursor 1 Y-Position High */
145         {0xA8, 0x00},           /* Cursor 2 Control Reg */
146         {0xA9, 0x00},           /* Cursor 2 Vertical Extension Reg */
147         {0xAA, 0x00},           /* Cursor 2 Base Address Low */
148         {0xAB, 0x00},           /* Cursor 2 Base Address High */
149         {0xAC, 0x00},           /* Cursor 2 X-Position Low */
150         {0xAD, 0x00},           /* Cursor 2 X-Position High */
151         {0xAE, 0x00},           /* Cursor 2 Y-Position Low */
152         {0xAF, 0x00},           /* Cursor 2 Y-Position High */
153         {0xC0, 0x7D},           /* Dot Clock 0 VCO M-Divisor */
154         {0xC1, 0x07},           /* Dot Clock 0 VCO N-Divisor */
155         {0xC3, 0x34},           /* Dot Clock 0 Divisor select */
156         {0xC4, 0x55},           /* Dot Clock 1 VCO M-Divisor */
157         {0xC5, 0x09},           /* Dot Clock 1 VCO N-Divisor */
158         {0xC7, 0x24},           /* Dot Clock 1 Divisor select */
159         {0xC8, 0x7D},           /* Dot Clock 2 VCO M-Divisor */
160         {0xC9, 0x07},           /* Dot Clock 2 VCO N-Divisor */
161         {0xCB, 0x34},           /* Dot Clock 2 Divisor select */
162         {0xCC, 0x38},           /* Memory Clock 0 VCO M-Divisor */
163         {0xCD, 0x03},           /* Memory Clock 0 VCO N-Divisor */
164         {0xCE, 0x90},           /* Memory Clock 0 Divisor select */
165         {0xCF, 0x06},           /* Clock Config */
166         {0xD0, 0x0F},           /* Power Down */
167         {0xD1, 0x01},           /* Power Down BitBLT */
168         {0xFF, 0xFF}            /* end of table */
169 };
170 /* Clock Config:
171  * =============
172  *
173  * PD Registers:
174  * -------------
175  * Bit2 and Bit4..6 are used for the Loop Divisor and Post Divisor.
176  * They are encoded as follows:
177  *
178  * +---+--------------+
179  * | 2 | Loop Divisor |
180  * +---+--------------+
181  * | 1 | 1            |
182  * +---+--------------+
183  * | 0 | 4            |
184  * +---+--------------+
185  * Note: The Memory Clock does not have a Loop Divisor.
186  * +---+---+---+--------------+
187  * | 6 | 5 | 4 | Post Divisor |
188  * +---+---+---+--------------+
189  * | 0 | 0 | 0 | 1            |
190  * +---+---+---+--------------+
191  * | 0 | 0 | 1 | 2            |
192  * +---+---+---+--------------+
193  * | 0 | 1 | 0 | 4            |
194  * +---+---+---+--------------+
195  * | 0 | 1 | 1 | 8            |
196  * +---+---+---+--------------+
197  * | 1 | 0 | 0 | 16           |
198  * +---+---+---+--------------+
199  * | 1 | 0 | 1 | 32           |
200  * +---+---+---+--------------+
201  * | 1 | 1 | X | reserved     |
202  * +---+---+---+--------------+
203  *
204  * All other bits are reserved in these registers.
205  *
206  * Clock VCO M Registers:
207  * ----------------------
208  * These Registers contain the M Value -2.
209  *
210  * Clock VCO N Registers:
211  * ----------------------
212  * These Registers contain the N Value -2.
213  *
214  * Formulas:
215  * ---------
216  * Fvco = (Fref * Loop Divisor * M/N), whereas 100MHz < Fvco < 220MHz
217  * Fout = Fvco / Post Divisor
218  *
219  * Dot Clk0 (default 25MHz):
220  * -------------------------
221  * Fvco = 14.318 * 127 / 9 = 202.045MHz
222  * Fout = 202.045MHz / 8 = 25.25MHz
223  * Post Divisor = 8
224  * Loop Divisor = 1
225  * XRC0 = (M - 2) = 125 = 0x7D
226  * XRC1 = (N - 2) = 7   = 0x07
227  * XRC3 =                 0x34
228  *
229  * Dot Clk1 (default 28MHz):
230  * -------------------------
231  * Fvco = 14.318 * 87 / 11 = 113.24MHz
232  * Fout = 113.24MHz / 4 = 28.31MHz
233  * Post Divisor = 4
234  * Loop Divisor = 1
235  * XRC4 = (M - 2) = 85 = 0x55
236  * XRC5 = (N - 2) = 9  = 0x09
237  * XRC7 =                0x24
238  *
239  * Dot Clk2 (variable for extended modes set to 25MHz):
240  * ----------------------------------------------------
241  * Fvco = 14.318 * 127 / 9 = 202.045MHz
242  * Fout = 202.045MHz / 8 = 25.25MHz
243  * Post Divisor = 8
244  * Loop Divisor = 1
245  * XRC8 = (M - 2) = 125 = 0x7D
246  * XRC9 = (N - 2) = 7   = 0x07
247  * XRCB =                 0x34
248  *
249  * Memory Clk for most modes >50MHz:
250  * ----------------------------------
251  * Fvco = 14.318 * 58 / 5 = 166MHz
252  * Fout = 166MHz / 2      = 83MHz
253  * Post Divisor = 2
254  * XRCC = (M - 2) = 57  = 0x38
255  * XRCD = (N - 2) = 3   = 0x03
256  * XRCE =                 0x90
257  *
258  * Note Bit7 enables the clock source from the VCO
259  *
260  */
261
262 /*******************************************************************
263  * Chips struct
264  *******************************************************************/
265 struct ctfb_chips_properties {
266         int device_id;          /* PCI Device ID */
267         unsigned long max_mem;  /* memory for frame buffer */
268         int vld_set;            /* value of VLD if bit2 in clock control is set */
269         int vld_not_set;        /* value of VLD if bit2 in clock control is set */
270         int mn_diff;            /* difference between M/N Value + mn_diff = M/N Register */
271         int mn_min;             /* min value of M/N Value */
272         int mn_max;             /* max value of M/N Value */
273         int vco_min;            /* VCO Min in MHz */
274         int vco_max;            /* VCO Max in MHz */
275 };
276
277 static const struct ctfb_chips_properties chips[] = {
278         {PCI_DEVICE_ID_CT_69000, 0x200000, 1, 4, -2, 3, 257, 100, 220},
279         {PCI_DEVICE_ID_CT_65555, 0x100000, 16, 4, 0, 1, 255, 48, 220},  /* NOT TESTED */
280         {0, 0, 0, 0, 0, 0, 0, 0, 0}     /* Terminator */
281 };
282
283 /*
284  * The Graphic Device
285  */
286 GraphicDevice ctfb;
287
288 /*******************************************************************************
289 *
290 * Low Level Routines
291 */
292
293 /*******************************************************************************
294 *
295 * Read CT ISA register
296 */
297 #ifdef VGA_DEBUG
298 static unsigned char
299 ctRead (unsigned short index)
300 {
301         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
302         if (index == CT_AR_O)
303                 /* synch the Flip Flop */
304                 in8 (pGD->isaBase + CT_STATUS_REG1_O);
305
306         return (in8 (pGD->isaBase + index));
307 }
308 #endif
309 /*******************************************************************************
310 *
311 * Write CT ISA register
312 */
313 static void
314 ctWrite (unsigned short index, unsigned char val)
315 {
316         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
317
318         out8 ((pGD->isaBase + index), val);
319 }
320
321 /*******************************************************************************
322 *
323 * Read CT ISA register indexed
324 */
325 static unsigned char
326 ctRead_i (unsigned short index, char reg)
327 {
328         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
329         if (index == CT_AR_O)
330                 /* synch the Flip Flop */
331                 in8 (pGD->isaBase + CT_STATUS_REG1_O);
332         out8 ((pGD->isaBase + index), reg);
333         return (in8 (pGD->isaBase + index + 1));
334 }
335
336 /*******************************************************************************
337 *
338 * Write CT ISA register indexed
339 */
340 static void
341 ctWrite_i (unsigned short index, char reg, char val)
342 {
343         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
344         if (index == CT_AR_O) {
345                 /* synch the Flip Flop */
346                 in8 (pGD->isaBase + CT_STATUS_REG1_O);
347                 out8 ((pGD->isaBase + index), reg);
348                 out8 ((pGD->isaBase + index), val);
349         } else {
350                 out8 ((pGD->isaBase + index), reg);
351                 out8 ((pGD->isaBase + index + 1), val);
352         }
353 }
354
355 /*******************************************************************************
356 *
357 * Write a table of CT ISA register
358 */
359 static void
360 ctLoadRegs (unsigned short index, CT_CFG_TABLE * regTab)
361 {
362         while (regTab->reg != 0xFF) {
363                 ctWrite_i (index, regTab->reg, regTab->val);
364                 regTab++;
365         }
366 }
367
368 /*****************************************************************************/
369 static void
370 SetArRegs (void)
371 {
372         int i, tmp;
373
374         for (i = 0; i < 0x10; i++)
375                 ctWrite_i (CT_AR_O, i, i);
376         if (text)
377                 tmp = 0x04;
378         else
379                 tmp = 0x41;
380
381         ctWrite_i (CT_AR_O, 0x10, tmp); /* Mode Control Register */
382         ctWrite_i (CT_AR_O, 0x11, 0x00);        /* Overscan Color Register */
383         ctWrite_i (CT_AR_O, 0x12, 0x0f);        /* Memory Plane Enable Register */
384         if (fntwidth == 9)
385                 tmp = 0x08;
386         else
387                 tmp = 0x00;
388         ctWrite_i (CT_AR_O, 0x13, tmp); /* Horizontal Pixel Panning */
389         ctWrite_i (CT_AR_O, 0x14, 0x00);        /* Color Select Register    */
390         ctWrite (CT_AR_O, 0x20);        /* enable video             */
391 }
392
393 /*****************************************************************************/
394 static void
395 SetGrRegs (void)
396 {                               /* Set Graphics Mode */
397         int i;
398
399         for (i = 0; i < 0x05; i++)
400                 ctWrite_i (CT_GR_O, i, 0);
401         if (text) {
402                 ctWrite_i (CT_GR_O, 0x05, 0x10);
403                 ctWrite_i (CT_GR_O, 0x06, 0x02);
404         } else {
405                 ctWrite_i (CT_GR_O, 0x05, 0x40);
406                 ctWrite_i (CT_GR_O, 0x06, 0x05);
407         }
408         ctWrite_i (CT_GR_O, 0x07, 0x0f);
409         ctWrite_i (CT_GR_O, 0x08, 0xff);
410 }
411
412 /*****************************************************************************/
413 static void
414 SetSrRegs (void)
415 {
416         int tmp = 0;
417
418         ctWrite_i (CT_SR_O, 0x00, 0x00);        /* reset */
419         /*rr( sr, 0x01, tmp );
420            if( fntwidth == 8 ) tmp |= 0x01; else tmp &= ~0x01;
421            wr( sr, 0x01, tmp );  */
422         if (fntwidth == 8)
423                 ctWrite_i (CT_SR_O, 0x01, 0x01);        /* Clocking Mode Register */
424         else
425                 ctWrite_i (CT_SR_O, 0x01, 0x00);        /* Clocking Mode Register */
426         ctWrite_i (CT_SR_O, 0x02, 0x0f);        /* Enable CPU wr access to given memory plane */
427         ctWrite_i (CT_SR_O, 0x03, 0x00);        /* Character Map Select Register */
428         if (text)
429                 tmp = 0x02;
430         else
431                 tmp = 0x0e;
432         ctWrite_i (CT_SR_O, 0x04, tmp); /* Enable CPU accesses to the rest of the 256KB
433                                            total VGA memory beyond the first 64KB and set
434                                            fb mapping mode. */
435         ctWrite_i (CT_SR_O, 0x00, 0x03);        /* enable */
436 }
437
438 /*****************************************************************************/
439 static void
440 SetBitsPerPixelIntoXrRegs (int bpp)
441 {
442         unsigned int n = (bpp >> 3), tmp;       /* only for 15, 8, 16, 24 bpp */
443         static char md[4] = { 0x04, 0x02, 0x05, 0x06 }; /* DisplayColorMode */
444         static char off[4] = { ~0x20, ~0x30, ~0x20, ~0x10 };    /* mask */
445         static char on[4] = { 0x10, 0x00, 0x10, 0x20 }; /* mask */
446         if (bpp == 15)
447                 n = 0;
448         tmp = ctRead_i (CT_XR_O, 0x20);
449         tmp &= off[n];
450         tmp |= on[n];
451         ctWrite_i (CT_XR_O, 0x20, tmp); /* BitBLT Configuration */
452         ctWrite_i (CT_XR_O, 0x81, md[n]);
453 }
454
455 /*****************************************************************************/
456 static void
457 SetCrRegs (struct ctfb_res_modes *var, int bits_per_pixel)
458 {                               /* he -le-   ht|0    hd -ri- hs     -h-      he */
459         unsigned char cr[0x7a];
460         int i, tmp;
461         unsigned int hd, hs, he, ht, hbe;       /* Horizontal.  */
462         unsigned int vd, vs, ve, vt;    /* vertical */
463         unsigned int bpp, wd, dblscan, interlaced, bcast, CrtHalfLine;
464         unsigned int CompSyncCharClkDelay, CompSyncPixelClkDelay;
465         unsigned int NTSC_PAL_HorizontalPulseWidth, BlDelayCtrl;
466         unsigned int HorizontalEqualizationPulses;
467         unsigned int HorizontalSerration1Start, HorizontalSerration2Start;
468
469         const int LineCompare = 0x3ff;
470         unsigned int TextScanLines = 1; /* this is in fact a vertical zoom factor   */
471         unsigned int RAMDAC_BlankPedestalEnable = 0;    /* 1=en-, 0=disable, see XR82 */
472
473         hd = (var->xres) / 8;   /* HDisp.  */
474         hs = (var->xres + var->right_margin) / 8;       /* HsStrt  */
475         he = (var->xres + var->right_margin + var->hsync_len) / 8;      /* HsEnd   */
476         ht = (var->left_margin + var->xres + var->right_margin + var->hsync_len) / 8;   /* HTotal  */
477         hbe = ht - 1;           /* HBlankEnable todo docu wants ht here, but it does not work */
478         /* ve -up-  vt|0    vd -lo- vs     -v-      ve */
479         vd = var->yres;         /* VDisplay   */
480         vs = var->yres + var->lower_margin;     /* VSyncStart */
481         ve = var->yres + var->lower_margin + var->vsync_len;    /* VSyncEnd */
482         vt = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;        /* VTotal  */
483         bpp = bits_per_pixel;
484         dblscan = (var->vmode & FB_VMODE_DOUBLE) ? 1 : 0;
485         interlaced = var->vmode & FB_VMODE_INTERLACED;
486         bcast = var->sync & FB_SYNC_BROADCAST;
487         CrtHalfLine = bcast ? (hd >> 1) : 0;
488         BlDelayCtrl = bcast ? 1 : 0;
489         CompSyncCharClkDelay = 0;       /* 2 bit */
490         CompSyncPixelClkDelay = 0;      /* 3 bit */
491         if (bcast) {
492                 NTSC_PAL_HorizontalPulseWidth = 7;      /*( var->hsync_len >> 1 ) + 1 */
493                 HorizontalEqualizationPulses = 0;       /* inverse value */
494                 HorizontalSerration1Start = 31; /* ( ht >> 1 ) */
495                 HorizontalSerration2Start = 89; /* ( ht >> 1 ) */
496         } else {
497                 NTSC_PAL_HorizontalPulseWidth = 0;
498                 /* 4 bit: hsync pulse width = ( ( CR74[4:0] - CR74[5] )
499                  * / 2 ) + 1 --> CR74[4:0] = 2*(hs-1) + CR74[5] */
500                 HorizontalEqualizationPulses = 1;       /* inverse value */
501                 HorizontalSerration1Start = 0;  /* ( ht >> 1 ) */
502                 HorizontalSerration2Start = 0;  /* ( ht >> 1 ) */
503         }
504
505         if (bpp == 15)
506                 bpp = 16;
507         wd = var->xres * bpp / 64;      /* double words per line */
508         if (interlaced) {       /* we divide all vertical timings, exept vd */
509                 vs >>= 1;
510                 ve >>= 1;
511                 vt >>= 1;
512         }
513         memset (cr, 0, sizeof (cr));
514         cr[0x00] = 0xff & (ht - 5);
515         cr[0x01] = hd - 1;      /* soll:4f ist 59 */
516         cr[0x02] = hd;
517         cr[0x03] = (hbe & 0x1F) | 0x80; /* hd + ht - hd  */
518         cr[0x04] = hs;
519         cr[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
520         cr[0x06] = (vt - 2) & 0xFF;
521         cr[0x30] = (vt - 2) >> 8;
522         cr[0x07] = ((vt & 0x100) >> 8)
523             | ((vd & 0x100) >> 7)
524             | ((vs & 0x100) >> 6)
525             | ((vs & 0x100) >> 5)
526             | ((LineCompare & 0x100) >> 4)
527             | ((vt & 0x200) >> 4)
528             | ((vd & 0x200) >> 3)
529             | ((vs & 0x200) >> 2);
530         cr[0x08] = 0x00;
531         cr[0x09] = (dblscan << 7)
532             | ((LineCompare & 0x200) >> 3)
533             | ((vs & 0x200) >> 4)
534             | (TextScanLines - 1);
535         cr[0x10] = vs & 0xff;   /* VSyncPulseStart */
536         cr[0x32] = (vs & 0xf00) >> 8;   /* VSyncPulseStart */
537         cr[0x11] = (ve & 0x0f); /* | 0x20;      */
538         cr[0x12] = (vd - 1) & 0xff;     /* LineCount  */
539         cr[0x31] = ((vd - 1) & 0xf00) >> 8;     /* LineCount */
540         cr[0x13] = wd & 0xff;
541         cr[0x41] = (wd & 0xf00) >> 8;
542         cr[0x15] = vs & 0xff;
543         cr[0x33] = (vs & 0xf00) >> 8;
544         cr[0x38] = (0x100 & (ht - 5)) >> 8;
545         cr[0x3C] = 0xc0 & hbe;
546         cr[0x16] = (vt - 1) & 0xff;     /* vbe - docu wants vt here, */
547         cr[0x17] = 0xe3;        /* but it does not work */
548         cr[0x18] = 0xff & LineCompare;
549         cr[0x22] = 0xff;        /* todo? */
550         cr[0x70] = interlaced ? (0x80 | CrtHalfLine) : 0x00;    /* check:0xa6  */
551         cr[0x71] = 0x80 | (RAMDAC_BlankPedestalEnable << 6)
552             | (BlDelayCtrl << 5)
553             | ((0x03 & CompSyncCharClkDelay) << 3)
554             | (0x07 & CompSyncPixelClkDelay);   /* todo: see XR82 */
555         cr[0x72] = HorizontalSerration1Start;
556         cr[0x73] = HorizontalSerration2Start;
557         cr[0x74] = (HorizontalEqualizationPulses << 5)
558             | NTSC_PAL_HorizontalPulseWidth;
559         /* todo: ct69000 has also 0x75-79 */
560         /* now set the registers */
561         for (i = 0; i <= 0x0d; i++) {   /*CR00 .. CR0D */
562                 ctWrite_i (CT_CR_O, i, cr[i]);
563         }
564         for (i = 0x10; i <= 0x18; i++) {        /*CR10 .. CR18 */
565                 ctWrite_i (CT_CR_O, i, cr[i]);
566         }
567         i = 0x22;               /*CR22 */
568         ctWrite_i (CT_CR_O, i, cr[i]);
569         for (i = 0x30; i <= 0x33; i++) {        /*CR30 .. CR33 */
570                 ctWrite_i (CT_CR_O, i, cr[i]);
571         }
572         i = 0x38;               /*CR38 */
573         ctWrite_i (CT_CR_O, i, cr[i]);
574         i = 0x3C;               /*CR3C */
575         ctWrite_i (CT_CR_O, i, cr[i]);
576         for (i = 0x40; i <= 0x41; i++) {        /*CR40 .. CR41 */
577                 ctWrite_i (CT_CR_O, i, cr[i]);
578         }
579         for (i = 0x70; i <= 0x74; i++) {        /*CR70 .. CR74 */
580                 ctWrite_i (CT_CR_O, i, cr[i]);
581         }
582         tmp = ctRead_i (CT_CR_O, 0x40);
583         tmp &= 0x0f;
584         tmp |= 0x80;
585         ctWrite_i (CT_CR_O, 0x40, tmp); /* StartAddressEnable */
586 }
587
588 /* pixelclock control */
589
590 /*****************************************************************************
591  We have a rational number p/q and need an m/n which is very close to p/q
592  but has m and n within mnmin and mnmax. We have no floating point in the
593  kernel. We can use long long without divide. And we have time to compute...
594 ******************************************************************************/
595 static unsigned int
596 FindBestPQFittingMN (unsigned int p, unsigned int q, unsigned int mnmin,
597                      unsigned int mnmax, unsigned int *pm, unsigned int *pn)
598 {
599         /* this code is not for general purpose usable but good for our number ranges */
600         unsigned int n = mnmin, m = 0;
601         long long int L = 0, P = p, Q = q, H = P >> 1;
602         long long int D = 0x7ffffffffffffffLL;
603         for (n = mnmin; n <= mnmax; n++) {
604                 m = mnmin;      /* p/q ~ m/n -> p*n ~ m*q -> p*n-x*q ~ 0 */
605                 L = P * n - m * Q;      /* n * vco - m * fref should be near 0 */
606                 while (L > 0 && m < mnmax) {
607                         L -= q; /* difference is greater as 0 subtract fref */
608                         m++;    /* and increment m */
609                 }
610                 /* difference is less or equal than 0 or m > maximum */
611                 if (m > mnmax)
612                         break;  /* no solution: if we increase n we get the same situation */
613                 /* L is <= 0 now */
614                 if (-L > H && m > mnmin) {      /* if difference > the half fref */
615                         L += q; /* we take the situation before */
616                         m--;    /* because its closer to 0 */
617                 }
618                 L = (L < 0) ? -L : +L;  /* absolute value */
619                 if (D < L)      /* if last difference was better take next n */
620                         continue;
621                 D = L;
622                 *pm = m;
623                 *pn = n;        /*  keep improved data */
624                 if (D == 0)
625                         break;  /* best result we can get */
626         }
627         return (unsigned int) (0xffffffff & D);
628 }
629
630 /* that is the hardware < 69000 we have to manage
631  +---------+  +-------------------+  +----------------------+  +--+
632  | REFCLK  |__|NTSC Divisor Select|__|FVCO Reference Divisor|__|÷N|__
633  | 14.3MHz |  |(NTSCDS) (÷1, Ã·5)  |  |Select (RDS) (÷1, Ã·4) |  |  |  |
634  +---------+  +-------------------+  +----------------------+  +--+  |
635   ___________________________________________________________________|
636  |
637  |                                    fvco                      fout
638  | +--------+  +------------+  +-----+     +-------------------+   +----+
639  +-| Phase  |__|Charge Pump |__| VCO |_____|Post Divisor (PD)  |___|CLK |--->
640  +-| Detect |  |& Filter VCO|  |     |  |  |÷1, 2, 4, 8, 16, 32|   |    |
641  | +--------+  +------------+  +-----+  |  +-------------------+   +----+
642  |                                      |
643  |    +--+   +---------------+          |
644  |____|÷M|___|VCO Loop Divide|__________|
645       |  |   |(VLD)(÷4, Ã·16) |
646       +--+   +---------------+
647 ****************************************************************************
648   that is the hardware >= 69000 we have to manage
649  +---------+  +--+
650  | REFCLK  |__|÷N|__
651  | 14.3MHz |  |  |  |
652  +---------+  +--+  |
653   __________________|
654  |
655  |                                    fvco                      fout
656  | +--------+  +------------+  +-----+     +-------------------+   +----+
657  +-| Phase  |__|Charge Pump |__| VCO |_____|Post Divisor (PD)  |___|CLK |--->
658  +-| Detect |  |& Filter VCO|  |     |  |  |÷1, 2, 4, 8, 16, 32|   |    |
659  | +--------+  +------------+  +-----+  |  +-------------------+   +----+
660  |                                      |
661  |    +--+   +---------------+          |
662  |____|÷M|___|VCO Loop Divide|__________|
663       |  |   |(VLD)(÷1, Ã·4)  |
664       +--+   +---------------+
665
666
667 */
668
669 #define VIDEO_FREF 14318180;    /* Hz  */
670 /*****************************************************************************/
671 static int
672 ReadPixClckFromXrRegsBack (struct ctfb_chips_properties *param)
673 {
674         unsigned int m, n, vld, pd, PD, fref, xr_cb, i, pixclock;
675         i = 0;
676         pixclock = -1;
677         fref = VIDEO_FREF;
678         m = ctRead_i (CT_XR_O, 0xc8);
679         n = ctRead_i (CT_XR_O, 0xc9);
680         m -= param->mn_diff;
681         n -= param->mn_diff;
682         xr_cb = ctRead_i (CT_XR_O, 0xcb);
683         PD = (0x70 & xr_cb) >> 4;
684         pd = 1;
685         for (i = 0; i < PD; i++) {
686                 pd *= 2;
687         }
688         vld = (0x04 & xr_cb) ? param->vld_set : param->vld_not_set;
689         if (n * vld * m) {
690                 unsigned long long p = 1000000000000LL * pd * n;
691                 unsigned long long q = (long long) fref * vld * m;
692                 while ((p > 0xffffffffLL) || (q > 0xffffffffLL)) {
693                         p >>= 1;        /* can't divide with long long so we scale down */
694                         q >>= 1;
695                 }
696                 pixclock = (unsigned) p / (unsigned) q;
697         } else
698                 printf ("Invalid data in xr regs.\n");
699         return pixclock;
700 }
701
702 /*****************************************************************************/
703 static void
704 FindAndSetPllParamIntoXrRegs (unsigned int pixelclock,
705                               struct ctfb_chips_properties *param)
706 {
707         unsigned int m, n, vld, pd, PD, fref, xr_cb;
708         unsigned int fvcomin, fvcomax, pclckmin, pclckmax, pclk;
709         unsigned int pfreq, fvco, new_pixclock;
710         unsigned int D,nback,mback;
711
712         fref = VIDEO_FREF;
713         pd = 1;
714         PD = 0;
715         fvcomin = param->vco_min;
716         fvcomax = param->vco_max;       /* MHz */
717         pclckmin = 1000000 / fvcomax + 1;       /*   4546 */
718         pclckmax = 32000000 / fvcomin - 1;      /* 666665 */
719         pclk = minmax (pclckmin, pixelclock, pclckmax); /* ps pp */
720         pfreq = 250 * (4000000000U / pclk);
721         fvco = pfreq;           /* Hz */
722         new_pixclock = 0;
723         while (fvco < fvcomin * 1000000) {
724                 /* double VCO starting with the pixelclock frequency
725                  * as long as it is lower than the minimal VCO frequency */
726                 fvco *= 2;
727                 pd *= 2;
728                 PD++;
729         }
730         /* fvco is exactly pd * pixelclock and higher than the ninmal VCO frequency */
731         /* first try */
732         vld = param->vld_set;
733         D=FindBestPQFittingMN (fvco / vld, fref, param->mn_min, param->mn_max, &m, &n); /* rds = 1 */
734         mback=m;
735         nback=n;
736         /* second try */
737         vld = param->vld_not_set;
738         if(D<FindBestPQFittingMN (fvco / vld, fref, param->mn_min, param->mn_max, &m, &n)) {    /* rds = 1 */
739                 /* first try was better */
740                 m=mback;
741                 n=nback;
742                 vld = param->vld_set;
743         }
744         m += param->mn_diff;
745         n += param->mn_diff;
746         PRINTF ("VCO %d, pd %d, m %d n %d vld %d \n", fvco, pd, m, n, vld);
747         xr_cb = ((0x7 & PD) << 4) | (vld == param->vld_set ? 0x04 : 0);
748         /* All four of the registers used for dot clock 2 (XRC8 - XRCB) must be
749          * written, and in order from XRC8 to XRCB, before the hardware will
750          * update the synthesizer s settings.
751          */
752         ctWrite_i (CT_XR_O, 0xc8, m);
753         ctWrite_i (CT_XR_O, 0xc9, n);   /* xrca does not exist in CT69000 and CT69030 */
754         ctWrite_i (CT_XR_O, 0xca, 0);   /* because of a hw bug I guess, but we write */
755         ctWrite_i (CT_XR_O, 0xcb, xr_cb);       /* 0 to it for savety */
756         new_pixclock = ReadPixClckFromXrRegsBack (param);
757         PRINTF ("pixelclock.set = %d, pixelclock.real = %d \n",
758                 pixelclock, new_pixclock);
759 }
760
761 /*****************************************************************************/
762 static void
763 SetMsrRegs (struct ctfb_res_modes *mode)
764 {
765         unsigned char h_synch_high, v_synch_high;
766
767         h_synch_high = (mode->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 0x40;  /* horizontal Synch High active */
768         v_synch_high = (mode->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 0x80; /* vertical Synch High active */
769         ctWrite (CT_MSR_W_O, (h_synch_high | v_synch_high | 0x29));
770         /* upper64K==0x20, CLC2select==0x08, RAMenable==0x02!(todo), CGA==0x01
771          * Selects the upper 64KB page.Bit5=1
772          * CLK2 (left reserved in standard VGA) Bit3|2=1|0
773          * Disables CPU access to frame buffer. Bit1=0
774          * Sets the I/O address decode for ST01, FCR, and all CR registers
775          * to the 3Dx I/O address range (CGA emulation). Bit0=1
776          */
777 }
778
779 /************************************************************************************/
780 #ifdef VGA_DUMP_REG
781
782 static void
783 ctDispRegs (unsigned short index, int from, int to)
784 {
785         unsigned char status;
786         int i;
787
788         for (i = from; i < to; i++) {
789                 status = ctRead_i (index, i);
790                 printf ("%02X: is %02X\n", i, status);
791         }
792 }
793
794 void
795 video_dump_reg (void)
796 {
797         int i;
798
799         printf ("Extended Regs:\n");
800         ctDispRegs (CT_XR_O, 0, 0xC);
801         ctDispRegs (CT_XR_O, 0xe, 0xf);
802         ctDispRegs (CT_XR_O, 0x20, 0x21);
803         ctDispRegs (CT_XR_O, 0x40, 0x50);
804         ctDispRegs (CT_XR_O, 0x60, 0x64);
805         ctDispRegs (CT_XR_O, 0x67, 0x68);
806         ctDispRegs (CT_XR_O, 0x70, 0x72);
807         ctDispRegs (CT_XR_O, 0x80, 0x83);
808         ctDispRegs (CT_XR_O, 0xA0, 0xB0);
809         ctDispRegs (CT_XR_O, 0xC0, 0xD3);
810         printf ("Sequencer Regs:\n");
811         ctDispRegs (CT_SR_O, 0, 0x8);
812         printf ("Graphic Regs:\n");
813         ctDispRegs (CT_GR_O, 0, 0x9);
814         printf ("CRT Regs:\n");
815         ctDispRegs (CT_CR_O, 0, 0x19);
816         ctDispRegs (CT_CR_O, 0x22, 0x23);
817         ctDispRegs (CT_CR_O, 0x30, 0x34);
818         ctDispRegs (CT_CR_O, 0x38, 0x39);
819         ctDispRegs (CT_CR_O, 0x3C, 0x3D);
820         ctDispRegs (CT_CR_O, 0x40, 0x42);
821         ctDispRegs (CT_CR_O, 0x70, 0x80);
822         /* don't display the attributes */
823 }
824
825 #endif
826
827 #ifdef CONFIG_VIDEO_HW_CURSOR
828 /***************************************************************
829  * Set Hardware Cursor in Pixel
830  */
831 void
832 video_set_hw_cursor (int x, int y)
833 {
834         int sig_x = 0, sig_y = 0;
835         if (x < 0) {
836                 x *= -1;
837                 sig_x = 1;
838         }
839         if (y < 0) {
840                 y *= -1;
841                 sig_y = 1;
842         }
843         ctWrite_i (CT_XR_O, 0xa4, x & 0xff);
844         ctWrite_i (CT_XR_O, 0xa5, (x >> 8) & 0x7);
845         ctWrite_i (CT_XR_O, 0xa6, y & 0xff);
846         ctWrite_i (CT_XR_O, 0xa7, (y >> 8) & 0x7);
847 }
848
849 /***************************************************************
850  * Init Hardware Cursor. To know the size of the Cursor,
851  * we have to know the Font size.
852  */
853 void
854 video_init_hw_cursor (int font_width, int font_height)
855 {
856         unsigned char xr_80;
857         unsigned long *curs, pattern;
858         int i;
859         int cursor_start;
860         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
861
862         cursor_start = pGD->dprBase;
863         xr_80 = ctRead_i (CT_XR_O, 0x80);
864         /* set start address */
865         ctWrite_i (CT_XR_O, 0xa2, (cursor_start >> 8) & 0xf0);
866         ctWrite_i (CT_XR_O, 0xa3, (cursor_start >> 16) & 0x3f);
867         /* set cursor shape */
868         curs = (unsigned long *) cursor_start;
869         i = 0;
870         while (i < 0x400) {
871                 curs[i++] = 0xffffffff; /* AND mask */
872                 curs[i++] = 0xffffffff; /* AND mask */
873                 curs[i++] = 0;  /* XOR mask */
874                 curs[i++] = 0;  /* XOR mask */
875                 /* Transparent */
876         }
877         pattern = 0xffffffff >> font_width;
878         i = 0;
879         while (i < (font_height * 2)) {
880                 curs[i++] = pattern;    /* AND mask */
881                 curs[i++] = pattern;    /* AND mask */
882                 curs[i++] = 0;  /* XOR mask */
883                 curs[i++] = 0;  /* XOR mask */
884                 /* Cursor Color 0 */
885         }
886         /* set blink rate */
887         ctWrite_i (CT_FP_O, 0x19, 0xf);
888
889         /* set cursors colors */
890         xr_80 = ctRead_i (CT_XR_O, 0x80);
891         xr_80 |= 0x1;           /* alternate palette select */
892         ctWrite_i (CT_XR_O, 0x80, xr_80);
893         video_set_lut (4, CONSOLE_FG_COL, CONSOLE_FG_COL, CONSOLE_FG_COL);
894         /* position 4 is color 0 cursor 0 */
895         xr_80 &= 0xfe;          /* normal palette select */
896         ctWrite_i (CT_XR_O, 0x80, xr_80);
897         /* cursor enable */
898         ctWrite_i (CT_XR_O, 0xa0, 0x91);
899         xr_80 |= 0x10;          /* enable hwcursor */
900         ctWrite_i (CT_XR_O, 0x80, xr_80);
901         video_set_hw_cursor (0, 0);
902 }
903 #endif                          /* CONFIG_VIDEO_HW_CURSOR */
904
905 /***************************************************************
906  * Wait for BitBlt ready
907  */
908 static int
909 video_wait_bitblt (unsigned long addr)
910 {
911         unsigned long br04;
912         int i = 0;
913         br04 = in32r (addr);
914         while (br04 & 0x80000000) {
915                 udelay (1);
916                 br04 = in32r (addr);
917                 if (i++ > 1000000) {
918                         printf ("ERROR Timeout %lx\n", br04);
919                         return 1;
920                 }
921         }
922         return 0;
923 }
924
925 /***************************************************************
926  * Set up BitBlt Registrs
927  */
928 static void
929 SetDrawingEngine (int bits_per_pixel)
930 {
931         unsigned long br04, br00;
932         unsigned char tmp;
933
934         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
935
936         tmp = ctRead_i (CT_XR_O, 0x20); /* BitBLT Configuration */
937         tmp |= 0x02;            /* reset BitBLT */
938         ctWrite_i (CT_XR_O, 0x20, tmp); /* BitBLT Configuration */
939         udelay (10);
940         tmp &= 0xfd;            /* release reset BitBLT */
941         ctWrite_i (CT_XR_O, 0x20, tmp); /* BitBLT Configuration */
942         video_wait_bitblt (pGD->pciBase + BR04_o);
943
944         /* set pattern Address */
945         out32r (pGD->pciBase + BR05_o, PATTERN_ADR & 0x003ffff8);
946         br04 = 0;
947         if (bits_per_pixel == 1) {
948                 br04 |= 0x00040000;     /* monochome Pattern */
949                 br04 |= 0x00001000;     /* monochome source */
950         }
951         br00 = ((pGD->winSizeX * pGD->gdfBytesPP) << 16) + (pGD->winSizeX * pGD->gdfBytesPP);   /* bytes per scanline */
952         out32r (pGD->pciBase + BR00_o, br00);   /* */
953         out32r (pGD->pciBase + BR08_o, (10 << 16) + 10);        /* dummy */
954         out32r (pGD->pciBase + BR04_o, br04);   /* write all 0 */
955         out32r (pGD->pciBase + BR07_o, 0);      /* destination */
956         video_wait_bitblt (pGD->pciBase + BR04_o);
957 }
958
959 /****************************************************************************
960 * supported Video Chips
961 */
962 static struct pci_device_id supported[] = {
963         {PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_69000},
964         {}
965 };
966
967 /*******************************************************************************
968 *
969 * Init video chip
970 */
971 void *
972 video_hw_init (void)
973 {
974         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
975         unsigned short device_id;
976         pci_dev_t devbusfn;
977         int videomode;
978         unsigned long t1, hsynch, vsynch;
979         unsigned int pci_mem_base, *vm;
980         int tmp, i, bits_per_pixel;
981         char *penv;
982         struct ctfb_res_modes *res_mode;
983         struct ctfb_res_modes var_mode;
984         struct ctfb_chips_properties *chips_param;
985         /* Search for video chip */
986
987         if ((devbusfn = pci_find_devices (supported, 0)) < 0) {
988 #ifdef CONFIG_VIDEO_ONBOARD
989                 printf ("Video: Controller not found !\n");
990 #endif
991                 return (NULL);
992         }
993
994         /* PCI setup */
995         pci_write_config_dword (devbusfn, PCI_COMMAND,
996                                 (PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
997         pci_read_config_word (devbusfn, PCI_DEVICE_ID, &device_id);
998         pci_read_config_dword (devbusfn, PCI_BASE_ADDRESS_0, &pci_mem_base);
999         pci_mem_base = pci_mem_to_phys (devbusfn, pci_mem_base);
1000
1001         /* get chips params */
1002         for (chips_param = (struct ctfb_chips_properties *) &chips[0];
1003              chips_param->device_id != 0; chips_param++) {
1004                 if (chips_param->device_id == device_id)
1005                         break;
1006         }
1007         if (chips_param->device_id == 0) {
1008 #ifdef CONFIG_VIDEO_ONBOARD
1009                 printf ("Video: controller 0x%X not supported\n", device_id);
1010 #endif
1011                 return NULL;
1012         }
1013         /* supported Video controller found */
1014         printf ("Video: ");
1015
1016         tmp = 0;
1017         videomode = 0x301;
1018         /* get video mode via environment */
1019         if ((penv = getenv ("videomode")) != NULL) {
1020                 /* deceide if it is a string */
1021                 if (penv[0] <= '9') {
1022                         videomode = (int) simple_strtoul (penv, NULL, 16);
1023                         tmp = 1;
1024                 }
1025         } else {
1026                 tmp = 1;
1027         }
1028         if (tmp) {
1029                 /* parameter are vesa modes */
1030                 /* search params */
1031                 for (i = 0; i < VESA_MODES_COUNT; i++) {
1032                         if (vesa_modes[i].vesanr == videomode)
1033                                 break;
1034                 }
1035                 if (i == VESA_MODES_COUNT) {
1036                         printf ("no VESA Mode found, switching to mode 0x301 ");
1037                         i = 0;
1038                 }
1039                 res_mode =
1040                     (struct ctfb_res_modes *) &res_mode_init[vesa_modes[i].
1041                                                              resindex];
1042                 bits_per_pixel = vesa_modes[i].bits_per_pixel;
1043         } else {
1044
1045                 res_mode = (struct ctfb_res_modes *) &var_mode;
1046                 bits_per_pixel = video_get_params (res_mode, penv);
1047         }
1048
1049         /* calculate available color depth for controller memory */
1050         if (bits_per_pixel == 15)
1051                 tmp = 2;
1052         else
1053                 tmp = bits_per_pixel >> 3;      /* /8 */
1054         if (((chips_param->max_mem -
1055               ACCELMEMORY) / (res_mode->xres * res_mode->yres)) < tmp) {
1056                 tmp =
1057                     ((chips_param->max_mem -
1058                       ACCELMEMORY) / (res_mode->xres * res_mode->yres));
1059                 if (tmp == 0) {
1060                         printf
1061                             ("No matching videomode found .-> reduce resolution\n");
1062                         return NULL;
1063                 } else {
1064                         printf ("Switching back to %d Bits per Pixel ",
1065                                 tmp << 3);
1066                         bits_per_pixel = tmp << 3;
1067                 }
1068         }
1069
1070         /* calculate hsynch and vsynch freq (info only) */
1071         t1 = (res_mode->left_margin + res_mode->xres +
1072               res_mode->right_margin + res_mode->hsync_len) / 8;
1073         t1 *= 8;
1074         t1 *= res_mode->pixclock;
1075         t1 /= 1000;
1076         hsynch = 1000000000L / t1;
1077         t1 *=
1078             (res_mode->upper_margin + res_mode->yres +
1079              res_mode->lower_margin + res_mode->vsync_len);
1080         t1 /= 1000;
1081         vsynch = 1000000000L / t1;
1082
1083         /* fill in Graphic device struct */
1084         sprintf (pGD->modeIdent, "%dx%dx%d %ldkHz %ldHz", res_mode->xres,
1085                  res_mode->yres, bits_per_pixel, (hsynch / 1000),
1086                  (vsynch / 1000));
1087         printf ("%s\n", pGD->modeIdent);
1088         pGD->winSizeX = res_mode->xres;
1089         pGD->winSizeY = res_mode->yres;
1090         pGD->plnSizeX = res_mode->xres;
1091         pGD->plnSizeY = res_mode->yres;
1092         switch (bits_per_pixel) {
1093         case 8:
1094                 pGD->gdfBytesPP = 1;
1095                 pGD->gdfIndex = GDF__8BIT_INDEX;
1096                 break;
1097         case 15:
1098                 pGD->gdfBytesPP = 2;
1099                 pGD->gdfIndex = GDF_15BIT_555RGB;
1100                 break;
1101         case 16:
1102                 pGD->gdfBytesPP = 2;
1103                 pGD->gdfIndex = GDF_16BIT_565RGB;
1104                 break;
1105         case 24:
1106                 pGD->gdfBytesPP = 3;
1107                 pGD->gdfIndex = GDF_24BIT_888RGB;
1108                 break;
1109         }
1110         pGD->isaBase = CFG_ISA_IO_BASE_ADDRESS;
1111         pGD->pciBase = pci_mem_base;
1112         pGD->frameAdrs = pci_mem_base;
1113         pGD->memSize = chips_param->max_mem;
1114         /* Cursor Start Address */
1115         pGD->dprBase =
1116             (pGD->winSizeX * pGD->winSizeY * pGD->gdfBytesPP) + pci_mem_base;
1117         if ((pGD->dprBase & 0x0fff) != 0) {
1118                 /* allign it */
1119                 pGD->dprBase &= 0xfffff000;
1120                 pGD->dprBase += 0x00001000;
1121         }
1122         PRINTF ("Cursor Start %x Pattern Start %x\n", pGD->dprBase,
1123                 PATTERN_ADR);
1124         pGD->vprBase = pci_mem_base;    /* Dummy */
1125         pGD->cprBase = pci_mem_base;    /* Dummy */
1126         /* set up Hardware */
1127
1128         ctWrite (CT_MSR_W_O, 0x01);
1129
1130         /* set the extended Registers */
1131         ctLoadRegs (CT_XR_O, xreg);
1132         /* set atribute registers */
1133         SetArRegs ();
1134         /* set Graphics register */
1135         SetGrRegs ();
1136         /* set sequencer */
1137         SetSrRegs ();
1138
1139         /* set msr */
1140         SetMsrRegs (res_mode);
1141
1142         /* set CRT Registers */
1143         SetCrRegs (res_mode, bits_per_pixel);
1144         /* set color mode */
1145         SetBitsPerPixelIntoXrRegs (bits_per_pixel);
1146
1147         /* set PLL */
1148         FindAndSetPllParamIntoXrRegs (res_mode->pixclock, chips_param);
1149
1150         ctWrite_i (CT_SR_O, 0, 0x03);   /* clear synchronous reset */
1151         /* Clear video memory */
1152         i = pGD->memSize / 4;
1153         vm = (unsigned int *) pGD->pciBase;
1154         while (i--)
1155                 *vm++ = 0;
1156         SetDrawingEngine (bits_per_pixel);
1157 #ifdef VGA_DUMP_REG
1158         video_dump_reg ();
1159 #endif
1160
1161         return ((void *) &ctfb);
1162 }
1163
1164  /*******************************************************************************
1165 *
1166 * Set a RGB color in the LUT (8 bit index)
1167 */
1168 void
1169 video_set_lut (unsigned int index,      /* color number */
1170                unsigned char r, /* red */
1171                unsigned char g, /* green */
1172                unsigned char b  /* blue */
1173     )
1174 {
1175
1176         ctWrite (CT_LUT_MASK_O, 0xff);
1177
1178         ctWrite (CT_LUT_START_O, (char) index);
1179
1180         ctWrite (CT_LUT_RGB_O, r);      /* red */
1181         ctWrite (CT_LUT_RGB_O, g);      /* green */
1182         ctWrite (CT_LUT_RGB_O, b);      /* blue */
1183         udelay (1);
1184         ctWrite (CT_LUT_MASK_O, 0xff);
1185 }
1186
1187 /*******************************************************************************
1188 *
1189 * Drawing engine fill on screen region
1190 */
1191 void
1192 video_hw_rectfill (unsigned int bpp,    /* bytes per pixel */
1193                    unsigned int dst_x,  /* dest pos x */
1194                    unsigned int dst_y,  /* dest pos y */
1195                    unsigned int dim_x,  /* frame width */
1196                    unsigned int dim_y,  /* frame height */
1197                    unsigned int color   /* fill color */
1198     )
1199 {
1200         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
1201         unsigned long *p, br04;
1202
1203         video_wait_bitblt (pGD->pciBase + BR04_o);
1204
1205         p = (unsigned long *) PATTERN_ADR;
1206         dim_x *= bpp;
1207         if (bpp == 3)
1208                 bpp++;          /* 24Bit needs a 32bit pattern */
1209         memset (p, color, (bpp * sizeof (unsigned char) * 8 * 8));      /* 8 x 8 pattern data */
1210         out32r (pGD->pciBase + BR07_o, ((pGD->winSizeX * dst_y) + dst_x) * pGD->gdfBytesPP);    /* destination */
1211         br04 = in32r (pGD->pciBase + BR04_o) & 0xffffff00;
1212         br04 |= 0xF0;           /* write Pattern P -> D */
1213         out32r (pGD->pciBase + BR04_o, br04);   /* */
1214         out32r (pGD->pciBase + BR08_o, (dim_y << 16) + dim_x);  /* starts the BITBlt */
1215         video_wait_bitblt (pGD->pciBase + BR04_o);
1216 }
1217
1218 /*******************************************************************************
1219 *
1220 * Drawing engine bitblt with screen region
1221 */
1222 void
1223 video_hw_bitblt (unsigned int bpp,      /* bytes per pixel */
1224                  unsigned int src_x,    /* source pos x */
1225                  unsigned int src_y,    /* source pos y */
1226                  unsigned int dst_x,    /* dest pos x */
1227                  unsigned int dst_y,    /* dest pos y */
1228                  unsigned int dim_x,    /* frame width */
1229                  unsigned int dim_y     /* frame height */
1230     )
1231 {
1232         GraphicDevice *pGD = (GraphicDevice *) & ctfb;
1233         unsigned long br04;
1234
1235         br04 = in32r (pGD->pciBase + BR04_o);
1236
1237         /* to prevent data corruption due to overlap, we have to
1238          * find out if, and how the frames overlaps */
1239         if (src_x < dst_x) {
1240                 /* src is more left than dest
1241                  * the frame may overlap -> start from right to left */
1242                 br04 |= 0x00000100;     /* set bit 8 */
1243                 src_x += dim_x;
1244                 dst_x += dim_x;
1245         } else {
1246                 br04 &= 0xfffffeff;     /* clear bit 8 left to right */
1247         }
1248         if (src_y < dst_y) {
1249                 /* src is higher than dst
1250                  * the frame may overlap => start from bottom */
1251                 br04 |= 0x00000200;     /* set bit 9 */
1252                 src_y += dim_y;
1253                 dst_y += dim_y;
1254         } else {
1255                 br04 &= 0xfffffdff;     /* clear bit 9 top to bottom */
1256         }
1257         dim_x *= bpp;
1258         out32r (pGD->pciBase + BR06_o, ((pGD->winSizeX * src_y) + src_x) * pGD->gdfBytesPP);    /* source */
1259         out32r (pGD->pciBase + BR07_o, ((pGD->winSizeX * dst_y) + dst_x) * pGD->gdfBytesPP);    /* destination */
1260         br04 &= 0xffffff00;
1261         br04 |= 0x000000CC;     /* S -> D */
1262         out32r (pGD->pciBase + BR04_o, br04);   /* */
1263         out32r (pGD->pciBase + BR08_o, (dim_y << 16) + dim_x);  /* start the BITBlt */
1264         video_wait_bitblt (pGD->pciBase + BR04_o);
1265 }
1266
1267 #endif                          /* CONFIG_CT69000 */
1268
1269 #endif                          /* CONFIG_VIDEO */