c35406d37b53df73198e8e5744a1acdfb13614b5
[oweals/u-boot.git] / arch / powerpc / cpu / mpc8xx / video.c
1 /*
2  * (C) Copyright 2000
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  * (C) Copyright 2002
5  * Wolfgang Denk, wd@denx.de
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 /* #define DEBUG */
11
12 /************************************************************************/
13 /* ** HEADER FILES                                                      */
14 /************************************************************************/
15
16 #include <stdarg.h>
17 #include <common.h>
18 #include <config.h>
19 #include <version.h>
20 #include <i2c.h>
21 #include <linux/types.h>
22 #include <stdio_dev.h>
23
24 #ifdef CONFIG_VIDEO
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 /************************************************************************/
29 /* ** DEBUG SETTINGS                                                    */
30 /************************************************************************/
31
32 #if 0
33 #define VIDEO_DEBUG_COLORBARS   /* Force colorbars output */
34 #endif
35
36 /************************************************************************/
37 /* ** VIDEO MODE SETTINGS                                               */
38 /************************************************************************/
39
40 #if 0
41 #define VIDEO_MODE_EXTENDED             /* Allow screen size bigger than visible area */
42 #define VIDEO_MODE_NTSC
43 #endif
44
45 #define VIDEO_MODE_PAL
46
47 #if 0
48 #define VIDEO_BLINK                     /* This enables cursor blinking (under construction) */
49 #endif
50
51 #define VIDEO_INFO                      /* Show U-Boot information */
52 #define VIDEO_INFO_X            VIDEO_LOGO_WIDTH+8
53 #define VIDEO_INFO_Y            16
54
55 /************************************************************************/
56 /* ** VIDEO MODE CONSTANTS                                              */
57 /************************************************************************/
58
59 #ifdef VIDEO_MODE_EXTENDED
60 #define VIDEO_COLS      VIDEO_ACTIVE_COLS
61 #define VIDEO_ROWS      VIDEO_ACTIVE_ROWS
62 #else
63 #define VIDEO_COLS      VIDEO_VISIBLE_COLS
64 #define VIDEO_ROWS      VIDEO_VISIBLE_ROWS
65 #endif
66
67 #define VIDEO_PIXEL_SIZE        (VIDEO_MODE_BPP/8)
68 #define VIDEO_SIZE              (VIDEO_ROWS*VIDEO_COLS*VIDEO_PIXEL_SIZE)        /* Total size of buffer */
69 #define VIDEO_PIX_BLOCKS        (VIDEO_SIZE >> 2)       /* Number of ints */
70 #define VIDEO_LINE_LEN          (VIDEO_COLS*VIDEO_PIXEL_SIZE)   /* Number of bytes per line */
71 #define VIDEO_BURST_LEN         (VIDEO_COLS/8)
72
73 #ifdef VIDEO_MODE_YUYV
74 #define VIDEO_BG_COL    0x80D880D8      /* Background color in YUYV format */
75 #else
76 #define VIDEO_BG_COL    0xF8F8F8F8      /* Background color in RGB format */
77 #endif
78
79 /************************************************************************/
80 /* ** FONT AND LOGO DATA                                                */
81 /************************************************************************/
82
83 #include <video_font.h>                 /* Get font data, width and height */
84
85 #ifdef CONFIG_VIDEO_LOGO
86 #include <video_logo.h>                 /* Get logo data, width and height */
87
88 #define VIDEO_LOGO_WIDTH        DEF_U_BOOT_LOGO_WIDTH
89 #define VIDEO_LOGO_HEIGHT       DEF_U_BOOT_LOGO_HEIGHT
90 #define VIDEO_LOGO_ADDR         &u_boot_logo
91 #endif
92
93 /************************************************************************/
94 /* ** VIDEO CONTROLLER CONSTANTS                                        */
95 /************************************************************************/
96
97 /* VCCR - VIDEO CONTROLLER CONFIGURATION REGISTER */
98
99 #define VIDEO_VCCR_VON  0               /* Video controller ON */
100 #define VIDEO_VCCR_CSRC 1               /* Clock source */
101 #define VIDEO_VCCR_PDF  13              /* Pixel display format */
102 #define VIDEO_VCCR_IEN  11              /* Interrupt enable */
103
104 /* VSR - VIDEO STATUS REGISTER */
105
106 #define VIDEO_VSR_CAS   6               /* Active set */
107 #define VIDEO_VSR_EOF   0               /* End of frame */
108
109 /* VCMR - VIDEO COMMAND REGISTER */
110
111 #define VIDEO_VCMR_BD   0               /* Blank display */
112 #define VIDEO_VCMR_ASEL 1               /* Active set selection */
113
114 /* VBCB - VIDEO BACKGROUND COLOR BUFFER REGISTER */
115
116 #define VIDEO_BCSR4_RESET_BIT   21      /* BCSR4 - Extern video encoder reset */
117 #define VIDEO_BCSR4_EXTCLK_BIT  22      /* BCSR4 - Extern clock enable */
118 #define VIDEO_BCSR4_VIDLED_BIT  23      /* BCSR4 - Video led disable */
119
120 /************************************************************************/
121 /* ** CONSOLE CONSTANTS                                                 */
122 /************************************************************************/
123
124 #ifdef  CONFIG_VIDEO_LOGO
125 #define CONSOLE_ROWS            ((VIDEO_ROWS - VIDEO_LOGO_HEIGHT) / VIDEO_FONT_HEIGHT)
126 #define VIDEO_LOGO_SKIP         (VIDEO_COLS - VIDEO_LOGO_WIDTH)
127 #else
128 #define CONSOLE_ROWS            (VIDEO_ROWS / VIDEO_FONT_HEIGHT)
129 #endif
130
131 #define CONSOLE_COLS            (VIDEO_COLS / VIDEO_FONT_WIDTH)
132 #define CONSOLE_ROW_SIZE        (VIDEO_FONT_HEIGHT * VIDEO_LINE_LEN)
133 #define CONSOLE_ROW_FIRST       (video_console_address)
134 #define CONSOLE_ROW_SECOND      (video_console_address + CONSOLE_ROW_SIZE)
135 #define CONSOLE_ROW_LAST        (video_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE)
136 #define CONSOLE_SIZE            (CONSOLE_ROW_SIZE * CONSOLE_ROWS)
137 #define CONSOLE_SCROLL_SIZE     (CONSOLE_SIZE - CONSOLE_ROW_SIZE)
138
139 /*
140  * Simple color definitions
141  */
142 #define CONSOLE_COLOR_BLACK      0
143 #define CONSOLE_COLOR_RED        1
144 #define CONSOLE_COLOR_GREEN      2
145 #define CONSOLE_COLOR_YELLOW     3
146 #define CONSOLE_COLOR_BLUE       4
147 #define CONSOLE_COLOR_MAGENTA    5
148 #define CONSOLE_COLOR_CYAN       6
149 #define CONSOLE_COLOR_GREY      13
150 #define CONSOLE_COLOR_GREY2     14
151 #define CONSOLE_COLOR_WHITE     15      /* Must remain last / highest */
152
153 /************************************************************************/
154 /* ** BITOPS MACROS                                                     */
155 /************************************************************************/
156
157 #define HISHORT(i)      ((i >> 16)&0xffff)
158 #define LOSHORT(i)      (i & 0xffff)
159 #define HICHAR(s)       ((i >> 8)&0xff)
160 #define LOCHAR(s)       (i & 0xff)
161 #define HI(c)           ((c >> 4)&0xf)
162 #define LO(c)           (c & 0xf)
163 #define SWAPINT(i)      (HISHORT(i) | (LOSHORT(i) << 16))
164 #define SWAPSHORT(s)    (HICHAR(s) | (LOCHAR(s) << 8))
165 #define SWAPCHAR(c)     (HI(c) | (LO(c) << 4))
166 #define BITMASK(b)      (1 << (b))
167 #define GETBIT(v,b)     (((v) & BITMASK(b)) > 0)
168 #define SETBIT(v,b,d)   (v = (((d)>0) ? (v) | BITMASK(b): (v) & ~BITMASK(b)))
169
170 /************************************************************************/
171 /* ** STRUCTURES                                                        */
172 /************************************************************************/
173
174 typedef struct {
175         unsigned char V, Y1, U, Y2;
176 } tYUYV;
177
178 /* This structure is based on the Video Ram in the MPC823. */
179 typedef struct VRAM {
180         unsigned        hx:2,           /* Horizontal sync */
181                         vx:2,           /* Vertical sync */
182                         fx:2,           /* Frame */
183                         bx:2,           /* Blank */
184                         res1:6,         /* Reserved */
185                         vds:2,          /* Video Data Select */
186                         inter:1,        /* Interrupt */
187                         res2:2,         /* Reserved */
188                         lcyc:11,        /* Loop/video cycles */
189                         lp:1,           /* Loop start/end */
190                         lst:1;          /* Last entry */
191 } VRAM;
192
193 /************************************************************************/
194 /* ** VARIABLES                                                         */
195 /************************************************************************/
196
197 static int
198         video_panning_range_x = 0,      /* Video mode invisible pixels x range */
199         video_panning_range_y = 0,      /* Video mode invisible pixels y range */
200         video_panning_value_x = 0,      /* Video mode x panning value (absolute) */
201         video_panning_value_y = 0,      /* Video mode y panning value (absolute) */
202         video_panning_factor_x = 0,     /* Video mode x panning value (-127 +127) */
203         video_panning_factor_y = 0,     /* Video mode y panning value (-127 +127) */
204         console_col = 0,                /* Cursor col */
205         console_row = 0,                /* Cursor row */
206         video_palette[16];              /* Our palette */
207
208 static const int video_font_draw_table[] =
209         { 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff };
210
211 static char
212         video_color_fg = 0,             /* Current fg color index (0-15) */
213         video_color_bg = 0,             /* Current bg color index (0-15) */
214         video_enable = 0;               /* Video has been initialized? */
215
216 static void
217         *video_fb_address,              /* Frame buffer address */
218         *video_console_address;         /* Console frame buffer start address */
219
220 /************************************************************************/
221 /* ** MEMORY FUNCTIONS (32bit)                                          */
222 /************************************************************************/
223
224 static void memsetl (int *p, int c, int v)
225 {
226         while (c--)
227                 *(p++) = v;
228 }
229
230 static void memcpyl (int *d, int *s, int c)
231 {
232         while (c--)
233                 *(d++) = *(s++);
234 }
235
236 /************************************************************************/
237 /* ** VIDEO DRAWING AND COLOR FUNCTIONS                                 */
238 /************************************************************************/
239
240 static int video_maprgb (int r, int g, int b)
241 {
242 #ifdef VIDEO_MODE_YUYV
243         unsigned int pR, pG, pB;
244         tYUYV YUYV;
245         unsigned int *ret = (unsigned int *) &YUYV;
246
247         /* Transform (0-255) components to (0-100) */
248
249         pR = r * 100 / 255;
250         pG = g * 100 / 255;
251         pB = b * 100 / 255;
252
253         /* Calculate YUV values (0-255) from RGB beetween 0-100 */
254
255         YUYV.Y1 = YUYV.Y2 = 209 * (pR + pG + pB) / 300 + 16;
256         YUYV.U  = pR - (pG * 3 / 4) - (pB / 4) + 128;
257         YUYV.V  = pB - (pR / 4) - (pG * 3 / 4) + 128;
258         return *ret;
259 #endif
260 #ifdef VIDEO_MODE_RGB
261         return ((r >> 3) << 11) | ((g > 2) << 6) | (b >> 3);
262 #endif
263 }
264
265 static void video_setpalette (int color, int r, int g, int b)
266 {
267         color &= 0xf;
268
269         video_palette[color] = video_maprgb (r, g, b);
270
271         /* Swap values if our panning offset is odd */
272         if (video_panning_value_x & 1)
273                 video_palette[color] = SWAPINT (video_palette[color]);
274 }
275
276 static void video_fill (int color)
277 {
278         memsetl (video_fb_address, VIDEO_PIX_BLOCKS, color);
279 }
280
281 static void video_setfgcolor (int i)
282 {
283         video_color_fg = i & 0xf;
284 }
285
286 static void video_setbgcolor (int i)
287 {
288         video_color_bg = i & 0xf;
289 }
290
291 static int video_pickcolor (int i)
292 {
293         return video_palette[i & 0xf];
294 }
295
296 /* Absolute console plotting functions */
297
298 #ifdef VIDEO_BLINK
299 static void video_revchar (int xx, int yy)
300 {
301         int rows;
302         u8 *dest;
303
304         dest = video_fb_address + yy * VIDEO_LINE_LEN + xx * 2;
305
306         for (rows = VIDEO_FONT_HEIGHT; rows--; dest += VIDEO_LINE_LEN) {
307                 switch (VIDEO_FONT_WIDTH) {
308                 case 16:
309                         ((u32 *) dest)[6] ^= 0xffffffff;
310                         ((u32 *) dest)[7] ^= 0xffffffff;
311                         /* FALL THROUGH */
312                 case 12:
313                         ((u32 *) dest)[4] ^= 0xffffffff;
314                         ((u32 *) dest)[5] ^= 0xffffffff;
315                         /* FALL THROUGH */
316                 case 8:
317                         ((u32 *) dest)[2] ^= 0xffffffff;
318                         ((u32 *) dest)[3] ^= 0xffffffff;
319                         /* FALL THROUGH */
320                 case 4:
321                         ((u32 *) dest)[0] ^= 0xffffffff;
322                         ((u32 *) dest)[1] ^= 0xffffffff;
323                 }
324         }
325 }
326 #endif
327
328 static void video_drawchars (int xx, int yy, unsigned char *s, int count)
329 {
330         u8 *cdat, *dest, *dest0;
331         int rows, offset, c;
332         u32 eorx, fgx, bgx;
333
334         offset = yy * VIDEO_LINE_LEN + xx * 2;
335         dest0 = video_fb_address + offset;
336
337         fgx = video_pickcolor (video_color_fg);
338         bgx = video_pickcolor (video_color_bg);
339
340         if (xx & 1) {
341                 fgx = SWAPINT (fgx);
342                 bgx = SWAPINT (bgx);
343         }
344
345         eorx = fgx ^ bgx;
346
347         switch (VIDEO_FONT_WIDTH) {
348         case 4:
349         case 8:
350                 while (count--) {
351                         c = *s;
352                         cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
353                         for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
354                              rows--;
355                              dest += VIDEO_LINE_LEN) {
356                                 u8 bits = *cdat++;
357
358                                 ((u32 *) dest)[0] =
359                                         (video_font_draw_table[bits >> 6] & eorx) ^ bgx;
360                                 ((u32 *) dest)[1] =
361                                         (video_font_draw_table[bits >> 4 & 3] & eorx) ^ bgx;
362                                 if (VIDEO_FONT_WIDTH == 8) {
363                                         ((u32 *) dest)[2] =
364                                                 (video_font_draw_table[bits >> 2 & 3] & eorx) ^ bgx;
365                                         ((u32 *) dest)[3] =
366                                                 (video_font_draw_table[bits & 3] & eorx) ^ bgx;
367                                 }
368                         }
369                         dest0 += VIDEO_FONT_WIDTH * 2;
370                         s++;
371                 }
372                 break;
373         case 12:
374         case 16:
375                 while (count--) {
376                         cdat = video_fontdata + (*s) * (VIDEO_FONT_HEIGHT << 1);
377                         for (rows = VIDEO_FONT_HEIGHT, dest = dest0; rows--;
378                                  dest += VIDEO_LINE_LEN) {
379                                 u8 bits = *cdat++;
380
381                                 ((u32 *) dest)[0] =
382                                         (video_font_draw_table[bits >> 6] & eorx) ^ bgx;
383                                 ((u32 *) dest)[1] =
384                                         (video_font_draw_table[bits >> 4 & 3] & eorx) ^ bgx;
385                                 ((u32 *) dest)[2] =
386                                         (video_font_draw_table[bits >> 2 & 3] & eorx) ^ bgx;
387                                 ((u32 *) dest)[3] =
388                                         (video_font_draw_table[bits & 3] & eorx) ^ bgx;
389                                 bits = *cdat++;
390                                 ((u32 *) dest)[4] =
391                                         (video_font_draw_table[bits >> 6] & eorx) ^ bgx;
392                                 ((u32 *) dest)[5] =
393                                         (video_font_draw_table[bits >> 4 & 3] & eorx) ^ bgx;
394                                 if (VIDEO_FONT_WIDTH == 16) {
395                                         ((u32 *) dest)[6] =
396                                                 (video_font_draw_table[bits >> 2 & 3] & eorx) ^ bgx;
397                                         ((u32 *) dest)[7] =
398                                                 (video_font_draw_table[bits & 3] & eorx) ^ bgx;
399                                 }
400                         }
401                         s++;
402                         dest0 += VIDEO_FONT_WIDTH * 2;
403                 }
404                 break;
405         }
406 }
407
408 static inline void video_drawstring (int xx, int yy, char *s)
409 {
410         video_drawchars (xx, yy, (unsigned char *)s, strlen (s));
411 }
412
413 /* Relative to console plotting functions */
414
415 static void video_putchars (int xx, int yy, unsigned char *s, int count)
416 {
417 #ifdef CONFIG_VIDEO_LOGO
418         video_drawchars (xx, yy + VIDEO_LOGO_HEIGHT, s, count);
419 #else
420         video_drawchars (xx, yy, s, count);
421 #endif
422 }
423
424 static void video_putchar (int xx, int yy, unsigned char c)
425 {
426 #ifdef CONFIG_VIDEO_LOGO
427         video_drawchars (xx, yy + VIDEO_LOGO_HEIGHT, &c, 1);
428 #else
429         video_drawchars (xx, yy, &c, 1);
430 #endif
431 }
432
433 static inline void video_putstring (int xx, int yy, unsigned char *s)
434 {
435         video_putchars (xx, yy, (unsigned char *)s, strlen ((char *)s));
436 }
437
438 /************************************************************************/
439 /* ** VIDEO CONTROLLER LOW-LEVEL FUNCTIONS                              */
440 /************************************************************************/
441
442 static void video_mode_dupefield (VRAM * source, VRAM * dest, int entries)
443 {
444         int i;
445
446         for (i = 0; i < entries; i++) {
447                 dest[i] = source[i];    /* Copy the entire record */
448                 dest[i].fx = (!dest[i].fx) * 3; /* Negate field bit */
449         }
450
451         dest[0].lcyc++;                 /* Add a cycle to the first entry */
452         dest[entries - 1].lst = 1;      /* Set end of ram entries */
453 }
454
455 static void inline video_mode_addentry (VRAM * vr,
456         int Hx, int Vx, int Fx, int Bx,
457         int VDS, int INT, int LCYC, int LP, int LST)
458 {
459         vr->hx = Hx;
460         vr->vx = Vx;
461         vr->fx = Fx;
462         vr->bx = Bx;
463         vr->vds = VDS;
464         vr->inter = INT;
465         vr->lcyc = LCYC;
466         vr->lp = LP;
467         vr->lst = LST;
468 }
469
470 #define ADDENTRY(a,b,c,d,e,f,g,h,i)     video_mode_addentry(&vr[entry++],a,b,c,d,e,f,g,h,i)
471
472 static int video_mode_generate (void)
473 {
474         immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
475         VRAM *vr = (VRAM *) (((void *) immap) + 0xb00); /* Pointer to the VRAM table */
476         int DX, X1, X2, DY, Y1, Y2, entry = 0, fifo;
477
478         /* CHECKING PARAMETERS */
479
480         if (video_panning_factor_y < -128)
481                 video_panning_factor_y = -128;
482
483         if (video_panning_factor_y > 128)
484                 video_panning_factor_y = 128;
485
486         if (video_panning_factor_x < -128)
487                 video_panning_factor_x = -128;
488
489         if (video_panning_factor_x > 128)
490                 video_panning_factor_x = 128;
491
492         /* Setting panning */
493
494         DX = video_panning_range_x = (VIDEO_ACTIVE_COLS - VIDEO_COLS) * 2;
495         DY = video_panning_range_y = (VIDEO_ACTIVE_ROWS - VIDEO_ROWS) / 2;
496
497         video_panning_value_x = (video_panning_factor_x + 128) * DX / 256;
498         video_panning_value_y = (video_panning_factor_y + 128) * DY / 256;
499
500         /* We assume these are burst units (multiplied by 2, we need it pari) */
501         X1 = video_panning_value_x & 0xfffe;
502         X2 = DX - X1;
503
504         /* We assume these are field line units (divided by 2, we need it pari) */
505         Y1 = video_panning_value_y & 0xfffe;
506         Y2 = DY - Y1;
507
508         debug("X1=%d, X2=%d, Y1=%d, Y2=%d, DX=%d, DY=%d VIDEO_COLS=%d \n",
509               X1, X2, Y1, Y2, DX, DY, VIDEO_COLS);
510
511 #ifdef VIDEO_MODE_NTSC
512 /*
513  *           Hx Vx Fx Bx VDS INT LCYC LP LST
514  *
515  * Retrace blanking
516  */
517         ADDENTRY (0, 0, 3, 0, 1, 0, 3, 1, 0);
518         ADDENTRY (3, 0, 3, 0, 1, 0, 243, 0, 0);
519         ADDENTRY (3, 0, 3, 0, 1, 0, 1440, 0, 0);
520         ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
521 /*
522  * Vertical blanking
523  */
524         ADDENTRY (0, 0, 0, 0, 1, 0, 18, 1, 0);
525         ADDENTRY (3, 0, 0, 0, 1, 0, 243, 0, 0);
526         ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
527         ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
528 /*
529  * Odd field active area (TOP)
530  */
531         if (Y1 > 0) {
532                 ADDENTRY (0, 0, 0, 0, 1, 0, Y1, 1, 0);
533                 ADDENTRY (3, 0, 0, 0, 1, 0, 235, 0, 0);
534                 ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
535                 ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
536         }
537 /*
538  * Odd field active area
539  */
540         ADDENTRY (0, 0, 0, 0, 1, 0, 240 - DY, 1, 0);
541         ADDENTRY (3, 0, 0, 0, 1, 0, 235, 0, 0);
542         ADDENTRY (3, 0, 0, 3, 1, 0, 8 + X1, 0, 0);
543         ADDENTRY (3, 0, 0, 3, 0, 0, VIDEO_COLS * 2, 0, 0);
544
545         if (X2 > 0)
546                 ADDENTRY (3, 0, 0, 3, 1, 0, X2, 0, 0);
547
548         ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
549
550 /*
551  * Odd field active area (BOTTOM)
552  */
553         if (Y1 > 0) {
554                 ADDENTRY (0, 0, 0, 0, 1, 0, Y2, 1, 0);
555                 ADDENTRY (3, 0, 0, 0, 1, 0, 235, 0, 0);
556                 ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
557                 ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
558         }
559 /*
560  * Vertical blanking
561  */
562         ADDENTRY (0, 0, 0, 0, 1, 0, 4, 1, 0);
563         ADDENTRY (3, 0, 0, 0, 1, 0, 243, 0, 0);
564         ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
565         ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
566 /*
567  * Vertical blanking
568  */
569         ADDENTRY (0, 0, 3, 0, 1, 0, 19, 1, 0);
570         ADDENTRY (3, 0, 3, 0, 1, 0, 243, 0, 0);
571         ADDENTRY (3, 0, 3, 0, 1, 0, 1440, 0, 0);
572         ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
573 /*
574  * Even field active area (TOP)
575  */
576         if (Y1 > 0) {
577                 ADDENTRY (0, 0, 3, 0, 1, 0, Y1, 1, 0);
578                 ADDENTRY (3, 0, 3, 0, 1, 0, 235, 0, 0);
579                 ADDENTRY (3, 0, 3, 3, 1, 0, 1448, 0, 0);
580                 ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
581         }
582 /*
583  * Even field active area (CENTER)
584  */
585         ADDENTRY (0, 0, 3, 0, 1, 0, 240 - DY, 1, 0);
586         ADDENTRY (3, 0, 3, 0, 1, 0, 235, 0, 0);
587         ADDENTRY (3, 0, 3, 3, 1, 0, 8 + X1, 0, 0);
588         ADDENTRY (3, 0, 3, 3, 0, 0, VIDEO_COLS * 2, 0, 0);
589
590         if (X2 > 0)
591                 ADDENTRY (3, 0, 3, 3, 1, 0, X2, 0, 0);
592
593         ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
594 /*
595  * Even field active area (BOTTOM)
596  */
597         if (Y1 > 0) {
598                 ADDENTRY (0, 0, 3, 0, 1, 0, Y2, 1, 0);
599                 ADDENTRY (3, 0, 3, 0, 1, 0, 235, 0, 0);
600                 ADDENTRY (3, 0, 3, 3, 1, 0, 1448, 0, 0);
601                 ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
602         }
603 /*
604  * Vertical blanking
605  */
606         ADDENTRY (0, 0, 3, 0, 1, 0, 1, 1, 0);
607         ADDENTRY (3, 0, 3, 0, 1, 0, 243, 0, 0);
608         ADDENTRY (3, 0, 3, 0, 1, 0, 1440, 0, 0);
609         ADDENTRY (3, 0, 3, 0, 1, 1, 32, 1, 1);
610 #endif
611
612 #ifdef VIDEO_MODE_PAL
613
614 /*
615  *      Hx Vx Fx Bx VDS INT LCYC LP LST
616  *
617  * vertical; blanking
618  */
619         ADDENTRY (0, 0, 0, 0, 1, 0, 22, 1, 0);
620         ADDENTRY (3, 0, 0, 0, 1, 0, 263, 0, 0);
621         ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
622         ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
623 /*
624  * active area (TOP)
625  */
626         if (Y1 > 0) {
627                 ADDENTRY (0, 0, 0, 0, 1, 0, Y1, 1, 0);  /* 11? */
628                 ADDENTRY (3, 0, 0, 0, 1, 0, 255, 0, 0);
629                 ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
630                 ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
631         }
632 /*
633  * field active area (CENTER)
634  */
635         ADDENTRY (0, 0, 0, 0, 1, 0, 288 - DY, 1, 0);    /* 265? */
636         ADDENTRY (3, 0, 0, 0, 1, 0, 255, 0, 0);
637         ADDENTRY (3, 0, 0, 3, 1, 0, 8 + X1, 0, 0);
638         ADDENTRY (3, 0, 0, 3, 0, 0, VIDEO_COLS * 2, 0, 0);
639
640         if (X2 > 0)
641                 ADDENTRY (3, 0, 0, 1, 1, 0, X2, 0, 0);
642
643         ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
644 /*
645  * field active area (BOTTOM)
646  */
647         if (Y2 > 0) {
648                 ADDENTRY (0, 0, 0, 0, 1, 0, Y2, 1, 0);  /* 12? */
649                 ADDENTRY (3, 0, 0, 0, 1, 0, 255, 0, 0);
650                 ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
651                 ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
652         }
653 /*
654  * field vertical; blanking
655  */
656         ADDENTRY (0, 0, 0, 0, 1, 0, 2, 1, 0);
657         ADDENTRY (3, 0, 0, 0, 1, 0, 263, 0, 0);
658         ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
659         ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
660 /*
661  * Create the other field (like this, but whit other field selected,
662  * one more cycle loop and a last identifier)
663  */
664         video_mode_dupefield (vr, &vr[entry], entry);
665
666 #endif /* VIDEO_MODE_PAL */
667
668         /* See what FIFO are we using */
669         fifo = GETBIT (immap->im_vid.vid_vsr, VIDEO_VSR_CAS);
670
671         /* Set number of lines and burst (only one frame for now) */
672         if (fifo) {
673                 immap->im_vid.vid_vfcr0 = VIDEO_BURST_LEN |
674                         (VIDEO_BURST_LEN << 8) | ((VIDEO_ROWS / 2) << 19);
675         } else {
676                 immap->im_vid.vid_vfcr1 = VIDEO_BURST_LEN |
677                         (VIDEO_BURST_LEN << 8) | ((VIDEO_ROWS / 2) << 19);
678         }
679
680         SETBIT (immap->im_vid.vid_vcmr, VIDEO_VCMR_ASEL, !fifo);
681
682 /*
683  * Wait until changes are applied (not done)
684  * while (GETBIT(immap->im_vid.vid_vsr, VIDEO_VSR_CAS) == fifo) ;
685  */
686
687         /* Return number of VRAM entries */
688         return entry * 2;
689 }
690
691 static void video_encoder_init (void)
692 {
693         return;
694 }
695
696 static void video_ctrl_init (void *memptr)
697 {
698         immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
699
700         video_fb_address = memptr;
701
702         /* Set background */
703         debug ("[VIDEO CTRL] Setting background color...\n");
704         immap->im_vid.vid_vbcb = VIDEO_BG_COL;
705
706         /* Show the background */
707         debug ("[VIDEO CTRL] Forcing background...\n");
708         SETBIT (immap->im_vid.vid_vcmr, VIDEO_VCMR_BD, 1);
709
710         /* Turn off video controller */
711         debug ("[VIDEO CTRL] Turning off video controller...\n");
712         SETBIT (immap->im_vid.vid_vccr, VIDEO_VCCR_VON, 0);
713
714         /* Generate and make active a new video mode */
715         debug ("[VIDEO CTRL] Generating video mode...\n");
716         video_mode_generate ();
717
718         /* Start of frame buffer (even and odd frame, to make it working with */
719         /* any selected active set) */
720         debug ("[VIDEO CTRL] Setting frame buffer address...\n");
721         immap->im_vid.vid_vfaa1 =
722                 immap->im_vid.vid_vfaa0 = (u32) video_fb_address;
723         immap->im_vid.vid_vfba1 =
724         immap->im_vid.vid_vfba0 =
725                 (u32) video_fb_address + VIDEO_LINE_LEN;
726
727         /* YUV, Big endian, SHIFT/CLK/CLK input (BEFORE ENABLING 27MHZ EXT CLOCK) */
728         debug ("[VIDEO CTRL] Setting pixel mode and clocks...\n");
729         immap->im_vid.vid_vccr = 0x2042;
730
731         /* Configure port pins */
732         debug ("[VIDEO CTRL] Configuring input/output pins...\n");
733         immap->im_ioport.iop_pdpar = 0x1fff;
734         immap->im_ioport.iop_pddir = 0x0000;
735
736         /* Blanking the screen. */
737         debug ("[VIDEO CTRL] Blanking the screen...\n");
738         video_fill (VIDEO_BG_COL);
739
740         /*
741          * Turns on Aggressive Mode. Normally, turning on the caches
742          * will cause the screen to flicker when the caches try to
743          * fill. This gives the FIFO's for the Video Controller
744          * higher priority and prevents flickering because of
745          * underrun. This may still be an issue when using FLASH,
746          * since accessing data from Flash is so slow.
747          */
748         debug ("[VIDEO CTRL] Turning on aggressive mode...\n");
749         immap->im_siu_conf.sc_sdcr = 0x40;
750
751         /* Turn on video controller */
752         debug ("[VIDEO CTRL] Turning on video controller...\n");
753         SETBIT (immap->im_vid.vid_vccr, VIDEO_VCCR_VON, 1);
754
755         /* Show the display */
756         debug ("[VIDEO CTRL] Enabling the video...\n");
757         SETBIT (immap->im_vid.vid_vcmr, VIDEO_VCMR_BD, 0);
758 }
759
760 /************************************************************************/
761 /* ** CONSOLE FUNCTIONS                                                 */
762 /************************************************************************/
763
764 static void console_scrollup (void)
765 {
766         /* Copy up rows ignoring the first one */
767         memcpyl (CONSOLE_ROW_FIRST, CONSOLE_ROW_SECOND, CONSOLE_SCROLL_SIZE >> 2);
768
769         /* Clear the last one */
770         memsetl (CONSOLE_ROW_LAST, CONSOLE_ROW_SIZE >> 2, VIDEO_BG_COL);
771 }
772
773 static inline void console_back (void)
774 {
775         console_col--;
776
777         if (console_col < 0) {
778                 console_col = CONSOLE_COLS - 1;
779                 console_row--;
780                 if (console_row < 0)
781                         console_row = 0;
782         }
783
784         video_putchar ( console_col * VIDEO_FONT_WIDTH,
785                         console_row * VIDEO_FONT_HEIGHT, ' ');
786 }
787
788 static inline void console_newline (void)
789 {
790         console_row++;
791         console_col = 0;
792
793         /* Check if we need to scroll the terminal */
794         if (console_row >= CONSOLE_ROWS) {
795                 /* Scroll everything up */
796                 console_scrollup ();
797
798                 /* Decrement row number */
799                 console_row--;
800         }
801 }
802
803 void video_putc(struct stdio_dev *dev, const char c)
804 {
805         if (!video_enable) {
806                 serial_putc (c);
807                 return;
808         }
809
810         switch (c) {
811         case 13:                        /* Simply ignore this */
812                 break;
813
814         case '\n':                      /* Next line, please */
815                 console_newline ();
816                 break;
817
818         case 9:                         /* Tab (8 chars alignment) */
819                 console_col |= 0x0008;  /* Next 8 chars boundary */
820                 console_col &= ~0x0007; /* Set this bit to zero */
821
822                 if (console_col >= CONSOLE_COLS)
823                         console_newline ();
824                 break;
825
826         case 8:                         /* Eat last character */
827                 console_back ();
828                 break;
829
830         default:                        /* Add to the console */
831                 video_putchar ( console_col * VIDEO_FONT_WIDTH,
832                                 console_row * VIDEO_FONT_HEIGHT, c);
833                 console_col++;
834                 /* Check if we need to go to next row */
835                 if (console_col >= CONSOLE_COLS)
836                         console_newline ();
837         }
838 }
839
840 void video_puts(struct stdio_dev *dev, const char *s)
841 {
842         int count = strlen (s);
843
844         if (!video_enable)
845                 while (count--)
846                         serial_putc (*s++);
847         else
848                 while (count--)
849                         video_putc(dev, *s++);
850 }
851
852 /************************************************************************/
853 /* ** CURSOR BLINKING FUNCTIONS                                         */
854 /************************************************************************/
855
856 #ifdef VIDEO_BLINK
857
858 #define BLINK_TIMER_ID          0
859 #define BLINK_TIMER_HZ          2
860
861 static unsigned char blink_enabled = 0;
862 static timer_t blink_timer;
863
864 static void blink_update (void)
865 {
866         static int blink_row = -1, blink_col = -1, blink_old = 0;
867
868         /* Check if we have a new position to invert */
869         if ((console_row != blink_row) || (console_col != blink_col)) {
870                 /* Check if we need to reverse last character */
871                 if (blink_old)
872                         video_revchar ( blink_col * VIDEO_FONT_WIDTH,
873                                         (blink_row
874 #ifdef CONFIG_VIDEO_LOGO
875                                          + VIDEO_LOGO_HEIGHT
876 #endif
877                                         ) * VIDEO_FONT_HEIGHT);
878
879                 /* Update values */
880                 blink_row = console_row;
881                 blink_col = console_col;
882                 blink_old = 0;
883         }
884
885 /* Reverse this character */
886         blink_old = !blink_old;
887         video_revchar ( console_col * VIDEO_FONT_WIDTH,
888                         (console_row
889 #ifdef CONFIG_VIDEO_LOGO
890                         + VIDEO_LOGO_HEIGHT
891 #endif
892                         ) * VIDEO_FONT_HEIGHT);
893
894 }
895
896 /*
897  * Handler for blinking cursor
898  */
899 static void blink_handler (void *arg)
900 {
901 /* Blink */
902         blink_update ();
903 /* Ack the timer */
904         timer_ack (&blink_timer);
905 }
906
907 int blink_set (int blink)
908 {
909         int ret = blink_enabled;
910
911         if (blink)
912                 timer_enable (&blink_timer);
913         else
914                 timer_disable (&blink_timer);
915
916         blink_enabled = blink;
917
918         return ret;
919 }
920
921 static inline void blink_close (void)
922 {
923         timer_close (&blink_timer);
924 }
925
926 static inline void blink_init (void)
927 {
928         timer_init (&blink_timer,
929                         BLINK_TIMER_ID, BLINK_TIMER_HZ,
930                         blink_handler);
931 }
932 #endif
933
934 /************************************************************************/
935 /* ** LOGO PLOTTING FUNCTIONS                                           */
936 /************************************************************************/
937
938 #ifdef CONFIG_VIDEO_LOGO
939 void easylogo_plot (fastimage_t * image, void *screen, int width, int x,
940                                         int y)
941 {
942         int skip = width - image->width, xcount, ycount = image->height;
943
944 #ifdef VIDEO_MODE_YUYV
945         ushort *source = (ushort *) image->data;
946         ushort *dest   = (ushort *) screen + y * width + x;
947
948         while (ycount--) {
949                 xcount = image->width;
950                 while (xcount--)
951                         *dest++ = *source++;
952                 dest += skip;
953         }
954 #endif
955 #ifdef VIDEO_MODE_RGB
956         unsigned char
957         *source = (unsigned short *) image->data,
958                         *dest = (unsigned short *) screen + ((y * width) + x) * 3;
959
960         while (ycount--) {
961                 xcount = image->width * 3;
962                 memcpy (dest, source, xcount);
963                 source += xcount;
964                 dest += ycount;
965         }
966 #endif
967 }
968
969 static void *video_logo (void)
970 {
971         u16 *screen = video_fb_address, width = VIDEO_COLS;
972 #ifdef VIDEO_INFO
973         char temp[32];
974         char info[80];
975 #endif /* VIDEO_INFO */
976
977         easylogo_plot (VIDEO_LOGO_ADDR, screen, width, 0, 0);
978
979 #ifdef VIDEO_INFO
980         sprintf (info, "%s (%s - %s) ",
981                  U_BOOT_VERSION, U_BOOT_DATE, U_BOOT_TIME);
982         video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y, info);
983
984         strcpy(info, "(C) 2002 DENX Software Engineering");
985         video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT,
986                                         info);
987
988         strcpy(info, "    Wolfgang DENK, wd@denx.de");
989         video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT * 2,
990                                         info);
991
992         /* leave one blank line */
993
994         sprintf(info, "MPC823 CPU at %s MHz, %ld MiB RAM, %ld MiB Flash",
995                 strmhz(temp, gd->cpu_clk),
996                 gd->ram_size >> 20,
997                 gd->bd->bi_flashsize >> 20 );
998         video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT * 4,
999                                         info);
1000 #endif
1001
1002         return video_fb_address + VIDEO_LOGO_HEIGHT * VIDEO_LINE_LEN;
1003 }
1004 #endif
1005
1006 /************************************************************************/
1007 /* ** VIDEO HIGH-LEVEL FUNCTIONS                                        */
1008 /************************************************************************/
1009
1010 static int video_init (void *videobase)
1011 {
1012         /* Initialize the encoder */
1013         debug ("[VIDEO] Initializing video encoder...\n");
1014         video_encoder_init ();
1015
1016         /* Initialize the video controller */
1017         debug ("[VIDEO] Initializing video controller at %08x...\n",
1018                    (int) videobase);
1019         video_ctrl_init (videobase);
1020
1021         /* Setting the palette */
1022         video_setpalette  (CONSOLE_COLOR_BLACK,      0,    0,    0);
1023         video_setpalette  (CONSOLE_COLOR_RED,     0xFF,    0,    0);
1024         video_setpalette  (CONSOLE_COLOR_GREEN,      0, 0xFF,    0);
1025         video_setpalette  (CONSOLE_COLOR_YELLOW,  0xFF, 0xFF,    0);
1026         video_setpalette  (CONSOLE_COLOR_BLUE,       0,    0, 0xFF);
1027         video_setpalette  (CONSOLE_COLOR_MAGENTA, 0xFF,    0, 0xFF);
1028         video_setpalette  (CONSOLE_COLOR_CYAN,       0, 0xFF, 0xFF);
1029         video_setpalette  (CONSOLE_COLOR_GREY,    0xAA, 0xAA, 0xAA);
1030         video_setpalette  (CONSOLE_COLOR_GREY2,   0xF8, 0xF8, 0xF8);
1031         video_setpalette  (CONSOLE_COLOR_WHITE,   0xFF, 0xFF, 0xFF);
1032
1033 #ifndef CONFIG_SYS_WHITE_ON_BLACK
1034         video_setfgcolor (CONSOLE_COLOR_BLACK);
1035         video_setbgcolor (CONSOLE_COLOR_GREY2);
1036 #else
1037         video_setfgcolor (CONSOLE_COLOR_GREY2);
1038         video_setbgcolor (CONSOLE_COLOR_BLACK);
1039 #endif  /* CONFIG_SYS_WHITE_ON_BLACK */
1040
1041 #ifdef CONFIG_VIDEO_LOGO
1042         /* Paint the logo and retrieve tv base address */
1043         debug ("[VIDEO] Drawing the logo...\n");
1044         video_console_address = video_logo ();
1045 #else
1046         video_console_address = video_fb_address;
1047 #endif
1048
1049 #ifdef VIDEO_BLINK
1050         /* Enable the blinking (under construction) */
1051         blink_init ();
1052         blink_set (0);                          /* To Fix! */
1053 #endif
1054
1055         /* Initialize the console */
1056         console_col = 0;
1057         console_row = 0;
1058         video_enable = 1;
1059
1060 #ifdef VIDEO_MODE_PAL
1061 # define VIDEO_MODE_TMP1        "PAL"
1062 #endif
1063 #ifdef VIDEO_MODE_NTSC
1064 # define VIDEO_MODE_TMP1        "NTSC"
1065 #endif
1066 #ifdef VIDEO_MODE_YUYV
1067 # define VIDEO_MODE_TMP2        "YCbYCr"
1068 #endif
1069 #ifdef VIDEO_MODE_RGB
1070 # define VIDEO_MODE_TMP2        "RGB"
1071 #endif
1072         debug ( VIDEO_MODE_TMP1
1073                 " %dx%dx%d (" VIDEO_MODE_TMP2 ") on %s - console %dx%d\n",
1074                         VIDEO_COLS, VIDEO_ROWS, VIDEO_MODE_BPP,
1075                         VIDEO_ENCODER_NAME, CONSOLE_COLS, CONSOLE_ROWS);
1076         return 0;
1077 }
1078
1079 int drv_video_init (void)
1080 {
1081         int error, devices = 1;
1082
1083         struct stdio_dev videodev;
1084
1085         video_init ((void *)(gd->fb_base));     /* Video initialization */
1086
1087 /* Device initialization */
1088
1089         memset (&videodev, 0, sizeof (videodev));
1090
1091         strcpy (videodev.name, "video");
1092         videodev.flags = DEV_FLAGS_OUTPUT;      /* Output only */
1093         videodev.putc = video_putc;     /* 'putc' function */
1094         videodev.puts = video_puts;     /* 'puts' function */
1095
1096         error = stdio_register (&videodev);
1097
1098         return (error == 0) ? devices : error;
1099 }
1100
1101 /************************************************************************/
1102 /* ** ROM capable initialization part - needed to reserve FB memory     */
1103 /************************************************************************/
1104
1105 /*
1106  * This is called early in the system initialization to grab memory
1107  * for the video controller.
1108  * Returns new address for monitor, after reserving video buffer memory
1109  *
1110  * Note that this is running from ROM, so no write access to global data.
1111  */
1112 ulong video_setmem (ulong addr)
1113 {
1114         /* Allocate pages for the frame buffer. */
1115         addr -= VIDEO_SIZE;
1116
1117         debug ("Reserving %dk for Video Framebuffer at: %08lx\n",
1118                 VIDEO_SIZE>>10, addr);
1119
1120         return (addr);
1121 }
1122
1123 #endif